xref: /freebsd-src/contrib/libevent/util-internal.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 UTIL_INTERNAL_H_INCLUDED_
27c43e99fdSEd Maste #define UTIL_INTERNAL_H_INCLUDED_
28c43e99fdSEd Maste 
29c43e99fdSEd Maste #include "event2/event-config.h"
30c43e99fdSEd Maste #include "evconfig-private.h"
31c43e99fdSEd Maste 
32c43e99fdSEd Maste #include <errno.h>
33c43e99fdSEd Maste 
34c43e99fdSEd Maste /* For EVUTIL_ASSERT */
35c43e99fdSEd Maste #include "log-internal.h"
36c43e99fdSEd Maste #include <stdio.h>
37c43e99fdSEd Maste #include <stdlib.h>
38c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_SOCKET_H
39c43e99fdSEd Maste #include <sys/socket.h>
40c43e99fdSEd Maste #endif
41c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_EVENTFD_H
42c43e99fdSEd Maste #include <sys/eventfd.h>
43c43e99fdSEd Maste #endif
44c43e99fdSEd Maste #include "event2/util.h"
45c43e99fdSEd Maste 
46c43e99fdSEd Maste #include "time-internal.h"
47c43e99fdSEd Maste #include "ipv6-internal.h"
48c43e99fdSEd Maste 
49c43e99fdSEd Maste #ifdef __cplusplus
50c43e99fdSEd Maste extern "C" {
51c43e99fdSEd Maste #endif
52c43e99fdSEd Maste 
53*b50261e2SCy Schubert /* __has_attribute() wrapper */
54*b50261e2SCy Schubert #ifdef __has_attribute
55*b50261e2SCy Schubert # define EVUTIL_HAS_ATTRIBUTE __has_attribute
56*b50261e2SCy Schubert #endif
57*b50261e2SCy Schubert /** clang 3 __has_attribute misbehaves in some versions */
58*b50261e2SCy Schubert #if defined(__clang__) && __clang__ == 1
59*b50261e2SCy Schubert # if defined(__apple_build_version__)
60*b50261e2SCy Schubert #  if __clang_major__ <= 6
61*b50261e2SCy Schubert #   undef EVUTIL_HAS_ATTRIBUTE
62*b50261e2SCy Schubert #  endif
63*b50261e2SCy Schubert # else /* !__apple_build_version__ */
64*b50261e2SCy Schubert #  if __clang_major__ == 3 && __clang_minor__ >= 2 && __clang_minor__ <= 5
65*b50261e2SCy Schubert #   undef EVUTIL_HAS_ATTRIBUTE
66*b50261e2SCy Schubert #  endif
67*b50261e2SCy Schubert # endif /* __apple_build_version__ */
68*b50261e2SCy Schubert #endif /*\ defined(__clang__) && __clang__ == 1 */
69*b50261e2SCy Schubert #ifndef EVUTIL_HAS_ATTRIBUTE
70*b50261e2SCy Schubert # define EVUTIL_HAS_ATTRIBUTE(x) 0
71*b50261e2SCy Schubert #endif
72*b50261e2SCy Schubert 
73c43e99fdSEd Maste /* If we need magic to say "inline", get it for free internally. */
74c43e99fdSEd Maste #ifdef EVENT__inline
75c43e99fdSEd Maste #define inline EVENT__inline
76c43e99fdSEd Maste #endif
77*b50261e2SCy Schubert 
78*b50261e2SCy Schubert /* Define to appropriate substitute if compiler doesnt have __func__ */
79*b50261e2SCy Schubert #if defined(EVENT__HAVE___func__)
80*b50261e2SCy Schubert # ifndef __func__
81*b50261e2SCy Schubert #  define __func__ __func__
82*b50261e2SCy Schubert # endif
83*b50261e2SCy Schubert #elif defined(EVENT__HAVE___FUNCTION__)
84*b50261e2SCy Schubert # define __func__ __FUNCTION__
85*b50261e2SCy Schubert #else
86*b50261e2SCy Schubert # define __func__ __FILE__
87c43e99fdSEd Maste #endif
88c43e99fdSEd Maste 
89c43e99fdSEd Maste /* A good no-op to use in macro definitions. */
90c43e99fdSEd Maste #define EVUTIL_NIL_STMT_ ((void)0)
91c43e99fdSEd Maste /* A no-op that tricks the compiler into thinking a condition is used while
92c43e99fdSEd Maste  * definitely not making any code for it.  Used to compile out asserts while
93c43e99fdSEd Maste  * avoiding "unused variable" warnings.  The "!" forces the compiler to
94c43e99fdSEd Maste  * do the sizeof() on an int, in case "condition" is a bitfield value.
95c43e99fdSEd Maste  */
96c43e99fdSEd Maste #define EVUTIL_NIL_CONDITION_(condition) do { \
97c43e99fdSEd Maste 	(void)sizeof(!(condition));  \
98c43e99fdSEd Maste } while(0)
99c43e99fdSEd Maste 
100c43e99fdSEd Maste /* Internal use only: macros to match patterns of error codes in a
101c43e99fdSEd Maste    cross-platform way.  We need these macros because of two historical
102c43e99fdSEd Maste    reasons: first, nonblocking IO functions are generally written to give an
103c43e99fdSEd Maste    error on the "blocked now, try later" case, so sometimes an error from a
104c43e99fdSEd Maste    read, write, connect, or accept means "no error; just wait for more
105c43e99fdSEd Maste    data," and we need to look at the error code.  Second, Windows defines
106c43e99fdSEd Maste    a different set of error codes for sockets. */
107c43e99fdSEd Maste 
108c43e99fdSEd Maste #ifndef _WIN32
109c43e99fdSEd Maste 
110c43e99fdSEd Maste #if EAGAIN == EWOULDBLOCK
111c43e99fdSEd Maste #define EVUTIL_ERR_IS_EAGAIN(e) \
112c43e99fdSEd Maste 	((e) == EAGAIN)
113c43e99fdSEd Maste #else
114c43e99fdSEd Maste #define EVUTIL_ERR_IS_EAGAIN(e) \
115c43e99fdSEd Maste 	((e) == EAGAIN || (e) == EWOULDBLOCK)
116c43e99fdSEd Maste #endif
117c43e99fdSEd Maste 
118c43e99fdSEd Maste /* True iff e is an error that means a read/write operation can be retried. */
119c43e99fdSEd Maste #define EVUTIL_ERR_RW_RETRIABLE(e)				\
120c43e99fdSEd Maste 	((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e))
121c43e99fdSEd Maste /* True iff e is an error that means an connect can be retried. */
122c43e99fdSEd Maste #define EVUTIL_ERR_CONNECT_RETRIABLE(e)			\
123c43e99fdSEd Maste 	((e) == EINTR || (e) == EINPROGRESS)
124c43e99fdSEd Maste /* True iff e is an error that means a accept can be retried. */
125c43e99fdSEd Maste #define EVUTIL_ERR_ACCEPT_RETRIABLE(e)			\
126c43e99fdSEd Maste 	((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e) || (e) == ECONNABORTED)
127c43e99fdSEd Maste 
128c43e99fdSEd Maste /* True iff e is an error that means the connection was refused */
129c43e99fdSEd Maste #define EVUTIL_ERR_CONNECT_REFUSED(e)					\
130c43e99fdSEd Maste 	((e) == ECONNREFUSED)
131c43e99fdSEd Maste 
132c43e99fdSEd Maste #else
133c43e99fdSEd Maste /* Win32 */
134c43e99fdSEd Maste 
135c43e99fdSEd Maste #define EVUTIL_ERR_IS_EAGAIN(e) \
136c43e99fdSEd Maste 	((e) == WSAEWOULDBLOCK || (e) == EAGAIN)
137c43e99fdSEd Maste 
138c43e99fdSEd Maste #define EVUTIL_ERR_RW_RETRIABLE(e)					\
139c43e99fdSEd Maste 	((e) == WSAEWOULDBLOCK ||					\
140c43e99fdSEd Maste 	    (e) == WSAEINTR)
141c43e99fdSEd Maste 
142c43e99fdSEd Maste #define EVUTIL_ERR_CONNECT_RETRIABLE(e)					\
143c43e99fdSEd Maste 	((e) == WSAEWOULDBLOCK ||					\
144c43e99fdSEd Maste 	    (e) == WSAEINTR ||						\
145c43e99fdSEd Maste 	    (e) == WSAEINPROGRESS ||					\
146c43e99fdSEd Maste 	    (e) == WSAEINVAL)
147c43e99fdSEd Maste 
148c43e99fdSEd Maste #define EVUTIL_ERR_ACCEPT_RETRIABLE(e)			\
149c43e99fdSEd Maste 	EVUTIL_ERR_RW_RETRIABLE(e)
150c43e99fdSEd Maste 
151c43e99fdSEd Maste #define EVUTIL_ERR_CONNECT_REFUSED(e)					\
152c43e99fdSEd Maste 	((e) == WSAECONNREFUSED)
153c43e99fdSEd Maste 
154c43e99fdSEd Maste #endif
155c43e99fdSEd Maste 
156c43e99fdSEd Maste /* Arguments for shutdown() */
157c43e99fdSEd Maste #ifdef SHUT_RD
158c43e99fdSEd Maste #define EVUTIL_SHUT_RD SHUT_RD
159c43e99fdSEd Maste #else
160c43e99fdSEd Maste #define EVUTIL_SHUT_RD 0
161c43e99fdSEd Maste #endif
162c43e99fdSEd Maste #ifdef SHUT_WR
163c43e99fdSEd Maste #define EVUTIL_SHUT_WR SHUT_WR
164c43e99fdSEd Maste #else
165c43e99fdSEd Maste #define EVUTIL_SHUT_WR 1 /* SD_SEND */
166c43e99fdSEd Maste #endif
167c43e99fdSEd Maste #ifdef SHUT_BOTH
168c43e99fdSEd Maste #define EVUTIL_SHUT_BOTH SHUT_BOTH
169c43e99fdSEd Maste #else
170c43e99fdSEd Maste #define EVUTIL_SHUT_BOTH 2
171c43e99fdSEd Maste #endif
172c43e99fdSEd Maste 
173c43e99fdSEd Maste /* Helper: Verify that all the elements in 'dlist' are internally consistent.
174c43e99fdSEd Maste  * Checks for circular lists and bad prev/next pointers.
175c43e99fdSEd Maste  *
176c43e99fdSEd Maste  * Example usage:
177c43e99fdSEd Maste  *    EVUTIL_ASSERT_LIST_OK(eventlist, event, ev_next);
178c43e99fdSEd Maste  */
179c43e99fdSEd Maste #define EVUTIL_ASSERT_LIST_OK(dlist, type, field) do {			\
180c43e99fdSEd Maste 		struct type *elm1, *elm2, **nextp;			\
181c43e99fdSEd Maste 		if (LIST_EMPTY((dlist)))				\
182c43e99fdSEd Maste 			break;						\
183c43e99fdSEd Maste 									\
184c43e99fdSEd Maste 		/* Check list for circularity using Floyd's */		\
185c43e99fdSEd Maste 		/* 'Tortoise and Hare' algorithm */			\
186c43e99fdSEd Maste 		elm1 = LIST_FIRST((dlist));				\
187c43e99fdSEd Maste 		elm2 = LIST_NEXT(elm1, field);				\
188c43e99fdSEd Maste 		while (elm1 && elm2) {					\
189c43e99fdSEd Maste 			EVUTIL_ASSERT(elm1 != elm2);			\
190c43e99fdSEd Maste 			elm1 = LIST_NEXT(elm1, field);			\
191c43e99fdSEd Maste 			elm2 = LIST_NEXT(elm2, field);			\
192c43e99fdSEd Maste 			if (!elm2)					\
193c43e99fdSEd Maste 				break;					\
194c43e99fdSEd Maste 			EVUTIL_ASSERT(elm1 != elm2);			\
195c43e99fdSEd Maste 			elm2 = LIST_NEXT(elm2, field);			\
196c43e99fdSEd Maste 		}							\
197c43e99fdSEd Maste 									\
198c43e99fdSEd Maste 		/* Now check next and prev pointers for consistency. */ \
199c43e99fdSEd Maste 		nextp = &LIST_FIRST((dlist));				\
200c43e99fdSEd Maste 		elm1 = LIST_FIRST((dlist));				\
201c43e99fdSEd Maste 		while (elm1) {						\
202c43e99fdSEd Maste 			EVUTIL_ASSERT(*nextp == elm1);			\
203c43e99fdSEd Maste 			EVUTIL_ASSERT(nextp == elm1->field.le_prev);	\
204c43e99fdSEd Maste 			nextp = &LIST_NEXT(elm1, field);		\
205c43e99fdSEd Maste 			elm1 = *nextp;					\
206c43e99fdSEd Maste 		}							\
207c43e99fdSEd Maste 	} while (0)
208c43e99fdSEd Maste 
209c43e99fdSEd Maste /* Helper: Verify that all the elements in a TAILQ are internally consistent.
210c43e99fdSEd Maste  * Checks for circular lists and bad prev/next pointers.
211c43e99fdSEd Maste  *
212c43e99fdSEd Maste  * Example usage:
213c43e99fdSEd Maste  *    EVUTIL_ASSERT_TAILQ_OK(activelist, event, ev_active_next);
214c43e99fdSEd Maste  */
215c43e99fdSEd Maste #define EVUTIL_ASSERT_TAILQ_OK(tailq, type, field) do {			\
216c43e99fdSEd Maste 		struct type *elm1, *elm2, **nextp;			\
217c43e99fdSEd Maste 		if (TAILQ_EMPTY((tailq)))				\
218c43e99fdSEd Maste 			break;						\
219c43e99fdSEd Maste 									\
220c43e99fdSEd Maste 		/* Check list for circularity using Floyd's */		\
221c43e99fdSEd Maste 		/* 'Tortoise and Hare' algorithm */			\
222c43e99fdSEd Maste 		elm1 = TAILQ_FIRST((tailq));				\
223c43e99fdSEd Maste 		elm2 = TAILQ_NEXT(elm1, field);				\
224c43e99fdSEd Maste 		while (elm1 && elm2) {					\
225c43e99fdSEd Maste 			EVUTIL_ASSERT(elm1 != elm2);			\
226c43e99fdSEd Maste 			elm1 = TAILQ_NEXT(elm1, field);			\
227c43e99fdSEd Maste 			elm2 = TAILQ_NEXT(elm2, field);			\
228c43e99fdSEd Maste 			if (!elm2)					\
229c43e99fdSEd Maste 				break;					\
230c43e99fdSEd Maste 			EVUTIL_ASSERT(elm1 != elm2);			\
231c43e99fdSEd Maste 			elm2 = TAILQ_NEXT(elm2, field);			\
232c43e99fdSEd Maste 		}							\
233c43e99fdSEd Maste 									\
234c43e99fdSEd Maste 		/* Now check next and prev pointers for consistency. */ \
235c43e99fdSEd Maste 		nextp = &TAILQ_FIRST((tailq));				\
236c43e99fdSEd Maste 		elm1 = TAILQ_FIRST((tailq));				\
237c43e99fdSEd Maste 		while (elm1) {						\
238c43e99fdSEd Maste 			EVUTIL_ASSERT(*nextp == elm1);			\
239c43e99fdSEd Maste 			EVUTIL_ASSERT(nextp == elm1->field.tqe_prev);	\
240c43e99fdSEd Maste 			nextp = &TAILQ_NEXT(elm1, field);		\
241c43e99fdSEd Maste 			elm1 = *nextp;					\
242c43e99fdSEd Maste 		}							\
243c43e99fdSEd Maste 		EVUTIL_ASSERT(nextp == (tailq)->tqh_last);		\
244c43e99fdSEd Maste 	} while (0)
245c43e99fdSEd Maste 
246c43e99fdSEd Maste /* Locale-independent replacements for some ctypes functions.  Use these
247c43e99fdSEd Maste  * when you care about ASCII's notion of character types, because you are about
248c43e99fdSEd Maste  * to send those types onto the wire.
249c43e99fdSEd Maste  */
250*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
251c43e99fdSEd Maste int EVUTIL_ISALPHA_(char c);
252*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
253c43e99fdSEd Maste int EVUTIL_ISALNUM_(char c);
254c43e99fdSEd Maste int EVUTIL_ISSPACE_(char c);
255*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
256c43e99fdSEd Maste int EVUTIL_ISDIGIT_(char c);
257*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
258c43e99fdSEd Maste int EVUTIL_ISXDIGIT_(char c);
259c43e99fdSEd Maste int EVUTIL_ISPRINT_(char c);
260c43e99fdSEd Maste int EVUTIL_ISLOWER_(char c);
261c43e99fdSEd Maste int EVUTIL_ISUPPER_(char c);
262*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
263c43e99fdSEd Maste char EVUTIL_TOUPPER_(char c);
264*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
265c43e99fdSEd Maste char EVUTIL_TOLOWER_(char c);
266c43e99fdSEd Maste 
267c43e99fdSEd Maste /** Remove all trailing horizontal whitespace (space or tab) from the end of a
268c43e99fdSEd Maste  * string */
269*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
270c43e99fdSEd Maste void evutil_rtrim_lws_(char *);
271c43e99fdSEd Maste 
272c43e99fdSEd Maste 
273c43e99fdSEd Maste /** Helper macro.  If we know that a given pointer points to a field in a
274c43e99fdSEd Maste     structure, return a pointer to the structure itself.  Used to implement
275c43e99fdSEd Maste     our half-baked C OO.  Example:
276c43e99fdSEd Maste 
277c43e99fdSEd Maste     struct subtype {
278c43e99fdSEd Maste 	int x;
279c43e99fdSEd Maste 	struct supertype common;
280c43e99fdSEd Maste 	int y;
281c43e99fdSEd Maste     };
282c43e99fdSEd Maste     ...
283c43e99fdSEd Maste     void fn(struct supertype *super) {
284c43e99fdSEd Maste 	struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common);
285c43e99fdSEd Maste 	...
286c43e99fdSEd Maste     }
287c43e99fdSEd Maste  */
288c43e99fdSEd Maste #define EVUTIL_UPCAST(ptr, type, field)				\
289c43e99fdSEd Maste 	((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
290c43e99fdSEd Maste 
291c43e99fdSEd Maste /* As open(pathname, flags, mode), except that the file is always opened with
292c43e99fdSEd Maste  * the close-on-exec flag set. (And the mode argument is mandatory.)
293c43e99fdSEd Maste  */
294c43e99fdSEd Maste int evutil_open_closeonexec_(const char *pathname, int flags, unsigned mode);
295c43e99fdSEd Maste 
296*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
297c43e99fdSEd Maste int evutil_read_file_(const char *filename, char **content_out, size_t *len_out,
298c43e99fdSEd Maste     int is_binary);
299c43e99fdSEd Maste 
300*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
301c43e99fdSEd Maste int evutil_socket_connect_(evutil_socket_t *fd_ptr, const struct sockaddr *sa, int socklen);
302c43e99fdSEd Maste 
303c43e99fdSEd Maste int evutil_socket_finished_connecting_(evutil_socket_t fd);
304c43e99fdSEd Maste 
305*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
306c43e99fdSEd Maste int evutil_ersatz_socketpair_(int, int , int, evutil_socket_t[]);
307c43e99fdSEd Maste 
308c43e99fdSEd Maste int evutil_resolve_(int family, const char *hostname, struct sockaddr *sa,
309c43e99fdSEd Maste     ev_socklen_t *socklen, int port);
310c43e99fdSEd Maste 
311c43e99fdSEd Maste const char *evutil_getenv_(const char *name);
312c43e99fdSEd Maste 
313c43e99fdSEd Maste /* Structure to hold the state of our weak random number generator.
314c43e99fdSEd Maste  */
315c43e99fdSEd Maste struct evutil_weakrand_state {
316c43e99fdSEd Maste 	ev_uint32_t seed;
317c43e99fdSEd Maste };
318c43e99fdSEd Maste 
319c43e99fdSEd Maste #define EVUTIL_WEAKRAND_MAX EV_INT32_MAX
320c43e99fdSEd Maste 
321c43e99fdSEd Maste /* Initialize the state of a week random number generator based on 'seed'.  If
322c43e99fdSEd Maste  * the seed is 0, construct a new seed based on not-very-strong platform
323c43e99fdSEd Maste  * entropy, like the PID and the time of day.
324c43e99fdSEd Maste  *
325c43e99fdSEd Maste  * This function, and the other evutil_weakrand* functions, are meant for
326c43e99fdSEd Maste  * speed, not security or statistical strength.  If you need a RNG which an
327c43e99fdSEd Maste  * attacker can't predict, or which passes strong statistical tests, use the
328c43e99fdSEd Maste  * evutil_secure_rng* functions instead.
329c43e99fdSEd Maste  */
330*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
331c43e99fdSEd Maste ev_uint32_t evutil_weakrand_seed_(struct evutil_weakrand_state *state, ev_uint32_t seed);
332c43e99fdSEd Maste /* Return a pseudorandom value between 0 and EVUTIL_WEAKRAND_MAX inclusive.
333c43e99fdSEd Maste  * Updates the state in 'seed' as needed -- this value must be protected by a
334c43e99fdSEd Maste  * lock.
335c43e99fdSEd Maste  */
336*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
337c43e99fdSEd Maste ev_int32_t evutil_weakrand_(struct evutil_weakrand_state *seed);
338c43e99fdSEd Maste /* Return a pseudorandom value x such that 0 <= x < top. top must be no more
339c43e99fdSEd Maste  * than EVUTIL_WEAKRAND_MAX. Updates the state in 'seed' as needed -- this
340c43e99fdSEd Maste  * value must be proteced by a lock */
341*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
342c43e99fdSEd Maste ev_int32_t evutil_weakrand_range_(struct evutil_weakrand_state *seed, ev_int32_t top);
343c43e99fdSEd Maste 
344c43e99fdSEd Maste /* Evaluates to the same boolean value as 'p', and hints to the compiler that
345c43e99fdSEd Maste  * we expect this value to be false. */
346c43e99fdSEd Maste #if defined(__GNUC__) && __GNUC__ >= 3         /* gcc 3.0 or later */
347c43e99fdSEd Maste #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
348c43e99fdSEd Maste #else
349c43e99fdSEd Maste #define EVUTIL_UNLIKELY(p) (p)
350c43e99fdSEd Maste #endif
351c43e99fdSEd Maste 
352*b50261e2SCy Schubert #if EVUTIL_HAS_ATTRIBUTE(fallthrough)
353*b50261e2SCy Schubert #define EVUTIL_FALLTHROUGH __attribute__((fallthrough))
354*b50261e2SCy Schubert #else
355*b50261e2SCy Schubert #define EVUTIL_FALLTHROUGH /* fallthrough */
356*b50261e2SCy Schubert #endif
357*b50261e2SCy Schubert 
358c43e99fdSEd Maste /* Replacement for assert() that calls event_errx on failure. */
359c43e99fdSEd Maste #ifdef NDEBUG
360c43e99fdSEd Maste #define EVUTIL_ASSERT(cond) EVUTIL_NIL_CONDITION_(cond)
361c43e99fdSEd Maste #define EVUTIL_FAILURE_CHECK(cond) 0
362c43e99fdSEd Maste #else
363c43e99fdSEd Maste #define EVUTIL_ASSERT(cond)						\
364c43e99fdSEd Maste 	do {								\
365c43e99fdSEd Maste 		if (EVUTIL_UNLIKELY(!(cond))) {				\
366c43e99fdSEd Maste 			event_errx(EVENT_ERR_ABORT_,			\
367c43e99fdSEd Maste 			    "%s:%d: Assertion %s failed in %s",		\
368c43e99fdSEd Maste 			    __FILE__,__LINE__,#cond,__func__);		\
369c43e99fdSEd Maste 			/* In case a user-supplied handler tries to */	\
370c43e99fdSEd Maste 			/* return control to us, log and abort here. */	\
371c43e99fdSEd Maste 			(void)fprintf(stderr,				\
372c43e99fdSEd Maste 			    "%s:%d: Assertion %s failed in %s",		\
373c43e99fdSEd Maste 			    __FILE__,__LINE__,#cond,__func__);		\
374c43e99fdSEd Maste 			abort();					\
375c43e99fdSEd Maste 		}							\
376c43e99fdSEd Maste 	} while (0)
377c43e99fdSEd Maste #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
378c43e99fdSEd Maste #endif
379c43e99fdSEd Maste 
380c43e99fdSEd Maste #ifndef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE
381c43e99fdSEd Maste /* Replacement for sockaddr storage that we can use internally on platforms
382c43e99fdSEd Maste  * that lack it.  It is not space-efficient, but neither is sockaddr_storage.
383c43e99fdSEd Maste  */
384c43e99fdSEd Maste struct sockaddr_storage {
385c43e99fdSEd Maste 	union {
386c43e99fdSEd Maste 		struct sockaddr ss_sa;
387c43e99fdSEd Maste 		struct sockaddr_in ss_sin;
388c43e99fdSEd Maste 		struct sockaddr_in6 ss_sin6;
389c43e99fdSEd Maste 		char ss_padding[128];
390c43e99fdSEd Maste 	} ss_union;
391c43e99fdSEd Maste };
392c43e99fdSEd Maste #define ss_family ss_union.ss_sa.sa_family
393c43e99fdSEd Maste #endif
394c43e99fdSEd Maste 
395c43e99fdSEd Maste /* Internal addrinfo error code.  This one is returned from only from
396c43e99fdSEd Maste  * evutil_getaddrinfo_common_, when we are sure that we'll have to hit a DNS
397c43e99fdSEd Maste  * server. */
398c43e99fdSEd Maste #define EVUTIL_EAI_NEED_RESOLVE      -90002
399c43e99fdSEd Maste 
400c43e99fdSEd Maste struct evdns_base;
401c43e99fdSEd Maste struct evdns_getaddrinfo_request;
402c43e99fdSEd Maste typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
403c43e99fdSEd Maste     struct evdns_base *base,
404c43e99fdSEd Maste     const char *nodename, const char *servname,
405c43e99fdSEd Maste     const struct evutil_addrinfo *hints_in,
406c43e99fdSEd Maste     void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
407*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
408c43e99fdSEd Maste void evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn);
409c43e99fdSEd Maste typedef void (*evdns_getaddrinfo_cancel_fn)(
410c43e99fdSEd Maste     struct evdns_getaddrinfo_request *req);
411*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
412c43e99fdSEd Maste void evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel_fn fn);
413c43e99fdSEd Maste 
414*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
415c43e99fdSEd Maste struct evutil_addrinfo *evutil_new_addrinfo_(struct sockaddr *sa,
416c43e99fdSEd Maste     ev_socklen_t socklen, const struct evutil_addrinfo *hints);
417*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
418c43e99fdSEd Maste struct evutil_addrinfo *evutil_addrinfo_append_(struct evutil_addrinfo *first,
419c43e99fdSEd Maste     struct evutil_addrinfo *append);
420*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
421c43e99fdSEd Maste void evutil_adjust_hints_for_addrconfig_(struct evutil_addrinfo *hints);
422*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
423c43e99fdSEd Maste int evutil_getaddrinfo_common_(const char *nodename, const char *servname,
424c43e99fdSEd Maste     struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
425c43e99fdSEd Maste 
426c43e99fdSEd Maste struct evdns_getaddrinfo_request *evutil_getaddrinfo_async_(
427c43e99fdSEd Maste     struct evdns_base *dns_base,
428c43e99fdSEd Maste     const char *nodename, const char *servname,
429c43e99fdSEd Maste     const struct evutil_addrinfo *hints_in,
430c43e99fdSEd Maste     void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
431c43e99fdSEd Maste void evutil_getaddrinfo_cancel_async_(struct evdns_getaddrinfo_request *data);
432c43e99fdSEd Maste 
433c43e99fdSEd Maste /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
434c43e99fdSEd Maste  * ::1). */
435*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
436c43e99fdSEd Maste int evutil_sockaddr_is_loopback_(const struct sockaddr *sa);
437c43e99fdSEd Maste 
438c43e99fdSEd Maste 
439c43e99fdSEd Maste /**
440c43e99fdSEd Maste     Formats a sockaddr sa into a string buffer of size outlen stored in out.
441c43e99fdSEd Maste     Returns a pointer to out.  Always writes something into out, so it's safe
442c43e99fdSEd Maste     to use the output of this function without checking it for NULL.
443c43e99fdSEd Maste  */
444*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
445c43e99fdSEd Maste const char *evutil_format_sockaddr_port_(const struct sockaddr *sa, char *out, size_t outlen);
446c43e99fdSEd Maste 
447c43e99fdSEd Maste int evutil_hex_char_to_int_(char c);
448c43e99fdSEd Maste 
449c43e99fdSEd Maste 
450c43e99fdSEd Maste void evutil_free_secure_rng_globals_(void);
451c43e99fdSEd Maste void evutil_free_globals_(void);
452c43e99fdSEd Maste 
453c43e99fdSEd Maste #ifdef _WIN32
454*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
455c43e99fdSEd Maste HMODULE evutil_load_windows_system_library_(const TCHAR *library_name);
456c43e99fdSEd Maste #endif
457c43e99fdSEd Maste 
458c43e99fdSEd Maste #ifndef EV_SIZE_FMT
459c43e99fdSEd Maste #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
460c43e99fdSEd Maste #define EV_U64_FMT "%I64u"
461c43e99fdSEd Maste #define EV_I64_FMT "%I64d"
462c43e99fdSEd Maste #define EV_I64_ARG(x) ((__int64)(x))
463c43e99fdSEd Maste #define EV_U64_ARG(x) ((unsigned __int64)(x))
464c43e99fdSEd Maste #else
465c43e99fdSEd Maste #define EV_U64_FMT "%llu"
466c43e99fdSEd Maste #define EV_I64_FMT "%lld"
467c43e99fdSEd Maste #define EV_I64_ARG(x) ((long long)(x))
468c43e99fdSEd Maste #define EV_U64_ARG(x) ((unsigned long long)(x))
469c43e99fdSEd Maste #endif
470c43e99fdSEd Maste #endif
471c43e99fdSEd Maste 
472c43e99fdSEd Maste #ifdef _WIN32
473c43e99fdSEd Maste #define EV_SOCK_FMT EV_I64_FMT
474c43e99fdSEd Maste #define EV_SOCK_ARG(x) EV_I64_ARG((x))
475c43e99fdSEd Maste #else
476c43e99fdSEd Maste #define EV_SOCK_FMT "%d"
477c43e99fdSEd Maste #define EV_SOCK_ARG(x) (x)
478c43e99fdSEd Maste #endif
479c43e99fdSEd Maste 
480c43e99fdSEd Maste #if defined(__STDC__) && defined(__STDC_VERSION__) && !defined(__MINGW64_VERSION_MAJOR)
481c43e99fdSEd Maste #if (__STDC_VERSION__ >= 199901L)
482c43e99fdSEd Maste #define EV_SIZE_FMT "%zu"
483c43e99fdSEd Maste #define EV_SSIZE_FMT "%zd"
484c43e99fdSEd Maste #define EV_SIZE_ARG(x) (x)
485c43e99fdSEd Maste #define EV_SSIZE_ARG(x) (x)
486c43e99fdSEd Maste #endif
487c43e99fdSEd Maste #endif
488c43e99fdSEd Maste 
489c43e99fdSEd Maste #ifndef EV_SIZE_FMT
490c43e99fdSEd Maste #if (EVENT__SIZEOF_SIZE_T <= EVENT__SIZEOF_LONG)
491c43e99fdSEd Maste #define EV_SIZE_FMT "%lu"
492c43e99fdSEd Maste #define EV_SSIZE_FMT "%ld"
493c43e99fdSEd Maste #define EV_SIZE_ARG(x) ((unsigned long)(x))
494c43e99fdSEd Maste #define EV_SSIZE_ARG(x) ((long)(x))
495c43e99fdSEd Maste #else
496c43e99fdSEd Maste #define EV_SIZE_FMT EV_U64_FMT
497c43e99fdSEd Maste #define EV_SSIZE_FMT EV_I64_FMT
498c43e99fdSEd Maste #define EV_SIZE_ARG(x) EV_U64_ARG(x)
499c43e99fdSEd Maste #define EV_SSIZE_ARG(x) EV_I64_ARG(x)
500c43e99fdSEd Maste #endif
501c43e99fdSEd Maste #endif
502c43e99fdSEd Maste 
503*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
504c43e99fdSEd Maste evutil_socket_t evutil_socket_(int domain, int type, int protocol);
505c43e99fdSEd Maste evutil_socket_t evutil_accept4_(evutil_socket_t sockfd, struct sockaddr *addr,
506c43e99fdSEd Maste     ev_socklen_t *addrlen, int flags);
507c43e99fdSEd Maste 
508c43e99fdSEd Maste     /* used by one of the test programs.. */
509c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL
510c43e99fdSEd Maste int evutil_make_internal_pipe_(evutil_socket_t fd[2]);
511c43e99fdSEd Maste evutil_socket_t evutil_eventfd_(unsigned initval, int flags);
512c43e99fdSEd Maste 
513c43e99fdSEd Maste #ifdef SOCK_NONBLOCK
514c43e99fdSEd Maste #define EVUTIL_SOCK_NONBLOCK SOCK_NONBLOCK
515c43e99fdSEd Maste #else
516c43e99fdSEd Maste #define EVUTIL_SOCK_NONBLOCK 0x4000000
517c43e99fdSEd Maste #endif
518c43e99fdSEd Maste #ifdef SOCK_CLOEXEC
519c43e99fdSEd Maste #define EVUTIL_SOCK_CLOEXEC SOCK_CLOEXEC
520c43e99fdSEd Maste #else
521c43e99fdSEd Maste #define EVUTIL_SOCK_CLOEXEC 0x80000000
522c43e99fdSEd Maste #endif
523c43e99fdSEd Maste #ifdef EFD_NONBLOCK
524c43e99fdSEd Maste #define EVUTIL_EFD_NONBLOCK EFD_NONBLOCK
525c43e99fdSEd Maste #else
526c43e99fdSEd Maste #define EVUTIL_EFD_NONBLOCK 0x4000
527c43e99fdSEd Maste #endif
528c43e99fdSEd Maste #ifdef EFD_CLOEXEC
529c43e99fdSEd Maste #define EVUTIL_EFD_CLOEXEC EFD_CLOEXEC
530c43e99fdSEd Maste #else
531c43e99fdSEd Maste #define EVUTIL_EFD_CLOEXEC 0x8000
532c43e99fdSEd Maste #endif
533c43e99fdSEd Maste 
534c43e99fdSEd Maste void evutil_memclear_(void *mem, size_t len);
535c43e99fdSEd Maste 
536*b50261e2SCy Schubert struct in_addr;
537*b50261e2SCy Schubert struct in6_addr;
538*b50261e2SCy Schubert 
539*b50261e2SCy Schubert /* This is a any, loopback, link-local, multicast */
540*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
541*b50261e2SCy Schubert int evutil_v4addr_is_local_(const struct in_addr *in);
542*b50261e2SCy Schubert /* This is a reserved, ipv4compat, ipv4map, loopback,
543*b50261e2SCy Schubert  * link-local, multicast, or unspecified address. */
544*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
545*b50261e2SCy Schubert int evutil_v6addr_is_local_(const struct in6_addr *in);
546*b50261e2SCy Schubert 
547c43e99fdSEd Maste #ifdef __cplusplus
548c43e99fdSEd Maste }
549c43e99fdSEd Maste #endif
550c43e99fdSEd Maste 
551c43e99fdSEd Maste #endif
552