. pci driver now returns devices, even when they have been pci_reserve()d
[minix3.git] / lib / ip / getpeername.c
blob39804ff4121a9bb285a6611d98fce57173edcf51
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>
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>
14 #define DEBUG 0
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)
22 int r;
23 nwio_tcpconf_t tcpconf;
25 r= ioctl(socket, NWIOGTCPCONF, &tcpconf);
26 if (r != -1 || errno != ENOTTY)
28 if (r == -1)
30 /* Bad file descriptor */
31 return -1;
33 return _tcp_getpeername(socket, address, address_len,
34 &tcpconf);
37 #if DEBUG
38 fprintf(stderr, "getpeername: not implemented for fd %d\n", socket);
39 #endif
40 errno= ENOSYS;
41 return -1;
44 static int _tcp_getpeername(int socket, struct sockaddr *_RESTRICT address,
45 socklen_t *_RESTRICT address_len, nwio_tcpconf_t *tcpconfp)
47 socklen_t len;
48 struct sockaddr_in sin;
50 if (tcpconfp->nwtc_remaddr == 0 ||
51 tcpconfp->nwtc_remport == 0)
53 errno= ENOTCONN;
54 return -1;
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;
62 len= *address_len;
63 if (len > sizeof(sin))
64 len= sizeof(sin);
65 memcpy(address, &sin, len);
66 *address_len= len;
68 return 0;