util.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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_INCLUDED_
  27. #define EVENT2_UTIL_H_INCLUDED_
  28. /** @file event2/util.h
  29. Common convenience functions for cross-platform portability and
  30. related socket manipulations.
  31. */
  32. #include <event2/visibility.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #include <event2/event-config.h>
  37. #ifdef EVENT__HAVE_SYS_TIME_H
  38. #include <sys/time.h>
  39. #endif
  40. #ifdef EVENT__HAVE_STDINT_H
  41. #include <stdint.h>
  42. #elif defined(EVENT__HAVE_INTTYPES_H)
  43. #include <inttypes.h>
  44. #endif
  45. #ifdef EVENT__HAVE_SYS_TYPES_H
  46. #include <sys/types.h>
  47. #endif
  48. #ifdef EVENT__HAVE_STDDEF_H
  49. #include <stddef.h>
  50. #endif
  51. #ifdef _MSC_VER
  52. #include <BaseTsd.h>
  53. #endif
  54. #include <stdarg.h>
  55. #ifdef EVENT__HAVE_NETDB_H
  56. #include <netdb.h>
  57. #endif
  58. #ifdef _WIN32
  59. #include <winsock2.h>
  60. #ifdef EVENT__HAVE_GETADDRINFO
  61. /* for EAI_* definitions. */
  62. #include <ws2tcpip.h>
  63. #endif
  64. #else
  65. #ifdef EVENT__HAVE_ERRNO_H
  66. #include <errno.h>
  67. #endif
  68. #include <sys/socket.h>
  69. #endif
  70. #include <time.h>
  71. /* Some openbsd autoconf versions get the name of this macro wrong. */
  72. #if defined(EVENT__SIZEOF_VOID__) && !defined(EVENT__SIZEOF_VOID_P)
  73. #define EVENT__SIZEOF_VOID_P EVENT__SIZEOF_VOID__
  74. #endif
  75. /**
  76. * @name Standard integer types.
  77. *
  78. * Integer type definitions for types that are supposed to be defined in the
  79. * C99-specified stdint.h. Shamefully, some platforms do not include
  80. * stdint.h, so we need to replace it. (If you are on a platform like this,
  81. * your C headers are now over 10 years out of date. You should bug them to
  82. * do something about this.)
  83. *
  84. * We define:
  85. *
  86. * <dl>
  87. * <dt>ev_uint64_t, ev_uint32_t, ev_uint16_t, ev_uint8_t</dt>
  88. * <dd>unsigned integer types of exactly 64, 32, 16, and 8 bits
  89. * respectively.</dd>
  90. * <dt>ev_int64_t, ev_int32_t, ev_int16_t, ev_int8_t</dt>
  91. * <dd>signed integer types of exactly 64, 32, 16, and 8 bits
  92. * respectively.</dd>
  93. * <dt>ev_uintptr_t, ev_intptr_t</dt>
  94. * <dd>unsigned/signed integers large enough
  95. * to hold a pointer without loss of bits.</dd>
  96. * <dt>ev_ssize_t</dt>
  97. * <dd>A signed type of the same size as size_t</dd>
  98. * <dt>ev_off_t</dt>
  99. * <dd>A signed type typically used to represent offsets within a
  100. * (potentially large) file</dd>
  101. *
  102. * @{
  103. */
  104. #ifdef EVENT__HAVE_UINT64_T
  105. #define ev_uint64_t uint64_t
  106. #define ev_int64_t int64_t
  107. #elif defined(_WIN32)
  108. #define ev_uint64_t unsigned __int64
  109. #define ev_int64_t signed __int64
  110. #elif EVENT__SIZEOF_LONG_LONG == 8
  111. #define ev_uint64_t unsigned long long
  112. #define ev_int64_t long long
  113. #elif EVENT__SIZEOF_LONG == 8
  114. #define ev_uint64_t unsigned long
  115. #define ev_int64_t long
  116. #elif defined(EVENT_IN_DOXYGEN_)
  117. #define ev_uint64_t ...
  118. #define ev_int64_t ...
  119. #else
  120. #error "No way to define ev_uint64_t"
  121. #endif
  122. #ifdef EVENT__HAVE_UINT32_T
  123. #define ev_uint32_t uint32_t
  124. #define ev_int32_t int32_t
  125. #elif defined(_WIN32)
  126. #define ev_uint32_t unsigned int
  127. #define ev_int32_t signed int
  128. #elif EVENT__SIZEOF_LONG == 4
  129. #define ev_uint32_t unsigned long
  130. #define ev_int32_t signed long
  131. #elif EVENT__SIZEOF_INT == 4
  132. #define ev_uint32_t unsigned int
  133. #define ev_int32_t signed int
  134. #elif defined(EVENT_IN_DOXYGEN_)
  135. #define ev_uint32_t ...
  136. #define ev_int32_t ...
  137. #else
  138. #error "No way to define ev_uint32_t"
  139. #endif
  140. #ifdef EVENT__HAVE_UINT16_T
  141. #define ev_uint16_t uint16_t
  142. #define ev_int16_t int16_t
  143. #elif defined(_WIN32)
  144. #define ev_uint16_t unsigned short
  145. #define ev_int16_t signed short
  146. #elif EVENT__SIZEOF_INT == 2
  147. #define ev_uint16_t unsigned int
  148. #define ev_int16_t signed int
  149. #elif EVENT__SIZEOF_SHORT == 2
  150. #define ev_uint16_t unsigned short
  151. #define ev_int16_t signed short
  152. #elif defined(EVENT_IN_DOXYGEN_)
  153. #define ev_uint16_t ...
  154. #define ev_int16_t ...
  155. #else
  156. #error "No way to define ev_uint16_t"
  157. #endif
  158. #ifdef EVENT__HAVE_UINT8_T
  159. #define ev_uint8_t uint8_t
  160. #define ev_int8_t int8_t
  161. #elif defined(EVENT_IN_DOXYGEN_)
  162. #define ev_uint8_t ...
  163. #define ev_int8_t ...
  164. #else
  165. #define ev_uint8_t unsigned char
  166. #define ev_int8_t signed char
  167. #endif
  168. #ifdef EVENT__HAVE_UINTPTR_T
  169. #define ev_uintptr_t uintptr_t
  170. #define ev_intptr_t intptr_t
  171. #elif EVENT__SIZEOF_VOID_P <= 4
  172. #define ev_uintptr_t ev_uint32_t
  173. #define ev_intptr_t ev_int32_t
  174. #elif EVENT__SIZEOF_VOID_P <= 8
  175. #define ev_uintptr_t ev_uint64_t
  176. #define ev_intptr_t ev_int64_t
  177. #elif defined(EVENT_IN_DOXYGEN_)
  178. #define ev_uintptr_t ...
  179. #define ev_intptr_t ...
  180. #else
  181. #error "No way to define ev_uintptr_t"
  182. #endif
  183. #ifdef EVENT__ssize_t
  184. #define ev_ssize_t EVENT__ssize_t
  185. #else
  186. #define ev_ssize_t ssize_t
  187. #endif
  188. /* Note that we define ev_off_t based on the compile-time size of off_t that
  189. * we used to build Libevent, and not based on the current size of off_t.
  190. * (For example, we don't define ev_off_t to off_t.). We do this because
  191. * some systems let you build your software with different off_t sizes
  192. * at runtime, and so putting in any dependency on off_t would risk API
  193. * mismatch.
  194. */
  195. #ifdef _WIN32
  196. #define ev_off_t ev_int64_t
  197. #elif EVENT__SIZEOF_OFF_T == 8
  198. #define ev_off_t ev_int64_t
  199. #elif EVENT__SIZEOF_OFF_T == 4
  200. #define ev_off_t ev_int32_t
  201. #elif defined(EVENT_IN_DOXYGEN_)
  202. #define ev_off_t ...
  203. #else
  204. #define ev_off_t off_t
  205. #endif
  206. /**@}*/
  207. /* Limits for integer types.
  208. We're making two assumptions here:
  209. - The compiler does constant folding properly.
  210. - The platform does signed arithmetic in two's complement.
  211. */
  212. /**
  213. @name Limits for integer types
  214. These macros hold the largest or smallest values possible for the
  215. ev_[u]int*_t types.
  216. @{
  217. */
  218. #ifndef EVENT__HAVE_STDINT_H
  219. #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
  220. #define EV_INT64_MAX ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
  221. #define EV_INT64_MIN ((-EV_INT64_MAX) - 1)
  222. #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
  223. #define EV_INT32_MAX ((ev_int32_t) 0x7fffffffL)
  224. #define EV_INT32_MIN ((-EV_INT32_MAX) - 1)
  225. #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
  226. #define EV_INT16_MAX ((ev_int16_t) 0x7fffL)
  227. #define EV_INT16_MIN ((-EV_INT16_MAX) - 1)
  228. #define EV_UINT8_MAX 255
  229. #define EV_INT8_MAX 127
  230. #define EV_INT8_MIN ((-EV_INT8_MAX) - 1)
  231. #else
  232. #define EV_UINT64_MAX UINT64_MAX
  233. #define EV_INT64_MAX INT64_MAX
  234. #define EV_INT64_MIN INT64_MIN
  235. #define EV_UINT32_MAX UINT32_MAX
  236. #define EV_INT32_MAX INT32_MAX
  237. #define EV_INT32_MIN INT32_MIN
  238. #define EV_UINT16_MAX UINT16_MAX
  239. #define EV_INT16_MIN INT16_MIN
  240. #define EV_INT16_MAX INT16_MAX
  241. #define EV_UINT8_MAX UINT8_MAX
  242. #define EV_INT8_MAX INT8_MAX
  243. #define EV_INT8_MIN INT8_MIN
  244. /** @} */
  245. #endif
  246. /**
  247. @name Limits for SIZE_T and SSIZE_T
  248. @{
  249. */
  250. #if EVENT__SIZEOF_SIZE_T == 8
  251. #define EV_SIZE_MAX EV_UINT64_MAX
  252. #define EV_SSIZE_MAX EV_INT64_MAX
  253. #elif EVENT__SIZEOF_SIZE_T == 4
  254. #define EV_SIZE_MAX EV_UINT32_MAX
  255. #define EV_SSIZE_MAX EV_INT32_MAX
  256. #elif defined(EVENT_IN_DOXYGEN_)
  257. #define EV_SIZE_MAX ...
  258. #define EV_SSIZE_MAX ...
  259. #else
  260. #error "No way to define SIZE_MAX"
  261. #endif
  262. #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
  263. /**@}*/
  264. #ifdef _WIN32
  265. #define ev_socklen_t int
  266. #elif defined(EVENT__socklen_t)
  267. #define ev_socklen_t EVENT__socklen_t
  268. #else
  269. #define ev_socklen_t socklen_t
  270. #endif
  271. #ifdef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
  272. #if !defined(EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
  273. && !defined(ss_family)
  274. #define ss_family __ss_family
  275. #endif
  276. #endif
  277. /**
  278. * A type wide enough to hold the output of "socket()" or "accept()". On
  279. * Windows, this is an intptr_t; elsewhere, it is an int. */
  280. #ifdef _WIN32
  281. #define evutil_socket_t intptr_t
  282. #else
  283. #define evutil_socket_t int
  284. #endif
  285. /**
  286. * Structure to hold information about a monotonic timer
  287. *
  288. * Use this with evutil_configure_monotonic_time() and
  289. * evutil_gettime_monotonic().
  290. *
  291. * This is an opaque structure; you can allocate one using
  292. * evutil_monotonic_timer_new().
  293. *
  294. * @see evutil_monotonic_timer_new(), evutil_monotonic_timer_free(),
  295. * evutil_configure_monotonic_time(), evutil_gettime_monotonic()
  296. */
  297. struct evutil_monotonic_timer
  298. #ifdef EVENT_IN_DOXYGEN_
  299. {/*Empty body so that doxygen will generate documentation here.*/}
  300. #endif
  301. ;
  302. #define EV_MONOT_PRECISE 1
  303. #define EV_MONOT_FALLBACK 2
  304. /** Format a date string using RFC 1123 format (used in HTTP).
  305. * If `tm` is NULL, current system's time will be used.
  306. * The number of characters written will be returned.
  307. * One should check if the return value is smaller than `datelen` to check if
  308. * the result is truncated or not.
  309. */
  310. EVENT2_EXPORT_SYMBOL int
  311. evutil_date_rfc1123(char *date, const size_t datelen, const struct tm *tm);
  312. /** Allocate a new struct evutil_monotonic_timer for use with the
  313. * evutil_configure_monotonic_time() and evutil_gettime_monotonic()
  314. * functions. You must configure the timer with
  315. * evutil_configure_monotonic_time() before using it.
  316. */
  317. EVENT2_EXPORT_SYMBOL
  318. struct evutil_monotonic_timer * evutil_monotonic_timer_new(void);
  319. /** Free a struct evutil_monotonic_timer that was allocated using
  320. * evutil_monotonic_timer_new().
  321. */
  322. EVENT2_EXPORT_SYMBOL
  323. void evutil_monotonic_timer_free(struct evutil_monotonic_timer *timer);
  324. /** Set up a struct evutil_monotonic_timer; flags can include
  325. * EV_MONOT_PRECISE and EV_MONOT_FALLBACK.
  326. */
  327. EVENT2_EXPORT_SYMBOL
  328. int evutil_configure_monotonic_time(struct evutil_monotonic_timer *timer,
  329. int flags);
  330. /** Query the current monotonic time from a struct evutil_monotonic_timer
  331. * previously configured with evutil_configure_monotonic_time(). Monotonic
  332. * time is guaranteed never to run in reverse, but is not necessarily epoch-
  333. * based, or relative to any other definite point. Use it to make reliable
  334. * measurements of elapsed time between events even when the system time
  335. * may be changed.
  336. *
  337. * It is not safe to use this funtion on the same timer from multiple
  338. * threads.
  339. */
  340. EVENT2_EXPORT_SYMBOL
  341. int evutil_gettime_monotonic(struct evutil_monotonic_timer *timer,
  342. struct timeval *tp);
  343. /** Create two new sockets that are connected to each other.
  344. On Unix, this simply calls socketpair(). On Windows, it uses the
  345. loopback network interface on 127.0.0.1, and only
  346. AF_INET,SOCK_STREAM are supported.
  347. (This may fail on some Windows hosts where firewall software has cleverly
  348. decided to keep 127.0.0.1 from talking to itself.)
  349. Parameters and return values are as for socketpair()
  350. */
  351. EVENT2_EXPORT_SYMBOL
  352. int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
  353. /** Do platform-specific operations as needed to make a socket nonblocking.
  354. @param sock The socket to make nonblocking
  355. @return 0 on success, -1 on failure
  356. */
  357. EVENT2_EXPORT_SYMBOL
  358. int evutil_make_socket_nonblocking(evutil_socket_t sock);
  359. /** Do platform-specific operations to make a listener socket reusable.
  360. Specifically, we want to make sure that another program will be able
  361. to bind this address right after we've closed the listener.
  362. This differs from Windows's interpretation of "reusable", which
  363. allows multiple listeners to bind the same address at the same time.
  364. @param sock The socket to make reusable
  365. @return 0 on success, -1 on failure
  366. */
  367. EVENT2_EXPORT_SYMBOL
  368. int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
  369. /** Do platform-specific operations to make a listener port reusable.
  370. Specifically, we want to make sure that multiple programs which also
  371. set the same socket option will be able to bind, listen at the same time.
  372. This is a feature available only to Linux 3.9+
  373. @param sock The socket to make reusable
  374. @return 0 on success, -1 on failure
  375. */
  376. EVENT2_EXPORT_SYMBOL
  377. int evutil_make_listen_socket_reuseable_port(evutil_socket_t sock);
  378. /** Set ipv6 only bind socket option to make listener work only in ipv6 sockets.
  379. According to RFC3493 and most Linux distributions, default value for the
  380. sockets is to work in IPv4-mapped mode. In IPv4-mapped mode, it is not possible
  381. to bind same port from different IPv4 and IPv6 handlers.
  382. @param sock The socket to make in ipv6only working mode
  383. @return 0 on success, -1 on failure
  384. */
  385. EVENT2_EXPORT_SYMBOL
  386. int evutil_make_listen_socket_ipv6only(evutil_socket_t sock);
  387. /** Do platform-specific operations as needed to close a socket upon a
  388. successful execution of one of the exec*() functions.
  389. @param sock The socket to be closed
  390. @return 0 on success, -1 on failure
  391. */
  392. EVENT2_EXPORT_SYMBOL
  393. int evutil_make_socket_closeonexec(evutil_socket_t sock);
  394. /** Do the platform-specific call needed to close a socket returned from
  395. socket() or accept().
  396. @param sock The socket to be closed
  397. @return 0 on success (whether the operation is supported or not),
  398. -1 on failure
  399. */
  400. EVENT2_EXPORT_SYMBOL
  401. int evutil_closesocket(evutil_socket_t sock);
  402. #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
  403. /** Do platform-specific operations, if possible, to make a tcp listener
  404. * socket defer accept()s until there is data to read.
  405. *
  406. * Not all platforms support this. You don't want to do this for every
  407. * listener socket: only the ones that implement a protocol where the
  408. * client transmits before the server needs to respond.
  409. *
  410. * @param sock The listening socket to to make deferred
  411. * @return 0 on success (whether the operation is supported or not),
  412. * -1 on failure
  413. */
  414. EVENT2_EXPORT_SYMBOL
  415. int evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock);
  416. #ifdef _WIN32
  417. /** Return the most recent socket error. Not idempotent on all platforms. */
  418. #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
  419. /** Replace the most recent socket error with errcode */
  420. #define EVUTIL_SET_SOCKET_ERROR(errcode) \
  421. do { WSASetLastError(errcode); } while (0)
  422. /** Return the most recent socket error to occur on sock. */
  423. EVENT2_EXPORT_SYMBOL
  424. int evutil_socket_geterror(evutil_socket_t sock);
  425. /** Convert a socket error to a string. */
  426. EVENT2_EXPORT_SYMBOL
  427. const char *evutil_socket_error_to_string(int errcode);
  428. #define EVUTIL_INVALID_SOCKET INVALID_SOCKET
  429. #elif defined(EVENT_IN_DOXYGEN_)
  430. /**
  431. @name Socket error functions
  432. These functions are needed for making programs compatible between
  433. Windows and Unix-like platforms.
  434. You see, Winsock handles socket errors differently from the rest of
  435. the world. Elsewhere, a socket error is like any other error and is
  436. stored in errno. But winsock functions require you to retrieve the
  437. error with a special function, and don't let you use strerror for
  438. the error codes. And handling EWOULDBLOCK is ... different.
  439. @{
  440. */
  441. /** Return the most recent socket error. Not idempotent on all platforms. */
  442. #define EVUTIL_SOCKET_ERROR() ...
  443. /** Replace the most recent socket error with errcode */
  444. #define EVUTIL_SET_SOCKET_ERROR(errcode) ...
  445. /** Return the most recent socket error to occur on sock. */
  446. #define evutil_socket_geterror(sock) ...
  447. /** Convert a socket error to a string. */
  448. #define evutil_socket_error_to_string(errcode) ...
  449. #define EVUTIL_INVALID_SOCKET -1
  450. /**@}*/
  451. #else /** !EVENT_IN_DOXYGEN_ && !_WIN32 */
  452. #define EVUTIL_SOCKET_ERROR() (errno)
  453. #define EVUTIL_SET_SOCKET_ERROR(errcode) \
  454. do { errno = (errcode); } while (0)
  455. #define evutil_socket_geterror(sock) (errno)
  456. #define evutil_socket_error_to_string(errcode) (strerror(errcode))
  457. #define EVUTIL_INVALID_SOCKET -1
  458. #endif /** !_WIN32 */
  459. /**
  460. * @name Manipulation macros for struct timeval.
  461. *
  462. * We define replacements
  463. * for timeradd, timersub, timerclear, timercmp, and timerisset.
  464. *
  465. * @{
  466. */
  467. #ifdef EVENT__HAVE_TIMERADD
  468. #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
  469. #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
  470. #else
  471. #define evutil_timeradd(tvp, uvp, vvp) \
  472. do { \
  473. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  474. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  475. if ((vvp)->tv_usec >= 1000000) { \
  476. (vvp)->tv_sec++; \
  477. (vvp)->tv_usec -= 1000000; \
  478. } \
  479. } while (0)
  480. #define evutil_timersub(tvp, uvp, vvp) \
  481. do { \
  482. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  483. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  484. if ((vvp)->tv_usec < 0) { \
  485. (vvp)->tv_sec--; \
  486. (vvp)->tv_usec += 1000000; \
  487. } \
  488. } while (0)
  489. #endif /* !EVENT__HAVE_TIMERADD */
  490. #ifdef EVENT__HAVE_TIMERCLEAR
  491. #define evutil_timerclear(tvp) timerclear(tvp)
  492. #else
  493. #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  494. #endif
  495. /**@}*/
  496. /** Return true iff the tvp is related to uvp according to the relational
  497. * operator cmp. Recognized values for cmp are ==, <=, <, >=, and >. */
  498. #define evutil_timercmp(tvp, uvp, cmp) \
  499. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  500. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  501. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  502. #ifdef EVENT__HAVE_TIMERISSET
  503. #define evutil_timerisset(tvp) timerisset(tvp)
  504. #else
  505. #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  506. #endif
  507. /** Replacement for offsetof on platforms that don't define it. */
  508. #ifdef offsetof
  509. #define evutil_offsetof(type, field) offsetof(type, field)
  510. #else
  511. #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
  512. #endif
  513. /* big-int related functions */
  514. /** Parse a 64-bit value from a string. Arguments are as for strtol. */
  515. EVENT2_EXPORT_SYMBOL
  516. ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
  517. /** Replacement for gettimeofday on platforms that lack it. */
  518. #ifdef EVENT__HAVE_GETTIMEOFDAY
  519. #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
  520. #else
  521. struct timezone;
  522. EVENT2_EXPORT_SYMBOL
  523. int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
  524. #endif
  525. /** Replacement for snprintf to get consistent behavior on platforms for
  526. which the return value of snprintf does not conform to C99.
  527. */
  528. EVENT2_EXPORT_SYMBOL
  529. int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
  530. #ifdef __GNUC__
  531. __attribute__((format(printf, 3, 4)))
  532. #endif
  533. ;
  534. /** Replacement for vsnprintf to get consistent behavior on platforms for
  535. which the return value of snprintf does not conform to C99.
  536. */
  537. EVENT2_EXPORT_SYMBOL
  538. int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
  539. #ifdef __GNUC__
  540. __attribute__((format(printf, 3, 0)))
  541. #endif
  542. ;
  543. /** Replacement for inet_ntop for platforms which lack it. */
  544. EVENT2_EXPORT_SYMBOL
  545. const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
  546. /** Variation of inet_pton that also parses IPv6 scopes. Public for
  547. unit tests. No reason to call this directly.
  548. */
  549. EVENT2_EXPORT_SYMBOL
  550. int evutil_inet_pton_scope(int af, const char *src, void *dst,
  551. unsigned *indexp);
  552. /** Replacement for inet_pton for platforms which lack it. */
  553. EVENT2_EXPORT_SYMBOL
  554. int evutil_inet_pton(int af, const char *src, void *dst);
  555. struct sockaddr;
  556. /** Parse an IPv4 or IPv6 address, with optional port, from a string.
  557. Recognized formats are:
  558. - [IPv6Address]:port
  559. - [IPv6Address]
  560. - IPv6Address
  561. - IPv4Address:port
  562. - IPv4Address
  563. If no port is specified, the port in the output is set to 0.
  564. @param str The string to parse.
  565. @param out A struct sockaddr to hold the result. This should probably be
  566. a struct sockaddr_storage.
  567. @param outlen A pointer to the number of bytes that that 'out' can safely
  568. hold. Set to the number of bytes used in 'out' on success.
  569. @return -1 if the address is not well-formed, if the port is out of range,
  570. or if out is not large enough to hold the result. Otherwise returns
  571. 0 on success.
  572. */
  573. EVENT2_EXPORT_SYMBOL
  574. int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
  575. /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
  576. * preceeds sa2, or greater than 0 if sa1 follows sa2. If include_port is
  577. * true, consider the port as well as the address. Only implemented for
  578. * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
  579. * the same between Libevent versions. */
  580. EVENT2_EXPORT_SYMBOL
  581. int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
  582. int include_port);
  583. /** As strcasecmp, but always compares the characters in locale-independent
  584. ASCII. That's useful if you're handling data in ASCII-based protocols.
  585. */
  586. EVENT2_EXPORT_SYMBOL
  587. int evutil_ascii_strcasecmp(const char *str1, const char *str2);
  588. /** As strncasecmp, but always compares the characters in locale-independent
  589. ASCII. That's useful if you're handling data in ASCII-based protocols.
  590. */
  591. EVENT2_EXPORT_SYMBOL
  592. int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
  593. /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
  594. * if this system has no getaddrinfo(). */
  595. #ifdef EVENT__HAVE_STRUCT_ADDRINFO
  596. #define evutil_addrinfo addrinfo
  597. #else
  598. /** A definition of struct addrinfo for systems that lack it.
  599. (This is just an alias for struct addrinfo if the system defines
  600. struct addrinfo.)
  601. */
  602. struct evutil_addrinfo {
  603. int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
  604. int ai_family; /* PF_xxx */
  605. int ai_socktype; /* SOCK_xxx */
  606. int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
  607. size_t ai_addrlen; /* length of ai_addr */
  608. char *ai_canonname; /* canonical name for nodename */
  609. struct sockaddr *ai_addr; /* binary address */
  610. struct evutil_addrinfo *ai_next; /* next structure in linked list */
  611. };
  612. #endif
  613. /** @name evutil_getaddrinfo() error codes
  614. These values are possible error codes for evutil_getaddrinfo() and
  615. related functions.
  616. @{
  617. */
  618. #if defined(EAI_ADDRFAMILY) && defined(EVENT__HAVE_GETADDRINFO)
  619. #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
  620. #else
  621. #define EVUTIL_EAI_ADDRFAMILY -901
  622. #endif
  623. #if defined(EAI_AGAIN) && defined(EVENT__HAVE_GETADDRINFO)
  624. #define EVUTIL_EAI_AGAIN EAI_AGAIN
  625. #else
  626. #define EVUTIL_EAI_AGAIN -902
  627. #endif
  628. #if defined(EAI_BADFLAGS) && defined(EVENT__HAVE_GETADDRINFO)
  629. #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
  630. #else
  631. #define EVUTIL_EAI_BADFLAGS -903
  632. #endif
  633. #if defined(EAI_FAIL) && defined(EVENT__HAVE_GETADDRINFO)
  634. #define EVUTIL_EAI_FAIL EAI_FAIL
  635. #else
  636. #define EVUTIL_EAI_FAIL -904
  637. #endif
  638. #if defined(EAI_FAMILY) && defined(EVENT__HAVE_GETADDRINFO)
  639. #define EVUTIL_EAI_FAMILY EAI_FAMILY
  640. #else
  641. #define EVUTIL_EAI_FAMILY -905
  642. #endif
  643. #if defined(EAI_MEMORY) && defined(EVENT__HAVE_GETADDRINFO)
  644. #define EVUTIL_EAI_MEMORY EAI_MEMORY
  645. #else
  646. #define EVUTIL_EAI_MEMORY -906
  647. #endif
  648. /* This test is a bit complicated, since some MS SDKs decide to
  649. * remove NODATA or redefine it to be the same as NONAME, in a
  650. * fun interpretation of RFC 2553 and RFC 3493. */
  651. #if defined(EAI_NODATA) && defined(EVENT__HAVE_GETADDRINFO) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
  652. #define EVUTIL_EAI_NODATA EAI_NODATA
  653. #else
  654. #define EVUTIL_EAI_NODATA -907
  655. #endif
  656. #if defined(EAI_NONAME) && defined(EVENT__HAVE_GETADDRINFO)
  657. #define EVUTIL_EAI_NONAME EAI_NONAME
  658. #else
  659. #define EVUTIL_EAI_NONAME -908
  660. #endif
  661. #if defined(EAI_SERVICE) && defined(EVENT__HAVE_GETADDRINFO)
  662. #define EVUTIL_EAI_SERVICE EAI_SERVICE
  663. #else
  664. #define EVUTIL_EAI_SERVICE -909
  665. #endif
  666. #if defined(EAI_SOCKTYPE) && defined(EVENT__HAVE_GETADDRINFO)
  667. #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
  668. #else
  669. #define EVUTIL_EAI_SOCKTYPE -910
  670. #endif
  671. #if defined(EAI_SYSTEM) && defined(EVENT__HAVE_GETADDRINFO)
  672. #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
  673. #else
  674. #define EVUTIL_EAI_SYSTEM -911
  675. #endif
  676. #define EVUTIL_EAI_CANCEL -90001
  677. #if defined(AI_PASSIVE) && defined(EVENT__HAVE_GETADDRINFO)
  678. #define EVUTIL_AI_PASSIVE AI_PASSIVE
  679. #else
  680. #define EVUTIL_AI_PASSIVE 0x1000
  681. #endif
  682. #if defined(AI_CANONNAME) && defined(EVENT__HAVE_GETADDRINFO)
  683. #define EVUTIL_AI_CANONNAME AI_CANONNAME
  684. #else
  685. #define EVUTIL_AI_CANONNAME 0x2000
  686. #endif
  687. #if defined(AI_NUMERICHOST) && defined(EVENT__HAVE_GETADDRINFO)
  688. #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
  689. #else
  690. #define EVUTIL_AI_NUMERICHOST 0x4000
  691. #endif
  692. #if defined(AI_NUMERICSERV) && defined(EVENT__HAVE_GETADDRINFO)
  693. #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
  694. #else
  695. #define EVUTIL_AI_NUMERICSERV 0x8000
  696. #endif
  697. #if defined(AI_V4MAPPED) && defined(EVENT__HAVE_GETADDRINFO)
  698. #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
  699. #else
  700. #define EVUTIL_AI_V4MAPPED 0x10000
  701. #endif
  702. #if defined(AI_ALL) && defined(EVENT__HAVE_GETADDRINFO)
  703. #define EVUTIL_AI_ALL AI_ALL
  704. #else
  705. #define EVUTIL_AI_ALL 0x20000
  706. #endif
  707. #if defined(AI_ADDRCONFIG) && defined(EVENT__HAVE_GETADDRINFO)
  708. #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
  709. #else
  710. #define EVUTIL_AI_ADDRCONFIG 0x40000
  711. #endif
  712. /**@}*/
  713. struct evutil_addrinfo;
  714. /**
  715. * This function clones getaddrinfo for systems that don't have it. For full
  716. * details, see RFC 3493, section 6.1.
  717. *
  718. * Limitations:
  719. * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
  720. * gethostbyname, with their attendant issues.
  721. * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
  722. *
  723. * For a nonblocking variant, see evdns_getaddrinfo.
  724. */
  725. EVENT2_EXPORT_SYMBOL
  726. int evutil_getaddrinfo(const char *nodename, const char *servname,
  727. const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
  728. /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
  729. EVENT2_EXPORT_SYMBOL
  730. void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
  731. EVENT2_EXPORT_SYMBOL
  732. const char *evutil_gai_strerror(int err);
  733. /** Generate n bytes of secure pseudorandom data, and store them in buf.
  734. *
  735. * Current versions of Libevent use an ARC4-based random number generator,
  736. * seeded using the platform's entropy source (/dev/urandom on Unix-like
  737. * systems; CryptGenRandom on Windows). This is not actually as secure as it
  738. * should be: ARC4 is a pretty lousy cipher, and the current implementation
  739. * provides only rudimentary prediction- and backtracking-resistance. Don't
  740. * use this for serious cryptographic applications.
  741. */
  742. EVENT2_EXPORT_SYMBOL
  743. void evutil_secure_rng_get_bytes(void *buf, size_t n);
  744. /**
  745. * Seed the secure random number generator if needed, and return 0 on
  746. * success or -1 on failure.
  747. *
  748. * It is okay to call this function more than once; it will still return
  749. * 0 if the RNG has been successfully seeded and -1 if it can't be
  750. * seeded.
  751. *
  752. * Ordinarily you don't need to call this function from your own code;
  753. * Libevent will seed the RNG itself the first time it needs good random
  754. * numbers. You only need to call it if (a) you want to double-check
  755. * that one of the seeding methods did succeed, or (b) you plan to drop
  756. * the capability to seed (by chrooting, or dropping capabilities, or
  757. * whatever), and you want to make sure that seeding happens before your
  758. * program loses the ability to do it.
  759. */
  760. EVENT2_EXPORT_SYMBOL
  761. int evutil_secure_rng_init(void);
  762. /**
  763. * Set a filename to use in place of /dev/urandom for seeding the secure
  764. * PRNG. Return 0 on success, -1 on failure.
  765. *
  766. * Call this function BEFORE calling any other initialization or RNG
  767. * functions.
  768. *
  769. * (This string will _NOT_ be copied internally. Do not free it while any
  770. * user of the secure RNG might be running. Don't pass anything other than a
  771. * real /dev/...random device file here, or you might lose security.)
  772. *
  773. * This API is unstable, and might change in a future libevent version.
  774. */
  775. EVENT2_EXPORT_SYMBOL
  776. int evutil_secure_rng_set_urandom_device_file(char *fname);
  777. #if !defined(EVENT__HAVE_ARC4RANDOM) || defined(EVENT__HAVE_ARC4RANDOM_ADDRANDOM)
  778. /** Seed the random number generator with extra random bytes.
  779. You should almost never need to call this function; it should be
  780. sufficient to invoke evutil_secure_rng_init(), or let Libevent take
  781. care of calling evutil_secure_rng_init() on its own.
  782. If you call this function as a _replacement_ for the regular
  783. entropy sources, then you need to be sure that your input
  784. contains a fairly large amount of strong entropy. Doing so is
  785. notoriously hard: most people who try get it wrong. Watch out!
  786. @param dat a buffer full of a strong source of random numbers
  787. @param datlen the number of bytes to read from datlen
  788. */
  789. EVENT2_EXPORT_SYMBOL
  790. void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
  791. #endif
  792. #ifdef __cplusplus
  793. }
  794. #endif
  795. #endif /* EVENT1_EVUTIL_H_INCLUDED_ */