secondary cache feature in vm.
[minix.git] / lib / libc / ip / accept.c
blob8f1085ce72399f9e056b7d263bfcb445876043d3
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>
9 #include <net/netlib.h>
10 #include <net/gen/in.h>
11 #include <net/gen/tcp.h>
12 #include <net/gen/tcp_io.h>
13 #include <net/gen/udp.h>
14 #include <net/gen/udp_io.h>
16 #define DEBUG 0
18 static int _tcp_accept(int socket, struct sockaddr *_RESTRICT address,
19 socklen_t *_RESTRICT address_len);
21 int accept(int socket, struct sockaddr *_RESTRICT address,
22 socklen_t *_RESTRICT address_len)
24 int r;
25 nwio_udpopt_t udpopt;
27 r= _tcp_accept(socket, address, address_len);
28 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
29 return r;
31 /* Unfortunately, we have to return EOPNOTSUPP for a socket that
32 * does not support accept (such as a UDP socket) and ENOTSOCK for
33 * filedescriptors that do not refer to a socket.
35 r= ioctl(socket, NWIOGUDPOPT, &udpopt);
36 if (r == 0)
38 /* UDP socket */
39 errno= EOPNOTSUPP;
40 return -1;
42 if ((errno == ENOTTY || errno == EBADIOCTL))
44 errno= ENOTSOCK;
45 return -1;
48 return r;
51 static int _tcp_accept(int socket, struct sockaddr *_RESTRICT address,
52 socklen_t *_RESTRICT address_len)
54 int r, s1, t_errno;
55 tcp_cookie_t cookie;
57 s1= open(TCP_DEVICE, O_RDWR);
58 if (s1 == -1)
59 return s1;
60 r= ioctl(s1, NWIOGTCPCOOKIE, &cookie);
61 if (r == -1)
63 t_errno= errno;
64 close(s1);
65 errno= t_errno;
66 return -1;
68 r= ioctl(socket, NWIOTCPACCEPTTO, &cookie);
69 if (r == -1)
71 t_errno= errno;
72 close(s1);
73 errno= t_errno;
74 return -1;
76 if (address != NULL)
77 getpeername(s1, address, address_len);
78 return s1;