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