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