michael@0: /* michael@0: * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #include "event2/event-config.h" michael@0: michael@0: #define _GNU_SOURCE michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #include michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: #undef WIN32_LEAN_AND_MEAN michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_SOCKET_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_UNISTD_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_FCNTL_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_STDLIB_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_NETINET_IN_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_NETINET_IN6_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_ARPA_INET_H michael@0: #include michael@0: #endif michael@0: michael@0: #ifndef _EVENT_HAVE_GETTIMEOFDAY michael@0: #include michael@0: #include michael@0: #endif michael@0: #include michael@0: michael@0: #include "event2/util.h" michael@0: #include "util-internal.h" michael@0: #include "log-internal.h" michael@0: #include "mm-internal.h" michael@0: michael@0: #include "strlcpy-internal.h" michael@0: #include "ipv6-internal.h" michael@0: michael@0: #ifdef WIN32 michael@0: #define open _open michael@0: #define read _read michael@0: #define close _close michael@0: #define fstat _fstati64 michael@0: #define stat _stati64 michael@0: #define mode_t int michael@0: #endif michael@0: michael@0: int michael@0: evutil_open_closeonexec(const char *pathname, int flags, unsigned mode) michael@0: { michael@0: int fd; michael@0: michael@0: #ifdef O_CLOEXEC michael@0: flags |= O_CLOEXEC; michael@0: #endif michael@0: michael@0: if (flags & O_CREAT) michael@0: fd = open(pathname, flags, (mode_t)mode); michael@0: else michael@0: fd = open(pathname, flags); michael@0: if (fd < 0) michael@0: return -1; michael@0: michael@0: #if !defined(O_CLOEXEC) && defined(FD_CLOEXEC) michael@0: if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) michael@0: return -1; michael@0: #endif michael@0: michael@0: return fd; michael@0: } michael@0: michael@0: /** michael@0: Read the contents of 'filename' into a newly allocated NUL-terminated michael@0: string. Set *content_out to hold this string, and *len_out to hold its michael@0: length (not including the appended NUL). If 'is_binary', open the file in michael@0: binary mode. michael@0: michael@0: Returns 0 on success, -1 if the open fails, and -2 for all other failures. michael@0: michael@0: Used internally only; may go away in a future version. michael@0: */ michael@0: int michael@0: evutil_read_file(const char *filename, char **content_out, size_t *len_out, michael@0: int is_binary) michael@0: { michael@0: int fd, r; michael@0: struct stat st; michael@0: char *mem; michael@0: size_t read_so_far=0; michael@0: int mode = O_RDONLY; michael@0: michael@0: EVUTIL_ASSERT(content_out); michael@0: EVUTIL_ASSERT(len_out); michael@0: *content_out = NULL; michael@0: *len_out = 0; michael@0: michael@0: #ifdef O_BINARY michael@0: if (is_binary) michael@0: mode |= O_BINARY; michael@0: #endif michael@0: michael@0: fd = evutil_open_closeonexec(filename, mode, 0); michael@0: if (fd < 0) michael@0: return -1; michael@0: if (fstat(fd, &st) || st.st_size < 0 || michael@0: st.st_size > EV_SSIZE_MAX-1 ) { michael@0: close(fd); michael@0: return -2; michael@0: } michael@0: mem = mm_malloc((size_t)st.st_size + 1); michael@0: if (!mem) { michael@0: close(fd); michael@0: return -2; michael@0: } michael@0: read_so_far = 0; michael@0: #ifdef WIN32 michael@0: #define N_TO_READ(x) ((x) > INT_MAX) ? INT_MAX : ((int)(x)) michael@0: #else michael@0: #define N_TO_READ(x) (x) michael@0: #endif michael@0: while ((r = read(fd, mem+read_so_far, N_TO_READ(st.st_size - read_so_far))) > 0) { michael@0: read_so_far += r; michael@0: if (read_so_far >= (size_t)st.st_size) michael@0: break; michael@0: EVUTIL_ASSERT(read_so_far < (size_t)st.st_size); michael@0: } michael@0: close(fd); michael@0: if (r < 0) { michael@0: mm_free(mem); michael@0: return -2; michael@0: } michael@0: mem[read_so_far] = 0; michael@0: michael@0: *len_out = read_so_far; michael@0: *content_out = mem; michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2]) michael@0: { michael@0: #ifndef WIN32 michael@0: return socketpair(family, type, protocol, fd); michael@0: #else michael@0: return evutil_ersatz_socketpair(family, type, protocol, fd); michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: evutil_ersatz_socketpair(int family, int type, int protocol, michael@0: evutil_socket_t fd[2]) michael@0: { michael@0: /* This code is originally from Tor. Used with permission. */ michael@0: michael@0: /* This socketpair does not work when localhost is down. So michael@0: * it's really not the same thing at all. But it's close enough michael@0: * for now, and really, when localhost is down sometimes, we michael@0: * have other problems too. michael@0: */ michael@0: #ifdef WIN32 michael@0: #define ERR(e) WSA##e michael@0: #else michael@0: #define ERR(e) e michael@0: #endif michael@0: evutil_socket_t listener = -1; michael@0: evutil_socket_t connector = -1; michael@0: evutil_socket_t acceptor = -1; michael@0: struct sockaddr_in listen_addr; michael@0: struct sockaddr_in connect_addr; michael@0: ev_socklen_t size; michael@0: int saved_errno = -1; michael@0: michael@0: if (protocol michael@0: || (family != AF_INET michael@0: #ifdef AF_UNIX michael@0: && family != AF_UNIX michael@0: #endif michael@0: )) { michael@0: EVUTIL_SET_SOCKET_ERROR(ERR(EAFNOSUPPORT)); michael@0: return -1; michael@0: } michael@0: if (!fd) { michael@0: EVUTIL_SET_SOCKET_ERROR(ERR(EINVAL)); michael@0: return -1; michael@0: } michael@0: michael@0: listener = socket(AF_INET, type, 0); michael@0: if (listener < 0) michael@0: return -1; michael@0: memset(&listen_addr, 0, sizeof(listen_addr)); michael@0: listen_addr.sin_family = AF_INET; michael@0: listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); michael@0: listen_addr.sin_port = 0; /* kernel chooses port. */ michael@0: if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr)) michael@0: == -1) michael@0: goto tidy_up_and_fail; michael@0: if (listen(listener, 1) == -1) michael@0: goto tidy_up_and_fail; michael@0: michael@0: connector = socket(AF_INET, type, 0); michael@0: if (connector < 0) michael@0: goto tidy_up_and_fail; michael@0: /* We want to find out the port number to connect to. */ michael@0: size = sizeof(connect_addr); michael@0: if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1) michael@0: goto tidy_up_and_fail; michael@0: if (size != sizeof (connect_addr)) michael@0: goto abort_tidy_up_and_fail; michael@0: if (connect(connector, (struct sockaddr *) &connect_addr, michael@0: sizeof(connect_addr)) == -1) michael@0: goto tidy_up_and_fail; michael@0: michael@0: size = sizeof(listen_addr); michael@0: acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size); michael@0: if (acceptor < 0) michael@0: goto tidy_up_and_fail; michael@0: if (size != sizeof(listen_addr)) michael@0: goto abort_tidy_up_and_fail; michael@0: evutil_closesocket(listener); michael@0: /* Now check we are talking to ourself by matching port and host on the michael@0: two sockets. */ michael@0: if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1) michael@0: goto tidy_up_and_fail; michael@0: if (size != sizeof (connect_addr) michael@0: || listen_addr.sin_family != connect_addr.sin_family michael@0: || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr michael@0: || listen_addr.sin_port != connect_addr.sin_port) michael@0: goto abort_tidy_up_and_fail; michael@0: fd[0] = connector; michael@0: fd[1] = acceptor; michael@0: michael@0: return 0; michael@0: michael@0: abort_tidy_up_and_fail: michael@0: saved_errno = ERR(ECONNABORTED); michael@0: tidy_up_and_fail: michael@0: if (saved_errno < 0) michael@0: saved_errno = EVUTIL_SOCKET_ERROR(); michael@0: if (listener != -1) michael@0: evutil_closesocket(listener); michael@0: if (connector != -1) michael@0: evutil_closesocket(connector); michael@0: if (acceptor != -1) michael@0: evutil_closesocket(acceptor); michael@0: michael@0: EVUTIL_SET_SOCKET_ERROR(saved_errno); michael@0: return -1; michael@0: #undef ERR michael@0: } michael@0: michael@0: int michael@0: evutil_make_socket_nonblocking(evutil_socket_t fd) michael@0: { michael@0: #ifdef WIN32 michael@0: { michael@0: u_long nonblocking = 1; michael@0: if (ioctlsocket(fd, FIONBIO, &nonblocking) == SOCKET_ERROR) { michael@0: event_sock_warn(fd, "fcntl(%d, F_GETFL)", (int)fd); michael@0: return -1; michael@0: } michael@0: } michael@0: #else michael@0: { michael@0: int flags; michael@0: if ((flags = fcntl(fd, F_GETFL, NULL)) < 0) { michael@0: event_warn("fcntl(%d, F_GETFL)", fd); michael@0: return -1; michael@0: } michael@0: if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { michael@0: event_warn("fcntl(%d, F_SETFL)", fd); michael@0: return -1; michael@0: } michael@0: } michael@0: #endif michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: evutil_make_listen_socket_reuseable(evutil_socket_t sock) michael@0: { michael@0: #ifndef WIN32 michael@0: int one = 1; michael@0: /* REUSEADDR on Unix means, "don't hang on to this address after the michael@0: * listener is closed." On Windows, though, it means "don't keep other michael@0: * processes from binding to this address while we're using it. */ michael@0: return setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one, michael@0: (ev_socklen_t)sizeof(one)); michael@0: #else michael@0: return 0; michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: evutil_make_socket_closeonexec(evutil_socket_t fd) michael@0: { michael@0: #if !defined(WIN32) && defined(_EVENT_HAVE_SETFD) michael@0: int flags; michael@0: if ((flags = fcntl(fd, F_GETFD, NULL)) < 0) { michael@0: event_warn("fcntl(%d, F_GETFD)", fd); michael@0: return -1; michael@0: } michael@0: if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { michael@0: event_warn("fcntl(%d, F_SETFD)", fd); michael@0: return -1; michael@0: } michael@0: #endif michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: evutil_closesocket(evutil_socket_t sock) michael@0: { michael@0: #ifndef WIN32 michael@0: return close(sock); michael@0: #else michael@0: return closesocket(sock); michael@0: #endif michael@0: } michael@0: michael@0: ev_int64_t michael@0: evutil_strtoll(const char *s, char **endptr, int base) michael@0: { michael@0: #ifdef _EVENT_HAVE_STRTOLL michael@0: return (ev_int64_t)strtoll(s, endptr, base); michael@0: #elif _EVENT_SIZEOF_LONG == 8 michael@0: return (ev_int64_t)strtol(s, endptr, base); michael@0: #elif defined(WIN32) && defined(_MSC_VER) && _MSC_VER < 1300 michael@0: /* XXXX on old versions of MS APIs, we only support base michael@0: * 10. */ michael@0: ev_int64_t r; michael@0: if (base != 10) michael@0: return 0; michael@0: r = (ev_int64_t) _atoi64(s); michael@0: while (isspace(*s)) michael@0: ++s; michael@0: if (*s == '-') michael@0: ++s; michael@0: while (isdigit(*s)) michael@0: ++s; michael@0: if (endptr) michael@0: *endptr = (char*) s; michael@0: return r; michael@0: #elif defined(WIN32) michael@0: return (ev_int64_t) _strtoi64(s, endptr, base); michael@0: #elif defined(_EVENT_SIZEOF_LONG_LONG) && _EVENT_SIZEOF_LONG_LONG == 8 michael@0: long long r; michael@0: int n; michael@0: if (base != 10 && base != 16) michael@0: return 0; michael@0: if (base == 10) { michael@0: n = sscanf(s, "%lld", &r); michael@0: } else { michael@0: unsigned long long ru=0; michael@0: n = sscanf(s, "%llx", &ru); michael@0: if (ru > EV_INT64_MAX) michael@0: return 0; michael@0: r = (long long) ru; michael@0: } michael@0: if (n != 1) michael@0: return 0; michael@0: while (EVUTIL_ISSPACE(*s)) michael@0: ++s; michael@0: if (*s == '-') michael@0: ++s; michael@0: if (base == 10) { michael@0: while (EVUTIL_ISDIGIT(*s)) michael@0: ++s; michael@0: } else { michael@0: while (EVUTIL_ISXDIGIT(*s)) michael@0: ++s; michael@0: } michael@0: if (endptr) michael@0: *endptr = (char*) s; michael@0: return r; michael@0: #else michael@0: #error "I don't know how to parse 64-bit integers." michael@0: #endif michael@0: } michael@0: michael@0: #ifndef _EVENT_HAVE_GETTIMEOFDAY michael@0: /* No gettimeofday; this muse be windows. */ michael@0: int michael@0: evutil_gettimeofday(struct timeval *tv, struct timezone *tz) michael@0: { michael@0: struct _timeb tb; michael@0: michael@0: if (tv == NULL) michael@0: return -1; michael@0: michael@0: /* XXXX michael@0: * _ftime is not the greatest interface here; GetSystemTimeAsFileTime michael@0: * would give us better resolution, whereas something cobbled together michael@0: * with GetTickCount could maybe give us monotonic behavior. michael@0: * michael@0: * Either way, I think this value might be skewed to ignore the michael@0: * timezone, and just return local time. That's not so good. michael@0: */ michael@0: _ftime(&tb); michael@0: tv->tv_sec = (long) tb.time; michael@0: tv->tv_usec = ((int) tb.millitm) * 1000; michael@0: return 0; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef WIN32 michael@0: int michael@0: evutil_socket_geterror(evutil_socket_t sock) michael@0: { michael@0: int optval, optvallen=sizeof(optval); michael@0: int err = WSAGetLastError(); michael@0: if (err == WSAEWOULDBLOCK && sock >= 0) { michael@0: if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, michael@0: &optvallen)) michael@0: return err; michael@0: if (optval) michael@0: return optval; michael@0: } michael@0: return err; michael@0: } michael@0: #endif michael@0: michael@0: /* XXX we should use an enum here. */ michael@0: /* 2 for connection refused, 1 for connected, 0 for not yet, -1 for error. */ michael@0: int michael@0: evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen) michael@0: { michael@0: int made_fd = 0; michael@0: michael@0: if (*fd_ptr < 0) { michael@0: if ((*fd_ptr = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) michael@0: goto err; michael@0: made_fd = 1; michael@0: if (evutil_make_socket_nonblocking(*fd_ptr) < 0) { michael@0: goto err; michael@0: } michael@0: } michael@0: michael@0: if (connect(*fd_ptr, sa, socklen) < 0) { michael@0: int e = evutil_socket_geterror(*fd_ptr); michael@0: if (EVUTIL_ERR_CONNECT_RETRIABLE(e)) michael@0: return 0; michael@0: if (EVUTIL_ERR_CONNECT_REFUSED(e)) michael@0: return 2; michael@0: goto err; michael@0: } else { michael@0: return 1; michael@0: } michael@0: michael@0: err: michael@0: if (made_fd) { michael@0: evutil_closesocket(*fd_ptr); michael@0: *fd_ptr = -1; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: /* Check whether a socket on which we called connect() is done michael@0: connecting. Return 1 for connected, 0 for not yet, -1 for error. In the michael@0: error case, set the current socket errno to the error that happened during michael@0: the connect operation. */ michael@0: int michael@0: evutil_socket_finished_connecting(evutil_socket_t fd) michael@0: { michael@0: int e; michael@0: ev_socklen_t elen = sizeof(e); michael@0: michael@0: if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&e, &elen) < 0) michael@0: return -1; michael@0: michael@0: if (e) { michael@0: if (EVUTIL_ERR_CONNECT_RETRIABLE(e)) michael@0: return 0; michael@0: EVUTIL_SET_SOCKET_ERROR(e); michael@0: return -1; michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: #if (EVUTIL_AI_PASSIVE|EVUTIL_AI_CANONNAME|EVUTIL_AI_NUMERICHOST| \ michael@0: EVUTIL_AI_NUMERICSERV|EVUTIL_AI_V4MAPPED|EVUTIL_AI_ALL| \ michael@0: EVUTIL_AI_ADDRCONFIG) != \ michael@0: (EVUTIL_AI_PASSIVE^EVUTIL_AI_CANONNAME^EVUTIL_AI_NUMERICHOST^ \ michael@0: EVUTIL_AI_NUMERICSERV^EVUTIL_AI_V4MAPPED^EVUTIL_AI_ALL^ \ michael@0: EVUTIL_AI_ADDRCONFIG) michael@0: #error "Some of our EVUTIL_AI_* flags seem to overlap with system AI_* flags" michael@0: #endif michael@0: michael@0: /* We sometimes need to know whether we have an ipv4 address and whether we michael@0: have an ipv6 address. If 'have_checked_interfaces', then we've already done michael@0: the test. If 'had_ipv4_address', then it turns out we had an ipv4 address. michael@0: If 'had_ipv6_address', then it turns out we had an ipv6 address. These are michael@0: set by evutil_check_interfaces. */ michael@0: static int have_checked_interfaces, had_ipv4_address, had_ipv6_address; michael@0: michael@0: /* Macro: True iff the IPv4 address 'addr', in host order, is in 127.0.0.0/8 michael@0: */ michael@0: #define EVUTIL_V4ADDR_IS_LOCALHOST(addr) (((addr)>>24) == 127) michael@0: michael@0: /* Macro: True iff the IPv4 address 'addr', in host order, is a class D michael@0: * (multiclass) address. michael@0: */ michael@0: #define EVUTIL_V4ADDR_IS_CLASSD(addr) ((((addr)>>24) & 0xf0) == 0xe0) michael@0: michael@0: /* Test whether we have an ipv4 interface and an ipv6 interface. Return 0 if michael@0: * the test seemed successful. */ michael@0: static int michael@0: evutil_check_interfaces(int force_recheck) michael@0: { michael@0: const char ZEROES[] = "\x00\x00\x00\x00\x00\x00\x00\x00" michael@0: "\x00\x00\x00\x00\x00\x00\x00\x00"; michael@0: evutil_socket_t fd = -1; michael@0: struct sockaddr_in sin, sin_out; michael@0: struct sockaddr_in6 sin6, sin6_out; michael@0: ev_socklen_t sin_out_len = sizeof(sin_out); michael@0: ev_socklen_t sin6_out_len = sizeof(sin6_out); michael@0: int r; michael@0: char buf[128]; michael@0: if (have_checked_interfaces && !force_recheck) michael@0: return 0; michael@0: michael@0: /* To check whether we have an interface open for a given protocol, we michael@0: * try to make a UDP 'connection' to a remote host on the internet. michael@0: * We don't actually use it, so the address doesn't matter, but we michael@0: * want to pick one that keep us from using a host- or link-local michael@0: * interface. */ michael@0: memset(&sin, 0, sizeof(sin)); michael@0: sin.sin_family = AF_INET; michael@0: sin.sin_port = htons(53); michael@0: r = evutil_inet_pton(AF_INET, "18.244.0.188", &sin.sin_addr); michael@0: EVUTIL_ASSERT(r); michael@0: michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: sin6.sin6_family = AF_INET6; michael@0: sin6.sin6_port = htons(53); michael@0: r = evutil_inet_pton(AF_INET6, "2001:4860:b002::68", &sin6.sin6_addr); michael@0: EVUTIL_ASSERT(r); michael@0: michael@0: memset(&sin_out, 0, sizeof(sin_out)); michael@0: memset(&sin6_out, 0, sizeof(sin6_out)); michael@0: michael@0: /* XXX some errnos mean 'no address'; some mean 'not enough sockets'. */ michael@0: if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) >= 0 && michael@0: connect(fd, (struct sockaddr*)&sin, sizeof(sin)) == 0 && michael@0: getsockname(fd, (struct sockaddr*)&sin_out, &sin_out_len) == 0) { michael@0: /* We might have an IPv4 interface. */ michael@0: ev_uint32_t addr = ntohl(sin_out.sin_addr.s_addr); michael@0: if (addr == 0 || michael@0: EVUTIL_V4ADDR_IS_LOCALHOST(addr) || michael@0: EVUTIL_V4ADDR_IS_CLASSD(addr)) { michael@0: evutil_inet_ntop(AF_INET, &sin_out.sin_addr, michael@0: buf, sizeof(buf)); michael@0: /* This is a reserved, ipv4compat, ipv4map, loopback, michael@0: * link-local or unspecified address. The host should michael@0: * never have given it to us; it could never connect michael@0: * to sin. */ michael@0: event_warnx("Got a strange local ipv4 address %s",buf); michael@0: } else { michael@0: event_debug(("Detected an IPv4 interface")); michael@0: had_ipv4_address = 1; michael@0: } michael@0: } michael@0: if (fd >= 0) michael@0: evutil_closesocket(fd); michael@0: michael@0: if ((fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) >= 0 && michael@0: connect(fd, (struct sockaddr*)&sin6, sizeof(sin6)) == 0 && michael@0: getsockname(fd, (struct sockaddr*)&sin6_out, &sin6_out_len) == 0) { michael@0: /* We might have an IPv6 interface. */ michael@0: const unsigned char *addr = michael@0: (unsigned char*)sin6_out.sin6_addr.s6_addr; michael@0: if (!memcmp(addr, ZEROES, 8) || michael@0: (addr[0] == 0xfe && (addr[1] & 0xc0) == 0x80)) { michael@0: /* This is a reserved, ipv4compat, ipv4map, loopback, michael@0: * link-local or unspecified address. The host should michael@0: * never have given it to us; it could never connect michael@0: * to sin6. */ michael@0: evutil_inet_ntop(AF_INET6, &sin6_out.sin6_addr, michael@0: buf, sizeof(buf)); michael@0: event_warnx("Got a strange local ipv6 address %s",buf); michael@0: } else { michael@0: event_debug(("Detected an IPv4 interface")); michael@0: had_ipv6_address = 1; michael@0: } michael@0: } michael@0: michael@0: if (fd >= 0) michael@0: evutil_closesocket(fd); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /* Internal addrinfo flag. This one is set when we allocate the addrinfo from michael@0: * inside libevent. Otherwise, the built-in getaddrinfo() function allocated michael@0: * it, and we should trust what they said. michael@0: **/ michael@0: #define EVUTIL_AI_LIBEVENT_ALLOCATED 0x80000000 michael@0: michael@0: /* Helper: construct a new addrinfo containing the socket address in michael@0: * 'sa', which must be a sockaddr_in or a sockaddr_in6. Take the michael@0: * socktype and protocol info from hints. If they weren't set, then michael@0: * allocate both a TCP and a UDP addrinfo. michael@0: */ michael@0: struct evutil_addrinfo * michael@0: evutil_new_addrinfo(struct sockaddr *sa, ev_socklen_t socklen, michael@0: const struct evutil_addrinfo *hints) michael@0: { michael@0: struct evutil_addrinfo *res; michael@0: EVUTIL_ASSERT(hints); michael@0: michael@0: if (hints->ai_socktype == 0 && hints->ai_protocol == 0) { michael@0: /* Indecisive user! Give them a UDP and a TCP. */ michael@0: struct evutil_addrinfo *r1, *r2; michael@0: struct evutil_addrinfo tmp; michael@0: memcpy(&tmp, hints, sizeof(tmp)); michael@0: tmp.ai_socktype = SOCK_STREAM; tmp.ai_protocol = IPPROTO_TCP; michael@0: r1 = evutil_new_addrinfo(sa, socklen, &tmp); michael@0: if (!r1) michael@0: return NULL; michael@0: tmp.ai_socktype = SOCK_DGRAM; tmp.ai_protocol = IPPROTO_UDP; michael@0: r2 = evutil_new_addrinfo(sa, socklen, &tmp); michael@0: if (!r2) { michael@0: evutil_freeaddrinfo(r1); michael@0: return NULL; michael@0: } michael@0: r1->ai_next = r2; michael@0: return r1; michael@0: } michael@0: michael@0: /* We're going to allocate extra space to hold the sockaddr. */ michael@0: res = mm_calloc(1,sizeof(struct evutil_addrinfo)+socklen); michael@0: if (!res) michael@0: return NULL; michael@0: res->ai_addr = (struct sockaddr*) michael@0: (((char*)res) + sizeof(struct evutil_addrinfo)); michael@0: memcpy(res->ai_addr, sa, socklen); michael@0: res->ai_addrlen = socklen; michael@0: res->ai_family = sa->sa_family; /* Same or not? XXX */ michael@0: res->ai_flags = EVUTIL_AI_LIBEVENT_ALLOCATED; michael@0: res->ai_socktype = hints->ai_socktype; michael@0: res->ai_protocol = hints->ai_protocol; michael@0: michael@0: return res; michael@0: } michael@0: michael@0: /* Append the addrinfo 'append' to the end of 'first', and return the start of michael@0: * the list. Either element can be NULL, in which case we return the element michael@0: * that is not NULL. */ michael@0: struct evutil_addrinfo * michael@0: evutil_addrinfo_append(struct evutil_addrinfo *first, michael@0: struct evutil_addrinfo *append) michael@0: { michael@0: struct evutil_addrinfo *ai = first; michael@0: if (!ai) michael@0: return append; michael@0: while (ai->ai_next) michael@0: ai = ai->ai_next; michael@0: ai->ai_next = append; michael@0: michael@0: return first; michael@0: } michael@0: michael@0: static int michael@0: parse_numeric_servname(const char *servname) michael@0: { michael@0: int n; michael@0: char *endptr=NULL; michael@0: n = (int) strtol(servname, &endptr, 10); michael@0: if (n>=0 && n <= 65535 && servname[0] && endptr && !endptr[0]) michael@0: return n; michael@0: else michael@0: return -1; michael@0: } michael@0: michael@0: /** Parse a service name in 'servname', which can be a decimal port. michael@0: * Return the port number, or -1 on error. michael@0: */ michael@0: static int michael@0: evutil_parse_servname(const char *servname, const char *protocol, michael@0: const struct evutil_addrinfo *hints) michael@0: { michael@0: int n = parse_numeric_servname(servname); michael@0: if (n>=0) michael@0: return n; michael@0: #if defined(_EVENT_HAVE_GETSERVBYNAME) || defined(WIN32) michael@0: if (!(hints->ai_flags & EVUTIL_AI_NUMERICSERV)) { michael@0: struct servent *ent = getservbyname(servname, protocol); michael@0: if (ent) { michael@0: return ntohs(ent->s_port); michael@0: } michael@0: } michael@0: #endif michael@0: return -1; michael@0: } michael@0: michael@0: /* Return a string corresponding to a protocol number that we can pass to michael@0: * getservyname. */ michael@0: static const char * michael@0: evutil_unparse_protoname(int proto) michael@0: { michael@0: switch (proto) { michael@0: case 0: michael@0: return NULL; michael@0: case IPPROTO_TCP: michael@0: return "tcp"; michael@0: case IPPROTO_UDP: michael@0: return "udp"; michael@0: #ifdef IPPROTO_SCTP michael@0: case IPPROTO_SCTP: michael@0: return "sctp"; michael@0: #endif michael@0: default: michael@0: #ifdef _EVENT_HAVE_GETPROTOBYNUMBER michael@0: { michael@0: struct protoent *ent = getprotobynumber(proto); michael@0: if (ent) michael@0: return ent->p_name; michael@0: } michael@0: #endif michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: evutil_getaddrinfo_infer_protocols(struct evutil_addrinfo *hints) michael@0: { michael@0: /* If we can guess the protocol from the socktype, do so. */ michael@0: if (!hints->ai_protocol && hints->ai_socktype) { michael@0: if (hints->ai_socktype == SOCK_DGRAM) michael@0: hints->ai_protocol = IPPROTO_UDP; michael@0: else if (hints->ai_socktype == SOCK_STREAM) michael@0: hints->ai_protocol = IPPROTO_TCP; michael@0: } michael@0: michael@0: /* Set the socktype if it isn't set. */ michael@0: if (!hints->ai_socktype && hints->ai_protocol) { michael@0: if (hints->ai_protocol == IPPROTO_UDP) michael@0: hints->ai_socktype = SOCK_DGRAM; michael@0: else if (hints->ai_protocol == IPPROTO_TCP) michael@0: hints->ai_socktype = SOCK_STREAM; michael@0: #ifdef IPPROTO_SCTP michael@0: else if (hints->ai_protocol == IPPROTO_SCTP) michael@0: hints->ai_socktype = SOCK_STREAM; michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: #if AF_UNSPEC != PF_UNSPEC michael@0: #error "I cannot build on a system where AF_UNSPEC != PF_UNSPEC" michael@0: #endif michael@0: michael@0: /** Implements the part of looking up hosts by name that's common to both michael@0: * the blocking and nonblocking resolver: michael@0: * - Adjust 'hints' to have a reasonable socktype and protocol. michael@0: * - Look up the port based on 'servname', and store it in *portnum, michael@0: * - Handle the nodename==NULL case michael@0: * - Handle some invalid arguments cases. michael@0: * - Handle the cases where nodename is an IPv4 or IPv6 address. michael@0: * michael@0: * If we need the resolver to look up the hostname, we return michael@0: * EVUTIL_EAI_NEED_RESOLVE. Otherwise, we can completely implement michael@0: * getaddrinfo: we return 0 or an appropriate EVUTIL_EAI_* error, and michael@0: * set *res as getaddrinfo would. michael@0: */ michael@0: int michael@0: evutil_getaddrinfo_common(const char *nodename, const char *servname, michael@0: struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum) michael@0: { michael@0: int port = 0; michael@0: const char *pname; michael@0: michael@0: if (nodename == NULL && servname == NULL) michael@0: return EVUTIL_EAI_NONAME; michael@0: michael@0: /* We only understand 3 families */ michael@0: if (hints->ai_family != PF_UNSPEC && hints->ai_family != PF_INET && michael@0: hints->ai_family != PF_INET6) michael@0: return EVUTIL_EAI_FAMILY; michael@0: michael@0: evutil_getaddrinfo_infer_protocols(hints); michael@0: michael@0: /* Look up the port number and protocol, if possible. */ michael@0: pname = evutil_unparse_protoname(hints->ai_protocol); michael@0: if (servname) { michael@0: /* XXXX We could look at the protocol we got back from michael@0: * getservbyname, but it doesn't seem too useful. */ michael@0: port = evutil_parse_servname(servname, pname, hints); michael@0: if (port < 0) { michael@0: return EVUTIL_EAI_NONAME; michael@0: } michael@0: } michael@0: michael@0: /* If we have no node name, then we're supposed to bind to 'any' and michael@0: * connect to localhost. */ michael@0: if (nodename == NULL) { michael@0: struct evutil_addrinfo *res4=NULL, *res6=NULL; michael@0: if (hints->ai_family != PF_INET) { /* INET6 or UNSPEC. */ michael@0: struct sockaddr_in6 sin6; michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: sin6.sin6_family = AF_INET6; michael@0: sin6.sin6_port = htons(port); michael@0: if (hints->ai_flags & EVUTIL_AI_PASSIVE) { michael@0: /* Bind to :: */ michael@0: } else { michael@0: /* connect to ::1 */ michael@0: sin6.sin6_addr.s6_addr[15] = 1; michael@0: } michael@0: res6 = evutil_new_addrinfo((struct sockaddr*)&sin6, michael@0: sizeof(sin6), hints); michael@0: if (!res6) michael@0: return EVUTIL_EAI_MEMORY; michael@0: } michael@0: michael@0: if (hints->ai_family != PF_INET6) { /* INET or UNSPEC */ michael@0: struct sockaddr_in sin; michael@0: memset(&sin, 0, sizeof(sin)); michael@0: sin.sin_family = AF_INET; michael@0: sin.sin_port = htons(port); michael@0: if (hints->ai_flags & EVUTIL_AI_PASSIVE) { michael@0: /* Bind to 0.0.0.0 */ michael@0: } else { michael@0: /* connect to 127.0.0.1 */ michael@0: sin.sin_addr.s_addr = htonl(0x7f000001); michael@0: } michael@0: res4 = evutil_new_addrinfo((struct sockaddr*)&sin, michael@0: sizeof(sin), hints); michael@0: if (!res4) { michael@0: if (res6) michael@0: evutil_freeaddrinfo(res6); michael@0: return EVUTIL_EAI_MEMORY; michael@0: } michael@0: } michael@0: *res = evutil_addrinfo_append(res4, res6); michael@0: return 0; michael@0: } michael@0: michael@0: /* If we can, we should try to parse the hostname without resolving michael@0: * it. */ michael@0: /* Try ipv6. */ michael@0: if (hints->ai_family == PF_INET6 || hints->ai_family == PF_UNSPEC) { michael@0: struct sockaddr_in6 sin6; michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: if (1==evutil_inet_pton(AF_INET6, nodename, &sin6.sin6_addr)) { michael@0: /* Got an ipv6 address. */ michael@0: sin6.sin6_family = AF_INET6; michael@0: sin6.sin6_port = htons(port); michael@0: *res = evutil_new_addrinfo((struct sockaddr*)&sin6, michael@0: sizeof(sin6), hints); michael@0: if (!*res) michael@0: return EVUTIL_EAI_MEMORY; michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: /* Try ipv4. */ michael@0: if (hints->ai_family == PF_INET || hints->ai_family == PF_UNSPEC) { michael@0: struct sockaddr_in sin; michael@0: memset(&sin, 0, sizeof(sin)); michael@0: if (1==evutil_inet_pton(AF_INET, nodename, &sin.sin_addr)) { michael@0: /* Got an ipv6 address. */ michael@0: sin.sin_family = AF_INET; michael@0: sin.sin_port = htons(port); michael@0: *res = evutil_new_addrinfo((struct sockaddr*)&sin, michael@0: sizeof(sin), hints); michael@0: if (!*res) michael@0: return EVUTIL_EAI_MEMORY; michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* If we have reached this point, we definitely need to do a DNS michael@0: * lookup. */ michael@0: if ((hints->ai_flags & EVUTIL_AI_NUMERICHOST)) { michael@0: /* If we're not allowed to do one, then say so. */ michael@0: return EVUTIL_EAI_NONAME; michael@0: } michael@0: *portnum = port; michael@0: return EVUTIL_EAI_NEED_RESOLVE; michael@0: } michael@0: michael@0: #ifdef _EVENT_HAVE_GETADDRINFO michael@0: #define USE_NATIVE_GETADDRINFO michael@0: #endif michael@0: michael@0: #ifdef USE_NATIVE_GETADDRINFO michael@0: /* A mask of all the flags that we declare, so we can clear them before calling michael@0: * the native getaddrinfo */ michael@0: static const unsigned int ALL_NONNATIVE_AI_FLAGS = michael@0: #ifndef AI_PASSIVE michael@0: EVUTIL_AI_PASSIVE | michael@0: #endif michael@0: #ifndef AI_CANONNAME michael@0: EVUTIL_AI_CANONNAME | michael@0: #endif michael@0: #ifndef AI_NUMERICHOST michael@0: EVUTIL_AI_NUMERICHOST | michael@0: #endif michael@0: #ifndef AI_NUMERICSERV michael@0: EVUTIL_AI_NUMERICSERV | michael@0: #endif michael@0: #ifndef AI_ADDRCONFIG michael@0: EVUTIL_AI_ADDRCONFIG | michael@0: #endif michael@0: #ifndef AI_ALL michael@0: EVUTIL_AI_ALL | michael@0: #endif michael@0: #ifndef AI_V4MAPPED michael@0: EVUTIL_AI_V4MAPPED | michael@0: #endif michael@0: EVUTIL_AI_LIBEVENT_ALLOCATED; michael@0: michael@0: static const unsigned int ALL_NATIVE_AI_FLAGS = michael@0: #ifdef AI_PASSIVE michael@0: AI_PASSIVE | michael@0: #endif michael@0: #ifdef AI_CANONNAME michael@0: AI_CANONNAME | michael@0: #endif michael@0: #ifdef AI_NUMERICHOST michael@0: AI_NUMERICHOST | michael@0: #endif michael@0: #ifdef AI_NUMERICSERV michael@0: AI_NUMERICSERV | michael@0: #endif michael@0: #ifdef AI_ADDRCONFIG michael@0: AI_ADDRCONFIG | michael@0: #endif michael@0: #ifdef AI_ALL michael@0: AI_ALL | michael@0: #endif michael@0: #ifdef AI_V4MAPPED michael@0: AI_V4MAPPED | michael@0: #endif michael@0: 0; michael@0: #endif michael@0: michael@0: #ifndef USE_NATIVE_GETADDRINFO michael@0: /* Helper for systems with no getaddrinfo(): make one or more addrinfos out of michael@0: * a struct hostent. michael@0: */ michael@0: static struct evutil_addrinfo * michael@0: addrinfo_from_hostent(const struct hostent *ent, michael@0: int port, const struct evutil_addrinfo *hints) michael@0: { michael@0: int i; michael@0: struct sockaddr_in sin; michael@0: struct sockaddr_in6 sin6; michael@0: struct sockaddr *sa; michael@0: int socklen; michael@0: struct evutil_addrinfo *res=NULL, *ai; michael@0: void *addrp; michael@0: michael@0: if (ent->h_addrtype == PF_INET) { michael@0: memset(&sin, 0, sizeof(sin)); michael@0: sin.sin_family = AF_INET; michael@0: sin.sin_port = htons(port); michael@0: sa = (struct sockaddr *)&sin; michael@0: socklen = sizeof(struct sockaddr_in); michael@0: addrp = &sin.sin_addr; michael@0: if (ent->h_length != sizeof(sin.sin_addr)) { michael@0: event_warnx("Weird h_length from gethostbyname"); michael@0: return NULL; michael@0: } michael@0: } else if (ent->h_addrtype == PF_INET6) { michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: sin6.sin6_family = AF_INET6; michael@0: sin6.sin6_port = htons(port); michael@0: sa = (struct sockaddr *)&sin6; michael@0: socklen = sizeof(struct sockaddr_in); michael@0: addrp = &sin6.sin6_addr; michael@0: if (ent->h_length != sizeof(sin6.sin6_addr)) { michael@0: event_warnx("Weird h_length from gethostbyname"); michael@0: return NULL; michael@0: } michael@0: } else michael@0: return NULL; michael@0: michael@0: for (i = 0; ent->h_addr_list[i]; ++i) { michael@0: memcpy(addrp, ent->h_addr_list[i], ent->h_length); michael@0: ai = evutil_new_addrinfo(sa, socklen, hints); michael@0: if (!ai) { michael@0: evutil_freeaddrinfo(res); michael@0: return NULL; michael@0: } michael@0: res = evutil_addrinfo_append(res, ai); michael@0: } michael@0: michael@0: if (res && ((hints->ai_flags & EVUTIL_AI_CANONNAME) && ent->h_name)) { michael@0: res->ai_canonname = mm_strdup(ent->h_name); michael@0: if (res->ai_canonname == NULL) { michael@0: evutil_freeaddrinfo(res); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: #endif michael@0: michael@0: /* If the EVUTIL_AI_ADDRCONFIG flag is set on hints->ai_flags, and michael@0: * hints->ai_family is PF_UNSPEC, then revise the value of hints->ai_family so michael@0: * that we'll only get addresses we could maybe connect to. michael@0: */ michael@0: void michael@0: evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints) michael@0: { michael@0: if (!(hints->ai_flags & EVUTIL_AI_ADDRCONFIG)) michael@0: return; michael@0: if (hints->ai_family != PF_UNSPEC) michael@0: return; michael@0: if (!have_checked_interfaces) michael@0: evutil_check_interfaces(0); michael@0: if (had_ipv4_address && !had_ipv6_address) { michael@0: hints->ai_family = PF_INET; michael@0: } else if (!had_ipv4_address && had_ipv6_address) { michael@0: hints->ai_family = PF_INET6; michael@0: } michael@0: } michael@0: michael@0: #ifdef USE_NATIVE_GETADDRINFO michael@0: static int need_numeric_port_hack_=0; michael@0: static int need_socktype_protocol_hack_=0; michael@0: static int tested_for_getaddrinfo_hacks=0; michael@0: michael@0: /* Some older BSDs (like OpenBSD up to 4.6) used to believe that michael@0: giving a numeric port without giving an ai_socktype was verboten. michael@0: We test for this so we can apply an appropriate workaround. If it michael@0: turns out that the bug is present, then: michael@0: michael@0: - If nodename==NULL and servname is numeric, we build an answer michael@0: ourselves using evutil_getaddrinfo_common(). michael@0: michael@0: - If nodename!=NULL and servname is numeric, then we set michael@0: servname=NULL when calling getaddrinfo, and post-process the michael@0: result to set the ports on it. michael@0: michael@0: We test for this bug at runtime, since otherwise we can't have the michael@0: same binary run on multiple BSD versions. michael@0: michael@0: - Some versions of Solaris believe that it's nice to leave to protocol michael@0: field set to 0. We test for this so we can apply an appropriate michael@0: workaround. michael@0: */ michael@0: static void michael@0: test_for_getaddrinfo_hacks(void) michael@0: { michael@0: int r, r2; michael@0: struct evutil_addrinfo *ai=NULL, *ai2=NULL; michael@0: struct evutil_addrinfo hints; michael@0: michael@0: memset(&hints,0,sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_flags = michael@0: #ifdef AI_NUMERICHOST michael@0: AI_NUMERICHOST | michael@0: #endif michael@0: #ifdef AI_NUMERICSERV michael@0: AI_NUMERICSERV | michael@0: #endif michael@0: 0; michael@0: r = getaddrinfo("1.2.3.4", "80", &hints, &ai); michael@0: hints.ai_socktype = SOCK_STREAM; michael@0: r2 = getaddrinfo("1.2.3.4", "80", &hints, &ai2); michael@0: if (r2 == 0 && r != 0) { michael@0: need_numeric_port_hack_=1; michael@0: } michael@0: if (ai2 && ai2->ai_protocol == 0) { michael@0: need_socktype_protocol_hack_=1; michael@0: } michael@0: michael@0: if (ai) michael@0: freeaddrinfo(ai); michael@0: if (ai2) michael@0: freeaddrinfo(ai2); michael@0: tested_for_getaddrinfo_hacks=1; michael@0: } michael@0: michael@0: static inline int michael@0: need_numeric_port_hack(void) michael@0: { michael@0: if (!tested_for_getaddrinfo_hacks) michael@0: test_for_getaddrinfo_hacks(); michael@0: return need_numeric_port_hack_; michael@0: } michael@0: michael@0: static inline int michael@0: need_socktype_protocol_hack(void) michael@0: { michael@0: if (!tested_for_getaddrinfo_hacks) michael@0: test_for_getaddrinfo_hacks(); michael@0: return need_socktype_protocol_hack_; michael@0: } michael@0: michael@0: static void michael@0: apply_numeric_port_hack(int port, struct evutil_addrinfo **ai) michael@0: { michael@0: /* Now we run through the list and set the ports on all of the michael@0: * results where ports would make sense. */ michael@0: for ( ; *ai; ai = &(*ai)->ai_next) { michael@0: struct sockaddr *sa = (*ai)->ai_addr; michael@0: if (sa && sa->sa_family == AF_INET) { michael@0: struct sockaddr_in *sin = (struct sockaddr_in*)sa; michael@0: sin->sin_port = htons(port); michael@0: } else if (sa && sa->sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa; michael@0: sin6->sin6_port = htons(port); michael@0: } else { michael@0: /* A numeric port makes no sense here; remove this one michael@0: * from the list. */ michael@0: struct evutil_addrinfo *victim = *ai; michael@0: *ai = victim->ai_next; michael@0: victim->ai_next = NULL; michael@0: freeaddrinfo(victim); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static int michael@0: apply_socktype_protocol_hack(struct evutil_addrinfo *ai) michael@0: { michael@0: struct evutil_addrinfo *ai_new; michael@0: for (; ai; ai = ai->ai_next) { michael@0: evutil_getaddrinfo_infer_protocols(ai); michael@0: if (ai->ai_socktype || ai->ai_protocol) michael@0: continue; michael@0: ai_new = mm_malloc(sizeof(*ai_new)); michael@0: if (!ai_new) michael@0: return -1; michael@0: memcpy(ai_new, ai, sizeof(*ai_new)); michael@0: ai->ai_socktype = SOCK_STREAM; michael@0: ai->ai_protocol = IPPROTO_TCP; michael@0: ai_new->ai_socktype = SOCK_DGRAM; michael@0: ai_new->ai_protocol = IPPROTO_UDP; michael@0: michael@0: ai_new->ai_next = ai->ai_next; michael@0: ai->ai_next = ai_new; michael@0: } michael@0: return 0; michael@0: } michael@0: #endif michael@0: michael@0: int michael@0: evutil_getaddrinfo(const char *nodename, const char *servname, michael@0: const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res) michael@0: { michael@0: #ifdef USE_NATIVE_GETADDRINFO michael@0: struct evutil_addrinfo hints; michael@0: int portnum=-1, need_np_hack, err; michael@0: michael@0: if (hints_in) { michael@0: memcpy(&hints, hints_in, sizeof(hints)); michael@0: } else { michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: } michael@0: michael@0: #ifndef AI_ADDRCONFIG michael@0: /* Not every system has AI_ADDRCONFIG, so fake it. */ michael@0: if (hints.ai_family == PF_UNSPEC && michael@0: (hints.ai_flags & EVUTIL_AI_ADDRCONFIG)) { michael@0: evutil_adjust_hints_for_addrconfig(&hints); michael@0: } michael@0: #endif michael@0: michael@0: #ifndef AI_NUMERICSERV michael@0: /* Not every system has AI_NUMERICSERV, so fake it. */ michael@0: if (hints.ai_flags & EVUTIL_AI_NUMERICSERV) { michael@0: if (servname && parse_numeric_servname(servname)<0) michael@0: return EVUTIL_EAI_NONAME; michael@0: } michael@0: #endif michael@0: michael@0: /* Enough operating systems handle enough common non-resolve michael@0: * cases here weirdly enough that we are better off just michael@0: * overriding them. For example: michael@0: * michael@0: * - Windows doesn't like to infer the protocol from the michael@0: * socket type, or fill in socket or protocol types much at michael@0: * all. It also seems to do its own broken implicit michael@0: * always-on version of AI_ADDRCONFIG that keeps it from michael@0: * ever resolving even a literal IPv6 address when michael@0: * ai_addrtype is PF_UNSPEC. michael@0: */ michael@0: #ifdef WIN32 michael@0: { michael@0: int tmp_port; michael@0: err = evutil_getaddrinfo_common(nodename,servname,&hints, michael@0: res, &tmp_port); michael@0: if (err == 0 || michael@0: err == EVUTIL_EAI_MEMORY || michael@0: err == EVUTIL_EAI_NONAME) michael@0: return err; michael@0: /* If we make it here, the system getaddrinfo can michael@0: * have a crack at it. */ michael@0: } michael@0: #endif michael@0: michael@0: /* See documentation for need_numeric_port_hack above.*/ michael@0: need_np_hack = need_numeric_port_hack() && servname && !hints.ai_socktype michael@0: && ((portnum=parse_numeric_servname(servname)) >= 0); michael@0: if (need_np_hack) { michael@0: if (!nodename) michael@0: return evutil_getaddrinfo_common( michael@0: NULL,servname,&hints, res, &portnum); michael@0: servname = NULL; michael@0: } michael@0: michael@0: if (need_socktype_protocol_hack()) { michael@0: evutil_getaddrinfo_infer_protocols(&hints); michael@0: } michael@0: michael@0: /* Make sure that we didn't actually steal any AI_FLAGS values that michael@0: * the system is using. (This is a constant expression, and should ge michael@0: * optimized out.) michael@0: * michael@0: * XXXX Turn this into a compile-time failure rather than a run-time michael@0: * failure. michael@0: */ michael@0: EVUTIL_ASSERT((ALL_NONNATIVE_AI_FLAGS & ALL_NATIVE_AI_FLAGS) == 0); michael@0: michael@0: /* Clear any flags that only libevent understands. */ michael@0: hints.ai_flags &= ~ALL_NONNATIVE_AI_FLAGS; michael@0: michael@0: err = getaddrinfo(nodename, servname, &hints, res); michael@0: if (need_np_hack) michael@0: apply_numeric_port_hack(portnum, res); michael@0: michael@0: if (need_socktype_protocol_hack()) { michael@0: if (apply_socktype_protocol_hack(*res) < 0) { michael@0: evutil_freeaddrinfo(*res); michael@0: *res = NULL; michael@0: return EVUTIL_EAI_MEMORY; michael@0: } michael@0: } michael@0: return err; michael@0: #else michael@0: int port=0, err; michael@0: struct hostent *ent = NULL; michael@0: struct evutil_addrinfo hints; michael@0: michael@0: if (hints_in) { michael@0: memcpy(&hints, hints_in, sizeof(hints)); michael@0: } else { michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: } michael@0: michael@0: evutil_adjust_hints_for_addrconfig(&hints); michael@0: michael@0: err = evutil_getaddrinfo_common(nodename, servname, &hints, res, &port); michael@0: if (err != EVUTIL_EAI_NEED_RESOLVE) { michael@0: /* We either succeeded or failed. No need to continue */ michael@0: return err; michael@0: } michael@0: michael@0: err = 0; michael@0: /* Use any of the various gethostbyname_r variants as available. */ michael@0: { michael@0: #ifdef _EVENT_HAVE_GETHOSTBYNAME_R_6_ARG michael@0: /* This one is what glibc provides. */ michael@0: char buf[2048]; michael@0: struct hostent hostent; michael@0: int r; michael@0: r = gethostbyname_r(nodename, &hostent, buf, sizeof(buf), &ent, michael@0: &err); michael@0: #elif defined(_EVENT_HAVE_GETHOSTBYNAME_R_5_ARG) michael@0: char buf[2048]; michael@0: struct hostent hostent; michael@0: ent = gethostbyname_r(nodename, &hostent, buf, sizeof(buf), michael@0: &err); michael@0: #elif defined(_EVENT_HAVE_GETHOSTBYNAME_R_3_ARG) michael@0: struct hostent_data data; michael@0: struct hostent hostent; michael@0: memset(&data, 0, sizeof(data)); michael@0: err = gethostbyname_r(nodename, &hostent, &data); michael@0: ent = err ? NULL : &hostent; michael@0: #else michael@0: /* fall back to gethostbyname. */ michael@0: /* XXXX This needs a lock everywhere but Windows. */ michael@0: ent = gethostbyname(nodename); michael@0: #ifdef WIN32 michael@0: err = WSAGetLastError(); michael@0: #else michael@0: err = h_errno; michael@0: #endif michael@0: #endif michael@0: michael@0: /* Now we have either ent or err set. */ michael@0: if (!ent) { michael@0: /* XXX is this right for windows ? */ michael@0: switch (err) { michael@0: case TRY_AGAIN: michael@0: return EVUTIL_EAI_AGAIN; michael@0: case NO_RECOVERY: michael@0: default: michael@0: return EVUTIL_EAI_FAIL; michael@0: case HOST_NOT_FOUND: michael@0: return EVUTIL_EAI_NONAME; michael@0: case NO_ADDRESS: michael@0: #if NO_DATA != NO_ADDRESS michael@0: case NO_DATA: michael@0: #endif michael@0: return EVUTIL_EAI_NODATA; michael@0: } michael@0: } michael@0: michael@0: if (ent->h_addrtype != hints.ai_family && michael@0: hints.ai_family != PF_UNSPEC) { michael@0: /* This wasn't the type we were hoping for. Too bad michael@0: * we never had a chance to ask gethostbyname for what michael@0: * we wanted. */ michael@0: return EVUTIL_EAI_NONAME; michael@0: } michael@0: michael@0: /* Make sure we got _some_ answers. */ michael@0: if (ent->h_length == 0) michael@0: return EVUTIL_EAI_NODATA; michael@0: michael@0: /* If we got an address type we don't know how to make a michael@0: sockaddr for, give up. */ michael@0: if (ent->h_addrtype != PF_INET && ent->h_addrtype != PF_INET6) michael@0: return EVUTIL_EAI_FAMILY; michael@0: michael@0: *res = addrinfo_from_hostent(ent, port, &hints); michael@0: if (! *res) michael@0: return EVUTIL_EAI_MEMORY; michael@0: } michael@0: michael@0: return 0; michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: evutil_freeaddrinfo(struct evutil_addrinfo *ai) michael@0: { michael@0: #ifdef _EVENT_HAVE_GETADDRINFO michael@0: if (!(ai->ai_flags & EVUTIL_AI_LIBEVENT_ALLOCATED)) { michael@0: freeaddrinfo(ai); michael@0: return; michael@0: } michael@0: #endif michael@0: while (ai) { michael@0: struct evutil_addrinfo *next = ai->ai_next; michael@0: if (ai->ai_canonname) michael@0: mm_free(ai->ai_canonname); michael@0: mm_free(ai); michael@0: ai = next; michael@0: } michael@0: } michael@0: michael@0: static evdns_getaddrinfo_fn evdns_getaddrinfo_impl = NULL; michael@0: michael@0: void michael@0: evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn) michael@0: { michael@0: if (!evdns_getaddrinfo_impl) michael@0: evdns_getaddrinfo_impl = fn; michael@0: } michael@0: michael@0: /* Internal helper function: act like evdns_getaddrinfo if dns_base is set; michael@0: * otherwise do a blocking resolve and pass the result to the callback in the michael@0: * way that evdns_getaddrinfo would. michael@0: */ michael@0: int michael@0: evutil_getaddrinfo_async(struct evdns_base *dns_base, michael@0: const char *nodename, const char *servname, michael@0: const struct evutil_addrinfo *hints_in, michael@0: void (*cb)(int, struct evutil_addrinfo *, void *), void *arg) michael@0: { michael@0: if (dns_base && evdns_getaddrinfo_impl) { michael@0: evdns_getaddrinfo_impl( michael@0: dns_base, nodename, servname, hints_in, cb, arg); michael@0: } else { michael@0: struct evutil_addrinfo *ai=NULL; michael@0: int err; michael@0: err = evutil_getaddrinfo(nodename, servname, hints_in, &ai); michael@0: cb(err, ai, arg); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: const char * michael@0: evutil_gai_strerror(int err) michael@0: { michael@0: /* As a sneaky side-benefit, this case statement will get most michael@0: * compilers to tell us if any of the error codes we defined michael@0: * conflict with the platform's native error codes. */ michael@0: switch (err) { michael@0: case EVUTIL_EAI_CANCEL: michael@0: return "Request canceled"; michael@0: case 0: michael@0: return "No error"; michael@0: michael@0: case EVUTIL_EAI_ADDRFAMILY: michael@0: return "address family for nodename not supported"; michael@0: case EVUTIL_EAI_AGAIN: michael@0: return "temporary failure in name resolution"; michael@0: case EVUTIL_EAI_BADFLAGS: michael@0: return "invalid value for ai_flags"; michael@0: case EVUTIL_EAI_FAIL: michael@0: return "non-recoverable failure in name resolution"; michael@0: case EVUTIL_EAI_FAMILY: michael@0: return "ai_family not supported"; michael@0: case EVUTIL_EAI_MEMORY: michael@0: return "memory allocation failure"; michael@0: case EVUTIL_EAI_NODATA: michael@0: return "no address associated with nodename"; michael@0: case EVUTIL_EAI_NONAME: michael@0: return "nodename nor servname provided, or not known"; michael@0: case EVUTIL_EAI_SERVICE: michael@0: return "servname not supported for ai_socktype"; michael@0: case EVUTIL_EAI_SOCKTYPE: michael@0: return "ai_socktype not supported"; michael@0: case EVUTIL_EAI_SYSTEM: michael@0: return "system error"; michael@0: default: michael@0: #if defined(USE_NATIVE_GETADDRINFO) && defined(WIN32) michael@0: return gai_strerrorA(err); michael@0: #elif defined(USE_NATIVE_GETADDRINFO) michael@0: return gai_strerror(err); michael@0: #else michael@0: return "Unknown error code"; michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: #ifdef WIN32 michael@0: #define E(code, s) { code, (s " [" #code " ]") } michael@0: static struct { int code; const char *msg; } windows_socket_errors[] = { michael@0: E(WSAEINTR, "Interrupted function call"), michael@0: E(WSAEACCES, "Permission denied"), michael@0: E(WSAEFAULT, "Bad address"), michael@0: E(WSAEINVAL, "Invalid argument"), michael@0: E(WSAEMFILE, "Too many open files"), michael@0: E(WSAEWOULDBLOCK, "Resource temporarily unavailable"), michael@0: E(WSAEINPROGRESS, "Operation now in progress"), michael@0: E(WSAEALREADY, "Operation already in progress"), michael@0: E(WSAENOTSOCK, "Socket operation on nonsocket"), michael@0: E(WSAEDESTADDRREQ, "Destination address required"), michael@0: E(WSAEMSGSIZE, "Message too long"), michael@0: E(WSAEPROTOTYPE, "Protocol wrong for socket"), michael@0: E(WSAENOPROTOOPT, "Bad protocol option"), michael@0: E(WSAEPROTONOSUPPORT, "Protocol not supported"), michael@0: E(WSAESOCKTNOSUPPORT, "Socket type not supported"), michael@0: /* What's the difference between NOTSUPP and NOSUPPORT? :) */ michael@0: E(WSAEOPNOTSUPP, "Operation not supported"), michael@0: E(WSAEPFNOSUPPORT, "Protocol family not supported"), michael@0: E(WSAEAFNOSUPPORT, "Address family not supported by protocol family"), michael@0: E(WSAEADDRINUSE, "Address already in use"), michael@0: E(WSAEADDRNOTAVAIL, "Cannot assign requested address"), michael@0: E(WSAENETDOWN, "Network is down"), michael@0: E(WSAENETUNREACH, "Network is unreachable"), michael@0: E(WSAENETRESET, "Network dropped connection on reset"), michael@0: E(WSAECONNABORTED, "Software caused connection abort"), michael@0: E(WSAECONNRESET, "Connection reset by peer"), michael@0: E(WSAENOBUFS, "No buffer space available"), michael@0: E(WSAEISCONN, "Socket is already connected"), michael@0: E(WSAENOTCONN, "Socket is not connected"), michael@0: E(WSAESHUTDOWN, "Cannot send after socket shutdown"), michael@0: E(WSAETIMEDOUT, "Connection timed out"), michael@0: E(WSAECONNREFUSED, "Connection refused"), michael@0: E(WSAEHOSTDOWN, "Host is down"), michael@0: E(WSAEHOSTUNREACH, "No route to host"), michael@0: E(WSAEPROCLIM, "Too many processes"), michael@0: michael@0: /* Yes, some of these start with WSA, not WSAE. No, I don't know why. */ michael@0: E(WSASYSNOTREADY, "Network subsystem is unavailable"), michael@0: E(WSAVERNOTSUPPORTED, "Winsock.dll out of range"), michael@0: E(WSANOTINITIALISED, "Successful WSAStartup not yet performed"), michael@0: E(WSAEDISCON, "Graceful shutdown now in progress"), michael@0: #ifdef WSATYPE_NOT_FOUND michael@0: E(WSATYPE_NOT_FOUND, "Class type not found"), michael@0: #endif michael@0: E(WSAHOST_NOT_FOUND, "Host not found"), michael@0: E(WSATRY_AGAIN, "Nonauthoritative host not found"), michael@0: E(WSANO_RECOVERY, "This is a nonrecoverable error"), michael@0: E(WSANO_DATA, "Valid name, no data record of requested type)"), michael@0: michael@0: /* There are some more error codes whose numeric values are marked michael@0: * OS dependent. They start with WSA_, apparently for the same michael@0: * reason that practitioners of some craft traditions deliberately michael@0: * introduce imperfections into their baskets and rugs "to allow the michael@0: * evil spirits to escape." If we catch them, then our binaries michael@0: * might not report consistent results across versions of Windows. michael@0: * Thus, I'm going to let them all fall through. michael@0: */ michael@0: { -1, NULL }, michael@0: }; michael@0: #undef E michael@0: /** Equivalent to strerror, but for windows socket errors. */ michael@0: const char * michael@0: evutil_socket_error_to_string(int errcode) michael@0: { michael@0: /* XXXX Is there really no built-in function to do this? */ michael@0: int i; michael@0: for (i=0; windows_socket_errors[i].code >= 0; ++i) { michael@0: if (errcode == windows_socket_errors[i].code) michael@0: return windows_socket_errors[i].msg; michael@0: } michael@0: return strerror(errcode); michael@0: } michael@0: #endif michael@0: michael@0: int michael@0: evutil_snprintf(char *buf, size_t buflen, const char *format, ...) michael@0: { michael@0: int r; michael@0: va_list ap; michael@0: va_start(ap, format); michael@0: r = evutil_vsnprintf(buf, buflen, format, ap); michael@0: va_end(ap); michael@0: return r; michael@0: } michael@0: michael@0: int michael@0: evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap) michael@0: { michael@0: int r; michael@0: if (!buflen) michael@0: return 0; michael@0: #ifdef _MSC_VER michael@0: r = _vsnprintf(buf, buflen, format, ap); michael@0: if (r < 0) michael@0: r = _vscprintf(format, ap); michael@0: #elif defined(sgi) michael@0: /* Make sure we always use the correct vsnprintf on IRIX */ michael@0: extern int _xpg5_vsnprintf(char * __restrict, michael@0: __SGI_LIBC_NAMESPACE_QUALIFIER size_t, michael@0: const char * __restrict, /* va_list */ char *); michael@0: michael@0: r = _xpg5_vsnprintf(buf, buflen, format, ap); michael@0: #else michael@0: r = vsnprintf(buf, buflen, format, ap); michael@0: #endif michael@0: buf[buflen-1] = '\0'; michael@0: return r; michael@0: } michael@0: michael@0: #define USE_INTERNAL_NTOP michael@0: #define USE_INTERNAL_PTON michael@0: michael@0: const char * michael@0: evutil_inet_ntop(int af, const void *src, char *dst, size_t len) michael@0: { michael@0: #if defined(_EVENT_HAVE_INET_NTOP) && !defined(USE_INTERNAL_NTOP) michael@0: return inet_ntop(af, src, dst, len); michael@0: #else michael@0: if (af == AF_INET) { michael@0: const struct in_addr *in = src; michael@0: const ev_uint32_t a = ntohl(in->s_addr); michael@0: int r; michael@0: r = evutil_snprintf(dst, len, "%d.%d.%d.%d", michael@0: (int)(ev_uint8_t)((a>>24)&0xff), michael@0: (int)(ev_uint8_t)((a>>16)&0xff), michael@0: (int)(ev_uint8_t)((a>>8 )&0xff), michael@0: (int)(ev_uint8_t)((a )&0xff)); michael@0: if (r<0||(size_t)r>=len) michael@0: return NULL; michael@0: else michael@0: return dst; michael@0: #ifdef AF_INET6 michael@0: } else if (af == AF_INET6) { michael@0: const struct in6_addr *addr = src; michael@0: char buf[64], *cp; michael@0: int longestGapLen = 0, longestGapPos = -1, i, michael@0: curGapPos = -1, curGapLen = 0; michael@0: ev_uint16_t words[8]; michael@0: for (i = 0; i < 8; ++i) { michael@0: words[i] = michael@0: (((ev_uint16_t)addr->s6_addr[2*i])<<8) + addr->s6_addr[2*i+1]; michael@0: } michael@0: if (words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && michael@0: words[4] == 0 && ((words[5] == 0 && words[6] && words[7]) || michael@0: (words[5] == 0xffff))) { michael@0: /* This is an IPv4 address. */ michael@0: if (words[5] == 0) { michael@0: evutil_snprintf(buf, sizeof(buf), "::%d.%d.%d.%d", michael@0: addr->s6_addr[12], addr->s6_addr[13], michael@0: addr->s6_addr[14], addr->s6_addr[15]); michael@0: } else { michael@0: evutil_snprintf(buf, sizeof(buf), "::%x:%d.%d.%d.%d", words[5], michael@0: addr->s6_addr[12], addr->s6_addr[13], michael@0: addr->s6_addr[14], addr->s6_addr[15]); michael@0: } michael@0: if (strlen(buf) > len) michael@0: return NULL; michael@0: strlcpy(dst, buf, len); michael@0: return dst; michael@0: } michael@0: i = 0; michael@0: while (i < 8) { michael@0: if (words[i] == 0) { michael@0: curGapPos = i++; michael@0: curGapLen = 1; michael@0: while (i<8 && words[i] == 0) { michael@0: ++i; ++curGapLen; michael@0: } michael@0: if (curGapLen > longestGapLen) { michael@0: longestGapPos = curGapPos; michael@0: longestGapLen = curGapLen; michael@0: } michael@0: } else { michael@0: ++i; michael@0: } michael@0: } michael@0: if (longestGapLen<=1) michael@0: longestGapPos = -1; michael@0: michael@0: cp = buf; michael@0: for (i = 0; i < 8; ++i) { michael@0: if (words[i] == 0 && longestGapPos == i) { michael@0: if (i == 0) michael@0: *cp++ = ':'; michael@0: *cp++ = ':'; michael@0: while (i < 8 && words[i] == 0) michael@0: ++i; michael@0: --i; /* to compensate for loop increment. */ michael@0: } else { michael@0: evutil_snprintf(cp, michael@0: sizeof(buf)-(cp-buf), "%x", (unsigned)words[i]); michael@0: cp += strlen(cp); michael@0: if (i != 7) michael@0: *cp++ = ':'; michael@0: } michael@0: } michael@0: *cp = '\0'; michael@0: if (strlen(buf) > len) michael@0: return NULL; michael@0: strlcpy(dst, buf, len); michael@0: return dst; michael@0: #endif michael@0: } else { michael@0: return NULL; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: evutil_inet_pton(int af, const char *src, void *dst) michael@0: { michael@0: #if defined(_EVENT_HAVE_INET_PTON) && !defined(USE_INTERNAL_PTON) michael@0: return inet_pton(af, src, dst); michael@0: #else michael@0: if (af == AF_INET) { michael@0: int a,b,c,d; michael@0: char more; michael@0: struct in_addr *addr = dst; michael@0: if (sscanf(src, "%d.%d.%d.%d%c", &a,&b,&c,&d,&more) != 4) michael@0: return 0; michael@0: if (a < 0 || a > 255) return 0; michael@0: if (b < 0 || b > 255) return 0; michael@0: if (c < 0 || c > 255) return 0; michael@0: if (d < 0 || d > 255) return 0; michael@0: addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d); michael@0: return 1; michael@0: #ifdef AF_INET6 michael@0: } else if (af == AF_INET6) { michael@0: struct in6_addr *out = dst; michael@0: ev_uint16_t words[8]; michael@0: int gapPos = -1, i, setWords=0; michael@0: const char *dot = strchr(src, '.'); michael@0: const char *eow; /* end of words. */ michael@0: if (dot == src) michael@0: return 0; michael@0: else if (!dot) michael@0: eow = src+strlen(src); michael@0: else { michael@0: int byte1,byte2,byte3,byte4; michael@0: char more; michael@0: for (eow = dot-1; eow >= src && EVUTIL_ISDIGIT(*eow); --eow) michael@0: ; michael@0: ++eow; michael@0: michael@0: /* We use "scanf" because some platform inet_aton()s are too lax michael@0: * about IPv4 addresses of the form "1.2.3" */ michael@0: if (sscanf(eow, "%d.%d.%d.%d%c", michael@0: &byte1,&byte2,&byte3,&byte4,&more) != 4) michael@0: return 0; michael@0: michael@0: if (byte1 > 255 || byte1 < 0 || michael@0: byte2 > 255 || byte2 < 0 || michael@0: byte3 > 255 || byte3 < 0 || michael@0: byte4 > 255 || byte4 < 0) michael@0: return 0; michael@0: michael@0: words[6] = (byte1<<8) | byte2; michael@0: words[7] = (byte3<<8) | byte4; michael@0: setWords += 2; michael@0: } michael@0: michael@0: i = 0; michael@0: while (src < eow) { michael@0: if (i > 7) michael@0: return 0; michael@0: if (EVUTIL_ISXDIGIT(*src)) { michael@0: char *next; michael@0: long r = strtol(src, &next, 16); michael@0: if (next > 4+src) michael@0: return 0; michael@0: if (next == src) michael@0: return 0; michael@0: if (r<0 || r>65536) michael@0: return 0; michael@0: michael@0: words[i++] = (ev_uint16_t)r; michael@0: setWords++; michael@0: src = next; michael@0: if (*src != ':' && src != eow) michael@0: return 0; michael@0: ++src; michael@0: } else if (*src == ':' && i > 0 && gapPos==-1) { michael@0: gapPos = i; michael@0: ++src; michael@0: } else if (*src == ':' && i == 0 && src[1] == ':' && gapPos==-1) { michael@0: gapPos = i; michael@0: src += 2; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: if (setWords > 8 || michael@0: (setWords == 8 && gapPos != -1) || michael@0: (setWords < 8 && gapPos == -1)) michael@0: return 0; michael@0: michael@0: if (gapPos >= 0) { michael@0: int nToMove = setWords - (dot ? 2 : 0) - gapPos; michael@0: int gapLen = 8 - setWords; michael@0: /* assert(nToMove >= 0); */ michael@0: if (nToMove < 0) michael@0: return -1; /* should be impossible */ michael@0: memmove(&words[gapPos+gapLen], &words[gapPos], michael@0: sizeof(ev_uint16_t)*nToMove); michael@0: memset(&words[gapPos], 0, sizeof(ev_uint16_t)*gapLen); michael@0: } michael@0: for (i = 0; i < 8; ++i) { michael@0: out->s6_addr[2*i ] = words[i] >> 8; michael@0: out->s6_addr[2*i+1] = words[i] & 0xff; michael@0: } michael@0: michael@0: return 1; michael@0: #endif michael@0: } else { michael@0: return -1; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: evutil_parse_sockaddr_port(const char *ip_as_string, struct sockaddr *out, int *outlen) michael@0: { michael@0: int port; michael@0: char buf[128]; michael@0: const char *cp, *addr_part, *port_part; michael@0: int is_ipv6; michael@0: /* recognized formats are: michael@0: * [ipv6]:port michael@0: * ipv6 michael@0: * [ipv6] michael@0: * ipv4:port michael@0: * ipv4 michael@0: */ michael@0: michael@0: cp = strchr(ip_as_string, ':'); michael@0: if (*ip_as_string == '[') { michael@0: int len; michael@0: if (!(cp = strchr(ip_as_string, ']'))) { michael@0: return -1; michael@0: } michael@0: len = (int) ( cp-(ip_as_string + 1) ); michael@0: if (len > (int)sizeof(buf)-1) { michael@0: return -1; michael@0: } michael@0: memcpy(buf, ip_as_string+1, len); michael@0: buf[len] = '\0'; michael@0: addr_part = buf; michael@0: if (cp[1] == ':') michael@0: port_part = cp+2; michael@0: else michael@0: port_part = NULL; michael@0: is_ipv6 = 1; michael@0: } else if (cp && strchr(cp+1, ':')) { michael@0: is_ipv6 = 1; michael@0: addr_part = ip_as_string; michael@0: port_part = NULL; michael@0: } else if (cp) { michael@0: is_ipv6 = 0; michael@0: if (cp - ip_as_string > (int)sizeof(buf)-1) { michael@0: return -1; michael@0: } michael@0: memcpy(buf, ip_as_string, cp-ip_as_string); michael@0: buf[cp-ip_as_string] = '\0'; michael@0: addr_part = buf; michael@0: port_part = cp+1; michael@0: } else { michael@0: addr_part = ip_as_string; michael@0: port_part = NULL; michael@0: is_ipv6 = 0; michael@0: } michael@0: michael@0: if (port_part == NULL) { michael@0: port = 0; michael@0: } else { michael@0: port = atoi(port_part); michael@0: if (port <= 0 || port > 65535) { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: if (!addr_part) michael@0: return -1; /* Should be impossible. */ michael@0: #ifdef AF_INET6 michael@0: if (is_ipv6) michael@0: { michael@0: struct sockaddr_in6 sin6; michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN michael@0: sin6.sin6_len = sizeof(sin6); michael@0: #endif michael@0: sin6.sin6_family = AF_INET6; michael@0: sin6.sin6_port = htons(port); michael@0: if (1 != evutil_inet_pton(AF_INET6, addr_part, &sin6.sin6_addr)) michael@0: return -1; michael@0: if ((int)sizeof(sin6) > *outlen) michael@0: return -1; michael@0: memset(out, 0, *outlen); michael@0: memcpy(out, &sin6, sizeof(sin6)); michael@0: *outlen = sizeof(sin6); michael@0: return 0; michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: struct sockaddr_in sin; michael@0: memset(&sin, 0, sizeof(sin)); michael@0: #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN michael@0: sin.sin_len = sizeof(sin); michael@0: #endif michael@0: sin.sin_family = AF_INET; michael@0: sin.sin_port = htons(port); michael@0: if (1 != evutil_inet_pton(AF_INET, addr_part, &sin.sin_addr)) michael@0: return -1; michael@0: if ((int)sizeof(sin) > *outlen) michael@0: return -1; michael@0: memset(out, 0, *outlen); michael@0: memcpy(out, &sin, sizeof(sin)); michael@0: *outlen = sizeof(sin); michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: const char * michael@0: evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen) michael@0: { michael@0: char b[128]; michael@0: const char *res=NULL; michael@0: int port; michael@0: if (sa->sa_family == AF_INET) { michael@0: const struct sockaddr_in *sin = (const struct sockaddr_in*)sa; michael@0: res = evutil_inet_ntop(AF_INET, &sin->sin_addr,b,sizeof(b)); michael@0: port = ntohs(sin->sin_port); michael@0: if (res) { michael@0: evutil_snprintf(out, outlen, "%s:%d", b, port); michael@0: return out; michael@0: } michael@0: } else if (sa->sa_family == AF_INET6) { michael@0: const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6*)sa; michael@0: res = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr,b,sizeof(b)); michael@0: port = ntohs(sin6->sin6_port); michael@0: if (res) { michael@0: evutil_snprintf(out, outlen, "[%s]:%d", b, port); michael@0: return out; michael@0: } michael@0: } michael@0: michael@0: evutil_snprintf(out, outlen, "", michael@0: (int)sa->sa_family); michael@0: return out; michael@0: } michael@0: michael@0: int michael@0: evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2, michael@0: int include_port) michael@0: { michael@0: int r; michael@0: if (0 != (r = (sa1->sa_family - sa2->sa_family))) michael@0: return r; michael@0: michael@0: if (sa1->sa_family == AF_INET) { michael@0: const struct sockaddr_in *sin1, *sin2; michael@0: sin1 = (const struct sockaddr_in *)sa1; michael@0: sin2 = (const struct sockaddr_in *)sa2; michael@0: if (sin1->sin_addr.s_addr < sin2->sin_addr.s_addr) michael@0: return -1; michael@0: else if (sin1->sin_addr.s_addr > sin2->sin_addr.s_addr) michael@0: return 1; michael@0: else if (include_port && michael@0: (r = ((int)sin1->sin_port - (int)sin2->sin_port))) michael@0: return r; michael@0: else michael@0: return 0; michael@0: } michael@0: #ifdef AF_INET6 michael@0: else if (sa1->sa_family == AF_INET6) { michael@0: const struct sockaddr_in6 *sin1, *sin2; michael@0: sin1 = (const struct sockaddr_in6 *)sa1; michael@0: sin2 = (const struct sockaddr_in6 *)sa2; michael@0: if ((r = memcmp(sin1->sin6_addr.s6_addr, sin2->sin6_addr.s6_addr, 16))) michael@0: return r; michael@0: else if (include_port && michael@0: (r = ((int)sin1->sin6_port - (int)sin2->sin6_port))) michael@0: return r; michael@0: else michael@0: return 0; michael@0: } michael@0: #endif michael@0: return 1; michael@0: } michael@0: michael@0: /* Tables to implement ctypes-replacement EVUTIL_IS*() functions. Each table michael@0: * has 256 bits to look up whether a character is in some set or not. This michael@0: * fails on non-ASCII platforms, but so does every other place where we michael@0: * take a char and write it onto the network. michael@0: **/ michael@0: static const ev_uint32_t EVUTIL_ISALPHA_TABLE[8] = michael@0: { 0, 0, 0x7fffffe, 0x7fffffe, 0, 0, 0, 0 }; michael@0: static const ev_uint32_t EVUTIL_ISALNUM_TABLE[8] = michael@0: { 0, 0x3ff0000, 0x7fffffe, 0x7fffffe, 0, 0, 0, 0 }; michael@0: static const ev_uint32_t EVUTIL_ISSPACE_TABLE[8] = { 0x3e00, 0x1, 0, 0, 0, 0, 0, 0 }; michael@0: static const ev_uint32_t EVUTIL_ISXDIGIT_TABLE[8] = michael@0: { 0, 0x3ff0000, 0x7e, 0x7e, 0, 0, 0, 0 }; michael@0: static const ev_uint32_t EVUTIL_ISDIGIT_TABLE[8] = { 0, 0x3ff0000, 0, 0, 0, 0, 0, 0 }; michael@0: static const ev_uint32_t EVUTIL_ISPRINT_TABLE[8] = michael@0: { 0, 0xffffffff, 0xffffffff, 0x7fffffff, 0, 0, 0, 0x0 }; michael@0: static const ev_uint32_t EVUTIL_ISUPPER_TABLE[8] = { 0, 0, 0x7fffffe, 0, 0, 0, 0, 0 }; michael@0: static const ev_uint32_t EVUTIL_ISLOWER_TABLE[8] = { 0, 0, 0, 0x7fffffe, 0, 0, 0, 0 }; michael@0: /* Upper-casing and lowercasing tables to map characters to upper/lowercase michael@0: * equivalents. */ michael@0: static const unsigned char EVUTIL_TOUPPER_TABLE[256] = { michael@0: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, michael@0: 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, michael@0: 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, michael@0: 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63, michael@0: 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, michael@0: 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95, michael@0: 96,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, michael@0: 80,81,82,83,84,85,86,87,88,89,90,123,124,125,126,127, michael@0: 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, michael@0: 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, michael@0: 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, michael@0: 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, michael@0: 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, michael@0: 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, michael@0: 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, michael@0: 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, michael@0: }; michael@0: static const unsigned char EVUTIL_TOLOWER_TABLE[256] = { michael@0: 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, michael@0: 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, michael@0: 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, michael@0: 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63, michael@0: 64,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111, michael@0: 112,113,114,115,116,117,118,119,120,121,122,91,92,93,94,95, michael@0: 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111, michael@0: 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, michael@0: 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, michael@0: 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, michael@0: 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, michael@0: 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, michael@0: 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, michael@0: 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, michael@0: 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, michael@0: 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, michael@0: }; michael@0: michael@0: #define IMPL_CTYPE_FN(name) \ michael@0: int EVUTIL_##name(char c) { \ michael@0: ev_uint8_t u = c; \ michael@0: return !!(EVUTIL_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \ michael@0: } michael@0: IMPL_CTYPE_FN(ISALPHA) michael@0: IMPL_CTYPE_FN(ISALNUM) michael@0: IMPL_CTYPE_FN(ISSPACE) michael@0: IMPL_CTYPE_FN(ISDIGIT) michael@0: IMPL_CTYPE_FN(ISXDIGIT) michael@0: IMPL_CTYPE_FN(ISPRINT) michael@0: IMPL_CTYPE_FN(ISLOWER) michael@0: IMPL_CTYPE_FN(ISUPPER) michael@0: michael@0: char EVUTIL_TOLOWER(char c) michael@0: { michael@0: return ((char)EVUTIL_TOLOWER_TABLE[(ev_uint8_t)c]); michael@0: } michael@0: char EVUTIL_TOUPPER(char c) michael@0: { michael@0: return ((char)EVUTIL_TOUPPER_TABLE[(ev_uint8_t)c]); michael@0: } michael@0: int michael@0: evutil_ascii_strcasecmp(const char *s1, const char *s2) michael@0: { michael@0: char c1, c2; michael@0: while (1) { michael@0: c1 = EVUTIL_TOLOWER(*s1++); michael@0: c2 = EVUTIL_TOLOWER(*s2++); michael@0: if (c1 < c2) michael@0: return -1; michael@0: else if (c1 > c2) michael@0: return 1; michael@0: else if (c1 == 0) michael@0: return 0; michael@0: } michael@0: } michael@0: int evutil_ascii_strncasecmp(const char *s1, const char *s2, size_t n) michael@0: { michael@0: char c1, c2; michael@0: while (n--) { michael@0: c1 = EVUTIL_TOLOWER(*s1++); michael@0: c2 = EVUTIL_TOLOWER(*s2++); michael@0: if (c1 < c2) michael@0: return -1; michael@0: else if (c1 > c2) michael@0: return 1; michael@0: else if (c1 == 0) michael@0: return 0; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static int michael@0: evutil_issetugid(void) michael@0: { michael@0: #ifdef _EVENT_HAVE_ISSETUGID michael@0: return issetugid(); michael@0: #else michael@0: michael@0: #ifdef _EVENT_HAVE_GETEUID michael@0: if (getuid() != geteuid()) michael@0: return 1; michael@0: #endif michael@0: #ifdef _EVENT_HAVE_GETEGID michael@0: if (getgid() != getegid()) michael@0: return 1; michael@0: #endif michael@0: return 0; michael@0: #endif michael@0: } michael@0: michael@0: const char * michael@0: evutil_getenv(const char *varname) michael@0: { michael@0: if (evutil_issetugid()) michael@0: return NULL; michael@0: michael@0: return getenv(varname); michael@0: } michael@0: michael@0: long michael@0: _evutil_weakrand(void) michael@0: { michael@0: #ifdef WIN32 michael@0: return rand(); michael@0: #else michael@0: return random(); michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: evutil_sockaddr_is_loopback(const struct sockaddr *addr) michael@0: { michael@0: static const char LOOPBACK_S6[16] = michael@0: "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"; michael@0: if (addr->sa_family == AF_INET) { michael@0: struct sockaddr_in *sin = (struct sockaddr_in *)addr; michael@0: return (ntohl(sin->sin_addr.s_addr) & 0xff000000) == 0x7f000000; michael@0: } else if (addr->sa_family == AF_INET6) { michael@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; michael@0: return !memcmp(sin6->sin6_addr.s6_addr, LOOPBACK_S6, 16); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: #define MAX_SECONDS_IN_MSEC_LONG \ michael@0: (((LONG_MAX) - 999) / 1000) michael@0: michael@0: long michael@0: evutil_tv_to_msec(const struct timeval *tv) michael@0: { michael@0: if (tv->tv_usec > 1000000 || tv->tv_sec > MAX_SECONDS_IN_MSEC_LONG) michael@0: return -1; michael@0: michael@0: return (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000); michael@0: } michael@0: michael@0: int michael@0: evutil_hex_char_to_int(char c) michael@0: { michael@0: switch(c) michael@0: { michael@0: case '0': return 0; michael@0: case '1': return 1; michael@0: case '2': return 2; michael@0: case '3': return 3; michael@0: case '4': return 4; michael@0: case '5': return 5; michael@0: case '6': return 6; michael@0: case '7': return 7; michael@0: case '8': return 8; michael@0: case '9': return 9; michael@0: case 'A': case 'a': return 10; michael@0: case 'B': case 'b': return 11; michael@0: case 'C': case 'c': return 12; michael@0: case 'D': case 'd': return 13; michael@0: case 'E': case 'e': return 14; michael@0: case 'F': case 'f': return 15; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: #ifdef WIN32 michael@0: HANDLE michael@0: evutil_load_windows_system_library(const TCHAR *library_name) michael@0: { michael@0: TCHAR path[MAX_PATH]; michael@0: unsigned n; michael@0: n = GetSystemDirectory(path, MAX_PATH); michael@0: if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH) michael@0: return 0; michael@0: _tcscat(path, TEXT("\\")); michael@0: _tcscat(path, library_name); michael@0: return LoadLibrary(path); michael@0: } michael@0: #endif michael@0: