1 /* $NetBSD: util.h,v 1.2 2013/04/11 16:56:42 christos 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 UINT64_MAX 220 #define EV_INT64_MAX INT64_MAX 221 #define EV_INT64_MIN INT64_MIN 222 #define EV_UINT32_MAX UINT32_MAX 223 #define EV_INT32_MAX INT32_MAX 224 #define EV_INT32_MIN INT32_MIN 225 #define EV_UINT16_MAX UINT16_MAX 226 #define EV_INT16_MAX INT16_MAX 227 #define EV_INT16_MIN INT16_MIN 228 #define EV_UINT8_MAX UINT8_MAX 229 #define EV_INT8_MAX INT8_MAX 230 #define EV_INT8_MIN INT8_MIN 231 /** @} */ 232 233 /** 234 @name Limits for SIZE_T and SSIZE_T 235 236 @{ 237 */ 238 #define EV_SIZE_MAX SIZE_MAX 239 #define EV_SSIZE_MAX SSIZE_MAX 240 #define EV_SSIZE_MIN SSIZE_MIN 241 /**@}*/ 242 243 #ifdef WIN32 244 #define ev_socklen_t int 245 #elif defined(_EVENT_socklen_t) 246 #define ev_socklen_t _EVENT_socklen_t 247 #else 248 #define ev_socklen_t socklen_t 249 #endif 250 251 #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY 252 #if !defined(_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \ 253 && !defined(ss_family) 254 #define ss_family __ss_family 255 #endif 256 #endif 257 258 /** 259 * A type wide enough to hold the output of "socket()" or "accept()". On 260 * Windows, this is an intptr_t; elsewhere, it is an int. */ 261 #ifdef WIN32 262 #define evutil_socket_t intptr_t 263 #else 264 #define evutil_socket_t int 265 #endif 266 267 /** Create two new sockets that are connected to each other. 268 269 On Unix, this simply calls socketpair(). On Windows, it uses the 270 loopback network interface on 127.0.0.1, and only 271 AF_INET,SOCK_STREAM are supported. 272 273 (This may fail on some Windows hosts where firewall software has cleverly 274 decided to keep 127.0.0.1 from talking to itself.) 275 276 Parameters and return values are as for socketpair() 277 */ 278 int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]); 279 /** Do platform-specific operations as needed to make a socket nonblocking. 280 281 @param sock The socket to make nonblocking 282 @return 0 on success, -1 on failure 283 */ 284 int evutil_make_socket_nonblocking(evutil_socket_t sock); 285 286 /** Do platform-specific operations to make a listener socket reusable. 287 288 Specifically, we want to make sure that another program will be able 289 to bind this address right after we've closed the listener. 290 291 This differs from Windows's interpretation of "reusable", which 292 allows multiple listeners to bind the same address at the same time. 293 294 @param sock The socket to make reusable 295 @return 0 on success, -1 on failure 296 */ 297 int evutil_make_listen_socket_reuseable(evutil_socket_t sock); 298 299 /** Do platform-specific operations as needed to close a socket upon a 300 successful execution of one of the exec*() functions. 301 302 @param sock The socket to be closed 303 @return 0 on success, -1 on failure 304 */ 305 int evutil_make_socket_closeonexec(evutil_socket_t sock); 306 307 /** Do the platform-specific call needed to close a socket returned from 308 socket() or accept(). 309 310 @param sock The socket to be closed 311 @return 0 on success, -1 on failure 312 */ 313 int evutil_closesocket(evutil_socket_t sock); 314 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s) 315 316 317 #ifdef WIN32 318 /** Return the most recent socket error. Not idempotent on all platforms. */ 319 #define EVUTIL_SOCKET_ERROR() WSAGetLastError() 320 /** Replace the most recent socket error with errcode */ 321 #define EVUTIL_SET_SOCKET_ERROR(errcode) \ 322 do { WSASetLastError(errcode); } while (/*CONSTCOND*/0) 323 /** Return the most recent socket error to occur on sock. */ 324 int evutil_socket_geterror(evutil_socket_t sock); 325 /** Convert a socket error to a string. */ 326 const char *evutil_socket_error_to_string(int errcode); 327 #elif defined(_EVENT_IN_DOXYGEN) 328 /** 329 @name Socket error functions 330 331 These functions are needed for making programs compatible between 332 Windows and Unix-like platforms. 333 334 You see, Winsock handles socket errors differently from the rest of 335 the world. Elsewhere, a socket error is like any other error and is 336 stored in errno. But winsock functions require you to retrieve the 337 error with a special function, and don't let you use strerror for 338 the error codes. And handling EWOULDBLOCK is ... different. 339 340 @{ 341 */ 342 /** Return the most recent socket error. Not idempotent on all platforms. */ 343 #define EVUTIL_SOCKET_ERROR() ... 344 /** Replace the most recent socket error with errcode */ 345 #define EVUTIL_SET_SOCKET_ERROR(errcode) ... 346 /** Return the most recent socket error to occur on sock. */ 347 #define evutil_socket_geterror(sock) ... 348 /** Convert a socket error to a string. */ 349 #define evutil_socket_error_to_string(errcode) ... 350 /**@}*/ 351 #else 352 #define EVUTIL_SOCKET_ERROR() (errno) 353 #define EVUTIL_SET_SOCKET_ERROR(errcode) \ 354 do { errno = (errcode); } while (/*CONSTCOND*/0) 355 #define evutil_socket_geterror(sock) (errno) 356 #define evutil_socket_error_to_string(errcode) (strerror(errcode)) 357 #endif 358 359 360 /** 361 * @name Manipulation macros for struct timeval. 362 * 363 * We define replacements 364 * for timeradd, timersub, timerclear, timercmp, and timerisset. 365 * 366 * @{ 367 */ 368 #ifdef _EVENT_HAVE_TIMERADD 369 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp)) 370 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp)) 371 #else 372 #define evutil_timeradd(tvp, uvp, vvp) \ 373 do { \ 374 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 375 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 376 if ((vvp)->tv_usec >= 1000000) { \ 377 (vvp)->tv_sec++; \ 378 (vvp)->tv_usec -= 1000000; \ 379 } \ 380 } while (/*CONSTCOND*/0) 381 #define evutil_timersub(tvp, uvp, vvp) \ 382 do { \ 383 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 384 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 385 if ((vvp)->tv_usec < 0) { \ 386 (vvp)->tv_sec--; \ 387 (vvp)->tv_usec += 1000000; \ 388 } \ 389 } while (/*CONSTCOND*/0) 390 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */ 391 392 #ifdef _EVENT_HAVE_TIMERCLEAR 393 #define evutil_timerclear(tvp) timerclear(tvp) 394 #else 395 #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 396 #endif 397 /**@}*/ 398 399 /** Return true iff the tvp is related to uvp according to the relational 400 * operator cmp. Recognized values for cmp are ==, <=, <, >=, and >. */ 401 #define evutil_timercmp(tvp, uvp, cmp) \ 402 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 403 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 404 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 405 406 #ifdef _EVENT_HAVE_TIMERISSET 407 #define evutil_timerisset(tvp) timerisset(tvp) 408 #else 409 #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 410 #endif 411 412 /** Replacement for offsetof on platforms that don't define it. */ 413 #ifdef offsetof 414 #define evutil_offsetof(type, field) offsetof(type, field) 415 #else 416 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field)) 417 #endif 418 419 /* big-int related functions */ 420 /** Parse a 64-bit value from a string. Arguments are as for strtol. */ 421 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base); 422 423 /** Replacement for gettimeofday on platforms that lack it. */ 424 #ifdef _EVENT_HAVE_GETTIMEOFDAY 425 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz)) 426 #else 427 struct timezone; 428 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz); 429 #endif 430 431 /** Replacement for snprintf to get consistent behavior on platforms for 432 which the return value of snprintf does not conform to C99. 433 */ 434 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...) 435 #ifdef __GNUC__ 436 __attribute__((format(printf, 3, 4))) 437 #endif 438 ; 439 /** Replacement for vsnprintf to get consistent behavior on platforms for 440 which the return value of snprintf does not conform to C99. 441 */ 442 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap) 443 #ifdef __GNUC__ 444 __attribute__((format(printf, 3, 0))) 445 #endif 446 ; 447 448 /** Replacement for inet_ntop for platforms which lack it. */ 449 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len); 450 /** Replacement for inet_pton for platforms which lack it. */ 451 int evutil_inet_pton(int af, const char *src, void *dst); 452 struct sockaddr; 453 454 /** Parse an IPv4 or IPv6 address, with optional port, from a string. 455 456 Recognized formats are: 457 - [IPv6Address]:port 458 - [IPv6Address] 459 - IPv6Address 460 - IPv4Address:port 461 - IPv4Address 462 463 If no port is specified, the port in the output is set to 0. 464 465 @param str The string to parse. 466 @param out A struct sockaddr to hold the result. This should probably be 467 a struct sockaddr_storage. 468 @param outlen A pointer to the number of bytes that that 'out' can safely 469 hold. Set to the number of bytes used in 'out' on success. 470 @return -1 if the address is not well-formed, if the port is out of range, 471 or if out is not large enough to hold the result. Otherwise returns 472 0 on success. 473 */ 474 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen); 475 476 /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1 477 * preceeds sa2, or greater than 0 if sa1 follows sa2. If include_port is 478 * true, consider the port as well as the address. Only implemented for 479 * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain 480 * the same between Libevent versions. */ 481 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2, 482 int include_port); 483 484 /** As strcasecmp, but always compares the characters in locale-independent 485 ASCII. That's useful if you're handling data in ASCII-based protocols. 486 */ 487 int evutil_ascii_strcasecmp(const char *str1, const char *str2); 488 /** As strncasecmp, but always compares the characters in locale-independent 489 ASCII. That's useful if you're handling data in ASCII-based protocols. 490 */ 491 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n); 492 493 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it 494 * if this system has no getaddrinfo(). */ 495 #ifdef _EVENT_HAVE_STRUCT_ADDRINFO 496 #define evutil_addrinfo addrinfo 497 #else 498 /** A definition of struct addrinfo for systems that lack it. 499 500 (This is just an alias for struct addrinfo if the system defines 501 struct addrinfo.) 502 */ 503 struct evutil_addrinfo { 504 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */ 505 int ai_family; /* PF_xxx */ 506 int ai_socktype; /* SOCK_xxx */ 507 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ 508 size_t ai_addrlen; /* length of ai_addr */ 509 char *ai_canonname; /* canonical name for nodename */ 510 struct sockaddr *ai_addr; /* binary address */ 511 struct evutil_addrinfo *ai_next; /* next structure in linked list */ 512 }; 513 #endif 514 /** @name evutil_getaddrinfo() error codes 515 516 These values are possible error codes for evutil_getaddrinfo() and 517 related functions. 518 519 @{ 520 */ 521 #ifdef EAI_ADDRFAMILY 522 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY 523 #else 524 #define EVUTIL_EAI_ADDRFAMILY -901 525 #endif 526 #ifdef EAI_AGAIN 527 #define EVUTIL_EAI_AGAIN EAI_AGAIN 528 #else 529 #define EVUTIL_EAI_AGAIN -902 530 #endif 531 #ifdef EAI_BADFLAGS 532 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS 533 #else 534 #define EVUTIL_EAI_BADFLAGS -903 535 #endif 536 #ifdef EAI_FAIL 537 #define EVUTIL_EAI_FAIL EAI_FAIL 538 #else 539 #define EVUTIL_EAI_FAIL -904 540 #endif 541 #ifdef EAI_FAMILY 542 #define EVUTIL_EAI_FAMILY EAI_FAMILY 543 #else 544 #define EVUTIL_EAI_FAMILY -905 545 #endif 546 #ifdef EAI_MEMORY 547 #define EVUTIL_EAI_MEMORY EAI_MEMORY 548 #else 549 #define EVUTIL_EAI_MEMORY -906 550 #endif 551 /* This test is a bit complicated, since some MS SDKs decide to 552 * remove NODATA or redefine it to be the same as NONAME, in a 553 * fun interpretation of RFC 2553 and RFC 3493. */ 554 #if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME) 555 #define EVUTIL_EAI_NODATA EAI_NODATA 556 #else 557 #define EVUTIL_EAI_NODATA -907 558 #endif 559 #ifdef EAI_NONAME 560 #define EVUTIL_EAI_NONAME EAI_NONAME 561 #else 562 #define EVUTIL_EAI_NONAME -908 563 #endif 564 #ifdef EAI_SERVICE 565 #define EVUTIL_EAI_SERVICE EAI_SERVICE 566 #else 567 #define EVUTIL_EAI_SERVICE -909 568 #endif 569 #ifdef EAI_SOCKTYPE 570 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE 571 #else 572 #define EVUTIL_EAI_SOCKTYPE -910 573 #endif 574 #ifdef EAI_SYSTEM 575 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM 576 #else 577 #define EVUTIL_EAI_SYSTEM -911 578 #endif 579 580 #define EVUTIL_EAI_CANCEL -90001 581 582 #ifdef AI_PASSIVE 583 #define EVUTIL_AI_PASSIVE AI_PASSIVE 584 #else 585 #define EVUTIL_AI_PASSIVE 0x1000 586 #endif 587 #ifdef AI_CANONNAME 588 #define EVUTIL_AI_CANONNAME AI_CANONNAME 589 #else 590 #define EVUTIL_AI_CANONNAME 0x2000 591 #endif 592 #ifdef AI_NUMERICHOST 593 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST 594 #else 595 #define EVUTIL_AI_NUMERICHOST 0x4000 596 #endif 597 #ifdef AI_NUMERICSERV 598 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV 599 #else 600 #define EVUTIL_AI_NUMERICSERV 0x8000 601 #endif 602 #ifdef AI_V4MAPPED 603 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED 604 #else 605 #define EVUTIL_AI_V4MAPPED 0x10000 606 #endif 607 #ifdef AI_ALL 608 #define EVUTIL_AI_ALL AI_ALL 609 #else 610 #define EVUTIL_AI_ALL 0x20000 611 #endif 612 #ifdef AI_ADDRCONFIG 613 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG 614 #else 615 #define EVUTIL_AI_ADDRCONFIG 0x40000 616 #endif 617 /**@}*/ 618 619 struct evutil_addrinfo; 620 /** 621 * This function clones getaddrinfo for systems that don't have it. For full 622 * details, see RFC 3493, section 6.1. 623 * 624 * Limitations: 625 * - When the system has no getaddrinfo, we fall back to gethostbyname_r or 626 * gethostbyname, with their attendant issues. 627 * - The AI_V4MAPPED and AI_ALL flags are not currently implemented. 628 * 629 * For a nonblocking variant, see evdns_getaddrinfo. 630 */ 631 int evutil_getaddrinfo(const char *nodename, const char *servname, 632 const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res); 633 634 /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */ 635 void evutil_freeaddrinfo(struct evutil_addrinfo *ai); 636 637 const char *evutil_gai_strerror(int err); 638 639 /** Generate n bytes of secure pseudorandom data, and store them in buf. 640 * 641 * By default, Libevent uses an ARC4-based random number generator, seeded 642 * using the platform's entropy source (/dev/urandom on Unix-like systems; 643 * CryptGenRandom on Windows). 644 */ 645 void evutil_secure_rng_get_bytes(void *buf, size_t n); 646 647 /** 648 * Seed the secure random number generator if needed, and return 0 on 649 * success or -1 on failure. 650 * 651 * It is okay to call this function more than once; it will still return 652 * 0 if the RNG has been successfully seeded and -1 if it can't be 653 * seeded. 654 * 655 * Ordinarily you don't need to call this function from your own code; 656 * Libevent will seed the RNG itself the first time it needs good random 657 * numbers. You only need to call it if (a) you want to double-check 658 * that one of the seeding methods did succeed, or (b) you plan to drop 659 * the capability to seed (by chrooting, or dropping capabilities, or 660 * whatever), and you want to make sure that seeding happens before your 661 * program loses the ability to do it. 662 */ 663 int evutil_secure_rng_init(void); 664 665 /** Seed the random number generator with extra random bytes. 666 667 You should almost never need to call this function; it should be 668 sufficient to invoke evutil_secure_rng_init(), or let Libevent take 669 care of calling evutil_secure_rng_init() on its own. 670 671 If you call this function as a _replacement_ for the regular 672 entropy sources, then you need to be sure that your input 673 contains a fairly large amount of strong entropy. Doing so is 674 notoriously hard: most people who try get it wrong. Watch out! 675 676 @param dat a buffer full of a strong source of random numbers 677 @param datlen the number of bytes to read from datlen 678 */ 679 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen); 680 681 #ifdef __cplusplus 682 } 683 #endif 684 685 #endif /* _EVUTIL_H_ */ 686