5 #include <sys/socket.h>
6 #include <netinet/in.h>
8 #include <net/gen/in.h>
9 #include <net/gen/tcp.h>
10 #include <net/gen/tcp_io.h>
11 #include <net/gen/udp.h>
12 #include <net/gen/udp_io.h>
16 static int _tcp_getpeername(int socket
, struct sockaddr
*_RESTRICT address
,
17 socklen_t
*_RESTRICT address_len
, nwio_tcpconf_t
*tcpconfp
);
19 int getpeername(int socket
, struct sockaddr
*_RESTRICT address
,
20 socklen_t
*_RESTRICT address_len
)
23 nwio_tcpconf_t tcpconf
;
25 r
= ioctl(socket
, NWIOGTCPCONF
, &tcpconf
);
26 if (r
!= -1 || errno
!= ENOTTY
)
30 /* Bad file descriptor */
33 return _tcp_getpeername(socket
, address
, address_len
,
38 fprintf(stderr
, "getpeername: not implemented for fd %d\n", socket
);
44 static int _tcp_getpeername(int socket
, struct sockaddr
*_RESTRICT address
,
45 socklen_t
*_RESTRICT address_len
, nwio_tcpconf_t
*tcpconfp
)
48 struct sockaddr_in sin
;
50 if (tcpconfp
->nwtc_remaddr
== 0 ||
51 tcpconfp
->nwtc_remport
== 0)
57 memset(&sin
, '\0', sizeof(sin
));
58 sin
.sin_family
= AF_INET
;
59 sin
.sin_addr
.s_addr
= tcpconfp
->nwtc_remaddr
;
60 sin
.sin_port
= tcpconfp
->nwtc_remport
;
63 if (len
> sizeof(sin
))
65 memcpy(address
, &sin
, len
);