util.h 22 KB

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