1*9034ec65Schristos /*	$NetBSD: util.h,v 1.6 2020/05/25 20:47:34 christos Exp $	*/
22b3787f6Schristos 
32b3787f6Schristos /*
42b3787f6Schristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
52b3787f6Schristos  *
62b3787f6Schristos  * Redistribution and use in source and binary forms, with or without
72b3787f6Schristos  * modification, are permitted provided that the following conditions
82b3787f6Schristos  * are met:
92b3787f6Schristos  * 1. Redistributions of source code must retain the above copyright
102b3787f6Schristos  *    notice, this list of conditions and the following disclaimer.
112b3787f6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
122b3787f6Schristos  *    notice, this list of conditions and the following disclaimer in the
132b3787f6Schristos  *    documentation and/or other materials provided with the distribution.
142b3787f6Schristos  * 3. The name of the author may not be used to endorse or promote products
152b3787f6Schristos  *    derived from this software without specific prior written permission.
162b3787f6Schristos  *
172b3787f6Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
182b3787f6Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192b3787f6Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202b3787f6Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
212b3787f6Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
222b3787f6Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232b3787f6Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242b3787f6Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252b3787f6Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
262b3787f6Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272b3787f6Schristos  */
282b3787f6Schristos #ifndef EVENT2_UTIL_H_INCLUDED_
292b3787f6Schristos #define EVENT2_UTIL_H_INCLUDED_
302b3787f6Schristos 
312b3787f6Schristos /** @file event2/util.h
322b3787f6Schristos 
332b3787f6Schristos   Common convenience functions for cross-platform portability and
342b3787f6Schristos   related socket manipulations.
352b3787f6Schristos 
362b3787f6Schristos  */
371b6f2cd4Schristos #include <event2/visibility.h>
382b3787f6Schristos 
392b3787f6Schristos #ifdef __cplusplus
402b3787f6Schristos extern "C" {
412b3787f6Schristos #endif
422b3787f6Schristos 
432b3787f6Schristos #include <event2/event-config.h>
442b3787f6Schristos #ifdef EVENT__HAVE_SYS_TIME_H
452b3787f6Schristos #include <sys/time.h>
462b3787f6Schristos #endif
472b3787f6Schristos #ifdef EVENT__HAVE_STDINT_H
482b3787f6Schristos #include <stdint.h>
492b3787f6Schristos #elif defined(EVENT__HAVE_INTTYPES_H)
502b3787f6Schristos #include <inttypes.h>
512b3787f6Schristos #endif
522b3787f6Schristos #ifdef EVENT__HAVE_SYS_TYPES_H
532b3787f6Schristos #include <sys/types.h>
542b3787f6Schristos #endif
552b3787f6Schristos #ifdef EVENT__HAVE_STDDEF_H
562b3787f6Schristos #include <stddef.h>
572b3787f6Schristos #endif
582b3787f6Schristos #ifdef _MSC_VER
592b3787f6Schristos #include <BaseTsd.h>
602b3787f6Schristos #endif
612b3787f6Schristos #include <stdarg.h>
622b3787f6Schristos #ifdef EVENT__HAVE_NETDB_H
632b3787f6Schristos #if !defined(_GNU_SOURCE)
642b3787f6Schristos #define _GNU_SOURCE
652b3787f6Schristos #endif
662b3787f6Schristos #include <netdb.h>
672b3787f6Schristos #endif
682b3787f6Schristos 
692b3787f6Schristos #ifdef _WIN32
702b3787f6Schristos #include <winsock2.h>
712b3787f6Schristos #ifdef EVENT__HAVE_GETADDRINFO
722b3787f6Schristos /* for EAI_* definitions. */
732b3787f6Schristos #include <ws2tcpip.h>
742b3787f6Schristos #endif
752b3787f6Schristos #else
762b3787f6Schristos #include <sys/socket.h>
772b3787f6Schristos #endif
782b3787f6Schristos 
792b3787f6Schristos /* Some openbsd autoconf versions get the name of this macro wrong. */
802b3787f6Schristos #if defined(EVENT__SIZEOF_VOID__) && !defined(EVENT__SIZEOF_VOID_P)
812b3787f6Schristos #define EVENT__SIZEOF_VOID_P EVENT__SIZEOF_VOID__
822b3787f6Schristos #endif
832b3787f6Schristos 
842b3787f6Schristos /**
852b3787f6Schristos  * @name Standard integer types.
862b3787f6Schristos  *
872b3787f6Schristos  * Integer type definitions for types that are supposed to be defined in the
882b3787f6Schristos  * C99-specified stdint.h.  Shamefully, some platforms do not include
892b3787f6Schristos  * stdint.h, so we need to replace it.  (If you are on a platform like this,
902b3787f6Schristos  * your C headers are now over 10 years out of date.  You should bug them to
912b3787f6Schristos  * do something about this.)
922b3787f6Schristos  *
932b3787f6Schristos  * We define:
942b3787f6Schristos  *
952b3787f6Schristos  * <dl>
962b3787f6Schristos  *   <dt>ev_uint64_t, ev_uint32_t, ev_uint16_t, ev_uint8_t</dt>
972b3787f6Schristos  *      <dd>unsigned integer types of exactly 64, 32, 16, and 8 bits
982b3787f6Schristos  *          respectively.</dd>
992b3787f6Schristos  *    <dt>ev_int64_t, ev_int32_t, ev_int16_t, ev_int8_t</dt>
1002b3787f6Schristos  *      <dd>signed integer types of exactly 64, 32, 16, and 8 bits
1012b3787f6Schristos  *          respectively.</dd>
1022b3787f6Schristos  *    <dt>ev_uintptr_t, ev_intptr_t</dt>
1032b3787f6Schristos  *      <dd>unsigned/signed integers large enough
1042b3787f6Schristos  *      to hold a pointer without loss of bits.</dd>
1052b3787f6Schristos  *    <dt>ev_ssize_t</dt>
1062b3787f6Schristos  *      <dd>A signed type of the same size as size_t</dd>
1072b3787f6Schristos  *    <dt>ev_off_t</dt>
1082b3787f6Schristos  *      <dd>A signed type typically used to represent offsets within a
1092b3787f6Schristos  *      (potentially large) file</dd>
1102b3787f6Schristos  *
1112b3787f6Schristos  * @{
1122b3787f6Schristos  */
1132b3787f6Schristos #ifdef EVENT__HAVE_UINT64_T
1142b3787f6Schristos #define ev_uint64_t uint64_t
1152b3787f6Schristos #define ev_int64_t int64_t
1162b3787f6Schristos #elif defined(_WIN32)
1172b3787f6Schristos #define ev_uint64_t unsigned __int64
1182b3787f6Schristos #define ev_int64_t signed __int64
1192b3787f6Schristos #elif EVENT__SIZEOF_LONG_LONG == 8
1202b3787f6Schristos #define ev_uint64_t unsigned long long
1212b3787f6Schristos #define ev_int64_t long long
1222b3787f6Schristos #elif EVENT__SIZEOF_LONG == 8
1232b3787f6Schristos #define ev_uint64_t unsigned long
1242b3787f6Schristos #define ev_int64_t long
1252b3787f6Schristos #elif defined(EVENT_IN_DOXYGEN_)
1262b3787f6Schristos #define ev_uint64_t ...
1272b3787f6Schristos #define ev_int64_t ...
1282b3787f6Schristos #else
1292b3787f6Schristos #error "No way to define ev_uint64_t"
1302b3787f6Schristos #endif
1312b3787f6Schristos 
1322b3787f6Schristos #ifdef EVENT__HAVE_UINT32_T
1332b3787f6Schristos #define ev_uint32_t uint32_t
1342b3787f6Schristos #define ev_int32_t int32_t
1352b3787f6Schristos #elif defined(_WIN32)
1362b3787f6Schristos #define ev_uint32_t unsigned int
1372b3787f6Schristos #define ev_int32_t signed int
1382b3787f6Schristos #elif EVENT__SIZEOF_LONG == 4
1392b3787f6Schristos #define ev_uint32_t unsigned long
1402b3787f6Schristos #define ev_int32_t signed long
1412b3787f6Schristos #elif EVENT__SIZEOF_INT == 4
1422b3787f6Schristos #define ev_uint32_t unsigned int
1432b3787f6Schristos #define ev_int32_t signed int
1442b3787f6Schristos #elif defined(EVENT_IN_DOXYGEN_)
1452b3787f6Schristos #define ev_uint32_t ...
1462b3787f6Schristos #define ev_int32_t ...
1472b3787f6Schristos #else
1482b3787f6Schristos #error "No way to define ev_uint32_t"
1492b3787f6Schristos #endif
1502b3787f6Schristos 
1512b3787f6Schristos #ifdef EVENT__HAVE_UINT16_T
1522b3787f6Schristos #define ev_uint16_t uint16_t
1532b3787f6Schristos #define ev_int16_t  int16_t
1542b3787f6Schristos #elif defined(_WIN32)
1552b3787f6Schristos #define ev_uint16_t unsigned short
1562b3787f6Schristos #define ev_int16_t  signed short
1572b3787f6Schristos #elif EVENT__SIZEOF_INT == 2
1582b3787f6Schristos #define ev_uint16_t unsigned int
1592b3787f6Schristos #define ev_int16_t  signed int
1602b3787f6Schristos #elif EVENT__SIZEOF_SHORT == 2
1612b3787f6Schristos #define ev_uint16_t unsigned short
1622b3787f6Schristos #define ev_int16_t  signed short
1632b3787f6Schristos #elif defined(EVENT_IN_DOXYGEN_)
1642b3787f6Schristos #define ev_uint16_t ...
1652b3787f6Schristos #define ev_int16_t ...
1662b3787f6Schristos #else
1672b3787f6Schristos #error "No way to define ev_uint16_t"
1682b3787f6Schristos #endif
1692b3787f6Schristos 
1702b3787f6Schristos #ifdef EVENT__HAVE_UINT8_T
1712b3787f6Schristos #define ev_uint8_t uint8_t
1722b3787f6Schristos #define ev_int8_t int8_t
1732b3787f6Schristos #elif defined(EVENT_IN_DOXYGEN_)
1742b3787f6Schristos #define ev_uint8_t ...
1752b3787f6Schristos #define ev_int8_t ...
1762b3787f6Schristos #else
1772b3787f6Schristos #define ev_uint8_t unsigned char
1782b3787f6Schristos #define ev_int8_t signed char
1792b3787f6Schristos #endif
1802b3787f6Schristos 
1812b3787f6Schristos #ifdef EVENT__HAVE_UINTPTR_T
1822b3787f6Schristos #define ev_uintptr_t uintptr_t
1832b3787f6Schristos #define ev_intptr_t intptr_t
1842b3787f6Schristos #elif EVENT__SIZEOF_VOID_P <= 4
1852b3787f6Schristos #define ev_uintptr_t ev_uint32_t
1862b3787f6Schristos #define ev_intptr_t ev_int32_t
1872b3787f6Schristos #elif EVENT__SIZEOF_VOID_P <= 8
1882b3787f6Schristos #define ev_uintptr_t ev_uint64_t
1892b3787f6Schristos #define ev_intptr_t ev_int64_t
1902b3787f6Schristos #elif defined(EVENT_IN_DOXYGEN_)
1912b3787f6Schristos #define ev_uintptr_t ...
1922b3787f6Schristos #define ev_intptr_t ...
1932b3787f6Schristos #else
1942b3787f6Schristos #error "No way to define ev_uintptr_t"
1952b3787f6Schristos #endif
1962b3787f6Schristos 
1972b3787f6Schristos #ifdef EVENT__ssize_t
1982b3787f6Schristos #define ev_ssize_t EVENT__ssize_t
1992b3787f6Schristos #else
2002b3787f6Schristos #define ev_ssize_t ssize_t
2012b3787f6Schristos #endif
2022b3787f6Schristos 
2032b3787f6Schristos /* Note that we define ev_off_t based on the compile-time size of off_t that
2042b3787f6Schristos  * we used to build Libevent, and not based on the current size of off_t.
2052b3787f6Schristos  * (For example, we don't define ev_off_t to off_t.).  We do this because
2062b3787f6Schristos  * some systems let you build your software with different off_t sizes
2072b3787f6Schristos  * at runtime, and so putting in any dependency on off_t would risk API
2082b3787f6Schristos  * mismatch.
2092b3787f6Schristos  */
2102b3787f6Schristos #ifdef _WIN32
2112b3787f6Schristos #define ev_off_t ev_int64_t
2122b3787f6Schristos #elif EVENT__SIZEOF_OFF_T == 8
2132b3787f6Schristos #define ev_off_t ev_int64_t
2142b3787f6Schristos #elif EVENT__SIZEOF_OFF_T == 4
2152b3787f6Schristos #define ev_off_t ev_int32_t
2162b3787f6Schristos #elif defined(EVENT_IN_DOXYGEN_)
2172b3787f6Schristos #define ev_off_t ...
2182b3787f6Schristos #else
2192b3787f6Schristos #define ev_off_t off_t
2202b3787f6Schristos #endif
2212b3787f6Schristos /**@}*/
2222b3787f6Schristos 
2232b3787f6Schristos /* Limits for integer types.
2242b3787f6Schristos 
2252b3787f6Schristos    We're making two assumptions here:
2262b3787f6Schristos      - The compiler does constant folding properly.
2272b3787f6Schristos      - The platform does signed arithmetic in two's complement.
2282b3787f6Schristos */
2292b3787f6Schristos 
2302b3787f6Schristos /**
2312b3787f6Schristos    @name Limits for integer types
2322b3787f6Schristos 
2332b3787f6Schristos    These macros hold the largest or smallest values possible for the
2342b3787f6Schristos    ev_[u]int*_t types.
2352b3787f6Schristos 
2362b3787f6Schristos    @{
2372b3787f6Schristos */
2382b3787f6Schristos #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
2392b3787f6Schristos #define EV_INT64_MAX  ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
2402b3787f6Schristos #define EV_INT64_MIN  ((-EV_INT64_MAX) - 1)
2412b3787f6Schristos #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
2422b3787f6Schristos #define EV_INT32_MAX  ((ev_int32_t) 0x7fffffffL)
2432b3787f6Schristos #define EV_INT32_MIN  ((-EV_INT32_MAX) - 1)
2442b3787f6Schristos #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
2452b3787f6Schristos #define EV_INT16_MAX  ((ev_int16_t) 0x7fffL)
2462b3787f6Schristos #define EV_INT16_MIN  ((-EV_INT16_MAX) - 1)
2472b3787f6Schristos #define EV_UINT8_MAX  255
2482b3787f6Schristos #define EV_INT8_MAX   127
2492b3787f6Schristos #define EV_INT8_MIN   ((-EV_INT8_MAX) - 1)
2502b3787f6Schristos /** @} */
2512b3787f6Schristos 
2522b3787f6Schristos /**
2532b3787f6Schristos    @name Limits for SIZE_T and SSIZE_T
2542b3787f6Schristos 
2552b3787f6Schristos    @{
2562b3787f6Schristos */
2572b3787f6Schristos #if EVENT__SIZEOF_SIZE_T == 8
2582b3787f6Schristos #define EV_SIZE_MAX EV_UINT64_MAX
2592b3787f6Schristos #define EV_SSIZE_MAX EV_INT64_MAX
2602b3787f6Schristos #elif EVENT__SIZEOF_SIZE_T == 4
2612b3787f6Schristos #define EV_SIZE_MAX EV_UINT32_MAX
2622b3787f6Schristos #define EV_SSIZE_MAX EV_INT32_MAX
2632b3787f6Schristos #elif defined(EVENT_IN_DOXYGEN_)
2642b3787f6Schristos #define EV_SIZE_MAX ...
2652b3787f6Schristos #define EV_SSIZE_MAX ...
2662b3787f6Schristos #else
2672b3787f6Schristos #error "No way to define SIZE_MAX"
2682b3787f6Schristos #endif
2692b3787f6Schristos 
2702b3787f6Schristos #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
2712b3787f6Schristos /**@}*/
2722b3787f6Schristos 
2732b3787f6Schristos #ifdef _WIN32
2742b3787f6Schristos #define ev_socklen_t int
2752b3787f6Schristos #elif defined(EVENT__socklen_t)
2762b3787f6Schristos #define ev_socklen_t EVENT__socklen_t
2772b3787f6Schristos #else
2782b3787f6Schristos #define ev_socklen_t socklen_t
2792b3787f6Schristos #endif
2802b3787f6Schristos 
2812b3787f6Schristos #ifdef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
2822b3787f6Schristos #if !defined(EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
2832b3787f6Schristos  && !defined(ss_family)
2842b3787f6Schristos #define ss_family __ss_family
2852b3787f6Schristos #endif
2862b3787f6Schristos #endif
2872b3787f6Schristos 
2882b3787f6Schristos /**
2892b3787f6Schristos  * A type wide enough to hold the output of "socket()" or "accept()".  On
2902b3787f6Schristos  * Windows, this is an intptr_t; elsewhere, it is an int. */
2912b3787f6Schristos #ifdef _WIN32
2922b3787f6Schristos #define evutil_socket_t intptr_t
2932b3787f6Schristos #else
2942b3787f6Schristos #define evutil_socket_t int
2952b3787f6Schristos #endif
2962b3787f6Schristos 
29750cc4415Schristos /**
29850cc4415Schristos  * Structure to hold information about a monotonic timer
29950cc4415Schristos  *
30050cc4415Schristos  * Use this with evutil_configure_monotonic_time() and
30150cc4415Schristos  * evutil_gettime_monotonic().
30250cc4415Schristos  *
30350cc4415Schristos  * This is an opaque structure; you can allocate one using
30450cc4415Schristos  * evutil_monotonic_timer_new().
30550cc4415Schristos  *
30650cc4415Schristos  * @see evutil_monotonic_timer_new(), evutil_monotonic_timer_free(),
30750cc4415Schristos  * evutil_configure_monotonic_time(), evutil_gettime_monotonic()
30850cc4415Schristos  */
30950cc4415Schristos struct evutil_monotonic_timer
31050cc4415Schristos #ifdef EVENT_IN_DOXYGEN_
31150cc4415Schristos {/*Empty body so that doxygen will generate documentation here.*/}
31250cc4415Schristos #endif
31350cc4415Schristos ;
31450cc4415Schristos 
31550cc4415Schristos #define EV_MONOT_PRECISE  1
31650cc4415Schristos #define EV_MONOT_FALLBACK 2
31750cc4415Schristos 
31850cc4415Schristos /** Allocate a new struct evutil_monotonic_timer for use with the
31950cc4415Schristos  * evutil_configure_monotonic_time() and evutil_gettime_monotonic()
32050cc4415Schristos  * functions.  You must configure the timer with
32150cc4415Schristos  * evutil_configure_monotonic_time() before using it.
32250cc4415Schristos  */
32350cc4415Schristos EVENT2_EXPORT_SYMBOL
32450cc4415Schristos struct evutil_monotonic_timer * evutil_monotonic_timer_new(void);
32550cc4415Schristos 
32650cc4415Schristos /** Free a struct evutil_monotonic_timer that was allocated using
32750cc4415Schristos  * evutil_monotonic_timer_new().
32850cc4415Schristos  */
32950cc4415Schristos EVENT2_EXPORT_SYMBOL
33050cc4415Schristos void evutil_monotonic_timer_free(struct evutil_monotonic_timer *timer);
33150cc4415Schristos 
33250cc4415Schristos /** Set up a struct evutil_monotonic_timer; flags can include
33350cc4415Schristos  * EV_MONOT_PRECISE and EV_MONOT_FALLBACK.
33450cc4415Schristos  */
33550cc4415Schristos EVENT2_EXPORT_SYMBOL
33650cc4415Schristos int evutil_configure_monotonic_time(struct evutil_monotonic_timer *timer,
33750cc4415Schristos                                     int flags);
33850cc4415Schristos 
33950cc4415Schristos /** Query the current monotonic time from a struct evutil_monotonic_timer
34050cc4415Schristos  * previously configured with evutil_configure_monotonic_time().  Monotonic
34150cc4415Schristos  * time is guaranteed never to run in reverse, but is not necessarily epoch-
34250cc4415Schristos  * based, or relative to any other definite point.  Use it to make reliable
34350cc4415Schristos  * measurements of elapsed time between events even when the system time
34450cc4415Schristos  * may be changed.
34550cc4415Schristos  *
34650cc4415Schristos  * It is not safe to use this funtion on the same timer from multiple
34750cc4415Schristos  * threads.
34850cc4415Schristos  */
34950cc4415Schristos EVENT2_EXPORT_SYMBOL
35050cc4415Schristos int evutil_gettime_monotonic(struct evutil_monotonic_timer *timer,
35150cc4415Schristos                              struct timeval *tp);
35250cc4415Schristos 
3532b3787f6Schristos /** Create two new sockets that are connected to each other.
3542b3787f6Schristos 
3552b3787f6Schristos     On Unix, this simply calls socketpair().  On Windows, it uses the
3562b3787f6Schristos     loopback network interface on 127.0.0.1, and only
3572b3787f6Schristos     AF_INET,SOCK_STREAM are supported.
3582b3787f6Schristos 
3592b3787f6Schristos     (This may fail on some Windows hosts where firewall software has cleverly
3602b3787f6Schristos     decided to keep 127.0.0.1 from talking to itself.)
3612b3787f6Schristos 
3622b3787f6Schristos     Parameters and return values are as for socketpair()
3632b3787f6Schristos */
3641b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
3652b3787f6Schristos int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
3662b3787f6Schristos /** Do platform-specific operations as needed to make a socket nonblocking.
3672b3787f6Schristos 
3682b3787f6Schristos     @param sock The socket to make nonblocking
3692b3787f6Schristos     @return 0 on success, -1 on failure
3702b3787f6Schristos  */
3711b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
3722b3787f6Schristos int evutil_make_socket_nonblocking(evutil_socket_t sock);
3732b3787f6Schristos 
3742b3787f6Schristos /** Do platform-specific operations to make a listener socket reusable.
3752b3787f6Schristos 
3762b3787f6Schristos     Specifically, we want to make sure that another program will be able
3772b3787f6Schristos     to bind this address right after we've closed the listener.
3782b3787f6Schristos 
3792b3787f6Schristos     This differs from Windows's interpretation of "reusable", which
3802b3787f6Schristos     allows multiple listeners to bind the same address at the same time.
3812b3787f6Schristos 
3822b3787f6Schristos     @param sock The socket to make reusable
3832b3787f6Schristos     @return 0 on success, -1 on failure
3842b3787f6Schristos  */
3851b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
3862b3787f6Schristos int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
3872b3787f6Schristos 
38850cc4415Schristos /** Do platform-specific operations to make a listener port reusable.
38950cc4415Schristos 
39050cc4415Schristos     Specifically, we want to make sure that multiple programs which also
39150cc4415Schristos     set the same socket option will be able to bind, listen at the same time.
39250cc4415Schristos 
39350cc4415Schristos     This is a feature available only to Linux 3.9+
39450cc4415Schristos 
39550cc4415Schristos     @param sock The socket to make reusable
39650cc4415Schristos     @return 0 on success, -1 on failure
39750cc4415Schristos  */
39850cc4415Schristos EVENT2_EXPORT_SYMBOL
39950cc4415Schristos int evutil_make_listen_socket_reuseable_port(evutil_socket_t sock);
40050cc4415Schristos 
4012b3787f6Schristos /** Do platform-specific operations as needed to close a socket upon a
4022b3787f6Schristos     successful execution of one of the exec*() functions.
4032b3787f6Schristos 
4042b3787f6Schristos     @param sock The socket to be closed
4052b3787f6Schristos     @return 0 on success, -1 on failure
4062b3787f6Schristos  */
4071b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
4082b3787f6Schristos int evutil_make_socket_closeonexec(evutil_socket_t sock);
4092b3787f6Schristos 
4102b3787f6Schristos /** Do the platform-specific call needed to close a socket returned from
4112b3787f6Schristos     socket() or accept().
4122b3787f6Schristos 
4132b3787f6Schristos     @param sock The socket to be closed
4142b3787f6Schristos     @return 0 on success, -1 on failure
4152b3787f6Schristos  */
4161b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
4172b3787f6Schristos int evutil_closesocket(evutil_socket_t sock);
4182b3787f6Schristos #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
4192b3787f6Schristos 
4202b3787f6Schristos /** Do platform-specific operations, if possible, to make a tcp listener
4212b3787f6Schristos  *  socket defer accept()s until there is data to read.
4222b3787f6Schristos  *
4232b3787f6Schristos  *  Not all platforms support this.  You don't want to do this for every
4242b3787f6Schristos  *  listener socket: only the ones that implement a protocol where the
4252b3787f6Schristos  *  client transmits before the server needs to respond.
4262b3787f6Schristos  *
4272b3787f6Schristos  *  @param sock The listening socket to to make deferred
4282b3787f6Schristos  *  @return 0 on success (whether the operation is supported or not),
4292b3787f6Schristos  *       -1 on failure
4302b3787f6Schristos */
4311b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
4322b3787f6Schristos int evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock);
4332b3787f6Schristos 
4342b3787f6Schristos #ifdef _WIN32
4352b3787f6Schristos /** Return the most recent socket error.  Not idempotent on all platforms. */
4362b3787f6Schristos #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
4372b3787f6Schristos /** Replace the most recent socket error with errcode */
4382b3787f6Schristos #define EVUTIL_SET_SOCKET_ERROR(errcode)		\
4392b3787f6Schristos 	do { WSASetLastError(errcode); } while (0)
4402b3787f6Schristos /** Return the most recent socket error to occur on sock. */
4411b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
4422b3787f6Schristos int evutil_socket_geterror(evutil_socket_t sock);
4432b3787f6Schristos /** Convert a socket error to a string. */
4441b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
4452b3787f6Schristos const char *evutil_socket_error_to_string(int errcode);
4462b3787f6Schristos #elif defined(EVENT_IN_DOXYGEN_)
4472b3787f6Schristos /**
4482b3787f6Schristos    @name Socket error functions
4492b3787f6Schristos 
4502b3787f6Schristos    These functions are needed for making programs compatible between
4512b3787f6Schristos    Windows and Unix-like platforms.
4522b3787f6Schristos 
4532b3787f6Schristos    You see, Winsock handles socket errors differently from the rest of
4542b3787f6Schristos    the world.  Elsewhere, a socket error is like any other error and is
4552b3787f6Schristos    stored in errno.  But winsock functions require you to retrieve the
4562b3787f6Schristos    error with a special function, and don't let you use strerror for
4572b3787f6Schristos    the error codes.  And handling EWOULDBLOCK is ... different.
4582b3787f6Schristos 
4592b3787f6Schristos    @{
4602b3787f6Schristos */
4612b3787f6Schristos /** Return the most recent socket error.  Not idempotent on all platforms. */
4622b3787f6Schristos #define EVUTIL_SOCKET_ERROR() ...
4632b3787f6Schristos /** Replace the most recent socket error with errcode */
4642b3787f6Schristos #define EVUTIL_SET_SOCKET_ERROR(errcode) ...
4652b3787f6Schristos /** Return the most recent socket error to occur on sock. */
4662b3787f6Schristos #define evutil_socket_geterror(sock) ...
4672b3787f6Schristos /** Convert a socket error to a string. */
4682b3787f6Schristos #define evutil_socket_error_to_string(errcode) ...
4692b3787f6Schristos /**@}*/
4702b3787f6Schristos #else
4712b3787f6Schristos #define EVUTIL_SOCKET_ERROR() (errno)
4722b3787f6Schristos #define EVUTIL_SET_SOCKET_ERROR(errcode)		\
4732b3787f6Schristos 		do { errno = (errcode); } while (0)
4742b3787f6Schristos #define evutil_socket_geterror(sock) (errno)
4752b3787f6Schristos #define evutil_socket_error_to_string(errcode) (strerror(errcode))
4762b3787f6Schristos #endif
4772b3787f6Schristos 
4782b3787f6Schristos 
4792b3787f6Schristos /**
4802b3787f6Schristos  * @name Manipulation macros for struct timeval.
4812b3787f6Schristos  *
4822b3787f6Schristos  * We define replacements
4832b3787f6Schristos  * for timeradd, timersub, timerclear, timercmp, and timerisset.
4842b3787f6Schristos  *
4852b3787f6Schristos  * @{
4862b3787f6Schristos  */
4872b3787f6Schristos #ifdef EVENT__HAVE_TIMERADD
4882b3787f6Schristos #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
4892b3787f6Schristos #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
4902b3787f6Schristos #else
4912b3787f6Schristos #define evutil_timeradd(tvp, uvp, vvp)					\
4922b3787f6Schristos 	do {								\
4932b3787f6Schristos 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
4942b3787f6Schristos 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
4952b3787f6Schristos 		if ((vvp)->tv_usec >= 1000000) {			\
4962b3787f6Schristos 			(vvp)->tv_sec++;				\
4972b3787f6Schristos 			(vvp)->tv_usec -= 1000000;			\
4982b3787f6Schristos 		}							\
4992b3787f6Schristos 	} while (0)
5002b3787f6Schristos #define	evutil_timersub(tvp, uvp, vvp)					\
5012b3787f6Schristos 	do {								\
5022b3787f6Schristos 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
5032b3787f6Schristos 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
5042b3787f6Schristos 		if ((vvp)->tv_usec < 0) {				\
5052b3787f6Schristos 			(vvp)->tv_sec--;				\
5062b3787f6Schristos 			(vvp)->tv_usec += 1000000;			\
5072b3787f6Schristos 		}							\
5082b3787f6Schristos 	} while (0)
5092b3787f6Schristos #endif /* !EVENT__HAVE_TIMERADD */
5102b3787f6Schristos 
5112b3787f6Schristos #ifdef EVENT__HAVE_TIMERCLEAR
5122b3787f6Schristos #define evutil_timerclear(tvp) timerclear(tvp)
5132b3787f6Schristos #else
5142b3787f6Schristos #define	evutil_timerclear(tvp)	(tvp)->tv_sec = (tvp)->tv_usec = 0
5152b3787f6Schristos #endif
5162b3787f6Schristos /**@}*/
5172b3787f6Schristos 
5182b3787f6Schristos /** Return true iff the tvp is related to uvp according to the relational
5192b3787f6Schristos  * operator cmp.  Recognized values for cmp are ==, <=, <, >=, and >. */
5202b3787f6Schristos #define	evutil_timercmp(tvp, uvp, cmp)					\
5212b3787f6Schristos 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
5222b3787f6Schristos 	 ((tvp)->tv_usec cmp (uvp)->tv_usec) :				\
5232b3787f6Schristos 	 ((tvp)->tv_sec cmp (uvp)->tv_sec))
5242b3787f6Schristos 
5252b3787f6Schristos #ifdef EVENT__HAVE_TIMERISSET
5262b3787f6Schristos #define evutil_timerisset(tvp) timerisset(tvp)
5272b3787f6Schristos #else
5282b3787f6Schristos #define	evutil_timerisset(tvp)	((tvp)->tv_sec || (tvp)->tv_usec)
5292b3787f6Schristos #endif
5302b3787f6Schristos 
5312b3787f6Schristos /** Replacement for offsetof on platforms that don't define it. */
5322b3787f6Schristos #ifdef offsetof
5332b3787f6Schristos #define evutil_offsetof(type, field) offsetof(type, field)
5342b3787f6Schristos #else
5352b3787f6Schristos #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
5362b3787f6Schristos #endif
5372b3787f6Schristos 
5382b3787f6Schristos /* big-int related functions */
5392b3787f6Schristos /** Parse a 64-bit value from a string.  Arguments are as for strtol. */
5401b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
5412b3787f6Schristos ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
5422b3787f6Schristos 
5432b3787f6Schristos /** Replacement for gettimeofday on platforms that lack it. */
5442b3787f6Schristos #ifdef EVENT__HAVE_GETTIMEOFDAY
5452b3787f6Schristos #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
5462b3787f6Schristos #else
5472b3787f6Schristos struct timezone;
5481b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
5492b3787f6Schristos int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
5502b3787f6Schristos #endif
5512b3787f6Schristos 
5522b3787f6Schristos /** Replacement for snprintf to get consistent behavior on platforms for
5532b3787f6Schristos     which the return value of snprintf does not conform to C99.
5542b3787f6Schristos  */
5551b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
5562b3787f6Schristos int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
5572b3787f6Schristos #ifdef __GNUC__
5582b3787f6Schristos 	__attribute__((format(printf, 3, 4)))
5592b3787f6Schristos #endif
5602b3787f6Schristos ;
5612b3787f6Schristos /** Replacement for vsnprintf to get consistent behavior on platforms for
5622b3787f6Schristos     which the return value of snprintf does not conform to C99.
5632b3787f6Schristos  */
5641b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
5652b3787f6Schristos int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
5662b3787f6Schristos #ifdef __GNUC__
5672b3787f6Schristos 	__attribute__((format(printf, 3, 0)))
5682b3787f6Schristos #endif
5692b3787f6Schristos ;
5702b3787f6Schristos 
5712b3787f6Schristos /** Replacement for inet_ntop for platforms which lack it. */
5721b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
5732b3787f6Schristos const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
5742b3787f6Schristos /** Replacement for inet_pton for platforms which lack it. */
5751b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
5762b3787f6Schristos int evutil_inet_pton(int af, const char *src, void *dst);
5772b3787f6Schristos struct sockaddr;
5782b3787f6Schristos 
5792b3787f6Schristos /** Parse an IPv4 or IPv6 address, with optional port, from a string.
5802b3787f6Schristos 
5812b3787f6Schristos     Recognized formats are:
5822b3787f6Schristos     - [IPv6Address]:port
5832b3787f6Schristos     - [IPv6Address]
5842b3787f6Schristos     - IPv6Address
5852b3787f6Schristos     - IPv4Address:port
5862b3787f6Schristos     - IPv4Address
5872b3787f6Schristos 
5882b3787f6Schristos     If no port is specified, the port in the output is set to 0.
5892b3787f6Schristos 
5902b3787f6Schristos     @param str The string to parse.
5912b3787f6Schristos     @param out A struct sockaddr to hold the result.  This should probably be
5922b3787f6Schristos        a struct sockaddr_storage.
5932b3787f6Schristos     @param outlen A pointer to the number of bytes that that 'out' can safely
5942b3787f6Schristos        hold.  Set to the number of bytes used in 'out' on success.
5952b3787f6Schristos     @return -1 if the address is not well-formed, if the port is out of range,
5962b3787f6Schristos        or if out is not large enough to hold the result.  Otherwise returns
5972b3787f6Schristos        0 on success.
5982b3787f6Schristos */
5991b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
6002b3787f6Schristos int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
6012b3787f6Schristos 
6022b3787f6Schristos /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
6032b3787f6Schristos  * preceeds sa2, or greater than 0 if sa1 follows sa2.  If include_port is
6042b3787f6Schristos  * true, consider the port as well as the address.  Only implemented for
6052b3787f6Schristos  * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
6062b3787f6Schristos  * the same between Libevent versions. */
6071b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
6082b3787f6Schristos int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
6092b3787f6Schristos     int include_port);
6102b3787f6Schristos 
6112b3787f6Schristos /** As strcasecmp, but always compares the characters in locale-independent
6122b3787f6Schristos     ASCII.  That's useful if you're handling data in ASCII-based protocols.
6132b3787f6Schristos  */
6141b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
6152b3787f6Schristos int evutil_ascii_strcasecmp(const char *str1, const char *str2);
6162b3787f6Schristos /** As strncasecmp, but always compares the characters in locale-independent
6172b3787f6Schristos     ASCII.  That's useful if you're handling data in ASCII-based protocols.
6182b3787f6Schristos  */
6191b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
6202b3787f6Schristos int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
6212b3787f6Schristos 
6222b3787f6Schristos /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
6232b3787f6Schristos  * if this system has no getaddrinfo(). */
6242b3787f6Schristos #ifdef EVENT__HAVE_STRUCT_ADDRINFO
6252b3787f6Schristos #define evutil_addrinfo addrinfo
6262b3787f6Schristos #else
6272b3787f6Schristos /** A definition of struct addrinfo for systems that lack it.
6282b3787f6Schristos 
6292b3787f6Schristos     (This is just an alias for struct addrinfo if the system defines
6302b3787f6Schristos     struct addrinfo.)
6312b3787f6Schristos */
6322b3787f6Schristos struct evutil_addrinfo {
6332b3787f6Schristos 	int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
6342b3787f6Schristos 	int     ai_family;    /* PF_xxx */
6352b3787f6Schristos 	int     ai_socktype;  /* SOCK_xxx */
6362b3787f6Schristos 	int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
6372b3787f6Schristos 	size_t  ai_addrlen;   /* length of ai_addr */
6382b3787f6Schristos 	char   *ai_canonname; /* canonical name for nodename */
6392b3787f6Schristos 	struct sockaddr  *ai_addr; /* binary address */
6402b3787f6Schristos 	struct evutil_addrinfo  *ai_next; /* next structure in linked list */
6412b3787f6Schristos };
6422b3787f6Schristos #endif
6432b3787f6Schristos /** @name evutil_getaddrinfo() error codes
6442b3787f6Schristos 
6452b3787f6Schristos     These values are possible error codes for evutil_getaddrinfo() and
6462b3787f6Schristos     related functions.
6472b3787f6Schristos 
6482b3787f6Schristos     @{
6492b3787f6Schristos */
6502b3787f6Schristos #if defined(EAI_ADDRFAMILY) && defined(EVENT__HAVE_GETADDRINFO)
6512b3787f6Schristos #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
6522b3787f6Schristos #else
6532b3787f6Schristos #define EVUTIL_EAI_ADDRFAMILY -901
6542b3787f6Schristos #endif
6552b3787f6Schristos #if defined(EAI_AGAIN) && defined(EVENT__HAVE_GETADDRINFO)
6562b3787f6Schristos #define EVUTIL_EAI_AGAIN EAI_AGAIN
6572b3787f6Schristos #else
6582b3787f6Schristos #define EVUTIL_EAI_AGAIN -902
6592b3787f6Schristos #endif
6602b3787f6Schristos #if defined(EAI_BADFLAGS) && defined(EVENT__HAVE_GETADDRINFO)
6612b3787f6Schristos #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
6622b3787f6Schristos #else
6632b3787f6Schristos #define EVUTIL_EAI_BADFLAGS -903
6642b3787f6Schristos #endif
6652b3787f6Schristos #if defined(EAI_FAIL) && defined(EVENT__HAVE_GETADDRINFO)
6662b3787f6Schristos #define EVUTIL_EAI_FAIL EAI_FAIL
6672b3787f6Schristos #else
6682b3787f6Schristos #define EVUTIL_EAI_FAIL -904
6692b3787f6Schristos #endif
6702b3787f6Schristos #if defined(EAI_FAMILY) && defined(EVENT__HAVE_GETADDRINFO)
6712b3787f6Schristos #define EVUTIL_EAI_FAMILY EAI_FAMILY
6722b3787f6Schristos #else
6732b3787f6Schristos #define EVUTIL_EAI_FAMILY -905
6742b3787f6Schristos #endif
6752b3787f6Schristos #if defined(EAI_MEMORY) && defined(EVENT__HAVE_GETADDRINFO)
6762b3787f6Schristos #define EVUTIL_EAI_MEMORY EAI_MEMORY
6772b3787f6Schristos #else
6782b3787f6Schristos #define EVUTIL_EAI_MEMORY -906
6792b3787f6Schristos #endif
6802b3787f6Schristos /* This test is a bit complicated, since some MS SDKs decide to
6812b3787f6Schristos  * remove NODATA or redefine it to be the same as NONAME, in a
6822b3787f6Schristos  * fun interpretation of RFC 2553 and RFC 3493. */
6832b3787f6Schristos #if defined(EAI_NODATA) && defined(EVENT__HAVE_GETADDRINFO) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
6842b3787f6Schristos #define EVUTIL_EAI_NODATA EAI_NODATA
6852b3787f6Schristos #else
6862b3787f6Schristos #define EVUTIL_EAI_NODATA -907
6872b3787f6Schristos #endif
6882b3787f6Schristos #if defined(EAI_NONAME) && defined(EVENT__HAVE_GETADDRINFO)
6892b3787f6Schristos #define EVUTIL_EAI_NONAME EAI_NONAME
6902b3787f6Schristos #else
6912b3787f6Schristos #define EVUTIL_EAI_NONAME -908
6922b3787f6Schristos #endif
6932b3787f6Schristos #if defined(EAI_SERVICE) && defined(EVENT__HAVE_GETADDRINFO)
6942b3787f6Schristos #define EVUTIL_EAI_SERVICE EAI_SERVICE
6952b3787f6Schristos #else
6962b3787f6Schristos #define EVUTIL_EAI_SERVICE -909
6972b3787f6Schristos #endif
6982b3787f6Schristos #if defined(EAI_SOCKTYPE) && defined(EVENT__HAVE_GETADDRINFO)
6992b3787f6Schristos #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
7002b3787f6Schristos #else
7012b3787f6Schristos #define EVUTIL_EAI_SOCKTYPE -910
7022b3787f6Schristos #endif
7032b3787f6Schristos #if defined(EAI_SYSTEM) && defined(EVENT__HAVE_GETADDRINFO)
7042b3787f6Schristos #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
7052b3787f6Schristos #else
7062b3787f6Schristos #define EVUTIL_EAI_SYSTEM -911
7072b3787f6Schristos #endif
7082b3787f6Schristos 
7092b3787f6Schristos #define EVUTIL_EAI_CANCEL -90001
7102b3787f6Schristos 
7112b3787f6Schristos #if defined(AI_PASSIVE) && defined(EVENT__HAVE_GETADDRINFO)
7122b3787f6Schristos #define EVUTIL_AI_PASSIVE AI_PASSIVE
7132b3787f6Schristos #else
7142b3787f6Schristos #define EVUTIL_AI_PASSIVE 0x1000
7152b3787f6Schristos #endif
7162b3787f6Schristos #if defined(AI_CANONNAME) && defined(EVENT__HAVE_GETADDRINFO)
7172b3787f6Schristos #define EVUTIL_AI_CANONNAME AI_CANONNAME
7182b3787f6Schristos #else
7192b3787f6Schristos #define EVUTIL_AI_CANONNAME 0x2000
7202b3787f6Schristos #endif
7212b3787f6Schristos #if defined(AI_NUMERICHOST) && defined(EVENT__HAVE_GETADDRINFO)
7222b3787f6Schristos #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
7232b3787f6Schristos #else
7242b3787f6Schristos #define EVUTIL_AI_NUMERICHOST 0x4000
7252b3787f6Schristos #endif
7262b3787f6Schristos #if defined(AI_NUMERICSERV) && defined(EVENT__HAVE_GETADDRINFO)
7272b3787f6Schristos #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
7282b3787f6Schristos #else
7292b3787f6Schristos #define EVUTIL_AI_NUMERICSERV 0x8000
7302b3787f6Schristos #endif
7312b3787f6Schristos #if defined(AI_V4MAPPED) && defined(EVENT__HAVE_GETADDRINFO)
7322b3787f6Schristos #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
7332b3787f6Schristos #else
7342b3787f6Schristos #define EVUTIL_AI_V4MAPPED 0x10000
7352b3787f6Schristos #endif
7362b3787f6Schristos #if defined(AI_ALL) && defined(EVENT__HAVE_GETADDRINFO)
7372b3787f6Schristos #define EVUTIL_AI_ALL AI_ALL
7382b3787f6Schristos #else
7392b3787f6Schristos #define EVUTIL_AI_ALL 0x20000
7402b3787f6Schristos #endif
7412b3787f6Schristos #if defined(AI_ADDRCONFIG) && defined(EVENT__HAVE_GETADDRINFO)
7422b3787f6Schristos #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
7432b3787f6Schristos #else
7442b3787f6Schristos #define EVUTIL_AI_ADDRCONFIG 0x40000
7452b3787f6Schristos #endif
7462b3787f6Schristos /**@}*/
7472b3787f6Schristos 
7482b3787f6Schristos struct evutil_addrinfo;
7492b3787f6Schristos /**
7502b3787f6Schristos  * This function clones getaddrinfo for systems that don't have it.  For full
7512b3787f6Schristos  * details, see RFC 3493, section 6.1.
7522b3787f6Schristos  *
7532b3787f6Schristos  * Limitations:
7542b3787f6Schristos  * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
7552b3787f6Schristos  *   gethostbyname, with their attendant issues.
7562b3787f6Schristos  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
7572b3787f6Schristos  *
7582b3787f6Schristos  * For a nonblocking variant, see evdns_getaddrinfo.
7592b3787f6Schristos  */
7601b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
7612b3787f6Schristos int evutil_getaddrinfo(const char *nodename, const char *servname,
7622b3787f6Schristos     const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
7632b3787f6Schristos 
7642b3787f6Schristos /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
7651b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
7662b3787f6Schristos void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
7672b3787f6Schristos 
7681b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
7692b3787f6Schristos const char *evutil_gai_strerror(int err);
7702b3787f6Schristos 
7712b3787f6Schristos /** Generate n bytes of secure pseudorandom data, and store them in buf.
7722b3787f6Schristos  *
7731b6f2cd4Schristos  * Current versions of Libevent use an ARC4-based random number generator,
7741b6f2cd4Schristos  * seeded using the platform's entropy source (/dev/urandom on Unix-like
7751b6f2cd4Schristos  * systems; CryptGenRandom on Windows).  This is not actually as secure as it
7761b6f2cd4Schristos  * should be: ARC4 is a pretty lousy cipher, and the current implementation
7771b6f2cd4Schristos  * provides only rudimentary prediction- and backtracking-resistance.  Don't
7781b6f2cd4Schristos  * use this for serious cryptographic applications.
7792b3787f6Schristos  */
7801b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
7812b3787f6Schristos void evutil_secure_rng_get_bytes(void *buf, size_t n);
7822b3787f6Schristos 
7832b3787f6Schristos /**
7842b3787f6Schristos  * Seed the secure random number generator if needed, and return 0 on
7852b3787f6Schristos  * success or -1 on failure.
7862b3787f6Schristos  *
7872b3787f6Schristos  * It is okay to call this function more than once; it will still return
7882b3787f6Schristos  * 0 if the RNG has been successfully seeded and -1 if it can't be
7892b3787f6Schristos  * seeded.
7902b3787f6Schristos  *
7912b3787f6Schristos  * Ordinarily you don't need to call this function from your own code;
7922b3787f6Schristos  * Libevent will seed the RNG itself the first time it needs good random
7932b3787f6Schristos  * numbers.  You only need to call it if (a) you want to double-check
7942b3787f6Schristos  * that one of the seeding methods did succeed, or (b) you plan to drop
7952b3787f6Schristos  * the capability to seed (by chrooting, or dropping capabilities, or
7962b3787f6Schristos  * whatever), and you want to make sure that seeding happens before your
7972b3787f6Schristos  * program loses the ability to do it.
7982b3787f6Schristos  */
7991b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
8002b3787f6Schristos int evutil_secure_rng_init(void);
8012b3787f6Schristos 
8021b6f2cd4Schristos /**
8031b6f2cd4Schristos  * Set a filename to use in place of /dev/urandom for seeding the secure
8041b6f2cd4Schristos  * PRNG. Return 0 on success, -1 on failure.
8051b6f2cd4Schristos  *
8061b6f2cd4Schristos  * Call this function BEFORE calling any other initialization or RNG
8071b6f2cd4Schristos  * functions.
8081b6f2cd4Schristos  *
8091b6f2cd4Schristos  * (This string will _NOT_ be copied internally. Do not free it while any
8101b6f2cd4Schristos  * user of the secure RNG might be running. Don't pass anything other than a
8111b6f2cd4Schristos  * real /dev/...random device file here, or you might lose security.)
8121b6f2cd4Schristos  *
8131b6f2cd4Schristos  * This API is unstable, and might change in a future libevent version.
8141b6f2cd4Schristos  */
8151b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
8161b6f2cd4Schristos int evutil_secure_rng_set_urandom_device_file(char *fname);
8171b6f2cd4Schristos 
8182b3787f6Schristos /** Seed the random number generator with extra random bytes.
8192b3787f6Schristos 
8202b3787f6Schristos     You should almost never need to call this function; it should be
8212b3787f6Schristos     sufficient to invoke evutil_secure_rng_init(), or let Libevent take
8222b3787f6Schristos     care of calling evutil_secure_rng_init() on its own.
8232b3787f6Schristos 
8242b3787f6Schristos     If you call this function as a _replacement_ for the regular
8252b3787f6Schristos     entropy sources, then you need to be sure that your input
8262b3787f6Schristos     contains a fairly large amount of strong entropy.  Doing so is
8272b3787f6Schristos     notoriously hard: most people who try get it wrong.  Watch out!
8282b3787f6Schristos 
8292b3787f6Schristos     @param dat a buffer full of a strong source of random numbers
8302b3787f6Schristos     @param datlen the number of bytes to read from datlen
8312b3787f6Schristos  */
8321b6f2cd4Schristos EVENT2_EXPORT_SYMBOL
8332b3787f6Schristos void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
8342b3787f6Schristos 
8352b3787f6Schristos #ifdef __cplusplus
8362b3787f6Schristos }
8372b3787f6Schristos #endif
8382b3787f6Schristos 
8392b3787f6Schristos #endif /* EVENT1_EVUTIL_H_INCLUDED_ */
840