[9] | 1 | #include <errno.h>
|
---|
| 2 | #include <stdio.h>
|
---|
| 3 | #include <string.h>
|
---|
| 4 | #include <sys/ioctl.h>
|
---|
| 5 | #include <sys/socket.h>
|
---|
| 6 | #include <netinet/in.h>
|
---|
| 7 |
|
---|
| 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>
|
---|
| 13 |
|
---|
| 14 | #define DEBUG 0
|
---|
| 15 |
|
---|
| 16 | static int _tcp_getpeername(int socket, struct sockaddr *_RESTRICT address,
|
---|
| 17 | socklen_t *_RESTRICT address_len, nwio_tcpconf_t *tcpconfp);
|
---|
| 18 |
|
---|
| 19 | int getpeername(int socket, struct sockaddr *_RESTRICT address,
|
---|
| 20 | socklen_t *_RESTRICT address_len)
|
---|
| 21 | {
|
---|
| 22 | int r;
|
---|
| 23 | nwio_tcpconf_t tcpconf;
|
---|
| 24 |
|
---|
| 25 | r= ioctl(socket, NWIOGTCPCONF, &tcpconf);
|
---|
| 26 | if (r != -1 || errno != ENOTTY)
|
---|
| 27 | {
|
---|
| 28 | if (r == -1)
|
---|
| 29 | {
|
---|
| 30 | /* Bad file descriptor */
|
---|
| 31 | return -1;
|
---|
| 32 | }
|
---|
| 33 | return _tcp_getpeername(socket, address, address_len,
|
---|
| 34 | &tcpconf);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | #if DEBUG
|
---|
| 38 | fprintf(stderr, "getpeername: not implemented for fd %d\n", socket);
|
---|
| 39 | #endif
|
---|
| 40 | errno= ENOSYS;
|
---|
| 41 | return -1;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | static int _tcp_getpeername(int socket, struct sockaddr *_RESTRICT address,
|
---|
| 45 | socklen_t *_RESTRICT address_len, nwio_tcpconf_t *tcpconfp)
|
---|
| 46 | {
|
---|
| 47 | socklen_t len;
|
---|
| 48 | struct sockaddr_in sin;
|
---|
| 49 |
|
---|
| 50 | if (tcpconfp->nwtc_remaddr == 0 ||
|
---|
| 51 | tcpconfp->nwtc_remport == 0)
|
---|
| 52 | {
|
---|
| 53 | errno= ENOTCONN;
|
---|
| 54 | return -1;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 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;
|
---|
| 61 |
|
---|
| 62 | len= *address_len;
|
---|
| 63 | if (len > sizeof(sin))
|
---|
| 64 | len= sizeof(sin);
|
---|
| 65 | memcpy(address, &sin, len);
|
---|
| 66 | *address_len= len;
|
---|
| 67 |
|
---|
| 68 | return 0;
|
---|
| 69 | }
|
---|
| 70 |
|
---|