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