xref: /minix3/minix/lib/libc/sys/sendto.c (revision 53d2fa057ee88a709bcc793d3c777acad2c2a2ee)
1433d6423SLionel Sambuc #include <sys/cdefs.h>
2433d6423SLionel Sambuc #include "namespace.h"
3c38dbb97SDavid van Moolenbroek #include <lib.h>
4433d6423SLionel Sambuc 
5433d6423SLionel Sambuc #include <assert.h>
6433d6423SLionel Sambuc #include <errno.h>
7433d6423SLionel Sambuc #include <stdio.h>
8433d6423SLionel Sambuc #include <stdlib.h>
9433d6423SLionel Sambuc #include <string.h>
10433d6423SLionel Sambuc #include <unistd.h>
11433d6423SLionel Sambuc #include <sys/ioctl.h>
12433d6423SLionel Sambuc #include <sys/socket.h>
13433d6423SLionel Sambuc #include <netinet/in.h>
14433d6423SLionel Sambuc 
15433d6423SLionel Sambuc #include <net/gen/in.h>
167f5f010bSBen Gras #include <net/gen/ip_hdr.h>
1717580212SDavid van Moolenbroek #include <net/gen/ip_io.h>
18433d6423SLionel Sambuc #include <net/gen/tcp.h>
19433d6423SLionel Sambuc #include <net/gen/tcp_io.h>
20433d6423SLionel Sambuc #include <net/gen/udp.h>
21433d6423SLionel Sambuc #include <net/gen/udp_hdr.h>
22433d6423SLionel Sambuc #include <net/gen/udp_io.h>
23433d6423SLionel Sambuc 
24433d6423SLionel Sambuc #define DEBUG 0
25433d6423SLionel Sambuc 
26433d6423SLionel Sambuc static ssize_t _tcp_sendto(int sock, const void *message, size_t length,
27433d6423SLionel Sambuc 	int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
28433d6423SLionel Sambuc static ssize_t _udp_sendto(int sock, const void *message, size_t length,
29433d6423SLionel Sambuc 	int flags, const struct sockaddr *dest_addr, socklen_t dest_len,
30433d6423SLionel Sambuc 	nwio_udpopt_t *udpoptp);
31433d6423SLionel Sambuc static ssize_t _uds_sendto_conn(int sock, const void *message, size_t length,
32433d6423SLionel Sambuc 	int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
33433d6423SLionel Sambuc static ssize_t _uds_sendto_dgram(int sock, const void *message, size_t length,
34433d6423SLionel Sambuc 	int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
35433d6423SLionel Sambuc 
36c38dbb97SDavid van Moolenbroek /*
37c38dbb97SDavid van Moolenbroek  * Send a message on a socket.
38c38dbb97SDavid van Moolenbroek  */
39c38dbb97SDavid van Moolenbroek static ssize_t
__sendto(int fd,const void * buffer,size_t length,int flags,const struct sockaddr * dest_addr,socklen_t dest_len)40c38dbb97SDavid van Moolenbroek __sendto(int fd, const void * buffer, size_t length, int flags,
41c38dbb97SDavid van Moolenbroek 	const struct sockaddr * dest_addr, socklen_t dest_len)
42c38dbb97SDavid van Moolenbroek {
43c38dbb97SDavid van Moolenbroek 	message m;
44c38dbb97SDavid van Moolenbroek 
45c38dbb97SDavid van Moolenbroek 	memset(&m, 0, sizeof(m));
46c38dbb97SDavid van Moolenbroek 	m.m_lc_vfs_sendrecv.fd = fd;
47c38dbb97SDavid van Moolenbroek 	m.m_lc_vfs_sendrecv.buf = (vir_bytes)buffer;
48c38dbb97SDavid van Moolenbroek 	m.m_lc_vfs_sendrecv.len = length;
49c38dbb97SDavid van Moolenbroek 	m.m_lc_vfs_sendrecv.flags = flags;
50c38dbb97SDavid van Moolenbroek 	m.m_lc_vfs_sendrecv.addr = (vir_bytes)dest_addr;
51c38dbb97SDavid van Moolenbroek 	m.m_lc_vfs_sendrecv.addr_len = dest_len;
52c38dbb97SDavid van Moolenbroek 
53c38dbb97SDavid van Moolenbroek 	return _syscall(VFS_PROC_NR, VFS_SENDTO, &m);
54c38dbb97SDavid van Moolenbroek }
55c38dbb97SDavid van Moolenbroek 
sendto(int sock,const void * message,size_t length,int flags,const struct sockaddr * dest_addr,socklen_t dest_len)56433d6423SLionel Sambuc ssize_t sendto(int sock, const void *message, size_t length, int flags,
57433d6423SLionel Sambuc 	const struct sockaddr *dest_addr, socklen_t dest_len)
58433d6423SLionel Sambuc {
59433d6423SLionel Sambuc 	int r;
60433d6423SLionel Sambuc 	nwio_tcpopt_t tcpopt;
61433d6423SLionel Sambuc 	nwio_udpopt_t udpopt;
6217580212SDavid van Moolenbroek 	nwio_ipopt_t ipopt;
63433d6423SLionel Sambuc 	int uds_sotype = -1;
64433d6423SLionel Sambuc 
65c38dbb97SDavid van Moolenbroek 	r = __sendto(sock, message, length, flags, dest_addr, dest_len);
6684ed480eSDavid van Moolenbroek 	if (r != -1 || (errno != ENOTSOCK && errno != ENOSYS))
67c38dbb97SDavid van Moolenbroek 		return r;
68c38dbb97SDavid van Moolenbroek 
69*53d2fa05SDavid van Moolenbroek 	/* For old socket driver implementations, this flag is the default. */
70*53d2fa05SDavid van Moolenbroek 	flags &= ~MSG_NOSIGNAL;
71*53d2fa05SDavid van Moolenbroek 
72433d6423SLionel Sambuc 	r= ioctl(sock, NWIOGTCPOPT, &tcpopt);
73433d6423SLionel Sambuc 	if (r != -1 || errno != ENOTTY)
74433d6423SLionel Sambuc 	{
75433d6423SLionel Sambuc 		if (r == -1)
76433d6423SLionel Sambuc 			return r;
77433d6423SLionel Sambuc 		return _tcp_sendto(sock, message, length, flags,
78433d6423SLionel Sambuc 			dest_addr, dest_len);
79433d6423SLionel Sambuc 	}
80433d6423SLionel Sambuc 
81433d6423SLionel Sambuc 	r= ioctl(sock, NWIOGUDPOPT, &udpopt);
82433d6423SLionel Sambuc 	if (r != -1 || errno != ENOTTY)
83433d6423SLionel Sambuc 	{
84433d6423SLionel Sambuc 		if (r == -1)
85433d6423SLionel Sambuc 			return r;
86433d6423SLionel Sambuc 		return _udp_sendto(sock, message, length, flags,
87433d6423SLionel Sambuc 			dest_addr, dest_len, &udpopt);
88433d6423SLionel Sambuc 	}
89433d6423SLionel Sambuc 
90433d6423SLionel Sambuc 	r= ioctl(sock, NWIOGUDSSOTYPE, &uds_sotype);
91433d6423SLionel Sambuc 	if (r != -1 || errno != ENOTTY)
92433d6423SLionel Sambuc 	{
93433d6423SLionel Sambuc 		if (r == -1) {
94433d6423SLionel Sambuc 			return r;
95433d6423SLionel Sambuc 		}
96433d6423SLionel Sambuc 
97433d6423SLionel Sambuc 		if (uds_sotype == SOCK_DGRAM) {
98433d6423SLionel Sambuc 
99433d6423SLionel Sambuc 			return _uds_sendto_dgram(sock, message,
100433d6423SLionel Sambuc 				length, flags,dest_addr, dest_len);
101433d6423SLionel Sambuc 		} else {
102433d6423SLionel Sambuc 
103433d6423SLionel Sambuc 			return _uds_sendto_conn(sock, message,
104433d6423SLionel Sambuc 				length, flags, dest_addr, dest_len);
105433d6423SLionel Sambuc 		}
106433d6423SLionel Sambuc 	}
107433d6423SLionel Sambuc 
10817580212SDavid van Moolenbroek 	r= ioctl(sock, NWIOGIPOPT, &ipopt);
10917580212SDavid van Moolenbroek 	if (r != -1 || errno != ENOTTY)
1107f5f010bSBen Gras 	{
1117f5f010bSBen Gras 		ip_hdr_t *ip_hdr;
1127c48de6cSDavid van Moolenbroek 		const struct sockaddr_in *sinp;
1137c48de6cSDavid van Moolenbroek 		ssize_t retval;
1147c48de6cSDavid van Moolenbroek 		int saved_errno;
1157f5f010bSBen Gras 
11617580212SDavid van Moolenbroek 		if (r == -1) {
11717580212SDavid van Moolenbroek 			return r;
11817580212SDavid van Moolenbroek 		}
11917580212SDavid van Moolenbroek 
1207c48de6cSDavid van Moolenbroek 		sinp = (const struct sockaddr_in *)dest_addr;
1217f5f010bSBen Gras 		if (sinp->sin_family != AF_INET)
1227f5f010bSBen Gras 		{
1237f5f010bSBen Gras 			errno= EAFNOSUPPORT;
1247f5f010bSBen Gras 			return -1;
1257f5f010bSBen Gras 		}
1267f5f010bSBen Gras 
1277f5f010bSBen Gras 		/* raw */
1287c48de6cSDavid van Moolenbroek 		/* XXX this is horrible: we have to copy the entire buffer
1297c48de6cSDavid van Moolenbroek 		 * because we have to change one header field. Obviously we
1307c48de6cSDavid van Moolenbroek 		 * can't modify the user buffer directly..
1317c48de6cSDavid van Moolenbroek 		 */
1327c48de6cSDavid van Moolenbroek 		if ((ip_hdr = malloc(length)) == NULL)
1337c48de6cSDavid van Moolenbroek 			return -1; /* errno is ENOMEM */
1347c48de6cSDavid van Moolenbroek 		memcpy(ip_hdr, message, length);
1357f5f010bSBen Gras 		ip_hdr->ih_dst= sinp->sin_addr.s_addr;
1367f5f010bSBen Gras 
1377c48de6cSDavid van Moolenbroek 		retval = write(sock, ip_hdr, length);
1387c48de6cSDavid van Moolenbroek 
1397c48de6cSDavid van Moolenbroek 		saved_errno = errno;
1407c48de6cSDavid van Moolenbroek 		free(ip_hdr);
1417c48de6cSDavid van Moolenbroek 		errno = saved_errno;
1427c48de6cSDavid van Moolenbroek 		return retval;
1437f5f010bSBen Gras 	}
1447f5f010bSBen Gras 
145c38dbb97SDavid van Moolenbroek 	errno = ENOTSOCK;
146433d6423SLionel Sambuc 	return -1;
147433d6423SLionel Sambuc }
148433d6423SLionel Sambuc 
_tcp_sendto(int sock,const void * message,size_t length,int flags,const struct sockaddr * dest_addr,socklen_t dest_len)149433d6423SLionel Sambuc static ssize_t _tcp_sendto(int sock, const void *message, size_t length,
150433d6423SLionel Sambuc 	int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
151433d6423SLionel Sambuc {
152433d6423SLionel Sambuc 
153433d6423SLionel Sambuc 	if (flags != 0) {
154433d6423SLionel Sambuc #if DEBUG
155433d6423SLionel Sambuc 		fprintf(stderr, "sendto(tcp): flags not implemented\n");
156433d6423SLionel Sambuc #endif
157433d6423SLionel Sambuc 		errno= ENOSYS;
158433d6423SLionel Sambuc 		return -1;
159433d6423SLionel Sambuc 	}
160433d6423SLionel Sambuc 
161433d6423SLionel Sambuc 	/* Silently ignore destination, if given. */
162433d6423SLionel Sambuc 
163433d6423SLionel Sambuc 	return write(sock, message, length);
164433d6423SLionel Sambuc }
165433d6423SLionel Sambuc 
_udp_sendto(int sock,const void * message,size_t length,int flags,const struct sockaddr * dest_addr,socklen_t dest_len,nwio_udpopt_t * udpoptp)166433d6423SLionel Sambuc static ssize_t _udp_sendto(int sock, const void *message, size_t length,
167433d6423SLionel Sambuc 	int flags, const struct sockaddr *dest_addr, socklen_t dest_len,
168433d6423SLionel Sambuc 	nwio_udpopt_t *udpoptp)
169433d6423SLionel Sambuc {
170433d6423SLionel Sambuc 	int r, t_errno;
171433d6423SLionel Sambuc 	size_t buflen;
172433d6423SLionel Sambuc 	void *buf;
173433d6423SLionel Sambuc 	struct sockaddr_in *sinp;
174433d6423SLionel Sambuc 	udp_io_hdr_t *io_hdrp;
175433d6423SLionel Sambuc 
176433d6423SLionel Sambuc 	if (flags)
177433d6423SLionel Sambuc 	{
178433d6423SLionel Sambuc #if DEBUG
179433d6423SLionel Sambuc 		fprintf(stderr, "sendto(udp): flags not implemented\n");
180433d6423SLionel Sambuc #endif
181433d6423SLionel Sambuc 		errno= ENOSYS;
182433d6423SLionel Sambuc 		return -1;
183433d6423SLionel Sambuc 	}
184433d6423SLionel Sambuc 
185433d6423SLionel Sambuc 	if (udpoptp->nwuo_flags & NWUO_RWDATONLY)
186433d6423SLionel Sambuc 		return write(sock, message, length);
187433d6423SLionel Sambuc 
188433d6423SLionel Sambuc 	if ((udpoptp->nwuo_flags & NWUO_RP_ANY) ||
189433d6423SLionel Sambuc 		(udpoptp->nwuo_flags & NWUO_RA_ANY))
190433d6423SLionel Sambuc 	{
191433d6423SLionel Sambuc 		if (!dest_addr)
192433d6423SLionel Sambuc 		{
193433d6423SLionel Sambuc 			errno= ENOTCONN;
194433d6423SLionel Sambuc 			return -1;
195433d6423SLionel Sambuc 		}
196433d6423SLionel Sambuc 
197433d6423SLionel Sambuc 		/* Check destination address */
198433d6423SLionel Sambuc 		if (dest_len < sizeof(*sinp))
199433d6423SLionel Sambuc 		{
200433d6423SLionel Sambuc 			errno= EINVAL;
201433d6423SLionel Sambuc 			return -1;
202433d6423SLionel Sambuc 		}
203433d6423SLionel Sambuc 		sinp= (struct sockaddr_in *) __UNCONST(dest_addr);
204433d6423SLionel Sambuc 		if (sinp->sin_family != AF_INET)
205433d6423SLionel Sambuc 		{
206433d6423SLionel Sambuc 			errno= EAFNOSUPPORT;
207433d6423SLionel Sambuc 			return -1;
208433d6423SLionel Sambuc 		}
209433d6423SLionel Sambuc 	}
210433d6423SLionel Sambuc 
211433d6423SLionel Sambuc 	buflen= sizeof(*io_hdrp) + length;
212433d6423SLionel Sambuc 	if (buflen < length)
213433d6423SLionel Sambuc 	{
214433d6423SLionel Sambuc 		/* Overflow */
215433d6423SLionel Sambuc 		errno= EMSGSIZE;
216433d6423SLionel Sambuc 		return -1;
217433d6423SLionel Sambuc 	}
218433d6423SLionel Sambuc 	buf= malloc(buflen);
219433d6423SLionel Sambuc 	if (buf == NULL)
220433d6423SLionel Sambuc 		return -1;
221433d6423SLionel Sambuc 
222433d6423SLionel Sambuc 	io_hdrp= buf;
223433d6423SLionel Sambuc 	io_hdrp->uih_src_addr= 0;	/* Unused */
224433d6423SLionel Sambuc 	io_hdrp->uih_src_port= 0;	/* Will cause error if NWUO_LP_ANY */
225433d6423SLionel Sambuc 	if (udpoptp->nwuo_flags & NWUO_RA_ANY)
226433d6423SLionel Sambuc 		io_hdrp->uih_dst_addr= sinp->sin_addr.s_addr;
227433d6423SLionel Sambuc 	else
228433d6423SLionel Sambuc 		io_hdrp->uih_dst_addr= 0;
229433d6423SLionel Sambuc 	if (udpoptp->nwuo_flags & NWUO_RP_ANY)
230433d6423SLionel Sambuc 		io_hdrp->uih_dst_port= sinp->sin_port;
231433d6423SLionel Sambuc 	else
232433d6423SLionel Sambuc 		io_hdrp->uih_dst_port= 0;
233433d6423SLionel Sambuc 	io_hdrp->uih_ip_opt_len= 0;
234433d6423SLionel Sambuc 	io_hdrp->uih_data_len= 0;
235433d6423SLionel Sambuc 
236433d6423SLionel Sambuc 	memcpy(&io_hdrp[1], message, length);
237433d6423SLionel Sambuc 	r= write(sock, buf, buflen);
238433d6423SLionel Sambuc 	if (r == -1)
239433d6423SLionel Sambuc 	{
240433d6423SLionel Sambuc 		t_errno= errno;
241433d6423SLionel Sambuc 		free(buf);
242433d6423SLionel Sambuc 		errno= t_errno;
243433d6423SLionel Sambuc 		return -1;
244433d6423SLionel Sambuc 	}
2455dd8da10SDavid van Moolenbroek 	assert((size_t)r == buflen);
246433d6423SLionel Sambuc 	free(buf);
247433d6423SLionel Sambuc 	return length;
248433d6423SLionel Sambuc }
249433d6423SLionel Sambuc 
_uds_sendto_conn(int sock,const void * message,size_t length,int flags,const struct sockaddr * dest_addr,socklen_t dest_len)250433d6423SLionel Sambuc static ssize_t _uds_sendto_conn(int sock, const void *message, size_t length,
251433d6423SLionel Sambuc 	int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
252433d6423SLionel Sambuc {
253433d6423SLionel Sambuc 
254433d6423SLionel Sambuc 	/* for connection oriented unix domain sockets (SOCK_STREAM /
255433d6423SLionel Sambuc 	 * SOCK_SEQPACKET)
256433d6423SLionel Sambuc 	 */
257433d6423SLionel Sambuc 
258433d6423SLionel Sambuc 	if (flags != 0) {
259433d6423SLionel Sambuc #if DEBUG
260433d6423SLionel Sambuc 		fprintf(stderr, "sendto(uds): flags not implemented\n");
261433d6423SLionel Sambuc #endif
262433d6423SLionel Sambuc 		errno= ENOSYS;
263433d6423SLionel Sambuc 		return -1;
264433d6423SLionel Sambuc 	}
265433d6423SLionel Sambuc 
266433d6423SLionel Sambuc 	/* Silently ignore destination, if given. */
267433d6423SLionel Sambuc 
268433d6423SLionel Sambuc 	return write(sock, message, length);
269433d6423SLionel Sambuc }
270433d6423SLionel Sambuc 
_uds_sendto_dgram(int sock,const void * message,size_t length,int flags,const struct sockaddr * dest_addr,socklen_t dest_len)271433d6423SLionel Sambuc static ssize_t _uds_sendto_dgram(int sock, const void *message, size_t length,
272433d6423SLionel Sambuc 	int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
273433d6423SLionel Sambuc {
274433d6423SLionel Sambuc 	int r;
275433d6423SLionel Sambuc 
276433d6423SLionel Sambuc 	/* for connectionless unix domain sockets (SOCK_DGRAM) */
277433d6423SLionel Sambuc 
278433d6423SLionel Sambuc 	if (flags != 0) {
279433d6423SLionel Sambuc #if DEBUG
280433d6423SLionel Sambuc 		fprintf(stderr, "sendto(uds): flags not implemented\n");
281433d6423SLionel Sambuc #endif
282433d6423SLionel Sambuc 		errno= ENOSYS;
283433d6423SLionel Sambuc 		return -1;
284433d6423SLionel Sambuc 	}
285433d6423SLionel Sambuc 
286433d6423SLionel Sambuc 	if (dest_addr == NULL) {
287433d6423SLionel Sambuc 		errno = EFAULT;
288433d6423SLionel Sambuc 		return -1;
289433d6423SLionel Sambuc 	}
290433d6423SLionel Sambuc 
291433d6423SLionel Sambuc 	/* set the target address */
292433d6423SLionel Sambuc 	r= ioctl(sock, NWIOSUDSTADDR, (void *) __UNCONST(dest_addr));
293433d6423SLionel Sambuc 	if (r == -1) {
294433d6423SLionel Sambuc 		return r;
295433d6423SLionel Sambuc 	}
296433d6423SLionel Sambuc 
297433d6423SLionel Sambuc 	/* do the send */
298433d6423SLionel Sambuc 	return write(sock, message, length);
299433d6423SLionel Sambuc }
300