8 #include <sys/socket.h>
9 #include <netinet/in.h>
11 #include <net/gen/in.h>
12 #include <net/gen/tcp.h>
13 #include <net/gen/tcp_io.h>
14 #include <net/gen/udp.h>
19 static int _tcp_getpeername(int sock
, struct sockaddr
*__restrict address
,
20 socklen_t
*__restrict address_len
, nwio_tcpconf_t
*tcpconfp
);
22 static int _uds_getpeername(int sock
, struct sockaddr
*__restrict address
,
23 socklen_t
*__restrict address_len
, struct sockaddr_un
*uds_addr
);
25 int getpeername(int sock
, struct sockaddr
*__restrict address
,
26 socklen_t
*__restrict address_len
)
29 nwio_tcpconf_t tcpconf
;
30 struct sockaddr_un uds_addr
;
32 r
= ioctl(sock
, NWIOGTCPCONF
, &tcpconf
);
33 if (r
!= -1 || (errno
!= ENOTTY
&& errno
!= EBADIOCTL
))
37 /* Bad file descriptor */
40 return _tcp_getpeername(sock
, address
, address_len
,
44 r
= ioctl(sock
, NWIOGUDSPADDR
, &uds_addr
);
45 if (r
!= -1 || (errno
!= ENOTTY
&& errno
!= EBADIOCTL
))
49 /* Bad file descriptor */
52 return _uds_getpeername(sock
, address
, address_len
,
58 fprintf(stderr
, "getpeername: not implemented for fd %d\n", sock
);
64 static int _tcp_getpeername(int sock
, struct sockaddr
*__restrict address
,
65 socklen_t
*__restrict address_len
, nwio_tcpconf_t
*tcpconfp
)
68 struct sockaddr_in sin
;
70 if (tcpconfp
->nwtc_remaddr
== 0 ||
71 tcpconfp
->nwtc_remport
== 0)
77 memset(&sin
, '\0', sizeof(sin
));
78 sin
.sin_family
= AF_INET
;
79 sin
.sin_addr
.s_addr
= tcpconfp
->nwtc_remaddr
;
80 sin
.sin_port
= tcpconfp
->nwtc_remport
;
83 if (len
> sizeof(sin
))
85 memcpy(address
, &sin
, len
);
91 static int _uds_getpeername(int sock
, struct sockaddr
*__restrict address
,
92 socklen_t
*__restrict address_len
, struct sockaddr_un
*uds_addr
)
96 if (uds_addr
->sun_family
!= AF_UNIX
)
103 if (len
> sizeof(struct sockaddr_un
))
104 len
= sizeof(struct sockaddr_un
);
106 memcpy(address
, uds_addr
, len
);