secondary cache feature in vm.
[minix.git] / lib / libc / ip / recvfrom.c
blob6595356663e28b228b53ab230dedd2c800cf3d32
1 #undef NDEBUG
3 #include <assert.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <sys/ioctl.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
13 #include <net/gen/in.h>
14 #include <net/gen/tcp.h>
15 #include <net/gen/tcp_io.h>
16 #include <net/gen/udp.h>
17 #include <net/gen/udp_hdr.h>
18 #include <net/gen/udp_io.h>
20 #define DEBUG 0
22 static ssize_t _tcp_recvfrom(int socket, void *_RESTRICT buffer, size_t length,
23 int flags, struct sockaddr *_RESTRICT address,
24 socklen_t *_RESTRICT address_len, nwio_tcpconf_t *tcpconfp);
25 static ssize_t _udp_recvfrom(int socket, void *_RESTRICT buffer, size_t length,
26 int flags, struct sockaddr *_RESTRICT address,
27 socklen_t *_RESTRICT address_len, nwio_udpopt_t *udpoptp);
29 ssize_t recvfrom(int socket, void *_RESTRICT buffer, size_t length,
30 int flags, struct sockaddr *_RESTRICT address,
31 socklen_t *_RESTRICT address_len)
33 int r;
34 nwio_tcpconf_t tcpconf;
35 nwio_udpopt_t udpopt;
37 #if DEBUG
38 fprintf(stderr, "recvfrom: for fd %d\n", socket);
39 #endif
41 r= ioctl(socket, NWIOGTCPCONF, &tcpconf);
42 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
44 if (r == -1)
45 return r;
46 return _tcp_recvfrom(socket, buffer, length, flags,
47 address, address_len, &tcpconf);
50 r= ioctl(socket, NWIOGUDPOPT, &udpopt);
51 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
53 if (r == -1)
54 return r;
55 return _udp_recvfrom(socket, buffer, length, flags,
56 address, address_len, &udpopt);
59 #if DEBUG
60 fprintf(stderr, "recvfrom: not implemented for fd %d\n", socket);
61 #endif
62 errno= ENOSYS;
63 assert(0);
64 return -1;
67 static ssize_t _tcp_recvfrom(int socket, void *_RESTRICT buffer, size_t length,
68 int flags, struct sockaddr *_RESTRICT address,
69 socklen_t *_RESTRICT address_len, nwio_tcpconf_t *tcpconfp)
71 int r;
72 size_t len;
73 struct sockaddr_in sin;
75 if (flags != 0)
77 #if DEBUG
78 fprintf(stderr, "recvfrom(tcp): flags not implemented\n");
79 #endif
80 errno= ENOSYS;
81 return -1;
84 r = read(socket, buffer, length);
86 if (r >= 0 && address != NULL)
88 sin.sin_family= AF_INET;
89 sin.sin_addr.s_addr= tcpconfp->nwtc_remaddr;
90 sin.sin_port= tcpconfp->nwtc_remport;
91 len= *address_len;
92 if (len > sizeof(sin))
93 len= sizeof(sin);
94 memcpy(address, &sin, len);
95 *address_len= sizeof(sin);
98 return r;
101 static ssize_t _udp_recvfrom(int socket, void *_RESTRICT buffer, size_t length,
102 int flags, struct sockaddr *_RESTRICT address,
103 socklen_t *_RESTRICT address_len, nwio_udpopt_t *udpoptp)
105 int r, t_errno;
106 size_t buflen, len;
107 void *buf;
108 udp_io_hdr_t *io_hdrp;
109 struct sockaddr_in sin;
111 if (flags)
113 #if DEBUG
114 fprintf(stderr, "recvfrom(udp): flags not implemented\n");
115 #endif
116 errno= ENOSYS;
117 return -1;
120 if (udpoptp->nwuo_flags & NWUO_RWDATONLY)
122 if (address != NULL &&
123 (udpoptp->nwuo_flags & (NWUO_RA_SET | NWUO_RP_SET)) !=
124 (NWUO_RA_SET | NWUO_RP_SET))
127 #if DEBUG
128 fprintf(stderr,
129 "recvfrom(udp): RWDATONLY on unconnected socket\n");
130 #endif
131 errno= ENOTCONN;
132 return -1;
135 r= read(socket, buffer, length);
136 if (r == -1)
137 return r;
139 if (address != NULL)
141 sin.sin_family= AF_INET;
142 sin.sin_addr.s_addr= udpoptp->nwuo_remaddr;
143 sin.sin_port= udpoptp->nwuo_remport;
144 len= *address_len;
145 if (len > sizeof(sin))
146 len= sizeof(sin);
147 memcpy(address, &sin, len);
148 *address_len= sizeof(sin);
151 return r;
154 buflen= sizeof(*io_hdrp) + length;
155 if (buflen < length)
157 /* Overflow */
158 errno= EMSGSIZE;
159 return -1;
161 buf= malloc(buflen);
162 if (buf == NULL)
163 return -1;
165 r= read(socket, buf, buflen);
166 if (r == -1)
168 t_errno= errno;
169 #if DEBUG
170 fprintf(stderr, "recvfrom(udp): read failed: %s\n",
171 strerror(errno));
172 fprintf(stderr, "udp opt flags = 0x%x\n", udpoptp->nwuo_flags);
173 #endif
174 free(buf);
175 errno= t_errno;
176 return -1;
179 assert(r >= sizeof(*io_hdrp));
180 length= r-sizeof(*io_hdrp);
182 io_hdrp= buf;
183 memcpy(buffer, &io_hdrp[1], length);
185 if (address != NULL)
187 sin.sin_family= AF_INET;
188 sin.sin_addr.s_addr= io_hdrp->uih_src_addr;
189 sin.sin_port= io_hdrp->uih_src_port;
190 len= *address_len;
191 if (len > sizeof(sin))
192 len= sizeof(sin);
193 memcpy(address, &sin, len);
194 *address_len= sizeof(sin);
196 free(buf);
197 return length;