netbsd ping & traceroute
[minix3.git] / minix / lib / libc / sys / recvfrom.c
blob01d3e48c9ea5e3e6a035966f633ec1c5dac9876f
1 #include <sys/cdefs.h>
2 #include "namespace.h"
4 #undef NDEBUG
6 #include <assert.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
16 #include <net/gen/in.h>
17 #include <net/gen/tcp.h>
18 #include <net/gen/tcp_io.h>
19 #include <net/gen/udp.h>
20 #include <net/gen/udp_hdr.h>
21 #include <net/gen/udp_io.h>
23 #include <net/gen/ip_hdr.h>
24 #include <net/gen/icmp_hdr.h>
26 #define DEBUG 0
28 static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length,
29 int flags, struct sockaddr *__restrict address,
30 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp);
31 static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length,
32 int flags, struct sockaddr *__restrict address,
33 socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp);
34 static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer,
35 size_t length, int flags, struct sockaddr *__restrict address,
36 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr);
37 static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer,
38 size_t length, int flags, struct sockaddr *__restrict address,
39 socklen_t *__restrict address_len);
41 ssize_t recvfrom(int sock, void *__restrict buffer, size_t length,
42 int flags, struct sockaddr *__restrict address,
43 socklen_t *__restrict address_len)
45 int r;
46 nwio_tcpconf_t tcpconf;
47 nwio_udpopt_t udpopt;
48 struct sockaddr_un uds_addr;
49 int uds_sotype = -1;
51 #if DEBUG
52 fprintf(stderr, "recvfrom: for fd %d\n", sock);
53 #endif
55 r= ioctl(sock, NWIOGTCPCONF, &tcpconf);
56 if (r != -1 || errno != ENOTTY)
58 if (r == -1)
59 return r;
60 return _tcp_recvfrom(sock, buffer, length, flags,
61 address, address_len, &tcpconf);
64 r= ioctl(sock, NWIOGUDPOPT, &udpopt);
65 if (r != -1 || errno != ENOTTY)
67 if (r == -1)
68 return r;
69 return _udp_recvfrom(sock, buffer, length, flags,
70 address, address_len, &udpopt);
73 r= ioctl(sock, NWIOGUDSSOTYPE, &uds_sotype);
74 if (r != -1 || errno != ENOTTY)
77 if (r == -1) {
78 return r;
81 if (uds_sotype == SOCK_DGRAM) {
82 return _uds_recvfrom_dgram(sock, buffer,
83 length, flags, address, address_len);
84 } else {
85 return _uds_recvfrom_conn(sock, buffer,
86 length, flags, address, address_len,
87 &uds_addr);
92 ip_hdr_t *ip_hdr;
93 int ihl, rd;
94 icmp_hdr_t *icmp_hdr;
95 struct sockaddr_in sin;
97 rd = read(sock, buffer, length);
99 if(rd < 0) return rd;
101 assert(rd >= sizeof(*ip_hdr));
103 ip_hdr= buffer;
105 if (address != NULL)
107 int len;
108 memset(&sin, 0, sizeof(sin));
109 sin.sin_family= AF_INET;
110 sin.sin_addr.s_addr= ip_hdr->ih_src;
111 sin.sin_len= sizeof(sin);
112 len= *address_len;
113 if (len > sizeof(sin))
114 len= sizeof(sin);
115 memcpy(address, &sin, len);
116 *address_len= sizeof(sin);
119 return rd;
122 #if DEBUG
123 fprintf(stderr, "recvfrom: not implemented for fd %d\n", sock);
124 #endif
125 errno= ENOSYS;
126 assert(0);
127 return -1;
130 static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length,
131 int flags, struct sockaddr *__restrict address,
132 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp)
134 int r;
135 size_t len;
136 struct sockaddr_in sin;
138 if (flags != 0)
140 #if DEBUG
141 fprintf(stderr, "recvfrom(tcp): flags not implemented\n");
142 #endif
143 errno= ENOSYS;
144 return -1;
147 r = read(sock, buffer, length);
149 if (r >= 0 && address != NULL)
151 sin.sin_family= AF_INET;
152 sin.sin_addr.s_addr= tcpconfp->nwtc_remaddr;
153 sin.sin_port= tcpconfp->nwtc_remport;
154 sin.sin_len= sizeof(sin);
155 len= *address_len;
156 if (len > sizeof(sin))
157 len= sizeof(sin);
158 memcpy(address, &sin, len);
159 *address_len= sizeof(sin);
162 return r;
165 static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length,
166 int flags, struct sockaddr *__restrict address,
167 socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp)
169 int r, t_errno;
170 size_t buflen, len;
171 void *buf;
172 udp_io_hdr_t *io_hdrp;
173 struct sockaddr_in sin;
175 if (flags)
177 #if DEBUG
178 fprintf(stderr, "recvfrom(udp): flags not implemented\n");
179 #endif
180 errno= ENOSYS;
181 return -1;
184 if (udpoptp->nwuo_flags & NWUO_RWDATONLY)
186 if (address != NULL &&
187 (udpoptp->nwuo_flags & (NWUO_RA_SET | NWUO_RP_SET)) !=
188 (NWUO_RA_SET | NWUO_RP_SET))
191 #if DEBUG
192 fprintf(stderr,
193 "recvfrom(udp): RWDATONLY on unconnected socket\n");
194 #endif
195 errno= ENOTCONN;
196 return -1;
199 r= read(sock, buffer, length);
200 if (r == -1)
201 return r;
203 if (address != NULL)
205 sin.sin_family= AF_INET;
206 sin.sin_addr.s_addr= udpoptp->nwuo_remaddr;
207 sin.sin_port= udpoptp->nwuo_remport;
208 sin.sin_len= sizeof(sin);
209 len= *address_len;
210 if (len > sizeof(sin))
211 len= sizeof(sin);
212 memcpy(address, &sin, len);
213 *address_len= sizeof(sin);
216 return r;
219 buflen= sizeof(*io_hdrp) + length;
220 if (buflen < length)
222 /* Overflow */
223 errno= EMSGSIZE;
224 return -1;
226 buf= malloc(buflen);
227 if (buf == NULL)
228 return -1;
230 r= read(sock, buf, buflen);
231 if (r == -1)
233 t_errno= errno;
234 #if DEBUG
235 fprintf(stderr, "recvfrom(udp): read failed: %s\n",
236 strerror(errno));
237 fprintf(stderr, "udp opt flags = 0x%x\n", udpoptp->nwuo_flags);
238 #endif
239 free(buf);
240 errno= t_errno;
241 return -1;
244 assert(r >= sizeof(*io_hdrp));
245 length= r-sizeof(*io_hdrp);
247 io_hdrp= buf;
248 memcpy(buffer, &io_hdrp[1], length);
250 if (address != NULL)
252 sin.sin_family= AF_INET;
253 sin.sin_addr.s_addr= io_hdrp->uih_src_addr;
254 sin.sin_port= io_hdrp->uih_src_port;
255 sin.sin_len= sizeof(sin);
256 len= *address_len;
257 if (len > sizeof(sin))
258 len= sizeof(sin);
259 memcpy(address, &sin, len);
260 *address_len= sizeof(sin);
262 free(buf);
263 return length;
266 static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer,
267 size_t length, int flags, struct sockaddr *__restrict address,
268 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr)
270 int r;
271 size_t len;
273 /* for connection oriented unix domain sockets (SOCK_STREAM /
274 * SOCK_SEQPACKET)
277 if (flags != 0)
279 #if DEBUG
280 fprintf(stderr, "recvfrom(uds): flags not implemented\n");
281 #endif
282 errno= ENOSYS;
283 return -1;
286 r = read(sock, buffer, length);
288 if (r >= 0 && address != NULL)
291 len= *address_len;
292 if (len > sizeof(struct sockaddr_un))
293 len= sizeof(struct sockaddr_un);
294 memcpy(address, uds_addr, len);
295 *address_len= sizeof(struct sockaddr_un);
298 return r;
301 static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer,
302 size_t length, int flags, struct sockaddr *__restrict address,
303 socklen_t *__restrict address_len)
305 int r;
306 size_t len;
308 /* for connectionless unix domain sockets (SOCK_DGRAM) */
310 if (flags != 0)
312 #if DEBUG
313 fprintf(stderr, "recvfrom(uds): flags not implemented\n");
314 #endif
315 errno= ENOSYS;
316 return -1;
319 r = read(sock, buffer, length);
321 if (r >= 0 && address != NULL)
323 len= *address_len;
324 if (len > sizeof(struct sockaddr_un))
325 len= sizeof(struct sockaddr_un);
326 ioctl(sock, NWIOGUDSFADDR, address);
327 *address_len= sizeof(struct sockaddr_un);
330 return r;