source: vendor/3.5.0/lib/tsocket/tsocket_bsd.c

Last change on this file was 414, checked in by Herwig Bauernfeind, 15 years ago

Samba 3.5.0: Initial import

File size: 47.6 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3
4 Copyright (C) Stefan Metzmacher 2009
5
6 ** NOTE! The following LGPL license applies to the tsocket
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "replace.h"
25#include "system/filesys.h"
26#include "system/network.h"
27#include "tsocket.h"
28#include "tsocket_internal.h"
29
30static int tsocket_bsd_error_from_errno(int ret,
31 int sys_errno,
32 bool *retry)
33{
34 *retry = false;
35
36 if (ret >= 0) {
37 return 0;
38 }
39
40 if (ret != -1) {
41 return EIO;
42 }
43
44 if (sys_errno == 0) {
45 return EIO;
46 }
47
48 if (sys_errno == EINTR) {
49 *retry = true;
50 return sys_errno;
51 }
52
53 if (sys_errno == EINPROGRESS) {
54 *retry = true;
55 return sys_errno;
56 }
57
58 if (sys_errno == EAGAIN) {
59 *retry = true;
60 return sys_errno;
61 }
62
63#ifdef EWOULDBLOCK
64 if (sys_errno == EWOULDBLOCK) {
65 *retry = true;
66 return sys_errno;
67 }
68#endif
69
70 return sys_errno;
71}
72
73static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd)
74{
75 int i;
76 int sys_errno = 0;
77 int fds[3];
78 int num_fds = 0;
79
80 int result, flags;
81
82 if (fd == -1) {
83 return -1;
84 }
85
86 /* first make a fd >= 3 */
87 if (high_fd) {
88 while (fd < 3) {
89 fds[num_fds++] = fd;
90 fd = dup(fd);
91 if (fd == -1) {
92 sys_errno = errno;
93 break;
94 }
95 }
96 for (i=0; i<num_fds; i++) {
97 close(fds[i]);
98 }
99 if (fd == -1) {
100 errno = sys_errno;
101 return fd;
102 }
103 }
104
105 /* fd should be nonblocking. */
106
107#ifdef O_NONBLOCK
108#define FLAG_TO_SET O_NONBLOCK
109#else
110#ifdef SYSV
111#define FLAG_TO_SET O_NDELAY
112#else /* BSD */
113#define FLAG_TO_SET FNDELAY
114#endif
115#endif
116
117 if ((flags = fcntl(fd, F_GETFL)) == -1) {
118 goto fail;
119 }
120
121 flags |= FLAG_TO_SET;
122 if (fcntl(fd, F_SETFL, flags) == -1) {
123 goto fail;
124 }
125
126#undef FLAG_TO_SET
127
128 /* fd should be closed on exec() */
129#ifdef FD_CLOEXEC
130 result = flags = fcntl(fd, F_GETFD, 0);
131 if (flags >= 0) {
132 flags |= FD_CLOEXEC;
133 result = fcntl(fd, F_SETFD, flags);
134 }
135 if (result < 0) {
136 goto fail;
137 }
138#endif
139 return fd;
140
141 fail:
142 if (fd != -1) {
143 sys_errno = errno;
144 close(fd);
145 errno = sys_errno;
146 }
147 return -1;
148}
149
150static ssize_t tsocket_bsd_pending(int fd)
151{
152 int ret, error;
153 int value = 0;
154 socklen_t len;
155
156 ret = ioctl(fd, FIONREAD, &value);
157 if (ret == -1) {
158 return ret;
159 }
160
161 if (ret != 0) {
162 /* this should not be reached */
163 errno = EIO;
164 return -1;
165 }
166
167 if (value != 0) {
168 return value;
169 }
170
171 error = 0;
172 len = sizeof(error);
173
174 /*
175 * if no data is available check if the socket is in error state. For
176 * dgram sockets it's the way to return ICMP error messages of
177 * connected sockets to the caller.
178 */
179 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
180 if (ret == -1) {
181 return ret;
182 }
183 if (error != 0) {
184 errno = error;
185 return -1;
186 }
187 return 0;
188}
189
190static const struct tsocket_address_ops tsocket_address_bsd_ops;
191
192struct tsocket_address_bsd {
193 union {
194 struct sockaddr sa;
195 struct sockaddr_in in;
196#ifdef HAVE_IPV6
197 struct sockaddr_in6 in6;
198#endif
199 struct sockaddr_un un;
200 struct sockaddr_storage ss;
201 } u;
202};
203
204int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
205 struct sockaddr *sa,
206 size_t sa_socklen,
207 struct tsocket_address **_addr,
208 const char *location)
209{
210 struct tsocket_address *addr;
211 struct tsocket_address_bsd *bsda;
212
213 switch (sa->sa_family) {
214 case AF_UNIX:
215 if (sa_socklen < sizeof(struct sockaddr_un)) {
216 errno = EINVAL;
217 return -1;
218 }
219 break;
220 case AF_INET:
221 if (sa_socklen < sizeof(struct sockaddr_in)) {
222 errno = EINVAL;
223 return -1;
224 }
225 break;
226#ifdef HAVE_IPV6
227 case AF_INET6:
228 if (sa_socklen < sizeof(struct sockaddr_in6)) {
229 errno = EINVAL;
230 return -1;
231 }
232 break;
233#endif
234 default:
235 errno = EAFNOSUPPORT;
236 return -1;
237 }
238
239 if (sa_socklen > sizeof(struct sockaddr_storage)) {
240 errno = EINVAL;
241 return -1;
242 }
243
244 addr = tsocket_address_create(mem_ctx,
245 &tsocket_address_bsd_ops,
246 &bsda,
247 struct tsocket_address_bsd,
248 location);
249 if (!addr) {
250 errno = ENOMEM;
251 return -1;
252 }
253
254 ZERO_STRUCTP(bsda);
255
256 memcpy(&bsda->u.ss, sa, sa_socklen);
257
258 *_addr = addr;
259 return 0;
260}
261
262ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
263 struct sockaddr *sa,
264 size_t sa_socklen)
265{
266 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
267 struct tsocket_address_bsd);
268 ssize_t rlen = 0;
269
270 if (!bsda) {
271 errno = EINVAL;
272 return -1;
273 }
274
275 switch (bsda->u.sa.sa_family) {
276 case AF_UNIX:
277 rlen = sizeof(struct sockaddr_un);
278 break;
279 case AF_INET:
280 rlen = sizeof(struct sockaddr_in);
281 break;
282#ifdef HAVE_IPV6
283 case AF_INET6:
284 rlen = sizeof(struct sockaddr_in6);
285 break;
286#endif
287 default:
288 errno = EAFNOSUPPORT;
289 return -1;
290 }
291
292 if (sa_socklen < rlen) {
293 errno = EINVAL;
294 return -1;
295 }
296
297 if (sa_socklen > sizeof(struct sockaddr_storage)) {
298 memset(sa, 0, sa_socklen);
299 sa_socklen = sizeof(struct sockaddr_storage);
300 }
301
302 memcpy(sa, &bsda->u.ss, sa_socklen);
303 return rlen;
304}
305
306int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
307 const char *fam,
308 const char *addr,
309 uint16_t port,
310 struct tsocket_address **_addr,
311 const char *location)
312{
313 struct addrinfo hints;
314 struct addrinfo *result = NULL;
315 char port_str[6];
316 int ret;
317
318 ZERO_STRUCT(hints);
319 /*
320 * we use SOCKET_STREAM here to get just one result
321 * back from getaddrinfo().
322 */
323 hints.ai_socktype = SOCK_STREAM;
324 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
325
326 if (strcasecmp(fam, "ip") == 0) {
327 hints.ai_family = AF_UNSPEC;
328 if (!addr) {
329#ifdef HAVE_IPV6
330 addr = "::";
331#else
332 addr = "0.0.0.0";
333#endif
334 }
335 } else if (strcasecmp(fam, "ipv4") == 0) {
336 hints.ai_family = AF_INET;
337 if (!addr) {
338 addr = "0.0.0.0";
339 }
340#ifdef HAVE_IPV6
341 } else if (strcasecmp(fam, "ipv6") == 0) {
342 hints.ai_family = AF_INET6;
343 if (!addr) {
344 addr = "::";
345 }
346#endif
347 } else {
348 errno = EAFNOSUPPORT;
349 return -1;
350 }
351
352 snprintf(port_str, sizeof(port_str) - 1, "%u", port);
353
354 ret = getaddrinfo(addr, port_str, &hints, &result);
355 if (ret != 0) {
356 switch (ret) {
357 case EAI_FAIL:
358 errno = EINVAL;
359 break;
360 }
361 ret = -1;
362 goto done;
363 }
364
365 if (result->ai_socktype != SOCK_STREAM) {
366 errno = EINVAL;
367 ret = -1;
368 goto done;
369 }
370
371 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
372 result->ai_addr,
373 result->ai_addrlen,
374 _addr,
375 location);
376
377done:
378 if (result) {
379 freeaddrinfo(result);
380 }
381 return ret;
382}
383
384char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
385 TALLOC_CTX *mem_ctx)
386{
387 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
388 struct tsocket_address_bsd);
389 char addr_str[INET6_ADDRSTRLEN+1];
390 const char *str;
391
392 if (!bsda) {
393 errno = EINVAL;
394 return NULL;
395 }
396
397 switch (bsda->u.sa.sa_family) {
398 case AF_INET:
399 str = inet_ntop(bsda->u.in.sin_family,
400 &bsda->u.in.sin_addr,
401 addr_str, sizeof(addr_str));
402 break;
403#ifdef HAVE_IPV6
404 case AF_INET6:
405 str = inet_ntop(bsda->u.in6.sin6_family,
406 &bsda->u.in6.sin6_addr,
407 addr_str, sizeof(addr_str));
408 break;
409#endif
410 default:
411 errno = EINVAL;
412 return NULL;
413 }
414
415 if (!str) {
416 return NULL;
417 }
418
419 return talloc_strdup(mem_ctx, str);
420}
421
422uint16_t tsocket_address_inet_port(const struct tsocket_address *addr)
423{
424 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
425 struct tsocket_address_bsd);
426 uint16_t port = 0;
427
428 if (!bsda) {
429 errno = EINVAL;
430 return 0;
431 }
432
433 switch (bsda->u.sa.sa_family) {
434 case AF_INET:
435 port = ntohs(bsda->u.in.sin_port);
436 break;
437#ifdef HAVE_IPV6
438 case AF_INET6:
439 port = ntohs(bsda->u.in6.sin6_port);
440 break;
441#endif
442 default:
443 errno = EINVAL;
444 return 0;
445 }
446
447 return port;
448}
449
450int tsocket_address_inet_set_port(struct tsocket_address *addr,
451 uint16_t port)
452{
453 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
454 struct tsocket_address_bsd);
455
456 if (!bsda) {
457 errno = EINVAL;
458 return -1;
459 }
460
461 switch (bsda->u.sa.sa_family) {
462 case AF_INET:
463 bsda->u.in.sin_port = htons(port);
464 break;
465#ifdef HAVE_IPV6
466 case AF_INET6:
467 bsda->u.in6.sin6_port = htons(port);
468 break;
469#endif
470 default:
471 errno = EINVAL;
472 return -1;
473 }
474
475 return 0;
476}
477
478int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
479 const char *path,
480 struct tsocket_address **_addr,
481 const char *location)
482{
483 struct sockaddr_un un;
484 void *p = &un;
485 int ret;
486
487 if (!path) {
488 path = "";
489 }
490
491 if (strlen(path) > sizeof(un.sun_path)-1) {
492 errno = ENAMETOOLONG;
493 return -1;
494 }
495
496 ZERO_STRUCT(un);
497 un.sun_family = AF_UNIX;
498 strncpy(un.sun_path, path, sizeof(un.sun_path)-1);
499
500 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
501 (struct sockaddr *)p,
502 sizeof(un),
503 _addr,
504 location);
505
506 return ret;
507}
508
509char *tsocket_address_unix_path(const struct tsocket_address *addr,
510 TALLOC_CTX *mem_ctx)
511{
512 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
513 struct tsocket_address_bsd);
514 const char *str;
515
516 if (!bsda) {
517 errno = EINVAL;
518 return NULL;
519 }
520
521 switch (bsda->u.sa.sa_family) {
522 case AF_UNIX:
523 str = bsda->u.un.sun_path;
524 break;
525 default:
526 errno = EINVAL;
527 return NULL;
528 }
529
530 return talloc_strdup(mem_ctx, str);
531}
532
533static char *tsocket_address_bsd_string(const struct tsocket_address *addr,
534 TALLOC_CTX *mem_ctx)
535{
536 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
537 struct tsocket_address_bsd);
538 char *str;
539 char *addr_str;
540 const char *prefix = NULL;
541 uint16_t port;
542
543 switch (bsda->u.sa.sa_family) {
544 case AF_UNIX:
545 return talloc_asprintf(mem_ctx, "unix:%s",
546 bsda->u.un.sun_path);
547 case AF_INET:
548 prefix = "ipv4";
549 break;
550#ifdef HAVE_IPV6
551 case AF_INET6:
552 prefix = "ipv6";
553 break;
554#endif
555 default:
556 errno = EINVAL;
557 return NULL;
558 }
559
560 addr_str = tsocket_address_inet_addr_string(addr, mem_ctx);
561 if (!addr_str) {
562 return NULL;
563 }
564
565 port = tsocket_address_inet_port(addr);
566
567 str = talloc_asprintf(mem_ctx, "%s:%s:%u",
568 prefix, addr_str, port);
569 talloc_free(addr_str);
570
571 return str;
572}
573
574static struct tsocket_address *tsocket_address_bsd_copy(const struct tsocket_address *addr,
575 TALLOC_CTX *mem_ctx,
576 const char *location)
577{
578 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
579 struct tsocket_address_bsd);
580 struct tsocket_address *copy;
581 int ret;
582
583 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
584 &bsda->u.sa,
585 sizeof(bsda->u.ss),
586 &copy,
587 location);
588 if (ret != 0) {
589 return NULL;
590 }
591
592 return copy;
593}
594
595static const struct tsocket_address_ops tsocket_address_bsd_ops = {
596 .name = "bsd",
597 .string = tsocket_address_bsd_string,
598 .copy = tsocket_address_bsd_copy,
599};
600
601struct tdgram_bsd {
602 int fd;
603
604 void *event_ptr;
605 struct tevent_fd *fde;
606
607 void *readable_private;
608 void (*readable_handler)(void *private_data);
609 void *writeable_private;
610 void (*writeable_handler)(void *private_data);
611};
612
613static void tdgram_bsd_fde_handler(struct tevent_context *ev,
614 struct tevent_fd *fde,
615 uint16_t flags,
616 void *private_data)
617{
618 struct tdgram_bsd *bsds = talloc_get_type_abort(private_data,
619 struct tdgram_bsd);
620
621 if (flags & TEVENT_FD_WRITE) {
622 bsds->writeable_handler(bsds->writeable_private);
623 return;
624 }
625 if (flags & TEVENT_FD_READ) {
626 if (!bsds->readable_handler) {
627 TEVENT_FD_NOT_READABLE(bsds->fde);
628 return;
629 }
630 bsds->readable_handler(bsds->readable_private);
631 return;
632 }
633}
634
635static int tdgram_bsd_set_readable_handler(struct tdgram_bsd *bsds,
636 struct tevent_context *ev,
637 void (*handler)(void *private_data),
638 void *private_data)
639{
640 if (ev == NULL) {
641 if (handler) {
642 errno = EINVAL;
643 return -1;
644 }
645 if (!bsds->readable_handler) {
646 return 0;
647 }
648 bsds->readable_handler = NULL;
649 bsds->readable_private = NULL;
650
651 return 0;
652 }
653
654 /* read and write must use the same tevent_context */
655 if (bsds->event_ptr != ev) {
656 if (bsds->readable_handler || bsds->writeable_handler) {
657 errno = EINVAL;
658 return -1;
659 }
660 bsds->event_ptr = NULL;
661 TALLOC_FREE(bsds->fde);
662 }
663
664 if (tevent_fd_get_flags(bsds->fde) == 0) {
665 TALLOC_FREE(bsds->fde);
666
667 bsds->fde = tevent_add_fd(ev, bsds,
668 bsds->fd, TEVENT_FD_READ,
669 tdgram_bsd_fde_handler,
670 bsds);
671 if (!bsds->fde) {
672 errno = ENOMEM;
673 return -1;
674 }
675
676 /* cache the event context we're running on */
677 bsds->event_ptr = ev;
678 } else if (!bsds->readable_handler) {
679 TEVENT_FD_READABLE(bsds->fde);
680 }
681
682 bsds->readable_handler = handler;
683 bsds->readable_private = private_data;
684
685 return 0;
686}
687
688static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd *bsds,
689 struct tevent_context *ev,
690 void (*handler)(void *private_data),
691 void *private_data)
692{
693 if (ev == NULL) {
694 if (handler) {
695 errno = EINVAL;
696 return -1;
697 }
698 if (!bsds->writeable_handler) {
699 return 0;
700 }
701 bsds->writeable_handler = NULL;
702 bsds->writeable_private = NULL;
703 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
704
705 return 0;
706 }
707
708 /* read and write must use the same tevent_context */
709 if (bsds->event_ptr != ev) {
710 if (bsds->readable_handler || bsds->writeable_handler) {
711 errno = EINVAL;
712 return -1;
713 }
714 bsds->event_ptr = NULL;
715 TALLOC_FREE(bsds->fde);
716 }
717
718 if (tevent_fd_get_flags(bsds->fde) == 0) {
719 TALLOC_FREE(bsds->fde);
720
721 bsds->fde = tevent_add_fd(ev, bsds,
722 bsds->fd, TEVENT_FD_WRITE,
723 tdgram_bsd_fde_handler,
724 bsds);
725 if (!bsds->fde) {
726 errno = ENOMEM;
727 return -1;
728 }
729
730 /* cache the event context we're running on */
731 bsds->event_ptr = ev;
732 } else if (!bsds->writeable_handler) {
733 TEVENT_FD_WRITEABLE(bsds->fde);
734 }
735
736 bsds->writeable_handler = handler;
737 bsds->writeable_private = private_data;
738
739 return 0;
740}
741
742struct tdgram_bsd_recvfrom_state {
743 struct tdgram_context *dgram;
744
745 uint8_t *buf;
746 size_t len;
747 struct tsocket_address *src;
748};
749
750static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state *state)
751{
752 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
753 struct tdgram_bsd);
754
755 tdgram_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
756
757 return 0;
758}
759
760static void tdgram_bsd_recvfrom_handler(void *private_data);
761
762static struct tevent_req *tdgram_bsd_recvfrom_send(TALLOC_CTX *mem_ctx,
763 struct tevent_context *ev,
764 struct tdgram_context *dgram)
765{
766 struct tevent_req *req;
767 struct tdgram_bsd_recvfrom_state *state;
768 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
769 int ret;
770
771 req = tevent_req_create(mem_ctx, &state,
772 struct tdgram_bsd_recvfrom_state);
773 if (!req) {
774 return NULL;
775 }
776
777 state->dgram = dgram;
778 state->buf = NULL;
779 state->len = 0;
780 state->src = NULL;
781
782 talloc_set_destructor(state, tdgram_bsd_recvfrom_destructor);
783
784 if (bsds->fd == -1) {
785 tevent_req_error(req, ENOTCONN);
786 goto post;
787 }
788
789 /*
790 * this is a fast path, not waiting for the
791 * socket to become explicit readable gains
792 * about 10%-20% performance in benchmark tests.
793 */
794 tdgram_bsd_recvfrom_handler(req);
795 if (!tevent_req_is_in_progress(req)) {
796 goto post;
797 }
798
799 ret = tdgram_bsd_set_readable_handler(bsds, ev,
800 tdgram_bsd_recvfrom_handler,
801 req);
802 if (ret == -1) {
803 tevent_req_error(req, errno);
804 goto post;
805 }
806
807 return req;
808
809 post:
810 tevent_req_post(req, ev);
811 return req;
812}
813
814static void tdgram_bsd_recvfrom_handler(void *private_data)
815{
816 struct tevent_req *req = talloc_get_type_abort(private_data,
817 struct tevent_req);
818 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
819 struct tdgram_bsd_recvfrom_state);
820 struct tdgram_context *dgram = state->dgram;
821 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
822 struct tsocket_address_bsd *bsda;
823 ssize_t ret;
824 struct sockaddr *sa = NULL;
825 socklen_t sa_socklen = 0;
826 int err;
827 bool retry;
828
829 ret = tsocket_bsd_pending(bsds->fd);
830 if (ret == 0) {
831 /* retry later */
832 return;
833 }
834 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
835 if (retry) {
836 /* retry later */
837 return;
838 }
839 if (tevent_req_error(req, err)) {
840 return;
841 }
842
843 state->buf = talloc_array(state, uint8_t, ret);
844 if (tevent_req_nomem(state->buf, req)) {
845 return;
846 }
847 state->len = ret;
848
849 state->src = tsocket_address_create(state,
850 &tsocket_address_bsd_ops,
851 &bsda,
852 struct tsocket_address_bsd,
853 __location__ "bsd_recvfrom");
854 if (tevent_req_nomem(state->src, req)) {
855 return;
856 }
857
858 ZERO_STRUCTP(bsda);
859
860 sa = &bsda->u.sa;
861 sa_socklen = sizeof(bsda->u.ss);
862 /*
863 * for unix sockets we can't use the size of sockaddr_storage
864 * we would get EINVAL
865 */
866 if (bsda->u.sa.sa_family == AF_UNIX) {
867 sa_socklen = sizeof(bsda->u.un);
868 }
869
870 ret = recvfrom(bsds->fd, state->buf, state->len, 0, sa, &sa_socklen);
871 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
872 if (retry) {
873 /* retry later */
874 return;
875 }
876 if (tevent_req_error(req, err)) {
877 return;
878 }
879
880 /*
881 * Some systems (FreeBSD, see bug #7115) return too much
882 * bytes in tsocket_bsd_pending()/ioctl(fd, FIONREAD, ...),
883 * the return value includes some IP/UDP header bytes,
884 * while recvfrom() just returns the payload.
885 */
886 state->buf = talloc_realloc(state, state->buf, uint8_t, ret);
887 if (tevent_req_nomem(state->buf, req)) {
888 return;
889 }
890 state->len = ret;
891
892 tevent_req_done(req);
893}
894
895static ssize_t tdgram_bsd_recvfrom_recv(struct tevent_req *req,
896 int *perrno,
897 TALLOC_CTX *mem_ctx,
898 uint8_t **buf,
899 struct tsocket_address **src)
900{
901 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
902 struct tdgram_bsd_recvfrom_state);
903 ssize_t ret;
904
905 ret = tsocket_simple_int_recv(req, perrno);
906 if (ret == 0) {
907 *buf = talloc_move(mem_ctx, &state->buf);
908 ret = state->len;
909 if (src) {
910 *src = talloc_move(mem_ctx, &state->src);
911 }
912 }
913
914 tevent_req_received(req);
915 return ret;
916}
917
918struct tdgram_bsd_sendto_state {
919 struct tdgram_context *dgram;
920
921 const uint8_t *buf;
922 size_t len;
923 const struct tsocket_address *dst;
924
925 ssize_t ret;
926};
927
928static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
929{
930 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
931 struct tdgram_bsd);
932
933 tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
934
935 return 0;
936}
937
938static void tdgram_bsd_sendto_handler(void *private_data);
939
940static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
941 struct tevent_context *ev,
942 struct tdgram_context *dgram,
943 const uint8_t *buf,
944 size_t len,
945 const struct tsocket_address *dst)
946{
947 struct tevent_req *req;
948 struct tdgram_bsd_sendto_state *state;
949 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
950 int ret;
951
952 req = tevent_req_create(mem_ctx, &state,
953 struct tdgram_bsd_sendto_state);
954 if (!req) {
955 return NULL;
956 }
957
958 state->dgram = dgram;
959 state->buf = buf;
960 state->len = len;
961 state->dst = dst;
962 state->ret = -1;
963
964 talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
965
966 if (bsds->fd == -1) {
967 tevent_req_error(req, ENOTCONN);
968 goto post;
969 }
970
971 /*
972 * this is a fast path, not waiting for the
973 * socket to become explicit writeable gains
974 * about 10%-20% performance in benchmark tests.
975 */
976 tdgram_bsd_sendto_handler(req);
977 if (!tevent_req_is_in_progress(req)) {
978 goto post;
979 }
980
981 ret = tdgram_bsd_set_writeable_handler(bsds, ev,
982 tdgram_bsd_sendto_handler,
983 req);
984 if (ret == -1) {
985 tevent_req_error(req, errno);
986 goto post;
987 }
988
989 return req;
990
991 post:
992 tevent_req_post(req, ev);
993 return req;
994}
995
996static void tdgram_bsd_sendto_handler(void *private_data)
997{
998 struct tevent_req *req = talloc_get_type_abort(private_data,
999 struct tevent_req);
1000 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1001 struct tdgram_bsd_sendto_state);
1002 struct tdgram_context *dgram = state->dgram;
1003 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1004 struct sockaddr *sa = NULL;
1005 socklen_t sa_socklen = 0;
1006 ssize_t ret;
1007 int err;
1008 bool retry;
1009
1010 if (state->dst) {
1011 struct tsocket_address_bsd *bsda =
1012 talloc_get_type(state->dst->private_data,
1013 struct tsocket_address_bsd);
1014
1015 sa = &bsda->u.sa;
1016 sa_socklen = sizeof(bsda->u.ss);
1017 /*
1018 * for unix sockets we can't use the size of sockaddr_storage
1019 * we would get EINVAL
1020 */
1021 if (bsda->u.sa.sa_family == AF_UNIX) {
1022 sa_socklen = sizeof(bsda->u.un);
1023 }
1024 }
1025
1026 ret = sendto(bsds->fd, state->buf, state->len, 0, sa, sa_socklen);
1027 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1028 if (retry) {
1029 /* retry later */
1030 return;
1031 }
1032 if (tevent_req_error(req, err)) {
1033 return;
1034 }
1035
1036 state->ret = ret;
1037
1038 tevent_req_done(req);
1039}
1040
1041static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
1042{
1043 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1044 struct tdgram_bsd_sendto_state);
1045 ssize_t ret;
1046
1047 ret = tsocket_simple_int_recv(req, perrno);
1048 if (ret == 0) {
1049 ret = state->ret;
1050 }
1051
1052 tevent_req_received(req);
1053 return ret;
1054}
1055
1056struct tdgram_bsd_disconnect_state {
1057 uint8_t __dummy;
1058};
1059
1060static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1061 struct tevent_context *ev,
1062 struct tdgram_context *dgram)
1063{
1064 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1065 struct tevent_req *req;
1066 struct tdgram_bsd_disconnect_state *state;
1067 int ret;
1068 int err;
1069 bool dummy;
1070
1071 req = tevent_req_create(mem_ctx, &state,
1072 struct tdgram_bsd_disconnect_state);
1073 if (req == NULL) {
1074 return NULL;
1075 }
1076
1077 if (bsds->fd == -1) {
1078 tevent_req_error(req, ENOTCONN);
1079 goto post;
1080 }
1081
1082 ret = close(bsds->fd);
1083 bsds->fd = -1;
1084 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1085 if (tevent_req_error(req, err)) {
1086 goto post;
1087 }
1088
1089 tevent_req_done(req);
1090post:
1091 tevent_req_post(req, ev);
1092 return req;
1093}
1094
1095static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1096 int *perrno)
1097{
1098 int ret;
1099
1100 ret = tsocket_simple_int_recv(req, perrno);
1101
1102 tevent_req_received(req);
1103 return ret;
1104}
1105
1106static const struct tdgram_context_ops tdgram_bsd_ops = {
1107 .name = "bsd",
1108
1109 .recvfrom_send = tdgram_bsd_recvfrom_send,
1110 .recvfrom_recv = tdgram_bsd_recvfrom_recv,
1111
1112 .sendto_send = tdgram_bsd_sendto_send,
1113 .sendto_recv = tdgram_bsd_sendto_recv,
1114
1115 .disconnect_send = tdgram_bsd_disconnect_send,
1116 .disconnect_recv = tdgram_bsd_disconnect_recv,
1117};
1118
1119static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1120{
1121 TALLOC_FREE(bsds->fde);
1122 if (bsds->fd != -1) {
1123 close(bsds->fd);
1124 bsds->fd = -1;
1125 }
1126 return 0;
1127}
1128
1129static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1130 const struct tsocket_address *remote,
1131 bool broadcast,
1132 TALLOC_CTX *mem_ctx,
1133 struct tdgram_context **_dgram,
1134 const char *location)
1135{
1136 struct tsocket_address_bsd *lbsda =
1137 talloc_get_type_abort(local->private_data,
1138 struct tsocket_address_bsd);
1139 struct tsocket_address_bsd *rbsda = NULL;
1140 struct tdgram_context *dgram;
1141 struct tdgram_bsd *bsds;
1142 int fd;
1143 int ret;
1144 bool do_bind = false;
1145 bool do_reuseaddr = false;
1146 bool do_ipv6only = false;
1147 bool is_inet = false;
1148 int sa_fam = lbsda->u.sa.sa_family;
1149 socklen_t sa_socklen = sizeof(lbsda->u.ss);
1150
1151 if (remote) {
1152 rbsda = talloc_get_type_abort(remote->private_data,
1153 struct tsocket_address_bsd);
1154 }
1155
1156 switch (lbsda->u.sa.sa_family) {
1157 case AF_UNIX:
1158 if (broadcast) {
1159 errno = EINVAL;
1160 return -1;
1161 }
1162 if (lbsda->u.un.sun_path[0] != 0) {
1163 do_reuseaddr = true;
1164 do_bind = true;
1165 }
1166 /*
1167 * for unix sockets we can't use the size of sockaddr_storage
1168 * we would get EINVAL
1169 */
1170 sa_socklen = sizeof(lbsda->u.un);
1171 break;
1172 case AF_INET:
1173 if (lbsda->u.in.sin_port != 0) {
1174 do_reuseaddr = true;
1175 do_bind = true;
1176 }
1177 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
1178 do_bind = true;
1179 }
1180 is_inet = true;
1181 sa_socklen = sizeof(rbsda->u.in);
1182 break;
1183#ifdef HAVE_IPV6
1184 case AF_INET6:
1185 if (lbsda->u.in6.sin6_port != 0) {
1186 do_reuseaddr = true;
1187 do_bind = true;
1188 }
1189 if (memcmp(&in6addr_any,
1190 &lbsda->u.in6.sin6_addr,
1191 sizeof(in6addr_any)) != 0) {
1192 do_bind = true;
1193 }
1194 is_inet = true;
1195 sa_socklen = sizeof(rbsda->u.in6);
1196 do_ipv6only = true;
1197 break;
1198#endif
1199 default:
1200 errno = EINVAL;
1201 return -1;
1202 }
1203
1204 if (!do_bind && is_inet && rbsda) {
1205 sa_fam = rbsda->u.sa.sa_family;
1206 switch (sa_fam) {
1207 case AF_INET:
1208 sa_socklen = sizeof(rbsda->u.in);
1209 do_ipv6only = false;
1210 break;
1211#ifdef HAVE_IPV6
1212 case AF_INET6:
1213 sa_socklen = sizeof(rbsda->u.in6);
1214 do_ipv6only = true;
1215 break;
1216#endif
1217 }
1218 }
1219
1220 fd = socket(sa_fam, SOCK_DGRAM, 0);
1221 if (fd < 0) {
1222 return fd;
1223 }
1224
1225 fd = tsocket_bsd_common_prepare_fd(fd, true);
1226 if (fd < 0) {
1227 return fd;
1228 }
1229
1230 dgram = tdgram_context_create(mem_ctx,
1231 &tdgram_bsd_ops,
1232 &bsds,
1233 struct tdgram_bsd,
1234 location);
1235 if (!dgram) {
1236 int saved_errno = errno;
1237 close(fd);
1238 errno = saved_errno;
1239 return -1;
1240 }
1241 ZERO_STRUCTP(bsds);
1242 bsds->fd = fd;
1243 talloc_set_destructor(bsds, tdgram_bsd_destructor);
1244
1245#ifdef HAVE_IPV6
1246 if (do_ipv6only) {
1247 int val = 1;
1248
1249 ret = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
1250 (const void *)&val, sizeof(val));
1251 if (ret == -1) {
1252 int saved_errno = errno;
1253 talloc_free(dgram);
1254 errno = saved_errno;
1255 return ret;
1256 }
1257 }
1258#endif
1259
1260 if (broadcast) {
1261 int val = 1;
1262
1263 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1264 (const void *)&val, sizeof(val));
1265 if (ret == -1) {
1266 int saved_errno = errno;
1267 talloc_free(dgram);
1268 errno = saved_errno;
1269 return ret;
1270 }
1271 }
1272
1273 if (do_reuseaddr) {
1274 int val = 1;
1275
1276 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1277 (const void *)&val, sizeof(val));
1278 if (ret == -1) {
1279 int saved_errno = errno;
1280 talloc_free(dgram);
1281 errno = saved_errno;
1282 return ret;
1283 }
1284 }
1285
1286 if (do_bind) {
1287 ret = bind(fd, &lbsda->u.sa, sa_socklen);
1288 if (ret == -1) {
1289 int saved_errno = errno;
1290 talloc_free(dgram);
1291 errno = saved_errno;
1292 return ret;
1293 }
1294 }
1295
1296 if (rbsda) {
1297 if (rbsda->u.sa.sa_family != sa_fam) {
1298 talloc_free(dgram);
1299 errno = EINVAL;
1300 return -1;
1301 }
1302
1303 ret = connect(fd, &rbsda->u.sa, sa_socklen);
1304 if (ret == -1) {
1305 int saved_errno = errno;
1306 talloc_free(dgram);
1307 errno = saved_errno;
1308 return ret;
1309 }
1310 }
1311
1312 *_dgram = dgram;
1313 return 0;
1314}
1315
1316int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1317 const struct tsocket_address *remote,
1318 TALLOC_CTX *mem_ctx,
1319 struct tdgram_context **dgram,
1320 const char *location)
1321{
1322 struct tsocket_address_bsd *lbsda =
1323 talloc_get_type_abort(local->private_data,
1324 struct tsocket_address_bsd);
1325 int ret;
1326
1327 switch (lbsda->u.sa.sa_family) {
1328 case AF_INET:
1329 break;
1330#ifdef HAVE_IPV6
1331 case AF_INET6:
1332 break;
1333#endif
1334 default:
1335 errno = EINVAL;
1336 return -1;
1337 }
1338
1339 ret = tdgram_bsd_dgram_socket(local, remote, false,
1340 mem_ctx, dgram, location);
1341
1342 return ret;
1343}
1344
1345int _tdgram_unix_socket(const struct tsocket_address *local,
1346 const struct tsocket_address *remote,
1347 TALLOC_CTX *mem_ctx,
1348 struct tdgram_context **dgram,
1349 const char *location)
1350{
1351 struct tsocket_address_bsd *lbsda =
1352 talloc_get_type_abort(local->private_data,
1353 struct tsocket_address_bsd);
1354 int ret;
1355
1356 switch (lbsda->u.sa.sa_family) {
1357 case AF_UNIX:
1358 break;
1359 default:
1360 errno = EINVAL;
1361 return -1;
1362 }
1363
1364 ret = tdgram_bsd_dgram_socket(local, remote, false,
1365 mem_ctx, dgram, location);
1366
1367 return ret;
1368}
1369
1370struct tstream_bsd {
1371 int fd;
1372
1373 void *event_ptr;
1374 struct tevent_fd *fde;
1375
1376 void *readable_private;
1377 void (*readable_handler)(void *private_data);
1378 void *writeable_private;
1379 void (*writeable_handler)(void *private_data);
1380};
1381
1382static void tstream_bsd_fde_handler(struct tevent_context *ev,
1383 struct tevent_fd *fde,
1384 uint16_t flags,
1385 void *private_data)
1386{
1387 struct tstream_bsd *bsds = talloc_get_type_abort(private_data,
1388 struct tstream_bsd);
1389
1390 if (flags & TEVENT_FD_WRITE) {
1391 bsds->writeable_handler(bsds->writeable_private);
1392 return;
1393 }
1394 if (flags & TEVENT_FD_READ) {
1395 if (!bsds->readable_handler) {
1396 if (bsds->writeable_handler) {
1397 bsds->writeable_handler(bsds->writeable_private);
1398 return;
1399 }
1400 TEVENT_FD_NOT_READABLE(bsds->fde);
1401 return;
1402 }
1403 bsds->readable_handler(bsds->readable_private);
1404 return;
1405 }
1406}
1407
1408static int tstream_bsd_set_readable_handler(struct tstream_bsd *bsds,
1409 struct tevent_context *ev,
1410 void (*handler)(void *private_data),
1411 void *private_data)
1412{
1413 if (ev == NULL) {
1414 if (handler) {
1415 errno = EINVAL;
1416 return -1;
1417 }
1418 if (!bsds->readable_handler) {
1419 return 0;
1420 }
1421 bsds->readable_handler = NULL;
1422 bsds->readable_private = NULL;
1423
1424 return 0;
1425 }
1426
1427 /* read and write must use the same tevent_context */
1428 if (bsds->event_ptr != ev) {
1429 if (bsds->readable_handler || bsds->writeable_handler) {
1430 errno = EINVAL;
1431 return -1;
1432 }
1433 bsds->event_ptr = NULL;
1434 TALLOC_FREE(bsds->fde);
1435 }
1436
1437 if (tevent_fd_get_flags(bsds->fde) == 0) {
1438 TALLOC_FREE(bsds->fde);
1439
1440 bsds->fde = tevent_add_fd(ev, bsds,
1441 bsds->fd, TEVENT_FD_READ,
1442 tstream_bsd_fde_handler,
1443 bsds);
1444 if (!bsds->fde) {
1445 errno = ENOMEM;
1446 return -1;
1447 }
1448
1449 /* cache the event context we're running on */
1450 bsds->event_ptr = ev;
1451 } else if (!bsds->readable_handler) {
1452 TEVENT_FD_READABLE(bsds->fde);
1453 }
1454
1455 bsds->readable_handler = handler;
1456 bsds->readable_private = private_data;
1457
1458 return 0;
1459}
1460
1461static int tstream_bsd_set_writeable_handler(struct tstream_bsd *bsds,
1462 struct tevent_context *ev,
1463 void (*handler)(void *private_data),
1464 void *private_data)
1465{
1466 if (ev == NULL) {
1467 if (handler) {
1468 errno = EINVAL;
1469 return -1;
1470 }
1471 if (!bsds->writeable_handler) {
1472 return 0;
1473 }
1474 bsds->writeable_handler = NULL;
1475 bsds->writeable_private = NULL;
1476 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
1477
1478 return 0;
1479 }
1480
1481 /* read and write must use the same tevent_context */
1482 if (bsds->event_ptr != ev) {
1483 if (bsds->readable_handler || bsds->writeable_handler) {
1484 errno = EINVAL;
1485 return -1;
1486 }
1487 bsds->event_ptr = NULL;
1488 TALLOC_FREE(bsds->fde);
1489 }
1490
1491 if (tevent_fd_get_flags(bsds->fde) == 0) {
1492 TALLOC_FREE(bsds->fde);
1493
1494 bsds->fde = tevent_add_fd(ev, bsds,
1495 bsds->fd,
1496 TEVENT_FD_READ | TEVENT_FD_WRITE,
1497 tstream_bsd_fde_handler,
1498 bsds);
1499 if (!bsds->fde) {
1500 errno = ENOMEM;
1501 return -1;
1502 }
1503
1504 /* cache the event context we're running on */
1505 bsds->event_ptr = ev;
1506 } else if (!bsds->writeable_handler) {
1507 uint16_t flags = tevent_fd_get_flags(bsds->fde);
1508 flags |= TEVENT_FD_READ | TEVENT_FD_WRITE;
1509 tevent_fd_set_flags(bsds->fde, flags);
1510 }
1511
1512 bsds->writeable_handler = handler;
1513 bsds->writeable_private = private_data;
1514
1515 return 0;
1516}
1517
1518static ssize_t tstream_bsd_pending_bytes(struct tstream_context *stream)
1519{
1520 struct tstream_bsd *bsds = tstream_context_data(stream,
1521 struct tstream_bsd);
1522 ssize_t ret;
1523
1524 if (bsds->fd == -1) {
1525 errno = ENOTCONN;
1526 return -1;
1527 }
1528
1529 ret = tsocket_bsd_pending(bsds->fd);
1530
1531 return ret;
1532}
1533
1534struct tstream_bsd_readv_state {
1535 struct tstream_context *stream;
1536
1537 struct iovec *vector;
1538 size_t count;
1539
1540 int ret;
1541};
1542
1543static int tstream_bsd_readv_destructor(struct tstream_bsd_readv_state *state)
1544{
1545 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1546 struct tstream_bsd);
1547
1548 tstream_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
1549
1550 return 0;
1551}
1552
1553static void tstream_bsd_readv_handler(void *private_data);
1554
1555static struct tevent_req *tstream_bsd_readv_send(TALLOC_CTX *mem_ctx,
1556 struct tevent_context *ev,
1557 struct tstream_context *stream,
1558 struct iovec *vector,
1559 size_t count)
1560{
1561 struct tevent_req *req;
1562 struct tstream_bsd_readv_state *state;
1563 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1564 int ret;
1565
1566 req = tevent_req_create(mem_ctx, &state,
1567 struct tstream_bsd_readv_state);
1568 if (!req) {
1569 return NULL;
1570 }
1571
1572 state->stream = stream;
1573 /* we make a copy of the vector so that we can modify it */
1574 state->vector = talloc_array(state, struct iovec, count);
1575 if (tevent_req_nomem(state->vector, req)) {
1576 goto post;
1577 }
1578 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1579 state->count = count;
1580 state->ret = 0;
1581
1582 talloc_set_destructor(state, tstream_bsd_readv_destructor);
1583
1584 if (bsds->fd == -1) {
1585 tevent_req_error(req, ENOTCONN);
1586 goto post;
1587 }
1588
1589 /*
1590 * this is a fast path, not waiting for the
1591 * socket to become explicit readable gains
1592 * about 10%-20% performance in benchmark tests.
1593 */
1594 tstream_bsd_readv_handler(req);
1595 if (!tevent_req_is_in_progress(req)) {
1596 goto post;
1597 }
1598
1599 ret = tstream_bsd_set_readable_handler(bsds, ev,
1600 tstream_bsd_readv_handler,
1601 req);
1602 if (ret == -1) {
1603 tevent_req_error(req, errno);
1604 goto post;
1605 }
1606
1607 return req;
1608
1609 post:
1610 tevent_req_post(req, ev);
1611 return req;
1612}
1613
1614static void tstream_bsd_readv_handler(void *private_data)
1615{
1616 struct tevent_req *req = talloc_get_type_abort(private_data,
1617 struct tevent_req);
1618 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1619 struct tstream_bsd_readv_state);
1620 struct tstream_context *stream = state->stream;
1621 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1622 int ret;
1623 int err;
1624 bool retry;
1625
1626 ret = readv(bsds->fd, state->vector, state->count);
1627 if (ret == 0) {
1628 /* propagate end of file */
1629 tevent_req_error(req, EPIPE);
1630 return;
1631 }
1632 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1633 if (retry) {
1634 /* retry later */
1635 return;
1636 }
1637 if (tevent_req_error(req, err)) {
1638 return;
1639 }
1640
1641 state->ret += ret;
1642
1643 while (ret > 0) {
1644 if (ret < state->vector[0].iov_len) {
1645 uint8_t *base;
1646 base = (uint8_t *)state->vector[0].iov_base;
1647 base += ret;
1648 state->vector[0].iov_base = base;
1649 state->vector[0].iov_len -= ret;
1650 break;
1651 }
1652 ret -= state->vector[0].iov_len;
1653 state->vector += 1;
1654 state->count -= 1;
1655 }
1656
1657 /*
1658 * there're maybe some empty vectors at the end
1659 * which we need to skip, otherwise we would get
1660 * ret == 0 from the readv() call and return EPIPE
1661 */
1662 while (state->count > 0) {
1663 if (state->vector[0].iov_len > 0) {
1664 break;
1665 }
1666 state->vector += 1;
1667 state->count -= 1;
1668 }
1669
1670 if (state->count > 0) {
1671 /* we have more to read */
1672 return;
1673 }
1674
1675 tevent_req_done(req);
1676}
1677
1678static int tstream_bsd_readv_recv(struct tevent_req *req,
1679 int *perrno)
1680{
1681 struct tstream_bsd_readv_state *state = tevent_req_data(req,
1682 struct tstream_bsd_readv_state);
1683 int ret;
1684
1685 ret = tsocket_simple_int_recv(req, perrno);
1686 if (ret == 0) {
1687 ret = state->ret;
1688 }
1689
1690 tevent_req_received(req);
1691 return ret;
1692}
1693
1694struct tstream_bsd_writev_state {
1695 struct tstream_context *stream;
1696
1697 struct iovec *vector;
1698 size_t count;
1699
1700 int ret;
1701};
1702
1703static int tstream_bsd_writev_destructor(struct tstream_bsd_writev_state *state)
1704{
1705 struct tstream_bsd *bsds = tstream_context_data(state->stream,
1706 struct tstream_bsd);
1707
1708 tstream_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1709
1710 return 0;
1711}
1712
1713static void tstream_bsd_writev_handler(void *private_data);
1714
1715static struct tevent_req *tstream_bsd_writev_send(TALLOC_CTX *mem_ctx,
1716 struct tevent_context *ev,
1717 struct tstream_context *stream,
1718 const struct iovec *vector,
1719 size_t count)
1720{
1721 struct tevent_req *req;
1722 struct tstream_bsd_writev_state *state;
1723 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1724 int ret;
1725
1726 req = tevent_req_create(mem_ctx, &state,
1727 struct tstream_bsd_writev_state);
1728 if (!req) {
1729 return NULL;
1730 }
1731
1732 state->stream = stream;
1733 /* we make a copy of the vector so that we can modify it */
1734 state->vector = talloc_array(state, struct iovec, count);
1735 if (tevent_req_nomem(state->vector, req)) {
1736 goto post;
1737 }
1738 memcpy(state->vector, vector, sizeof(struct iovec)*count);
1739 state->count = count;
1740 state->ret = 0;
1741
1742 talloc_set_destructor(state, tstream_bsd_writev_destructor);
1743
1744 if (bsds->fd == -1) {
1745 tevent_req_error(req, ENOTCONN);
1746 goto post;
1747 }
1748
1749 /*
1750 * this is a fast path, not waiting for the
1751 * socket to become explicit writeable gains
1752 * about 10%-20% performance in benchmark tests.
1753 */
1754 tstream_bsd_writev_handler(req);
1755 if (!tevent_req_is_in_progress(req)) {
1756 goto post;
1757 }
1758
1759 ret = tstream_bsd_set_writeable_handler(bsds, ev,
1760 tstream_bsd_writev_handler,
1761 req);
1762 if (ret == -1) {
1763 tevent_req_error(req, errno);
1764 goto post;
1765 }
1766
1767 return req;
1768
1769 post:
1770 tevent_req_post(req, ev);
1771 return req;
1772}
1773
1774static void tstream_bsd_writev_handler(void *private_data)
1775{
1776 struct tevent_req *req = talloc_get_type_abort(private_data,
1777 struct tevent_req);
1778 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1779 struct tstream_bsd_writev_state);
1780 struct tstream_context *stream = state->stream;
1781 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1782 ssize_t ret;
1783 int err;
1784 bool retry;
1785
1786 ret = writev(bsds->fd, state->vector, state->count);
1787 if (ret == 0) {
1788 /* propagate end of file */
1789 tevent_req_error(req, EPIPE);
1790 return;
1791 }
1792 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1793 if (retry) {
1794 /* retry later */
1795 return;
1796 }
1797 if (tevent_req_error(req, err)) {
1798 return;
1799 }
1800
1801 state->ret += ret;
1802
1803 while (ret > 0) {
1804 if (ret < state->vector[0].iov_len) {
1805 uint8_t *base;
1806 base = (uint8_t *)state->vector[0].iov_base;
1807 base += ret;
1808 state->vector[0].iov_base = base;
1809 state->vector[0].iov_len -= ret;
1810 break;
1811 }
1812 ret -= state->vector[0].iov_len;
1813 state->vector += 1;
1814 state->count -= 1;
1815 }
1816
1817 /*
1818 * there're maybe some empty vectors at the end
1819 * which we need to skip, otherwise we would get
1820 * ret == 0 from the writev() call and return EPIPE
1821 */
1822 while (state->count > 0) {
1823 if (state->vector[0].iov_len > 0) {
1824 break;
1825 }
1826 state->vector += 1;
1827 state->count -= 1;
1828 }
1829
1830 if (state->count > 0) {
1831 /* we have more to read */
1832 return;
1833 }
1834
1835 tevent_req_done(req);
1836}
1837
1838static int tstream_bsd_writev_recv(struct tevent_req *req, int *perrno)
1839{
1840 struct tstream_bsd_writev_state *state = tevent_req_data(req,
1841 struct tstream_bsd_writev_state);
1842 int ret;
1843
1844 ret = tsocket_simple_int_recv(req, perrno);
1845 if (ret == 0) {
1846 ret = state->ret;
1847 }
1848
1849 tevent_req_received(req);
1850 return ret;
1851}
1852
1853struct tstream_bsd_disconnect_state {
1854 void *__dummy;
1855};
1856
1857static struct tevent_req *tstream_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1858 struct tevent_context *ev,
1859 struct tstream_context *stream)
1860{
1861 struct tstream_bsd *bsds = tstream_context_data(stream, struct tstream_bsd);
1862 struct tevent_req *req;
1863 struct tstream_bsd_disconnect_state *state;
1864 int ret;
1865 int err;
1866 bool dummy;
1867
1868 req = tevent_req_create(mem_ctx, &state,
1869 struct tstream_bsd_disconnect_state);
1870 if (req == NULL) {
1871 return NULL;
1872 }
1873
1874 if (bsds->fd == -1) {
1875 tevent_req_error(req, ENOTCONN);
1876 goto post;
1877 }
1878
1879 ret = close(bsds->fd);
1880 bsds->fd = -1;
1881 err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1882 if (tevent_req_error(req, err)) {
1883 goto post;
1884 }
1885
1886 tevent_req_done(req);
1887post:
1888 tevent_req_post(req, ev);
1889 return req;
1890}
1891
1892static int tstream_bsd_disconnect_recv(struct tevent_req *req,
1893 int *perrno)
1894{
1895 int ret;
1896
1897 ret = tsocket_simple_int_recv(req, perrno);
1898
1899 tevent_req_received(req);
1900 return ret;
1901}
1902
1903static const struct tstream_context_ops tstream_bsd_ops = {
1904 .name = "bsd",
1905
1906 .pending_bytes = tstream_bsd_pending_bytes,
1907
1908 .readv_send = tstream_bsd_readv_send,
1909 .readv_recv = tstream_bsd_readv_recv,
1910
1911 .writev_send = tstream_bsd_writev_send,
1912 .writev_recv = tstream_bsd_writev_recv,
1913
1914 .disconnect_send = tstream_bsd_disconnect_send,
1915 .disconnect_recv = tstream_bsd_disconnect_recv,
1916};
1917
1918static int tstream_bsd_destructor(struct tstream_bsd *bsds)
1919{
1920 TALLOC_FREE(bsds->fde);
1921 if (bsds->fd != -1) {
1922 close(bsds->fd);
1923 bsds->fd = -1;
1924 }
1925 return 0;
1926}
1927
1928int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx,
1929 int fd,
1930 struct tstream_context **_stream,
1931 const char *location)
1932{
1933 struct tstream_context *stream;
1934 struct tstream_bsd *bsds;
1935
1936 stream = tstream_context_create(mem_ctx,
1937 &tstream_bsd_ops,
1938 &bsds,
1939 struct tstream_bsd,
1940 location);
1941 if (!stream) {
1942 return -1;
1943 }
1944 ZERO_STRUCTP(bsds);
1945 bsds->fd = fd;
1946 talloc_set_destructor(bsds, tstream_bsd_destructor);
1947
1948 *_stream = stream;
1949 return 0;
1950}
1951
1952struct tstream_bsd_connect_state {
1953 int fd;
1954 struct tevent_fd *fde;
1955 struct tstream_conext *stream;
1956};
1957
1958static int tstream_bsd_connect_destructor(struct tstream_bsd_connect_state *state)
1959{
1960 TALLOC_FREE(state->fde);
1961 if (state->fd != -1) {
1962 close(state->fd);
1963 state->fd = -1;
1964 }
1965
1966 return 0;
1967}
1968
1969static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
1970 struct tevent_fd *fde,
1971 uint16_t flags,
1972 void *private_data);
1973
1974static struct tevent_req * tstream_bsd_connect_send(TALLOC_CTX *mem_ctx,
1975 struct tevent_context *ev,
1976 int sys_errno,
1977 const struct tsocket_address *local,
1978 const struct tsocket_address *remote)
1979{
1980 struct tevent_req *req;
1981 struct tstream_bsd_connect_state *state;
1982 struct tsocket_address_bsd *lbsda =
1983 talloc_get_type_abort(local->private_data,
1984 struct tsocket_address_bsd);
1985 struct tsocket_address_bsd *rbsda =
1986 talloc_get_type_abort(remote->private_data,
1987 struct tsocket_address_bsd);
1988 int ret;
1989 int err;
1990 bool retry;
1991 bool do_bind = false;
1992 bool do_reuseaddr = false;
1993 bool do_ipv6only = false;
1994 bool is_inet = false;
1995 int sa_fam = lbsda->u.sa.sa_family;
1996 socklen_t sa_socklen = sizeof(rbsda->u.ss);
1997
1998 req = tevent_req_create(mem_ctx, &state,
1999 struct tstream_bsd_connect_state);
2000 if (!req) {
2001 return NULL;
2002 }
2003 state->fd = -1;
2004 state->fde = NULL;
2005
2006 talloc_set_destructor(state, tstream_bsd_connect_destructor);
2007
2008 /* give the wrappers a chance to report an error */
2009 if (sys_errno != 0) {
2010 tevent_req_error(req, sys_errno);
2011 goto post;
2012 }
2013
2014 switch (lbsda->u.sa.sa_family) {
2015 case AF_UNIX:
2016 if (lbsda->u.un.sun_path[0] != 0) {
2017 do_reuseaddr = true;
2018 do_bind = true;
2019 }
2020 /*
2021 * for unix sockets we can't use the size of sockaddr_storage
2022 * we would get EINVAL
2023 */
2024 sa_socklen = sizeof(rbsda->u.un);
2025 break;
2026 case AF_INET:
2027 if (lbsda->u.in.sin_port != 0) {
2028 do_reuseaddr = true;
2029 do_bind = true;
2030 }
2031 if (lbsda->u.in.sin_addr.s_addr != INADDR_ANY) {
2032 do_bind = true;
2033 }
2034 is_inet = true;
2035 sa_socklen = sizeof(rbsda->u.in);
2036 break;
2037#ifdef HAVE_IPV6
2038 case AF_INET6:
2039 if (lbsda->u.in6.sin6_port != 0) {
2040 do_reuseaddr = true;
2041 do_bind = true;
2042 }
2043 if (memcmp(&in6addr_any,
2044 &lbsda->u.in6.sin6_addr,
2045 sizeof(in6addr_any)) != 0) {
2046 do_bind = true;
2047 }
2048 is_inet = true;
2049 sa_socklen = sizeof(rbsda->u.in6);
2050 do_ipv6only = true;
2051 break;
2052#endif
2053 default:
2054 tevent_req_error(req, EINVAL);
2055 goto post;
2056 }
2057
2058 if (!do_bind && is_inet) {
2059 sa_fam = rbsda->u.sa.sa_family;
2060 switch (sa_fam) {
2061 case AF_INET:
2062 sa_socklen = sizeof(rbsda->u.in);
2063 do_ipv6only = false;
2064 break;
2065#ifdef HAVE_IPV6
2066 case AF_INET6:
2067 sa_socklen = sizeof(rbsda->u.in6);
2068 do_ipv6only = true;
2069 break;
2070#endif
2071 }
2072 }
2073
2074 state->fd = socket(sa_fam, SOCK_STREAM, 0);
2075 if (state->fd == -1) {
2076 tevent_req_error(req, errno);
2077 goto post;
2078 }
2079
2080 state->fd = tsocket_bsd_common_prepare_fd(state->fd, true);
2081 if (state->fd == -1) {
2082 tevent_req_error(req, errno);
2083 goto post;
2084 }
2085
2086#ifdef HAVE_IPV6
2087 if (do_ipv6only) {
2088 int val = 1;
2089
2090 ret = setsockopt(state->fd, IPPROTO_IPV6, IPV6_V6ONLY,
2091 (const void *)&val, sizeof(val));
2092 if (ret == -1) {
2093 tevent_req_error(req, errno);
2094 goto post;
2095 }
2096 }
2097#endif
2098
2099 if (do_reuseaddr) {
2100 int val = 1;
2101
2102 ret = setsockopt(state->fd, SOL_SOCKET, SO_REUSEADDR,
2103 (const void *)&val, sizeof(val));
2104 if (ret == -1) {
2105 tevent_req_error(req, errno);
2106 goto post;
2107 }
2108 }
2109
2110 if (do_bind) {
2111 ret = bind(state->fd, &lbsda->u.sa, sa_socklen);
2112 if (ret == -1) {
2113 tevent_req_error(req, errno);
2114 goto post;
2115 }
2116 }
2117
2118 if (rbsda->u.sa.sa_family != sa_fam) {
2119 tevent_req_error(req, EINVAL);
2120 goto post;
2121 }
2122
2123 ret = connect(state->fd, &rbsda->u.sa, sa_socklen);
2124 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2125 if (retry) {
2126 /* retry later */
2127 goto async;
2128 }
2129 if (tevent_req_error(req, err)) {
2130 goto post;
2131 }
2132
2133 tevent_req_done(req);
2134 goto post;
2135
2136 async:
2137 state->fde = tevent_add_fd(ev, state,
2138 state->fd,
2139 TEVENT_FD_READ | TEVENT_FD_WRITE,
2140 tstream_bsd_connect_fde_handler,
2141 req);
2142 if (tevent_req_nomem(state->fde, req)) {
2143 goto post;
2144 }
2145
2146 return req;
2147
2148 post:
2149 tevent_req_post(req, ev);
2150 return req;
2151}
2152
2153static void tstream_bsd_connect_fde_handler(struct tevent_context *ev,
2154 struct tevent_fd *fde,
2155 uint16_t flags,
2156 void *private_data)
2157{
2158 struct tevent_req *req = talloc_get_type_abort(private_data,
2159 struct tevent_req);
2160 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2161 struct tstream_bsd_connect_state);
2162 int ret;
2163 int error=0;
2164 socklen_t len = sizeof(error);
2165 int err;
2166 bool retry;
2167
2168 ret = getsockopt(state->fd, SOL_SOCKET, SO_ERROR, &error, &len);
2169 if (ret == 0) {
2170 if (error != 0) {
2171 errno = error;
2172 ret = -1;
2173 }
2174 }
2175 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
2176 if (retry) {
2177 /* retry later */
2178 return;
2179 }
2180 if (tevent_req_error(req, err)) {
2181 return;
2182 }
2183
2184 tevent_req_done(req);
2185}
2186
2187static int tstream_bsd_connect_recv(struct tevent_req *req,
2188 int *perrno,
2189 TALLOC_CTX *mem_ctx,
2190 struct tstream_context **stream,
2191 const char *location)
2192{
2193 struct tstream_bsd_connect_state *state = tevent_req_data(req,
2194 struct tstream_bsd_connect_state);
2195 int ret;
2196
2197 ret = tsocket_simple_int_recv(req, perrno);
2198 if (ret == 0) {
2199 ret = _tstream_bsd_existing_socket(mem_ctx,
2200 state->fd,
2201 stream,
2202 location);
2203 if (ret == -1) {
2204 *perrno = errno;
2205 goto done;
2206 }
2207 TALLOC_FREE(state->fde);
2208 state->fd = -1;
2209 }
2210
2211done:
2212 tevent_req_received(req);
2213 return ret;
2214}
2215
2216struct tevent_req * tstream_inet_tcp_connect_send(TALLOC_CTX *mem_ctx,
2217 struct tevent_context *ev,
2218 const struct tsocket_address *local,
2219 const struct tsocket_address *remote)
2220{
2221 struct tsocket_address_bsd *lbsda =
2222 talloc_get_type_abort(local->private_data,
2223 struct tsocket_address_bsd);
2224 struct tevent_req *req;
2225 int sys_errno = 0;
2226
2227 switch (lbsda->u.sa.sa_family) {
2228 case AF_INET:
2229 break;
2230#ifdef HAVE_IPV6
2231 case AF_INET6:
2232 break;
2233#endif
2234 default:
2235 sys_errno = EINVAL;
2236 break;
2237 }
2238
2239 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2240
2241 return req;
2242}
2243
2244int _tstream_inet_tcp_connect_recv(struct tevent_req *req,
2245 int *perrno,
2246 TALLOC_CTX *mem_ctx,
2247 struct tstream_context **stream,
2248 const char *location)
2249{
2250 return tstream_bsd_connect_recv(req, perrno, mem_ctx, stream, location);
2251}
2252
2253struct tevent_req * tstream_unix_connect_send(TALLOC_CTX *mem_ctx,
2254 struct tevent_context *ev,
2255 const struct tsocket_address *local,
2256 const struct tsocket_address *remote)
2257{
2258 struct tsocket_address_bsd *lbsda =
2259 talloc_get_type_abort(local->private_data,
2260 struct tsocket_address_bsd);
2261 struct tevent_req *req;
2262 int sys_errno = 0;
2263
2264 switch (lbsda->u.sa.sa_family) {
2265 case AF_UNIX:
2266 break;
2267 default:
2268 sys_errno = EINVAL;
2269 break;
2270 }
2271
2272 req = tstream_bsd_connect_send(mem_ctx, ev, sys_errno, local, remote);
2273
2274 return req;
2275}
2276
2277int _tstream_unix_connect_recv(struct tevent_req *req,
2278 int *perrno,
2279 TALLOC_CTX *mem_ctx,
2280 struct tstream_context **stream,
2281 const char *location)
2282{
2283 return tstream_bsd_connect_recv(req, perrno, mem_ctx, stream, location);
2284}
2285
2286int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1,
2287 struct tstream_context **_stream1,
2288 TALLOC_CTX *mem_ctx2,
2289 struct tstream_context **_stream2,
2290 const char *location)
2291{
2292 int ret;
2293 int fds[2];
2294 int fd1;
2295 int fd2;
2296 struct tstream_context *stream1 = NULL;
2297 struct tstream_context *stream2 = NULL;
2298
2299 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
2300 if (ret == -1) {
2301 return -1;
2302 }
2303 fd1 = fds[0];
2304 fd2 = fds[1];
2305
2306 fd1 = tsocket_bsd_common_prepare_fd(fd1, true);
2307 if (fd1 == -1) {
2308 int sys_errno = errno;
2309 close(fd2);
2310 errno = sys_errno;
2311 return -1;
2312 }
2313
2314 fd2 = tsocket_bsd_common_prepare_fd(fd2, true);
2315 if (fd2 == -1) {
2316 int sys_errno = errno;
2317 close(fd1);
2318 errno = sys_errno;
2319 return -1;
2320 }
2321
2322 ret = _tstream_bsd_existing_socket(mem_ctx1,
2323 fd1,
2324 &stream1,
2325 location);
2326 if (ret == -1) {
2327 int sys_errno = errno;
2328 close(fd1);
2329 close(fd2);
2330 errno = sys_errno;
2331 return -1;
2332 }
2333
2334 ret = _tstream_bsd_existing_socket(mem_ctx2,
2335 fd2,
2336 &stream2,
2337 location);
2338 if (ret == -1) {
2339 int sys_errno = errno;
2340 talloc_free(stream1);
2341 close(fd2);
2342 errno = sys_errno;
2343 return -1;
2344 }
2345
2346 *_stream1 = stream1;
2347 *_stream2 = stream2;
2348 return 0;
2349}
2350
Note: See TracBrowser for help on using the repository browser.