1 | #if HAVE_CONFIG_H |
---|
2 | #include "config.h" |
---|
3 | #endif |
---|
4 | |
---|
5 | #include <string.h> |
---|
6 | #include <stdarg.h> |
---|
7 | /* #include <stdlib.h> */ |
---|
8 | #include <stdio.h> |
---|
9 | #include <errno.h> |
---|
10 | |
---|
11 | #include <rtems.h> |
---|
12 | #include <rtems/libio_.h> |
---|
13 | #include <rtems/error.h> |
---|
14 | #include <rtems/rtems_bsdnet.h> |
---|
15 | |
---|
16 | #include <errno.h> |
---|
17 | #include <sys/types.h> |
---|
18 | #include <sys/param.h> |
---|
19 | #include <sys/mbuf.h> |
---|
20 | #include <sys/socket.h> |
---|
21 | #include <sys/socketvar.h> |
---|
22 | #include <sys/protosw.h> |
---|
23 | #include <sys/proc.h> |
---|
24 | #include <sys/fcntl.h> |
---|
25 | #include <sys/filio.h> |
---|
26 | #include <sys/sysctl.h> |
---|
27 | |
---|
28 | #include <net/if.h> |
---|
29 | #include <net/if_var.h> |
---|
30 | #include <net/route.h> |
---|
31 | |
---|
32 | #include "rtems_syscall.h" |
---|
33 | |
---|
34 | /* |
---|
35 | * Hooks to RTEMS I/O system |
---|
36 | */ |
---|
37 | static const rtems_filesystem_file_handlers_r socket_handlers; |
---|
38 | |
---|
39 | /* |
---|
40 | * Convert an RTEMS file descriptor to a BSD socket pointer. |
---|
41 | */ |
---|
42 | struct socket * |
---|
43 | rtems_bsdnet_fdToSocket (int fd) |
---|
44 | { |
---|
45 | rtems_libio_t *iop; |
---|
46 | |
---|
47 | if ((uint32_t)fd >= rtems_libio_number_iops) { |
---|
48 | errno = EBADF; |
---|
49 | return NULL; |
---|
50 | } |
---|
51 | |
---|
52 | iop = rtems_libio_iop(fd); |
---|
53 | |
---|
54 | if ((rtems_libio_iop_flags(iop) & LIBIO_FLAGS_OPEN) == 0) { |
---|
55 | errno = EBADF; |
---|
56 | return NULL; |
---|
57 | } |
---|
58 | |
---|
59 | if (iop->pathinfo.handlers != &socket_handlers) { |
---|
60 | errno = ENOTSOCK; |
---|
61 | return NULL; |
---|
62 | } |
---|
63 | |
---|
64 | if (iop->data1 == NULL) |
---|
65 | errno = EBADF; |
---|
66 | return iop->data1; |
---|
67 | } |
---|
68 | |
---|
69 | /* |
---|
70 | * Create an RTEMS file descriptor for a socket |
---|
71 | */ |
---|
72 | static int |
---|
73 | rtems_bsdnet_makeFdForSocket (void *so) |
---|
74 | { |
---|
75 | rtems_libio_t *iop; |
---|
76 | int fd; |
---|
77 | |
---|
78 | iop = rtems_libio_allocate(); |
---|
79 | if (iop == 0) |
---|
80 | rtems_set_errno_and_return_minus_one( ENFILE ); |
---|
81 | |
---|
82 | fd = rtems_libio_iop_to_descriptor(iop); |
---|
83 | iop->data0 = fd; |
---|
84 | iop->data1 = so; |
---|
85 | iop->pathinfo.handlers = &socket_handlers; |
---|
86 | iop->pathinfo.mt_entry = &rtems_filesystem_null_mt_entry; |
---|
87 | rtems_filesystem_location_add_to_mt_entry(&iop->pathinfo); |
---|
88 | rtems_libio_iop_flags_initialize(iop, LIBIO_FLAGS_READ_WRITE); |
---|
89 | return fd; |
---|
90 | } |
---|
91 | |
---|
92 | /* |
---|
93 | * Package system call argument into mbuf. |
---|
94 | */ |
---|
95 | static int |
---|
96 | sockargstombuf (struct mbuf **mp, const void *buf, int buflen, int type) |
---|
97 | { |
---|
98 | struct mbuf *m; |
---|
99 | |
---|
100 | if ((u_int)buflen > MLEN) |
---|
101 | return (EINVAL); |
---|
102 | m = m_get(M_WAIT, type); |
---|
103 | if (m == NULL) |
---|
104 | return (ENOBUFS); |
---|
105 | m->m_len = buflen; |
---|
106 | memcpy (mtod(m, caddr_t), buf, buflen); |
---|
107 | *mp = m; |
---|
108 | if (type == MT_SONAME) { |
---|
109 | struct sockaddr *sa; |
---|
110 | sa = mtod(m, struct sockaddr *); |
---|
111 | sa->sa_len = buflen; |
---|
112 | } |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | /* |
---|
117 | ********************************************************************* |
---|
118 | * BSD-style entry points * |
---|
119 | ********************************************************************* |
---|
120 | */ |
---|
121 | int |
---|
122 | socket (int domain, int type, int protocol) |
---|
123 | { |
---|
124 | int fd; |
---|
125 | int error; |
---|
126 | struct socket *so; |
---|
127 | |
---|
128 | rtems_bsdnet_semaphore_obtain (); |
---|
129 | error = socreate(domain, &so, type, protocol, NULL); |
---|
130 | if (error == 0) { |
---|
131 | fd = rtems_bsdnet_makeFdForSocket (so); |
---|
132 | if (fd < 0) |
---|
133 | soclose (so); |
---|
134 | } |
---|
135 | else { |
---|
136 | errno = error; |
---|
137 | fd = -1; |
---|
138 | } |
---|
139 | rtems_bsdnet_semaphore_release (); |
---|
140 | return fd; |
---|
141 | } |
---|
142 | |
---|
143 | int |
---|
144 | bind (int s, const struct sockaddr *name, socklen_t namelen) |
---|
145 | { |
---|
146 | int error; |
---|
147 | int ret = -1; |
---|
148 | struct socket *so; |
---|
149 | struct mbuf *nam; |
---|
150 | |
---|
151 | rtems_bsdnet_semaphore_obtain (); |
---|
152 | if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) { |
---|
153 | error = sockargstombuf (&nam, name, namelen, MT_SONAME); |
---|
154 | if (error == 0) { |
---|
155 | error = sobind (so, nam); |
---|
156 | if (error == 0) |
---|
157 | ret = 0; |
---|
158 | else |
---|
159 | errno = error; |
---|
160 | m_freem (nam); |
---|
161 | } |
---|
162 | else { |
---|
163 | errno = error; |
---|
164 | } |
---|
165 | } |
---|
166 | rtems_bsdnet_semaphore_release (); |
---|
167 | return ret; |
---|
168 | } |
---|
169 | |
---|
170 | int |
---|
171 | connect (int s, const struct sockaddr *name, socklen_t namelen) |
---|
172 | { |
---|
173 | int error; |
---|
174 | int ret = -1; |
---|
175 | struct socket *so; |
---|
176 | struct mbuf *nam; |
---|
177 | |
---|
178 | rtems_bsdnet_semaphore_obtain (); |
---|
179 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
180 | rtems_bsdnet_semaphore_release (); |
---|
181 | return -1; |
---|
182 | } |
---|
183 | if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { |
---|
184 | errno = EALREADY; |
---|
185 | rtems_bsdnet_semaphore_release (); |
---|
186 | return -1; |
---|
187 | } |
---|
188 | error = sockargstombuf (&nam, name, namelen, MT_SONAME); |
---|
189 | if (error) { |
---|
190 | errno = error; |
---|
191 | rtems_bsdnet_semaphore_release (); |
---|
192 | return -1; |
---|
193 | } |
---|
194 | error = soconnect (so, nam); |
---|
195 | if (error) |
---|
196 | goto bad; |
---|
197 | if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { |
---|
198 | m_freem(nam); |
---|
199 | errno = EINPROGRESS; |
---|
200 | rtems_bsdnet_semaphore_release (); |
---|
201 | return -1; |
---|
202 | } |
---|
203 | error = so->so_error; |
---|
204 | while (error == 0 && (so->so_state & SS_ISCONNECTING)) { |
---|
205 | error = soconnsleep (so); |
---|
206 | if (error) |
---|
207 | break; |
---|
208 | error = so->so_error; |
---|
209 | so->so_error = 0; |
---|
210 | } |
---|
211 | bad: |
---|
212 | if (error != ENXIO) |
---|
213 | so->so_state &= ~SS_ISCONNECTING; |
---|
214 | m_freem (nam); |
---|
215 | if (error) |
---|
216 | errno = error; |
---|
217 | else |
---|
218 | ret = 0; |
---|
219 | rtems_bsdnet_semaphore_release (); |
---|
220 | return ret; |
---|
221 | } |
---|
222 | |
---|
223 | int |
---|
224 | listen (int s, int backlog) |
---|
225 | { |
---|
226 | int error; |
---|
227 | int ret = -1; |
---|
228 | struct socket *so; |
---|
229 | |
---|
230 | rtems_bsdnet_semaphore_obtain (); |
---|
231 | if ((so = rtems_bsdnet_fdToSocket (s)) != NULL) { |
---|
232 | error = solisten (so, backlog); |
---|
233 | if (error == 0) |
---|
234 | ret = 0; |
---|
235 | else |
---|
236 | errno = error; |
---|
237 | } |
---|
238 | rtems_bsdnet_semaphore_release (); |
---|
239 | return ret; |
---|
240 | } |
---|
241 | |
---|
242 | int |
---|
243 | accept (int s, struct sockaddr *name, socklen_t *namelen) |
---|
244 | { |
---|
245 | int fd; |
---|
246 | struct socket *head, *so; |
---|
247 | struct mbuf *nam; |
---|
248 | int error; |
---|
249 | |
---|
250 | rtems_bsdnet_semaphore_obtain (); |
---|
251 | if ((head = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
252 | rtems_bsdnet_semaphore_release (); |
---|
253 | return -1; |
---|
254 | } |
---|
255 | if ((head->so_options & SO_ACCEPTCONN) == 0) { |
---|
256 | errno = EINVAL; |
---|
257 | rtems_bsdnet_semaphore_release (); |
---|
258 | return -1; |
---|
259 | } |
---|
260 | if ((head->so_state & SS_NBIO) && head->so_comp.tqh_first == NULL) { |
---|
261 | errno = EWOULDBLOCK; |
---|
262 | rtems_bsdnet_semaphore_release (); |
---|
263 | return -1; |
---|
264 | } |
---|
265 | error = head->so_error; |
---|
266 | while (error == 0 && head->so_comp.tqh_first == NULL) { |
---|
267 | if (head->so_state & SS_CANTRCVMORE) { |
---|
268 | error = ECONNABORTED; |
---|
269 | break; |
---|
270 | } |
---|
271 | error = soconnsleep (head); |
---|
272 | } |
---|
273 | if (error) { |
---|
274 | errno = error; |
---|
275 | rtems_bsdnet_semaphore_release (); |
---|
276 | return -1; |
---|
277 | } |
---|
278 | |
---|
279 | so = head->so_comp.tqh_first; |
---|
280 | TAILQ_REMOVE(&head->so_comp, so, so_list); |
---|
281 | head->so_qlen--; |
---|
282 | |
---|
283 | fd = rtems_bsdnet_makeFdForSocket (so); |
---|
284 | if (fd < 0) { |
---|
285 | TAILQ_INSERT_HEAD(&head->so_comp, so, so_list); |
---|
286 | head->so_qlen++; |
---|
287 | soconnwakeup (head); |
---|
288 | rtems_bsdnet_semaphore_release (); |
---|
289 | return -1; |
---|
290 | } |
---|
291 | so->so_state &= ~SS_COMP; |
---|
292 | so->so_head = NULL; |
---|
293 | |
---|
294 | nam = m_get(M_WAIT, MT_SONAME); |
---|
295 | (void) soaccept(so, nam); |
---|
296 | if (name) { |
---|
297 | /* check length before it is destroyed */ |
---|
298 | if (*namelen > nam->m_len) |
---|
299 | *namelen = nam->m_len; |
---|
300 | memcpy (name, mtod(nam, caddr_t), *namelen); |
---|
301 | } |
---|
302 | m_freem(nam); |
---|
303 | rtems_bsdnet_semaphore_release (); |
---|
304 | return (fd); |
---|
305 | |
---|
306 | } |
---|
307 | |
---|
308 | /* |
---|
309 | * Shutdown routine |
---|
310 | */ |
---|
311 | |
---|
312 | int |
---|
313 | shutdown (int s, int how) |
---|
314 | { |
---|
315 | struct socket *so; |
---|
316 | int error; |
---|
317 | |
---|
318 | rtems_bsdnet_semaphore_obtain (); |
---|
319 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
320 | rtems_bsdnet_semaphore_release (); |
---|
321 | return -1; |
---|
322 | } |
---|
323 | error = soshutdown(so, how); |
---|
324 | rtems_bsdnet_semaphore_release (); |
---|
325 | if (error) { |
---|
326 | errno = error; |
---|
327 | return -1; |
---|
328 | } |
---|
329 | return 0; |
---|
330 | } |
---|
331 | |
---|
332 | /* |
---|
333 | * All `transmit' operations end up calling this routine. |
---|
334 | */ |
---|
335 | ssize_t |
---|
336 | sendmsg (int s, const struct msghdr *mp, int flags) |
---|
337 | { |
---|
338 | int ret = -1; |
---|
339 | int error; |
---|
340 | struct uio auio; |
---|
341 | struct iovec *iov; |
---|
342 | struct socket *so; |
---|
343 | struct mbuf *to; |
---|
344 | struct mbuf *control = NULL; |
---|
345 | int i; |
---|
346 | int len; |
---|
347 | |
---|
348 | rtems_bsdnet_semaphore_obtain (); |
---|
349 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
350 | rtems_bsdnet_semaphore_release (); |
---|
351 | return -1; |
---|
352 | } |
---|
353 | auio.uio_iov = mp->msg_iov; |
---|
354 | auio.uio_iovcnt = mp->msg_iovlen; |
---|
355 | auio.uio_segflg = UIO_USERSPACE; |
---|
356 | auio.uio_rw = UIO_WRITE; |
---|
357 | auio.uio_offset = 0; |
---|
358 | auio.uio_resid = 0; |
---|
359 | iov = mp->msg_iov; |
---|
360 | for (i = 0; i < mp->msg_iovlen; i++, iov++) { |
---|
361 | if ((auio.uio_resid += iov->iov_len) < 0) { |
---|
362 | errno = EINVAL; |
---|
363 | rtems_bsdnet_semaphore_release (); |
---|
364 | return -1; |
---|
365 | } |
---|
366 | } |
---|
367 | if (mp->msg_name) { |
---|
368 | error = sockargstombuf (&to, mp->msg_name, mp->msg_namelen, MT_SONAME); |
---|
369 | if (error) { |
---|
370 | errno = error; |
---|
371 | rtems_bsdnet_semaphore_release (); |
---|
372 | return -1; |
---|
373 | } |
---|
374 | } |
---|
375 | else { |
---|
376 | to = NULL; |
---|
377 | } |
---|
378 | if (mp->msg_control) { |
---|
379 | if (mp->msg_controllen < sizeof (struct cmsghdr)) { |
---|
380 | errno = EINVAL; |
---|
381 | if (to) |
---|
382 | m_freem(to); |
---|
383 | rtems_bsdnet_semaphore_release (); |
---|
384 | return -1; |
---|
385 | } |
---|
386 | sockargstombuf (&control, mp->msg_control, mp->msg_controllen, MT_CONTROL); |
---|
387 | } |
---|
388 | else { |
---|
389 | control = NULL; |
---|
390 | } |
---|
391 | len = auio.uio_resid; |
---|
392 | error = sosend (so, to, &auio, (struct mbuf *)0, control, flags); |
---|
393 | if (error) { |
---|
394 | if (auio.uio_resid != len && (error == EINTR || error == EWOULDBLOCK)) |
---|
395 | error = 0; |
---|
396 | } |
---|
397 | if (error) |
---|
398 | errno = error; |
---|
399 | else |
---|
400 | ret = len - auio.uio_resid; |
---|
401 | if (to) |
---|
402 | m_freem(to); |
---|
403 | rtems_bsdnet_semaphore_release (); |
---|
404 | return (ret); |
---|
405 | } |
---|
406 | |
---|
407 | /* |
---|
408 | * Send a message to a host |
---|
409 | */ |
---|
410 | ssize_t |
---|
411 | sendto (int s, const void *buf, size_t buflen, int flags, const struct sockaddr *to, socklen_t tolen) |
---|
412 | { |
---|
413 | struct msghdr msg; |
---|
414 | struct iovec iov; |
---|
415 | |
---|
416 | iov.iov_base = (void *)buf; |
---|
417 | iov.iov_len = buflen; |
---|
418 | msg.msg_name = (caddr_t)to; |
---|
419 | msg.msg_namelen = tolen; |
---|
420 | msg.msg_iov = &iov; |
---|
421 | msg.msg_iovlen = 1; |
---|
422 | msg.msg_control = NULL; |
---|
423 | msg.msg_controllen = 0; |
---|
424 | return sendmsg (s, &msg, flags); |
---|
425 | } |
---|
426 | |
---|
427 | /* |
---|
428 | * All `receive' operations end up calling this routine. |
---|
429 | */ |
---|
430 | ssize_t |
---|
431 | recvmsg (int s, struct msghdr *mp, int flags) |
---|
432 | { |
---|
433 | int ret = -1; |
---|
434 | int error; |
---|
435 | struct uio auio; |
---|
436 | struct iovec *iov; |
---|
437 | struct socket *so; |
---|
438 | struct mbuf *from = NULL, *control = NULL; |
---|
439 | int i; |
---|
440 | int len; |
---|
441 | |
---|
442 | rtems_bsdnet_semaphore_obtain (); |
---|
443 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
444 | rtems_bsdnet_semaphore_release (); |
---|
445 | return -1; |
---|
446 | } |
---|
447 | auio.uio_iov = mp->msg_iov; |
---|
448 | auio.uio_iovcnt = mp->msg_iovlen; |
---|
449 | auio.uio_segflg = UIO_USERSPACE; |
---|
450 | auio.uio_rw = UIO_READ; |
---|
451 | auio.uio_offset = 0; |
---|
452 | auio.uio_resid = 0; |
---|
453 | iov = mp->msg_iov; |
---|
454 | for (i = 0; i < mp->msg_iovlen; i++, iov++) { |
---|
455 | if ((auio.uio_resid += iov->iov_len) < 0) { |
---|
456 | errno = EINVAL; |
---|
457 | rtems_bsdnet_semaphore_release (); |
---|
458 | return -1; |
---|
459 | } |
---|
460 | } |
---|
461 | len = auio.uio_resid; |
---|
462 | mp->msg_flags = flags; |
---|
463 | error = soreceive (so, &from, &auio, (struct mbuf **)NULL, |
---|
464 | mp->msg_control ? &control : (struct mbuf **)NULL, |
---|
465 | &mp->msg_flags); |
---|
466 | if (error) { |
---|
467 | if (auio.uio_resid != len && (error == EINTR || error == EWOULDBLOCK)) |
---|
468 | error = 0; |
---|
469 | } |
---|
470 | if (error) { |
---|
471 | errno = error; |
---|
472 | } |
---|
473 | else { |
---|
474 | ret = len - auio.uio_resid; |
---|
475 | if (mp->msg_name) { |
---|
476 | len = mp->msg_namelen; |
---|
477 | if ((len <= 0) || (from == NULL)) { |
---|
478 | len = 0; |
---|
479 | } |
---|
480 | else { |
---|
481 | if (len > from->m_len) |
---|
482 | len = from->m_len; |
---|
483 | memcpy (mp->msg_name, mtod(from, caddr_t), len); |
---|
484 | } |
---|
485 | mp->msg_namelen = len; |
---|
486 | } |
---|
487 | if (mp->msg_control) { |
---|
488 | struct mbuf *m; |
---|
489 | void *ctlbuf; |
---|
490 | |
---|
491 | len = mp->msg_controllen; |
---|
492 | m = control; |
---|
493 | mp->msg_controllen = 0; |
---|
494 | ctlbuf = mp->msg_control; |
---|
495 | |
---|
496 | while (m && (len > 0)) { |
---|
497 | unsigned int tocopy; |
---|
498 | |
---|
499 | if (len >= m->m_len) |
---|
500 | tocopy = m->m_len; |
---|
501 | else { |
---|
502 | mp->msg_flags |= MSG_CTRUNC; |
---|
503 | tocopy = len; |
---|
504 | } |
---|
505 | memcpy(ctlbuf, mtod(m, caddr_t), tocopy); |
---|
506 | ctlbuf += tocopy; |
---|
507 | len -= tocopy; |
---|
508 | m = m->m_next; |
---|
509 | } |
---|
510 | mp->msg_controllen = ctlbuf - mp->msg_control; |
---|
511 | } |
---|
512 | } |
---|
513 | if (from) |
---|
514 | m_freem (from); |
---|
515 | if (control) |
---|
516 | m_freem (control); |
---|
517 | rtems_bsdnet_semaphore_release (); |
---|
518 | return (ret); |
---|
519 | } |
---|
520 | |
---|
521 | /* |
---|
522 | * Receive a message from a host |
---|
523 | */ |
---|
524 | ssize_t |
---|
525 | recvfrom (int s, void *buf, size_t buflen, int flags, struct sockaddr *from, socklen_t *fromlen) |
---|
526 | { |
---|
527 | struct msghdr msg; |
---|
528 | struct iovec iov; |
---|
529 | int ret; |
---|
530 | |
---|
531 | iov.iov_base = buf; |
---|
532 | iov.iov_len = buflen; |
---|
533 | msg.msg_name = (caddr_t)from; |
---|
534 | if (fromlen) |
---|
535 | msg.msg_namelen = *fromlen; |
---|
536 | else |
---|
537 | msg.msg_namelen = 0; |
---|
538 | msg.msg_iov = &iov; |
---|
539 | msg.msg_iovlen = 1; |
---|
540 | msg.msg_control = NULL; |
---|
541 | msg.msg_controllen = 0; |
---|
542 | ret = recvmsg (s, &msg, flags); |
---|
543 | if ((from != NULL) && (fromlen != NULL) && (ret >= 0)) |
---|
544 | *fromlen = msg.msg_namelen; |
---|
545 | return ret; |
---|
546 | } |
---|
547 | |
---|
548 | int |
---|
549 | setsockopt (int s, int level, int name, const void *val, socklen_t len) |
---|
550 | { |
---|
551 | struct socket *so; |
---|
552 | struct mbuf *m = NULL; |
---|
553 | int error; |
---|
554 | |
---|
555 | rtems_bsdnet_semaphore_obtain (); |
---|
556 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
557 | rtems_bsdnet_semaphore_release (); |
---|
558 | return -1; |
---|
559 | } |
---|
560 | if (len > MLEN) { |
---|
561 | errno = EINVAL; |
---|
562 | rtems_bsdnet_semaphore_release (); |
---|
563 | return -1; |
---|
564 | } |
---|
565 | if (val) { |
---|
566 | error = sockargstombuf (&m, val, len, MT_SOOPTS); |
---|
567 | if (error) { |
---|
568 | errno = error; |
---|
569 | rtems_bsdnet_semaphore_release (); |
---|
570 | return -1; |
---|
571 | } |
---|
572 | } |
---|
573 | error = sosetopt(so, level, name, m); |
---|
574 | if (error) { |
---|
575 | errno = error; |
---|
576 | rtems_bsdnet_semaphore_release (); |
---|
577 | return -1; |
---|
578 | } |
---|
579 | rtems_bsdnet_semaphore_release (); |
---|
580 | return 0; |
---|
581 | } |
---|
582 | |
---|
583 | int |
---|
584 | getsockopt (int s, int level, int name, void *aval, socklen_t *avalsize) |
---|
585 | { |
---|
586 | struct socket *so; |
---|
587 | struct mbuf *m = NULL, *m0; |
---|
588 | char *val = aval; |
---|
589 | int i, op, valsize; |
---|
590 | int error; |
---|
591 | |
---|
592 | rtems_bsdnet_semaphore_obtain (); |
---|
593 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
594 | rtems_bsdnet_semaphore_release (); |
---|
595 | return -1; |
---|
596 | } |
---|
597 | if (val) |
---|
598 | valsize = *avalsize; |
---|
599 | else |
---|
600 | valsize = 0; |
---|
601 | if (((error = sogetopt(so, level, name, &m)) == 0) && val && valsize && m) { |
---|
602 | op = 0; |
---|
603 | while (m && op < valsize) { |
---|
604 | i = valsize - op; |
---|
605 | if (i > m->m_len) |
---|
606 | i = m->m_len; |
---|
607 | memcpy (val, mtod(m, caddr_t), i); |
---|
608 | op += i; |
---|
609 | val += i; |
---|
610 | m0 = m; |
---|
611 | MFREE (m0, m); |
---|
612 | } |
---|
613 | *avalsize = op; |
---|
614 | } |
---|
615 | if (m != NULL) |
---|
616 | (void) m_free(m); |
---|
617 | if (error) { |
---|
618 | errno = error; |
---|
619 | rtems_bsdnet_semaphore_release (); |
---|
620 | return -1; |
---|
621 | } |
---|
622 | rtems_bsdnet_semaphore_release (); |
---|
623 | return 0; |
---|
624 | } |
---|
625 | |
---|
626 | static int |
---|
627 | getpeersockname (int s, struct sockaddr *name, socklen_t *namelen, int pflag) |
---|
628 | { |
---|
629 | struct socket *so; |
---|
630 | struct mbuf *m; |
---|
631 | int len = *namelen; |
---|
632 | int error; |
---|
633 | |
---|
634 | rtems_bsdnet_semaphore_obtain (); |
---|
635 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
636 | rtems_bsdnet_semaphore_release (); |
---|
637 | return -1; |
---|
638 | } |
---|
639 | m = m_getclr(M_WAIT, MT_SONAME); |
---|
640 | if (m == NULL) { |
---|
641 | errno = ENOBUFS; |
---|
642 | rtems_bsdnet_semaphore_release (); |
---|
643 | return -1; |
---|
644 | } |
---|
645 | if (pflag) |
---|
646 | error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, m); |
---|
647 | else |
---|
648 | error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, m); |
---|
649 | if (error) { |
---|
650 | m_freem(m); |
---|
651 | errno = error; |
---|
652 | rtems_bsdnet_semaphore_release (); |
---|
653 | return -1; |
---|
654 | } |
---|
655 | if (len > m->m_len) { |
---|
656 | len = m->m_len; |
---|
657 | *namelen = len; |
---|
658 | } |
---|
659 | memcpy (name, mtod(m, caddr_t), len); |
---|
660 | m_freem (m); |
---|
661 | rtems_bsdnet_semaphore_release (); |
---|
662 | return 0; |
---|
663 | } |
---|
664 | |
---|
665 | int |
---|
666 | getpeername (int s, struct sockaddr *name, socklen_t *namelen) |
---|
667 | { |
---|
668 | return getpeersockname (s, name, namelen, 1); |
---|
669 | } |
---|
670 | int |
---|
671 | getsockname (int s, struct sockaddr *name, socklen_t *namelen) |
---|
672 | { |
---|
673 | return getpeersockname (s, name, namelen, 0); |
---|
674 | } |
---|
675 | |
---|
676 | int |
---|
677 | sysctl(const int *name, u_int namelen, void *oldp, |
---|
678 | size_t *oldlenp, const void *newp, size_t newlen) |
---|
679 | { |
---|
680 | int error; |
---|
681 | size_t j; |
---|
682 | |
---|
683 | rtems_bsdnet_semaphore_obtain (); |
---|
684 | error = userland_sysctl (0, name, namelen, oldp, oldlenp, 1, newp, newlen, &j); |
---|
685 | rtems_bsdnet_semaphore_release (); |
---|
686 | |
---|
687 | if (oldlenp) |
---|
688 | *oldlenp = j; |
---|
689 | |
---|
690 | if (error) |
---|
691 | { |
---|
692 | errno = error; |
---|
693 | return -1; |
---|
694 | } |
---|
695 | return 0; |
---|
696 | } |
---|
697 | |
---|
698 | /* |
---|
699 | ************************************************************************ |
---|
700 | * RTEMS I/O HANDLER ROUTINES * |
---|
701 | ************************************************************************ |
---|
702 | */ |
---|
703 | static int |
---|
704 | rtems_bsdnet_close (rtems_libio_t *iop) |
---|
705 | { |
---|
706 | struct socket *so; |
---|
707 | int error; |
---|
708 | |
---|
709 | rtems_bsdnet_semaphore_obtain (); |
---|
710 | if ((so = iop->data1) == NULL) { |
---|
711 | errno = EBADF; |
---|
712 | rtems_bsdnet_semaphore_release (); |
---|
713 | return -1; |
---|
714 | } |
---|
715 | iop->data1 = NULL; |
---|
716 | error = soclose (so); |
---|
717 | rtems_bsdnet_semaphore_release (); |
---|
718 | if (error) { |
---|
719 | errno = error; |
---|
720 | return -1; |
---|
721 | } |
---|
722 | return 0; |
---|
723 | } |
---|
724 | |
---|
725 | static ssize_t |
---|
726 | rtems_bsdnet_read (rtems_libio_t *iop, void *buffer, size_t count) |
---|
727 | { |
---|
728 | return recv (iop->data0, buffer, count, 0); |
---|
729 | } |
---|
730 | |
---|
731 | static ssize_t |
---|
732 | rtems_bsdnet_write (rtems_libio_t *iop, const void *buffer, size_t count) |
---|
733 | { |
---|
734 | return send (iop->data0, buffer, count, 0); |
---|
735 | } |
---|
736 | |
---|
737 | static int |
---|
738 | so_ioctl (rtems_libio_t *iop, struct socket *so, uint32_t command, void *buffer) |
---|
739 | { |
---|
740 | switch (command) { |
---|
741 | case FIONBIO: |
---|
742 | if (*(int *)buffer) { |
---|
743 | iop->flags |= O_NONBLOCK; |
---|
744 | so->so_state |= SS_NBIO; |
---|
745 | } |
---|
746 | else { |
---|
747 | iop->flags &= ~O_NONBLOCK; |
---|
748 | so->so_state &= ~SS_NBIO; |
---|
749 | } |
---|
750 | return 0; |
---|
751 | |
---|
752 | case FIONREAD: |
---|
753 | *(int *)buffer = so->so_rcv.sb_cc; |
---|
754 | return 0; |
---|
755 | } |
---|
756 | |
---|
757 | if (IOCGROUP(command) == 'i') |
---|
758 | return ifioctl (so, command, buffer, NULL); |
---|
759 | if (IOCGROUP(command) == 'r') |
---|
760 | return rtioctl (command, buffer, NULL); |
---|
761 | return (*so->so_proto->pr_usrreqs->pru_control)(so, command, buffer, 0); |
---|
762 | } |
---|
763 | |
---|
764 | static int |
---|
765 | rtems_bsdnet_ioctl (rtems_libio_t *iop, uint32_t command, void *buffer) |
---|
766 | { |
---|
767 | struct socket *so; |
---|
768 | int error; |
---|
769 | |
---|
770 | rtems_bsdnet_semaphore_obtain (); |
---|
771 | if ((so = iop->data1) == NULL) { |
---|
772 | errno = EBADF; |
---|
773 | rtems_bsdnet_semaphore_release (); |
---|
774 | return -1; |
---|
775 | } |
---|
776 | error = so_ioctl (iop, so, command, buffer); |
---|
777 | rtems_bsdnet_semaphore_release (); |
---|
778 | if (error) { |
---|
779 | errno = error; |
---|
780 | return -1; |
---|
781 | } |
---|
782 | return 0; |
---|
783 | } |
---|
784 | |
---|
785 | static int |
---|
786 | rtems_bsdnet_fcntl (rtems_libio_t *iop, int cmd) |
---|
787 | { |
---|
788 | struct socket *so; |
---|
789 | |
---|
790 | if (cmd == F_SETFL) { |
---|
791 | rtems_bsdnet_semaphore_obtain (); |
---|
792 | if ((so = iop->data1) == NULL) { |
---|
793 | rtems_bsdnet_semaphore_release (); |
---|
794 | return EBADF; |
---|
795 | } |
---|
796 | if (rtems_libio_iop_is_no_delay(iop)) |
---|
797 | so->so_state |= SS_NBIO; |
---|
798 | else |
---|
799 | so->so_state &= ~SS_NBIO; |
---|
800 | rtems_bsdnet_semaphore_release (); |
---|
801 | } |
---|
802 | return 0; |
---|
803 | } |
---|
804 | |
---|
805 | static int |
---|
806 | rtems_bsdnet_fstat (const rtems_filesystem_location_info_t *loc, struct stat *sp) |
---|
807 | { |
---|
808 | sp->st_mode = S_IFSOCK; |
---|
809 | return 0; |
---|
810 | } |
---|
811 | |
---|
812 | static const rtems_filesystem_file_handlers_r socket_handlers = { |
---|
813 | .open_h = rtems_filesystem_default_open, |
---|
814 | .close_h = rtems_bsdnet_close, |
---|
815 | .read_h = rtems_bsdnet_read, |
---|
816 | .write_h = rtems_bsdnet_write, |
---|
817 | .ioctl_h = rtems_bsdnet_ioctl, |
---|
818 | .lseek_h = rtems_filesystem_default_lseek, |
---|
819 | .fstat_h = rtems_bsdnet_fstat, |
---|
820 | .ftruncate_h = rtems_filesystem_default_ftruncate, |
---|
821 | .fsync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
822 | .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
823 | .fcntl_h = rtems_bsdnet_fcntl, |
---|
824 | .kqfilter_h = rtems_filesystem_default_kqfilter, |
---|
825 | .mmap_h = rtems_filesystem_default_mmap, |
---|
826 | .poll_h = rtems_filesystem_default_poll, |
---|
827 | .readv_h = rtems_filesystem_default_readv, |
---|
828 | .writev_h = rtems_filesystem_default_writev |
---|
829 | }; |
---|