e260b1fa25ef6be46bd61b4b00c9f9dabbc86aa6
[ira/wip.git] / lib / tsocket / tsocket_bsd.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Stefan Metzmacher 2009
5
6      ** NOTE! The following LGPL license applies to the tevent
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
30 static 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
73 static 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
150 static ssize_t tsocket_bsd_pending(int fd)
151 {
152         int ret;
153         int value = 0;
154
155         ret = ioctl(fd, FIONREAD, &value);
156         if (ret == -1) {
157                 return ret;
158         }
159
160         if (ret == 0) {
161                 if (value == 0) {
162                         int error=0;
163                         socklen_t len = sizeof(error);
164                         /*
165                          * if no data is available check if the socket
166                          * is in error state. For dgram sockets
167                          * it's the way to return ICMP error messages
168                          * of connected sockets to the caller.
169                          */
170                         ret = getsockopt(fd, SOL_SOCKET, SO_ERROR,
171                                          &error, &len);
172                         if (ret == -1) {
173                                 return ret;
174                         }
175                         if (error != 0) {
176                                 errno = error;
177                                 return -1;
178                         }
179                 }
180                 return value;
181         }
182
183         /* this should not be reached */
184         errno = EIO;
185         return -1;
186 }
187
188 static const struct tsocket_address_ops tsocket_address_bsd_ops;
189
190 struct tsocket_address_bsd {
191         union {
192                 struct sockaddr sa;
193                 struct sockaddr_in in;
194 #ifdef HAVE_IPV6
195                 struct sockaddr_in6 in6;
196 #endif
197                 struct sockaddr_un un;
198                 struct sockaddr_storage ss;
199         } u;
200 };
201
202 static int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
203                                               struct sockaddr *sa,
204                                               socklen_t sa_len,
205                                               struct tsocket_address **_addr,
206                                               const char *location)
207 {
208         struct tsocket_address *addr;
209         struct tsocket_address_bsd *bsda;
210
211         switch (sa->sa_family) {
212         case AF_UNIX:
213                 if (sa_len < sizeof(struct sockaddr_un)) {
214                         errno = EINVAL;
215                         return -1;
216                 }
217                 break;
218         case AF_INET:
219                 if (sa_len < sizeof(struct sockaddr_in)) {
220                         errno = EINVAL;
221                         return -1;
222                 }
223                 break;
224 #ifdef HAVE_IPV6
225         case AF_INET6:
226                 if (sa_len < sizeof(struct sockaddr_in6)) {
227                         errno = EINVAL;
228                         return -1;
229                 }
230                 break;
231 #endif
232         default:
233                 errno = EAFNOSUPPORT;
234                 return -1;
235         }
236
237         if (sa_len > sizeof(struct sockaddr_storage)) {
238                 errno = EINVAL;
239                 return -1;
240         }
241
242         addr = tsocket_address_create(mem_ctx,
243                                       &tsocket_address_bsd_ops,
244                                       &bsda,
245                                       struct tsocket_address_bsd,
246                                       location);
247         if (!addr) {
248                 errno = ENOMEM;
249                 return -1;
250         }
251
252         ZERO_STRUCTP(bsda);
253
254         memcpy(&bsda->u.ss, sa, sa_len);
255
256         *_addr = addr;
257         return 0;
258 }
259
260 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
261                                        const char *fam,
262                                        const char *addr,
263                                        uint16_t port,
264                                        struct tsocket_address **_addr,
265                                        const char *location)
266 {
267         struct addrinfo hints;
268         struct addrinfo *result = NULL;
269         char port_str[6];
270         int ret;
271
272         ZERO_STRUCT(hints);
273         /*
274          * we use SOCKET_STREAM here to get just one result
275          * back from getaddrinfo().
276          */
277         hints.ai_socktype = SOCK_STREAM;
278         hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
279
280         if (strcasecmp(fam, "ip") == 0) {
281                 hints.ai_family = AF_UNSPEC;
282                 if (!addr) {
283 #ifdef HAVE_IPV6
284                         addr = "::";
285 #else
286                         addr = "0.0.0.0";
287 #endif
288                 }
289         } else if (strcasecmp(fam, "ipv4") == 0) {
290                 hints.ai_family = AF_INET;
291                 if (!addr) {
292                         addr = "0.0.0.0";
293                 }
294 #ifdef HAVE_IPV6
295         } else if (strcasecmp(fam, "ipv6") == 0) {
296                 hints.ai_family = AF_INET6;
297                 if (!addr) {
298                         addr = "::";
299                 }
300 #endif
301         } else {
302                 errno = EAFNOSUPPORT;
303                 return -1;
304         }
305
306         snprintf(port_str, sizeof(port_str) - 1, "%u", port);
307
308         ret = getaddrinfo(addr, port_str, &hints, &result);
309         if (ret != 0) {
310                 switch (ret) {
311                 case EAI_FAIL:
312                         errno = EINVAL;
313                         break;
314                 }
315                 ret = -1;
316                 goto done;
317         }
318
319         if (result->ai_socktype != SOCK_STREAM) {
320                 errno = EINVAL;
321                 ret = -1;
322                 goto done;
323         }
324
325         ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
326                                                   result->ai_addr,
327                                                   result->ai_addrlen,
328                                                   _addr,
329                                                   location);
330
331 done:
332         if (result) {
333                 freeaddrinfo(result);
334         }
335         return ret;
336 }
337
338 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
339                                        TALLOC_CTX *mem_ctx)
340 {
341         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
342                                            struct tsocket_address_bsd);
343         char addr_str[INET6_ADDRSTRLEN+1];
344         const char *str;
345
346         if (!bsda) {
347                 errno = EINVAL;
348                 return NULL;
349         }
350
351         switch (bsda->u.sa.sa_family) {
352         case AF_INET:
353                 str = inet_ntop(bsda->u.in.sin_family,
354                                 &bsda->u.in.sin_addr,
355                                 addr_str, sizeof(addr_str));
356                 break;
357 #ifdef HAVE_IPV6
358         case AF_INET6:
359                 str = inet_ntop(bsda->u.in6.sin6_family,
360                                 &bsda->u.in6.sin6_addr,
361                                 addr_str, sizeof(addr_str));
362                 break;
363 #endif
364         default:
365                 errno = EINVAL;
366                 return NULL;
367         }
368
369         if (!str) {
370                 return NULL;
371         }
372
373         return talloc_strdup(mem_ctx, str);
374 }
375
376 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr)
377 {
378         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
379                                            struct tsocket_address_bsd);
380         uint16_t port = 0;
381
382         if (!bsda) {
383                 errno = EINVAL;
384                 return 0;
385         }
386
387         switch (bsda->u.sa.sa_family) {
388         case AF_INET:
389                 port = ntohs(bsda->u.in.sin_port);
390                 break;
391 #ifdef HAVE_IPV6
392         case AF_INET6:
393                 port = ntohs(bsda->u.in6.sin6_port);
394                 break;
395 #endif
396         default:
397                 errno = EINVAL;
398                 return 0;
399         }
400
401         return port;
402 }
403
404 int tsocket_address_inet_set_port(struct tsocket_address *addr,
405                                   uint16_t port)
406 {
407         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
408                                            struct tsocket_address_bsd);
409
410         if (!bsda) {
411                 errno = EINVAL;
412                 return -1;
413         }
414
415         switch (bsda->u.sa.sa_family) {
416         case AF_INET:
417                 bsda->u.in.sin_port = htons(port);
418                 break;
419 #ifdef HAVE_IPV6
420         case AF_INET6:
421                 bsda->u.in6.sin6_port = htons(port);
422                 break;
423 #endif
424         default:
425                 errno = EINVAL;
426                 return -1;
427         }
428
429         return 0;
430 }
431
432 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
433                                     const char *path,
434                                     struct tsocket_address **_addr,
435                                     const char *location)
436 {
437         struct sockaddr_un un;
438         void *p = &un;
439         int ret;
440
441         if (!path) {
442                 path = "";
443         }
444
445         ZERO_STRUCT(un);
446         un.sun_family = AF_UNIX;
447         strncpy(un.sun_path, path, sizeof(un.sun_path));
448
449         ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
450                                                  (struct sockaddr *)p,
451                                                  sizeof(un),
452                                                  _addr,
453                                                  location);
454
455         return ret;
456 }
457
458 char *tsocket_address_unix_path(const struct tsocket_address *addr,
459                                 TALLOC_CTX *mem_ctx)
460 {
461         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
462                                            struct tsocket_address_bsd);
463         const char *str;
464
465         if (!bsda) {
466                 errno = EINVAL;
467                 return NULL;
468         }
469
470         switch (bsda->u.sa.sa_family) {
471         case AF_UNIX:
472                 str = bsda->u.un.sun_path;
473                 break;
474         default:
475                 errno = EINVAL;
476                 return NULL;
477         }
478
479         return talloc_strdup(mem_ctx, str);
480 }
481
482 static char *tsocket_address_bsd_string(const struct tsocket_address *addr,
483                                         TALLOC_CTX *mem_ctx)
484 {
485         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
486                                            struct tsocket_address_bsd);
487         char *str;
488         char *addr_str;
489         const char *prefix = NULL;
490         uint16_t port;
491
492         switch (bsda->u.sa.sa_family) {
493         case AF_UNIX:
494                 return talloc_asprintf(mem_ctx, "unix:%s",
495                                        bsda->u.un.sun_path);
496         case AF_INET:
497                 prefix = "ipv4";
498                 break;
499 #ifdef HAVE_IPV6
500         case AF_INET6:
501                 prefix = "ipv6";
502                 break;
503 #endif
504         default:
505                 errno = EINVAL;
506                 return NULL;
507         }
508
509         addr_str = tsocket_address_inet_addr_string(addr, mem_ctx);
510         if (!addr_str) {
511                 return NULL;
512         }
513
514         port = tsocket_address_inet_port(addr);
515
516         str = talloc_asprintf(mem_ctx, "%s:%s:%u",
517                               prefix, addr_str, port);
518         talloc_free(addr_str);
519
520         return str;
521 }
522
523 static struct tsocket_address *tsocket_address_bsd_copy(const struct tsocket_address *addr,
524                                                          TALLOC_CTX *mem_ctx,
525                                                          const char *location)
526 {
527         struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
528                                            struct tsocket_address_bsd);
529         struct tsocket_address *copy;
530         int ret;
531
532         ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
533                                                  &bsda->u.sa,
534                                                  sizeof(bsda->u.ss),
535                                                  &copy,
536                                                  location);
537         if (ret != 0) {
538                 return NULL;
539         }
540
541         return copy;
542 }
543
544 static const struct tsocket_address_ops tsocket_address_bsd_ops = {
545         .name           = "bsd",
546         .string         = tsocket_address_bsd_string,
547         .copy           = tsocket_address_bsd_copy,
548 };
549
550 struct tdgram_bsd {
551         int fd;
552
553         void *event_ptr;
554         struct tevent_fd *fde;
555
556         void *readable_private;
557         void (*readable_handler)(void *private_data);
558         void *writeable_private;
559         void (*writeable_handler)(void *private_data);
560
561         struct tevent_req *read_req;
562         struct tevent_req *write_req;
563 };
564
565 static void tdgram_bsd_fde_handler(struct tevent_context *ev,
566                                    struct tevent_fd *fde,
567                                    uint16_t flags,
568                                    void *private_data)
569 {
570         struct tdgram_bsd *bsds = talloc_get_type_abort(private_data,
571                                   struct tdgram_bsd);
572
573         if (flags & TEVENT_FD_WRITE) {
574                 bsds->writeable_handler(bsds->writeable_private);
575                 return;
576         }
577         if (flags & TEVENT_FD_READ) {
578                 if (!bsds->readable_handler) {
579                         TEVENT_FD_NOT_READABLE(bsds->fde);
580                         return;
581                 }
582                 bsds->readable_handler(bsds->readable_private);
583                 return;
584         }
585 }
586
587 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd *bsds,
588                                            struct tevent_context *ev,
589                                            void (*handler)(void *private_data),
590                                            void *private_data)
591 {
592         if (ev == NULL) {
593                 if (handler) {
594                         errno = EINVAL;
595                         return -1;
596                 }
597                 if (!bsds->readable_handler) {
598                         return 0;
599                 }
600                 bsds->readable_handler = NULL;
601                 bsds->readable_private = NULL;
602
603                 return 0;
604         }
605
606         /* read and write must use the same tevent_context */
607         if (bsds->event_ptr != ev) {
608                 if (bsds->readable_handler || bsds->writeable_handler) {
609                         errno = EINVAL;
610                         return -1;
611                 }
612                 bsds->event_ptr = NULL;
613                 TALLOC_FREE(bsds->fde);
614         }
615
616         if (bsds->fde == NULL) {
617                 bsds->fde = tevent_add_fd(ev, bsds,
618                                           bsds->fd, TEVENT_FD_READ,
619                                           tdgram_bsd_fde_handler,
620                                           bsds);
621                 if (!bsds->fde) {
622                         return -1;
623                 }
624
625                 /* cache the event context we're running on */
626                 bsds->event_ptr = ev;
627         } else if (!bsds->readable_handler) {
628                 TEVENT_FD_READABLE(bsds->fde);
629         }
630
631         bsds->readable_handler = handler;
632         bsds->readable_private = private_data;
633
634         return 0;
635 }
636
637 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd *bsds,
638                                             struct tevent_context *ev,
639                                             void (*handler)(void *private_data),
640                                             void *private_data)
641 {
642         if (ev == NULL) {
643                 if (handler) {
644                         errno = EINVAL;
645                         return -1;
646                 }
647                 if (!bsds->writeable_handler) {
648                         return 0;
649                 }
650                 bsds->writeable_handler = NULL;
651                 bsds->writeable_private = NULL;
652                 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
653
654                 return 0;
655         }
656
657         /* read and write must use the same tevent_context */
658         if (bsds->event_ptr != ev) {
659                 if (bsds->readable_handler || bsds->writeable_handler) {
660                         errno = EINVAL;
661                         return -1;
662                 }
663                 bsds->event_ptr = NULL;
664                 TALLOC_FREE(bsds->fde);
665         }
666
667         if (bsds->fde == NULL) {
668                 bsds->fde = tevent_add_fd(ev, bsds,
669                                           bsds->fd, TEVENT_FD_WRITE,
670                                           tdgram_bsd_fde_handler,
671                                           bsds);
672                 if (!bsds->fde) {
673                         return -1;
674                 }
675
676                 /* cache the event context we're running on */
677                 bsds->event_ptr = ev;
678         } else if (!bsds->writeable_handler) {
679                 TEVENT_FD_WRITEABLE(bsds->fde);
680         }
681
682         bsds->writeable_handler = handler;
683         bsds->writeable_private = private_data;
684
685         return 0;
686 }
687
688 struct tdgram_bsd_recvfrom_state {
689         struct tdgram_context *dgram;
690
691         uint8_t *buf;
692         size_t len;
693         struct tsocket_address *src;
694 };
695
696 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state *state)
697 {
698         struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
699                                   struct tdgram_bsd);
700
701         bsds->read_req = NULL;
702         tdgram_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
703
704         return 0;
705 }
706
707 static void tdgram_bsd_recvfrom_handler(void *private_data);
708
709 static struct tevent_req *tdgram_bsd_recvfrom_send(TALLOC_CTX *mem_ctx,
710                                         struct tevent_context *ev,
711                                         struct tdgram_context *dgram)
712 {
713         struct tevent_req *req;
714         struct tdgram_bsd_recvfrom_state *state;
715         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
716         int ret;
717
718         req = tevent_req_create(mem_ctx, &state,
719                                 struct tdgram_bsd_recvfrom_state);
720         if (!req) {
721                 return NULL;
722         }
723
724         state->dgram    = dgram;
725         state->buf      = NULL;
726         state->len      = 0;
727         state->src      = NULL;
728
729         if (bsds->read_req) {
730                 tevent_req_error(req, EBUSY);
731                 goto post;
732         }
733         bsds->read_req = req;
734
735         talloc_set_destructor(state, tdgram_bsd_recvfrom_destructor);
736
737         if (bsds->fd == -1) {
738                 tevent_req_error(req, ENOTCONN);
739                 goto post;
740         }
741
742         /*
743          * this is a fast path, not waiting for the
744          * socket to become explicit readable gains
745          * about 10%-20% performance in benchmark tests.
746          */
747         tdgram_bsd_recvfrom_handler(req);
748         if (!tevent_req_is_in_progress(req)) {
749                 goto post;
750         }
751
752         ret = tdgram_bsd_set_readable_handler(bsds, ev,
753                                               tdgram_bsd_recvfrom_handler,
754                                               req);
755         if (ret == -1) {
756                 tevent_req_error(req, errno);
757                 goto post;
758         }
759
760         return req;
761
762  post:
763         tevent_req_post(req, ev);
764         return req;
765 }
766
767 static void tdgram_bsd_recvfrom_handler(void *private_data)
768 {
769         struct tevent_req *req = talloc_get_type_abort(private_data,
770                                  struct tevent_req);
771         struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
772                                         struct tdgram_bsd_recvfrom_state);
773         struct tdgram_context *dgram = state->dgram;
774         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
775         struct tsocket_address_bsd *bsda;
776         ssize_t ret;
777         struct sockaddr *sa = NULL;
778         socklen_t sa_len = 0;
779         int err;
780         bool retry;
781
782         ret = tsocket_bsd_pending(bsds->fd);
783         if (ret == 0) {
784                 /* retry later */
785                 return;
786         }
787         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
788         if (retry) {
789                 /* retry later */
790                 return;
791         }
792         if (tevent_req_error(req, err)) {
793                 return;
794         }
795
796         state->buf = talloc_array(state, uint8_t, ret);
797         if (tevent_req_nomem(state->buf, req)) {
798                 return;
799         }
800         state->len = ret;
801
802         state->src = tsocket_address_create(state,
803                                             &tsocket_address_bsd_ops,
804                                             &bsda,
805                                             struct tsocket_address_bsd,
806                                             __location__ "bsd_recvfrom");
807         if (tevent_req_nomem(state->src, req)) {
808                 return;
809         }
810
811         ZERO_STRUCTP(bsda);
812
813         sa = &bsda->u.sa;
814         sa_len = sizeof(bsda->u.ss);
815
816         ret = recvfrom(bsds->fd, state->buf, state->len, 0, sa, &sa_len);
817         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
818         if (retry) {
819                 /* retry later */
820                 return;
821         }
822         if (tevent_req_error(req, err)) {
823                 return;
824         }
825
826         if (ret != state->len) {
827                 tevent_req_error(req, EIO);
828                 return;
829         }
830
831         tevent_req_done(req);
832 }
833
834 static ssize_t tdgram_bsd_recvfrom_recv(struct tevent_req *req,
835                                         int *perrno,
836                                         TALLOC_CTX *mem_ctx,
837                                         uint8_t **buf,
838                                         struct tsocket_address **src)
839 {
840         struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
841                                         struct tdgram_bsd_recvfrom_state);
842         ssize_t ret;
843
844         ret = tsocket_simple_int_recv(req, perrno);
845         if (ret == 0) {
846                 *buf = talloc_move(mem_ctx, &state->buf);
847                 ret = state->len;
848                 if (src) {
849                         *src = talloc_move(mem_ctx, &state->src);
850                 }
851         }
852
853         tevent_req_received(req);
854         return ret;
855 }
856
857 struct tdgram_bsd_sendto_state {
858         struct tdgram_context *dgram;
859
860         const uint8_t *buf;
861         size_t len;
862         const struct tsocket_address *dst;
863
864         ssize_t ret;
865 };
866
867 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
868 {
869         struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
870                                   struct tdgram_bsd);
871
872         bsds->write_req = NULL;
873         tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
874         return 0;
875 }
876
877 static void tdgram_bsd_sendto_handler(void *private_data);
878
879 static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
880                                                  struct tevent_context *ev,
881                                                  struct tdgram_context *dgram,
882                                                  const uint8_t *buf,
883                                                  size_t len,
884                                                  const struct tsocket_address *dst)
885 {
886         struct tevent_req *req;
887         struct tdgram_bsd_sendto_state *state;
888         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
889         int ret;
890
891         req = tevent_req_create(mem_ctx, &state,
892                                 struct tdgram_bsd_sendto_state);
893         if (!req) {
894                 return NULL;
895         }
896
897         state->dgram    = dgram;
898         state->buf      = buf;
899         state->len      = len;
900         state->dst      = dst;
901         state->ret      = -1;
902
903         if (bsds->write_req) {
904                 tevent_req_error(req, EBUSY);
905                 goto post;
906         }
907         bsds->write_req = req;
908
909         talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
910
911         if (bsds->fd == -1) {
912                 tevent_req_error(req, ENOTCONN);
913                 goto post;
914         }
915
916         /*
917          * this is a fast path, not waiting for the
918          * socket to become explicit writeable gains
919          * about 10%-20% performance in benchmark tests.
920          */
921         tdgram_bsd_sendto_handler(req);
922         if (!tevent_req_is_in_progress(req)) {
923                 goto post;
924         }
925
926         ret = tdgram_bsd_set_writeable_handler(bsds, ev,
927                                                tdgram_bsd_sendto_handler,
928                                                req);
929         if (ret == -1) {
930                 tevent_req_error(req, errno);
931                 goto post;
932         }
933
934         return req;
935
936  post:
937         tevent_req_post(req, ev);
938         return req;
939 }
940
941 static void tdgram_bsd_sendto_handler(void *private_data)
942 {
943         struct tevent_req *req = talloc_get_type_abort(private_data,
944                                  struct tevent_req);
945         struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
946                                         struct tdgram_bsd_sendto_state);
947         struct tdgram_context *dgram = state->dgram;
948         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
949         struct sockaddr *sa = NULL;
950         socklen_t sa_len = 0;
951         ssize_t ret;
952         int err;
953         bool retry;
954
955         if (state->dst) {
956                 struct tsocket_address_bsd *bsda =
957                         talloc_get_type(state->dst->private_data,
958                         struct tsocket_address_bsd);
959
960                 sa = &bsda->u.sa;
961                 sa_len = sizeof(bsda->u.ss);
962         }
963
964         ret = sendto(bsds->fd, state->buf, state->len, 0, sa, sa_len);
965         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
966         if (retry) {
967                 /* retry later */
968                 return;
969         }
970         if (tevent_req_error(req, err)) {
971                 return;
972         }
973
974         state->ret = ret;
975
976         tevent_req_done(req);
977 }
978
979 static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
980 {
981         struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
982                                         struct tdgram_bsd_sendto_state);
983         ssize_t ret;
984
985         ret = tsocket_simple_int_recv(req, perrno);
986         if (ret == 0) {
987                 ret = state->ret;
988         }
989
990         tevent_req_received(req);
991         return ret;
992 }
993
994 struct tdgram_bsd_disconnect_state {
995         uint8_t __dummy;
996 };
997
998 static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
999                                                      struct tevent_context *ev,
1000                                                      struct tdgram_context *dgram)
1001 {
1002         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1003         struct tevent_req *req;
1004         struct tdgram_bsd_disconnect_state *state;
1005         int ret;
1006         int err;
1007         bool dummy;
1008
1009         req = tevent_req_create(mem_ctx, &state,
1010                                 struct tdgram_bsd_disconnect_state);
1011         if (req == NULL) {
1012                 return NULL;
1013         }
1014
1015         if (bsds->read_req || bsds->write_req) {
1016                 tevent_req_error(req, EBUSY);
1017                 goto post;
1018         }
1019
1020         if (bsds->fd == -1) {
1021                 tevent_req_error(req, ENOTCONN);
1022                 goto post;
1023         }
1024
1025         ret = close(bsds->fd);
1026         bsds->fd = -1;
1027         err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1028         if (tevent_req_error(req, err)) {
1029                 goto post;
1030         }
1031
1032         tevent_req_done(req);
1033 post:
1034         tevent_req_post(req, ev);
1035         return req;
1036 }
1037
1038 static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1039                                       int *perrno)
1040 {
1041         int ret;
1042
1043         ret = tsocket_simple_int_recv(req, perrno);
1044
1045         tevent_req_received(req);
1046         return ret;
1047 }
1048
1049 static const struct tdgram_context_ops tdgram_bsd_ops = {
1050         .name                   = "bsd",
1051
1052         .recvfrom_send          = tdgram_bsd_recvfrom_send,
1053         .recvfrom_recv          = tdgram_bsd_recvfrom_recv,
1054
1055         .sendto_send            = tdgram_bsd_sendto_send,
1056         .sendto_recv            = tdgram_bsd_sendto_recv,
1057
1058         .disconnect_send        = tdgram_bsd_disconnect_send,
1059         .disconnect_recv        = tdgram_bsd_disconnect_recv,
1060 };
1061
1062 static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1063 {
1064         TALLOC_FREE(bsds->fde);
1065         if (bsds->fd != -1) {
1066                 close(bsds->fd);
1067                 bsds->fd = -1;
1068         }
1069         return 0;
1070 }
1071
1072 static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1073                                    const struct tsocket_address *remote,
1074                                    bool broadcast,
1075                                    TALLOC_CTX *mem_ctx,
1076                                    struct tdgram_context **_dgram,
1077                                    const char *location)
1078 {
1079         struct tsocket_address_bsd *lbsda =
1080                 talloc_get_type_abort(local->private_data,
1081                 struct tsocket_address_bsd);
1082         struct tsocket_address_bsd *rbsda = NULL;
1083         struct tdgram_context *dgram;
1084         struct tdgram_bsd *bsds;
1085         int fd;
1086         int ret;
1087         bool do_bind = false;
1088         bool do_reuseaddr = false;
1089
1090         if (remote) {
1091                 rbsda = talloc_get_type_abort(remote->private_data,
1092                         struct tsocket_address_bsd);
1093         }
1094
1095         switch (lbsda->u.sa.sa_family) {
1096         case AF_UNIX:
1097                 if (broadcast) {
1098                         errno = EINVAL;
1099                         return -1;
1100                 }
1101                 if (lbsda->u.un.sun_path[0] != 0) {
1102                         do_reuseaddr = true;
1103                         do_bind = true;
1104                 }
1105                 break;
1106         case AF_INET:
1107                 if (lbsda->u.in.sin_port != 0) {
1108                         do_reuseaddr = true;
1109                         do_bind = true;
1110                 }
1111                 if (lbsda->u.in.sin_addr.s_addr == INADDR_ANY) {
1112                         do_bind = true;
1113                 }
1114                 break;
1115 #ifdef HAVE_IPV6
1116         case AF_INET6:
1117                 if (lbsda->u.in6.sin6_port != 0) {
1118                         do_reuseaddr = true;
1119                         do_bind = true;
1120                 }
1121                 if (memcmp(&in6addr_any,
1122                            &lbsda->u.in6.sin6_addr,
1123                            sizeof(in6addr_any)) != 0) {
1124                         do_bind = true;
1125                 }
1126                 break;
1127 #endif
1128         default:
1129                 errno = EINVAL;
1130                 return -1;
1131         }
1132
1133         fd = socket(lbsda->u.sa.sa_family, SOCK_DGRAM, 0);
1134         if (fd < 0) {
1135                 return fd;
1136         }
1137
1138         fd = tsocket_bsd_common_prepare_fd(fd, true);
1139         if (fd < 0) {
1140                 return fd;
1141         }
1142
1143         dgram = tdgram_context_create(mem_ctx,
1144                                       &tdgram_bsd_ops,
1145                                       &bsds,
1146                                       struct tdgram_bsd,
1147                                       location);
1148         if (!dgram) {
1149                 int saved_errno = errno;
1150                 close(fd);
1151                 errno = saved_errno;
1152                 return -1;
1153         }
1154         ZERO_STRUCTP(bsds);
1155         bsds->fd = fd;
1156         talloc_set_destructor(bsds, tdgram_bsd_destructor);
1157
1158         if (broadcast) {
1159                 int val = 1;
1160
1161                 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1162                                  (const void *)&val, sizeof(val));
1163                 if (ret == -1) {
1164                         int saved_errno = errno;
1165                         talloc_free(dgram);
1166                         errno = saved_errno;
1167                         return ret;
1168                 }
1169         }
1170
1171         if (do_reuseaddr) {
1172                 int val = 1;
1173
1174                 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1175                                  (const void *)&val, sizeof(val));
1176                 if (ret == -1) {
1177                         int saved_errno = errno;
1178                         talloc_free(dgram);
1179                         errno = saved_errno;
1180                         return ret;
1181                 }
1182         }
1183
1184         if (do_bind) {
1185                 ret = bind(fd, &lbsda->u.sa, sizeof(lbsda->u.ss));
1186                 if (ret == -1) {
1187                         int saved_errno = errno;
1188                         talloc_free(dgram);
1189                         errno = saved_errno;
1190                         return ret;
1191                 }
1192         }
1193
1194         if (rbsda) {
1195                 ret = connect(fd, &rbsda->u.sa, sizeof(rbsda->u.ss));
1196                 if (ret == -1) {
1197                         int saved_errno = errno;
1198                         talloc_free(dgram);
1199                         errno = saved_errno;
1200                         return ret;
1201                 }
1202         }
1203
1204         *_dgram = dgram;
1205         return 0;
1206 }
1207
1208 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1209                             const struct tsocket_address *remote,
1210                             TALLOC_CTX *mem_ctx,
1211                             struct tdgram_context **dgram,
1212                             const char *location)
1213 {
1214         struct tsocket_address_bsd *lbsda =
1215                 talloc_get_type_abort(local->private_data,
1216                 struct tsocket_address_bsd);
1217         int ret;
1218
1219         switch (lbsda->u.sa.sa_family) {
1220         case AF_INET:
1221                 break;
1222 #ifdef HAVE_IPV6
1223         case AF_INET6:
1224                 break;
1225 #endif
1226         default:
1227                 errno = EINVAL;
1228                 return -1;
1229         }
1230
1231         ret = tdgram_bsd_dgram_socket(local, remote, false,
1232                                       mem_ctx, dgram, location);
1233
1234         return ret;
1235 }
1236
1237 int _tdgram_unix_dgram_socket(const struct tsocket_address *local,
1238                               const struct tsocket_address *remote,
1239                               TALLOC_CTX *mem_ctx,
1240                               struct tdgram_context **dgram,
1241                               const char *location)
1242 {
1243         struct tsocket_address_bsd *lbsda =
1244                 talloc_get_type_abort(local->private_data,
1245                 struct tsocket_address_bsd);
1246         int ret;
1247
1248         switch (lbsda->u.sa.sa_family) {
1249         case AF_UNIX:
1250                 break;
1251         default:
1252                 errno = EINVAL;
1253                 return -1;
1254         }
1255
1256         ret = tdgram_bsd_dgram_socket(local, remote, false,
1257                                       mem_ctx, dgram, location);
1258
1259         return ret;
1260 }
1261