* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef EVENT2_UTIL_H_INCLUDED_
#define EVENT2_UTIL_H_INCLUDED_
Common convenience functions for cross-platform portability and
related socket manipulations.
#include <event2/visibility.h>
#include <event2/event-config.h>
#ifdef EVENT__HAVE_SYS_TIME_H
#ifdef EVENT__HAVE_STDINT_H
#elif defined(EVENT__HAVE_INTTYPES_H)
#ifdef EVENT__HAVE_SYS_TYPES_H
#ifdef EVENT__HAVE_STDDEF_H
#ifdef EVENT__HAVE_NETDB_H
#if !defined(_GNU_SOURCE)
#ifdef EVENT__HAVE_GETADDRINFO
/* for EAI_* definitions. */
#ifdef EVENT__HAVE_ERRNO_H
/* Some openbsd autoconf versions get the name of this macro wrong. */
#if defined(EVENT__SIZEOF_VOID__) && !defined(EVENT__SIZEOF_VOID_P)
#define EVENT__SIZEOF_VOID_P EVENT__SIZEOF_VOID__
* @name Standard integer types.
* Integer type definitions for types that are supposed to be defined in the
* C99-specified stdint.h. Shamefully, some platforms do not include
* stdint.h, so we need to replace it. (If you are on a platform like this,
* your C headers are now over 10 years out of date. You should bug them to
* do something about this.)
* <dt>ev_uint64_t, ev_uint32_t, ev_uint16_t, ev_uint8_t</dt>
* <dd>unsigned integer types of exactly 64, 32, 16, and 8 bits
* <dt>ev_int64_t, ev_int32_t, ev_int16_t, ev_int8_t</dt>
* <dd>signed integer types of exactly 64, 32, 16, and 8 bits
* <dt>ev_uintptr_t, ev_intptr_t</dt>
* <dd>unsigned/signed integers large enough
* to hold a pointer without loss of bits.</dd>
* <dd>A signed type of the same size as size_t</dd>
* <dd>A signed type typically used to represent offsets within a
* (potentially large) file</dd>
#ifdef EVENT__HAVE_UINT64_T
#define ev_uint64_t uint64_t
#define ev_int64_t int64_t
#define ev_uint64_t unsigned __int64
#define ev_int64_t signed __int64
#elif EVENT__SIZEOF_LONG_LONG == 8
#define ev_uint64_t unsigned long long
#define ev_int64_t long long
#elif EVENT__SIZEOF_LONG == 8
#define ev_uint64_t unsigned long
#elif defined(EVENT_IN_DOXYGEN_)
#error "No way to define ev_uint64_t"
#ifdef EVENT__HAVE_UINT32_T
#define ev_uint32_t uint32_t
#define ev_int32_t int32_t
#define ev_uint32_t unsigned int
#define ev_int32_t signed int
#elif EVENT__SIZEOF_LONG == 4
#define ev_uint32_t unsigned long
#define ev_int32_t signed long
#elif EVENT__SIZEOF_INT == 4
#define ev_uint32_t unsigned int
#define ev_int32_t signed int
#elif defined(EVENT_IN_DOXYGEN_)
#error "No way to define ev_uint32_t"
#ifdef EVENT__HAVE_UINT16_T
#define ev_uint16_t uint16_t
#define ev_int16_t int16_t
#define ev_uint16_t unsigned short
#define ev_int16_t signed short
#elif EVENT__SIZEOF_INT == 2
#define ev_uint16_t unsigned int
#define ev_int16_t signed int
#elif EVENT__SIZEOF_SHORT == 2
#define ev_uint16_t unsigned short
#define ev_int16_t signed short
#elif defined(EVENT_IN_DOXYGEN_)
#error "No way to define ev_uint16_t"
#ifdef EVENT__HAVE_UINT8_T
#define ev_uint8_t uint8_t
#elif defined(EVENT_IN_DOXYGEN_)
#define ev_uint8_t unsigned char
#define ev_int8_t signed char
#ifdef EVENT__HAVE_UINTPTR_T
#define ev_uintptr_t uintptr_t
#define ev_intptr_t intptr_t
#elif EVENT__SIZEOF_VOID_P <= 4
#define ev_uintptr_t ev_uint32_t
#define ev_intptr_t ev_int32_t
#elif EVENT__SIZEOF_VOID_P <= 8
#define ev_uintptr_t ev_uint64_t
#define ev_intptr_t ev_int64_t
#elif defined(EVENT_IN_DOXYGEN_)
#error "No way to define ev_uintptr_t"
#define ev_ssize_t EVENT__ssize_t
#define ev_ssize_t ssize_t
/* Note that we define ev_off_t based on the compile-time size of off_t that
* we used to build Libevent, and not based on the current size of off_t.
* (For example, we don't define ev_off_t to off_t.). We do this because
* some systems let you build your software with different off_t sizes
* at runtime, and so putting in any dependency on off_t would risk API
#define ev_off_t ev_int64_t
#elif EVENT__SIZEOF_OFF_T == 8
#define ev_off_t ev_int64_t
#elif EVENT__SIZEOF_OFF_T == 4
#define ev_off_t ev_int32_t
#elif defined(EVENT_IN_DOXYGEN_)
/* Limits for integer types.
We're making two assumptions here:
- The compiler does constant folding properly.
- The platform does signed arithmetic in two's complement.
@name Limits for integer types
These macros hold the largest or smallest values possible for the
#ifndef EVENT__HAVE_STDINT_H
#define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
#define EV_INT64_MAX ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
#define EV_INT64_MIN ((-EV_INT64_MAX) - 1)
#define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
#define EV_INT32_MAX ((ev_int32_t) 0x7fffffffL)
#define EV_INT32_MIN ((-EV_INT32_MAX) - 1)
#define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
#define EV_INT16_MAX ((ev_int16_t) 0x7fffL)
#define EV_INT16_MIN ((-EV_INT16_MAX) - 1)
#define EV_INT8_MIN ((-EV_INT8_MAX) - 1)
#define EV_UINT64_MAX UINT64_MAX
#define EV_INT64_MAX INT64_MAX
#define EV_INT64_MIN INT64_MIN
#define EV_UINT32_MAX UINT32_MAX
#define EV_INT32_MAX INT32_MAX
#define EV_INT32_MIN INT32_MIN
#define EV_UINT16_MAX UINT16_MAX
#define EV_INT16_MAX INT16_MAX
#define EV_UINT8_MAX UINT8_MAX
#define EV_INT8_MAX INT8_MAX
#define EV_INT8_MIN INT8_MIN
@name Limits for SIZE_T and SSIZE_T
#if EVENT__SIZEOF_SIZE_T == 8
#define EV_SIZE_MAX EV_UINT64_MAX
#define EV_SSIZE_MAX EV_INT64_MAX
#elif EVENT__SIZEOF_SIZE_T == 4
#define EV_SIZE_MAX EV_UINT32_MAX
#define EV_SSIZE_MAX EV_INT32_MAX
#elif defined(EVENT_IN_DOXYGEN_)
#error "No way to define SIZE_MAX"
#define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
#elif defined(EVENT__socklen_t)
#define ev_socklen_t EVENT__socklen_t
#define ev_socklen_t socklen_t
#ifdef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
#if !defined(EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
#define ss_family __ss_family
* A type wide enough to hold the output of "socket()" or "accept()". On
* Windows, this is an intptr_t; elsewhere, it is an int. */
#define evutil_socket_t intptr_t
#define evutil_socket_t int
* Structure to hold information about a monotonic timer
* Use this with evutil_configure_monotonic_time() and
* evutil_gettime_monotonic().
* This is an opaque structure; you can allocate one using
* evutil_monotonic_timer_new().
* @see evutil_monotonic_timer_new(), evutil_monotonic_timer_free(),
* evutil_configure_monotonic_time(), evutil_gettime_monotonic()
struct evutil_monotonic_timer
{/*Empty body so that doxygen will generate documentation here.*/}
#define EV_MONOT_PRECISE 1
#define EV_MONOT_FALLBACK 2
/** Format a date string using RFC 1123 format (used in HTTP).
* If `tm` is NULL, current system's time will be used.
* The number of characters written will be returned.
* One should check if the return value is smaller than `datelen` to check if
* the result is truncated or not.
evutil_date_rfc1123(char *date, const size_t datelen, const struct tm *tm);
/** Allocate a new struct evutil_monotonic_timer for use with the
* evutil_configure_monotonic_time() and evutil_gettime_monotonic()
* functions. You must configure the timer with
* evutil_configure_monotonic_time() before using it.
struct evutil_monotonic_timer * evutil_monotonic_timer_new(void);
/** Free a struct evutil_monotonic_timer that was allocated using
* evutil_monotonic_timer_new().
void evutil_monotonic_timer_free(struct evutil_monotonic_timer *timer);
/** Set up a struct evutil_monotonic_timer; flags can include
* EV_MONOT_PRECISE and EV_MONOT_FALLBACK.
int evutil_configure_monotonic_time(struct evutil_monotonic_timer *timer,
/** Query the current monotonic time from a struct evutil_monotonic_timer
* previously configured with evutil_configure_monotonic_time(). Monotonic
* time is guaranteed never to run in reverse, but is not necessarily epoch-
* based, or relative to any other definite point. Use it to make reliable
* measurements of elapsed time between events even when the system time
* It is not safe to use this funtion on the same timer from multiple
int evutil_gettime_monotonic(struct evutil_monotonic_timer *timer,
/** Create two new sockets that are connected to each other.
On Unix, this simply calls socketpair(). On Windows, it uses the
loopback network interface on 127.0.0.1, and only
AF_INET,SOCK_STREAM are supported.
(This may fail on some Windows hosts where firewall software has cleverly
decided to keep 127.0.0.1 from talking to itself.)
Parameters and return values are as for socketpair()
int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
/** Do platform-specific operations as needed to make a socket nonblocking.
@param sock The socket to make nonblocking
@return 0 on success, -1 on failure
int evutil_make_socket_nonblocking(evutil_socket_t sock);
/** Do platform-specific operations to make a listener socket reusable.
Specifically, we want to make sure that another program will be able
to bind this address right after we've closed the listener.
This differs from Windows's interpretation of "reusable", which
allows multiple listeners to bind the same address at the same time.
@param sock The socket to make reusable
@return 0 on success, -1 on failure
int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
/** Do platform-specific operations to make a listener port reusable.
Specifically, we want to make sure that multiple programs which also
set the same socket option will be able to bind, listen at the same time.
This is a feature available only to Linux 3.9+
@param sock The socket to make reusable
@return 0 on success, -1 on failure
int evutil_make_listen_socket_reuseable_port(evutil_socket_t sock);
/** Do platform-specific operations as needed to close a socket upon a
successful execution of one of the exec*() functions.
@param sock The socket to be closed
@return 0 on success, -1 on failure
int evutil_make_socket_closeonexec(evutil_socket_t sock);
/** Do the platform-specific call needed to close a socket returned from
@param sock The socket to be closed
@return 0 on success, -1 on failure
int evutil_closesocket(evutil_socket_t sock);
#define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
/** Do platform-specific operations, if possible, to make a tcp listener
* socket defer accept()s until there is data to read.
* Not all platforms support this. You don't want to do this for every
* listener socket: only the ones that implement a protocol where the
* client transmits before the server needs to respond.
* @param sock The listening socket to to make deferred
* @return 0 on success (whether the operation is supported or not),
int evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock);
/** Return the most recent socket error. Not idempotent on all platforms. */
#define EVUTIL_SOCKET_ERROR() WSAGetLastError()
/** Replace the most recent socket error with errcode */
#define EVUTIL_SET_SOCKET_ERROR(errcode) \
do { WSASetLastError(errcode); } while (0)
/** Return the most recent socket error to occur on sock. */
int evutil_socket_geterror(evutil_socket_t sock);
/** Convert a socket error to a string. */
const char *evutil_socket_error_to_string(int errcode);
#elif defined(EVENT_IN_DOXYGEN_)
@name Socket error functions
These functions are needed for making programs compatible between
Windows and Unix-like platforms.
You see, Winsock handles socket errors differently from the rest of
the world. Elsewhere, a socket error is like any other error and is
stored in errno. But winsock functions require you to retrieve the
error with a special function, and don't let you use strerror for
the error codes. And handling EWOULDBLOCK is ... different.
/** Return the most recent socket error. Not idempotent on all platforms. */
#define EVUTIL_SOCKET_ERROR() ...
/** Replace the most recent socket error with errcode */
#define EVUTIL_SET_SOCKET_ERROR(errcode) ...
/** Return the most recent socket error to occur on sock. */
#define evutil_socket_geterror(sock) ...
/** Convert a socket error to a string. */
#define evutil_socket_error_to_string(errcode) ...
#define EVUTIL_SOCKET_ERROR() (errno)
#define EVUTIL_SET_SOCKET_ERROR(errcode) \
do { errno = (errcode); } while (0)