tsocket: for unix domain sockets we need to use sizeof(struct sockaddr_un)
[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          * for unix sockets we can't use the size of sockaddr_storage
817          * we would get EINVAL
818          */
819         if (bsda->u.sa.sa_family == AF_UNIX) {
820                 sa_len = sizeof(bsda->u.un);
821         }
822
823         ret = recvfrom(bsds->fd, state->buf, state->len, 0, sa, &sa_len);
824         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
825         if (retry) {
826                 /* retry later */
827                 return;
828         }
829         if (tevent_req_error(req, err)) {
830                 return;
831         }
832
833         if (ret != state->len) {
834                 tevent_req_error(req, EIO);
835                 return;
836         }
837
838         tevent_req_done(req);
839 }
840
841 static ssize_t tdgram_bsd_recvfrom_recv(struct tevent_req *req,
842                                         int *perrno,
843                                         TALLOC_CTX *mem_ctx,
844                                         uint8_t **buf,
845                                         struct tsocket_address **src)
846 {
847         struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
848                                         struct tdgram_bsd_recvfrom_state);
849         ssize_t ret;
850
851         ret = tsocket_simple_int_recv(req, perrno);
852         if (ret == 0) {
853                 *buf = talloc_move(mem_ctx, &state->buf);
854                 ret = state->len;
855                 if (src) {
856                         *src = talloc_move(mem_ctx, &state->src);
857                 }
858         }
859
860         tevent_req_received(req);
861         return ret;
862 }
863
864 struct tdgram_bsd_sendto_state {
865         struct tdgram_context *dgram;
866
867         const uint8_t *buf;
868         size_t len;
869         const struct tsocket_address *dst;
870
871         ssize_t ret;
872 };
873
874 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
875 {
876         struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
877                                   struct tdgram_bsd);
878
879         bsds->write_req = NULL;
880         tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
881         return 0;
882 }
883
884 static void tdgram_bsd_sendto_handler(void *private_data);
885
886 static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
887                                                  struct tevent_context *ev,
888                                                  struct tdgram_context *dgram,
889                                                  const uint8_t *buf,
890                                                  size_t len,
891                                                  const struct tsocket_address *dst)
892 {
893         struct tevent_req *req;
894         struct tdgram_bsd_sendto_state *state;
895         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
896         int ret;
897
898         req = tevent_req_create(mem_ctx, &state,
899                                 struct tdgram_bsd_sendto_state);
900         if (!req) {
901                 return NULL;
902         }
903
904         state->dgram    = dgram;
905         state->buf      = buf;
906         state->len      = len;
907         state->dst      = dst;
908         state->ret      = -1;
909
910         if (bsds->write_req) {
911                 tevent_req_error(req, EBUSY);
912                 goto post;
913         }
914         bsds->write_req = req;
915
916         talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
917
918         if (bsds->fd == -1) {
919                 tevent_req_error(req, ENOTCONN);
920                 goto post;
921         }
922
923         /*
924          * this is a fast path, not waiting for the
925          * socket to become explicit writeable gains
926          * about 10%-20% performance in benchmark tests.
927          */
928         tdgram_bsd_sendto_handler(req);
929         if (!tevent_req_is_in_progress(req)) {
930                 goto post;
931         }
932
933         ret = tdgram_bsd_set_writeable_handler(bsds, ev,
934                                                tdgram_bsd_sendto_handler,
935                                                req);
936         if (ret == -1) {
937                 tevent_req_error(req, errno);
938                 goto post;
939         }
940
941         return req;
942
943  post:
944         tevent_req_post(req, ev);
945         return req;
946 }
947
948 static void tdgram_bsd_sendto_handler(void *private_data)
949 {
950         struct tevent_req *req = talloc_get_type_abort(private_data,
951                                  struct tevent_req);
952         struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
953                                         struct tdgram_bsd_sendto_state);
954         struct tdgram_context *dgram = state->dgram;
955         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
956         struct sockaddr *sa = NULL;
957         socklen_t sa_len = 0;
958         ssize_t ret;
959         int err;
960         bool retry;
961
962         if (state->dst) {
963                 struct tsocket_address_bsd *bsda =
964                         talloc_get_type(state->dst->private_data,
965                         struct tsocket_address_bsd);
966
967                 sa = &bsda->u.sa;
968                 sa_len = sizeof(bsda->u.ss);
969                 /*
970                  * for unix sockets we can't use the size of sockaddr_storage
971                  * we would get EINVAL
972                  */
973                 if (bsda->u.sa.sa_family == AF_UNIX) {
974                         sa_len = sizeof(bsda->u.un);
975                 }
976         }
977
978         ret = sendto(bsds->fd, state->buf, state->len, 0, sa, sa_len);
979         err = tsocket_bsd_error_from_errno(ret, errno, &retry);
980         if (retry) {
981                 /* retry later */
982                 return;
983         }
984         if (tevent_req_error(req, err)) {
985                 return;
986         }
987
988         state->ret = ret;
989
990         tevent_req_done(req);
991 }
992
993 static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
994 {
995         struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
996                                         struct tdgram_bsd_sendto_state);
997         ssize_t ret;
998
999         ret = tsocket_simple_int_recv(req, perrno);
1000         if (ret == 0) {
1001                 ret = state->ret;
1002         }
1003
1004         tevent_req_received(req);
1005         return ret;
1006 }
1007
1008 struct tdgram_bsd_disconnect_state {
1009         uint8_t __dummy;
1010 };
1011
1012 static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1013                                                      struct tevent_context *ev,
1014                                                      struct tdgram_context *dgram)
1015 {
1016         struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1017         struct tevent_req *req;
1018         struct tdgram_bsd_disconnect_state *state;
1019         int ret;
1020         int err;
1021         bool dummy;
1022
1023         req = tevent_req_create(mem_ctx, &state,
1024                                 struct tdgram_bsd_disconnect_state);
1025         if (req == NULL) {
1026                 return NULL;
1027         }
1028
1029         if (bsds->read_req || bsds->write_req) {
1030                 tevent_req_error(req, EBUSY);
1031                 goto post;
1032         }
1033
1034         if (bsds->fd == -1) {
1035                 tevent_req_error(req, ENOTCONN);
1036                 goto post;
1037         }
1038
1039         ret = close(bsds->fd);
1040         bsds->fd = -1;
1041         err = tsocket_bsd_error_from_errno(ret, errno, &dummy);
1042         if (tevent_req_error(req, err)) {
1043                 goto post;
1044         }
1045
1046         tevent_req_done(req);
1047 post:
1048         tevent_req_post(req, ev);
1049         return req;
1050 }
1051
1052 static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1053                                       int *perrno)
1054 {
1055         int ret;
1056
1057         ret = tsocket_simple_int_recv(req, perrno);
1058
1059         tevent_req_received(req);
1060         return ret;
1061 }
1062
1063 static const struct tdgram_context_ops tdgram_bsd_ops = {
1064         .name                   = "bsd",
1065
1066         .recvfrom_send          = tdgram_bsd_recvfrom_send,
1067         .recvfrom_recv          = tdgram_bsd_recvfrom_recv,
1068
1069         .sendto_send            = tdgram_bsd_sendto_send,
1070         .sendto_recv            = tdgram_bsd_sendto_recv,
1071
1072         .disconnect_send        = tdgram_bsd_disconnect_send,
1073         .disconnect_recv        = tdgram_bsd_disconnect_recv,
1074 };
1075
1076 static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1077 {
1078         TALLOC_FREE(bsds->fde);
1079         if (bsds->fd != -1) {
1080                 close(bsds->fd);
1081                 bsds->fd = -1;
1082         }
1083         return 0;
1084 }
1085
1086 static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1087                                    const struct tsocket_address *remote,
1088                                    bool broadcast,
1089                                    TALLOC_CTX *mem_ctx,
1090                                    struct tdgram_context **_dgram,
1091                                    const char *location)
1092 {
1093         struct tsocket_address_bsd *lbsda =
1094                 talloc_get_type_abort(local->private_data,
1095                 struct tsocket_address_bsd);
1096         struct tsocket_address_bsd *rbsda = NULL;
1097         struct tdgram_context *dgram;
1098         struct tdgram_bsd *bsds;
1099         int fd;
1100         int ret;
1101         bool do_bind = false;
1102         bool do_reuseaddr = false;
1103         socklen_t sa_len = sizeof(lbsda->u.ss);
1104
1105         if (remote) {
1106                 rbsda = talloc_get_type_abort(remote->private_data,
1107                         struct tsocket_address_bsd);
1108         }
1109
1110         switch (lbsda->u.sa.sa_family) {
1111         case AF_UNIX:
1112                 if (broadcast) {
1113                         errno = EINVAL;
1114                         return -1;
1115                 }
1116                 if (lbsda->u.un.sun_path[0] != 0) {
1117                         do_reuseaddr = true;
1118                         do_bind = true;
1119                 }
1120                 /*
1121                  * for unix sockets we can't use the size of sockaddr_storage
1122                  * we would get EINVAL
1123                  */
1124                 sa_len = sizeof(lbsda->u.un);
1125                 break;
1126         case AF_INET:
1127                 if (lbsda->u.in.sin_port != 0) {
1128                         do_reuseaddr = true;
1129                         do_bind = true;
1130                 }
1131                 if (lbsda->u.in.sin_addr.s_addr == INADDR_ANY) {
1132                         do_bind = true;
1133                 }
1134                 break;
1135 #ifdef HAVE_IPV6
1136         case AF_INET6:
1137                 if (lbsda->u.in6.sin6_port != 0) {
1138                         do_reuseaddr = true;
1139                         do_bind = true;
1140                 }
1141                 if (memcmp(&in6addr_any,
1142                            &lbsda->u.in6.sin6_addr,
1143                            sizeof(in6addr_any)) != 0) {
1144                         do_bind = true;
1145                 }
1146                 break;
1147 #endif
1148         default:
1149                 errno = EINVAL;
1150                 return -1;
1151         }
1152
1153         fd = socket(lbsda->u.sa.sa_family, SOCK_DGRAM, 0);
1154         if (fd < 0) {
1155                 return fd;
1156         }
1157
1158         fd = tsocket_bsd_common_prepare_fd(fd, true);
1159         if (fd < 0) {
1160                 return fd;
1161         }
1162
1163         dgram = tdgram_context_create(mem_ctx,
1164                                       &tdgram_bsd_ops,
1165                                       &bsds,
1166                                       struct tdgram_bsd,
1167                                       location);
1168         if (!dgram) {
1169                 int saved_errno = errno;
1170                 close(fd);
1171                 errno = saved_errno;
1172                 return -1;
1173         }
1174         ZERO_STRUCTP(bsds);
1175         bsds->fd = fd;
1176         talloc_set_destructor(bsds, tdgram_bsd_destructor);
1177
1178         if (broadcast) {
1179                 int val = 1;
1180
1181                 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1182                                  (const void *)&val, sizeof(val));
1183                 if (ret == -1) {
1184                         int saved_errno = errno;
1185                         talloc_free(dgram);
1186                         errno = saved_errno;
1187                         return ret;
1188                 }
1189         }
1190
1191         if (do_reuseaddr) {
1192                 int val = 1;
1193
1194                 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1195                                  (const void *)&val, sizeof(val));
1196                 if (ret == -1) {
1197                         int saved_errno = errno;
1198                         talloc_free(dgram);
1199                         errno = saved_errno;
1200                         return ret;
1201                 }
1202         }
1203
1204         if (do_bind) {
1205                 ret = bind(fd, &lbsda->u.sa, sa_len);
1206                 if (ret == -1) {
1207                         int saved_errno = errno;
1208                         talloc_free(dgram);
1209                         errno = saved_errno;
1210                         return ret;
1211                 }
1212         }
1213
1214         if (rbsda) {
1215                 ret = connect(fd, &rbsda->u.sa, sa_len);
1216                 if (ret == -1) {
1217                         int saved_errno = errno;
1218                         talloc_free(dgram);
1219                         errno = saved_errno;
1220                         return ret;
1221                 }
1222         }
1223
1224         *_dgram = dgram;
1225         return 0;
1226 }
1227
1228 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1229                             const struct tsocket_address *remote,
1230                             TALLOC_CTX *mem_ctx,
1231                             struct tdgram_context **dgram,
1232                             const char *location)
1233 {
1234         struct tsocket_address_bsd *lbsda =
1235                 talloc_get_type_abort(local->private_data,
1236                 struct tsocket_address_bsd);
1237         int ret;
1238
1239         switch (lbsda->u.sa.sa_family) {
1240         case AF_INET:
1241                 break;
1242 #ifdef HAVE_IPV6
1243         case AF_INET6:
1244                 break;
1245 #endif
1246         default:
1247                 errno = EINVAL;
1248                 return -1;
1249         }
1250
1251         ret = tdgram_bsd_dgram_socket(local, remote, false,
1252                                       mem_ctx, dgram, location);
1253
1254         return ret;
1255 }
1256
1257 int _tdgram_unix_dgram_socket(const struct tsocket_address *local,
1258                               const struct tsocket_address *remote,
1259                               TALLOC_CTX *mem_ctx,
1260                               struct tdgram_context **dgram,
1261                               const char *location)
1262 {
1263         struct tsocket_address_bsd *lbsda =
1264                 talloc_get_type_abort(local->private_data,
1265                 struct tsocket_address_bsd);
1266         int ret;
1267
1268         switch (lbsda->u.sa.sa_family) {
1269         case AF_UNIX:
1270                 break;
1271         default:
1272                 errno = EINVAL;
1273                 return -1;
1274         }
1275
1276         ret = tdgram_bsd_dgram_socket(local, remote, false,
1277                                       mem_ctx, dgram, location);
1278
1279         return ret;
1280 }
1281