libutil: add O_NOCTTY back to old pty open code
[minix.git] / lib / libc / sys-minix / recvfrom.c
blob091559f9ea5c796156c8f3ad01c77e35a319a1c1
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 #define DEBUG 0
25 static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length,
26 int flags, struct sockaddr *__restrict address,
27 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp);
28 static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length,
29 int flags, struct sockaddr *__restrict address,
30 socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp);
31 static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer,
32 size_t length, int flags, struct sockaddr *__restrict address,
33 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr);
34 static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer,
35 size_t length, int flags, struct sockaddr *__restrict address,
36 socklen_t *__restrict address_len);
38 ssize_t recvfrom(int sock, void *__restrict buffer, size_t length,
39 int flags, struct sockaddr *__restrict address,
40 socklen_t *__restrict address_len)
42 int r;
43 nwio_tcpconf_t tcpconf;
44 nwio_udpopt_t udpopt;
45 struct sockaddr_un uds_addr;
46 int uds_sotype = -1;
48 #if DEBUG
49 fprintf(stderr, "recvfrom: for fd %d\n", sock);
50 #endif
52 r= ioctl(sock, NWIOGTCPCONF, &tcpconf);
53 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
55 if (r == -1)
56 return r;
57 return _tcp_recvfrom(sock, buffer, length, flags,
58 address, address_len, &tcpconf);
61 r= ioctl(sock, NWIOGUDPOPT, &udpopt);
62 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
64 if (r == -1)
65 return r;
66 return _udp_recvfrom(sock, buffer, length, flags,
67 address, address_len, &udpopt);
70 r= ioctl(sock, NWIOGUDSSOTYPE, &uds_sotype);
71 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
74 if (r == -1) {
75 return r;
78 if (uds_sotype == SOCK_DGRAM) {
79 return _uds_recvfrom_dgram(sock, buffer,
80 length, flags, address, address_len);
81 } else {
82 return _uds_recvfrom_conn(sock, buffer,
83 length, flags, address, address_len,
84 &uds_addr);
88 #if DEBUG
89 fprintf(stderr, "recvfrom: not implemented for fd %d\n", sock);
90 #endif
91 errno= ENOSYS;
92 assert(0);
93 return -1;
96 static ssize_t _tcp_recvfrom(int sock, void *__restrict buffer, size_t length,
97 int flags, struct sockaddr *__restrict address,
98 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp)
100 int r;
101 size_t len;
102 struct sockaddr_in sin;
104 if (flags != 0)
106 #if DEBUG
107 fprintf(stderr, "recvfrom(tcp): flags not implemented\n");
108 #endif
109 errno= ENOSYS;
110 return -1;
113 r = read(sock, buffer, length);
115 if (r >= 0 && address != NULL)
117 sin.sin_family= AF_INET;
118 sin.sin_addr.s_addr= tcpconfp->nwtc_remaddr;
119 sin.sin_port= tcpconfp->nwtc_remport;
120 len= *address_len;
121 if (len > sizeof(sin))
122 len= sizeof(sin);
123 memcpy(address, &sin, len);
124 *address_len= sizeof(sin);
127 return r;
130 static ssize_t _udp_recvfrom(int sock, void *__restrict buffer, size_t length,
131 int flags, struct sockaddr *__restrict address,
132 socklen_t *__restrict address_len, nwio_udpopt_t *udpoptp)
134 int r, t_errno;
135 size_t buflen, len;
136 void *buf;
137 udp_io_hdr_t *io_hdrp;
138 struct sockaddr_in sin;
140 if (flags)
142 #if DEBUG
143 fprintf(stderr, "recvfrom(udp): flags not implemented\n");
144 #endif
145 errno= ENOSYS;
146 return -1;
149 if (udpoptp->nwuo_flags & NWUO_RWDATONLY)
151 if (address != NULL &&
152 (udpoptp->nwuo_flags & (NWUO_RA_SET | NWUO_RP_SET)) !=
153 (NWUO_RA_SET | NWUO_RP_SET))
156 #if DEBUG
157 fprintf(stderr,
158 "recvfrom(udp): RWDATONLY on unconnected socket\n");
159 #endif
160 errno= ENOTCONN;
161 return -1;
164 r= read(sock, buffer, length);
165 if (r == -1)
166 return r;
168 if (address != NULL)
170 sin.sin_family= AF_INET;
171 sin.sin_addr.s_addr= udpoptp->nwuo_remaddr;
172 sin.sin_port= udpoptp->nwuo_remport;
173 len= *address_len;
174 if (len > sizeof(sin))
175 len= sizeof(sin);
176 memcpy(address, &sin, len);
177 *address_len= sizeof(sin);
180 return r;
183 buflen= sizeof(*io_hdrp) + length;
184 if (buflen < length)
186 /* Overflow */
187 errno= EMSGSIZE;
188 return -1;
190 buf= malloc(buflen);
191 if (buf == NULL)
192 return -1;
194 r= read(sock, buf, buflen);
195 if (r == -1)
197 t_errno= errno;
198 #if DEBUG
199 fprintf(stderr, "recvfrom(udp): read failed: %s\n",
200 strerror(errno));
201 fprintf(stderr, "udp opt flags = 0x%x\n", udpoptp->nwuo_flags);
202 #endif
203 free(buf);
204 errno= t_errno;
205 return -1;
208 assert(r >= sizeof(*io_hdrp));
209 length= r-sizeof(*io_hdrp);
211 io_hdrp= buf;
212 memcpy(buffer, &io_hdrp[1], length);
214 if (address != NULL)
216 sin.sin_family= AF_INET;
217 sin.sin_addr.s_addr= io_hdrp->uih_src_addr;
218 sin.sin_port= io_hdrp->uih_src_port;
219 len= *address_len;
220 if (len > sizeof(sin))
221 len= sizeof(sin);
222 memcpy(address, &sin, len);
223 *address_len= sizeof(sin);
225 free(buf);
226 return length;
229 static ssize_t _uds_recvfrom_conn(int sock, void *__restrict buffer,
230 size_t length, int flags, struct sockaddr *__restrict address,
231 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr)
233 int r;
234 size_t len;
236 /* for connection oriented unix domain sockets (SOCK_STREAM /
237 * SOCK_SEQPACKET)
240 if (flags != 0)
242 #if DEBUG
243 fprintf(stderr, "recvfrom(uds): flags not implemented\n");
244 #endif
245 errno= ENOSYS;
246 return -1;
249 r = read(sock, buffer, length);
251 if (r >= 0 && address != NULL)
254 len= *address_len;
255 if (len > sizeof(struct sockaddr_un))
256 len= sizeof(struct sockaddr_un);
257 memcpy(address, uds_addr, len);
258 *address_len= sizeof(struct sockaddr_un);
261 return r;
264 static ssize_t _uds_recvfrom_dgram(int sock, void *__restrict buffer,
265 size_t length, int flags, struct sockaddr *__restrict address,
266 socklen_t *__restrict address_len)
268 int r;
269 size_t len;
271 /* for connectionless unix domain sockets (SOCK_DGRAM) */
273 if (flags != 0)
275 #if DEBUG
276 fprintf(stderr, "recvfrom(uds): flags not implemented\n");
277 #endif
278 errno= ENOSYS;
279 return -1;
282 r = read(sock, buffer, length);
284 if (r >= 0 && address != NULL)
286 len= *address_len;
287 if (len > sizeof(struct sockaddr_un))
288 len= sizeof(struct sockaddr_un);
289 ioctl(sock, NWIOGUDSFADDR, address);
290 *address_len= sizeof(struct sockaddr_un);
293 return r;