Uninitialized vector entry?
[minix3.git] / lib / ip / accept.c
blobc6fe2ee06766f08d0c52e5eb0584fd6ff7b25b52
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <sys/ioctl.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
10 #include <net/netlib.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>
15 #include <net/gen/udp_io.h>
17 #define DEBUG 0
19 static int _tcp_accept(int socket, struct sockaddr *_RESTRICT address,
20 socklen_t *_RESTRICT address_len);
22 int accept(int socket, struct sockaddr *_RESTRICT address,
23 socklen_t *_RESTRICT address_len)
25 int r;
26 nwio_udpopt_t udpopt;
28 r= _tcp_accept(socket, address, address_len);
29 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
30 return r;
32 /* Unfortunately, we have to return EOPNOTSUPP for a socket that
33 * does not support accept (such as a UDP socket) and ENOTSOCK for
34 * filedescriptors that do not refer to a socket.
36 r= ioctl(socket, NWIOGUDPOPT, &udpopt);
37 if (r == 0)
39 /* UDP socket */
40 errno= EOPNOTSUPP;
41 return -1;
43 if ((errno == ENOTTY || errno == EBADIOCTL))
45 errno= ENOTSOCK;
46 return -1;
49 return r;
52 static int _tcp_accept(int socket, struct sockaddr *_RESTRICT address,
53 socklen_t *_RESTRICT address_len)
55 int r, s1, t_errno;
56 tcp_cookie_t cookie;
58 s1= open(TCP_DEVICE, O_RDWR);
59 if (s1 == -1)
60 return s1;
61 r= ioctl(s1, NWIOGTCPCOOKIE, &cookie);
62 if (r == -1)
64 t_errno= errno;
65 close(s1);
66 errno= t_errno;
67 return -1;
69 r= ioctl(socket, NWIOTCPACCEPTTO, &cookie);
70 if (r == -1)
72 t_errno= errno;
73 close(s1);
74 errno= t_errno;
75 return -1;
77 if (address != NULL)
78 getpeername(s1, address, address_len);
79 return s1;