xref: /minix3/minix/lib/libc/sys/recvfrom.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include <sys/cdefs.h>
2*433d6423SLionel Sambuc #include "namespace.h"
3*433d6423SLionel Sambuc 
4*433d6423SLionel Sambuc #undef NDEBUG
5*433d6423SLionel Sambuc 
6*433d6423SLionel Sambuc #include <assert.h>
7*433d6423SLionel Sambuc #include <errno.h>
8*433d6423SLionel Sambuc #include <stdlib.h>
9*433d6423SLionel Sambuc #include <stdio.h>
10*433d6423SLionel Sambuc #include <string.h>
11*433d6423SLionel Sambuc #include <unistd.h>
12*433d6423SLionel Sambuc #include <sys/ioctl.h>
13*433d6423SLionel Sambuc #include <sys/socket.h>
14*433d6423SLionel Sambuc #include <netinet/in.h>
15*433d6423SLionel Sambuc 
16*433d6423SLionel Sambuc #include <net/gen/in.h>
17*433d6423SLionel Sambuc #include <net/gen/tcp.h>
18*433d6423SLionel Sambuc #include <net/gen/tcp_io.h>
19*433d6423SLionel Sambuc #include <net/gen/udp.h>
20*433d6423SLionel Sambuc #include <net/gen/udp_hdr.h>
21*433d6423SLionel Sambuc #include <net/gen/udp_io.h>
22*433d6423SLionel Sambuc 
23*433d6423SLionel Sambuc #define DEBUG 0
24*433d6423SLionel Sambuc 
25*433d6423SLionel Sambuc static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length,
26*433d6423SLionel Sambuc 	int flags, struct sockaddr *__restrict address,
27*433d6423SLionel Sambuc 	socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp);
28*433d6423SLionel Sambuc static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length,
29*433d6423SLionel Sambuc 	int flags, struct sockaddr *__restrict address,
30*433d6423SLionel Sambuc 	socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp);
31*433d6423SLionel Sambuc static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer,
32*433d6423SLionel Sambuc 	size_t length, int flags, struct sockaddr *__restrict address,
33*433d6423SLionel Sambuc 	socklen_t *__restrict address_len, struct sockaddr_un *uds_addr);
34*433d6423SLionel Sambuc static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer,
35*433d6423SLionel Sambuc 	size_t length, int flags, struct sockaddr *__restrict address,
36*433d6423SLionel Sambuc 	socklen_t *__restrict address_len);
37*433d6423SLionel Sambuc 
38*433d6423SLionel Sambuc ssize_t recvfrom(int sock, void *__restrict buffer, size_t length,
39*433d6423SLionel Sambuc 	int flags, struct sockaddr *__restrict address,
40*433d6423SLionel Sambuc 	socklen_t *__restrict address_len)
41*433d6423SLionel Sambuc {
42*433d6423SLionel Sambuc 	int r;
43*433d6423SLionel Sambuc 	nwio_tcpconf_t tcpconf;
44*433d6423SLionel Sambuc 	nwio_udpopt_t udpopt;
45*433d6423SLionel Sambuc 	struct sockaddr_un uds_addr;
46*433d6423SLionel Sambuc 	int uds_sotype = -1;
47*433d6423SLionel Sambuc 
48*433d6423SLionel Sambuc #if DEBUG
49*433d6423SLionel Sambuc 	fprintf(stderr, "recvfrom: for fd %d\n", sock);
50*433d6423SLionel Sambuc #endif
51*433d6423SLionel Sambuc 
52*433d6423SLionel Sambuc 	r= ioctl(sock, NWIOGTCPCONF, &tcpconf);
53*433d6423SLionel Sambuc 	if (r != -1 || errno != ENOTTY)
54*433d6423SLionel Sambuc 	{
55*433d6423SLionel Sambuc 		if (r == -1)
56*433d6423SLionel Sambuc 			return r;
57*433d6423SLionel Sambuc 		return _tcp_recvfrom(sock, buffer, length, flags,
58*433d6423SLionel Sambuc 			address, address_len, &tcpconf);
59*433d6423SLionel Sambuc 	}
60*433d6423SLionel Sambuc 
61*433d6423SLionel Sambuc 	r= ioctl(sock, NWIOGUDPOPT, &udpopt);
62*433d6423SLionel Sambuc 	if (r != -1 || errno != ENOTTY)
63*433d6423SLionel Sambuc 	{
64*433d6423SLionel Sambuc 		if (r == -1)
65*433d6423SLionel Sambuc 			return r;
66*433d6423SLionel Sambuc 		return _udp_recvfrom(sock, buffer, length, flags,
67*433d6423SLionel Sambuc 			address, address_len, &udpopt);
68*433d6423SLionel Sambuc 	}
69*433d6423SLionel Sambuc 
70*433d6423SLionel Sambuc 	r= ioctl(sock, NWIOGUDSSOTYPE, &uds_sotype);
71*433d6423SLionel Sambuc 	if (r != -1 || errno != ENOTTY)
72*433d6423SLionel Sambuc 	{
73*433d6423SLionel Sambuc 
74*433d6423SLionel Sambuc 		if (r == -1) {
75*433d6423SLionel Sambuc 			return r;
76*433d6423SLionel Sambuc 		}
77*433d6423SLionel Sambuc 
78*433d6423SLionel Sambuc 		if (uds_sotype == SOCK_DGRAM) {
79*433d6423SLionel Sambuc 			return _uds_recvfrom_dgram(sock, buffer,
80*433d6423SLionel Sambuc 				length, flags, address, address_len);
81*433d6423SLionel Sambuc 		} else {
82*433d6423SLionel Sambuc 			return _uds_recvfrom_conn(sock, buffer,
83*433d6423SLionel Sambuc 				length, flags, address, address_len,
84*433d6423SLionel Sambuc 				&uds_addr);
85*433d6423SLionel Sambuc 		}
86*433d6423SLionel Sambuc 	}
87*433d6423SLionel Sambuc 
88*433d6423SLionel Sambuc #if DEBUG
89*433d6423SLionel Sambuc 	fprintf(stderr, "recvfrom: not implemented for fd %d\n", sock);
90*433d6423SLionel Sambuc #endif
91*433d6423SLionel Sambuc 	errno= ENOSYS;
92*433d6423SLionel Sambuc 	assert(0);
93*433d6423SLionel Sambuc 	return -1;
94*433d6423SLionel Sambuc }
95*433d6423SLionel Sambuc 
96*433d6423SLionel Sambuc static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length,
97*433d6423SLionel Sambuc 	int flags, struct sockaddr *__restrict address,
98*433d6423SLionel Sambuc 	socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp)
99*433d6423SLionel Sambuc {
100*433d6423SLionel Sambuc 	int r;
101*433d6423SLionel Sambuc 	size_t len;
102*433d6423SLionel Sambuc 	struct sockaddr_in sin;
103*433d6423SLionel Sambuc 
104*433d6423SLionel Sambuc 	if (flags != 0)
105*433d6423SLionel Sambuc 	{
106*433d6423SLionel Sambuc #if DEBUG
107*433d6423SLionel Sambuc 		fprintf(stderr, "recvfrom(tcp): flags not implemented\n");
108*433d6423SLionel Sambuc #endif
109*433d6423SLionel Sambuc 		errno= ENOSYS;
110*433d6423SLionel Sambuc 		return -1;
111*433d6423SLionel Sambuc 	}
112*433d6423SLionel Sambuc 
113*433d6423SLionel Sambuc 	r = read(sock, buffer, length);
114*433d6423SLionel Sambuc 
115*433d6423SLionel Sambuc 	if (r >= 0 && address != NULL)
116*433d6423SLionel Sambuc 	{
117*433d6423SLionel Sambuc 		sin.sin_family= AF_INET;
118*433d6423SLionel Sambuc 		sin.sin_addr.s_addr= tcpconfp->nwtc_remaddr;
119*433d6423SLionel Sambuc 		sin.sin_port= tcpconfp->nwtc_remport;
120*433d6423SLionel Sambuc 		sin.sin_len= sizeof(sin);
121*433d6423SLionel Sambuc 		len= *address_len;
122*433d6423SLionel Sambuc 		if (len > sizeof(sin))
123*433d6423SLionel Sambuc 			len= sizeof(sin);
124*433d6423SLionel Sambuc 		memcpy(address, &sin, len);
125*433d6423SLionel Sambuc 		*address_len= sizeof(sin);
126*433d6423SLionel Sambuc 	}
127*433d6423SLionel Sambuc 
128*433d6423SLionel Sambuc 	return r;
129*433d6423SLionel Sambuc }
130*433d6423SLionel Sambuc 
131*433d6423SLionel Sambuc static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length,
132*433d6423SLionel Sambuc 	int flags, struct sockaddr *__restrict address,
133*433d6423SLionel Sambuc 	socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp)
134*433d6423SLionel Sambuc {
135*433d6423SLionel Sambuc 	int r, t_errno;
136*433d6423SLionel Sambuc 	size_t buflen, len;
137*433d6423SLionel Sambuc 	void *buf;
138*433d6423SLionel Sambuc 	udp_io_hdr_t *io_hdrp;
139*433d6423SLionel Sambuc 	struct sockaddr_in sin;
140*433d6423SLionel Sambuc 
141*433d6423SLionel Sambuc 	if (flags)
142*433d6423SLionel Sambuc 	{
143*433d6423SLionel Sambuc #if DEBUG
144*433d6423SLionel Sambuc 		fprintf(stderr, "recvfrom(udp): flags not implemented\n");
145*433d6423SLionel Sambuc #endif
146*433d6423SLionel Sambuc 		errno= ENOSYS;
147*433d6423SLionel Sambuc 		return -1;
148*433d6423SLionel Sambuc 	}
149*433d6423SLionel Sambuc 
150*433d6423SLionel Sambuc 	if (udpoptp->nwuo_flags & NWUO_RWDATONLY)
151*433d6423SLionel Sambuc 	{
152*433d6423SLionel Sambuc 		if (address != NULL &&
153*433d6423SLionel Sambuc 			(udpoptp->nwuo_flags & (NWUO_RA_SET | NWUO_RP_SET)) !=
154*433d6423SLionel Sambuc 			(NWUO_RA_SET | NWUO_RP_SET))
155*433d6423SLionel Sambuc 		{
156*433d6423SLionel Sambuc 
157*433d6423SLionel Sambuc #if DEBUG
158*433d6423SLionel Sambuc 			fprintf(stderr,
159*433d6423SLionel Sambuc 			"recvfrom(udp): RWDATONLY on unconnected socket\n");
160*433d6423SLionel Sambuc #endif
161*433d6423SLionel Sambuc 			errno= ENOTCONN;
162*433d6423SLionel Sambuc 			return -1;
163*433d6423SLionel Sambuc 		}
164*433d6423SLionel Sambuc 
165*433d6423SLionel Sambuc 		r= read(sock, buffer, length);
166*433d6423SLionel Sambuc 		if (r == -1)
167*433d6423SLionel Sambuc 			return r;
168*433d6423SLionel Sambuc 
169*433d6423SLionel Sambuc 		if (address != NULL)
170*433d6423SLionel Sambuc 		{
171*433d6423SLionel Sambuc 			sin.sin_family= AF_INET;
172*433d6423SLionel Sambuc 			sin.sin_addr.s_addr= udpoptp->nwuo_remaddr;
173*433d6423SLionel Sambuc 			sin.sin_port= udpoptp->nwuo_remport;
174*433d6423SLionel Sambuc 			sin.sin_len= sizeof(sin);
175*433d6423SLionel Sambuc 			len= *address_len;
176*433d6423SLionel Sambuc 			if (len > sizeof(sin))
177*433d6423SLionel Sambuc 				len= sizeof(sin);
178*433d6423SLionel Sambuc 			memcpy(address, &sin, len);
179*433d6423SLionel Sambuc 			*address_len= sizeof(sin);
180*433d6423SLionel Sambuc 		}
181*433d6423SLionel Sambuc 
182*433d6423SLionel Sambuc 		return r;
183*433d6423SLionel Sambuc 	}
184*433d6423SLionel Sambuc 
185*433d6423SLionel Sambuc 	buflen= sizeof(*io_hdrp) + length;
186*433d6423SLionel Sambuc 	if (buflen < length)
187*433d6423SLionel Sambuc 	{
188*433d6423SLionel Sambuc 		/* Overflow */
189*433d6423SLionel Sambuc 		errno= EMSGSIZE;
190*433d6423SLionel Sambuc 		return -1;
191*433d6423SLionel Sambuc 	}
192*433d6423SLionel Sambuc 	buf= malloc(buflen);
193*433d6423SLionel Sambuc 	if (buf == NULL)
194*433d6423SLionel Sambuc 		return -1;
195*433d6423SLionel Sambuc 
196*433d6423SLionel Sambuc 	r= read(sock, buf, buflen);
197*433d6423SLionel Sambuc 	if (r == -1)
198*433d6423SLionel Sambuc 	{
199*433d6423SLionel Sambuc 		t_errno= errno;
200*433d6423SLionel Sambuc #if DEBUG
201*433d6423SLionel Sambuc 		fprintf(stderr, "recvfrom(udp): read failed: %s\n",
202*433d6423SLionel Sambuc 			strerror(errno));
203*433d6423SLionel Sambuc 		fprintf(stderr, "udp opt flags = 0x%x\n", udpoptp->nwuo_flags);
204*433d6423SLionel Sambuc #endif
205*433d6423SLionel Sambuc 		free(buf);
206*433d6423SLionel Sambuc 		errno= t_errno;
207*433d6423SLionel Sambuc 		return -1;
208*433d6423SLionel Sambuc 	}
209*433d6423SLionel Sambuc 
210*433d6423SLionel Sambuc 	assert(r >= sizeof(*io_hdrp));
211*433d6423SLionel Sambuc 	length= r-sizeof(*io_hdrp);
212*433d6423SLionel Sambuc 
213*433d6423SLionel Sambuc 	io_hdrp= buf;
214*433d6423SLionel Sambuc 	memcpy(buffer, &io_hdrp[1], length);
215*433d6423SLionel Sambuc 
216*433d6423SLionel Sambuc 	if (address != NULL)
217*433d6423SLionel Sambuc 	{
218*433d6423SLionel Sambuc 		sin.sin_family= AF_INET;
219*433d6423SLionel Sambuc 		sin.sin_addr.s_addr= io_hdrp->uih_src_addr;
220*433d6423SLionel Sambuc 		sin.sin_port= io_hdrp->uih_src_port;
221*433d6423SLionel Sambuc 		sin.sin_len= sizeof(sin);
222*433d6423SLionel Sambuc 		len= *address_len;
223*433d6423SLionel Sambuc 		if (len > sizeof(sin))
224*433d6423SLionel Sambuc 			len= sizeof(sin);
225*433d6423SLionel Sambuc 		memcpy(address, &sin, len);
226*433d6423SLionel Sambuc 		*address_len= sizeof(sin);
227*433d6423SLionel Sambuc 	}
228*433d6423SLionel Sambuc 	free(buf);
229*433d6423SLionel Sambuc 	return length;
230*433d6423SLionel Sambuc }
231*433d6423SLionel Sambuc 
232*433d6423SLionel Sambuc static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer,
233*433d6423SLionel Sambuc 	size_t length, int flags, struct sockaddr *__restrict address,
234*433d6423SLionel Sambuc 	socklen_t *__restrict address_len, struct sockaddr_un *uds_addr)
235*433d6423SLionel Sambuc {
236*433d6423SLionel Sambuc 	int r;
237*433d6423SLionel Sambuc 	size_t len;
238*433d6423SLionel Sambuc 
239*433d6423SLionel Sambuc 	/* for connection oriented unix domain sockets (SOCK_STREAM /
240*433d6423SLionel Sambuc 	 * SOCK_SEQPACKET)
241*433d6423SLionel Sambuc 	 */
242*433d6423SLionel Sambuc 
243*433d6423SLionel Sambuc 	if (flags != 0)
244*433d6423SLionel Sambuc 	{
245*433d6423SLionel Sambuc #if DEBUG
246*433d6423SLionel Sambuc 		fprintf(stderr, "recvfrom(uds): flags not implemented\n");
247*433d6423SLionel Sambuc #endif
248*433d6423SLionel Sambuc 		errno= ENOSYS;
249*433d6423SLionel Sambuc 		return -1;
250*433d6423SLionel Sambuc 	}
251*433d6423SLionel Sambuc 
252*433d6423SLionel Sambuc 	r = read(sock, buffer, length);
253*433d6423SLionel Sambuc 
254*433d6423SLionel Sambuc 	if (r >= 0 && address != NULL)
255*433d6423SLionel Sambuc 	{
256*433d6423SLionel Sambuc 
257*433d6423SLionel Sambuc 		len= *address_len;
258*433d6423SLionel Sambuc 		if (len > sizeof(struct sockaddr_un))
259*433d6423SLionel Sambuc 			len= sizeof(struct sockaddr_un);
260*433d6423SLionel Sambuc 		memcpy(address, uds_addr, len);
261*433d6423SLionel Sambuc 		*address_len= sizeof(struct sockaddr_un);
262*433d6423SLionel Sambuc 	}
263*433d6423SLionel Sambuc 
264*433d6423SLionel Sambuc 	return r;
265*433d6423SLionel Sambuc }
266*433d6423SLionel Sambuc 
267*433d6423SLionel Sambuc static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer,
268*433d6423SLionel Sambuc 	size_t length, int flags, struct sockaddr *__restrict address,
269*433d6423SLionel Sambuc 	socklen_t *__restrict address_len)
270*433d6423SLionel Sambuc {
271*433d6423SLionel Sambuc 	int r;
272*433d6423SLionel Sambuc 	size_t len;
273*433d6423SLionel Sambuc 
274*433d6423SLionel Sambuc 	/* for connectionless unix domain sockets (SOCK_DGRAM) */
275*433d6423SLionel Sambuc 
276*433d6423SLionel Sambuc 	if (flags != 0)
277*433d6423SLionel Sambuc 	{
278*433d6423SLionel Sambuc #if DEBUG
279*433d6423SLionel Sambuc 		fprintf(stderr, "recvfrom(uds): flags not implemented\n");
280*433d6423SLionel Sambuc #endif
281*433d6423SLionel Sambuc 		errno= ENOSYS;
282*433d6423SLionel Sambuc 		return -1;
283*433d6423SLionel Sambuc 	}
284*433d6423SLionel Sambuc 
285*433d6423SLionel Sambuc 	r = read(sock, buffer, length);
286*433d6423SLionel Sambuc 
287*433d6423SLionel Sambuc 	if (r >= 0 && address != NULL)
288*433d6423SLionel Sambuc 	{
289*433d6423SLionel Sambuc 		len= *address_len;
290*433d6423SLionel Sambuc 		if (len > sizeof(struct sockaddr_un))
291*433d6423SLionel Sambuc 			len= sizeof(struct sockaddr_un);
292*433d6423SLionel Sambuc 		ioctl(sock, NWIOGUDSFADDR, address);
293*433d6423SLionel Sambuc 		*address_len= sizeof(struct sockaddr_un);
294*433d6423SLionel Sambuc 	}
295*433d6423SLionel Sambuc 
296*433d6423SLionel Sambuc 	return r;
297*433d6423SLionel Sambuc }
298*433d6423SLionel Sambuc 
299