1 1.7 christos /* $NetBSD: evutil.c,v 1.7 2021/04/07 03:36:48 christos Exp $ */ 2 1.7 christos 3 1.1 plunky /* 4 1.2 christos * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 5 1.1 plunky * 6 1.1 plunky * Redistribution and use in source and binary forms, with or without 7 1.1 plunky * modification, are permitted provided that the following conditions 8 1.1 plunky * are met: 9 1.1 plunky * 1. Redistributions of source code must retain the above copyright 10 1.1 plunky * notice, this list of conditions and the following disclaimer. 11 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 plunky * notice, this list of conditions and the following disclaimer in the 13 1.1 plunky * documentation and/or other materials provided with the distribution. 14 1.1 plunky * 3. The name of the author may not be used to endorse or promote products 15 1.1 plunky * derived from this software without specific prior written permission. 16 1.1 plunky * 17 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 1.1 plunky */ 28 1.2 christos 29 1.2 christos #include "event2/event-config.h" 30 1.2 christos #include <sys/cdefs.h> 31 1.7 christos __RCSID("$NetBSD: evutil.c,v 1.7 2021/04/07 03:36:48 christos Exp $"); 32 1.6 christos #include "evconfig-private.h" 33 1.2 christos 34 1.6 christos #ifdef _WIN32 35 1.1 plunky #include <winsock2.h> 36 1.7 christos #include <winerror.h> 37 1.2 christos #include <ws2tcpip.h> 38 1.1 plunky #define WIN32_LEAN_AND_MEAN 39 1.1 plunky #include <windows.h> 40 1.1 plunky #undef WIN32_LEAN_AND_MEAN 41 1.2 christos #include <io.h> 42 1.2 christos #include <tchar.h> 43 1.6 christos #include <process.h> 44 1.6 christos #undef _WIN32_WINNT 45 1.6 christos /* For structs needed by GetAdaptersAddresses */ 46 1.6 christos #define _WIN32_WINNT 0x0501 47 1.6 christos #include <iphlpapi.h> 48 1.7 christos #include <netioapi.h> 49 1.1 plunky #endif 50 1.1 plunky 51 1.1 plunky #include <sys/types.h> 52 1.6 christos #ifdef EVENT__HAVE_SYS_SOCKET_H 53 1.1 plunky #include <sys/socket.h> 54 1.1 plunky #endif 55 1.6 christos #ifdef EVENT__HAVE_UNISTD_H 56 1.1 plunky #include <unistd.h> 57 1.1 plunky #endif 58 1.6 christos #ifdef EVENT__HAVE_FCNTL_H 59 1.1 plunky #include <fcntl.h> 60 1.1 plunky #endif 61 1.6 christos #ifdef EVENT__HAVE_STDLIB_H 62 1.1 plunky #include <stdlib.h> 63 1.1 plunky #endif 64 1.1 plunky #include <errno.h> 65 1.2 christos #include <limits.h> 66 1.2 christos #include <stdio.h> 67 1.2 christos #include <string.h> 68 1.6 christos #ifdef EVENT__HAVE_NETINET_IN_H 69 1.2 christos #include <netinet/in.h> 70 1.2 christos #endif 71 1.6 christos #ifdef EVENT__HAVE_NETINET_IN6_H 72 1.2 christos #include <netinet/in6.h> 73 1.2 christos #endif 74 1.6 christos #ifdef EVENT__HAVE_NETINET_TCP_H 75 1.6 christos #include <netinet/tcp.h> 76 1.6 christos #endif 77 1.6 christos #ifdef EVENT__HAVE_ARPA_INET_H 78 1.2 christos #include <arpa/inet.h> 79 1.2 christos #endif 80 1.2 christos #include <time.h> 81 1.6 christos #include <sys/stat.h> 82 1.7 christos #ifndef _WIN32 83 1.7 christos #include <net/if.h> 84 1.7 christos #endif 85 1.6 christos #ifdef EVENT__HAVE_IFADDRS_H 86 1.6 christos #include <ifaddrs.h> 87 1.2 christos #endif 88 1.2 christos 89 1.2 christos #include "event2/util.h" 90 1.2 christos #include "util-internal.h" 91 1.2 christos #include "log-internal.h" 92 1.2 christos #include "mm-internal.h" 93 1.6 christos #include "evthread-internal.h" 94 1.2 christos 95 1.2 christos #include "strlcpy-internal.h" 96 1.2 christos #include "ipv6-internal.h" 97 1.2 christos 98 1.6 christos #ifdef _WIN32 99 1.6 christos #define HT_NO_CACHE_HASH_VALUES 100 1.6 christos #include "ht-internal.h" 101 1.2 christos #define open _open 102 1.2 christos #define read _read 103 1.2 christos #define close _close 104 1.6 christos #ifndef fstat 105 1.2 christos #define fstat _fstati64 106 1.6 christos #endif 107 1.6 christos #ifndef stat 108 1.2 christos #define stat _stati64 109 1.6 christos #endif 110 1.2 christos #define mode_t int 111 1.1 plunky #endif 112 1.1 plunky 113 1.2 christos int 114 1.6 christos evutil_open_closeonexec_(const char *pathname, int flags, unsigned mode) 115 1.2 christos { 116 1.2 christos int fd; 117 1.2 christos 118 1.2 christos #ifdef O_CLOEXEC 119 1.6 christos fd = open(pathname, flags|O_CLOEXEC, (mode_t)mode); 120 1.6 christos if (fd >= 0 || errno == EINVAL) 121 1.6 christos return fd; 122 1.6 christos /* If we got an EINVAL, fall through and try without O_CLOEXEC */ 123 1.2 christos #endif 124 1.6 christos fd = open(pathname, flags, (mode_t)mode); 125 1.2 christos if (fd < 0) 126 1.2 christos return -1; 127 1.2 christos 128 1.6 christos #if defined(FD_CLOEXEC) 129 1.6 christos if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { 130 1.6 christos close(fd); 131 1.2 christos return -1; 132 1.6 christos } 133 1.2 christos #endif 134 1.2 christos 135 1.2 christos return fd; 136 1.2 christos } 137 1.2 christos 138 1.2 christos /** 139 1.2 christos Read the contents of 'filename' into a newly allocated NUL-terminated 140 1.2 christos string. Set *content_out to hold this string, and *len_out to hold its 141 1.2 christos length (not including the appended NUL). If 'is_binary', open the file in 142 1.2 christos binary mode. 143 1.2 christos 144 1.2 christos Returns 0 on success, -1 if the open fails, and -2 for all other failures. 145 1.2 christos 146 1.2 christos Used internally only; may go away in a future version. 147 1.2 christos */ 148 1.2 christos int 149 1.6 christos evutil_read_file_(const char *filename, char **content_out, size_t *len_out, 150 1.2 christos int is_binary) 151 1.2 christos { 152 1.2 christos int fd, r; 153 1.2 christos struct stat st; 154 1.2 christos char *mem; 155 1.2 christos size_t read_so_far=0; 156 1.2 christos int mode = O_RDONLY; 157 1.2 christos 158 1.2 christos EVUTIL_ASSERT(content_out); 159 1.2 christos EVUTIL_ASSERT(len_out); 160 1.2 christos *content_out = NULL; 161 1.2 christos *len_out = 0; 162 1.2 christos 163 1.2 christos #ifdef O_BINARY 164 1.2 christos if (is_binary) 165 1.2 christos mode |= O_BINARY; 166 1.2 christos #endif 167 1.2 christos 168 1.6 christos fd = evutil_open_closeonexec_(filename, mode, 0); 169 1.2 christos if (fd < 0) 170 1.2 christos return -1; 171 1.2 christos if (fstat(fd, &st) || st.st_size < 0 || 172 1.2 christos st.st_size > EV_SSIZE_MAX-1 ) { 173 1.2 christos close(fd); 174 1.2 christos return -2; 175 1.2 christos } 176 1.2 christos mem = mm_malloc((size_t)st.st_size + 1); 177 1.2 christos if (!mem) { 178 1.2 christos close(fd); 179 1.2 christos return -2; 180 1.2 christos } 181 1.2 christos read_so_far = 0; 182 1.6 christos #ifdef _WIN32 183 1.2 christos #define N_TO_READ(x) ((x) > INT_MAX) ? INT_MAX : ((int)(x)) 184 1.2 christos #else 185 1.2 christos #define N_TO_READ(x) (x) 186 1.2 christos #endif 187 1.2 christos while ((r = read(fd, mem+read_so_far, N_TO_READ(st.st_size - read_so_far))) > 0) { 188 1.2 christos read_so_far += r; 189 1.2 christos if (read_so_far >= (size_t)st.st_size) 190 1.2 christos break; 191 1.2 christos EVUTIL_ASSERT(read_so_far < (size_t)st.st_size); 192 1.2 christos } 193 1.2 christos close(fd); 194 1.2 christos if (r < 0) { 195 1.2 christos mm_free(mem); 196 1.2 christos return -2; 197 1.2 christos } 198 1.2 christos mem[read_so_far] = 0; 199 1.2 christos 200 1.2 christos *len_out = read_so_far; 201 1.2 christos *content_out = mem; 202 1.2 christos return 0; 203 1.2 christos } 204 1.1 plunky 205 1.1 plunky int 206 1.2 christos evutil_socketpair(int family, int type, int protocol, evutil_socket_t fd[2]) 207 1.1 plunky { 208 1.6 christos #ifndef _WIN32 209 1.1 plunky return socketpair(family, type, protocol, fd); 210 1.1 plunky #else 211 1.6 christos return evutil_ersatz_socketpair_(family, type, protocol, fd); 212 1.2 christos #endif 213 1.2 christos } 214 1.2 christos 215 1.2 christos int 216 1.6 christos evutil_ersatz_socketpair_(int family, int type, int protocol, 217 1.2 christos evutil_socket_t fd[2]) 218 1.2 christos { 219 1.1 plunky /* This code is originally from Tor. Used with permission. */ 220 1.1 plunky 221 1.1 plunky /* This socketpair does not work when localhost is down. So 222 1.1 plunky * it's really not the same thing at all. But it's close enough 223 1.1 plunky * for now, and really, when localhost is down sometimes, we 224 1.1 plunky * have other problems too. 225 1.1 plunky */ 226 1.6 christos #ifdef _WIN32 227 1.2 christos #define ERR(e) WSA##e 228 1.2 christos #else 229 1.2 christos #define ERR(e) e 230 1.2 christos #endif 231 1.2 christos evutil_socket_t listener = -1; 232 1.2 christos evutil_socket_t connector = -1; 233 1.2 christos evutil_socket_t acceptor = -1; 234 1.1 plunky struct sockaddr_in listen_addr; 235 1.1 plunky struct sockaddr_in connect_addr; 236 1.2 christos ev_socklen_t size; 237 1.1 plunky int saved_errno = -1; 238 1.6 christos int family_test; 239 1.6 christos 240 1.6 christos family_test = family != AF_INET; 241 1.1 plunky #ifdef AF_UNIX 242 1.6 christos family_test = family_test && (family != AF_UNIX); 243 1.1 plunky #endif 244 1.6 christos if (protocol || family_test) { 245 1.2 christos EVUTIL_SET_SOCKET_ERROR(ERR(EAFNOSUPPORT)); 246 1.1 plunky return -1; 247 1.1 plunky } 248 1.6 christos 249 1.1 plunky if (!fd) { 250 1.2 christos EVUTIL_SET_SOCKET_ERROR(ERR(EINVAL)); 251 1.1 plunky return -1; 252 1.1 plunky } 253 1.1 plunky 254 1.1 plunky listener = socket(AF_INET, type, 0); 255 1.1 plunky if (listener < 0) 256 1.1 plunky return -1; 257 1.1 plunky memset(&listen_addr, 0, sizeof(listen_addr)); 258 1.1 plunky listen_addr.sin_family = AF_INET; 259 1.1 plunky listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 260 1.1 plunky listen_addr.sin_port = 0; /* kernel chooses port. */ 261 1.1 plunky if (bind(listener, (struct sockaddr *) &listen_addr, sizeof (listen_addr)) 262 1.1 plunky == -1) 263 1.1 plunky goto tidy_up_and_fail; 264 1.1 plunky if (listen(listener, 1) == -1) 265 1.1 plunky goto tidy_up_and_fail; 266 1.1 plunky 267 1.1 plunky connector = socket(AF_INET, type, 0); 268 1.1 plunky if (connector < 0) 269 1.1 plunky goto tidy_up_and_fail; 270 1.6 christos 271 1.6 christos memset(&connect_addr, 0, sizeof(connect_addr)); 272 1.6 christos 273 1.1 plunky /* We want to find out the port number to connect to. */ 274 1.1 plunky size = sizeof(connect_addr); 275 1.1 plunky if (getsockname(listener, (struct sockaddr *) &connect_addr, &size) == -1) 276 1.1 plunky goto tidy_up_and_fail; 277 1.1 plunky if (size != sizeof (connect_addr)) 278 1.1 plunky goto abort_tidy_up_and_fail; 279 1.1 plunky if (connect(connector, (struct sockaddr *) &connect_addr, 280 1.1 plunky sizeof(connect_addr)) == -1) 281 1.1 plunky goto tidy_up_and_fail; 282 1.1 plunky 283 1.1 plunky size = sizeof(listen_addr); 284 1.1 plunky acceptor = accept(listener, (struct sockaddr *) &listen_addr, &size); 285 1.1 plunky if (acceptor < 0) 286 1.1 plunky goto tidy_up_and_fail; 287 1.1 plunky if (size != sizeof(listen_addr)) 288 1.1 plunky goto abort_tidy_up_and_fail; 289 1.1 plunky /* Now check we are talking to ourself by matching port and host on the 290 1.1 plunky two sockets. */ 291 1.1 plunky if (getsockname(connector, (struct sockaddr *) &connect_addr, &size) == -1) 292 1.1 plunky goto tidy_up_and_fail; 293 1.1 plunky if (size != sizeof (connect_addr) 294 1.1 plunky || listen_addr.sin_family != connect_addr.sin_family 295 1.1 plunky || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr 296 1.1 plunky || listen_addr.sin_port != connect_addr.sin_port) 297 1.1 plunky goto abort_tidy_up_and_fail; 298 1.5 spz evutil_closesocket(listener); 299 1.1 plunky fd[0] = connector; 300 1.1 plunky fd[1] = acceptor; 301 1.1 plunky 302 1.1 plunky return 0; 303 1.1 plunky 304 1.1 plunky abort_tidy_up_and_fail: 305 1.2 christos saved_errno = ERR(ECONNABORTED); 306 1.1 plunky tidy_up_and_fail: 307 1.1 plunky if (saved_errno < 0) 308 1.2 christos saved_errno = EVUTIL_SOCKET_ERROR(); 309 1.1 plunky if (listener != -1) 310 1.2 christos evutil_closesocket(listener); 311 1.1 plunky if (connector != -1) 312 1.2 christos evutil_closesocket(connector); 313 1.1 plunky if (acceptor != -1) 314 1.2 christos evutil_closesocket(acceptor); 315 1.1 plunky 316 1.1 plunky EVUTIL_SET_SOCKET_ERROR(saved_errno); 317 1.1 plunky return -1; 318 1.2 christos #undef ERR 319 1.1 plunky } 320 1.1 plunky 321 1.1 plunky int 322 1.2 christos evutil_make_socket_nonblocking(evutil_socket_t fd) 323 1.1 plunky { 324 1.6 christos #ifdef _WIN32 325 1.1 plunky { 326 1.6 christos unsigned long nonblocking = 1; 327 1.2 christos if (ioctlsocket(fd, FIONBIO, &nonblocking) == SOCKET_ERROR) { 328 1.2 christos event_sock_warn(fd, "fcntl(%d, F_GETFL)", (int)fd); 329 1.2 christos return -1; 330 1.2 christos } 331 1.1 plunky } 332 1.1 plunky #else 333 1.2 christos { 334 1.2 christos int flags; 335 1.2 christos if ((flags = fcntl(fd, F_GETFL, NULL)) < 0) { 336 1.2 christos event_warn("fcntl(%d, F_GETFL)", fd); 337 1.2 christos return -1; 338 1.2 christos } 339 1.6 christos if (!(flags & O_NONBLOCK)) { 340 1.6 christos if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { 341 1.6 christos event_warn("fcntl(%d, F_SETFL)", fd); 342 1.6 christos return -1; 343 1.6 christos } 344 1.2 christos } 345 1.2 christos } 346 1.2 christos #endif 347 1.2 christos return 0; 348 1.2 christos } 349 1.2 christos 350 1.6 christos /* Faster version of evutil_make_socket_nonblocking for internal use. 351 1.6 christos * 352 1.6 christos * Requires that no F_SETFL flags were previously set on the fd. 353 1.6 christos */ 354 1.6 christos static int 355 1.6 christos evutil_fast_socket_nonblocking(evutil_socket_t fd) 356 1.6 christos { 357 1.6 christos #ifdef _WIN32 358 1.6 christos return evutil_make_socket_nonblocking(fd); 359 1.6 christos #else 360 1.6 christos if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { 361 1.6 christos event_warn("fcntl(%d, F_SETFL)", fd); 362 1.6 christos return -1; 363 1.6 christos } 364 1.6 christos return 0; 365 1.6 christos #endif 366 1.6 christos } 367 1.6 christos 368 1.2 christos int 369 1.2 christos evutil_make_listen_socket_reuseable(evutil_socket_t sock) 370 1.2 christos { 371 1.6 christos #if defined(SO_REUSEADDR) && !defined(_WIN32) 372 1.2 christos int one = 1; 373 1.2 christos /* REUSEADDR on Unix means, "don't hang on to this address after the 374 1.2 christos * listener is closed." On Windows, though, it means "don't keep other 375 1.2 christos * processes from binding to this address while we're using it. */ 376 1.2 christos return setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one, 377 1.2 christos (ev_socklen_t)sizeof(one)); 378 1.2 christos #else 379 1.2 christos return 0; 380 1.2 christos #endif 381 1.2 christos } 382 1.2 christos 383 1.2 christos int 384 1.6 christos evutil_make_listen_socket_reuseable_port(evutil_socket_t sock) 385 1.6 christos { 386 1.6 christos #if defined __linux__ && defined(SO_REUSEPORT) 387 1.6 christos int one = 1; 388 1.6 christos /* REUSEPORT on Linux 3.9+ means, "Multiple servers (processes or 389 1.6 christos * threads) can bind to the same port if they each set the option. */ 390 1.6 christos return setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (void*) &one, 391 1.6 christos (ev_socklen_t)sizeof(one)); 392 1.6 christos #else 393 1.6 christos return 0; 394 1.6 christos #endif 395 1.6 christos } 396 1.6 christos 397 1.6 christos int 398 1.7 christos evutil_make_listen_socket_ipv6only(evutil_socket_t sock) 399 1.7 christos { 400 1.7 christos #if defined(IPV6_V6ONLY) 401 1.7 christos int one = 1; 402 1.7 christos return setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*) &one, 403 1.7 christos (ev_socklen_t)sizeof(one)); 404 1.7 christos #endif 405 1.7 christos return 0; 406 1.7 christos } 407 1.7 christos 408 1.7 christos int 409 1.6 christos evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock) 410 1.6 christos { 411 1.6 christos #if defined(EVENT__HAVE_NETINET_TCP_H) && defined(TCP_DEFER_ACCEPT) 412 1.6 christos int one = 1; 413 1.6 christos 414 1.6 christos /* TCP_DEFER_ACCEPT tells the kernel to call defer accept() only after data 415 1.6 christos * has arrived and ready to read */ 416 1.6 christos return setsockopt(sock, IPPROTO_TCP, TCP_DEFER_ACCEPT, &one, 417 1.6 christos (ev_socklen_t)sizeof(one)); 418 1.6 christos #endif 419 1.6 christos return 0; 420 1.6 christos } 421 1.6 christos 422 1.6 christos int 423 1.2 christos evutil_make_socket_closeonexec(evutil_socket_t fd) 424 1.2 christos { 425 1.6 christos #if !defined(_WIN32) && defined(EVENT__HAVE_SETFD) 426 1.2 christos int flags; 427 1.2 christos if ((flags = fcntl(fd, F_GETFD, NULL)) < 0) { 428 1.2 christos event_warn("fcntl(%d, F_GETFD)", fd); 429 1.1 plunky return -1; 430 1.2 christos } 431 1.6 christos if (!(flags & FD_CLOEXEC)) { 432 1.6 christos if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { 433 1.6 christos event_warn("fcntl(%d, F_SETFD)", fd); 434 1.6 christos return -1; 435 1.6 christos } 436 1.6 christos } 437 1.6 christos #endif 438 1.6 christos return 0; 439 1.6 christos } 440 1.6 christos 441 1.6 christos /* Faster version of evutil_make_socket_closeonexec for internal use. 442 1.6 christos * 443 1.6 christos * Requires that no F_SETFD flags were previously set on the fd. 444 1.6 christos */ 445 1.6 christos static int 446 1.6 christos evutil_fast_socket_closeonexec(evutil_socket_t fd) 447 1.6 christos { 448 1.6 christos #if !defined(_WIN32) && defined(EVENT__HAVE_SETFD) 449 1.6 christos if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { 450 1.2 christos event_warn("fcntl(%d, F_SETFD)", fd); 451 1.2 christos return -1; 452 1.2 christos } 453 1.1 plunky #endif 454 1.1 plunky return 0; 455 1.1 plunky } 456 1.1 plunky 457 1.2 christos int 458 1.2 christos evutil_closesocket(evutil_socket_t sock) 459 1.2 christos { 460 1.6 christos #ifndef _WIN32 461 1.2 christos return close(sock); 462 1.2 christos #else 463 1.2 christos return closesocket(sock); 464 1.2 christos #endif 465 1.2 christos } 466 1.2 christos 467 1.1 plunky ev_int64_t 468 1.1 plunky evutil_strtoll(const char *s, char **endptr, int base) 469 1.1 plunky { 470 1.6 christos #ifdef EVENT__HAVE_STRTOLL 471 1.1 plunky return (ev_int64_t)strtoll(s, endptr, base); 472 1.6 christos #elif EVENT__SIZEOF_LONG == 8 473 1.1 plunky return (ev_int64_t)strtol(s, endptr, base); 474 1.6 christos #elif defined(_WIN32) && defined(_MSC_VER) && _MSC_VER < 1300 475 1.1 plunky /* XXXX on old versions of MS APIs, we only support base 476 1.1 plunky * 10. */ 477 1.1 plunky ev_int64_t r; 478 1.1 plunky if (base != 10) 479 1.1 plunky return 0; 480 1.1 plunky r = (ev_int64_t) _atoi64(s); 481 1.1 plunky while (isspace(*s)) 482 1.1 plunky ++s; 483 1.2 christos if (*s == '-') 484 1.2 christos ++s; 485 1.1 plunky while (isdigit(*s)) 486 1.1 plunky ++s; 487 1.1 plunky if (endptr) 488 1.1 plunky *endptr = (char*) s; 489 1.1 plunky return r; 490 1.6 christos #elif defined(_WIN32) 491 1.1 plunky return (ev_int64_t) _strtoi64(s, endptr, base); 492 1.6 christos #elif defined(EVENT__SIZEOF_LONG_LONG) && EVENT__SIZEOF_LONG_LONG == 8 493 1.2 christos long long r; 494 1.2 christos int n; 495 1.2 christos if (base != 10 && base != 16) 496 1.2 christos return 0; 497 1.2 christos if (base == 10) { 498 1.2 christos n = sscanf(s, "%lld", &r); 499 1.2 christos } else { 500 1.2 christos unsigned long long ru=0; 501 1.2 christos n = sscanf(s, "%llx", &ru); 502 1.2 christos if (ru > EV_INT64_MAX) 503 1.2 christos return 0; 504 1.2 christos r = (long long) ru; 505 1.2 christos } 506 1.2 christos if (n != 1) 507 1.2 christos return 0; 508 1.6 christos while (EVUTIL_ISSPACE_(*s)) 509 1.2 christos ++s; 510 1.2 christos if (*s == '-') 511 1.2 christos ++s; 512 1.2 christos if (base == 10) { 513 1.6 christos while (EVUTIL_ISDIGIT_(*s)) 514 1.2 christos ++s; 515 1.2 christos } else { 516 1.6 christos while (EVUTIL_ISXDIGIT_(*s)) 517 1.2 christos ++s; 518 1.2 christos } 519 1.2 christos if (endptr) 520 1.2 christos *endptr = (char*) s; 521 1.2 christos return r; 522 1.1 plunky #else 523 1.1 plunky #error "I don't know how to parse 64-bit integers." 524 1.1 plunky #endif 525 1.1 plunky } 526 1.1 plunky 527 1.6 christos #ifdef _WIN32 528 1.2 christos int 529 1.2 christos evutil_socket_geterror(evutil_socket_t sock) 530 1.2 christos { 531 1.2 christos int optval, optvallen=sizeof(optval); 532 1.2 christos int err = WSAGetLastError(); 533 1.2 christos if (err == WSAEWOULDBLOCK && sock >= 0) { 534 1.2 christos if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)&optval, 535 1.2 christos &optvallen)) 536 1.2 christos return err; 537 1.2 christos if (optval) 538 1.2 christos return optval; 539 1.2 christos } 540 1.2 christos return err; 541 1.2 christos } 542 1.2 christos #endif 543 1.2 christos 544 1.2 christos /* XXX we should use an enum here. */ 545 1.2 christos /* 2 for connection refused, 1 for connected, 0 for not yet, -1 for error. */ 546 1.2 christos int 547 1.6 christos evutil_socket_connect_(evutil_socket_t *fd_ptr, const struct sockaddr *sa, int socklen) 548 1.2 christos { 549 1.2 christos int made_fd = 0; 550 1.2 christos 551 1.2 christos if (*fd_ptr < 0) { 552 1.2 christos if ((*fd_ptr = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) 553 1.2 christos goto err; 554 1.2 christos made_fd = 1; 555 1.2 christos if (evutil_make_socket_nonblocking(*fd_ptr) < 0) { 556 1.2 christos goto err; 557 1.2 christos } 558 1.2 christos } 559 1.2 christos 560 1.2 christos if (connect(*fd_ptr, sa, socklen) < 0) { 561 1.2 christos int e = evutil_socket_geterror(*fd_ptr); 562 1.2 christos if (EVUTIL_ERR_CONNECT_RETRIABLE(e)) 563 1.2 christos return 0; 564 1.2 christos if (EVUTIL_ERR_CONNECT_REFUSED(e)) 565 1.2 christos return 2; 566 1.2 christos goto err; 567 1.2 christos } else { 568 1.2 christos return 1; 569 1.2 christos } 570 1.2 christos 571 1.2 christos err: 572 1.2 christos if (made_fd) { 573 1.2 christos evutil_closesocket(*fd_ptr); 574 1.2 christos *fd_ptr = -1; 575 1.2 christos } 576 1.2 christos return -1; 577 1.2 christos } 578 1.2 christos 579 1.2 christos /* Check whether a socket on which we called connect() is done 580 1.2 christos connecting. Return 1 for connected, 0 for not yet, -1 for error. In the 581 1.2 christos error case, set the current socket errno to the error that happened during 582 1.2 christos the connect operation. */ 583 1.2 christos int 584 1.6 christos evutil_socket_finished_connecting_(evutil_socket_t fd) 585 1.2 christos { 586 1.2 christos int e; 587 1.2 christos ev_socklen_t elen = sizeof(e); 588 1.2 christos 589 1.2 christos if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&e, &elen) < 0) 590 1.2 christos return -1; 591 1.2 christos 592 1.2 christos if (e) { 593 1.2 christos if (EVUTIL_ERR_CONNECT_RETRIABLE(e)) 594 1.2 christos return 0; 595 1.2 christos EVUTIL_SET_SOCKET_ERROR(e); 596 1.2 christos return -1; 597 1.2 christos } 598 1.2 christos 599 1.2 christos return 1; 600 1.2 christos } 601 1.2 christos 602 1.2 christos #if (EVUTIL_AI_PASSIVE|EVUTIL_AI_CANONNAME|EVUTIL_AI_NUMERICHOST| \ 603 1.2 christos EVUTIL_AI_NUMERICSERV|EVUTIL_AI_V4MAPPED|EVUTIL_AI_ALL| \ 604 1.2 christos EVUTIL_AI_ADDRCONFIG) != \ 605 1.2 christos (EVUTIL_AI_PASSIVE^EVUTIL_AI_CANONNAME^EVUTIL_AI_NUMERICHOST^ \ 606 1.2 christos EVUTIL_AI_NUMERICSERV^EVUTIL_AI_V4MAPPED^EVUTIL_AI_ALL^ \ 607 1.2 christos EVUTIL_AI_ADDRCONFIG) 608 1.2 christos #error "Some of our EVUTIL_AI_* flags seem to overlap with system AI_* flags" 609 1.2 christos #endif 610 1.2 christos 611 1.2 christos /* We sometimes need to know whether we have an ipv4 address and whether we 612 1.2 christos have an ipv6 address. If 'have_checked_interfaces', then we've already done 613 1.2 christos the test. If 'had_ipv4_address', then it turns out we had an ipv4 address. 614 1.2 christos If 'had_ipv6_address', then it turns out we had an ipv6 address. These are 615 1.2 christos set by evutil_check_interfaces. */ 616 1.2 christos static int have_checked_interfaces, had_ipv4_address, had_ipv6_address; 617 1.2 christos 618 1.7 christos /* True iff the IPv4 address 'addr', in host order, is in 127.0.0.0/8 */ 619 1.7 christos static inline int evutil_v4addr_is_localhost(ev_uint32_t addr) 620 1.7 christos { return addr>>24 == 127; } 621 1.7 christos 622 1.7 christos /* True iff the IPv4 address 'addr', in host order, is link-local 623 1.7 christos * 169.254.0.0/16 (RFC3927) */ 624 1.7 christos static inline int evutil_v4addr_is_linklocal(ev_uint32_t addr) 625 1.7 christos { return ((addr & 0xffff0000U) == 0xa9fe0000U); } 626 1.7 christos 627 1.7 christos /* True iff the IPv4 address 'addr', in host order, is a class D 628 1.7 christos * (multiclass) address. */ 629 1.7 christos static inline int evutil_v4addr_is_classd(ev_uint32_t addr) 630 1.7 christos { return ((addr>>24) & 0xf0) == 0xe0; } 631 1.7 christos 632 1.7 christos int 633 1.7 christos evutil_v4addr_is_local_(const struct in_addr *in) 634 1.7 christos { 635 1.7 christos const ev_uint32_t addr = ntohl(in->s_addr); 636 1.7 christos return addr == INADDR_ANY || 637 1.7 christos evutil_v4addr_is_localhost(addr) || 638 1.7 christos evutil_v4addr_is_linklocal(addr) || 639 1.7 christos evutil_v4addr_is_classd(addr); 640 1.7 christos } 641 1.7 christos int 642 1.7 christos evutil_v6addr_is_local_(const struct in6_addr *in) 643 1.7 christos { 644 1.7 christos static const char ZEROES[] = 645 1.7 christos "\x00\x00\x00\x00\x00\x00\x00\x00" 646 1.7 christos "\x00\x00\x00\x00\x00\x00\x00\x00"; 647 1.7 christos 648 1.7 christos const unsigned char *addr = (const unsigned char *)in->s6_addr; 649 1.7 christos return !memcmp(addr, ZEROES, 8) || 650 1.7 christos ((addr[0] & 0xfe) == 0xfc) || 651 1.7 christos (addr[0] == 0xfe && (addr[1] & 0xc0) == 0x80) || 652 1.7 christos (addr[0] == 0xfe && (addr[1] & 0xc0) == 0xc0) || 653 1.7 christos (addr[0] == 0xff); 654 1.7 christos } 655 1.2 christos 656 1.6 christos static void 657 1.6 christos evutil_found_ifaddr(const struct sockaddr *sa) 658 1.6 christos { 659 1.6 christos if (sa->sa_family == AF_INET) { 660 1.6 christos const struct sockaddr_in *sin = (const struct sockaddr_in *)sa; 661 1.7 christos if (!evutil_v4addr_is_local_(&sin->sin_addr)) { 662 1.6 christos event_debug(("Detected an IPv4 interface")); 663 1.6 christos had_ipv4_address = 1; 664 1.6 christos } 665 1.6 christos } else if (sa->sa_family == AF_INET6) { 666 1.7 christos const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa; 667 1.7 christos if (!evutil_v6addr_is_local_(&sin6->sin6_addr)) { 668 1.6 christos event_debug(("Detected an IPv6 interface")); 669 1.6 christos had_ipv6_address = 1; 670 1.6 christos } 671 1.6 christos } 672 1.6 christos } 673 1.6 christos 674 1.6 christos #ifdef _WIN32 675 1.6 christos typedef ULONG (WINAPI *GetAdaptersAddresses_fn_t)( 676 1.6 christos ULONG, ULONG, PVOID, PIP_ADAPTER_ADDRESSES, PULONG); 677 1.6 christos #endif 678 1.6 christos 679 1.6 christos static int 680 1.6 christos evutil_check_ifaddrs(void) 681 1.6 christos { 682 1.6 christos #if defined(EVENT__HAVE_GETIFADDRS) 683 1.6 christos /* Most free Unixy systems provide getifaddrs, which gives us a linked list 684 1.6 christos * of struct ifaddrs. */ 685 1.6 christos struct ifaddrs *ifa = NULL; 686 1.6 christos const struct ifaddrs *i; 687 1.6 christos if (getifaddrs(&ifa) < 0) { 688 1.6 christos event_warn("Unable to call getifaddrs()"); 689 1.6 christos return -1; 690 1.6 christos } 691 1.6 christos 692 1.6 christos for (i = ifa; i; i = i->ifa_next) { 693 1.6 christos if (!i->ifa_addr) 694 1.6 christos continue; 695 1.6 christos evutil_found_ifaddr(i->ifa_addr); 696 1.6 christos } 697 1.6 christos 698 1.6 christos freeifaddrs(ifa); 699 1.6 christos return 0; 700 1.6 christos #elif defined(_WIN32) 701 1.6 christos /* Windows XP began to provide GetAdaptersAddresses. Windows 2000 had a 702 1.6 christos "GetAdaptersInfo", but that's deprecated; let's just try 703 1.6 christos GetAdaptersAddresses and fall back to connect+getsockname. 704 1.6 christos */ 705 1.7 christos HMODULE lib = evutil_load_windows_system_library_(TEXT("iphlpapi.dll")); 706 1.6 christos GetAdaptersAddresses_fn_t fn; 707 1.6 christos ULONG size, res; 708 1.6 christos IP_ADAPTER_ADDRESSES *addresses = NULL, *address; 709 1.6 christos int result = -1; 710 1.6 christos 711 1.6 christos #define FLAGS (GAA_FLAG_SKIP_ANYCAST | \ 712 1.6 christos GAA_FLAG_SKIP_MULTICAST | \ 713 1.6 christos GAA_FLAG_SKIP_DNS_SERVER) 714 1.6 christos 715 1.6 christos if (!lib) 716 1.6 christos goto done; 717 1.6 christos 718 1.6 christos if (!(fn = (GetAdaptersAddresses_fn_t) GetProcAddress(lib, "GetAdaptersAddresses"))) 719 1.6 christos goto done; 720 1.6 christos 721 1.6 christos /* Guess how much space we need. */ 722 1.6 christos size = 15*1024; 723 1.6 christos addresses = mm_malloc(size); 724 1.6 christos if (!addresses) 725 1.6 christos goto done; 726 1.6 christos res = fn(AF_UNSPEC, FLAGS, NULL, addresses, &size); 727 1.6 christos if (res == ERROR_BUFFER_OVERFLOW) { 728 1.6 christos /* we didn't guess that we needed enough space; try again */ 729 1.6 christos mm_free(addresses); 730 1.6 christos addresses = mm_malloc(size); 731 1.6 christos if (!addresses) 732 1.6 christos goto done; 733 1.6 christos res = fn(AF_UNSPEC, FLAGS, NULL, addresses, &size); 734 1.6 christos } 735 1.6 christos if (res != NO_ERROR) 736 1.6 christos goto done; 737 1.6 christos 738 1.6 christos for (address = addresses; address; address = address->Next) { 739 1.6 christos IP_ADAPTER_UNICAST_ADDRESS *a; 740 1.6 christos for (a = address->FirstUnicastAddress; a; a = a->Next) { 741 1.6 christos /* Yes, it's a linked list inside a linked list */ 742 1.6 christos struct sockaddr *sa = a->Address.lpSockaddr; 743 1.6 christos evutil_found_ifaddr(sa); 744 1.6 christos } 745 1.6 christos } 746 1.6 christos 747 1.6 christos result = 0; 748 1.6 christos done: 749 1.6 christos if (lib) 750 1.6 christos FreeLibrary(lib); 751 1.6 christos if (addresses) 752 1.6 christos mm_free(addresses); 753 1.6 christos return result; 754 1.6 christos #else 755 1.6 christos return -1; 756 1.6 christos #endif 757 1.6 christos } 758 1.6 christos 759 1.2 christos /* Test whether we have an ipv4 interface and an ipv6 interface. Return 0 if 760 1.2 christos * the test seemed successful. */ 761 1.2 christos static int 762 1.7 christos evutil_check_interfaces(void) 763 1.2 christos { 764 1.2 christos evutil_socket_t fd = -1; 765 1.2 christos struct sockaddr_in sin, sin_out; 766 1.2 christos struct sockaddr_in6 sin6, sin6_out; 767 1.2 christos ev_socklen_t sin_out_len = sizeof(sin_out); 768 1.2 christos ev_socklen_t sin6_out_len = sizeof(sin6_out); 769 1.2 christos int r; 770 1.7 christos if (have_checked_interfaces) 771 1.2 christos return 0; 772 1.2 christos 773 1.7 christos /* From this point on we have done the ipv4/ipv6 interface check */ 774 1.7 christos have_checked_interfaces = 1; 775 1.7 christos 776 1.6 christos if (evutil_check_ifaddrs() == 0) { 777 1.6 christos /* Use a nice sane interface, if this system has one. */ 778 1.6 christos return 0; 779 1.6 christos } 780 1.6 christos 781 1.6 christos /* Ugh. There was no nice sane interface. So to check whether we have 782 1.6 christos * an interface open for a given protocol, will try to make a UDP 783 1.6 christos * 'connection' to a remote host on the internet. We don't actually 784 1.6 christos * use it, so the address doesn't matter, but we want to pick one that 785 1.6 christos * keep us from using a host- or link-local interface. */ 786 1.2 christos memset(&sin, 0, sizeof(sin)); 787 1.2 christos sin.sin_family = AF_INET; 788 1.2 christos sin.sin_port = htons(53); 789 1.2 christos r = evutil_inet_pton(AF_INET, "18.244.0.188", &sin.sin_addr); 790 1.2 christos EVUTIL_ASSERT(r); 791 1.2 christos 792 1.2 christos memset(&sin6, 0, sizeof(sin6)); 793 1.2 christos sin6.sin6_family = AF_INET6; 794 1.2 christos sin6.sin6_port = htons(53); 795 1.2 christos r = evutil_inet_pton(AF_INET6, "2001:4860:b002::68", &sin6.sin6_addr); 796 1.2 christos EVUTIL_ASSERT(r); 797 1.2 christos 798 1.2 christos memset(&sin_out, 0, sizeof(sin_out)); 799 1.2 christos memset(&sin6_out, 0, sizeof(sin6_out)); 800 1.2 christos 801 1.2 christos /* XXX some errnos mean 'no address'; some mean 'not enough sockets'. */ 802 1.2 christos if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) >= 0 && 803 1.2 christos connect(fd, (struct sockaddr*)&sin, sizeof(sin)) == 0 && 804 1.2 christos getsockname(fd, (struct sockaddr*)&sin_out, &sin_out_len) == 0) { 805 1.2 christos /* We might have an IPv4 interface. */ 806 1.6 christos evutil_found_ifaddr((struct sockaddr*) &sin_out); 807 1.2 christos } 808 1.2 christos if (fd >= 0) 809 1.2 christos evutil_closesocket(fd); 810 1.2 christos 811 1.2 christos if ((fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) >= 0 && 812 1.2 christos connect(fd, (struct sockaddr*)&sin6, sizeof(sin6)) == 0 && 813 1.2 christos getsockname(fd, (struct sockaddr*)&sin6_out, &sin6_out_len) == 0) { 814 1.2 christos /* We might have an IPv6 interface. */ 815 1.6 christos evutil_found_ifaddr((struct sockaddr*) &sin6_out); 816 1.2 christos } 817 1.2 christos 818 1.2 christos if (fd >= 0) 819 1.2 christos evutil_closesocket(fd); 820 1.2 christos 821 1.2 christos return 0; 822 1.2 christos } 823 1.2 christos 824 1.2 christos /* Internal addrinfo flag. This one is set when we allocate the addrinfo from 825 1.2 christos * inside libevent. Otherwise, the built-in getaddrinfo() function allocated 826 1.2 christos * it, and we should trust what they said. 827 1.2 christos **/ 828 1.2 christos #define EVUTIL_AI_LIBEVENT_ALLOCATED 0x80000000 829 1.2 christos 830 1.2 christos /* Helper: construct a new addrinfo containing the socket address in 831 1.2 christos * 'sa', which must be a sockaddr_in or a sockaddr_in6. Take the 832 1.2 christos * socktype and protocol info from hints. If they weren't set, then 833 1.2 christos * allocate both a TCP and a UDP addrinfo. 834 1.2 christos */ 835 1.2 christos struct evutil_addrinfo * 836 1.6 christos evutil_new_addrinfo_(struct sockaddr *sa, ev_socklen_t socklen, 837 1.2 christos const struct evutil_addrinfo *hints) 838 1.2 christos { 839 1.2 christos struct evutil_addrinfo *res; 840 1.2 christos EVUTIL_ASSERT(hints); 841 1.2 christos 842 1.2 christos if (hints->ai_socktype == 0 && hints->ai_protocol == 0) { 843 1.2 christos /* Indecisive user! Give them a UDP and a TCP. */ 844 1.2 christos struct evutil_addrinfo *r1, *r2; 845 1.2 christos struct evutil_addrinfo tmp; 846 1.2 christos memcpy(&tmp, hints, sizeof(tmp)); 847 1.2 christos tmp.ai_socktype = SOCK_STREAM; tmp.ai_protocol = IPPROTO_TCP; 848 1.6 christos r1 = evutil_new_addrinfo_(sa, socklen, &tmp); 849 1.2 christos if (!r1) 850 1.2 christos return NULL; 851 1.2 christos tmp.ai_socktype = SOCK_DGRAM; tmp.ai_protocol = IPPROTO_UDP; 852 1.6 christos r2 = evutil_new_addrinfo_(sa, socklen, &tmp); 853 1.2 christos if (!r2) { 854 1.2 christos evutil_freeaddrinfo(r1); 855 1.2 christos return NULL; 856 1.2 christos } 857 1.2 christos r1->ai_next = r2; 858 1.2 christos return r1; 859 1.2 christos } 860 1.2 christos 861 1.2 christos /* We're going to allocate extra space to hold the sockaddr. */ 862 1.2 christos res = mm_calloc(1,sizeof(struct evutil_addrinfo)+socklen); 863 1.2 christos if (!res) 864 1.2 christos return NULL; 865 1.2 christos res->ai_addr = (struct sockaddr*) 866 1.2 christos (((char*)res) + sizeof(struct evutil_addrinfo)); 867 1.2 christos memcpy(res->ai_addr, sa, socklen); 868 1.2 christos res->ai_addrlen = socklen; 869 1.2 christos res->ai_family = sa->sa_family; /* Same or not? XXX */ 870 1.2 christos res->ai_flags = EVUTIL_AI_LIBEVENT_ALLOCATED; 871 1.2 christos res->ai_socktype = hints->ai_socktype; 872 1.2 christos res->ai_protocol = hints->ai_protocol; 873 1.2 christos 874 1.2 christos return res; 875 1.2 christos } 876 1.2 christos 877 1.2 christos /* Append the addrinfo 'append' to the end of 'first', and return the start of 878 1.2 christos * the list. Either element can be NULL, in which case we return the element 879 1.2 christos * that is not NULL. */ 880 1.2 christos struct evutil_addrinfo * 881 1.6 christos evutil_addrinfo_append_(struct evutil_addrinfo *first, 882 1.2 christos struct evutil_addrinfo *append) 883 1.2 christos { 884 1.2 christos struct evutil_addrinfo *ai = first; 885 1.2 christos if (!ai) 886 1.2 christos return append; 887 1.2 christos while (ai->ai_next) 888 1.2 christos ai = ai->ai_next; 889 1.2 christos ai->ai_next = append; 890 1.2 christos 891 1.2 christos return first; 892 1.2 christos } 893 1.2 christos 894 1.2 christos static int 895 1.2 christos parse_numeric_servname(const char *servname) 896 1.2 christos { 897 1.2 christos int n; 898 1.2 christos char *endptr=NULL; 899 1.2 christos n = (int) strtol(servname, &endptr, 10); 900 1.2 christos if (n>=0 && n <= 65535 && servname[0] && endptr && !endptr[0]) 901 1.2 christos return n; 902 1.2 christos else 903 1.2 christos return -1; 904 1.2 christos } 905 1.2 christos 906 1.2 christos /** Parse a service name in 'servname', which can be a decimal port. 907 1.2 christos * Return the port number, or -1 on error. 908 1.2 christos */ 909 1.2 christos static int 910 1.2 christos evutil_parse_servname(const char *servname, const char *protocol, 911 1.2 christos const struct evutil_addrinfo *hints) 912 1.2 christos { 913 1.2 christos int n = parse_numeric_servname(servname); 914 1.2 christos if (n>=0) 915 1.2 christos return n; 916 1.6 christos #if defined(EVENT__HAVE_GETSERVBYNAME) || defined(_WIN32) 917 1.2 christos if (!(hints->ai_flags & EVUTIL_AI_NUMERICSERV)) { 918 1.2 christos struct servent *ent = getservbyname(servname, protocol); 919 1.2 christos if (ent) { 920 1.2 christos return ntohs(ent->s_port); 921 1.2 christos } 922 1.2 christos } 923 1.2 christos #endif 924 1.2 christos return -1; 925 1.2 christos } 926 1.2 christos 927 1.2 christos /* Return a string corresponding to a protocol number that we can pass to 928 1.2 christos * getservyname. */ 929 1.2 christos static const char * 930 1.2 christos evutil_unparse_protoname(int proto) 931 1.2 christos { 932 1.2 christos switch (proto) { 933 1.2 christos case 0: 934 1.2 christos return NULL; 935 1.2 christos case IPPROTO_TCP: 936 1.2 christos return "tcp"; 937 1.2 christos case IPPROTO_UDP: 938 1.2 christos return "udp"; 939 1.2 christos #ifdef IPPROTO_SCTP 940 1.2 christos case IPPROTO_SCTP: 941 1.2 christos return "sctp"; 942 1.2 christos #endif 943 1.2 christos default: 944 1.6 christos #ifdef EVENT__HAVE_GETPROTOBYNUMBER 945 1.2 christos { 946 1.2 christos struct protoent *ent = getprotobynumber(proto); 947 1.2 christos if (ent) 948 1.2 christos return ent->p_name; 949 1.2 christos } 950 1.2 christos #endif 951 1.2 christos return NULL; 952 1.2 christos } 953 1.2 christos } 954 1.2 christos 955 1.2 christos static void 956 1.2 christos evutil_getaddrinfo_infer_protocols(struct evutil_addrinfo *hints) 957 1.2 christos { 958 1.2 christos /* If we can guess the protocol from the socktype, do so. */ 959 1.2 christos if (!hints->ai_protocol && hints->ai_socktype) { 960 1.2 christos if (hints->ai_socktype == SOCK_DGRAM) 961 1.2 christos hints->ai_protocol = IPPROTO_UDP; 962 1.2 christos else if (hints->ai_socktype == SOCK_STREAM) 963 1.2 christos hints->ai_protocol = IPPROTO_TCP; 964 1.2 christos } 965 1.2 christos 966 1.2 christos /* Set the socktype if it isn't set. */ 967 1.2 christos if (!hints->ai_socktype && hints->ai_protocol) { 968 1.2 christos if (hints->ai_protocol == IPPROTO_UDP) 969 1.2 christos hints->ai_socktype = SOCK_DGRAM; 970 1.2 christos else if (hints->ai_protocol == IPPROTO_TCP) 971 1.2 christos hints->ai_socktype = SOCK_STREAM; 972 1.2 christos #ifdef IPPROTO_SCTP 973 1.2 christos else if (hints->ai_protocol == IPPROTO_SCTP) 974 1.2 christos hints->ai_socktype = SOCK_STREAM; 975 1.2 christos #endif 976 1.2 christos } 977 1.2 christos } 978 1.2 christos 979 1.2 christos #if AF_UNSPEC != PF_UNSPEC 980 1.2 christos #error "I cannot build on a system where AF_UNSPEC != PF_UNSPEC" 981 1.2 christos #endif 982 1.2 christos 983 1.2 christos /** Implements the part of looking up hosts by name that's common to both 984 1.2 christos * the blocking and nonblocking resolver: 985 1.2 christos * - Adjust 'hints' to have a reasonable socktype and protocol. 986 1.2 christos * - Look up the port based on 'servname', and store it in *portnum, 987 1.2 christos * - Handle the nodename==NULL case 988 1.2 christos * - Handle some invalid arguments cases. 989 1.2 christos * - Handle the cases where nodename is an IPv4 or IPv6 address. 990 1.2 christos * 991 1.2 christos * If we need the resolver to look up the hostname, we return 992 1.2 christos * EVUTIL_EAI_NEED_RESOLVE. Otherwise, we can completely implement 993 1.2 christos * getaddrinfo: we return 0 or an appropriate EVUTIL_EAI_* error, and 994 1.2 christos * set *res as getaddrinfo would. 995 1.2 christos */ 996 1.2 christos int 997 1.6 christos evutil_getaddrinfo_common_(const char *nodename, const char *servname, 998 1.2 christos struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum) 999 1.2 christos { 1000 1.2 christos int port = 0; 1001 1.7 christos unsigned int if_index; 1002 1.2 christos const char *pname; 1003 1.2 christos 1004 1.2 christos if (nodename == NULL && servname == NULL) 1005 1.2 christos return EVUTIL_EAI_NONAME; 1006 1.2 christos 1007 1.2 christos /* We only understand 3 families */ 1008 1.2 christos if (hints->ai_family != PF_UNSPEC && hints->ai_family != PF_INET && 1009 1.2 christos hints->ai_family != PF_INET6) 1010 1.2 christos return EVUTIL_EAI_FAMILY; 1011 1.2 christos 1012 1.2 christos evutil_getaddrinfo_infer_protocols(hints); 1013 1.2 christos 1014 1.2 christos /* Look up the port number and protocol, if possible. */ 1015 1.2 christos pname = evutil_unparse_protoname(hints->ai_protocol); 1016 1.2 christos if (servname) { 1017 1.2 christos /* XXXX We could look at the protocol we got back from 1018 1.2 christos * getservbyname, but it doesn't seem too useful. */ 1019 1.2 christos port = evutil_parse_servname(servname, pname, hints); 1020 1.2 christos if (port < 0) { 1021 1.2 christos return EVUTIL_EAI_NONAME; 1022 1.2 christos } 1023 1.2 christos } 1024 1.2 christos 1025 1.2 christos /* If we have no node name, then we're supposed to bind to 'any' and 1026 1.2 christos * connect to localhost. */ 1027 1.2 christos if (nodename == NULL) { 1028 1.2 christos struct evutil_addrinfo *res4=NULL, *res6=NULL; 1029 1.2 christos if (hints->ai_family != PF_INET) { /* INET6 or UNSPEC. */ 1030 1.2 christos struct sockaddr_in6 sin6; 1031 1.2 christos memset(&sin6, 0, sizeof(sin6)); 1032 1.2 christos sin6.sin6_family = AF_INET6; 1033 1.2 christos sin6.sin6_port = htons(port); 1034 1.2 christos if (hints->ai_flags & EVUTIL_AI_PASSIVE) { 1035 1.2 christos /* Bind to :: */ 1036 1.2 christos } else { 1037 1.2 christos /* connect to ::1 */ 1038 1.2 christos sin6.sin6_addr.s6_addr[15] = 1; 1039 1.2 christos } 1040 1.6 christos res6 = evutil_new_addrinfo_((struct sockaddr*)&sin6, 1041 1.2 christos sizeof(sin6), hints); 1042 1.2 christos if (!res6) 1043 1.2 christos return EVUTIL_EAI_MEMORY; 1044 1.2 christos } 1045 1.2 christos 1046 1.2 christos if (hints->ai_family != PF_INET6) { /* INET or UNSPEC */ 1047 1.2 christos struct sockaddr_in sin; 1048 1.2 christos memset(&sin, 0, sizeof(sin)); 1049 1.2 christos sin.sin_family = AF_INET; 1050 1.2 christos sin.sin_port = htons(port); 1051 1.2 christos if (hints->ai_flags & EVUTIL_AI_PASSIVE) { 1052 1.2 christos /* Bind to 0.0.0.0 */ 1053 1.2 christos } else { 1054 1.2 christos /* connect to 127.0.0.1 */ 1055 1.2 christos sin.sin_addr.s_addr = htonl(0x7f000001); 1056 1.2 christos } 1057 1.6 christos res4 = evutil_new_addrinfo_((struct sockaddr*)&sin, 1058 1.2 christos sizeof(sin), hints); 1059 1.2 christos if (!res4) { 1060 1.2 christos if (res6) 1061 1.2 christos evutil_freeaddrinfo(res6); 1062 1.2 christos return EVUTIL_EAI_MEMORY; 1063 1.2 christos } 1064 1.2 christos } 1065 1.6 christos *res = evutil_addrinfo_append_(res4, res6); 1066 1.2 christos return 0; 1067 1.2 christos } 1068 1.2 christos 1069 1.2 christos /* If we can, we should try to parse the hostname without resolving 1070 1.2 christos * it. */ 1071 1.2 christos /* Try ipv6. */ 1072 1.2 christos if (hints->ai_family == PF_INET6 || hints->ai_family == PF_UNSPEC) { 1073 1.2 christos struct sockaddr_in6 sin6; 1074 1.2 christos memset(&sin6, 0, sizeof(sin6)); 1075 1.7 christos if (1 == evutil_inet_pton_scope( 1076 1.7 christos AF_INET6, nodename, &sin6.sin6_addr, &if_index)) { 1077 1.2 christos /* Got an ipv6 address. */ 1078 1.2 christos sin6.sin6_family = AF_INET6; 1079 1.2 christos sin6.sin6_port = htons(port); 1080 1.7 christos sin6.sin6_scope_id = if_index; 1081 1.6 christos *res = evutil_new_addrinfo_((struct sockaddr*)&sin6, 1082 1.2 christos sizeof(sin6), hints); 1083 1.2 christos if (!*res) 1084 1.2 christos return EVUTIL_EAI_MEMORY; 1085 1.2 christos return 0; 1086 1.2 christos } 1087 1.2 christos } 1088 1.2 christos 1089 1.2 christos /* Try ipv4. */ 1090 1.2 christos if (hints->ai_family == PF_INET || hints->ai_family == PF_UNSPEC) { 1091 1.2 christos struct sockaddr_in sin; 1092 1.2 christos memset(&sin, 0, sizeof(sin)); 1093 1.2 christos if (1==evutil_inet_pton(AF_INET, nodename, &sin.sin_addr)) { 1094 1.7 christos /* Got an ipv4 address. */ 1095 1.2 christos sin.sin_family = AF_INET; 1096 1.2 christos sin.sin_port = htons(port); 1097 1.6 christos *res = evutil_new_addrinfo_((struct sockaddr*)&sin, 1098 1.2 christos sizeof(sin), hints); 1099 1.2 christos if (!*res) 1100 1.2 christos return EVUTIL_EAI_MEMORY; 1101 1.2 christos return 0; 1102 1.2 christos } 1103 1.2 christos } 1104 1.2 christos 1105 1.2 christos 1106 1.2 christos /* If we have reached this point, we definitely need to do a DNS 1107 1.2 christos * lookup. */ 1108 1.2 christos if ((hints->ai_flags & EVUTIL_AI_NUMERICHOST)) { 1109 1.2 christos /* If we're not allowed to do one, then say so. */ 1110 1.2 christos return EVUTIL_EAI_NONAME; 1111 1.2 christos } 1112 1.2 christos *portnum = port; 1113 1.2 christos return EVUTIL_EAI_NEED_RESOLVE; 1114 1.2 christos } 1115 1.2 christos 1116 1.6 christos #ifdef EVENT__HAVE_GETADDRINFO 1117 1.2 christos #define USE_NATIVE_GETADDRINFO 1118 1.2 christos #endif 1119 1.2 christos 1120 1.2 christos #ifdef USE_NATIVE_GETADDRINFO 1121 1.2 christos /* A mask of all the flags that we declare, so we can clear them before calling 1122 1.2 christos * the native getaddrinfo */ 1123 1.2 christos static const unsigned int ALL_NONNATIVE_AI_FLAGS = 1124 1.2 christos #ifndef AI_PASSIVE 1125 1.2 christos EVUTIL_AI_PASSIVE | 1126 1.2 christos #endif 1127 1.2 christos #ifndef AI_CANONNAME 1128 1.2 christos EVUTIL_AI_CANONNAME | 1129 1.2 christos #endif 1130 1.2 christos #ifndef AI_NUMERICHOST 1131 1.2 christos EVUTIL_AI_NUMERICHOST | 1132 1.2 christos #endif 1133 1.2 christos #ifndef AI_NUMERICSERV 1134 1.2 christos EVUTIL_AI_NUMERICSERV | 1135 1.2 christos #endif 1136 1.2 christos #ifndef AI_ADDRCONFIG 1137 1.2 christos EVUTIL_AI_ADDRCONFIG | 1138 1.2 christos #endif 1139 1.2 christos #ifndef AI_ALL 1140 1.2 christos EVUTIL_AI_ALL | 1141 1.2 christos #endif 1142 1.2 christos #ifndef AI_V4MAPPED 1143 1.2 christos EVUTIL_AI_V4MAPPED | 1144 1.2 christos #endif 1145 1.2 christos EVUTIL_AI_LIBEVENT_ALLOCATED; 1146 1.2 christos 1147 1.2 christos static const unsigned int ALL_NATIVE_AI_FLAGS = 1148 1.2 christos #ifdef AI_PASSIVE 1149 1.2 christos AI_PASSIVE | 1150 1.2 christos #endif 1151 1.2 christos #ifdef AI_CANONNAME 1152 1.2 christos AI_CANONNAME | 1153 1.2 christos #endif 1154 1.2 christos #ifdef AI_NUMERICHOST 1155 1.2 christos AI_NUMERICHOST | 1156 1.2 christos #endif 1157 1.2 christos #ifdef AI_NUMERICSERV 1158 1.2 christos AI_NUMERICSERV | 1159 1.2 christos #endif 1160 1.2 christos #ifdef AI_ADDRCONFIG 1161 1.2 christos AI_ADDRCONFIG | 1162 1.2 christos #endif 1163 1.2 christos #ifdef AI_ALL 1164 1.2 christos AI_ALL | 1165 1.2 christos #endif 1166 1.2 christos #ifdef AI_V4MAPPED 1167 1.2 christos AI_V4MAPPED | 1168 1.2 christos #endif 1169 1.2 christos 0; 1170 1.2 christos #endif 1171 1.2 christos 1172 1.2 christos #ifndef USE_NATIVE_GETADDRINFO 1173 1.2 christos /* Helper for systems with no getaddrinfo(): make one or more addrinfos out of 1174 1.2 christos * a struct hostent. 1175 1.2 christos */ 1176 1.2 christos static struct evutil_addrinfo * 1177 1.2 christos addrinfo_from_hostent(const struct hostent *ent, 1178 1.2 christos int port, const struct evutil_addrinfo *hints) 1179 1.2 christos { 1180 1.2 christos int i; 1181 1.2 christos struct sockaddr_in sin; 1182 1.2 christos struct sockaddr_in6 sin6; 1183 1.2 christos struct sockaddr *sa; 1184 1.2 christos int socklen; 1185 1.2 christos struct evutil_addrinfo *res=NULL, *ai; 1186 1.2 christos void *addrp; 1187 1.2 christos 1188 1.2 christos if (ent->h_addrtype == PF_INET) { 1189 1.2 christos memset(&sin, 0, sizeof(sin)); 1190 1.2 christos sin.sin_family = AF_INET; 1191 1.2 christos sin.sin_port = htons(port); 1192 1.2 christos sa = (struct sockaddr *)&sin; 1193 1.2 christos socklen = sizeof(struct sockaddr_in); 1194 1.2 christos addrp = &sin.sin_addr; 1195 1.2 christos if (ent->h_length != sizeof(sin.sin_addr)) { 1196 1.2 christos event_warnx("Weird h_length from gethostbyname"); 1197 1.2 christos return NULL; 1198 1.2 christos } 1199 1.2 christos } else if (ent->h_addrtype == PF_INET6) { 1200 1.2 christos memset(&sin6, 0, sizeof(sin6)); 1201 1.2 christos sin6.sin6_family = AF_INET6; 1202 1.2 christos sin6.sin6_port = htons(port); 1203 1.2 christos sa = (struct sockaddr *)&sin6; 1204 1.6 christos socklen = sizeof(struct sockaddr_in6); 1205 1.2 christos addrp = &sin6.sin6_addr; 1206 1.2 christos if (ent->h_length != sizeof(sin6.sin6_addr)) { 1207 1.2 christos event_warnx("Weird h_length from gethostbyname"); 1208 1.2 christos return NULL; 1209 1.2 christos } 1210 1.2 christos } else 1211 1.2 christos return NULL; 1212 1.2 christos 1213 1.2 christos for (i = 0; ent->h_addr_list[i]; ++i) { 1214 1.2 christos memcpy(addrp, ent->h_addr_list[i], ent->h_length); 1215 1.6 christos ai = evutil_new_addrinfo_(sa, socklen, hints); 1216 1.2 christos if (!ai) { 1217 1.2 christos evutil_freeaddrinfo(res); 1218 1.2 christos return NULL; 1219 1.2 christos } 1220 1.6 christos res = evutil_addrinfo_append_(res, ai); 1221 1.2 christos } 1222 1.2 christos 1223 1.2 christos if (res && ((hints->ai_flags & EVUTIL_AI_CANONNAME) && ent->h_name)) { 1224 1.2 christos res->ai_canonname = mm_strdup(ent->h_name); 1225 1.2 christos if (res->ai_canonname == NULL) { 1226 1.2 christos evutil_freeaddrinfo(res); 1227 1.2 christos return NULL; 1228 1.2 christos } 1229 1.2 christos } 1230 1.2 christos 1231 1.2 christos return res; 1232 1.2 christos } 1233 1.2 christos #endif 1234 1.2 christos 1235 1.2 christos /* If the EVUTIL_AI_ADDRCONFIG flag is set on hints->ai_flags, and 1236 1.2 christos * hints->ai_family is PF_UNSPEC, then revise the value of hints->ai_family so 1237 1.2 christos * that we'll only get addresses we could maybe connect to. 1238 1.2 christos */ 1239 1.2 christos void 1240 1.6 christos evutil_adjust_hints_for_addrconfig_(struct evutil_addrinfo *hints) 1241 1.2 christos { 1242 1.2 christos if (!(hints->ai_flags & EVUTIL_AI_ADDRCONFIG)) 1243 1.2 christos return; 1244 1.2 christos if (hints->ai_family != PF_UNSPEC) 1245 1.2 christos return; 1246 1.7 christos evutil_check_interfaces(); 1247 1.2 christos if (had_ipv4_address && !had_ipv6_address) { 1248 1.2 christos hints->ai_family = PF_INET; 1249 1.2 christos } else if (!had_ipv4_address && had_ipv6_address) { 1250 1.2 christos hints->ai_family = PF_INET6; 1251 1.2 christos } 1252 1.2 christos } 1253 1.2 christos 1254 1.2 christos #ifdef USE_NATIVE_GETADDRINFO 1255 1.2 christos static int need_numeric_port_hack_=0; 1256 1.2 christos static int need_socktype_protocol_hack_=0; 1257 1.2 christos static int tested_for_getaddrinfo_hacks=0; 1258 1.2 christos 1259 1.2 christos /* Some older BSDs (like OpenBSD up to 4.6) used to believe that 1260 1.2 christos giving a numeric port without giving an ai_socktype was verboten. 1261 1.2 christos We test for this so we can apply an appropriate workaround. If it 1262 1.2 christos turns out that the bug is present, then: 1263 1.2 christos 1264 1.2 christos - If nodename==NULL and servname is numeric, we build an answer 1265 1.6 christos ourselves using evutil_getaddrinfo_common_(). 1266 1.2 christos 1267 1.2 christos - If nodename!=NULL and servname is numeric, then we set 1268 1.2 christos servname=NULL when calling getaddrinfo, and post-process the 1269 1.2 christos result to set the ports on it. 1270 1.2 christos 1271 1.2 christos We test for this bug at runtime, since otherwise we can't have the 1272 1.2 christos same binary run on multiple BSD versions. 1273 1.2 christos 1274 1.2 christos - Some versions of Solaris believe that it's nice to leave to protocol 1275 1.2 christos field set to 0. We test for this so we can apply an appropriate 1276 1.2 christos workaround. 1277 1.2 christos */ 1278 1.6 christos static struct evutil_addrinfo *ai_find_protocol(struct evutil_addrinfo *ai) 1279 1.6 christos { 1280 1.6 christos while (ai) { 1281 1.6 christos if (ai->ai_protocol) 1282 1.6 christos return ai; 1283 1.6 christos ai = ai->ai_next; 1284 1.6 christos } 1285 1.6 christos return NULL; 1286 1.6 christos } 1287 1.2 christos static void 1288 1.2 christos test_for_getaddrinfo_hacks(void) 1289 1.2 christos { 1290 1.2 christos int r, r2; 1291 1.6 christos struct evutil_addrinfo *ai=NULL, *ai2=NULL, *ai3=NULL; 1292 1.2 christos struct evutil_addrinfo hints; 1293 1.2 christos 1294 1.2 christos memset(&hints,0,sizeof(hints)); 1295 1.2 christos hints.ai_family = PF_UNSPEC; 1296 1.2 christos hints.ai_flags = 1297 1.2 christos #ifdef AI_NUMERICHOST 1298 1.2 christos AI_NUMERICHOST | 1299 1.2 christos #endif 1300 1.2 christos #ifdef AI_NUMERICSERV 1301 1.2 christos AI_NUMERICSERV | 1302 1.2 christos #endif 1303 1.2 christos 0; 1304 1.2 christos r = getaddrinfo("1.2.3.4", "80", &hints, &ai); 1305 1.6 christos getaddrinfo("1.2.3.4", NULL, &hints, &ai3); 1306 1.2 christos hints.ai_socktype = SOCK_STREAM; 1307 1.2 christos r2 = getaddrinfo("1.2.3.4", "80", &hints, &ai2); 1308 1.2 christos if (r2 == 0 && r != 0) { 1309 1.2 christos need_numeric_port_hack_=1; 1310 1.2 christos } 1311 1.6 christos if (!ai_find_protocol(ai2) || !ai_find_protocol(ai3)) { 1312 1.2 christos need_socktype_protocol_hack_=1; 1313 1.2 christos } 1314 1.2 christos 1315 1.2 christos if (ai) 1316 1.2 christos freeaddrinfo(ai); 1317 1.2 christos if (ai2) 1318 1.2 christos freeaddrinfo(ai2); 1319 1.6 christos if (ai3) 1320 1.6 christos freeaddrinfo(ai3); 1321 1.2 christos tested_for_getaddrinfo_hacks=1; 1322 1.2 christos } 1323 1.2 christos 1324 1.2 christos static inline int 1325 1.2 christos need_numeric_port_hack(void) 1326 1.2 christos { 1327 1.2 christos if (!tested_for_getaddrinfo_hacks) 1328 1.2 christos test_for_getaddrinfo_hacks(); 1329 1.2 christos return need_numeric_port_hack_; 1330 1.2 christos } 1331 1.2 christos 1332 1.2 christos static inline int 1333 1.2 christos need_socktype_protocol_hack(void) 1334 1.2 christos { 1335 1.2 christos if (!tested_for_getaddrinfo_hacks) 1336 1.2 christos test_for_getaddrinfo_hacks(); 1337 1.2 christos return need_socktype_protocol_hack_; 1338 1.2 christos } 1339 1.2 christos 1340 1.2 christos static void 1341 1.2 christos apply_numeric_port_hack(int port, struct evutil_addrinfo **ai) 1342 1.2 christos { 1343 1.2 christos /* Now we run through the list and set the ports on all of the 1344 1.2 christos * results where ports would make sense. */ 1345 1.2 christos for ( ; *ai; ai = &(*ai)->ai_next) { 1346 1.2 christos struct sockaddr *sa = (*ai)->ai_addr; 1347 1.2 christos if (sa && sa->sa_family == AF_INET) { 1348 1.2 christos struct sockaddr_in *sin = (struct sockaddr_in*)sa; 1349 1.2 christos sin->sin_port = htons(port); 1350 1.2 christos } else if (sa && sa->sa_family == AF_INET6) { 1351 1.2 christos struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa; 1352 1.2 christos sin6->sin6_port = htons(port); 1353 1.2 christos } else { 1354 1.2 christos /* A numeric port makes no sense here; remove this one 1355 1.2 christos * from the list. */ 1356 1.2 christos struct evutil_addrinfo *victim = *ai; 1357 1.2 christos *ai = victim->ai_next; 1358 1.2 christos victim->ai_next = NULL; 1359 1.2 christos freeaddrinfo(victim); 1360 1.2 christos } 1361 1.2 christos } 1362 1.2 christos } 1363 1.2 christos 1364 1.2 christos static int 1365 1.2 christos apply_socktype_protocol_hack(struct evutil_addrinfo *ai) 1366 1.2 christos { 1367 1.2 christos struct evutil_addrinfo *ai_new; 1368 1.2 christos for (; ai; ai = ai->ai_next) { 1369 1.2 christos evutil_getaddrinfo_infer_protocols(ai); 1370 1.2 christos if (ai->ai_socktype || ai->ai_protocol) 1371 1.2 christos continue; 1372 1.2 christos ai_new = mm_malloc(sizeof(*ai_new)); 1373 1.2 christos if (!ai_new) 1374 1.2 christos return -1; 1375 1.2 christos memcpy(ai_new, ai, sizeof(*ai_new)); 1376 1.2 christos ai->ai_socktype = SOCK_STREAM; 1377 1.2 christos ai->ai_protocol = IPPROTO_TCP; 1378 1.2 christos ai_new->ai_socktype = SOCK_DGRAM; 1379 1.2 christos ai_new->ai_protocol = IPPROTO_UDP; 1380 1.2 christos 1381 1.2 christos ai_new->ai_next = ai->ai_next; 1382 1.2 christos ai->ai_next = ai_new; 1383 1.2 christos } 1384 1.2 christos return 0; 1385 1.2 christos } 1386 1.2 christos #endif 1387 1.2 christos 1388 1.2 christos int 1389 1.2 christos evutil_getaddrinfo(const char *nodename, const char *servname, 1390 1.2 christos const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res) 1391 1.2 christos { 1392 1.2 christos #ifdef USE_NATIVE_GETADDRINFO 1393 1.2 christos struct evutil_addrinfo hints; 1394 1.2 christos int portnum=-1, need_np_hack, err; 1395 1.2 christos 1396 1.2 christos if (hints_in) { 1397 1.2 christos memcpy(&hints, hints_in, sizeof(hints)); 1398 1.2 christos } else { 1399 1.2 christos memset(&hints, 0, sizeof(hints)); 1400 1.2 christos hints.ai_family = PF_UNSPEC; 1401 1.2 christos } 1402 1.2 christos 1403 1.2 christos #ifndef AI_ADDRCONFIG 1404 1.2 christos /* Not every system has AI_ADDRCONFIG, so fake it. */ 1405 1.2 christos if (hints.ai_family == PF_UNSPEC && 1406 1.2 christos (hints.ai_flags & EVUTIL_AI_ADDRCONFIG)) { 1407 1.6 christos evutil_adjust_hints_for_addrconfig_(&hints); 1408 1.2 christos } 1409 1.2 christos #endif 1410 1.2 christos 1411 1.2 christos #ifndef AI_NUMERICSERV 1412 1.2 christos /* Not every system has AI_NUMERICSERV, so fake it. */ 1413 1.2 christos if (hints.ai_flags & EVUTIL_AI_NUMERICSERV) { 1414 1.2 christos if (servname && parse_numeric_servname(servname)<0) 1415 1.2 christos return EVUTIL_EAI_NONAME; 1416 1.2 christos } 1417 1.2 christos #endif 1418 1.2 christos 1419 1.2 christos /* Enough operating systems handle enough common non-resolve 1420 1.2 christos * cases here weirdly enough that we are better off just 1421 1.2 christos * overriding them. For example: 1422 1.2 christos * 1423 1.2 christos * - Windows doesn't like to infer the protocol from the 1424 1.2 christos * socket type, or fill in socket or protocol types much at 1425 1.2 christos * all. It also seems to do its own broken implicit 1426 1.2 christos * always-on version of AI_ADDRCONFIG that keeps it from 1427 1.2 christos * ever resolving even a literal IPv6 address when 1428 1.2 christos * ai_addrtype is PF_UNSPEC. 1429 1.2 christos */ 1430 1.6 christos #ifdef _WIN32 1431 1.2 christos { 1432 1.2 christos int tmp_port; 1433 1.6 christos err = evutil_getaddrinfo_common_(nodename,servname,&hints, 1434 1.2 christos res, &tmp_port); 1435 1.2 christos if (err == 0 || 1436 1.2 christos err == EVUTIL_EAI_MEMORY || 1437 1.2 christos err == EVUTIL_EAI_NONAME) 1438 1.2 christos return err; 1439 1.2 christos /* If we make it here, the system getaddrinfo can 1440 1.2 christos * have a crack at it. */ 1441 1.2 christos } 1442 1.2 christos #endif 1443 1.2 christos 1444 1.2 christos /* See documentation for need_numeric_port_hack above.*/ 1445 1.2 christos need_np_hack = need_numeric_port_hack() && servname && !hints.ai_socktype 1446 1.2 christos && ((portnum=parse_numeric_servname(servname)) >= 0); 1447 1.2 christos if (need_np_hack) { 1448 1.2 christos if (!nodename) 1449 1.6 christos return evutil_getaddrinfo_common_( 1450 1.2 christos NULL,servname,&hints, res, &portnum); 1451 1.2 christos servname = NULL; 1452 1.2 christos } 1453 1.2 christos 1454 1.2 christos if (need_socktype_protocol_hack()) { 1455 1.2 christos evutil_getaddrinfo_infer_protocols(&hints); 1456 1.2 christos } 1457 1.2 christos 1458 1.2 christos /* Make sure that we didn't actually steal any AI_FLAGS values that 1459 1.2 christos * the system is using. (This is a constant expression, and should ge 1460 1.2 christos * optimized out.) 1461 1.2 christos * 1462 1.2 christos * XXXX Turn this into a compile-time failure rather than a run-time 1463 1.2 christos * failure. 1464 1.2 christos */ 1465 1.2 christos EVUTIL_ASSERT((ALL_NONNATIVE_AI_FLAGS & ALL_NATIVE_AI_FLAGS) == 0); 1466 1.2 christos 1467 1.2 christos /* Clear any flags that only libevent understands. */ 1468 1.2 christos hints.ai_flags &= ~ALL_NONNATIVE_AI_FLAGS; 1469 1.2 christos 1470 1.2 christos err = getaddrinfo(nodename, servname, &hints, res); 1471 1.2 christos if (need_np_hack) 1472 1.2 christos apply_numeric_port_hack(portnum, res); 1473 1.2 christos 1474 1.2 christos if (need_socktype_protocol_hack()) { 1475 1.2 christos if (apply_socktype_protocol_hack(*res) < 0) { 1476 1.2 christos evutil_freeaddrinfo(*res); 1477 1.2 christos *res = NULL; 1478 1.2 christos return EVUTIL_EAI_MEMORY; 1479 1.2 christos } 1480 1.2 christos } 1481 1.2 christos return err; 1482 1.2 christos #else 1483 1.2 christos int port=0, err; 1484 1.2 christos struct hostent *ent = NULL; 1485 1.2 christos struct evutil_addrinfo hints; 1486 1.2 christos 1487 1.2 christos if (hints_in) { 1488 1.2 christos memcpy(&hints, hints_in, sizeof(hints)); 1489 1.2 christos } else { 1490 1.2 christos memset(&hints, 0, sizeof(hints)); 1491 1.2 christos hints.ai_family = PF_UNSPEC; 1492 1.2 christos } 1493 1.2 christos 1494 1.6 christos evutil_adjust_hints_for_addrconfig_(&hints); 1495 1.2 christos 1496 1.6 christos err = evutil_getaddrinfo_common_(nodename, servname, &hints, res, &port); 1497 1.2 christos if (err != EVUTIL_EAI_NEED_RESOLVE) { 1498 1.2 christos /* We either succeeded or failed. No need to continue */ 1499 1.2 christos return err; 1500 1.2 christos } 1501 1.2 christos 1502 1.2 christos err = 0; 1503 1.2 christos /* Use any of the various gethostbyname_r variants as available. */ 1504 1.2 christos { 1505 1.6 christos #ifdef EVENT__HAVE_GETHOSTBYNAME_R_6_ARG 1506 1.2 christos /* This one is what glibc provides. */ 1507 1.2 christos char buf[2048]; 1508 1.2 christos struct hostent hostent; 1509 1.2 christos int r; 1510 1.2 christos r = gethostbyname_r(nodename, &hostent, buf, sizeof(buf), &ent, 1511 1.2 christos &err); 1512 1.6 christos #elif defined(EVENT__HAVE_GETHOSTBYNAME_R_5_ARG) 1513 1.2 christos char buf[2048]; 1514 1.2 christos struct hostent hostent; 1515 1.2 christos ent = gethostbyname_r(nodename, &hostent, buf, sizeof(buf), 1516 1.2 christos &err); 1517 1.6 christos #elif defined(EVENT__HAVE_GETHOSTBYNAME_R_3_ARG) 1518 1.2 christos struct hostent_data data; 1519 1.2 christos struct hostent hostent; 1520 1.2 christos memset(&data, 0, sizeof(data)); 1521 1.2 christos err = gethostbyname_r(nodename, &hostent, &data); 1522 1.2 christos ent = err ? NULL : &hostent; 1523 1.2 christos #else 1524 1.2 christos /* fall back to gethostbyname. */ 1525 1.2 christos /* XXXX This needs a lock everywhere but Windows. */ 1526 1.2 christos ent = gethostbyname(nodename); 1527 1.6 christos #ifdef _WIN32 1528 1.2 christos err = WSAGetLastError(); 1529 1.2 christos #else 1530 1.2 christos err = h_errno; 1531 1.2 christos #endif 1532 1.2 christos #endif 1533 1.2 christos 1534 1.2 christos /* Now we have either ent or err set. */ 1535 1.2 christos if (!ent) { 1536 1.2 christos /* XXX is this right for windows ? */ 1537 1.2 christos switch (err) { 1538 1.2 christos case TRY_AGAIN: 1539 1.2 christos return EVUTIL_EAI_AGAIN; 1540 1.2 christos case NO_RECOVERY: 1541 1.2 christos default: 1542 1.2 christos return EVUTIL_EAI_FAIL; 1543 1.2 christos case HOST_NOT_FOUND: 1544 1.2 christos return EVUTIL_EAI_NONAME; 1545 1.2 christos case NO_ADDRESS: 1546 1.2 christos #if NO_DATA != NO_ADDRESS 1547 1.2 christos case NO_DATA: 1548 1.2 christos #endif 1549 1.2 christos return EVUTIL_EAI_NODATA; 1550 1.2 christos } 1551 1.2 christos } 1552 1.2 christos 1553 1.2 christos if (ent->h_addrtype != hints.ai_family && 1554 1.2 christos hints.ai_family != PF_UNSPEC) { 1555 1.2 christos /* This wasn't the type we were hoping for. Too bad 1556 1.2 christos * we never had a chance to ask gethostbyname for what 1557 1.2 christos * we wanted. */ 1558 1.2 christos return EVUTIL_EAI_NONAME; 1559 1.2 christos } 1560 1.2 christos 1561 1.2 christos /* Make sure we got _some_ answers. */ 1562 1.2 christos if (ent->h_length == 0) 1563 1.2 christos return EVUTIL_EAI_NODATA; 1564 1.2 christos 1565 1.2 christos /* If we got an address type we don't know how to make a 1566 1.2 christos sockaddr for, give up. */ 1567 1.2 christos if (ent->h_addrtype != PF_INET && ent->h_addrtype != PF_INET6) 1568 1.2 christos return EVUTIL_EAI_FAMILY; 1569 1.2 christos 1570 1.2 christos *res = addrinfo_from_hostent(ent, port, &hints); 1571 1.2 christos if (! *res) 1572 1.2 christos return EVUTIL_EAI_MEMORY; 1573 1.2 christos } 1574 1.2 christos 1575 1.2 christos return 0; 1576 1.2 christos #endif 1577 1.2 christos } 1578 1.2 christos 1579 1.2 christos void 1580 1.2 christos evutil_freeaddrinfo(struct evutil_addrinfo *ai) 1581 1.2 christos { 1582 1.6 christos #ifdef EVENT__HAVE_GETADDRINFO 1583 1.2 christos if (!(ai->ai_flags & EVUTIL_AI_LIBEVENT_ALLOCATED)) { 1584 1.2 christos freeaddrinfo(ai); 1585 1.2 christos return; 1586 1.2 christos } 1587 1.2 christos #endif 1588 1.2 christos while (ai) { 1589 1.2 christos struct evutil_addrinfo *next = ai->ai_next; 1590 1.2 christos if (ai->ai_canonname) 1591 1.2 christos mm_free(ai->ai_canonname); 1592 1.2 christos mm_free(ai); 1593 1.2 christos ai = next; 1594 1.2 christos } 1595 1.2 christos } 1596 1.2 christos 1597 1.2 christos static evdns_getaddrinfo_fn evdns_getaddrinfo_impl = NULL; 1598 1.6 christos static evdns_getaddrinfo_cancel_fn evdns_getaddrinfo_cancel_impl = NULL; 1599 1.2 christos 1600 1.2 christos void 1601 1.6 christos evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn) 1602 1.2 christos { 1603 1.2 christos if (!evdns_getaddrinfo_impl) 1604 1.2 christos evdns_getaddrinfo_impl = fn; 1605 1.2 christos } 1606 1.6 christos void 1607 1.6 christos evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel_fn fn) 1608 1.6 christos { 1609 1.6 christos if (!evdns_getaddrinfo_cancel_impl) 1610 1.6 christos evdns_getaddrinfo_cancel_impl = fn; 1611 1.6 christos } 1612 1.2 christos 1613 1.2 christos /* Internal helper function: act like evdns_getaddrinfo if dns_base is set; 1614 1.2 christos * otherwise do a blocking resolve and pass the result to the callback in the 1615 1.2 christos * way that evdns_getaddrinfo would. 1616 1.2 christos */ 1617 1.6 christos struct evdns_getaddrinfo_request *evutil_getaddrinfo_async_( 1618 1.6 christos struct evdns_base *dns_base, 1619 1.2 christos const char *nodename, const char *servname, 1620 1.2 christos const struct evutil_addrinfo *hints_in, 1621 1.2 christos void (*cb)(int, struct evutil_addrinfo *, void *), void *arg) 1622 1.2 christos { 1623 1.2 christos if (dns_base && evdns_getaddrinfo_impl) { 1624 1.6 christos return evdns_getaddrinfo_impl( 1625 1.2 christos dns_base, nodename, servname, hints_in, cb, arg); 1626 1.2 christos } else { 1627 1.2 christos struct evutil_addrinfo *ai=NULL; 1628 1.2 christos int err; 1629 1.2 christos err = evutil_getaddrinfo(nodename, servname, hints_in, &ai); 1630 1.2 christos cb(err, ai, arg); 1631 1.6 christos return NULL; 1632 1.6 christos } 1633 1.6 christos } 1634 1.6 christos 1635 1.6 christos void evutil_getaddrinfo_cancel_async_(struct evdns_getaddrinfo_request *data) 1636 1.6 christos { 1637 1.6 christos if (evdns_getaddrinfo_cancel_impl && data) { 1638 1.6 christos evdns_getaddrinfo_cancel_impl(data); 1639 1.2 christos } 1640 1.2 christos } 1641 1.2 christos 1642 1.2 christos const char * 1643 1.2 christos evutil_gai_strerror(int err) 1644 1.2 christos { 1645 1.2 christos /* As a sneaky side-benefit, this case statement will get most 1646 1.2 christos * compilers to tell us if any of the error codes we defined 1647 1.2 christos * conflict with the platform's native error codes. */ 1648 1.2 christos switch (err) { 1649 1.2 christos case EVUTIL_EAI_CANCEL: 1650 1.2 christos return "Request canceled"; 1651 1.2 christos case 0: 1652 1.2 christos return "No error"; 1653 1.2 christos 1654 1.2 christos case EVUTIL_EAI_ADDRFAMILY: 1655 1.2 christos return "address family for nodename not supported"; 1656 1.2 christos case EVUTIL_EAI_AGAIN: 1657 1.2 christos return "temporary failure in name resolution"; 1658 1.2 christos case EVUTIL_EAI_BADFLAGS: 1659 1.2 christos return "invalid value for ai_flags"; 1660 1.2 christos case EVUTIL_EAI_FAIL: 1661 1.2 christos return "non-recoverable failure in name resolution"; 1662 1.2 christos case EVUTIL_EAI_FAMILY: 1663 1.2 christos return "ai_family not supported"; 1664 1.2 christos case EVUTIL_EAI_MEMORY: 1665 1.2 christos return "memory allocation failure"; 1666 1.2 christos case EVUTIL_EAI_NODATA: 1667 1.2 christos return "no address associated with nodename"; 1668 1.2 christos case EVUTIL_EAI_NONAME: 1669 1.2 christos return "nodename nor servname provided, or not known"; 1670 1.2 christos case EVUTIL_EAI_SERVICE: 1671 1.2 christos return "servname not supported for ai_socktype"; 1672 1.2 christos case EVUTIL_EAI_SOCKTYPE: 1673 1.2 christos return "ai_socktype not supported"; 1674 1.2 christos case EVUTIL_EAI_SYSTEM: 1675 1.2 christos return "system error"; 1676 1.2 christos default: 1677 1.6 christos #if defined(USE_NATIVE_GETADDRINFO) && defined(_WIN32) 1678 1.2 christos return gai_strerrorA(err); 1679 1.2 christos #elif defined(USE_NATIVE_GETADDRINFO) 1680 1.2 christos return gai_strerror(err); 1681 1.2 christos #else 1682 1.2 christos return "Unknown error code"; 1683 1.2 christos #endif 1684 1.2 christos } 1685 1.2 christos } 1686 1.2 christos 1687 1.6 christos #ifdef _WIN32 1688 1.6 christos /* destructively remove a trailing line terminator from s */ 1689 1.6 christos static void 1690 1.6 christos chomp (char *s) 1691 1.6 christos { 1692 1.6 christos size_t len; 1693 1.6 christos if (s && (len = strlen (s)) > 0 && s[len - 1] == '\n') { 1694 1.6 christos s[--len] = 0; 1695 1.6 christos if (len > 0 && s[len - 1] == '\r') 1696 1.6 christos s[--len] = 0; 1697 1.6 christos } 1698 1.6 christos } 1699 1.6 christos 1700 1.6 christos /* FormatMessage returns allocated strings, but evutil_socket_error_to_string 1701 1.6 christos * is supposed to return a string which is good indefinitely without having 1702 1.6 christos * to be freed. To make this work without leaking memory, we cache the 1703 1.6 christos * string the first time FormatMessage is called on a particular error 1704 1.6 christos * code, and then return the cached string on subsequent calls with the 1705 1.6 christos * same code. The strings aren't freed until libevent_global_shutdown 1706 1.6 christos * (or never). We use a linked list to cache the errors, because we 1707 1.6 christos * only expect there to be a few dozen, and that should be fast enough. 1708 1.6 christos */ 1709 1.6 christos 1710 1.6 christos struct cached_sock_errs_entry { 1711 1.6 christos HT_ENTRY(cached_sock_errs_entry) node; 1712 1.6 christos DWORD code; 1713 1.6 christos char *msg; /* allocated with LocalAlloc; free with LocalFree */ 1714 1.2 christos }; 1715 1.6 christos 1716 1.6 christos static inline unsigned 1717 1.6 christos hash_cached_sock_errs(const struct cached_sock_errs_entry *e) 1718 1.6 christos { 1719 1.6 christos /* Use Murmur3's 32-bit finalizer as an integer hash function */ 1720 1.6 christos DWORD h = e->code; 1721 1.6 christos h ^= h >> 16; 1722 1.6 christos h *= 0x85ebca6b; 1723 1.6 christos h ^= h >> 13; 1724 1.6 christos h *= 0xc2b2ae35; 1725 1.6 christos h ^= h >> 16; 1726 1.6 christos return h; 1727 1.6 christos } 1728 1.6 christos 1729 1.6 christos static inline int 1730 1.6 christos eq_cached_sock_errs(const struct cached_sock_errs_entry *a, 1731 1.6 christos const struct cached_sock_errs_entry *b) 1732 1.6 christos { 1733 1.6 christos return a->code == b->code; 1734 1.6 christos } 1735 1.6 christos 1736 1.6 christos #ifndef EVENT__DISABLE_THREAD_SUPPORT 1737 1.6 christos static void *windows_socket_errors_lock_ = NULL; 1738 1.6 christos #endif 1739 1.6 christos 1740 1.6 christos static HT_HEAD(cached_sock_errs_map, cached_sock_errs_entry) 1741 1.6 christos windows_socket_errors = HT_INITIALIZER(); 1742 1.6 christos 1743 1.6 christos HT_PROTOTYPE(cached_sock_errs_map, 1744 1.6 christos cached_sock_errs_entry, 1745 1.6 christos node, 1746 1.6 christos hash_cached_sock_errs, 1747 1.6 christos eq_cached_sock_errs); 1748 1.6 christos 1749 1.6 christos HT_GENERATE(cached_sock_errs_map, 1750 1.6 christos cached_sock_errs_entry, 1751 1.6 christos node, 1752 1.6 christos hash_cached_sock_errs, 1753 1.6 christos eq_cached_sock_errs, 1754 1.6 christos 0.5, 1755 1.6 christos mm_malloc, 1756 1.6 christos mm_realloc, 1757 1.6 christos mm_free); 1758 1.6 christos 1759 1.2 christos /** Equivalent to strerror, but for windows socket errors. */ 1760 1.2 christos const char * 1761 1.2 christos evutil_socket_error_to_string(int errcode) 1762 1.2 christos { 1763 1.6 christos struct cached_sock_errs_entry *errs, *newerr, find; 1764 1.6 christos char *msg = NULL; 1765 1.6 christos 1766 1.6 christos EVLOCK_LOCK(windows_socket_errors_lock_, 0); 1767 1.6 christos 1768 1.6 christos find.code = errcode; 1769 1.6 christos errs = HT_FIND(cached_sock_errs_map, &windows_socket_errors, &find); 1770 1.6 christos if (errs) { 1771 1.6 christos msg = errs->msg; 1772 1.6 christos goto done; 1773 1.6 christos } 1774 1.6 christos 1775 1.6 christos if (0 != FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | 1776 1.6 christos FORMAT_MESSAGE_IGNORE_INSERTS | 1777 1.6 christos FORMAT_MESSAGE_ALLOCATE_BUFFER, 1778 1.6 christos NULL, errcode, 0, (char *)&msg, 0, NULL)) 1779 1.6 christos chomp (msg); /* because message has trailing newline */ 1780 1.6 christos else { 1781 1.6 christos size_t len = 50; 1782 1.6 christos /* use LocalAlloc because FormatMessage does */ 1783 1.6 christos msg = LocalAlloc(LMEM_FIXED, len); 1784 1.6 christos if (!msg) { 1785 1.6 christos msg = (char *)"LocalAlloc failed during Winsock error"; 1786 1.6 christos goto done; 1787 1.6 christos } 1788 1.6 christos evutil_snprintf(msg, len, "winsock error 0x%08x", errcode); 1789 1.6 christos } 1790 1.6 christos 1791 1.6 christos newerr = (struct cached_sock_errs_entry *) 1792 1.6 christos mm_malloc(sizeof (struct cached_sock_errs_entry)); 1793 1.6 christos 1794 1.6 christos if (!newerr) { 1795 1.6 christos LocalFree(msg); 1796 1.6 christos msg = (char *)"malloc failed during Winsock error"; 1797 1.6 christos goto done; 1798 1.6 christos } 1799 1.6 christos 1800 1.6 christos newerr->code = errcode; 1801 1.6 christos newerr->msg = msg; 1802 1.6 christos HT_INSERT(cached_sock_errs_map, &windows_socket_errors, newerr); 1803 1.6 christos 1804 1.6 christos done: 1805 1.6 christos EVLOCK_UNLOCK(windows_socket_errors_lock_, 0); 1806 1.6 christos 1807 1.6 christos return msg; 1808 1.6 christos } 1809 1.6 christos 1810 1.6 christos #ifndef EVENT__DISABLE_THREAD_SUPPORT 1811 1.6 christos int 1812 1.6 christos evutil_global_setup_locks_(const int enable_locks) 1813 1.6 christos { 1814 1.6 christos EVTHREAD_SETUP_GLOBAL_LOCK(windows_socket_errors_lock_, 0); 1815 1.6 christos return 0; 1816 1.2 christos } 1817 1.2 christos #endif 1818 1.2 christos 1819 1.6 christos static void 1820 1.6 christos evutil_free_sock_err_globals(void) 1821 1.6 christos { 1822 1.6 christos struct cached_sock_errs_entry **errs, *tofree; 1823 1.6 christos 1824 1.6 christos for (errs = HT_START(cached_sock_errs_map, &windows_socket_errors) 1825 1.6 christos ; errs; ) { 1826 1.6 christos tofree = *errs; 1827 1.6 christos errs = HT_NEXT_RMV(cached_sock_errs_map, 1828 1.6 christos &windows_socket_errors, 1829 1.6 christos errs); 1830 1.6 christos LocalFree(tofree->msg); 1831 1.6 christos mm_free(tofree); 1832 1.6 christos } 1833 1.6 christos 1834 1.6 christos HT_CLEAR(cached_sock_errs_map, &windows_socket_errors); 1835 1.6 christos 1836 1.6 christos #ifndef EVENT__DISABLE_THREAD_SUPPORT 1837 1.6 christos if (windows_socket_errors_lock_ != NULL) { 1838 1.6 christos EVTHREAD_FREE_LOCK(windows_socket_errors_lock_, 0); 1839 1.6 christos windows_socket_errors_lock_ = NULL; 1840 1.6 christos } 1841 1.6 christos #endif 1842 1.6 christos } 1843 1.6 christos 1844 1.6 christos #else 1845 1.6 christos 1846 1.6 christos #ifndef EVENT__DISABLE_THREAD_SUPPORT 1847 1.6 christos int 1848 1.6 christos evutil_global_setup_locks_(const int enable_locks) 1849 1.6 christos { 1850 1.6 christos return 0; 1851 1.6 christos } 1852 1.6 christos #endif 1853 1.6 christos 1854 1.6 christos static void 1855 1.6 christos evutil_free_sock_err_globals(void) 1856 1.6 christos { 1857 1.6 christos } 1858 1.6 christos 1859 1.6 christos #endif 1860 1.6 christos 1861 1.1 plunky int 1862 1.1 plunky evutil_snprintf(char *buf, size_t buflen, const char *format, ...) 1863 1.1 plunky { 1864 1.1 plunky int r; 1865 1.1 plunky va_list ap; 1866 1.1 plunky va_start(ap, format); 1867 1.1 plunky r = evutil_vsnprintf(buf, buflen, format, ap); 1868 1.1 plunky va_end(ap); 1869 1.1 plunky return r; 1870 1.1 plunky } 1871 1.1 plunky 1872 1.1 plunky int 1873 1.1 plunky evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap) 1874 1.1 plunky { 1875 1.2 christos int r; 1876 1.2 christos if (!buflen) 1877 1.2 christos return 0; 1878 1.6 christos #if defined(_MSC_VER) || defined(_WIN32) 1879 1.2 christos r = _vsnprintf(buf, buflen, format, ap); 1880 1.2 christos if (r < 0) 1881 1.2 christos r = _vscprintf(format, ap); 1882 1.2 christos #elif defined(sgi) 1883 1.2 christos /* Make sure we always use the correct vsnprintf on IRIX */ 1884 1.2 christos extern int _xpg5_vsnprintf(char * __restrict, 1885 1.2 christos __SGI_LIBC_NAMESPACE_QUALIFIER size_t, 1886 1.2 christos const char * __restrict, /* va_list */ char *); 1887 1.2 christos 1888 1.2 christos r = _xpg5_vsnprintf(buf, buflen, format, ap); 1889 1.2 christos #else 1890 1.2 christos r = vsnprintf(buf, buflen, format, ap); 1891 1.2 christos #endif 1892 1.1 plunky buf[buflen-1] = '\0'; 1893 1.2 christos return r; 1894 1.2 christos } 1895 1.2 christos 1896 1.2 christos #define USE_INTERNAL_NTOP 1897 1.2 christos #define USE_INTERNAL_PTON 1898 1.2 christos 1899 1.2 christos const char * 1900 1.2 christos evutil_inet_ntop(int af, const void *src, char *dst, size_t len) 1901 1.2 christos { 1902 1.6 christos #if defined(EVENT__HAVE_INET_NTOP) && !defined(USE_INTERNAL_NTOP) 1903 1.2 christos return inet_ntop(af, src, dst, len); 1904 1.2 christos #else 1905 1.2 christos if (af == AF_INET) { 1906 1.2 christos const struct in_addr *in = src; 1907 1.2 christos const ev_uint32_t a = ntohl(in->s_addr); 1908 1.2 christos int r; 1909 1.2 christos r = evutil_snprintf(dst, len, "%d.%d.%d.%d", 1910 1.2 christos (int)(ev_uint8_t)((a>>24)&0xff), 1911 1.2 christos (int)(ev_uint8_t)((a>>16)&0xff), 1912 1.2 christos (int)(ev_uint8_t)((a>>8 )&0xff), 1913 1.2 christos (int)(ev_uint8_t)((a )&0xff)); 1914 1.2 christos if (r<0||(size_t)r>=len) 1915 1.2 christos return NULL; 1916 1.2 christos else 1917 1.2 christos return dst; 1918 1.2 christos #ifdef AF_INET6 1919 1.2 christos } else if (af == AF_INET6) { 1920 1.2 christos const struct in6_addr *addr = src; 1921 1.2 christos char buf[64], *cp; 1922 1.2 christos int longestGapLen = 0, longestGapPos = -1, i, 1923 1.2 christos curGapPos = -1, curGapLen = 0; 1924 1.2 christos ev_uint16_t words[8]; 1925 1.2 christos for (i = 0; i < 8; ++i) { 1926 1.2 christos words[i] = 1927 1.2 christos (((ev_uint16_t)addr->s6_addr[2*i])<<8) + addr->s6_addr[2*i+1]; 1928 1.2 christos } 1929 1.2 christos if (words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && 1930 1.2 christos words[4] == 0 && ((words[5] == 0 && words[6] && words[7]) || 1931 1.2 christos (words[5] == 0xffff))) { 1932 1.2 christos /* This is an IPv4 address. */ 1933 1.2 christos if (words[5] == 0) { 1934 1.2 christos evutil_snprintf(buf, sizeof(buf), "::%d.%d.%d.%d", 1935 1.2 christos addr->s6_addr[12], addr->s6_addr[13], 1936 1.2 christos addr->s6_addr[14], addr->s6_addr[15]); 1937 1.2 christos } else { 1938 1.2 christos evutil_snprintf(buf, sizeof(buf), "::%x:%d.%d.%d.%d", words[5], 1939 1.2 christos addr->s6_addr[12], addr->s6_addr[13], 1940 1.2 christos addr->s6_addr[14], addr->s6_addr[15]); 1941 1.2 christos } 1942 1.2 christos if (strlen(buf) > len) 1943 1.2 christos return NULL; 1944 1.2 christos strlcpy(dst, buf, len); 1945 1.2 christos return dst; 1946 1.2 christos } 1947 1.2 christos i = 0; 1948 1.2 christos while (i < 8) { 1949 1.2 christos if (words[i] == 0) { 1950 1.2 christos curGapPos = i++; 1951 1.2 christos curGapLen = 1; 1952 1.2 christos while (i<8 && words[i] == 0) { 1953 1.2 christos ++i; ++curGapLen; 1954 1.2 christos } 1955 1.2 christos if (curGapLen > longestGapLen) { 1956 1.2 christos longestGapPos = curGapPos; 1957 1.2 christos longestGapLen = curGapLen; 1958 1.2 christos } 1959 1.2 christos } else { 1960 1.2 christos ++i; 1961 1.2 christos } 1962 1.2 christos } 1963 1.2 christos if (longestGapLen<=1) 1964 1.2 christos longestGapPos = -1; 1965 1.2 christos 1966 1.2 christos cp = buf; 1967 1.2 christos for (i = 0; i < 8; ++i) { 1968 1.2 christos if (words[i] == 0 && longestGapPos == i) { 1969 1.2 christos if (i == 0) 1970 1.2 christos *cp++ = ':'; 1971 1.2 christos *cp++ = ':'; 1972 1.2 christos while (i < 8 && words[i] == 0) 1973 1.2 christos ++i; 1974 1.2 christos --i; /* to compensate for loop increment. */ 1975 1.2 christos } else { 1976 1.2 christos evutil_snprintf(cp, 1977 1.2 christos sizeof(buf)-(cp-buf), "%x", (unsigned)words[i]); 1978 1.2 christos cp += strlen(cp); 1979 1.2 christos if (i != 7) 1980 1.2 christos *cp++ = ':'; 1981 1.2 christos } 1982 1.2 christos } 1983 1.2 christos *cp = '\0'; 1984 1.2 christos if (strlen(buf) > len) 1985 1.2 christos return NULL; 1986 1.2 christos strlcpy(dst, buf, len); 1987 1.2 christos return dst; 1988 1.2 christos #endif 1989 1.2 christos } else { 1990 1.2 christos return NULL; 1991 1.2 christos } 1992 1.2 christos #endif 1993 1.2 christos } 1994 1.2 christos 1995 1.2 christos int 1996 1.7 christos evutil_inet_pton_scope(int af, const char *src, void *dst, unsigned *indexp) 1997 1.7 christos { 1998 1.7 christos int r; 1999 1.7 christos unsigned if_index; 2000 1.7 christos char *check, *cp, *tmp_src; 2001 1.7 christos 2002 1.7 christos *indexp = 0; /* Reasonable default */ 2003 1.7 christos 2004 1.7 christos /* Bail out if not IPv6 */ 2005 1.7 christos if (af != AF_INET6) 2006 1.7 christos return evutil_inet_pton(af, src, dst); 2007 1.7 christos 2008 1.7 christos cp = strchr(src, '%'); 2009 1.7 christos 2010 1.7 christos /* Bail out if no zone ID */ 2011 1.7 christos if (cp == NULL) 2012 1.7 christos return evutil_inet_pton(af, src, dst); 2013 1.7 christos 2014 1.7 christos if_index = if_nametoindex(cp + 1); 2015 1.7 christos if (if_index == 0) { 2016 1.7 christos /* Could be numeric */ 2017 1.7 christos if_index = strtoul(cp + 1, &check, 10); 2018 1.7 christos if (check[0] != '\0') 2019 1.7 christos return 0; 2020 1.7 christos } 2021 1.7 christos *indexp = if_index; 2022 1.7 christos tmp_src = mm_strdup(src); 2023 1.7 christos cp = strchr(tmp_src, '%'); 2024 1.7 christos *cp = '\0'; 2025 1.7 christos r = evutil_inet_pton(af, tmp_src, dst); 2026 1.7 christos free(tmp_src); 2027 1.7 christos return r; 2028 1.7 christos } 2029 1.7 christos 2030 1.7 christos int 2031 1.2 christos evutil_inet_pton(int af, const char *src, void *dst) 2032 1.2 christos { 2033 1.6 christos #if defined(EVENT__HAVE_INET_PTON) && !defined(USE_INTERNAL_PTON) 2034 1.2 christos return inet_pton(af, src, dst); 2035 1.2 christos #else 2036 1.2 christos if (af == AF_INET) { 2037 1.6 christos unsigned a,b,c,d; 2038 1.2 christos char more; 2039 1.2 christos struct in_addr *addr = dst; 2040 1.6 christos if (sscanf(src, "%u.%u.%u.%u%c", &a,&b,&c,&d,&more) != 4) 2041 1.2 christos return 0; 2042 1.6 christos if (a > 255) return 0; 2043 1.6 christos if (b > 255) return 0; 2044 1.6 christos if (c > 255) return 0; 2045 1.6 christos if (d > 255) return 0; 2046 1.2 christos addr->s_addr = htonl((a<<24) | (b<<16) | (c<<8) | d); 2047 1.2 christos return 1; 2048 1.2 christos #ifdef AF_INET6 2049 1.2 christos } else if (af == AF_INET6) { 2050 1.2 christos struct in6_addr *out = dst; 2051 1.2 christos ev_uint16_t words[8]; 2052 1.2 christos int gapPos = -1, i, setWords=0; 2053 1.2 christos const char *dot = strchr(src, '.'); 2054 1.2 christos const char *eow; /* end of words. */ 2055 1.2 christos if (dot == src) 2056 1.2 christos return 0; 2057 1.2 christos else if (!dot) 2058 1.2 christos eow = src+strlen(src); 2059 1.2 christos else { 2060 1.6 christos unsigned byte1,byte2,byte3,byte4; 2061 1.2 christos char more; 2062 1.6 christos for (eow = dot-1; eow >= src && EVUTIL_ISDIGIT_(*eow); --eow) 2063 1.2 christos ; 2064 1.2 christos ++eow; 2065 1.2 christos 2066 1.2 christos /* We use "scanf" because some platform inet_aton()s are too lax 2067 1.2 christos * about IPv4 addresses of the form "1.2.3" */ 2068 1.6 christos if (sscanf(eow, "%u.%u.%u.%u%c", 2069 1.2 christos &byte1,&byte2,&byte3,&byte4,&more) != 4) 2070 1.2 christos return 0; 2071 1.2 christos 2072 1.6 christos if (byte1 > 255 || 2073 1.6 christos byte2 > 255 || 2074 1.6 christos byte3 > 255 || 2075 1.6 christos byte4 > 255) 2076 1.2 christos return 0; 2077 1.2 christos 2078 1.2 christos words[6] = (byte1<<8) | byte2; 2079 1.2 christos words[7] = (byte3<<8) | byte4; 2080 1.2 christos setWords += 2; 2081 1.2 christos } 2082 1.2 christos 2083 1.2 christos i = 0; 2084 1.2 christos while (src < eow) { 2085 1.2 christos if (i > 7) 2086 1.2 christos return 0; 2087 1.6 christos if (EVUTIL_ISXDIGIT_(*src)) { 2088 1.2 christos char *next; 2089 1.2 christos long r = strtol(src, &next, 16); 2090 1.2 christos if (next > 4+src) 2091 1.2 christos return 0; 2092 1.2 christos if (next == src) 2093 1.2 christos return 0; 2094 1.2 christos if (r<0 || r>65536) 2095 1.2 christos return 0; 2096 1.2 christos 2097 1.2 christos words[i++] = (ev_uint16_t)r; 2098 1.2 christos setWords++; 2099 1.2 christos src = next; 2100 1.2 christos if (*src != ':' && src != eow) 2101 1.2 christos return 0; 2102 1.2 christos ++src; 2103 1.2 christos } else if (*src == ':' && i > 0 && gapPos==-1) { 2104 1.2 christos gapPos = i; 2105 1.2 christos ++src; 2106 1.2 christos } else if (*src == ':' && i == 0 && src[1] == ':' && gapPos==-1) { 2107 1.2 christos gapPos = i; 2108 1.2 christos src += 2; 2109 1.2 christos } else { 2110 1.2 christos return 0; 2111 1.2 christos } 2112 1.2 christos } 2113 1.2 christos 2114 1.2 christos if (setWords > 8 || 2115 1.2 christos (setWords == 8 && gapPos != -1) || 2116 1.2 christos (setWords < 8 && gapPos == -1)) 2117 1.2 christos return 0; 2118 1.2 christos 2119 1.2 christos if (gapPos >= 0) { 2120 1.2 christos int nToMove = setWords - (dot ? 2 : 0) - gapPos; 2121 1.2 christos int gapLen = 8 - setWords; 2122 1.2 christos /* assert(nToMove >= 0); */ 2123 1.2 christos if (nToMove < 0) 2124 1.2 christos return -1; /* should be impossible */ 2125 1.2 christos memmove(&words[gapPos+gapLen], &words[gapPos], 2126 1.2 christos sizeof(ev_uint16_t)*nToMove); 2127 1.2 christos memset(&words[gapPos], 0, sizeof(ev_uint16_t)*gapLen); 2128 1.2 christos } 2129 1.2 christos for (i = 0; i < 8; ++i) { 2130 1.2 christos out->s6_addr[2*i ] = words[i] >> 8; 2131 1.2 christos out->s6_addr[2*i+1] = words[i] & 0xff; 2132 1.2 christos } 2133 1.2 christos 2134 1.2 christos return 1; 2135 1.2 christos #endif 2136 1.2 christos } else { 2137 1.2 christos return -1; 2138 1.2 christos } 2139 1.2 christos #endif 2140 1.2 christos } 2141 1.2 christos 2142 1.2 christos int 2143 1.2 christos evutil_parse_sockaddr_port(const char *ip_as_string, struct sockaddr *out, int *outlen) 2144 1.2 christos { 2145 1.2 christos int port; 2146 1.7 christos unsigned int if_index; 2147 1.2 christos char buf[128]; 2148 1.2 christos const char *cp, *addr_part, *port_part; 2149 1.2 christos int is_ipv6; 2150 1.2 christos /* recognized formats are: 2151 1.2 christos * [ipv6]:port 2152 1.2 christos * ipv6 2153 1.2 christos * [ipv6] 2154 1.2 christos * ipv4:port 2155 1.2 christos * ipv4 2156 1.2 christos */ 2157 1.2 christos 2158 1.2 christos cp = strchr(ip_as_string, ':'); 2159 1.2 christos if (*ip_as_string == '[') { 2160 1.6 christos size_t len; 2161 1.2 christos if (!(cp = strchr(ip_as_string, ']'))) { 2162 1.2 christos return -1; 2163 1.2 christos } 2164 1.6 christos len = ( cp-(ip_as_string + 1) ); 2165 1.6 christos if (len > sizeof(buf)-1) { 2166 1.2 christos return -1; 2167 1.2 christos } 2168 1.2 christos memcpy(buf, ip_as_string+1, len); 2169 1.2 christos buf[len] = '\0'; 2170 1.2 christos addr_part = buf; 2171 1.2 christos if (cp[1] == ':') 2172 1.2 christos port_part = cp+2; 2173 1.2 christos else 2174 1.2 christos port_part = NULL; 2175 1.2 christos is_ipv6 = 1; 2176 1.2 christos } else if (cp && strchr(cp+1, ':')) { 2177 1.2 christos is_ipv6 = 1; 2178 1.2 christos addr_part = ip_as_string; 2179 1.2 christos port_part = NULL; 2180 1.2 christos } else if (cp) { 2181 1.2 christos is_ipv6 = 0; 2182 1.2 christos if (cp - ip_as_string > (int)sizeof(buf)-1) { 2183 1.2 christos return -1; 2184 1.2 christos } 2185 1.2 christos memcpy(buf, ip_as_string, cp-ip_as_string); 2186 1.2 christos buf[cp-ip_as_string] = '\0'; 2187 1.2 christos addr_part = buf; 2188 1.2 christos port_part = cp+1; 2189 1.2 christos } else { 2190 1.2 christos addr_part = ip_as_string; 2191 1.2 christos port_part = NULL; 2192 1.2 christos is_ipv6 = 0; 2193 1.2 christos } 2194 1.2 christos 2195 1.2 christos if (port_part == NULL) { 2196 1.2 christos port = 0; 2197 1.2 christos } else { 2198 1.2 christos port = atoi(port_part); 2199 1.2 christos if (port <= 0 || port > 65535) { 2200 1.2 christos return -1; 2201 1.2 christos } 2202 1.2 christos } 2203 1.2 christos 2204 1.2 christos if (!addr_part) 2205 1.2 christos return -1; /* Should be impossible. */ 2206 1.2 christos #ifdef AF_INET6 2207 1.2 christos if (is_ipv6) 2208 1.2 christos { 2209 1.2 christos struct sockaddr_in6 sin6; 2210 1.2 christos memset(&sin6, 0, sizeof(sin6)); 2211 1.6 christos #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN 2212 1.2 christos sin6.sin6_len = sizeof(sin6); 2213 1.2 christos #endif 2214 1.2 christos sin6.sin6_family = AF_INET6; 2215 1.2 christos sin6.sin6_port = htons(port); 2216 1.7 christos if (1 != evutil_inet_pton_scope( 2217 1.7 christos AF_INET6, addr_part, &sin6.sin6_addr, &if_index)) { 2218 1.2 christos return -1; 2219 1.7 christos } 2220 1.2 christos if ((int)sizeof(sin6) > *outlen) 2221 1.2 christos return -1; 2222 1.7 christos sin6.sin6_scope_id = if_index; 2223 1.2 christos memset(out, 0, *outlen); 2224 1.2 christos memcpy(out, &sin6, sizeof(sin6)); 2225 1.2 christos *outlen = sizeof(sin6); 2226 1.2 christos return 0; 2227 1.2 christos } 2228 1.2 christos else 2229 1.2 christos #endif 2230 1.2 christos { 2231 1.2 christos struct sockaddr_in sin; 2232 1.2 christos memset(&sin, 0, sizeof(sin)); 2233 1.6 christos #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN 2234 1.2 christos sin.sin_len = sizeof(sin); 2235 1.2 christos #endif 2236 1.2 christos sin.sin_family = AF_INET; 2237 1.2 christos sin.sin_port = htons(port); 2238 1.2 christos if (1 != evutil_inet_pton(AF_INET, addr_part, &sin.sin_addr)) 2239 1.2 christos return -1; 2240 1.2 christos if ((int)sizeof(sin) > *outlen) 2241 1.2 christos return -1; 2242 1.2 christos memset(out, 0, *outlen); 2243 1.2 christos memcpy(out, &sin, sizeof(sin)); 2244 1.2 christos *outlen = sizeof(sin); 2245 1.2 christos return 0; 2246 1.2 christos } 2247 1.2 christos } 2248 1.2 christos 2249 1.2 christos const char * 2250 1.6 christos evutil_format_sockaddr_port_(const struct sockaddr *sa, char *out, size_t outlen) 2251 1.2 christos { 2252 1.2 christos char b[128]; 2253 1.2 christos const char *res=NULL; 2254 1.2 christos int port; 2255 1.2 christos if (sa->sa_family == AF_INET) { 2256 1.2 christos const struct sockaddr_in *sin = (const struct sockaddr_in*)sa; 2257 1.2 christos res = evutil_inet_ntop(AF_INET, &sin->sin_addr,b,sizeof(b)); 2258 1.2 christos port = ntohs(sin->sin_port); 2259 1.2 christos if (res) { 2260 1.2 christos evutil_snprintf(out, outlen, "%s:%d", b, port); 2261 1.2 christos return out; 2262 1.2 christos } 2263 1.2 christos } else if (sa->sa_family == AF_INET6) { 2264 1.2 christos const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6*)sa; 2265 1.2 christos res = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr,b,sizeof(b)); 2266 1.2 christos port = ntohs(sin6->sin6_port); 2267 1.2 christos if (res) { 2268 1.2 christos evutil_snprintf(out, outlen, "[%s]:%d", b, port); 2269 1.2 christos return out; 2270 1.2 christos } 2271 1.2 christos } 2272 1.2 christos 2273 1.2 christos evutil_snprintf(out, outlen, "<addr with socktype %d>", 2274 1.2 christos (int)sa->sa_family); 2275 1.2 christos return out; 2276 1.2 christos } 2277 1.2 christos 2278 1.2 christos int 2279 1.2 christos evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2, 2280 1.2 christos int include_port) 2281 1.2 christos { 2282 1.2 christos int r; 2283 1.2 christos if (0 != (r = (sa1->sa_family - sa2->sa_family))) 2284 1.1 plunky return r; 2285 1.2 christos 2286 1.2 christos if (sa1->sa_family == AF_INET) { 2287 1.2 christos const struct sockaddr_in *sin1, *sin2; 2288 1.2 christos sin1 = (const struct sockaddr_in *)sa1; 2289 1.2 christos sin2 = (const struct sockaddr_in *)sa2; 2290 1.2 christos if (sin1->sin_addr.s_addr < sin2->sin_addr.s_addr) 2291 1.2 christos return -1; 2292 1.2 christos else if (sin1->sin_addr.s_addr > sin2->sin_addr.s_addr) 2293 1.2 christos return 1; 2294 1.2 christos else if (include_port && 2295 1.2 christos (r = ((int)sin1->sin_port - (int)sin2->sin_port))) 2296 1.2 christos return r; 2297 1.2 christos else 2298 1.2 christos return 0; 2299 1.2 christos } 2300 1.2 christos #ifdef AF_INET6 2301 1.2 christos else if (sa1->sa_family == AF_INET6) { 2302 1.2 christos const struct sockaddr_in6 *sin1, *sin2; 2303 1.2 christos sin1 = (const struct sockaddr_in6 *)sa1; 2304 1.2 christos sin2 = (const struct sockaddr_in6 *)sa2; 2305 1.2 christos if ((r = memcmp(sin1->sin6_addr.s6_addr, sin2->sin6_addr.s6_addr, 16))) 2306 1.2 christos return r; 2307 1.2 christos else if (include_port && 2308 1.2 christos (r = ((int)sin1->sin6_port - (int)sin2->sin6_port))) 2309 1.2 christos return r; 2310 1.2 christos else 2311 1.2 christos return 0; 2312 1.2 christos } 2313 1.2 christos #endif 2314 1.2 christos return 1; 2315 1.2 christos } 2316 1.2 christos 2317 1.2 christos /* Tables to implement ctypes-replacement EVUTIL_IS*() functions. Each table 2318 1.2 christos * has 256 bits to look up whether a character is in some set or not. This 2319 1.2 christos * fails on non-ASCII platforms, but so does every other place where we 2320 1.2 christos * take a char and write it onto the network. 2321 1.2 christos **/ 2322 1.2 christos static const ev_uint32_t EVUTIL_ISALPHA_TABLE[8] = 2323 1.2 christos { 0, 0, 0x7fffffe, 0x7fffffe, 0, 0, 0, 0 }; 2324 1.2 christos static const ev_uint32_t EVUTIL_ISALNUM_TABLE[8] = 2325 1.2 christos { 0, 0x3ff0000, 0x7fffffe, 0x7fffffe, 0, 0, 0, 0 }; 2326 1.2 christos static const ev_uint32_t EVUTIL_ISSPACE_TABLE[8] = { 0x3e00, 0x1, 0, 0, 0, 0, 0, 0 }; 2327 1.2 christos static const ev_uint32_t EVUTIL_ISXDIGIT_TABLE[8] = 2328 1.2 christos { 0, 0x3ff0000, 0x7e, 0x7e, 0, 0, 0, 0 }; 2329 1.2 christos static const ev_uint32_t EVUTIL_ISDIGIT_TABLE[8] = { 0, 0x3ff0000, 0, 0, 0, 0, 0, 0 }; 2330 1.2 christos static const ev_uint32_t EVUTIL_ISPRINT_TABLE[8] = 2331 1.2 christos { 0, 0xffffffff, 0xffffffff, 0x7fffffff, 0, 0, 0, 0x0 }; 2332 1.2 christos static const ev_uint32_t EVUTIL_ISUPPER_TABLE[8] = { 0, 0, 0x7fffffe, 0, 0, 0, 0, 0 }; 2333 1.2 christos static const ev_uint32_t EVUTIL_ISLOWER_TABLE[8] = { 0, 0, 0, 0x7fffffe, 0, 0, 0, 0 }; 2334 1.2 christos /* Upper-casing and lowercasing tables to map characters to upper/lowercase 2335 1.2 christos * equivalents. */ 2336 1.2 christos static const unsigned char EVUTIL_TOUPPER_TABLE[256] = { 2337 1.2 christos 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 2338 1.2 christos 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, 2339 1.2 christos 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, 2340 1.2 christos 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63, 2341 1.2 christos 64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, 2342 1.2 christos 80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95, 2343 1.2 christos 96,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, 2344 1.2 christos 80,81,82,83,84,85,86,87,88,89,90,123,124,125,126,127, 2345 1.2 christos 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 2346 1.2 christos 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 2347 1.2 christos 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, 2348 1.2 christos 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, 2349 1.2 christos 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 2350 1.2 christos 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 2351 1.2 christos 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 2352 1.2 christos 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, 2353 1.2 christos }; 2354 1.2 christos static const unsigned char EVUTIL_TOLOWER_TABLE[256] = { 2355 1.2 christos 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 2356 1.2 christos 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, 2357 1.2 christos 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, 2358 1.2 christos 48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63, 2359 1.2 christos 64,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111, 2360 1.2 christos 112,113,114,115,116,117,118,119,120,121,122,91,92,93,94,95, 2361 1.2 christos 96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111, 2362 1.2 christos 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 2363 1.2 christos 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, 2364 1.2 christos 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, 2365 1.2 christos 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, 2366 1.2 christos 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, 2367 1.2 christos 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, 2368 1.2 christos 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, 2369 1.2 christos 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, 2370 1.2 christos 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255, 2371 1.2 christos }; 2372 1.2 christos 2373 1.2 christos #define IMPL_CTYPE_FN(name) \ 2374 1.6 christos int EVUTIL_##name##_(char c) { \ 2375 1.2 christos ev_uint8_t u = c; \ 2376 1.7 christos return !!(EVUTIL_##name##_TABLE[(u >> 5) & 7] & (1U << (u & 31))); \ 2377 1.2 christos } 2378 1.2 christos IMPL_CTYPE_FN(ISALPHA) 2379 1.2 christos IMPL_CTYPE_FN(ISALNUM) 2380 1.2 christos IMPL_CTYPE_FN(ISSPACE) 2381 1.2 christos IMPL_CTYPE_FN(ISDIGIT) 2382 1.2 christos IMPL_CTYPE_FN(ISXDIGIT) 2383 1.2 christos IMPL_CTYPE_FN(ISPRINT) 2384 1.2 christos IMPL_CTYPE_FN(ISLOWER) 2385 1.2 christos IMPL_CTYPE_FN(ISUPPER) 2386 1.2 christos 2387 1.6 christos char EVUTIL_TOLOWER_(char c) 2388 1.2 christos { 2389 1.2 christos return ((char)EVUTIL_TOLOWER_TABLE[(ev_uint8_t)c]); 2390 1.2 christos } 2391 1.6 christos char EVUTIL_TOUPPER_(char c) 2392 1.2 christos { 2393 1.2 christos return ((char)EVUTIL_TOUPPER_TABLE[(ev_uint8_t)c]); 2394 1.2 christos } 2395 1.2 christos int 2396 1.2 christos evutil_ascii_strcasecmp(const char *s1, const char *s2) 2397 1.2 christos { 2398 1.2 christos char c1, c2; 2399 1.2 christos while (1) { 2400 1.6 christos c1 = EVUTIL_TOLOWER_(*s1++); 2401 1.6 christos c2 = EVUTIL_TOLOWER_(*s2++); 2402 1.2 christos if (c1 < c2) 2403 1.2 christos return -1; 2404 1.2 christos else if (c1 > c2) 2405 1.2 christos return 1; 2406 1.2 christos else if (c1 == 0) 2407 1.2 christos return 0; 2408 1.2 christos } 2409 1.2 christos } 2410 1.2 christos int evutil_ascii_strncasecmp(const char *s1, const char *s2, size_t n) 2411 1.2 christos { 2412 1.2 christos char c1, c2; 2413 1.2 christos while (n--) { 2414 1.6 christos c1 = EVUTIL_TOLOWER_(*s1++); 2415 1.6 christos c2 = EVUTIL_TOLOWER_(*s2++); 2416 1.2 christos if (c1 < c2) 2417 1.2 christos return -1; 2418 1.2 christos else if (c1 > c2) 2419 1.2 christos return 1; 2420 1.2 christos else if (c1 == 0) 2421 1.2 christos return 0; 2422 1.2 christos } 2423 1.2 christos return 0; 2424 1.2 christos } 2425 1.2 christos 2426 1.6 christos void 2427 1.6 christos evutil_rtrim_lws_(char *str) 2428 1.6 christos { 2429 1.6 christos char *cp; 2430 1.6 christos 2431 1.6 christos if (str == NULL) 2432 1.6 christos return; 2433 1.6 christos 2434 1.6 christos if ((cp = strchr(str, '\0')) == NULL || (cp == str)) 2435 1.6 christos return; 2436 1.6 christos 2437 1.6 christos --cp; 2438 1.6 christos 2439 1.6 christos while (*cp == ' ' || *cp == '\t') { 2440 1.6 christos *cp = '\0'; 2441 1.6 christos if (cp == str) 2442 1.6 christos break; 2443 1.6 christos --cp; 2444 1.6 christos } 2445 1.6 christos } 2446 1.6 christos 2447 1.2 christos static int 2448 1.2 christos evutil_issetugid(void) 2449 1.2 christos { 2450 1.6 christos #ifdef EVENT__HAVE_ISSETUGID 2451 1.2 christos return issetugid(); 2452 1.2 christos #else 2453 1.2 christos 2454 1.6 christos #ifdef EVENT__HAVE_GETEUID 2455 1.2 christos if (getuid() != geteuid()) 2456 1.2 christos return 1; 2457 1.2 christos #endif 2458 1.6 christos #ifdef EVENT__HAVE_GETEGID 2459 1.2 christos if (getgid() != getegid()) 2460 1.2 christos return 1; 2461 1.2 christos #endif 2462 1.2 christos return 0; 2463 1.2 christos #endif 2464 1.2 christos } 2465 1.2 christos 2466 1.2 christos const char * 2467 1.6 christos evutil_getenv_(const char *varname) 2468 1.2 christos { 2469 1.2 christos if (evutil_issetugid()) 2470 1.2 christos return NULL; 2471 1.2 christos 2472 1.2 christos return getenv(varname); 2473 1.2 christos } 2474 1.2 christos 2475 1.6 christos ev_uint32_t 2476 1.6 christos evutil_weakrand_seed_(struct evutil_weakrand_state *state, ev_uint32_t seed) 2477 1.2 christos { 2478 1.6 christos if (seed == 0) { 2479 1.6 christos struct timeval tv; 2480 1.6 christos evutil_gettimeofday(&tv, NULL); 2481 1.6 christos seed = (ev_uint32_t)tv.tv_sec + (ev_uint32_t)tv.tv_usec; 2482 1.6 christos #ifdef _WIN32 2483 1.6 christos seed += (ev_uint32_t) _getpid(); 2484 1.1 plunky #else 2485 1.6 christos seed += (ev_uint32_t) getpid(); 2486 1.1 plunky #endif 2487 1.6 christos } 2488 1.6 christos state->seed = seed; 2489 1.6 christos return seed; 2490 1.6 christos } 2491 1.6 christos 2492 1.6 christos ev_int32_t 2493 1.6 christos evutil_weakrand_(struct evutil_weakrand_state *state) 2494 1.6 christos { 2495 1.6 christos /* This RNG implementation is a linear congruential generator, with 2496 1.6 christos * modulus 2^31, multiplier 1103515245, and addend 12345. It's also 2497 1.6 christos * used by OpenBSD, and by Glibc's TYPE_0 RNG. 2498 1.6 christos * 2499 1.6 christos * The linear congruential generator is not an industrial-strength 2500 1.6 christos * RNG! It's fast, but it can have higher-order patterns. Notably, 2501 1.6 christos * the low bits tend to have periodicity. 2502 1.6 christos */ 2503 1.6 christos state->seed = ((state->seed) * 1103515245 + 12345) & 0x7fffffff; 2504 1.6 christos return (ev_int32_t)(state->seed); 2505 1.6 christos } 2506 1.6 christos 2507 1.6 christos ev_int32_t 2508 1.6 christos evutil_weakrand_range_(struct evutil_weakrand_state *state, ev_int32_t top) 2509 1.6 christos { 2510 1.6 christos ev_int32_t divisor, result; 2511 1.6 christos 2512 1.6 christos /* We can't just do weakrand() % top, since the low bits of the LCG 2513 1.6 christos * are less random than the high ones. (Specifically, since the LCG 2514 1.6 christos * modulus is 2^N, every 2^m for m<N will divide the modulus, and so 2515 1.6 christos * therefore the low m bits of the LCG will have period 2^m.) */ 2516 1.6 christos divisor = EVUTIL_WEAKRAND_MAX / top; 2517 1.6 christos do { 2518 1.6 christos result = evutil_weakrand_(state) / divisor; 2519 1.6 christos } while (result >= top); 2520 1.6 christos return result; 2521 1.1 plunky } 2522 1.2 christos 2523 1.5 spz /** 2524 1.5 spz * Volatile pointer to memset: we use this to keep the compiler from 2525 1.5 spz * eliminating our call to memset. 2526 1.5 spz */ 2527 1.5 spz void * (*volatile evutil_memset_volatile_)(void *, int, size_t) = memset; 2528 1.5 spz 2529 1.5 spz void 2530 1.5 spz evutil_memclear_(void *mem, size_t len) 2531 1.5 spz { 2532 1.5 spz evutil_memset_volatile_(mem, 0, len); 2533 1.5 spz } 2534 1.5 spz 2535 1.2 christos int 2536 1.6 christos evutil_sockaddr_is_loopback_(const struct sockaddr *addr) 2537 1.2 christos { 2538 1.2 christos static const char LOOPBACK_S6[16] = 2539 1.2 christos "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"; 2540 1.2 christos if (addr->sa_family == AF_INET) { 2541 1.2 christos const struct sockaddr_in *sin = (const struct sockaddr_in *)addr; 2542 1.2 christos return (ntohl(sin->sin_addr.s_addr) & 0xff000000) == 0x7f000000; 2543 1.2 christos } else if (addr->sa_family == AF_INET6) { 2544 1.2 christos const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)addr; 2545 1.2 christos return !memcmp(sin6->sin6_addr.s6_addr, LOOPBACK_S6, 16); 2546 1.2 christos } 2547 1.2 christos return 0; 2548 1.2 christos } 2549 1.2 christos 2550 1.2 christos int 2551 1.6 christos evutil_hex_char_to_int_(char c) 2552 1.2 christos { 2553 1.2 christos switch(c) 2554 1.2 christos { 2555 1.2 christos case '0': return 0; 2556 1.2 christos case '1': return 1; 2557 1.2 christos case '2': return 2; 2558 1.2 christos case '3': return 3; 2559 1.2 christos case '4': return 4; 2560 1.2 christos case '5': return 5; 2561 1.2 christos case '6': return 6; 2562 1.2 christos case '7': return 7; 2563 1.2 christos case '8': return 8; 2564 1.2 christos case '9': return 9; 2565 1.2 christos case 'A': case 'a': return 10; 2566 1.2 christos case 'B': case 'b': return 11; 2567 1.2 christos case 'C': case 'c': return 12; 2568 1.2 christos case 'D': case 'd': return 13; 2569 1.2 christos case 'E': case 'e': return 14; 2570 1.2 christos case 'F': case 'f': return 15; 2571 1.2 christos } 2572 1.2 christos return -1; 2573 1.2 christos } 2574 1.2 christos 2575 1.6 christos #ifdef _WIN32 2576 1.6 christos HMODULE 2577 1.6 christos evutil_load_windows_system_library_(const TCHAR *library_name) 2578 1.2 christos { 2579 1.2 christos TCHAR path[MAX_PATH]; 2580 1.2 christos unsigned n; 2581 1.2 christos n = GetSystemDirectory(path, MAX_PATH); 2582 1.2 christos if (n == 0 || n + _tcslen(library_name) + 2 >= MAX_PATH) 2583 1.2 christos return 0; 2584 1.2 christos _tcscat(path, TEXT("\\")); 2585 1.2 christos _tcscat(path, library_name); 2586 1.2 christos return LoadLibrary(path); 2587 1.2 christos } 2588 1.2 christos #endif 2589 1.2 christos 2590 1.6 christos /* Internal wrapper around 'socket' to provide Linux-style support for 2591 1.6 christos * syscall-saving methods where available. 2592 1.6 christos * 2593 1.6 christos * In addition to regular socket behavior, you can use a bitwise or to set the 2594 1.6 christos * flags EVUTIL_SOCK_NONBLOCK and EVUTIL_SOCK_CLOEXEC in the 'type' argument, 2595 1.6 christos * to make the socket nonblocking or close-on-exec with as few syscalls as 2596 1.6 christos * possible. 2597 1.6 christos */ 2598 1.6 christos evutil_socket_t 2599 1.6 christos evutil_socket_(int domain, int type, int protocol) 2600 1.6 christos { 2601 1.6 christos evutil_socket_t r; 2602 1.6 christos #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) 2603 1.6 christos r = socket(domain, type, protocol); 2604 1.6 christos if (r >= 0) 2605 1.6 christos return r; 2606 1.6 christos else if ((type & (SOCK_NONBLOCK|SOCK_CLOEXEC)) == 0) 2607 1.6 christos return -1; 2608 1.6 christos #endif 2609 1.6 christos #define SOCKET_TYPE_MASK (~(EVUTIL_SOCK_NONBLOCK|EVUTIL_SOCK_CLOEXEC)) 2610 1.6 christos r = socket(domain, type & SOCKET_TYPE_MASK, protocol); 2611 1.6 christos if (r < 0) 2612 1.6 christos return -1; 2613 1.6 christos if (type & EVUTIL_SOCK_NONBLOCK) { 2614 1.6 christos if (evutil_fast_socket_nonblocking(r) < 0) { 2615 1.6 christos evutil_closesocket(r); 2616 1.6 christos return -1; 2617 1.6 christos } 2618 1.6 christos } 2619 1.6 christos if (type & EVUTIL_SOCK_CLOEXEC) { 2620 1.6 christos if (evutil_fast_socket_closeonexec(r) < 0) { 2621 1.6 christos evutil_closesocket(r); 2622 1.6 christos return -1; 2623 1.6 christos } 2624 1.6 christos } 2625 1.6 christos return r; 2626 1.6 christos } 2627 1.6 christos 2628 1.6 christos /* Internal wrapper around 'accept' or 'accept4' to provide Linux-style 2629 1.6 christos * support for syscall-saving methods where available. 2630 1.6 christos * 2631 1.6 christos * In addition to regular accept behavior, you can set one or more of flags 2632 1.6 christos * EVUTIL_SOCK_NONBLOCK and EVUTIL_SOCK_CLOEXEC in the 'flags' argument, to 2633 1.6 christos * make the socket nonblocking or close-on-exec with as few syscalls as 2634 1.6 christos * possible. 2635 1.6 christos */ 2636 1.6 christos evutil_socket_t 2637 1.6 christos evutil_accept4_(evutil_socket_t sockfd, struct sockaddr *addr, 2638 1.6 christos ev_socklen_t *addrlen, int flags) 2639 1.6 christos { 2640 1.6 christos evutil_socket_t result; 2641 1.6 christos #if defined(EVENT__HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK) 2642 1.6 christos result = accept4(sockfd, addr, addrlen, flags); 2643 1.6 christos if (result >= 0 || (errno != EINVAL && errno != ENOSYS)) { 2644 1.6 christos /* A nonnegative result means that we succeeded, so return. 2645 1.6 christos * Failing with EINVAL means that an option wasn't supported, 2646 1.6 christos * and failing with ENOSYS means that the syscall wasn't 2647 1.6 christos * there: in those cases we want to fall back. Otherwise, we 2648 1.6 christos * got a real error, and we should return. */ 2649 1.6 christos return result; 2650 1.6 christos } 2651 1.6 christos #endif 2652 1.6 christos result = accept(sockfd, addr, addrlen); 2653 1.6 christos if (result < 0) 2654 1.6 christos return result; 2655 1.6 christos 2656 1.6 christos if (flags & EVUTIL_SOCK_CLOEXEC) { 2657 1.6 christos if (evutil_fast_socket_closeonexec(result) < 0) { 2658 1.6 christos evutil_closesocket(result); 2659 1.6 christos return -1; 2660 1.6 christos } 2661 1.6 christos } 2662 1.6 christos if (flags & EVUTIL_SOCK_NONBLOCK) { 2663 1.6 christos if (evutil_fast_socket_nonblocking(result) < 0) { 2664 1.6 christos evutil_closesocket(result); 2665 1.6 christos return -1; 2666 1.6 christos } 2667 1.6 christos } 2668 1.6 christos return result; 2669 1.6 christos } 2670 1.6 christos 2671 1.6 christos /* Internal function: Set fd[0] and fd[1] to a pair of fds such that writes on 2672 1.7 christos * fd[1] get read from fd[0]. Make both fds nonblocking and close-on-exec. 2673 1.6 christos * Return 0 on success, -1 on failure. 2674 1.6 christos */ 2675 1.6 christos int 2676 1.6 christos evutil_make_internal_pipe_(evutil_socket_t fd[2]) 2677 1.6 christos { 2678 1.6 christos /* 2679 1.6 christos Making the second socket nonblocking is a bit subtle, given that we 2680 1.6 christos ignore any EAGAIN returns when writing to it, and you don't usally 2681 1.6 christos do that for a nonblocking socket. But if the kernel gives us EAGAIN, 2682 1.6 christos then there's no need to add any more data to the buffer, since 2683 1.6 christos the main thread is already either about to wake up and drain it, 2684 1.6 christos or woken up and in the process of draining it. 2685 1.6 christos */ 2686 1.6 christos 2687 1.6 christos #if defined(EVENT__HAVE_PIPE2) 2688 1.6 christos if (pipe2(fd, O_NONBLOCK|O_CLOEXEC) == 0) 2689 1.6 christos return 0; 2690 1.6 christos #endif 2691 1.6 christos #if defined(EVENT__HAVE_PIPE) 2692 1.6 christos if (pipe(fd) == 0) { 2693 1.6 christos if (evutil_fast_socket_nonblocking(fd[0]) < 0 || 2694 1.6 christos evutil_fast_socket_nonblocking(fd[1]) < 0 || 2695 1.6 christos evutil_fast_socket_closeonexec(fd[0]) < 0 || 2696 1.6 christos evutil_fast_socket_closeonexec(fd[1]) < 0) { 2697 1.6 christos close(fd[0]); 2698 1.6 christos close(fd[1]); 2699 1.6 christos fd[0] = fd[1] = -1; 2700 1.6 christos return -1; 2701 1.6 christos } 2702 1.6 christos return 0; 2703 1.6 christos } else { 2704 1.6 christos event_warn("%s: pipe", __func__); 2705 1.6 christos } 2706 1.6 christos #endif 2707 1.6 christos 2708 1.6 christos #ifdef _WIN32 2709 1.6 christos #define LOCAL_SOCKETPAIR_AF AF_INET 2710 1.6 christos #else 2711 1.6 christos #define LOCAL_SOCKETPAIR_AF AF_UNIX 2712 1.6 christos #endif 2713 1.6 christos if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, fd) == 0) { 2714 1.6 christos if (evutil_fast_socket_nonblocking(fd[0]) < 0 || 2715 1.6 christos evutil_fast_socket_nonblocking(fd[1]) < 0 || 2716 1.6 christos evutil_fast_socket_closeonexec(fd[0]) < 0 || 2717 1.6 christos evutil_fast_socket_closeonexec(fd[1]) < 0) { 2718 1.6 christos evutil_closesocket(fd[0]); 2719 1.6 christos evutil_closesocket(fd[1]); 2720 1.6 christos fd[0] = fd[1] = -1; 2721 1.6 christos return -1; 2722 1.6 christos } 2723 1.6 christos return 0; 2724 1.6 christos } 2725 1.6 christos fd[0] = fd[1] = -1; 2726 1.6 christos return -1; 2727 1.6 christos } 2728 1.6 christos 2729 1.6 christos /* Wrapper around eventfd on systems that provide it. Unlike the system 2730 1.6 christos * eventfd, it always supports EVUTIL_EFD_CLOEXEC and EVUTIL_EFD_NONBLOCK as 2731 1.6 christos * flags. Returns -1 on error or if eventfd is not supported. 2732 1.6 christos */ 2733 1.6 christos evutil_socket_t 2734 1.6 christos evutil_eventfd_(unsigned initval, int flags) 2735 1.6 christos { 2736 1.6 christos #if defined(EVENT__HAVE_EVENTFD) && defined(EVENT__HAVE_SYS_EVENTFD_H) 2737 1.6 christos int r; 2738 1.6 christos #if defined(EFD_CLOEXEC) && defined(EFD_NONBLOCK) 2739 1.6 christos r = eventfd(initval, flags); 2740 1.6 christos if (r >= 0 || flags == 0) 2741 1.6 christos return r; 2742 1.6 christos #endif 2743 1.6 christos r = eventfd(initval, 0); 2744 1.6 christos if (r < 0) 2745 1.6 christos return r; 2746 1.6 christos if (flags & EVUTIL_EFD_CLOEXEC) { 2747 1.6 christos if (evutil_fast_socket_closeonexec(r) < 0) { 2748 1.6 christos evutil_closesocket(r); 2749 1.6 christos return -1; 2750 1.6 christos } 2751 1.6 christos } 2752 1.6 christos if (flags & EVUTIL_EFD_NONBLOCK) { 2753 1.6 christos if (evutil_fast_socket_nonblocking(r) < 0) { 2754 1.6 christos evutil_closesocket(r); 2755 1.6 christos return -1; 2756 1.6 christos } 2757 1.6 christos } 2758 1.6 christos return r; 2759 1.6 christos #else 2760 1.6 christos return -1; 2761 1.6 christos #endif 2762 1.6 christos } 2763 1.6 christos 2764 1.6 christos void 2765 1.6 christos evutil_free_globals_(void) 2766 1.6 christos { 2767 1.6 christos evutil_free_secure_rng_globals_(); 2768 1.6 christos evutil_free_sock_err_globals(); 2769 1.6 christos } 2770