dnscrypto-proxy: Update to release 1.3.0
[tomato.git] / release / src / router / dnscrypt / src / libevent-modified / include / event2 / util.h
blobf4c8da04830ae57d8be2c37cc7ded1ecec0385e9
1 /*
2 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
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.
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.
26 #ifndef _EVENT2_UTIL_H_
27 #define _EVENT2_UTIL_H_
29 /** @file event2/util.h
31 Common convenience functions for cross-platform portability and
32 related socket manipulations.
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
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
66 #ifdef WIN32
67 #include <winsock2.h>
68 #else
69 #include <sys/socket.h>
70 #endif
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
77 /**
78 * @name Standard integer types.
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.)
86 * We define:
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>
104 * @{
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
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
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
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
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
190 #ifdef _EVENT_ssize_t
191 #define ev_ssize_t _EVENT_ssize_t
192 #else
193 #define ev_ssize_t ssize_t
194 #endif
196 #ifdef WIN32
197 #define ev_off_t ev_int64_t
198 #else
199 #define ev_off_t off_t
200 #endif
201 /**@}*/
203 /* Limits for integer types.
205 We're making two assumptions here:
206 - The compiler does constant folding properly.
207 - The platform does signed arithmetic in two's complement.
211 @name Limits for integer types
213 These macros hold the largest or smallest values possible for the
214 ev_[u]int*_t types.
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 /** @} */
233 @name Limits for SIZE_T and SSIZE_T
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
250 #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
251 /**@}*/
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
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
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
277 /** Create two new sockets that are connected to each other.
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.
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.)
286 Parameters and return values are as for socketpair()
288 int 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.
291 @param sock The socket to make nonblocking
292 @return 0 on success, -1 on failure
294 int evutil_make_socket_nonblocking(evutil_socket_t sock);
296 /** Do platform-specific operations to make a listener socket reusable.
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.
301 This differs from Windows's interpretation of "reusable", which
302 allows multiple listeners to bind the same address at the same time.
304 @param sock The socket to make reusable
305 @return 0 on success, -1 on failure
307 int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
309 /** Do platform-specific operations as needed to close a socket upon a
310 successful execution of one of the exec*() functions.
312 @param sock The socket to be closed
313 @return 0 on success, -1 on failure
315 int evutil_make_socket_closeonexec(evutil_socket_t sock);
317 /** Do the platform-specific call needed to close a socket returned from
318 socket() or accept().
320 @param sock The socket to be closed
321 @return 0 on success, -1 on failure
323 int evutil_closesocket(evutil_socket_t sock);
324 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
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. */
334 int evutil_socket_geterror(evutil_socket_t sock);
335 /** Convert a socket error to a string. */
336 const char *evutil_socket_error_to_string(int errcode);
337 #elif defined(_EVENT_IN_DOXYGEN)
339 @name Socket error functions
341 These functions are needed for making programs compatible between
342 Windows and Unix-like platforms.
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.
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
371 * @name Manipulation macros for struct timeval.
373 * We define replacements
374 * for timeradd, timersub, timerclear, timercmp, and timerisset.
376 * @{
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; \
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; \
399 } while (0)
400 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */
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 /**@}*/
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))
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
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
429 /* big-int related functions */
430 /** Parse a 64-bit value from a string. Arguments are as for strtol. */
431 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
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
437 struct timezone;
438 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
439 #endif
441 /** Replacement for snprintf to get consistent behavior on platforms for
442 which the return value of snprintf does not conform to C99.
444 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
445 #ifdef __GNUC__
446 __attribute__((format(printf, 3, 4)))
447 #endif
449 /** Replacement for vsnprintf to get consistent behavior on platforms for
450 which the return value of snprintf does not conform to C99.
452 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
453 #ifdef __GNUC__
454 __attribute__((format(printf, 3, 0)))
455 #endif
458 /** Replacement for inet_ntop for platforms which lack it. */
459 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
460 /** Replacement for inet_pton for platforms which lack it. */
461 int evutil_inet_pton(int af, const char *src, void *dst);
462 struct sockaddr;
464 /** Parse an IPv4 or IPv6 address, with optional port, from a string.
466 Recognized formats are:
467 - [IPv6Address]:port
468 - [IPv6Address]
469 - IPv6Address
470 - IPv4Address:port
471 - IPv4Address
473 If no port is specified, the port in the output is set to 0.
475 @param str The string to parse.
476 @param out A struct sockaddr to hold the result. This should probably be
477 a struct sockaddr_storage.
478 @param outlen A pointer to the number of bytes that that 'out' can safely
479 hold. Set to the number of bytes used in 'out' on success.
480 @return -1 if the address is not well-formed, if the port is out of range,
481 or if out is not large enough to hold the result. Otherwise returns
482 0 on success.
484 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
487 * Format an address to ipv4:port or [ipv6]:port
489 * @param sa A struct sockaddr
490 * @param out The output buffer
491 * @param outlen The output buffer length
492 * @return 0 on success
494 const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out,
495 size_t outlen);
497 /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
498 * preceeds sa2, or greater than 0 if sa1 follows sa2. If include_port is
499 * true, consider the port as well as the address. Only implemented for
500 * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
501 * the same between Libevent versions. */
502 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
503 int include_port);
505 /** As strcasecmp, but always compares the characters in locale-independent
506 ASCII. That's useful if you're handling data in ASCII-based protocols.
508 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
509 /** As strncasecmp, but always compares the characters in locale-independent
510 ASCII. That's useful if you're handling data in ASCII-based protocols.
512 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
514 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
515 * if this system has no getaddrinfo(). */
516 #ifdef _EVENT_HAVE_STRUCT_ADDRINFO
517 #define evutil_addrinfo addrinfo
518 #else
519 /** A definition of struct addrinfo for systems that lack it.
521 (This is just an alias for struct addrinfo if the system defines
522 struct addrinfo.)
524 struct evutil_addrinfo {
525 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
526 int ai_family; /* PF_xxx */
527 int ai_socktype; /* SOCK_xxx */
528 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
529 size_t ai_addrlen; /* length of ai_addr */
530 char *ai_canonname; /* canonical name for nodename */
531 struct sockaddr *ai_addr; /* binary address */
532 struct evutil_addrinfo *ai_next; /* next structure in linked list */
534 #endif
535 /** @name evutil_getaddrinfo() error codes
537 These values are possible error codes for evutil_getaddrinfo() and
538 related functions.
542 #ifdef EAI_ADDRFAMILY
543 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
544 #else
545 #define EVUTIL_EAI_ADDRFAMILY -901
546 #endif
547 #ifdef EAI_AGAIN
548 #define EVUTIL_EAI_AGAIN EAI_AGAIN
549 #else
550 #define EVUTIL_EAI_AGAIN -902
551 #endif
552 #ifdef EAI_BADFLAGS
553 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
554 #else
555 #define EVUTIL_EAI_BADFLAGS -903
556 #endif
557 #ifdef EAI_FAIL
558 #define EVUTIL_EAI_FAIL EAI_FAIL
559 #else
560 #define EVUTIL_EAI_FAIL -904
561 #endif
562 #ifdef EAI_FAMILY
563 #define EVUTIL_EAI_FAMILY EAI_FAMILY
564 #else
565 #define EVUTIL_EAI_FAMILY -905
566 #endif
567 #ifdef EAI_MEMORY
568 #define EVUTIL_EAI_MEMORY EAI_MEMORY
569 #else
570 #define EVUTIL_EAI_MEMORY -906
571 #endif
572 /* This test is a bit complicated, since some MS SDKs decide to
573 * remove NODATA or redefine it to be the same as NONAME, in a
574 * fun interpretation of RFC 2553 and RFC 3493. */
575 #if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
576 #define EVUTIL_EAI_NODATA EAI_NODATA
577 #else
578 #define EVUTIL_EAI_NODATA -907
579 #endif
580 #ifdef EAI_NONAME
581 #define EVUTIL_EAI_NONAME EAI_NONAME
582 #else
583 #define EVUTIL_EAI_NONAME -908
584 #endif
585 #ifdef EAI_SERVICE
586 #define EVUTIL_EAI_SERVICE EAI_SERVICE
587 #else
588 #define EVUTIL_EAI_SERVICE -909
589 #endif
590 #ifdef EAI_SOCKTYPE
591 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
592 #else
593 #define EVUTIL_EAI_SOCKTYPE -910
594 #endif
595 #ifdef EAI_SYSTEM
596 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
597 #else
598 #define EVUTIL_EAI_SYSTEM -911
599 #endif
601 #define EVUTIL_EAI_CANCEL -90001
603 #ifdef AI_PASSIVE
604 #define EVUTIL_AI_PASSIVE AI_PASSIVE
605 #else
606 #define EVUTIL_AI_PASSIVE 0x1000
607 #endif
608 #ifdef AI_CANONNAME
609 #define EVUTIL_AI_CANONNAME AI_CANONNAME
610 #else
611 #define EVUTIL_AI_CANONNAME 0x2000
612 #endif
613 #ifdef AI_NUMERICHOST
614 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
615 #else
616 #define EVUTIL_AI_NUMERICHOST 0x4000
617 #endif
618 #ifdef AI_NUMERICSERV
619 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
620 #else
621 #define EVUTIL_AI_NUMERICSERV 0x8000
622 #endif
623 #ifdef AI_V4MAPPED
624 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
625 #else
626 #define EVUTIL_AI_V4MAPPED 0x10000
627 #endif
628 #ifdef AI_ALL
629 #define EVUTIL_AI_ALL AI_ALL
630 #else
631 #define EVUTIL_AI_ALL 0x20000
632 #endif
633 #ifdef AI_ADDRCONFIG
634 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
635 #else
636 #define EVUTIL_AI_ADDRCONFIG 0x40000
637 #endif
638 /**@}*/
640 struct evutil_addrinfo;
642 * This function clones getaddrinfo for systems that don't have it. For full
643 * details, see RFC 3493, section 6.1.
645 * Limitations:
646 * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
647 * gethostbyname, with their attendant issues.
648 * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
650 * For a nonblocking variant, see evdns_getaddrinfo.
652 int evutil_getaddrinfo(const char *nodename, const char *servname,
653 const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
655 /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
656 void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
658 const char *evutil_gai_strerror(int err);
660 /** Generate n bytes of secure pseudorandom data, and store them in buf.
662 * By default, Libevent uses an ARC4-based random number generator, seeded
663 * using the platform's entropy source (/dev/urandom on Unix-like systems;
664 * CryptGenRandom on Windows).
666 void evutil_secure_rng_get_bytes(void *buf, size_t n);
669 * Seed the secure random number generator if needed, and return 0 on
670 * success or -1 on failure.
672 * It is okay to call this function more than once; it will still return
673 * 0 if the RNG has been successfully seeded and -1 if it can't be
674 * seeded.
676 * Ordinarily you don't need to call this function from your own code;
677 * Libevent will seed the RNG itself the first time it needs good random
678 * numbers. You only need to call it if (a) you want to double-check
679 * that one of the seeding methods did succeed, or (b) you plan to drop
680 * the capability to seed (by chrooting, or dropping capabilities, or
681 * whatever), and you want to make sure that seeding happens before your
682 * program loses the ability to do it.
684 int evutil_secure_rng_init(void);
686 /** Seed the random number generator with extra random bytes.
688 You should almost never need to call this function; it should be
689 sufficient to invoke evutil_secure_rng_init(), or let Libevent take
690 care of calling evutil_secure_rng_init() on its own.
692 If you call this function as a _replacement_ for the regular
693 entropy sources, then you need to be sure that your input
694 contains a fairly large amount of strong entropy. Doing so is
695 notoriously hard: most people who try get it wrong. Watch out!
697 @param dat a buffer full of a strong source of random numbers
698 @param datlen the number of bytes to read from datlen
700 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
702 #ifdef __cplusplus
704 #endif
706 #endif /* _EVUTIL_H_ */