Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/event2/util.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  * 3. The name of the author may not be used to endorse or promote products
0013  *    derived from this software without specific prior written permission.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0017  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0018  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0019  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0020  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0024  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025  */
0026 #ifndef EVENT2_UTIL_H_INCLUDED_
0027 #define EVENT2_UTIL_H_INCLUDED_
0028 
0029 /** @file event2/util.h
0030 
0031   Common convenience functions for cross-platform portability and
0032   related socket manipulations.
0033 
0034  */
0035 #include <event2/visibility.h>
0036 
0037 #ifdef __cplusplus
0038 extern "C" {
0039 #endif
0040 
0041 #include <event2/event-config.h>
0042 #ifdef EVENT__HAVE_SYS_TIME_H
0043 #include <sys/time.h>
0044 #endif
0045 #ifdef EVENT__HAVE_STDINT_H
0046 #include <stdint.h>
0047 #elif defined(EVENT__HAVE_INTTYPES_H)
0048 #include <inttypes.h>
0049 #endif
0050 #ifdef EVENT__HAVE_SYS_TYPES_H
0051 #include <sys/types.h>
0052 #endif
0053 #ifdef EVENT__HAVE_STDDEF_H
0054 #include <stddef.h>
0055 #endif
0056 #ifdef _MSC_VER
0057 #include <BaseTsd.h>
0058 #endif
0059 #include <stdarg.h>
0060 #ifdef EVENT__HAVE_NETDB_H
0061 #include <netdb.h>
0062 #endif
0063 
0064 #ifdef _WIN32
0065 #include <winsock2.h>
0066 #ifdef EVENT__HAVE_GETADDRINFO
0067 /* for EAI_* definitions. */
0068 #include <ws2tcpip.h>
0069 #endif
0070 #else
0071 #ifdef EVENT__HAVE_ERRNO_H
0072 #include <errno.h>
0073 #endif
0074 #include <sys/socket.h>
0075 #endif
0076 
0077 #include <time.h>
0078 
0079 /* Some openbsd autoconf versions get the name of this macro wrong. */
0080 #if defined(EVENT__SIZEOF_VOID__) && !defined(EVENT__SIZEOF_VOID_P)
0081 #define EVENT__SIZEOF_VOID_P EVENT__SIZEOF_VOID__
0082 #endif
0083 
0084 /**
0085  * @name Standard integer types.
0086  *
0087  * Integer type definitions for types that are supposed to be defined in the
0088  * C99-specified stdint.h.  Shamefully, some platforms do not include
0089  * stdint.h, so we need to replace it.  (If you are on a platform like this,
0090  * your C headers are now over 10 years out of date.  You should bug them to
0091  * do something about this.)
0092  *
0093  * We define:
0094  *
0095  * <dl>
0096  *   <dt>ev_uint64_t, ev_uint32_t, ev_uint16_t, ev_uint8_t</dt>
0097  *      <dd>unsigned integer types of exactly 64, 32, 16, and 8 bits
0098  *          respectively.</dd>
0099  *    <dt>ev_int64_t, ev_int32_t, ev_int16_t, ev_int8_t</dt>
0100  *      <dd>signed integer types of exactly 64, 32, 16, and 8 bits
0101  *          respectively.</dd>
0102  *    <dt>ev_uintptr_t, ev_intptr_t</dt>
0103  *      <dd>unsigned/signed integers large enough
0104  *      to hold a pointer without loss of bits.</dd>
0105  *    <dt>ev_ssize_t</dt>
0106  *      <dd>A signed type of the same size as size_t</dd>
0107  *    <dt>ev_off_t</dt>
0108  *      <dd>A signed type typically used to represent offsets within a
0109  *      (potentially large) file</dd>
0110  *
0111  * @{
0112  */
0113 #ifdef EVENT__HAVE_UINT64_T
0114 #define ev_uint64_t uint64_t
0115 #define ev_int64_t int64_t
0116 #elif defined(_WIN32)
0117 #define ev_uint64_t unsigned __int64
0118 #define ev_int64_t signed __int64
0119 #elif EVENT__SIZEOF_LONG_LONG == 8
0120 #define ev_uint64_t unsigned long long
0121 #define ev_int64_t long long
0122 #elif EVENT__SIZEOF_LONG == 8
0123 #define ev_uint64_t unsigned long
0124 #define ev_int64_t long
0125 #elif defined(EVENT_IN_DOXYGEN_)
0126 #define ev_uint64_t ...
0127 #define ev_int64_t ...
0128 #else
0129 #error "No way to define ev_uint64_t"
0130 #endif
0131 
0132 #ifdef EVENT__HAVE_UINT32_T
0133 #define ev_uint32_t uint32_t
0134 #define ev_int32_t int32_t
0135 #elif defined(_WIN32)
0136 #define ev_uint32_t unsigned int
0137 #define ev_int32_t signed int
0138 #elif EVENT__SIZEOF_LONG == 4
0139 #define ev_uint32_t unsigned long
0140 #define ev_int32_t signed long
0141 #elif EVENT__SIZEOF_INT == 4
0142 #define ev_uint32_t unsigned int
0143 #define ev_int32_t signed int
0144 #elif defined(EVENT_IN_DOXYGEN_)
0145 #define ev_uint32_t ...
0146 #define ev_int32_t ...
0147 #else
0148 #error "No way to define ev_uint32_t"
0149 #endif
0150 
0151 #ifdef EVENT__HAVE_UINT16_T
0152 #define ev_uint16_t uint16_t
0153 #define ev_int16_t  int16_t
0154 #elif defined(_WIN32)
0155 #define ev_uint16_t unsigned short
0156 #define ev_int16_t  signed short
0157 #elif EVENT__SIZEOF_INT == 2
0158 #define ev_uint16_t unsigned int
0159 #define ev_int16_t  signed int
0160 #elif EVENT__SIZEOF_SHORT == 2
0161 #define ev_uint16_t unsigned short
0162 #define ev_int16_t  signed short
0163 #elif defined(EVENT_IN_DOXYGEN_)
0164 #define ev_uint16_t ...
0165 #define ev_int16_t ...
0166 #else
0167 #error "No way to define ev_uint16_t"
0168 #endif
0169 
0170 #ifdef EVENT__HAVE_UINT8_T
0171 #define ev_uint8_t uint8_t
0172 #define ev_int8_t int8_t
0173 #elif defined(EVENT_IN_DOXYGEN_)
0174 #define ev_uint8_t ...
0175 #define ev_int8_t ...
0176 #else
0177 #define ev_uint8_t unsigned char
0178 #define ev_int8_t signed char
0179 #endif
0180 
0181 #ifdef EVENT__HAVE_UINTPTR_T
0182 #define ev_uintptr_t uintptr_t
0183 #define ev_intptr_t intptr_t
0184 #elif EVENT__SIZEOF_VOID_P <= 4
0185 #define ev_uintptr_t ev_uint32_t
0186 #define ev_intptr_t ev_int32_t
0187 #elif EVENT__SIZEOF_VOID_P <= 8
0188 #define ev_uintptr_t ev_uint64_t
0189 #define ev_intptr_t ev_int64_t
0190 #elif defined(EVENT_IN_DOXYGEN_)
0191 #define ev_uintptr_t ...
0192 #define ev_intptr_t ...
0193 #else
0194 #error "No way to define ev_uintptr_t"
0195 #endif
0196 
0197 #ifdef EVENT__ssize_t
0198 #define ev_ssize_t EVENT__ssize_t
0199 #else
0200 #define ev_ssize_t ssize_t
0201 #endif
0202 
0203 /* Note that we define ev_off_t based on the compile-time size of off_t that
0204  * we used to build Libevent, and not based on the current size of off_t.
0205  * (For example, we don't define ev_off_t to off_t.).  We do this because
0206  * some systems let you build your software with different off_t sizes
0207  * at runtime, and so putting in any dependency on off_t would risk API
0208  * mismatch.
0209  */
0210 #ifdef _WIN32
0211 #define ev_off_t ev_int64_t
0212 #elif EVENT__SIZEOF_OFF_T == 8
0213 #define ev_off_t ev_int64_t
0214 #elif EVENT__SIZEOF_OFF_T == 4
0215 #define ev_off_t ev_int32_t
0216 #elif defined(EVENT_IN_DOXYGEN_)
0217 #define ev_off_t ...
0218 #else
0219 #define ev_off_t off_t
0220 #endif
0221 /**@}*/
0222 
0223 /* Limits for integer types.
0224 
0225    We're making two assumptions here:
0226      - The compiler does constant folding properly.
0227      - The platform does signed arithmetic in two's complement.
0228 */
0229 
0230 /**
0231    @name Limits for integer types
0232 
0233    These macros hold the largest or smallest values possible for the
0234    ev_[u]int*_t types.
0235 
0236    @{
0237 */
0238 #ifndef EVENT__HAVE_STDINT_H
0239 #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
0240 #define EV_INT64_MAX  ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
0241 #define EV_INT64_MIN  ((-EV_INT64_MAX) - 1)
0242 #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
0243 #define EV_INT32_MAX  ((ev_int32_t) 0x7fffffffL)
0244 #define EV_INT32_MIN  ((-EV_INT32_MAX) - 1)
0245 #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
0246 #define EV_INT16_MAX  ((ev_int16_t) 0x7fffL)
0247 #define EV_INT16_MIN  ((-EV_INT16_MAX) - 1)
0248 #define EV_UINT8_MAX  255
0249 #define EV_INT8_MAX   127
0250 #define EV_INT8_MIN   ((-EV_INT8_MAX) - 1)
0251 #else
0252 #define EV_UINT64_MAX UINT64_MAX
0253 #define EV_INT64_MAX  INT64_MAX
0254 #define EV_INT64_MIN  INT64_MIN
0255 #define EV_UINT32_MAX UINT32_MAX
0256 #define EV_INT32_MAX  INT32_MAX
0257 #define EV_INT32_MIN  INT32_MIN
0258 #define EV_UINT16_MAX UINT16_MAX
0259 #define EV_INT16_MIN  INT16_MIN
0260 #define EV_INT16_MAX  INT16_MAX
0261 #define EV_UINT8_MAX  UINT8_MAX
0262 #define EV_INT8_MAX   INT8_MAX
0263 #define EV_INT8_MIN   INT8_MIN
0264 /** @} */
0265 #endif
0266 
0267 
0268 /**
0269    @name Limits for SIZE_T and SSIZE_T
0270 
0271    @{
0272 */
0273 #if EVENT__SIZEOF_SIZE_T == 8
0274 #define EV_SIZE_MAX EV_UINT64_MAX
0275 #define EV_SSIZE_MAX EV_INT64_MAX
0276 #elif EVENT__SIZEOF_SIZE_T == 4
0277 #define EV_SIZE_MAX EV_UINT32_MAX
0278 #define EV_SSIZE_MAX EV_INT32_MAX
0279 #elif defined(EVENT_IN_DOXYGEN_)
0280 #define EV_SIZE_MAX ...
0281 #define EV_SSIZE_MAX ...
0282 #else
0283 #error "No way to define SIZE_MAX"
0284 #endif
0285 
0286 #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
0287 /**@}*/
0288 
0289 #ifdef _WIN32
0290 #define ev_socklen_t int
0291 #elif defined(EVENT__socklen_t)
0292 #define ev_socklen_t EVENT__socklen_t
0293 #else
0294 #define ev_socklen_t socklen_t
0295 #endif
0296 
0297 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
0298 #if !defined(EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
0299  && !defined(ss_family)
0300 #define ss_family __ss_family
0301 #endif
0302 #endif
0303 
0304 /**
0305  * A type wide enough to hold the output of "socket()" or "accept()".  On
0306  * Windows, this is an intptr_t; elsewhere, it is an int. */
0307 #ifdef _WIN32
0308 #define evutil_socket_t intptr_t
0309 #else
0310 #define evutil_socket_t int
0311 #endif
0312 
0313 /**
0314  * Structure to hold information about a monotonic timer
0315  *
0316  * Use this with evutil_configure_monotonic_time() and
0317  * evutil_gettime_monotonic().
0318  *
0319  * This is an opaque structure; you can allocate one using
0320  * evutil_monotonic_timer_new().
0321  *
0322  * @see evutil_monotonic_timer_new(), evutil_monotonic_timer_free(),
0323  * evutil_configure_monotonic_time(), evutil_gettime_monotonic()
0324  */
0325 struct evutil_monotonic_timer
0326 #ifdef EVENT_IN_DOXYGEN_
0327 {/*Empty body so that doxygen will generate documentation here.*/}
0328 #endif
0329 ;
0330 
0331 #define EV_MONOT_PRECISE  1
0332 #define EV_MONOT_FALLBACK 2
0333 
0334 /** Format a date string using RFC 1123 format (used in HTTP).
0335  * If `tm` is NULL, current system's time will be used.
0336  * The number of characters written will be returned.
0337  * One should check if the return value is smaller than `datelen` to check if
0338  * the result is truncated or not.
0339  */
0340 EVENT2_EXPORT_SYMBOL int
0341 evutil_date_rfc1123(char *date, const size_t datelen, const struct tm *tm);
0342 
0343 /** Allocate a new struct evutil_monotonic_timer for use with the
0344  * evutil_configure_monotonic_time() and evutil_gettime_monotonic()
0345  * functions.  You must configure the timer with
0346  * evutil_configure_monotonic_time() before using it.
0347  */
0348 EVENT2_EXPORT_SYMBOL
0349 struct evutil_monotonic_timer * evutil_monotonic_timer_new(void);
0350 
0351 /** Free a struct evutil_monotonic_timer that was allocated using
0352  * evutil_monotonic_timer_new().
0353  */
0354 EVENT2_EXPORT_SYMBOL
0355 void evutil_monotonic_timer_free(struct evutil_monotonic_timer *timer);
0356 
0357 /** Set up a struct evutil_monotonic_timer; flags can include
0358  * EV_MONOT_PRECISE and EV_MONOT_FALLBACK.
0359  */
0360 EVENT2_EXPORT_SYMBOL
0361 int evutil_configure_monotonic_time(struct evutil_monotonic_timer *timer,
0362                                     int flags);
0363 
0364 /** Query the current monotonic time from a struct evutil_monotonic_timer
0365  * previously configured with evutil_configure_monotonic_time().  Monotonic
0366  * time is guaranteed never to run in reverse, but is not necessarily epoch-
0367  * based, or relative to any other definite point.  Use it to make reliable
0368  * measurements of elapsed time between events even when the system time
0369  * may be changed.
0370  *
0371  * It is not safe to use this funtion on the same timer from multiple
0372  * threads.
0373  */
0374 EVENT2_EXPORT_SYMBOL
0375 int evutil_gettime_monotonic(struct evutil_monotonic_timer *timer,
0376                              struct timeval *tp);
0377 
0378 /** Create two new sockets that are connected to each other.
0379 
0380     On Unix, this simply calls socketpair().  On Windows, it uses the
0381     loopback network interface on 127.0.0.1, and only
0382     AF_INET,SOCK_STREAM are supported.
0383 
0384     (This may fail on some Windows hosts where firewall software has cleverly
0385     decided to keep 127.0.0.1 from talking to itself.)
0386 
0387     Parameters and return values are as for socketpair()
0388 */
0389 EVENT2_EXPORT_SYMBOL
0390 int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
0391 /** Do platform-specific operations as needed to make a socket nonblocking.
0392 
0393     @param sock The socket to make nonblocking
0394     @return 0 on success, -1 on failure
0395  */
0396 EVENT2_EXPORT_SYMBOL
0397 int evutil_make_socket_nonblocking(evutil_socket_t sock);
0398 
0399 /** Do platform-specific operations to make a listener socket reusable.
0400 
0401     Specifically, we want to make sure that another program will be able
0402     to bind this address right after we've closed the listener.
0403 
0404     This differs from Windows's interpretation of "reusable", which
0405     allows multiple listeners to bind the same address at the same time.
0406 
0407     @param sock The socket to make reusable
0408     @return 0 on success, -1 on failure
0409  */
0410 EVENT2_EXPORT_SYMBOL
0411 int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
0412 
0413 /** Do platform-specific operations to make a listener port reusable.
0414 
0415     Specifically, we want to make sure that multiple programs which also
0416     set the same socket option will be able to bind, listen at the same time.
0417 
0418     This is a feature available only to Linux 3.9+
0419 
0420     @param sock The socket to make reusable
0421     @return 0 on success, -1 on failure
0422  */
0423 EVENT2_EXPORT_SYMBOL
0424 int evutil_make_listen_socket_reuseable_port(evutil_socket_t sock);
0425 
0426 /** Set ipv6 only bind socket option to make listener work only in ipv6 sockets.
0427 
0428     According to RFC3493 and most Linux distributions, default value for the
0429     sockets is to work in IPv4-mapped mode. In IPv4-mapped mode, it is not possible
0430     to bind same port from different IPv4 and IPv6 handlers.
0431 
0432     @param sock The socket to make in ipv6only working mode
0433     @return 0 on success, -1 on failure
0434  */
0435 EVENT2_EXPORT_SYMBOL
0436 int evutil_make_listen_socket_ipv6only(evutil_socket_t sock);
0437 
0438 /** Do platform-specific operations as needed to close a socket upon a
0439     successful execution of one of the exec*() functions.
0440 
0441     @param sock The socket to be closed
0442     @return 0 on success, -1 on failure
0443  */
0444 EVENT2_EXPORT_SYMBOL
0445 int evutil_make_socket_closeonexec(evutil_socket_t sock);
0446 
0447 /** Do the platform-specific call needed to close a socket returned from
0448     socket() or accept().
0449 
0450     @param sock The socket to be closed
0451     @return 0 on success (whether the operation is supported or not),
0452             -1 on failure
0453  */
0454 EVENT2_EXPORT_SYMBOL
0455 int evutil_closesocket(evutil_socket_t sock);
0456 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
0457 
0458 /** Do platform-specific operations, if possible, to make a tcp listener
0459  *  socket defer accept()s until there is data to read.
0460  *  
0461  *  Not all platforms support this.  You don't want to do this for every
0462  *  listener socket: only the ones that implement a protocol where the
0463  *  client transmits before the server needs to respond.
0464  *
0465  *  @param sock The listening socket to to make deferred
0466  *  @return 0 on success (whether the operation is supported or not),
0467  *       -1 on failure
0468 */ 
0469 EVENT2_EXPORT_SYMBOL
0470 int evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock);
0471 
0472 #ifdef _WIN32
0473 /** Return the most recent socket error.  Not idempotent on all platforms. */
0474 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
0475 /** Replace the most recent socket error with errcode */
0476 #define EVUTIL_SET_SOCKET_ERROR(errcode)        \
0477     do { WSASetLastError(errcode); } while (0)
0478 /** Return the most recent socket error to occur on sock. */
0479 EVENT2_EXPORT_SYMBOL
0480 int evutil_socket_geterror(evutil_socket_t sock);
0481 /** Convert a socket error to a string. */
0482 EVENT2_EXPORT_SYMBOL
0483 const char *evutil_socket_error_to_string(int errcode);
0484 #define EVUTIL_INVALID_SOCKET INVALID_SOCKET
0485 #elif defined(EVENT_IN_DOXYGEN_)
0486 /**
0487    @name Socket error functions
0488 
0489    These functions are needed for making programs compatible between
0490    Windows and Unix-like platforms.
0491 
0492    You see, Winsock handles socket errors differently from the rest of
0493    the world.  Elsewhere, a socket error is like any other error and is
0494    stored in errno.  But winsock functions require you to retrieve the
0495    error with a special function, and don't let you use strerror for
0496    the error codes.  And handling EWOULDBLOCK is ... different.
0497 
0498    @{
0499 */
0500 /** Return the most recent socket error.  Not idempotent on all platforms. */
0501 #define EVUTIL_SOCKET_ERROR() ...
0502 /** Replace the most recent socket error with errcode */
0503 #define EVUTIL_SET_SOCKET_ERROR(errcode) ...
0504 /** Return the most recent socket error to occur on sock. */
0505 #define evutil_socket_geterror(sock) ...
0506 /** Convert a socket error to a string. */
0507 #define evutil_socket_error_to_string(errcode) ...
0508 #define EVUTIL_INVALID_SOCKET -1
0509 /**@}*/
0510 #else /** !EVENT_IN_DOXYGEN_ && !_WIN32 */
0511 #define EVUTIL_SOCKET_ERROR() (errno)
0512 #define EVUTIL_SET_SOCKET_ERROR(errcode)        \
0513         do { errno = (errcode); } while (0)
0514 #define evutil_socket_geterror(sock) (errno)
0515 #define evutil_socket_error_to_string(errcode) (strerror(errcode))
0516 #define EVUTIL_INVALID_SOCKET -1
0517 #endif /** !_WIN32 */
0518 
0519 
0520 /**
0521  * @name Manipulation macros for struct timeval.
0522  *
0523  * We define replacements
0524  * for timeradd, timersub, timerclear, timercmp, and timerisset.
0525  *
0526  * @{
0527  */
0528 #ifdef EVENT__HAVE_TIMERADD
0529 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
0530 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
0531 #else
0532 #define evutil_timeradd(tvp, uvp, vvp)                  \
0533     do {                                \
0534         (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;      \
0535         (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
0536         if ((vvp)->tv_usec >= 1000000) {            \
0537             (vvp)->tv_sec++;                \
0538             (vvp)->tv_usec -= 1000000;          \
0539         }                           \
0540     } while (0)
0541 #define evutil_timersub(tvp, uvp, vvp)                  \
0542     do {                                \
0543         (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;      \
0544         (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;   \
0545         if ((vvp)->tv_usec < 0) {               \
0546             (vvp)->tv_sec--;                \
0547             (vvp)->tv_usec += 1000000;          \
0548         }                           \
0549     } while (0)
0550 #endif /* !EVENT__HAVE_TIMERADD */
0551 
0552 #ifdef EVENT__HAVE_TIMERCLEAR
0553 #define evutil_timerclear(tvp) timerclear(tvp)
0554 #else
0555 #define evutil_timerclear(tvp)  (tvp)->tv_sec = (tvp)->tv_usec = 0
0556 #endif
0557 /**@}*/
0558 
0559 /** Return true iff the tvp is related to uvp according to the relational
0560  * operator cmp.  Recognized values for cmp are ==, <=, <, >=, and >. */
0561 #define evutil_timercmp(tvp, uvp, cmp)                  \
0562     (((tvp)->tv_sec == (uvp)->tv_sec) ?             \
0563      ((tvp)->tv_usec cmp (uvp)->tv_usec) :              \
0564      ((tvp)->tv_sec cmp (uvp)->tv_sec))
0565 
0566 #ifdef EVENT__HAVE_TIMERISSET
0567 #define evutil_timerisset(tvp) timerisset(tvp)
0568 #else
0569 #define evutil_timerisset(tvp)  ((tvp)->tv_sec || (tvp)->tv_usec)
0570 #endif
0571 
0572 /** Replacement for offsetof on platforms that don't define it. */
0573 #ifdef offsetof
0574 #define evutil_offsetof(type, field) offsetof(type, field)
0575 #else
0576 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
0577 #endif
0578 
0579 /* big-int related functions */
0580 /** Parse a 64-bit value from a string.  Arguments are as for strtol. */
0581 EVENT2_EXPORT_SYMBOL
0582 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
0583 
0584 /** Replacement for gettimeofday on platforms that lack it. */
0585 #ifdef EVENT__HAVE_GETTIMEOFDAY
0586 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
0587 #else
0588 struct timezone;
0589 EVENT2_EXPORT_SYMBOL
0590 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
0591 #endif
0592 
0593 /** Replacement for snprintf to get consistent behavior on platforms for
0594     which the return value of snprintf does not conform to C99.
0595  */
0596 EVENT2_EXPORT_SYMBOL
0597 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
0598 #ifdef __GNUC__
0599     __attribute__((format(printf, 3, 4)))
0600 #endif
0601 ;
0602 /** Replacement for vsnprintf to get consistent behavior on platforms for
0603     which the return value of snprintf does not conform to C99.
0604  */
0605 EVENT2_EXPORT_SYMBOL
0606 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
0607 #ifdef __GNUC__
0608     __attribute__((format(printf, 3, 0)))
0609 #endif
0610 ;
0611 
0612 /** Replacement for inet_ntop for platforms which lack it. */
0613 EVENT2_EXPORT_SYMBOL
0614 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
0615 /** Variation of inet_pton that also parses IPv6 scopes. Public for
0616     unit tests. No reason to call this directly.
0617  */
0618 EVENT2_EXPORT_SYMBOL
0619 int evutil_inet_pton_scope(int af, const char *src, void *dst,
0620     unsigned *indexp);
0621 /** Replacement for inet_pton for platforms which lack it. */
0622 EVENT2_EXPORT_SYMBOL
0623 int evutil_inet_pton(int af, const char *src, void *dst);
0624 struct sockaddr;
0625 
0626 /** Parse an IPv4 or IPv6 address, with optional port, from a string.
0627 
0628     Recognized formats are:
0629     - [IPv6Address]:port
0630     - [IPv6Address]
0631     - IPv6Address
0632     - IPv4Address:port
0633     - IPv4Address
0634 
0635     If no port is specified, the port in the output is set to 0.
0636 
0637     @param str The string to parse.
0638     @param out A struct sockaddr to hold the result.  This should probably be
0639        a struct sockaddr_storage.
0640     @param outlen A pointer to the number of bytes that that 'out' can safely
0641        hold.  Set to the number of bytes used in 'out' on success.
0642     @return -1 if the address is not well-formed, if the port is out of range,
0643        or if out is not large enough to hold the result.  Otherwise returns
0644        0 on success.
0645 */
0646 EVENT2_EXPORT_SYMBOL
0647 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
0648 
0649 /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
0650  * preceeds sa2, or greater than 0 if sa1 follows sa2.  If include_port is
0651  * true, consider the port as well as the address.  Only implemented for
0652  * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
0653  * the same between Libevent versions. */
0654 EVENT2_EXPORT_SYMBOL
0655 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
0656     int include_port);
0657 
0658 /** As strcasecmp, but always compares the characters in locale-independent
0659     ASCII.  That's useful if you're handling data in ASCII-based protocols.
0660  */
0661 EVENT2_EXPORT_SYMBOL
0662 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
0663 /** As strncasecmp, but always compares the characters in locale-independent
0664     ASCII.  That's useful if you're handling data in ASCII-based protocols.
0665  */
0666 EVENT2_EXPORT_SYMBOL
0667 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
0668 
0669 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
0670  * if this system has no getaddrinfo(). */
0671 #ifdef EVENT__HAVE_STRUCT_ADDRINFO
0672 #define evutil_addrinfo addrinfo
0673 #else
0674 /** A definition of struct addrinfo for systems that lack it.
0675 
0676     (This is just an alias for struct addrinfo if the system defines
0677     struct addrinfo.)
0678 */
0679 struct evutil_addrinfo {
0680     int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
0681     int     ai_family;    /* PF_xxx */
0682     int     ai_socktype;  /* SOCK_xxx */
0683     int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
0684     size_t  ai_addrlen;   /* length of ai_addr */
0685     char   *ai_canonname; /* canonical name for nodename */
0686     struct sockaddr  *ai_addr; /* binary address */
0687     struct evutil_addrinfo  *ai_next; /* next structure in linked list */
0688 };
0689 #endif
0690 /** @name evutil_getaddrinfo() error codes
0691 
0692     These values are possible error codes for evutil_getaddrinfo() and
0693     related functions.
0694 
0695     @{
0696 */
0697 #if defined(EAI_ADDRFAMILY) && defined(EVENT__HAVE_GETADDRINFO)
0698 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
0699 #else
0700 #define EVUTIL_EAI_ADDRFAMILY -901
0701 #endif
0702 #if defined(EAI_AGAIN) && defined(EVENT__HAVE_GETADDRINFO)
0703 #define EVUTIL_EAI_AGAIN EAI_AGAIN
0704 #else
0705 #define EVUTIL_EAI_AGAIN -902
0706 #endif
0707 #if defined(EAI_BADFLAGS) && defined(EVENT__HAVE_GETADDRINFO)
0708 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
0709 #else
0710 #define EVUTIL_EAI_BADFLAGS -903
0711 #endif
0712 #if defined(EAI_FAIL) && defined(EVENT__HAVE_GETADDRINFO)
0713 #define EVUTIL_EAI_FAIL EAI_FAIL
0714 #else
0715 #define EVUTIL_EAI_FAIL -904
0716 #endif
0717 #if defined(EAI_FAMILY) && defined(EVENT__HAVE_GETADDRINFO)
0718 #define EVUTIL_EAI_FAMILY EAI_FAMILY
0719 #else
0720 #define EVUTIL_EAI_FAMILY -905
0721 #endif
0722 #if defined(EAI_MEMORY) && defined(EVENT__HAVE_GETADDRINFO)
0723 #define EVUTIL_EAI_MEMORY EAI_MEMORY
0724 #else
0725 #define EVUTIL_EAI_MEMORY -906
0726 #endif
0727 /* This test is a bit complicated, since some MS SDKs decide to
0728  * remove NODATA or redefine it to be the same as NONAME, in a
0729  * fun interpretation of RFC 2553 and RFC 3493. */
0730 #if defined(EAI_NODATA) && defined(EVENT__HAVE_GETADDRINFO) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
0731 #define EVUTIL_EAI_NODATA EAI_NODATA
0732 #else
0733 #define EVUTIL_EAI_NODATA -907
0734 #endif
0735 #if defined(EAI_NONAME) && defined(EVENT__HAVE_GETADDRINFO)
0736 #define EVUTIL_EAI_NONAME EAI_NONAME
0737 #else
0738 #define EVUTIL_EAI_NONAME -908
0739 #endif
0740 #if defined(EAI_SERVICE) && defined(EVENT__HAVE_GETADDRINFO)
0741 #define EVUTIL_EAI_SERVICE EAI_SERVICE
0742 #else
0743 #define EVUTIL_EAI_SERVICE -909
0744 #endif
0745 #if defined(EAI_SOCKTYPE) && defined(EVENT__HAVE_GETADDRINFO)
0746 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
0747 #else
0748 #define EVUTIL_EAI_SOCKTYPE -910
0749 #endif
0750 #if defined(EAI_SYSTEM) && defined(EVENT__HAVE_GETADDRINFO)
0751 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
0752 #else
0753 #define EVUTIL_EAI_SYSTEM -911
0754 #endif
0755 
0756 #define EVUTIL_EAI_CANCEL -90001
0757 
0758 #if defined(AI_PASSIVE) && defined(EVENT__HAVE_GETADDRINFO)
0759 #define EVUTIL_AI_PASSIVE AI_PASSIVE
0760 #else
0761 #define EVUTIL_AI_PASSIVE 0x1000
0762 #endif
0763 #if defined(AI_CANONNAME) && defined(EVENT__HAVE_GETADDRINFO)
0764 #define EVUTIL_AI_CANONNAME AI_CANONNAME
0765 #else
0766 #define EVUTIL_AI_CANONNAME 0x2000
0767 #endif
0768 #if defined(AI_NUMERICHOST) && defined(EVENT__HAVE_GETADDRINFO)
0769 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
0770 #else
0771 #define EVUTIL_AI_NUMERICHOST 0x4000
0772 #endif
0773 #if defined(AI_NUMERICSERV) && defined(EVENT__HAVE_GETADDRINFO)
0774 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
0775 #else
0776 #define EVUTIL_AI_NUMERICSERV 0x8000
0777 #endif
0778 #if defined(AI_V4MAPPED) && defined(EVENT__HAVE_GETADDRINFO)
0779 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
0780 #else
0781 #define EVUTIL_AI_V4MAPPED 0x10000
0782 #endif
0783 #if defined(AI_ALL) && defined(EVENT__HAVE_GETADDRINFO)
0784 #define EVUTIL_AI_ALL AI_ALL
0785 #else
0786 #define EVUTIL_AI_ALL 0x20000
0787 #endif
0788 #if defined(AI_ADDRCONFIG) && defined(EVENT__HAVE_GETADDRINFO)
0789 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
0790 #else
0791 #define EVUTIL_AI_ADDRCONFIG 0x40000
0792 #endif
0793 /**@}*/
0794 
0795 struct evutil_addrinfo;
0796 /**
0797  * This function clones getaddrinfo for systems that don't have it.  For full
0798  * details, see RFC 3493, section 6.1.
0799  *
0800  * Limitations:
0801  * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
0802  *   gethostbyname, with their attendant issues.
0803  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
0804  *
0805  * For a nonblocking variant, see evdns_getaddrinfo.
0806  */
0807 EVENT2_EXPORT_SYMBOL
0808 int evutil_getaddrinfo(const char *nodename, const char *servname,
0809     const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
0810 
0811 /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
0812 EVENT2_EXPORT_SYMBOL
0813 void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
0814 
0815 EVENT2_EXPORT_SYMBOL
0816 const char *evutil_gai_strerror(int err);
0817 
0818 /** Generate n bytes of secure pseudorandom data, and store them in buf.
0819  *
0820  * Current versions of Libevent use an ARC4-based random number generator,
0821  * seeded using the platform's entropy source (/dev/urandom on Unix-like
0822  * systems; CryptGenRandom on Windows).  This is not actually as secure as it
0823  * should be: ARC4 is a pretty lousy cipher, and the current implementation
0824  * provides only rudimentary prediction- and backtracking-resistance.  Don't
0825  * use this for serious cryptographic applications.
0826  */
0827 EVENT2_EXPORT_SYMBOL
0828 void evutil_secure_rng_get_bytes(void *buf, size_t n);
0829 
0830 /**
0831  * Seed the secure random number generator if needed, and return 0 on
0832  * success or -1 on failure.
0833  *
0834  * It is okay to call this function more than once; it will still return
0835  * 0 if the RNG has been successfully seeded and -1 if it can't be
0836  * seeded.
0837  *
0838  * Ordinarily you don't need to call this function from your own code;
0839  * Libevent will seed the RNG itself the first time it needs good random
0840  * numbers.  You only need to call it if (a) you want to double-check
0841  * that one of the seeding methods did succeed, or (b) you plan to drop
0842  * the capability to seed (by chrooting, or dropping capabilities, or
0843  * whatever), and you want to make sure that seeding happens before your
0844  * program loses the ability to do it.
0845  */
0846 EVENT2_EXPORT_SYMBOL
0847 int evutil_secure_rng_init(void);
0848 
0849 /**
0850  * Set a filename to use in place of /dev/urandom for seeding the secure
0851  * PRNG. Return 0 on success, -1 on failure.
0852  *
0853  * Call this function BEFORE calling any other initialization or RNG
0854  * functions.
0855  *
0856  * (This string will _NOT_ be copied internally. Do not free it while any
0857  * user of the secure RNG might be running. Don't pass anything other than a
0858  * real /dev/...random device file here, or you might lose security.)
0859  *
0860  * This API is unstable, and might change in a future libevent version.
0861  */
0862 EVENT2_EXPORT_SYMBOL
0863 int evutil_secure_rng_set_urandom_device_file(char *fname);
0864 
0865 #if !defined(EVENT__HAVE_ARC4RANDOM) || defined(EVENT__HAVE_ARC4RANDOM_ADDRANDOM)
0866 /** Seed the random number generator with extra random bytes.
0867 
0868     You should almost never need to call this function; it should be
0869     sufficient to invoke evutil_secure_rng_init(), or let Libevent take
0870     care of calling evutil_secure_rng_init() on its own.
0871 
0872     If you call this function as a _replacement_ for the regular
0873     entropy sources, then you need to be sure that your input
0874     contains a fairly large amount of strong entropy.  Doing so is
0875     notoriously hard: most people who try get it wrong.  Watch out!
0876 
0877     @param dat a buffer full of a strong source of random numbers
0878     @param datlen the number of bytes to read from datlen
0879  */
0880 EVENT2_EXPORT_SYMBOL
0881 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
0882 #endif
0883 
0884 #ifdef __cplusplus
0885 }
0886 #endif
0887 
0888 #endif /* EVENT1_EVUTIL_H_INCLUDED_ */