1 /* $NetBSD: util.h,v 1.3 2015/01/29 07:26:02 spz Exp $ */ 2 /* 3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #ifndef _EVENT2_UTIL_H_ 28 #define _EVENT2_UTIL_H_ 29 30 /** @file event2/util.h 31 32 Common convenience functions for cross-platform portability and 33 related socket manipulations. 34 35 */ 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 #include <event2/event-config.h> 42 #ifdef _EVENT_HAVE_SYS_TIME_H 43 #include <sys/time.h> 44 #endif 45 #ifdef _EVENT_HAVE_STDINT_H 46 #include <stdint.h> 47 #elif defined(_EVENT_HAVE_INTTYPES_H) 48 #include <inttypes.h> 49 #endif 50 #ifdef _EVENT_HAVE_SYS_TYPES_H 51 #include <sys/types.h> 52 #endif 53 #ifdef _EVENT_HAVE_STDDEF_H 54 #include <stddef.h> 55 #endif 56 #ifdef _MSC_VER 57 #include <BaseTsd.h> 58 #endif 59 #include <stdarg.h> 60 #ifdef _EVENT_HAVE_NETDB_H 61 #if !defined(_GNU_SOURCE) 62 #define _GNU_SOURCE 63 #endif 64 #include <netdb.h> 65 #endif 66 67 #ifdef WIN32 68 #include <winsock2.h> 69 #else 70 #include <sys/socket.h> 71 #endif 72 73 /* Some openbsd autoconf versions get the name of this macro wrong. */ 74 #if defined(_EVENT_SIZEOF_VOID__) && !defined(_EVENT_SIZEOF_VOID_P) 75 #define _EVENT_SIZEOF_VOID_P _EVENT_SIZEOF_VOID__ 76 #endif 77 78 /** 79 * @name Standard integer types. 80 * 81 * Integer type definitions for types that are supposed to be defined in the 82 * C99-specified stdint.h. Shamefully, some platforms do not include 83 * stdint.h, so we need to replace it. (If you are on a platform like this, 84 * your C headers are now over 10 years out of date. You should bug them to 85 * do something about this.) 86 * 87 * We define: 88 * 89 * <dl> 90 * <dt>ev_uint64_t, ev_uint32_t, ev_uint16_t, ev_uint8_t</dt> 91 * <dd>unsigned integer types of exactly 64, 32, 16, and 8 bits 92 * respectively.</dd> 93 * <dt>ev_int64_t, ev_int32_t, ev_int16_t, ev_int8_t</dt> 94 * <dd>signed integer types of exactly 64, 32, 16, and 8 bits 95 * respectively.</dd> 96 * <dt>ev_uintptr_t, ev_intptr_t</dt> 97 * <dd>unsigned/signed integers large enough 98 * to hold a pointer without loss of bits.</dd> 99 * <dt>ev_ssize_t</dt> 100 * <dd>A signed type of the same size as size_t</dd> 101 * <dt>ev_off_t</dt> 102 * <dd>A signed type typically used to represent offsets within a 103 * (potentially large) file</dd> 104 * 105 * @{ 106 */ 107 #ifdef _EVENT_HAVE_UINT64_T 108 #define ev_uint64_t uint64_t 109 #define ev_int64_t int64_t 110 #elif defined(WIN32) 111 #define ev_uint64_t unsigned __int64 112 #define ev_int64_t signed __int64 113 #elif _EVENT_SIZEOF_LONG_LONG == 8 114 #define ev_uint64_t unsigned long long 115 #define ev_int64_t long long 116 #elif _EVENT_SIZEOF_LONG == 8 117 #define ev_uint64_t unsigned long 118 #define ev_int64_t long 119 #elif defined(_EVENT_IN_DOXYGEN) 120 #define ev_uint64_t ... 121 #define ev_int64_t ... 122 #else 123 #error "No way to define ev_uint64_t" 124 #endif 125 126 #ifdef _EVENT_HAVE_UINT32_T 127 #define ev_uint32_t uint32_t 128 #define ev_int32_t int32_t 129 #elif defined(WIN32) 130 #define ev_uint32_t unsigned int 131 #define ev_int32_t signed int 132 #elif _EVENT_SIZEOF_LONG == 4 133 #define ev_uint32_t unsigned long 134 #define ev_int32_t signed long 135 #elif _EVENT_SIZEOF_INT == 4 136 #define ev_uint32_t unsigned int 137 #define ev_int32_t signed int 138 #elif defined(_EVENT_IN_DOXYGEN) 139 #define ev_uint32_t ... 140 #define ev_int32_t ... 141 #else 142 #error "No way to define ev_uint32_t" 143 #endif 144 145 #ifdef _EVENT_HAVE_UINT16_T 146 #define ev_uint16_t uint16_t 147 #define ev_int16_t int16_t 148 #elif defined(WIN32) 149 #define ev_uint16_t unsigned short 150 #define ev_int16_t signed short 151 #elif _EVENT_SIZEOF_INT == 2 152 #define ev_uint16_t unsigned int 153 #define ev_int16_t signed int 154 #elif _EVENT_SIZEOF_SHORT == 2 155 #define ev_uint16_t unsigned short 156 #define ev_int16_t signed short 157 #elif defined(_EVENT_IN_DOXYGEN) 158 #define ev_uint16_t ... 159 #define ev_int16_t ... 160 #else 161 #error "No way to define ev_uint16_t" 162 #endif 163 164 #ifdef _EVENT_HAVE_UINT8_T 165 #define ev_uint8_t uint8_t 166 #define ev_int8_t int8_t 167 #elif defined(_EVENT_IN_DOXYGEN) 168 #define ev_uint8_t ... 169 #define ev_int8_t ... 170 #else 171 #define ev_uint8_t unsigned char 172 #define ev_int8_t signed char 173 #endif 174 175 #ifdef _EVENT_HAVE_UINTPTR_T 176 #define ev_uintptr_t uintptr_t 177 #define ev_intptr_t intptr_t 178 #elif _EVENT_SIZEOF_VOID_P <= 4 179 #define ev_uintptr_t ev_uint32_t 180 #define ev_intptr_t ev_int32_t 181 #elif _EVENT_SIZEOF_VOID_P <= 8 182 #define ev_uintptr_t ev_uint64_t 183 #define ev_intptr_t ev_int64_t 184 #elif defined(_EVENT_IN_DOXYGEN) 185 #define ev_uintptr_t ... 186 #define ev_intptr_t ... 187 #else 188 #error "No way to define ev_uintptr_t" 189 #endif 190 191 #ifdef _EVENT_ssize_t 192 #define ev_ssize_t _EVENT_ssize_t 193 #else 194 #define ev_ssize_t ssize_t 195 #endif 196 197 #ifdef WIN32 198 #define ev_off_t ev_int64_t 199 #else 200 #define ev_off_t off_t 201 #endif 202 /**@}*/ 203 204 /* Limits for integer types. 205 206 We're making two assumptions here: 207 - The compiler does constant folding properly. 208 - The platform does signed arithmetic in two's complement. 209 */ 210 211 /** 212 @name Limits for integer types 213 214 These macros hold the largest or smallest values possible for the 215 ev_[u]int*_t types. 216 217 @{ 218 */ 219 #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL) 220 #define EV_INT64_MAX ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL) 221 #define EV_INT64_MIN ((-EV_INT64_MAX) - 1) 222 #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL) 223 #define EV_INT32_MAX ((ev_int32_t) 0x7fffffffL) 224 #define EV_INT32_MIN ((-EV_INT32_MAX) - 1) 225 #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL) 226 #define EV_INT16_MAX ((ev_int16_t) 0x7fffL) 227 #define EV_INT16_MIN ((-EV_INT16_MAX) - 1) 228 #define EV_UINT8_MAX 255 229 #define EV_INT8_MAX 127 230 #define EV_INT8_MIN ((-EV_INT8_MAX) - 1) 231 /** @} */ 232 233 /** 234 @name Limits for SIZE_T and SSIZE_T 235 236 @{ 237 */ 238 #if _EVENT_SIZEOF_SIZE_T == 8 239 #define EV_SIZE_MAX EV_UINT64_MAX 240 #define EV_SSIZE_MAX EV_INT64_MAX 241 #elif _EVENT_SIZEOF_SIZE_T == 4 242 #define EV_SIZE_MAX EV_UINT32_MAX 243 #define EV_SSIZE_MAX EV_INT32_MAX 244 #elif defined(_EVENT_IN_DOXYGEN) 245 #define EV_SIZE_MAX ... 246 #define EV_SSIZE_MAX ... 247 #else 248 #error "No way to define SIZE_MAX" 249 #endif 250 251 #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1) 252 /**@}*/ 253 254 #ifdef WIN32 255 #define ev_socklen_t int 256 #elif defined(_EVENT_socklen_t) 257 #define ev_socklen_t _EVENT_socklen_t 258 #else 259 #define ev_socklen_t socklen_t 260 #endif 261 262 #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY 263 #if !defined(_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \ 264 && !defined(ss_family) 265 #define ss_family __ss_family 266 #endif 267 #endif 268 269 /** 270 * A type wide enough to hold the output of "socket()" or "accept()". On 271 * Windows, this is an intptr_t; elsewhere, it is an int. */ 272 #ifdef WIN32 273 #define evutil_socket_t intptr_t 274 #else 275 #define evutil_socket_t int 276 #endif 277 278 /** Create two new sockets that are connected to each other. 279 280 On Unix, this simply calls socketpair(). On Windows, it uses the 281 loopback network interface on 127.0.0.1, and only 282 AF_INET,SOCK_STREAM are supported. 283 284 (This may fail on some Windows hosts where firewall software has cleverly 285 decided to keep 127.0.0.1 from talking to itself.) 286 287 Parameters and return values are as for socketpair() 288 */ 289 int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]); 290 /** Do platform-specific operations as needed to make a socket nonblocking. 291 292 @param sock The socket to make nonblocking 293 @return 0 on success, -1 on failure 294 */ 295 int evutil_make_socket_nonblocking(evutil_socket_t sock); 296 297 /** Do platform-specific operations to make a listener socket reusable. 298 299 Specifically, we want to make sure that another program will be able 300 to bind this address right after we've closed the listener. 301 302 This differs from Windows's interpretation of "reusable", which 303 allows multiple listeners to bind the same address at the same time. 304 305 @param sock The socket to make reusable 306 @return 0 on success, -1 on failure 307 */ 308 int evutil_make_listen_socket_reuseable(evutil_socket_t sock); 309 310 /** Do platform-specific operations as needed to close a socket upon a 311 successful execution of one of the exec*() functions. 312 313 @param sock The socket to be closed 314 @return 0 on success, -1 on failure 315 */ 316 int evutil_make_socket_closeonexec(evutil_socket_t sock); 317 318 /** Do the platform-specific call needed to close a socket returned from 319 socket() or accept(). 320 321 @param sock The socket to be closed 322 @return 0 on success, -1 on failure 323 */ 324 int evutil_closesocket(evutil_socket_t sock); 325 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s) 326 327 328 #ifdef WIN32 329 /** Return the most recent socket error. Not idempotent on all platforms. */ 330 #define EVUTIL_SOCKET_ERROR() WSAGetLastError() 331 /** Replace the most recent socket error with errcode */ 332 #define EVUTIL_SET_SOCKET_ERROR(errcode) \ 333 do { WSASetLastError(errcode); } while (0) 334 /** Return the most recent socket error to occur on sock. */ 335 int evutil_socket_geterror(evutil_socket_t sock); 336 /** Convert a socket error to a string. */ 337 const char *evutil_socket_error_to_string(int errcode); 338 #elif defined(_EVENT_IN_DOXYGEN) 339 /** 340 @name Socket error functions 341 342 These functions are needed for making programs compatible between 343 Windows and Unix-like platforms. 344 345 You see, Winsock handles socket errors differently from the rest of 346 the world. Elsewhere, a socket error is like any other error and is 347 stored in errno. But winsock functions require you to retrieve the 348 error with a special function, and don't let you use strerror for 349 the error codes. And handling EWOULDBLOCK is ... different. 350 351 @{ 352 */ 353 /** Return the most recent socket error. Not idempotent on all platforms. */ 354 #define EVUTIL_SOCKET_ERROR() ... 355 /** Replace the most recent socket error with errcode */ 356 #define EVUTIL_SET_SOCKET_ERROR(errcode) ... 357 /** Return the most recent socket error to occur on sock. */ 358 #define evutil_socket_geterror(sock) ... 359 /** Convert a socket error to a string. */ 360 #define evutil_socket_error_to_string(errcode) ... 361 /**@}*/ 362 #else 363 #define EVUTIL_SOCKET_ERROR() (errno) 364 #define EVUTIL_SET_SOCKET_ERROR(errcode) \ 365 do { errno = (errcode); } while (0) 366 #define evutil_socket_geterror(sock) (errno) 367 #define evutil_socket_error_to_string(errcode) (strerror(errcode)) 368 #endif 369 370 371 /** 372 * @name Manipulation macros for struct timeval. 373 * 374 * We define replacements 375 * for timeradd, timersub, timerclear, timercmp, and timerisset. 376 * 377 * @{ 378 */ 379 #ifdef _EVENT_HAVE_TIMERADD 380 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp)) 381 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp)) 382 #else 383 #define evutil_timeradd(tvp, uvp, vvp) \ 384 do { \ 385 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 386 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 387 if ((vvp)->tv_usec >= 1000000) { \ 388 (vvp)->tv_sec++; \ 389 (vvp)->tv_usec -= 1000000; \ 390 } \ 391 } while (0) 392 #define evutil_timersub(tvp, uvp, vvp) \ 393 do { \ 394 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 395 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 396 if ((vvp)->tv_usec < 0) { \ 397 (vvp)->tv_sec--; \ 398 (vvp)->tv_usec += 1000000; \ 399 } \ 400 } while (0) 401 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */ 402 403 #ifdef _EVENT_HAVE_TIMERCLEAR 404 #define evutil_timerclear(tvp) timerclear(tvp) 405 #else 406 #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 407 #endif 408 /**@}*/ 409 410 /** Return true iff the tvp is related to uvp according to the relational 411 * operator cmp. Recognized values for cmp are ==, <=, <, >=, and >. */ 412 #define evutil_timercmp(tvp, uvp, cmp) \ 413 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 414 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 415 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 416 417 #ifdef _EVENT_HAVE_TIMERISSET 418 #define evutil_timerisset(tvp) timerisset(tvp) 419 #else 420 #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 421 #endif 422 423 /** Replacement for offsetof on platforms that don't define it. */ 424 #ifdef offsetof 425 #define evutil_offsetof(type, field) offsetof(type, field) 426 #else 427 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field)) 428 #endif 429 430 /* big-int related functions */ 431 /** Parse a 64-bit value from a string. Arguments are as for strtol. */ 432 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base); 433 434 /** Replacement for gettimeofday on platforms that lack it. */ 435 #ifdef _EVENT_HAVE_GETTIMEOFDAY 436 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz)) 437 #else 438 struct timezone; 439 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz); 440 #endif 441 442 /** Replacement for snprintf to get consistent behavior on platforms for 443 which the return value of snprintf does not conform to C99. 444 */ 445 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...) 446 #ifdef __GNUC__ 447 __attribute__((format(printf, 3, 4))) 448 #endif 449 ; 450 /** Replacement for vsnprintf to get consistent behavior on platforms for 451 which the return value of snprintf does not conform to C99. 452 */ 453 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap) 454 #ifdef __GNUC__ 455 __attribute__((format(printf, 3, 0))) 456 #endif 457 ; 458 459 /** Replacement for inet_ntop for platforms which lack it. */ 460 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len); 461 /** Replacement for inet_pton for platforms which lack it. */ 462 int evutil_inet_pton(int af, const char *src, void *dst); 463 struct sockaddr; 464 465 /** Parse an IPv4 or IPv6 address, with optional port, from a string. 466 467 Recognized formats are: 468 - [IPv6Address]:port 469 - [IPv6Address] 470 - IPv6Address 471 - IPv4Address:port 472 - IPv4Address 473 474 If no port is specified, the port in the output is set to 0. 475 476 @param str The string to parse. 477 @param out A struct sockaddr to hold the result. This should probably be 478 a struct sockaddr_storage. 479 @param outlen A pointer to the number of bytes that that 'out' can safely 480 hold. Set to the number of bytes used in 'out' on success. 481 @return -1 if the address is not well-formed, if the port is out of range, 482 or if out is not large enough to hold the result. Otherwise returns 483 0 on success. 484 */ 485 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen); 486 487 /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1 488 * preceeds sa2, or greater than 0 if sa1 follows sa2. If include_port is 489 * true, consider the port as well as the address. Only implemented for 490 * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain 491 * the same between Libevent versions. */ 492 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2, 493 int include_port); 494 495 /** As strcasecmp, but always compares the characters in locale-independent 496 ASCII. That's useful if you're handling data in ASCII-based protocols. 497 */ 498 int evutil_ascii_strcasecmp(const char *str1, const char *str2); 499 /** As strncasecmp, but always compares the characters in locale-independent 500 ASCII. That's useful if you're handling data in ASCII-based protocols. 501 */ 502 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n); 503 504 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it 505 * if this system has no getaddrinfo(). */ 506 #ifdef _EVENT_HAVE_STRUCT_ADDRINFO 507 #define evutil_addrinfo addrinfo 508 #else 509 /** A definition of struct addrinfo for systems that lack it. 510 511 (This is just an alias for struct addrinfo if the system defines 512 struct addrinfo.) 513 */ 514 struct evutil_addrinfo { 515 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */ 516 int ai_family; /* PF_xxx */ 517 int ai_socktype; /* SOCK_xxx */ 518 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ 519 size_t ai_addrlen; /* length of ai_addr */ 520 char *ai_canonname; /* canonical name for nodename */ 521 struct sockaddr *ai_addr; /* binary address */ 522 struct evutil_addrinfo *ai_next; /* next structure in linked list */ 523 }; 524 #endif 525 /** @name evutil_getaddrinfo() error codes 526 527 These values are possible error codes for evutil_getaddrinfo() and 528 related functions. 529 530 @{ 531 */ 532 #ifdef EAI_ADDRFAMILY 533 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY 534 #else 535 #define EVUTIL_EAI_ADDRFAMILY -901 536 #endif 537 #ifdef EAI_AGAIN 538 #define EVUTIL_EAI_AGAIN EAI_AGAIN 539 #else 540 #define EVUTIL_EAI_AGAIN -902 541 #endif 542 #ifdef EAI_BADFLAGS 543 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS 544 #else 545 #define EVUTIL_EAI_BADFLAGS -903 546 #endif 547 #ifdef EAI_FAIL 548 #define EVUTIL_EAI_FAIL EAI_FAIL 549 #else 550 #define EVUTIL_EAI_FAIL -904 551 #endif 552 #ifdef EAI_FAMILY 553 #define EVUTIL_EAI_FAMILY EAI_FAMILY 554 #else 555 #define EVUTIL_EAI_FAMILY -905 556 #endif 557 #ifdef EAI_MEMORY 558 #define EVUTIL_EAI_MEMORY EAI_MEMORY 559 #else 560 #define EVUTIL_EAI_MEMORY -906 561 #endif 562 /* This test is a bit complicated, since some MS SDKs decide to 563 * remove NODATA or redefine it to be the same as NONAME, in a 564 * fun interpretation of RFC 2553 and RFC 3493. */ 565 #if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME) 566 #define EVUTIL_EAI_NODATA EAI_NODATA 567 #else 568 #define EVUTIL_EAI_NODATA -907 569 #endif 570 #ifdef EAI_NONAME 571 #define EVUTIL_EAI_NONAME EAI_NONAME 572 #else 573 #define EVUTIL_EAI_NONAME -908 574 #endif 575 #ifdef EAI_SERVICE 576 #define EVUTIL_EAI_SERVICE EAI_SERVICE 577 #else 578 #define EVUTIL_EAI_SERVICE -909 579 #endif 580 #ifdef EAI_SOCKTYPE 581 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE 582 #else 583 #define EVUTIL_EAI_SOCKTYPE -910 584 #endif 585 #ifdef EAI_SYSTEM 586 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM 587 #else 588 #define EVUTIL_EAI_SYSTEM -911 589 #endif 590 591 #define EVUTIL_EAI_CANCEL -90001 592 593 #ifdef AI_PASSIVE 594 #define EVUTIL_AI_PASSIVE AI_PASSIVE 595 #else 596 #define EVUTIL_AI_PASSIVE 0x1000 597 #endif 598 #ifdef AI_CANONNAME 599 #define EVUTIL_AI_CANONNAME AI_CANONNAME 600 #else 601 #define EVUTIL_AI_CANONNAME 0x2000 602 #endif 603 #ifdef AI_NUMERICHOST 604 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST 605 #else 606 #define EVUTIL_AI_NUMERICHOST 0x4000 607 #endif 608 #ifdef AI_NUMERICSERV 609 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV 610 #else 611 #define EVUTIL_AI_NUMERICSERV 0x8000 612 #endif 613 #ifdef AI_V4MAPPED 614 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED 615 #else 616 #define EVUTIL_AI_V4MAPPED 0x10000 617 #endif 618 #ifdef AI_ALL 619 #define EVUTIL_AI_ALL AI_ALL 620 #else 621 #define EVUTIL_AI_ALL 0x20000 622 #endif 623 #ifdef AI_ADDRCONFIG 624 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG 625 #else 626 #define EVUTIL_AI_ADDRCONFIG 0x40000 627 #endif 628 /**@}*/ 629 630 struct evutil_addrinfo; 631 /** 632 * This function clones getaddrinfo for systems that don't have it. For full 633 * details, see RFC 3493, section 6.1. 634 * 635 * Limitations: 636 * - When the system has no getaddrinfo, we fall back to gethostbyname_r or 637 * gethostbyname, with their attendant issues. 638 * - The AI_V4MAPPED and AI_ALL flags are not currently implemented. 639 * 640 * For a nonblocking variant, see evdns_getaddrinfo. 641 */ 642 int evutil_getaddrinfo(const char *nodename, const char *servname, 643 const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res); 644 645 /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */ 646 void evutil_freeaddrinfo(struct evutil_addrinfo *ai); 647 648 const char *evutil_gai_strerror(int err); 649 650 /** Generate n bytes of secure pseudorandom data, and store them in buf. 651 * 652 * Current versions of Libevent use an ARC4-based random number generator, 653 * seeded using the platform's entropy source (/dev/urandom on Unix-like 654 * systems; CryptGenRandom on Windows). This is not actually as secure as it 655 * should be: ARC4 is a pretty lousy cipher, and the current implementation 656 * provides only rudimentary prediction- and backtracking-resistance. Don't 657 * use this for serious cryptographic applications. 658 */ 659 void evutil_secure_rng_get_bytes(void *buf, size_t n); 660 661 /** 662 * Seed the secure random number generator if needed, and return 0 on 663 * success or -1 on failure. 664 * 665 * It is okay to call this function more than once; it will still return 666 * 0 if the RNG has been successfully seeded and -1 if it can't be 667 * seeded. 668 * 669 * Ordinarily you don't need to call this function from your own code; 670 * Libevent will seed the RNG itself the first time it needs good random 671 * numbers. You only need to call it if (a) you want to double-check 672 * that one of the seeding methods did succeed, or (b) you plan to drop 673 * the capability to seed (by chrooting, or dropping capabilities, or 674 * whatever), and you want to make sure that seeding happens before your 675 * program loses the ability to do it. 676 */ 677 int evutil_secure_rng_init(void); 678 679 /** 680 * Set a filename to use in place of /dev/urandom for seeding the secure 681 * PRNG. Return 0 on success, -1 on failure. 682 * 683 * Call this function BEFORE calling any other initialization or RNG 684 * functions. 685 * 686 * (This string will _NOT_ be copied internally. Do not free it while any 687 * user of the secure RNG might be running. Don't pass anything other than a 688 * real /dev/...random device file here, or you might lose security.) 689 * 690 * This API is unstable, and might change in a future libevent version. 691 */ 692 int evutil_secure_rng_set_urandom_device_file(char *fname); 693 694 /** Seed the random number generator with extra random bytes. 695 696 You should almost never need to call this function; it should be 697 sufficient to invoke evutil_secure_rng_init(), or let Libevent take 698 care of calling evutil_secure_rng_init() on its own. 699 700 If you call this function as a _replacement_ for the regular 701 entropy sources, then you need to be sure that your input 702 contains a fairly large amount of strong entropy. Doing so is 703 notoriously hard: most people who try get it wrong. Watch out! 704 705 @param dat a buffer full of a strong source of random numbers 706 @param datlen the number of bytes to read from datlen 707 */ 708 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen); 709 710 #ifdef __cplusplus 711 } 712 #endif 713 714 #endif /* _EVUTIL_H_ */ 715