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