1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
|
/*
* network.c -- handles stuff dealing with connecting and name resolving
*
* Copyright 1995, 2007 Jeremy Nelson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notices, the above paragraph (the one permitting redistribution),
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "irc.h"
#include "ircaux.h"
#include "vars.h"
#include "newio.h"
#include "output.h"
#include <sys/ioctl.h>
/* This will eventually become a configurable */
#ifndef NO_JOB_CONTROL
#define ASYNC_DNS
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
typedef struct sockaddr_un USA;
#endif
static int Connect (int, SA *);
static socklen_t socklen (SA *);
static int Getnameinfo (const SA *, socklen_t, char *, size_t, char *, size_t, int);
static int Socket (int, int, int);
/*
Retval Meaning
-------- -------------------------------------------
-1 System call error occured (check errno)
-2 The operation isn't supported for the requested family.
-3 The hostname has an address, but not in the family you want.
-4 The "address presentation" hostname can't be converted.
-5 The hostname does not resolve.
-6 The requested family does not have a virtual host.
-7 The remote sockaddr to connect to was not provided.
-8 The local sockaddr and remote sockaddr are different families.
-9 The timeout expired before the connection was established.
-10 The requested local address is in use or not available.
-11 The connect()ion failed (at connect() time)
-12 The connection could not be established (after connect() time)
-13 The local sockaddr to bind was not provided.
-14 The family request does not make sense.
*/
/*****************************************************************************/
/*
* NAME: client_connect
* USAGE: Create a new socket and establish both endpoints with the
* arguments given.
* ARGS: l - A local sockaddr structure representing the local side of
* the connection. NULL is permitted. If this value is NULL,
* then the local side of the connection will not be bind()ed.
* ll - The sizeof(l) -- if 0, then 'l' is treated as a NULL value.
* r - A remote sockaddr structure representing the remote side of
* the connection. NULL is not permitted.
* rl - The sizeof(r) -- if 0, then 'r' is treated as a NULL value.
* Therefore, 0 is not permitted.
*/
int client_connect (SA *l, socklen_t ll, SA *r, socklen_t rl)
{
int fd = -1;
int family = AF_UNSPEC;
if (ll == 0)
l = NULL;
if (rl == 0)
r = NULL;
if (!r)
{
syserr(-1, "client_connect: remote addr missing (connect to who?)");
return -1;
}
if (l && r && l->sa_family != r->sa_family)
{
syserr(-1, "client_connect: local addr protocol (%d) is different "
"from remote addr protocol (%d)",
l->sa_family, r->sa_family);
return -1;
}
if (l)
family = l->sa_family;
if (r)
family = r->sa_family;
if ((fd = Socket(family, SOCK_STREAM, 0)) < 0)
{
syserr(-1, "client_connect: socket(%d) failed: %s",
family, strerror(errno));
return -1;
}
if (family == AF_UNIX || family == AF_INET
#ifdef INET6
|| family == AF_INET6
#endif
)
{
if (l && bind(fd, l, ll))
{
syserr(-1, "client_connect: bind(%d) failed: %s",
fd, strerror(errno));
close(fd);
return -1;
}
if (Connect(fd, r))
{
syserr(-1, "client_connect: connect(%d) failed: %s",
fd, strerror(errno));
close(fd);
return -1;
}
}
else
{
syserr(-1, "client_connect: protocol (%d) not supported", family);
return -1;
}
if (x_debug & DEBUG_SERVER_CONNECT)
yell("Connected on des [%d]", fd);
return fd;
}
/*****************************************************************************/
/*
* NAME: ip_bindery
* USAGE: Establish a local passive (listening) socket at the given port.
* ARGS: family - AF_INET is the only supported argument.
* port - The port to establish the connection upon. May be 0,
* which requests any open port.
* storage - Pointer to a sockaddr structure big enough for the
* specified family. Upon success, it will be filled with
* the local sockaddr of the new connection.
* NOTES: In most cases, the local address for 'storage' will be INADDR_ANY
* which doesn't really give you any useful information. It is not
* possible to authoritatively figure out the local IP address
* without using ioctl(). However, we guess the best we may.
*
* NOTES: This function lacks IPv6 support.
* This function lacks Unix Domain Socket support.
*/
int ip_bindery (int family, unsigned short port, SS *storage)
{
socklen_t len;
int fd;
if (inet_vhostsockaddr(family, port, NULL, storage, &len))
{
syserr(-1, "ip_bindery: inet_vhostsockaddr(%d,%d) failed.",
family, port);
return -1;
}
if (!len)
{
syserr(-1, "ip_bindery: inet_vhostsockaddr(%d,%d) didn't "
"return an address I could bind", family, port);
return -1;
}
if ((fd = client_bind((SA *)storage, len)) < 0)
{
syserr(-1, "ip_bindery: client_bind(%d,%d) failed.", family, port);
return -1;
}
return fd;
}
/*
* NAME: client_bind
* USAGE: Create a new socket and establish one endpoint with the arguments
* given.
* ARGS: local - A local sockaddr structure representing the local side of
* the connection. NULL is not permitted.
* local_len - The sizeof(l) -- if 0, then 'l' is treated as a NULL
* value. Therefore, 0 is not permitted.
*/
int client_bind (SA *local, socklen_t local_len)
{
int fd = -1;
int family = AF_UNSPEC;
if (local_len == 0)
local = NULL;
if (!local)
{
syserr(-1, "client_bind: address to bind to not provided");
return -1;
}
family = local->sa_family;
if ((fd = Socket(family, SOCK_STREAM, 0)) < 0)
{
syserr(-1, "client_bind: socket(%d) failed: %s",
family, strerror(errno));
return -1;
}
#ifdef IP_PORTRANGE
/*
* On 4.4BSD systems, this socket option asks the
* operating system to select a port from the "high
* port range" when we ask for port 0 (any port).
* Maybe some day Linux will support this.
*/
if (family == AF_INET && getenv("EPIC_USE_HIGHPORTS"))
{
int ports = IP_PORTRANGE_HIGH;
setsockopt(fd, IPPROTO_IP, IP_PORTRANGE,
(char *)&ports, sizeof(ports));
}
#endif
if (bind(fd, local, local_len))
{
syserr(-1, "client_bind: bind(%d) failed: %s",
fd, strerror(errno));
close(fd);
return -1;
}
/*
* Get the local sockaddr of the passive socket,
* specifically the port number, and stash it in
* 'port' which is the return value to the caller.
*/
if (getsockname(fd, (SA *)local, &local_len))
{
syserr(-1, "client_bind: getsockname(%d) failed: %s",
fd, strerror(errno));
close(fd);
return -1;
}
if (listen(fd, 4) < 0)
{
syserr(-1, "client_bind: listen(%d,4) failed: %s",
fd, strerror(errno));
close(fd);
return -1;
}
return fd;
}
/*****************************************************************************/
/*
* NAME: inet_vhostsockaddr
* USAGE: Get the sockaddr of the current virtual host, if one is in use.
* ARGS: family - The family whose sockaddr info is to be retrieved
* storage - Pointer to a sockaddr structure appropriate for family.
* len - This will be set to the size of the sockaddr structure that
* was copied, or set to 0 if no virtual host is available in
* the given family.
* NOTES: If "len" is set to 0, do not attempt to bind() 'storage'!
*/
int inet_vhostsockaddr (int family, int port, const char *wanthost, SS *storage, socklen_t *len)
{
char p_port[12];
char *p = NULL;
AI hints, *res;
int err;
const char *lhn;
/*
* If port == -1, AND "wanthost" is NULL, then this is a client connection,
* so we punt if there is no virtual host name. But if port is NOT zero,
* then the caller expects us to return a sockaddr they can bind() to,
* so we need to use LocalIPv(4|6)HostName, even if it's NULL. If you
* return *len == 0 for port != -1, then /dcc breaks.
*/
if ((family == AF_UNIX) ||
(family == AF_INET && port == -1 && empty(wanthost) && LocalIPv4HostName == NULL)
#ifdef INET6
|| (family == AF_INET6 && port == -1 && empty(wanthost) && LocalIPv6HostName == NULL)
#endif
)
{
*len = 0;
return 0; /* No vhost needed */
}
if (wanthost && *wanthost)
lhn = wanthost;
else if (family == AF_INET)
lhn = LocalIPv4HostName;
#ifdef INET6
else if (family == AF_INET6)
lhn = LocalIPv6HostName;
#endif
else
lhn = NULL;
/*
* Can it really be this simple?
*/
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
if (port != -1)
{
hints.ai_flags = AI_PASSIVE;
snprintf(p_port, 12, "%u", port);
p = p_port;
}
if ((err = my_getaddrinfo(lhn, p, &hints, &res)))
{
syserr(-1, "inet_vhostsockaddr: my_getaddrinfo(%s,%s) failed: %s",
lhn, p, gai_strerror(err));
return -1;
}
memcpy(storage, res->ai_addr, res->ai_addrlen);
my_freeaddrinfo(res);
*len = socklen((SA *)storage);
return 0;
}
/************************************************************************/
/*
* NAME: inet_strton
* USAGE: Convert "any" kind of address represented by a string into
* a socket address suitable for connect()ing or bind()ing with.
* ARGS: hostname - The address to convert. It may be any of the following:
* IPv4 "Presentation Address" (A.B.C.D)
* IPv6 "Presentation Address" (A:B::C:D)
* Hostname (foo.bar.com)
* 32 bit host order ipv4 address (2134546324)
* storage - A pointer to a (struct sockaddr_storage) with the
* "family" argument filled in (AF_INET or AF_INET6).
* If "hostname" is a p-addr, then the form of the p-addr
* must agree with the family in 'storage'.
*/
int inet_strton (const char *host, const char *port, SA *storage, int flags)
{
int family = storage->sa_family;
/* First check for legacy 32 bit integer DCC addresses */
if ((family == AF_INET || family == AF_UNSPEC) && host && is_number(host))
{
((ISA *)storage)->sin_family = AF_INET;
#ifdef HAVE_SA_LEN
((ISA *)storage)->sin_len = sizeof(ISA);
#endif
((ISA *)storage)->sin_addr.s_addr = htonl(strtoul(host, NULL, 10));
if (port)
((ISA *)storage)->sin_port = htons((unsigned short)strtoul(port, NULL, 10));
return 0;
}
else
{
AI hints;
AI *results;
int retval;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = flags;
hints.ai_family = family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
if ((retval = my_getaddrinfo(host, port, &hints, &results)))
{
syserr(-1, "inet_strton: my_getaddrinfo(%s,%s) failed: %s",
host, port, gai_strerror(retval));
return -1;
}
/* memcpy can bite me. */
memcpy(storage, results->ai_addr, results->ai_addrlen);
my_freeaddrinfo(results);
return 0;
}
}
/*
* NAME: inet_ntostr
* USAGE: Convert a "sockaddr name" (SA) into a Hostname/p-addr
* PLAIN ENGLISH: Convert getpeername() into "foo.bar.com"
* ARGS: name - The socket address, possibly returned by getpeername().
* retval - A string to store the hostname/paddr (RETURN VALUE)
* size - The length of 'retval' in bytes
* RETURN VALUE: "retval" is returned upon success
* "empty_string" is returned for any error.
*
* NOTES: 'flags' should be set to NI_NAMEREQD if you don't want the remote
* host's p-addr if it does not have a DNS hostname.
*/
int inet_ntostr (SA *name, char *host, int hsize, char *port, int psize, int flags)
{
int retval;
socklen_t len;
len = socklen(name);
if ((retval = Getnameinfo(name, len, host, hsize, port, psize,
flags | NI_NUMERICSERV)))
{
syserr(-1, "inet_ntostr: Getnameinfo(sockaddr->p_addr) failed: %s",
gai_strerror(retval));
return -1;
}
return 0;
}
/* * * * * * * * * */
/* Only used by $convert() and $nametoip() */
/*
* NAME: inet_hntop
* USAGE: Convert a Hostname into a "presentation address" (p-addr)
* PLAIN ENGLISH: Convert "A.B.C.D" into "foo.bar.com"
* ARGS: family - The family whose presesentation address format to use
* host - The hostname to convert
* retval - A string to store the p-addr (RETURN VALUE)
* size - The length of 'retval' in bytes
* RETURN VALUE: "retval" is returned upon success
* "empty_string" is returned for any error.
*/
int inet_hntop (int family, const char *host, char *retval, int size)
{
SS buffer;
((SA *)&buffer)->sa_family = family;
if (inet_strton(host, NULL, (SA *)&buffer, AI_ADDRCONFIG))
{
syserr(-1, "inet_hntop: inet_strton(%d,%s) failed", family, host);
return -1;
}
if (inet_ntostr((SA *)&buffer, retval, size, NULL, 0, NI_NUMERICHOST))
{
syserr(-1, "inet_hntop: inet_ntostr(%d,%s) failed", family, host);
return -1;
}
return 0;
}
/* Only used by $convert() and $iptoname() */
/*
* NAME: inet_ptohn
* USAGE: Convert a "presentation address" (p-addr) into a Hostname
* PLAIN ENGLISH: Convert "foo.bar.com" into "A.B.C.D"
* ARGS: family - The family whose presesentation address format to use
* ip - The presentation-address to look up
* retval - A string to store the hostname (RETURN VALUE)
* size - The length of 'retval' in bytes
* RETURN VALUE: "retval" is returned upon success
* "empty_string" is returned for any error.
*/
int inet_ptohn (int family, const char *ip, char *retval, int size)
{
SS buffer;
memset((char *)&buffer, 0, sizeof(buffer));
((SA *)&buffer)->sa_family = family;
if (inet_strton(ip, NULL, (SA *)&buffer, AI_NUMERICHOST))
{
syserr(-1, "inet_ptohn: inet_strton(%d,%s) failed", family, ip);
return -1;
}
if (inet_ntostr((SA *)&buffer, retval, size, NULL, 0, NI_NAMEREQD))
{
syserr(-1, "inet_ptohn: inet_ntostr(%d,%s) failed", family, ip);
return -1;
}
return 0;
}
/* Only used by $convert() */
/*
* NAME: one_to_another
* USAGE: Convert a p-addr to a Hostname, or a Hostname to a p-addr.
* PLAIN ENGLISH: Convert "A.B.C.D" to "foo.bar.com" or convert "foo.bar.com"
* into "A.B.C.D"
* ARGS: family - The address family in which to convert (AF_INET/AF_INET6)
* what - Either a Hostname or a p-addr.
* retval - If "what" is a Hostname, a place to store the p-addr
* If "what" is a p-addr, a place to store the Hostname
* size - The length of 'retval' in bytes
* RETURN VALUE: "retval" is returned upon success
* "empty_string" is returned for any error.
*
* NOTES: If "what" is a p-addr and there is no hostname associated with that
* address, that is considered an error and empty_string is returned.
*/
int one_to_another (int family, const char *what, char *retval, int size)
{
/* XXX I wish this wasn't necessary */
int old_window_display = window_display;
window_display = 0;
if (inet_ptohn(family, what, retval, size))
{
if (inet_hntop(family, what, retval, size))
{
window_display = old_window_display;
syserr(-1, "one_to_another: both inet_ptohn and "
"inet_hntop failed (%d,%s)",
family, what);
return -1;
}
}
window_display = old_window_display;
return 0;
}
/****************************************************************************/
int set_non_blocking (int fd)
{
int flag, rval;
#if defined(O_NONBLOCK)
flag = O_NONBLOCK;
#elif defined(O_NDELAY)
flag = O_NDELAY;
#elif defined(FIONBIO)
flag = 1;
#else
yell("Sorry! Can't set nonblocking on this system!");
#endif
#if defined(O_NONBLOCK) || defined(O_NDELAY)
if ((rval = fcntl(fd, F_GETFL, 0)) == -1)
{
syserr(-1, "set_non_blocking: fcntl(%d, F_GETFL) failed: %s",
fd, strerror(errno));
return -1;
}
if (fcntl(fd, F_SETFL, rval | flag) == -1)
{
syserr(-1, "set_non_blocking: fcntl(%d, F_SETFL) failed: %s",
fd, strerror(errno));
return -1;
}
#else
if (ioctl(fd, FIONBIO, &flag) < 0)
{
syserr(-1, "set_non_blocking: ioctl(%d, FIONBIO) failed: %s",
fd, strerror(errno));
return -1;
}
#endif
return 0;
}
int set_blocking (int fd)
{
int flag, rval;
#if defined(O_NONBLOCK)
flag = O_NONBLOCK;
#elif defined(O_NDELAY)
flag = O_NDELAY;
#elif defined(FIONBIO)
flag = 0;
#else
yell("Sorry! Can't set nonblocking on this system!");
#endif
#if defined(O_NONBLOCK) || defined(O_NDELAY)
if ((rval = fcntl(fd, F_GETFL, 0)) == -1)
{
syserr(-1, "set_blocking: fcntl(%d, F_GETFL) failed: %s",
fd, strerror(errno));
return -1;
}
if (fcntl(fd, F_SETFL, rval & ~flag) == -1)
{
syserr(-1, "set_blocking: fcntl(%d, F_SETFL) failed: %s",
fd, strerror(errno));
return -1;
}
#else
if (ioctl(fd, FIONBIO, &flag) < 0)
{
syserr(-1, "set_blocking: ioctl(%d, FIONBIO) failed: %s",
fd, strerror(errno));
return -1;
}
#endif
return 0;
}
/****************************************************************************/
/*
* It is possible for a race condition to exist; such that select()
* indicates that a listen()ing socket is able to recieve a new connection
* and that a later accept() call will still block because the connection
* has been closed in the interim. This wrapper for accept() attempts to
* defeat this by making the accept() call nonblocking.
*/
int my_accept (int s, SA *addr, socklen_t *addrlen)
{
int retval;
set_non_blocking(s);
if ((retval = accept(s, addr, addrlen)) < 0)
syserr(-1, "my_accept: accept(%d) failed: %s",
s, strerror(errno));
set_blocking(s);
set_blocking(retval); /* Just in case */
return retval;
}
static int Connect (int fd, SA *addr)
{
int retval;
set_non_blocking(fd);
if ((retval = connect(fd, addr, socklen(addr))))
{
if (errno != EINPROGRESS)
syserr(-1, "Connect: connect(%d) failed: %s", fd, strerror(errno));
else
retval = 0;
}
set_blocking(fd);
return retval;
}
/*
* XXX - Ugh! Some getaddrinfo()s take AF_UNIX paths as the 'servname'
* instead of as the 'nodename'. How heinous!
*/
int my_getaddrinfo (const char *nodename, const char *servname, const AI *hints, AI **res)
{
#ifdef GETADDRINFO_DOES_NOT_DO_AF_UNIX
int do_af_unix = 0;
USA storage;
AI * results;
int len;
if (nodename && strchr(nodename, '/'))
do_af_unix = 1;
if (hints && hints->ai_family == AF_UNIX)
do_af_unix = 1;
if (do_af_unix)
{
memset(&storage, 0, sizeof(storage));
storage.sun_family = AF_UNIX;
strlcpy(storage.sun_path, nodename, sizeof(storage.sun_path));
#ifdef HAVE_SA_LEN
# ifdef SUN_LEN
storage.sun_len = SUN_LEN(&storage);
# else
storage.sun_len = strlen(nodename) + 1;
# endif
#endif
len = strlen(storage.sun_path) + 3;
(*res) = new_malloc(sizeof(*results));
(*res)->ai_flags = 0;
(*res)->ai_family = AF_UNIX;
(*res)->ai_socktype = SOCK_STREAM;
(*res)->ai_protocol = 0;
(*res)->ai_addrlen = len;
(*res)->ai_canonname = malloc_strdup(nodename);
(*res)->ai_addr = new_malloc(sizeof(storage));
*(USA *)((*res)->ai_addr) = storage;
(*res)->ai_next = 0;
return 0;
}
#endif
/*
* XXX -- Support getaddrinfo()s that want an AF_UNIX path to
* be the second argument and not the first one. Bleh.
*/
if ((nodename && strchr(nodename, '/')) ||
(hints && hints->ai_family == AF_UNIX))
return getaddrinfo(NULL, nodename, hints, res);
else
return getaddrinfo(nodename, servname, hints, res);
}
void my_freeaddrinfo (AI *ai)
{
#ifdef GETADDRINFO_DOES_NOT_DO_AF_UNIX
if (ai->ai_family == AF_UNIX)
{
new_free(&ai->ai_canonname);
new_free(&ai->ai_addr);
new_free(&ai);
return;
}
#endif
freeaddrinfo(ai);
}
static int Getnameinfo(const SA *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags)
{
#ifdef GETADDRINFO_DOES_NOT_DO_AF_UNIX
if (sa->sa_family == AF_UNIX)
{
socklen_t len;
const USA * usa = (const USA *)sa;
#ifdef HAVE_SA_LEN
len = usa->sun_len;
#else
# ifdef SUN_LEN
len = SUN_LEN(usa);
# else
len = strlen(usa->sun_path);
# endif
#endif
if (host && hostlen)
{
if (hostlen <= len)
{
memcpy(host, usa->sun_path, hostlen);
host[hostlen - 1] = 0;
}
else
{
memcpy(host, &usa->sun_path, len);
host[len] = 0;
}
}
if (serv && servlen)
*serv = 0;
return 0;
}
#endif
if ((flags & GNI_INTEGER) && sa->sa_family == AF_INET)
{
snprintf(host, hostlen, "%lu",
(unsigned long)ntohl(((const ISA *)sa)->sin_addr.s_addr));
host = NULL;
hostlen = 0;
}
flags = flags & ~(GNI_INTEGER);
return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
}
static socklen_t socklen (SA *sockaddr)
{
if (sockaddr->sa_family == AF_INET)
return sizeof(ISA);
#ifdef INET6
else if (sockaddr->sa_family == AF_INET6)
return sizeof(ISA6);
#endif
else if (sockaddr->sa_family == AF_UNIX)
return strlen(((USA *)sockaddr)->sun_path) + 2;
else
return 0;
}
/* set's socket options */
int Socket (int domain, int type, int protocol)
{
int opt = 1;
int optlen = sizeof(opt);
int s;
#ifndef NO_STRUCT_LINGER
struct linger lin;
#endif
if ((s = socket(domain, type, protocol)) < 0)
{
syserr(-1, "Socket: socket(%d,%d,%d) failed: %s",
domain, type, protocol, strerror(errno));
return -1;
}
#ifndef NO_STRUCT_LINGER
lin.l_onoff = lin.l_linger = 0;
setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&lin, optlen);
#endif
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, optlen);
opt = 1;
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&opt, optlen);
return s;
}
pid_t async_getaddrinfo (const char *nodename, const char *servname, const AI *hints, int fd)
{
AI *results = NULL;
ssize_t err;
#ifdef ASYNC_DNS
{
/* XXX Letting /exec clean up after us is a hack. */
pid_t helper;
if ((helper = fork()))
return helper;
}
#endif
if ((err = my_getaddrinfo(nodename, servname, hints, &results)))
{
err = -labs(err); /* Always a negative number */
if (!write(fd, &err, sizeof(err)))
(void) 0;
close(fd);
#ifdef ASYNC_DNS
exit(0);
#else
return 0;
#endif
}
if (!results)
{
err = 0;
if (!write(fd, &err, sizeof(err)))
(void) 0;
close(fd);
#ifdef ASYNC_DNS
exit(0);
#else
return 0;
#endif
}
marshall_getaddrinfo(fd, results);
my_freeaddrinfo(results);
close(fd);
#ifdef ASYNC_DNS
exit(0);
#endif
return 0; /* XXX This function should be void */
}
void marshall_getaddrinfo (int fd, AI *results)
{
ssize_t len, gah;
ssize_t alignment = sizeof(void *);
AI *result, *copy;
char *retval, *ptr;
for (len = 0, result = results; result; result = result->ai_next)
{
len += sizeof(AI);
len += result->ai_addrlen;
if (result->ai_canonname)
len += strlen(result->ai_canonname) + 1;
while (len % alignment != 0)
len++;
}
/* Why do I know I'm gonna regret this? */
ptr = retval = new_malloc(len + 1);
memset(retval, 0, len + 1);
for (result = results; result; result = result->ai_next)
{
copy = (AI *)ptr;
/* Copy over the AI */
memcpy(copy, result, sizeof(AI));
/* Copy over the ai_addr */
ptr += sizeof(AI);
memcpy(ptr, result->ai_addr, result->ai_addrlen);
/* Point the AI at the ai_addr */
copy->ai_addr = (SA *)ptr;
/* Copy over the canonname */
/* Point the AI at the canonname */
ptr += result->ai_addrlen;
if (result->ai_canonname)
{
gah = strlen(result->ai_canonname) + 1;
memcpy(ptr, result->ai_canonname, gah);
copy->ai_canonname = ptr;
ptr += gah;
}
else
copy->ai_canonname = NULL;
/* Calculate the start of the next entry */
while ((intptr_t)ptr % alignment != 0)
ptr++;
/* Point the AI at the next entry */
if (result->ai_next)
copy->ai_next = (AI *)ptr;
else
copy->ai_next = NULL;
}
if (!write(fd, (void *)&len, sizeof(len)))
(void) 0;
if (!write(fd, (void *)retval, len))
(void) 0;
new_free(&retval);
}
void unmarshall_getaddrinfo (AI *results)
{
ssize_t len;
AI *result;
char *ptr;
ssize_t alignment = sizeof(void *);
/* Why do I know I'm gonna regret this? */
for (result = results; result; result = result->ai_next)
{
ptr = (char *)result;
/* Copy over the ai_addr */
ptr += sizeof(AI);
result->ai_addr = (SA *)ptr;
ptr += result->ai_addrlen;
if (result->ai_canonname)
{
result->ai_canonname = ptr;
len = strlen(result->ai_canonname) + 1;
ptr += len;
}
/* Calculate the start of the next entry */
while ((intptr_t)ptr % alignment != 0)
ptr++;
/* Point the AI at the next entry */
if (result->ai_next)
result->ai_next = (AI *)ptr;
}
}
|