5 from socket emulation library for Minix 2.0.x
10 #include "namespace.h"
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
18 #include <net/gen/in.h>
19 #include <net/gen/tcp.h>
20 #include <net/gen/tcp_io.h>
21 #include <net/gen/udp.h>
28 static int _tcp_getsockname(int fd
, struct sockaddr
*__restrict address
,
29 socklen_t
*__restrict address_len
, nwio_tcpconf_t
*tcpconfp
);
31 static int _uds_getsockname(int fd
, struct sockaddr
*__restrict address
,
32 socklen_t
*__restrict address_len
, struct sockaddr_un
*uds_addr
);
34 int getsockname(int fd
, struct sockaddr
*__restrict address
,
35 socklen_t
*__restrict address_len
)
38 nwio_tcpconf_t tcpconf
;
39 struct sockaddr_un uds_addr
;
42 fprintf(stderr
,"mnx_getsockname: ioctl fd %d.\n", fd
);
45 r
= ioctl(fd
, NWIOGTCPCONF
, &tcpconf
);
46 if (r
!= -1 || (errno
!= ENOTTY
&& errno
!= EBADIOCTL
))
50 /* Bad file descriptor */
54 return _tcp_getsockname(fd
, address
, address_len
, &tcpconf
);
57 r
= ioctl(fd
, NWIOGUDSADDR
, &uds_addr
);
58 if (r
!= -1 || (errno
!= ENOTTY
&& errno
!= EBADIOCTL
))
62 /* Bad file descriptor */
66 return _uds_getsockname(fd
, address
, address_len
, &uds_addr
);
70 fprintf(stderr
, "getsockname: not implemented for fd %d\n", socket
);
78 static int _tcp_getsockname(int fd
, struct sockaddr
*__restrict address
,
79 socklen_t
*__restrict address_len
, nwio_tcpconf_t
*tcpconf
)
82 struct sockaddr_in sin
;
85 fprintf(stderr
, "mnx_getsockname: from %s, %u",
86 inet_ntoa(tcpconf
.nwtc_remaddr
),
87 ntohs(tcpconf
.nwtc_remport
));
88 fprintf(stderr
," for %s, %u\n",
89 inet_ntoa(tcpconf
.nwtc_locaddr
),
90 ntohs(tcpconf
.nwtc_locport
));
93 memset(&sin
, '\0', sizeof(sin
));
94 sin
.sin_family
= AF_INET
;
95 sin
.sin_addr
.s_addr
= tcpconf
->nwtc_locaddr
;
96 sin
.sin_port
= tcpconf
->nwtc_locport
;
99 if (len
> sizeof(sin
))
101 memcpy(address
, &sin
, len
);
107 static int _uds_getsockname(int fd
, struct sockaddr
*__restrict address
,
108 socklen_t
*__restrict address_len
, struct sockaddr_un
*uds_addr
)
112 if (uds_addr
->sun_family
!= AF_UNIX
)
119 if (len
> sizeof(struct sockaddr_un
))
120 len
= sizeof(struct sockaddr_un
);
122 memcpy(address
, uds_addr
, len
);