2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2009
6 ** NOTE! The following LGPL license applies to the tevent
7 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
25 #include "system/filesys.h"
26 #include "system/network.h"
27 #include "system/filesys.h"
29 #include "tsocket_internal.h"
31 static int tsocket_bsd_error_from_errno(int ret,
49 if (sys_errno == EINTR) {
54 if (sys_errno == EINPROGRESS) {
59 if (sys_errno == EAGAIN) {
65 if (sys_errno == EWOULDBLOCK) {
74 static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd)
87 /* first make a fd >= 3 */
97 for (i=0; i<num_fds; i++) {
106 /* fd should be nonblocking. */
109 #define FLAG_TO_SET O_NONBLOCK
112 #define FLAG_TO_SET O_NDELAY
114 #define FLAG_TO_SET FNDELAY
118 if ((flags = fcntl(fd, F_GETFL)) == -1) {
122 flags |= FLAG_TO_SET;
123 if (fcntl(fd, F_SETFL, flags) == -1) {
129 /* fd should be closed on exec() */
131 result = flags = fcntl(fd, F_GETFD, 0);
134 result = fcntl(fd, F_SETFD, flags);
151 static ssize_t tsocket_bsd_pending(int fd)
156 ret = ioctl(fd, FIONREAD, &value);
164 socklen_t len = sizeof(error);
166 * if no data is available check if the socket
167 * is in error state. For dgram sockets
168 * it's the way to return ICMP error messages
169 * of connected sockets to the caller.
171 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR,
184 /* this should not be reached */
189 static const struct tsocket_context_ops tsocket_context_bsd_ops;
190 static const struct tsocket_address_ops tsocket_address_bsd_ops;
192 static int tsocket_context_bsd_set_option(const struct tsocket_context *sock,
197 struct tsocket_context_bsd {
198 bool close_on_disconnect;
200 struct tevent_fd *fde;
203 struct tsocket_address_bsd {
207 struct sockaddr_in in;
209 struct sockaddr_in6 in6;
211 struct sockaddr_un un;
212 struct sockaddr_storage ss;
216 static int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx,
219 struct tsocket_address **_addr,
220 const char *location)
222 struct tsocket_address *addr;
223 struct tsocket_address_bsd *bsda;
225 switch (sa->sa_family) {
227 if (sa_len < sizeof(struct sockaddr_un)) {
233 if (sa_len < sizeof(struct sockaddr_in)) {
240 if (sa_len < sizeof(struct sockaddr_in6)) {
247 errno = EAFNOSUPPORT;
251 if (sa_len > sizeof(struct sockaddr_storage)) {
256 addr = tsocket_address_create(mem_ctx,
257 &tsocket_address_bsd_ops,
259 struct tsocket_address_bsd,
268 memcpy(&bsda->u.ss, sa, sa_len);
274 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
278 struct tsocket_address **_addr,
279 const char *location)
281 struct addrinfo hints;
282 struct addrinfo *result = NULL;
288 * we use SOCKET_STREAM here to get just one result
289 * back from getaddrinfo().
291 hints.ai_socktype = SOCK_STREAM;
292 hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
294 if (strcasecmp(fam, "ip") == 0) {
295 hints.ai_family = AF_UNSPEC;
303 } else if (strcasecmp(fam, "ipv4") == 0) {
304 hints.ai_family = AF_INET;
309 } else if (strcasecmp(fam, "ipv6") == 0) {
310 hints.ai_family = AF_INET6;
316 errno = EAFNOSUPPORT;
320 snprintf(port_str, sizeof(port_str) - 1, "%u", port);
322 ret = getaddrinfo(addr, port_str, &hints, &result);
333 if (result->ai_socktype != SOCK_STREAM) {
339 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
347 freeaddrinfo(result);
352 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
355 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
356 struct tsocket_address_bsd);
357 char addr_str[INET6_ADDRSTRLEN+1];
365 switch (bsda->u.sa.sa_family) {
367 str = inet_ntop(bsda->u.in.sin_family,
368 &bsda->u.in.sin_addr,
369 addr_str, sizeof(addr_str));
373 str = inet_ntop(bsda->u.in6.sin6_family,
374 &bsda->u.in6.sin6_addr,
375 addr_str, sizeof(addr_str));
387 return talloc_strdup(mem_ctx, str);
390 uint16_t tsocket_address_inet_port(const struct tsocket_address *addr)
392 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
393 struct tsocket_address_bsd);
401 switch (bsda->u.sa.sa_family) {
403 port = ntohs(bsda->u.in.sin_port);
407 port = ntohs(bsda->u.in6.sin6_port);
418 int tsocket_address_inet_set_port(struct tsocket_address *addr,
421 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
422 struct tsocket_address_bsd);
429 switch (bsda->u.sa.sa_family) {
431 bsda->u.in.sin_port = htons(port);
435 bsda->u.in6.sin6_port = htons(port);
446 void tsocket_address_inet_set_broadcast(struct tsocket_address *addr,
449 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
450 struct tsocket_address_bsd);
456 bsda->broadcast = broadcast;
459 int _tsocket_address_unix_from_path(TALLOC_CTX *mem_ctx,
461 struct tsocket_address **_addr,
462 const char *location)
464 struct sockaddr_un un;
473 un.sun_family = AF_UNIX;
474 strncpy(un.sun_path, path, sizeof(un.sun_path));
476 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
477 (struct sockaddr *)p,
485 char *tsocket_address_unix_path(const struct tsocket_address *addr,
488 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
489 struct tsocket_address_bsd);
497 switch (bsda->u.sa.sa_family) {
499 str = bsda->u.un.sun_path;
506 return talloc_strdup(mem_ctx, str);
509 static char *tsocket_address_bsd_string(const struct tsocket_address *addr,
512 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
513 struct tsocket_address_bsd);
516 const char *prefix = NULL;
519 switch (bsda->u.sa.sa_family) {
521 return talloc_asprintf(mem_ctx, "unix:%s",
522 bsda->u.un.sun_path);
534 addr_str = tsocket_address_inet_addr_string(addr, mem_ctx);
539 port = tsocket_address_inet_port(addr);
541 str = talloc_asprintf(mem_ctx, "%s:%s:%u",
542 prefix, addr_str, port);
543 talloc_free(addr_str);
548 static struct tsocket_address *tsocket_address_bsd_copy(const struct tsocket_address *addr,
550 const char *location)
552 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
553 struct tsocket_address_bsd);
554 struct tsocket_address *copy;
557 ret = _tsocket_address_bsd_from_sockaddr(mem_ctx,
566 tsocket_address_inet_set_broadcast(copy, bsda->broadcast);
570 int _tsocket_context_bsd_wrap_existing(TALLOC_CTX *mem_ctx,
571 int fd, bool close_on_disconnect,
572 struct tsocket_context **_sock,
573 const char *location)
575 struct tsocket_context *sock;
576 struct tsocket_context_bsd *bsds;
578 sock = tsocket_context_create(mem_ctx,
579 &tsocket_context_bsd_ops,
581 struct tsocket_context_bsd,
587 bsds->close_on_disconnect = close_on_disconnect;
595 static int tsocket_address_bsd_create_socket(const struct tsocket_address *addr,
596 enum tsocket_type type,
598 struct tsocket_context **_sock,
599 const char *location)
601 struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
602 struct tsocket_address_bsd);
603 struct tsocket_context *sock;
607 bool do_bind = false;
608 bool do_reuseaddr = false;
611 case TSOCKET_TYPE_STREAM:
612 if (bsda->broadcast) {
616 bsd_type = SOCK_STREAM;
618 case TSOCKET_TYPE_DGRAM:
619 bsd_type = SOCK_DGRAM;
622 errno = EPROTONOSUPPORT;
626 switch (bsda->u.sa.sa_family) {
628 if (bsda->broadcast) {
632 if (bsda->u.un.sun_path[0] != 0) {
637 if (bsda->u.in.sin_port != 0) {
641 if (bsda->u.in.sin_addr.s_addr == INADDR_ANY) {
647 if (bsda->u.in6.sin6_port != 0) {
651 if (memcmp(&in6addr_any,
652 &bsda->u.in6.sin6_addr,
653 sizeof(in6addr_any)) != 0) {
663 fd = socket(bsda->u.sa.sa_family, bsd_type, 0);
668 fd = tsocket_common_prepare_fd(fd, true);
673 ret = _tsocket_context_bsd_wrap_existing(mem_ctx, fd, true,
676 int saved_errno = errno;
682 if (bsda->broadcast) {
683 ret = tsocket_context_bsd_set_option(sock, "SO_BROADCAST", true, "1");
685 int saved_errno = errno;
693 ret = tsocket_context_bsd_set_option(sock, "SO_REUSEADDR", true, "1");
695 int saved_errno = errno;
703 ret = bind(fd, &bsda->u.sa, sizeof(bsda->u.ss));
705 int saved_errno = errno;
716 static const struct tsocket_address_ops tsocket_address_bsd_ops = {
718 .string = tsocket_address_bsd_string,
719 .copy = tsocket_address_bsd_copy,
720 .create_socket = tsocket_address_bsd_create_socket
723 static void tsocket_context_bsd_fde_handler(struct tevent_context *ev,
724 struct tevent_fd *fde,
728 struct tsocket_context *sock = talloc_get_type(private_data,
729 struct tsocket_context);
731 if (flags & TEVENT_FD_WRITE) {
732 sock->event.write_handler(sock, sock->event.write_private);
735 if (flags & TEVENT_FD_READ) {
736 sock->event.read_handler(sock, sock->event.read_private);
741 static int tsocket_context_bsd_set_event_context(struct tsocket_context *sock,
742 struct tevent_context *ev)
744 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
745 struct tsocket_context_bsd);
747 talloc_free(bsds->fde);
749 ZERO_STRUCT(sock->event);
755 bsds->fde = tevent_add_fd(ev, bsds,
758 tsocket_context_bsd_fde_handler,
767 sock->event.ctx = ev;
772 static int tsocket_context_bsd_set_read_handler(struct tsocket_context *sock,
773 tsocket_event_handler_t handler,
776 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
777 struct tsocket_context_bsd);
779 if (sock->event.read_handler && !handler) {
780 TEVENT_FD_NOT_READABLE(bsds->fde);
781 } else if (!sock->event.read_handler && handler) {
782 TEVENT_FD_READABLE(bsds->fde);
785 sock->event.read_handler = handler;
786 sock->event.read_private = private_data;
791 static int tsocket_context_bsd_set_write_handler(struct tsocket_context *sock,
792 tsocket_event_handler_t handler,
795 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
796 struct tsocket_context_bsd);
798 if (sock->event.write_handler && !handler) {
799 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
800 } else if (!sock->event.write_handler && handler) {
801 TEVENT_FD_WRITEABLE(bsds->fde);
804 sock->event.write_handler = handler;
805 sock->event.write_private = private_data;
810 static int tsocket_context_bsd_connect_to(struct tsocket_context *sock,
811 const struct tsocket_address *remote)
813 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
814 struct tsocket_context_bsd);
815 struct tsocket_address_bsd *bsda = talloc_get_type(remote->private_data,
816 struct tsocket_address_bsd);
819 ret = connect(bsds->fd, &bsda->u.sa,
825 static int tsocket_context_bsd_listen_on(struct tsocket_context *sock,
828 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
829 struct tsocket_context_bsd);
832 ret = listen(bsds->fd, queue_size);
837 static int tsocket_context_bsd_accept_new(struct tsocket_context *sock,
839 struct tsocket_context **_new_sock,
840 const char *location)
842 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
843 struct tsocket_context_bsd);
845 struct tsocket_context *new_sock;
846 struct tsocket_context_bsd *new_bsds;
847 struct sockaddr_storage ss;
849 socklen_t ss_len = sizeof(ss);
851 new_fd = accept(bsds->fd, (struct sockaddr *)p, &ss_len);
856 new_fd = tsocket_common_prepare_fd(new_fd, true);
861 new_sock = tsocket_context_create(mem_ctx,
862 &tsocket_context_bsd_ops,
864 struct tsocket_context_bsd,
867 int saved_errno = errno;
873 new_bsds->close_on_disconnect = true;
874 new_bsds->fd = new_fd;
875 new_bsds->fde = NULL;
877 *_new_sock = new_sock;
881 static ssize_t tsocket_context_bsd_pending_data(struct tsocket_context *sock)
883 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
884 struct tsocket_context_bsd);
888 ret = ioctl(bsds->fd, FIONREAD, &value);
896 socklen_t len = sizeof(error);
898 * if no data is available check if the socket
899 * is in error state. For dgram sockets
900 * it's the way to return ICMP error messages
901 * of connected sockets to the caller.
903 ret = getsockopt(bsds->fd, SOL_SOCKET, SO_ERROR,
916 /* this should not be reached */
921 static int tsocket_context_bsd_readv_data(struct tsocket_context *sock,
922 const struct iovec *vector,
925 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
926 struct tsocket_context_bsd);
929 ret = readv(bsds->fd, vector, count);
934 static int tsocket_context_bsd_writev_data(struct tsocket_context *sock,
935 const struct iovec *vector,
938 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
939 struct tsocket_context_bsd);
942 ret = writev(bsds->fd, vector, count);
947 static ssize_t tsocket_context_bsd_recvfrom_data(struct tsocket_context *sock,
948 uint8_t *data, size_t len,
949 TALLOC_CTX *addr_ctx,
950 struct tsocket_address **remote)
952 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
953 struct tsocket_context_bsd);
954 struct tsocket_address *addr = NULL;
955 struct tsocket_address_bsd *bsda;
957 struct sockaddr *sa = NULL;
958 socklen_t sa_len = 0;
961 addr = tsocket_address_create(addr_ctx,
962 &tsocket_address_bsd_ops,
964 struct tsocket_address_bsd,
965 __location__ "recvfrom");
973 sa_len = sizeof(bsda->u.ss);
976 ret = recvfrom(bsds->fd, data, len, 0, sa, &sa_len);
978 int saved_errno = errno;
990 static ssize_t tsocket_context_bsd_sendto_data(struct tsocket_context *sock,
991 const uint8_t *data, size_t len,
992 const struct tsocket_address *remote)
994 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
995 struct tsocket_context_bsd);
996 struct sockaddr *sa = NULL;
997 socklen_t sa_len = 0;
1001 struct tsocket_address_bsd *bsda =
1002 talloc_get_type(remote->private_data,
1003 struct tsocket_address_bsd);
1006 sa_len = sizeof(bsda->u.ss);
1009 ret = sendto(bsds->fd, data, len, 0, sa, sa_len);
1014 static int tsocket_context_bsd_get_status(const struct tsocket_context *sock)
1016 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
1017 struct tsocket_context_bsd);
1020 socklen_t len = sizeof(error);
1022 if (bsds->fd == -1) {
1027 ret = getsockopt(bsds->fd, SOL_SOCKET, SO_ERROR, &error, &len);
1039 static int tsocket_context_bsd_get_local_address(const struct tsocket_context *sock,
1040 TALLOC_CTX *mem_ctx,
1041 struct tsocket_address **_addr,
1042 const char *location)
1044 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
1045 struct tsocket_context_bsd);
1046 struct tsocket_address *addr;
1047 struct tsocket_address_bsd *bsda;
1051 addr = tsocket_address_create(mem_ctx,
1052 &tsocket_address_bsd_ops,
1054 struct tsocket_address_bsd,
1062 sa_len = sizeof(bsda->u.ss);
1063 ret = getsockname(bsds->fd, &bsda->u.sa, &sa_len);
1065 int saved_errno = errno;
1067 errno = saved_errno;
1075 static int tsocket_context_bsd_get_remote_address(const struct tsocket_context *sock,
1076 TALLOC_CTX *mem_ctx,
1077 struct tsocket_address **_addr,
1078 const char *location)
1080 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
1081 struct tsocket_context_bsd);
1082 struct tsocket_address *addr;
1083 struct tsocket_address_bsd *bsda;
1087 addr = tsocket_address_create(mem_ctx,
1088 &tsocket_address_bsd_ops,
1090 struct tsocket_address_bsd,
1098 sa_len = sizeof(bsda->u.ss);
1099 ret = getpeername(bsds->fd, &bsda->u.sa, &sa_len);
1101 int saved_errno = errno;
1103 errno = saved_errno;
1111 static const struct tsocket_context_bsd_option {
1116 } tsocket_context_bsd_options[] = {
1117 #define TSOCKET_OPTION(_level, _optnum, _optval) { \
1120 .optnum = _optnum, \
1123 TSOCKET_OPTION(SOL_SOCKET, SO_REUSEADDR, 0),
1124 TSOCKET_OPTION(SOL_SOCKET, SO_BROADCAST, 0)
1127 static int tsocket_context_bsd_get_option(const struct tsocket_context *sock,
1129 TALLOC_CTX *mem_ctx,
1132 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
1133 struct tsocket_context_bsd);
1134 const struct tsocket_context_bsd_option *opt = NULL;
1137 socklen_t optval_len = sizeof(optval);
1141 for (i=0; i < ARRAY_SIZE(tsocket_context_bsd_options); i++) {
1142 if (strcmp(option, tsocket_context_bsd_options[i].name) != 0) {
1146 opt = &tsocket_context_bsd_options[i];
1154 ret = getsockopt(bsds->fd, opt->level, opt->optnum,
1155 (void *)&optval, &optval_len);
1160 if (optval_len != sizeof(optval)) {
1162 } if (opt->optval != 0) {
1163 if (optval == opt->optval) {
1164 value = talloc_strdup(mem_ctx, "1");
1166 value = talloc_strdup(mem_ctx, "0");
1172 value = talloc_asprintf(mem_ctx, "%d", optval);
1189 static int tsocket_context_bsd_set_option(const struct tsocket_context *sock,
1194 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
1195 struct tsocket_context_bsd);
1196 const struct tsocket_context_bsd_option *opt = NULL;
1201 for (i=0; i < ARRAY_SIZE(tsocket_context_bsd_options); i++) {
1202 if (strcmp(option, tsocket_context_bsd_options[i].name) != 0) {
1206 opt = &tsocket_context_bsd_options[i];
1215 if (opt->optval != 0) {
1220 optval = atoi(value);
1222 optval = opt->optval;
1225 ret = setsockopt(bsds->fd, opt->level, opt->optnum,
1226 (const void *)&optval, sizeof(optval));
1246 static void tsocket_context_bsd_disconnect(struct tsocket_context *sock)
1248 struct tsocket_context_bsd *bsds = talloc_get_type(sock->private_data,
1249 struct tsocket_context_bsd);
1251 tsocket_context_bsd_set_event_context(sock, NULL);
1253 if (bsds->fd != -1) {
1254 if (bsds->close_on_disconnect) {
1261 static const struct tsocket_context_ops tsocket_context_bsd_ops = {
1264 .set_event_context = tsocket_context_bsd_set_event_context,
1265 .set_read_handler = tsocket_context_bsd_set_read_handler,
1266 .set_write_handler = tsocket_context_bsd_set_write_handler,
1268 .connect_to = tsocket_context_bsd_connect_to,
1269 .listen_on = tsocket_context_bsd_listen_on,
1270 .accept_new = tsocket_context_bsd_accept_new,
1272 .pending_data = tsocket_context_bsd_pending_data,
1273 .readv_data = tsocket_context_bsd_readv_data,
1274 .writev_data = tsocket_context_bsd_writev_data,
1275 .recvfrom_data = tsocket_context_bsd_recvfrom_data,
1276 .sendto_data = tsocket_context_bsd_sendto_data,
1278 .get_status = tsocket_context_bsd_get_status,
1279 .get_local_address = tsocket_context_bsd_get_local_address,
1280 .get_remote_address = tsocket_context_bsd_get_remote_address,
1282 .get_option = tsocket_context_bsd_get_option,
1283 .set_option = tsocket_context_bsd_set_option,
1285 .disconnect = tsocket_context_bsd_disconnect
1292 struct tevent_fd *fde;
1294 void *readable_private;
1295 void (*readable_handler)(void *private_data);
1296 void *writeable_private;
1297 void (*writeable_handler)(void *private_data);
1299 struct tevent_req *read_req;
1300 struct tevent_req *write_req;
1303 static void tdgram_bsd_fde_handler(struct tevent_context *ev,
1304 struct tevent_fd *fde,
1308 struct tdgram_bsd *bsds = talloc_get_type_abort(private_data,
1311 if (flags & TEVENT_FD_WRITE) {
1312 bsds->writeable_handler(bsds->writeable_private);
1315 if (flags & TEVENT_FD_READ) {
1316 if (!bsds->readable_handler) {
1317 TEVENT_FD_NOT_READABLE(bsds->fde);
1320 bsds->readable_handler(bsds->readable_private);
1325 static int tdgram_bsd_set_readable_handler(struct tdgram_bsd *bsds,
1326 struct tevent_context *ev,
1327 void (*handler)(void *private_data),
1335 if (!bsds->readable_handler) {
1338 bsds->readable_handler = NULL;
1339 bsds->readable_private = NULL;
1344 /* read and write must use the same tevent_context */
1345 if (bsds->event_ptr != ev) {
1346 if (bsds->readable_handler || bsds->writeable_handler) {
1350 bsds->event_ptr = NULL;
1351 TALLOC_FREE(bsds->fde);
1354 if (bsds->fde == NULL) {
1355 bsds->fde = tevent_add_fd(ev, bsds,
1356 bsds->fd, TEVENT_FD_READ,
1357 tdgram_bsd_fde_handler,
1363 /* cache the event context we're running on */
1364 bsds->event_ptr = ev;
1365 } else if (!bsds->readable_handler) {
1366 TEVENT_FD_READABLE(bsds->fde);
1369 bsds->readable_handler = handler;
1370 bsds->readable_private = private_data;
1375 static int tdgram_bsd_set_writeable_handler(struct tdgram_bsd *bsds,
1376 struct tevent_context *ev,
1377 void (*handler)(void *private_data),
1385 if (!bsds->writeable_handler) {
1388 bsds->writeable_handler = NULL;
1389 bsds->writeable_private = NULL;
1390 TEVENT_FD_NOT_WRITEABLE(bsds->fde);
1395 /* read and write must use the same tevent_context */
1396 if (bsds->event_ptr != ev) {
1397 if (bsds->readable_handler || bsds->writeable_handler) {
1401 bsds->event_ptr = NULL;
1402 TALLOC_FREE(bsds->fde);
1405 if (bsds->fde == NULL) {
1406 bsds->fde = tevent_add_fd(ev, bsds,
1407 bsds->fd, TEVENT_FD_WRITE,
1408 tdgram_bsd_fde_handler,
1414 /* cache the event context we're running on */
1415 bsds->event_ptr = ev;
1416 } else if (!bsds->writeable_handler) {
1417 TEVENT_FD_WRITEABLE(bsds->fde);
1420 bsds->writeable_handler = handler;
1421 bsds->writeable_private = private_data;
1426 struct tdgram_bsd_recvfrom_state {
1427 struct tdgram_context *dgram;
1431 struct tsocket_address *src;
1434 static int tdgram_bsd_recvfrom_destructor(struct tdgram_bsd_recvfrom_state *state)
1436 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
1439 bsds->read_req = NULL;
1440 tdgram_bsd_set_readable_handler(bsds, NULL, NULL, NULL);
1445 static void tdgram_bsd_recvfrom_handler(void *private_data);
1447 static struct tevent_req *tdgram_bsd_recvfrom_send(TALLOC_CTX *mem_ctx,
1448 struct tevent_context *ev,
1449 struct tdgram_context *dgram)
1451 struct tevent_req *req;
1452 struct tdgram_bsd_recvfrom_state *state;
1453 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1456 req = tevent_req_create(mem_ctx, &state,
1457 struct tdgram_bsd_recvfrom_state);
1462 state->dgram = dgram;
1467 if (bsds->read_req) {
1468 tevent_req_error(req, EBUSY);
1471 bsds->read_req = req;
1473 talloc_set_destructor(state, tdgram_bsd_recvfrom_destructor);
1475 if (bsds->fd == -1) {
1476 tevent_req_error(req, ENOTCONN);
1481 * this is a fast path, not waiting for the
1482 * socket to become explicit readable gains
1483 * about 10%-20% performance in benchmark tests.
1485 tdgram_bsd_recvfrom_handler(req);
1486 if (!tevent_req_is_in_progress(req)) {
1490 ret = tdgram_bsd_set_readable_handler(bsds, ev,
1491 tdgram_bsd_recvfrom_handler,
1494 tevent_req_error(req, errno);
1501 tevent_req_post(req, ev);
1505 static void tdgram_bsd_recvfrom_handler(void *private_data)
1507 struct tevent_req *req = talloc_get_type_abort(private_data,
1509 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
1510 struct tdgram_bsd_recvfrom_state);
1511 struct tdgram_context *dgram = state->dgram;
1512 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1513 struct tsocket_address_bsd *bsda;
1515 struct sockaddr *sa = NULL;
1516 socklen_t sa_len = 0;
1520 ret = tsocket_bsd_pending(bsds->fd);
1525 err = tsocket_bsd_error_from_errno(ret, errno, &retry);
1530 if (tevent_req_error(req, err)) {
1534 state->buf = talloc_array(state, uint8_t, ret);
1535 if (tevent_req_nomem(state->buf, req)) {
1540 state->src = tsocket_address_create(state,
1541 &tsocket_address_bsd_ops,
1543 struct tsocket_address_bsd,
1544 __location__ "bsd_recvfrom");
1545 if (tevent_req_nomem(state->src, req)) {
1552 sa_len = sizeof(bsda->u.ss);
1554 ret = recvfrom(bsds->fd, state->buf, state->len, 0, sa, &sa_len);
1555 err = tsocket_error_from_errno(ret, errno, &retry);
1560 if (tevent_req_error(req, err)) {
1564 if (ret != state->len) {
1565 tevent_req_error(req, EIO);
1569 tevent_req_done(req);
1572 static ssize_t tdgram_bsd_recvfrom_recv(struct tevent_req *req,
1574 TALLOC_CTX *mem_ctx,
1576 struct tsocket_address **src)
1578 struct tdgram_bsd_recvfrom_state *state = tevent_req_data(req,
1579 struct tdgram_bsd_recvfrom_state);
1582 ret = tsocket_simple_int_recv(req, perrno);
1584 *buf = talloc_move(mem_ctx, &state->buf);
1587 *src = talloc_move(mem_ctx, &state->src);
1591 tevent_req_received(req);
1595 struct tdgram_bsd_sendto_state {
1596 struct tdgram_context *dgram;
1600 const struct tsocket_address *dst;
1605 static int tdgram_bsd_sendto_destructor(struct tdgram_bsd_sendto_state *state)
1607 struct tdgram_bsd *bsds = tdgram_context_data(state->dgram,
1610 bsds->write_req = NULL;
1611 tdgram_bsd_set_writeable_handler(bsds, NULL, NULL, NULL);
1615 static void tdgram_bsd_sendto_handler(void *private_data);
1617 static struct tevent_req *tdgram_bsd_sendto_send(TALLOC_CTX *mem_ctx,
1618 struct tevent_context *ev,
1619 struct tdgram_context *dgram,
1622 const struct tsocket_address *dst)
1624 struct tevent_req *req;
1625 struct tdgram_bsd_sendto_state *state;
1626 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1629 req = tevent_req_create(mem_ctx, &state,
1630 struct tdgram_bsd_sendto_state);
1635 state->dgram = dgram;
1641 if (bsds->write_req) {
1642 tevent_req_error(req, EBUSY);
1645 bsds->write_req = req;
1647 talloc_set_destructor(state, tdgram_bsd_sendto_destructor);
1649 if (bsds->fd == -1) {
1650 tevent_req_error(req, ENOTCONN);
1655 * this is a fast path, not waiting for the
1656 * socket to become explicit writeable gains
1657 * about 10%-20% performance in benchmark tests.
1659 tdgram_bsd_sendto_handler(req);
1660 if (!tevent_req_is_in_progress(req)) {
1664 ret = tdgram_bsd_set_writeable_handler(bsds, ev,
1665 tdgram_bsd_sendto_handler,
1668 tevent_req_error(req, errno);
1675 tevent_req_post(req, ev);
1679 static void tdgram_bsd_sendto_handler(void *private_data)
1681 struct tevent_req *req = talloc_get_type_abort(private_data,
1683 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1684 struct tdgram_bsd_sendto_state);
1685 struct tdgram_context *dgram = state->dgram;
1686 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1687 struct sockaddr *sa = NULL;
1688 socklen_t sa_len = 0;
1694 struct tsocket_address_bsd *bsda =
1695 talloc_get_type(state->dst->private_data,
1696 struct tsocket_address_bsd);
1699 sa_len = sizeof(bsda->u.ss);
1702 ret = sendto(bsds->fd, state->buf, state->len, 0, sa, sa_len);
1703 err = tsocket_error_from_errno(ret, errno, &retry);
1708 if (tevent_req_error(req, err)) {
1714 tevent_req_done(req);
1717 static ssize_t tdgram_bsd_sendto_recv(struct tevent_req *req, int *perrno)
1719 struct tdgram_bsd_sendto_state *state = tevent_req_data(req,
1720 struct tdgram_bsd_sendto_state);
1723 ret = tsocket_simple_int_recv(req, perrno);
1728 tevent_req_received(req);
1732 struct tdgram_bsd_disconnect_state {
1736 static struct tevent_req *tdgram_bsd_disconnect_send(TALLOC_CTX *mem_ctx,
1737 struct tevent_context *ev,
1738 struct tdgram_context *dgram)
1740 struct tdgram_bsd *bsds = tdgram_context_data(dgram, struct tdgram_bsd);
1741 struct tevent_req *req;
1742 struct tdgram_bsd_disconnect_state *state;
1747 req = tevent_req_create(mem_ctx, &state,
1748 struct tdgram_bsd_disconnect_state);
1754 if (bsds->read_req || bsds->write_req) {
1755 tevent_req_error(req, EBUSY);
1759 if (bsds->fd == -1) {
1760 tevent_req_error(req, ENOTCONN);
1764 state->ret = close(bsds->fd);
1766 err = tsocket_error_from_errno(ret, errno, &dummy);
1767 if (tevent_req_error(req, err)) {
1771 tevent_req_done(req);
1773 tevent_req_post(req, ev);
1777 static int tdgram_bsd_disconnect_recv(struct tevent_req *req,
1780 struct tdgram_bsd_disconnect_state *state = tevent_req_data(req,
1781 struct tdgram_bsd_disconnect_state);
1784 ret = tsocket_simple_int_recv(req, perrno);
1789 tevent_req_received(req);
1793 static const struct tdgram_context_ops tdgram_bsd_ops = {
1796 .recvfrom_send = tdgram_bsd_recvfrom_send,
1797 .recvfrom_recv = tdgram_bsd_recvfrom_recv,
1799 .sendto_send = tdgram_bsd_sendto_send,
1800 .sendto_recv = tdgram_bsd_sendto_recv,
1802 .disconnect_send = tdgram_bsd_disconnect_send,
1803 .disconnect_recv = tdgram_bsd_disconnect_recv,
1806 static int tdgram_bsd_destructor(struct tdgram_bsd *bsds)
1808 TALLOC_FREE(bsds->fde);
1809 if (bsds->fd != -1) {
1816 static int tdgram_bsd_dgram_socket(const struct tsocket_address *local,
1817 const struct tsocket_address *remote,
1818 TALLOC_CTX *mem_ctx,
1819 struct tdgram_context **_dgram,
1820 const char *location)
1822 struct tsocket_address_bsd *lbsda =
1823 talloc_get_type_abort(local->private_data,
1824 struct tsocket_address_bsd);
1825 struct tsocket_address_bsd *rbsda = NULL;
1826 struct tdgram_context *dgram;
1827 struct tdgram_bsd *bsds;
1830 bool do_bind = false;
1831 bool do_reuseaddr = false;
1834 rbsda = talloc_get_type_abort(remote->private_data,
1835 struct tsocket_address_bsd);
1838 switch (lbsda->u.sa.sa_family) {
1840 if (lbsda->u.un.sun_path[0] != 0) {
1841 do_reuseaddr = true;
1846 if (lbsda->u.in.sin_port != 0) {
1847 do_reuseaddr = true;
1850 if (lbsda->u.in.sin_addr.s_addr == INADDR_ANY) {
1856 if (lbsda->u.in6.sin6_port != 0) {
1857 do_reuseaddr = true;
1860 if (memcmp(&in6addr_any,
1861 &lbsda->u.in6.sin6_addr,
1862 sizeof(in6addr_any)) != 0) {
1872 fd = socket(lbsda->u.sa.sa_family, SOCK_DGRAM, 0);
1877 fd = tsocket_bsd_common_prepare_fd(fd, true);
1882 dgram = tdgram_context_create(mem_ctx,
1888 int saved_errno = errno;
1890 errno = saved_errno;
1895 talloc_set_destructor(bsds, tdgram_bsd_destructor);
1897 if (lbsda->broadcast) {
1900 ret = setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
1901 (const void *)&val, sizeof(val));
1903 int saved_errno = errno;
1905 errno = saved_errno;
1913 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1914 (const void *)&val, sizeof(val));
1916 int saved_errno = errno;
1918 errno = saved_errno;
1924 ret = bind(fd, &lbsda->u.sa, sizeof(lbsda->u.ss));
1926 int saved_errno = errno;
1928 errno = saved_errno;
1934 ret = connect(fd, &rbsda->u.sa, sizeof(rbsda->u.ss));
1936 int saved_errno = errno;
1938 errno = saved_errno;
1947 int _tdgram_inet_udp_socket(const struct tsocket_address *local,
1948 const struct tsocket_address *remote,
1949 TALLOC_CTX *mem_ctx,
1950 struct tdgram_context **dgram,
1951 const char *location)
1953 struct tsocket_address_bsd *lbsda =
1954 talloc_get_type_abort(local->private_data,
1955 struct tsocket_address_bsd);
1958 switch (lbsda->u.sa.sa_family) {
1970 ret = tdgram_bsd_dgram_socket(local, remote, mem_ctx, dgram, location);
1975 int _tdgram_unix_dgram_socket(const struct tsocket_address *local,
1976 const struct tsocket_address *remote,
1977 TALLOC_CTX *mem_ctx,
1978 struct tdgram_context **dgram,
1979 const char *location)
1981 struct tsocket_address_bsd *lbsda =
1982 talloc_get_type_abort(local->private_data,
1983 struct tsocket_address_bsd);
1986 switch (lbsda->u.sa.sa_family) {
1994 ret = tdgram_bsd_dgram_socket(local, remote, mem_ctx, dgram, location);