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
37 extern "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 */
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.
290 
291     @param sock The socket to make nonblocking
292     @return 0 on success, -1 on failure
293  */
294 int 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  */
307 int 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  */
315 int 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  */
323 int 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. */
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)
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. */
431 ev_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
437 struct timezone;
438 int 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  */
444 int 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  */
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
456 ;
457 
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;
463 
464 /** Parse an IPv4 or IPv6 address, with optional port, from a string.
465 
466     Recognized formats are:
467     - [IPv6Address]:port
468     - [IPv6Address]
469     - IPv6Address
470     - IPv4Address:port
471     - IPv4Address
472 
473     If no port is specified, the port in the output is set to 0.
474 
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.
483 */
484 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
485 
486 /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
487  * preceeds sa2, or greater than 0 if sa1 follows sa2.  If include_port is
488  * true, consider the port as well as the address.  Only implemented for
489  * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
490  * the same between Libevent versions. */
491 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
492     int include_port);
493 
494 /** As strcasecmp, but always compares the characters in locale-independent
495     ASCII.  That's useful if you're handling data in ASCII-based protocols.
496  */
497 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
498 /** As strncasecmp, but always compares the characters in locale-independent
499     ASCII.  That's useful if you're handling data in ASCII-based protocols.
500  */
501 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
502 
503 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
504  * if this system has no getaddrinfo(). */
505 #ifdef _EVENT_HAVE_STRUCT_ADDRINFO
506 #define evutil_addrinfo addrinfo
507 #else
508 /** A definition of struct addrinfo for systems that lack it.
509 
510     (This is just an alias for struct addrinfo if the system defines
511     struct addrinfo.)
512 */
513 struct evutil_addrinfo {
514 	int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
515 	int     ai_family;    /* PF_xxx */
516 	int     ai_socktype;  /* SOCK_xxx */
517 	int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
518 	size_t  ai_addrlen;   /* length of ai_addr */
519 	char   *ai_canonname; /* canonical name for nodename */
520 	struct sockaddr  *ai_addr; /* binary address */
521 	struct evutil_addrinfo  *ai_next; /* next structure in linked list */
522 };
523 #endif
524 /** @name evutil_getaddrinfo() error codes
525 
526     These values are possible error codes for evutil_getaddrinfo() and
527     related functions.
528 
529     @{
530 */
531 #ifdef EAI_ADDRFAMILY
532 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
533 #else
534 #define EVUTIL_EAI_ADDRFAMILY -901
535 #endif
536 #ifdef EAI_AGAIN
537 #define EVUTIL_EAI_AGAIN EAI_AGAIN
538 #else
539 #define EVUTIL_EAI_AGAIN -902
540 #endif
541 #ifdef EAI_BADFLAGS
542 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
543 #else
544 #define EVUTIL_EAI_BADFLAGS -903
545 #endif
546 #ifdef EAI_FAIL
547 #define EVUTIL_EAI_FAIL EAI_FAIL
548 #else
549 #define EVUTIL_EAI_FAIL -904
550 #endif
551 #ifdef EAI_FAMILY
552 #define EVUTIL_EAI_FAMILY EAI_FAMILY
553 #else
554 #define EVUTIL_EAI_FAMILY -905
555 #endif
556 #ifdef EAI_MEMORY
557 #define EVUTIL_EAI_MEMORY EAI_MEMORY
558 #else
559 #define EVUTIL_EAI_MEMORY -906
560 #endif
561 /* This test is a bit complicated, since some MS SDKs decide to
562  * remove NODATA or redefine it to be the same as NONAME, in a
563  * fun interpretation of RFC 2553 and RFC 3493. */
564 #if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
565 #define EVUTIL_EAI_NODATA EAI_NODATA
566 #else
567 #define EVUTIL_EAI_NODATA -907
568 #endif
569 #ifdef EAI_NONAME
570 #define EVUTIL_EAI_NONAME EAI_NONAME
571 #else
572 #define EVUTIL_EAI_NONAME -908
573 #endif
574 #ifdef EAI_SERVICE
575 #define EVUTIL_EAI_SERVICE EAI_SERVICE
576 #else
577 #define EVUTIL_EAI_SERVICE -909
578 #endif
579 #ifdef EAI_SOCKTYPE
580 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
581 #else
582 #define EVUTIL_EAI_SOCKTYPE -910
583 #endif
584 #ifdef EAI_SYSTEM
585 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
586 #else
587 #define EVUTIL_EAI_SYSTEM -911
588 #endif
589 
590 #define EVUTIL_EAI_CANCEL -90001
591 
592 #ifdef AI_PASSIVE
593 #define EVUTIL_AI_PASSIVE AI_PASSIVE
594 #else
595 #define EVUTIL_AI_PASSIVE 0x1000
596 #endif
597 #ifdef AI_CANONNAME
598 #define EVUTIL_AI_CANONNAME AI_CANONNAME
599 #else
600 #define EVUTIL_AI_CANONNAME 0x2000
601 #endif
602 #ifdef AI_NUMERICHOST
603 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
604 #else
605 #define EVUTIL_AI_NUMERICHOST 0x4000
606 #endif
607 #ifdef AI_NUMERICSERV
608 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
609 #else
610 #define EVUTIL_AI_NUMERICSERV 0x8000
611 #endif
612 #ifdef AI_V4MAPPED
613 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
614 #else
615 #define EVUTIL_AI_V4MAPPED 0x10000
616 #endif
617 #ifdef AI_ALL
618 #define EVUTIL_AI_ALL AI_ALL
619 #else
620 #define EVUTIL_AI_ALL 0x20000
621 #endif
622 #ifdef AI_ADDRCONFIG
623 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
624 #else
625 #define EVUTIL_AI_ADDRCONFIG 0x40000
626 #endif
627 /**@}*/
628 
629 struct evutil_addrinfo;
630 /**
631  * This function clones getaddrinfo for systems that don't have it.  For full
632  * details, see RFC 3493, section 6.1.
633  *
634  * Limitations:
635  * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
636  *   gethostbyname, with their attendant issues.
637  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
638  *
639  * For a nonblocking variant, see evdns_getaddrinfo.
640  */
641 int evutil_getaddrinfo(const char *nodename, const char *servname,
642     const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
643 
644 /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
645 void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
646 
647 const char *evutil_gai_strerror(int err);
648 
649 /** Generate n bytes of secure pseudorandom data, and store them in buf.
650  *
651  * Current versions of Libevent use an ARC4-based random number generator,
652  * seeded using the platform's entropy source (/dev/urandom on Unix-like
653  * systems; CryptGenRandom on Windows).  This is not actually as secure as it
654  * should be: ARC4 is a pretty lousy cipher, and the current implementation
655  * provides only rudimentary prediction- and backtracking-resistance.  Don't
656  * use this for serious cryptographic applications.
657  */
658 void evutil_secure_rng_get_bytes(void *buf, size_t n);
659 
660 /**
661  * Seed the secure random number generator if needed, and return 0 on
662  * success or -1 on failure.
663  *
664  * It is okay to call this function more than once; it will still return
665  * 0 if the RNG has been successfully seeded and -1 if it can't be
666  * seeded.
667  *
668  * Ordinarily you don't need to call this function from your own code;
669  * Libevent will seed the RNG itself the first time it needs good random
670  * numbers.  You only need to call it if (a) you want to double-check
671  * that one of the seeding methods did succeed, or (b) you plan to drop
672  * the capability to seed (by chrooting, or dropping capabilities, or
673  * whatever), and you want to make sure that seeding happens before your
674  * program loses the ability to do it.
675  */
676 int evutil_secure_rng_init(void);
677 
678 /**
679  * Set a filename to use in place of /dev/urandom for seeding the secure
680  * PRNG. Return 0 on success, -1 on failure.
681  *
682  * Call this function BEFORE calling any other initialization or RNG
683  * functions.
684  *
685  * (This string will _NOT_ be copied internally. Do not free it while any
686  * user of the secure RNG might be running. Don't pass anything other than a
687  * real /dev/...random device file here, or you might lose security.)
688  *
689  * This API is unstable, and might change in a future libevent version.
690  */
691 int evutil_secure_rng_set_urandom_device_file(char *fname);
692 
693 /** Seed the random number generator with extra random bytes.
694 
695     You should almost never need to call this function; it should be
696     sufficient to invoke evutil_secure_rng_init(), or let Libevent take
697     care of calling evutil_secure_rng_init() on its own.
698 
699     If you call this function as a _replacement_ for the regular
700     entropy sources, then you need to be sure that your input
701     contains a fairly large amount of strong entropy.  Doing so is
702     notoriously hard: most people who try get it wrong.  Watch out!
703 
704     @param dat a buffer full of a strong source of random numbers
705     @param datlen the number of bytes to read from datlen
706  */
707 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
708 
709 #ifdef __cplusplus
710 }
711 #endif
712 
713 #endif /* _EVUTIL_H_ */
714