libevent
2.2.1
Event notification library
|
Common convenience functions for cross-platform portability and related socket manipulations. More...
#include <event2/visibility.h>
#include <event2/event-config.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <time.h>
Go to the source code of this file.
Data Structures | |
struct | evutil_addrinfo |
A definition of struct addrinfo for systems that lack it. More... | |
struct | evutil_monotonic_timer |
Structure to hold information about a monotonic timer. More... | |
Macros | |
#define | EV_MONOT_FALLBACK 2 |
#define | EV_MONOT_PRECISE 1 |
#define | ev_socklen_t socklen_t |
#define | EVUTIL_CLOSESOCKET(s) evutil_closesocket(s) |
#define | evutil_offsetof(type, field) ((off_t)(&((type *)0)->field)) |
Replacement for offsetof on platforms that don't define it. | |
#define | evutil_socket_t int |
A type wide enough to hold the output of "socket()" or "accept()". More... | |
#define | evutil_timercmp(tvp, uvp, cmp) |
Return true iff the tvp is related to uvp according to the relational operator cmp. More... | |
#define | evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) |
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.) We define:
| |
#define | ev_int16_t ... |
#define | ev_int32_t ... |
#define | ev_int64_t ... |
#define | ev_int8_t ... |
#define | ev_intptr_t ev_int32_t |
#define | ev_off_t ... |
#define | ev_ssize_t ssize_t |
#define | ev_uint16_t ... |
#define | ev_uint32_t ... |
#define | ev_uint64_t ... |
#define | ev_uint8_t ... |
#define | ev_uintptr_t ev_uint32_t |
Limits for integer types | |
These macros hold the largest or smallest values possible for the ev_[u]int*_t types. | |
#define | EV_INT16_MAX ((ev_int16_t) 0x7fffL) |
#define | EV_INT16_MIN ((-EV_INT16_MAX) - 1) |
#define | EV_INT32_MAX ((ev_int32_t) 0x7fffffffL) |
#define | EV_INT32_MIN ((-EV_INT32_MAX) - 1) |
#define | EV_INT64_MAX ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL) |
#define | EV_INT64_MIN ((-EV_INT64_MAX) - 1) |
#define | EV_INT8_MAX 127 |
#define | EV_INT8_MIN ((-EV_INT8_MAX) - 1) |
#define | EV_UINT16_MAX ((ev_uint16_t)0xffffUL) |
#define | EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL) |
#define | EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL) |
#define | EV_UINT8_MAX 255 |
Limits for SIZE_T and SSIZE_T | |
#define | EV_SIZE_MAX ... |
#define | EV_SSIZE_MAX ... |
#define | EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1) |
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. | |
#define | EVUTIL_INVALID_SOCKET -1 |
#define | EVUTIL_SET_SOCKET_ERROR(errcode) ... |
Replace the most recent socket error with errcode. | |
#define | EVUTIL_SOCKET_ERROR() ... |
Return the most recent socket error. More... | |
#define | evutil_socket_error_to_string(errcode) ... |
Convert a socket error to a string. | |
#define | evutil_socket_geterror(sock) ... |
Return the most recent socket error to occur on sock. | |
Manipulation macros for struct timeval. | |
We define replacements for timeradd, timersub, timerclear, timercmp, and timerisset. | |
#define | evutil_timeradd(tvp, uvp, vvp) |
#define | evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 |
#define | evutil_timersub(tvp, uvp, vvp) |
evutil_getaddrinfo() error codes | |
These values are possible error codes for evutil_getaddrinfo() and related functions. | |
#define | EVUTIL_AI_ADDRCONFIG 0x40000 |
#define | EVUTIL_AI_ALL 0x20000 |
#define | EVUTIL_AI_CANONNAME 0x2000 |
#define | EVUTIL_AI_NUMERICHOST 0x4000 |
#define | EVUTIL_AI_NUMERICSERV 0x8000 |
#define | EVUTIL_AI_PASSIVE 0x1000 |
#define | EVUTIL_AI_V4MAPPED 0x10000 |
#define | EVUTIL_EAI_ADDRFAMILY -901 |
#define | EVUTIL_EAI_AGAIN -902 |
#define | EVUTIL_EAI_BADFLAGS -903 |
#define | EVUTIL_EAI_CANCEL -90001 |
#define | EVUTIL_EAI_FAIL -904 |
#define | EVUTIL_EAI_FAMILY -905 |
#define | EVUTIL_EAI_MEMORY -906 |
#define | EVUTIL_EAI_NODATA -907 |
#define | EVUTIL_EAI_NONAME -908 |
#define | EVUTIL_EAI_SERVICE -909 |
#define | EVUTIL_EAI_SOCKTYPE -910 |
#define | EVUTIL_EAI_SYSTEM -911 |
Functions | |
EVENT2_EXPORT_SYMBOL int | evutil_ascii_strcasecmp (const char *str1, const char *str2) |
As strcasecmp, but always compares the characters in locale-independent ASCII. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_ascii_strncasecmp (const char *str1, const char *str2, size_t n) |
As strncasecmp, but always compares the characters in locale-independent ASCII. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_closesocket (evutil_socket_t sock) |
Do the platform-specific call needed to close a socket returned from socket() or accept(). More... | |
EVENT2_EXPORT_SYMBOL int | evutil_configure_monotonic_time (struct evutil_monotonic_timer *timer, int flags) |
Set up a struct evutil_monotonic_timer; flags can include EV_MONOT_PRECISE and EV_MONOT_FALLBACK. | |
EVENT2_EXPORT_SYMBOL int | evutil_date_rfc1123 (char *date, const size_t datelen, const struct tm *tm) |
Format a date string using RFC 1123 format (used in HTTP). More... | |
EVENT2_EXPORT_SYMBOL void | evutil_freeaddrinfo (struct evutil_addrinfo *ai) |
Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. | |
const EVENT2_EXPORT_SYMBOL char * | evutil_gai_strerror (int err) |
EVENT2_EXPORT_SYMBOL int | evutil_getaddrinfo (const char *nodename, const char *servname, const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res) |
This function clones getaddrinfo for systems that don't have it. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_gettime_monotonic (struct evutil_monotonic_timer *timer, struct timeval *tp) |
Query the current monotonic time from a struct evutil_monotonic_timer previously configured with evutil_configure_monotonic_time(). More... | |
EVENT2_EXPORT_SYMBOL int | evutil_gettimeofday (struct timeval *tv, struct timezone *tz) |
const EVENT2_EXPORT_SYMBOL char * | evutil_inet_ntop (int af, const void *src, char *dst, size_t len) |
Replacement for inet_ntop for platforms which lack it. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_inet_pton (int af, const char *src, void *dst) |
Replacement for inet_pton for platforms which lack it. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_inet_pton_scope (int af, const char *src, void *dst, unsigned *indexp) |
Variation of inet_pton that also parses IPv6 scopes. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_make_listen_socket_ipv6only (evutil_socket_t sock) |
Set ipv6 only bind socket option to make listener work only in ipv6 sockets. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_make_listen_socket_not_ipv6only (evutil_socket_t sock) |
Set ipv6 only bind socket option to make listener work in both ipv4 and ipv6 sockets. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_make_listen_socket_reuseable (evutil_socket_t sock) |
Do platform-specific operations to make a listener socket reusable. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_make_listen_socket_reuseable_port (evutil_socket_t sock) |
Do platform-specific operations to make a listener port reusable. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_make_socket_closeonexec (evutil_socket_t sock) |
Do platform-specific operations as needed to close a socket upon a successful execution of one of the exec*() functions. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_make_socket_nonblocking (evutil_socket_t sock) |
Do platform-specific operations as needed to make a socket nonblocking. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_make_tcp_listen_socket_deferred (evutil_socket_t sock) |
Do platform-specific operations, if possible, to make a tcp listener socket defer accept()s until there is data to read. More... | |
EVENT2_EXPORT_SYMBOL void | evutil_monotonic_timer_free (struct evutil_monotonic_timer *timer) |
Free a struct evutil_monotonic_timer that was allocated using evutil_monotonic_timer_new(). | |
EVENT2_EXPORT_SYMBOL struct evutil_monotonic_timer * | evutil_monotonic_timer_new (void) |
Allocate a new struct evutil_monotonic_timer for use with the evutil_configure_monotonic_time() and evutil_gettime_monotonic() functions. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_parse_sockaddr_port (const char *str, struct sockaddr *out, int *outlen) |
Parse an IPv4 or IPv6 address, with optional port, from a string. More... | |
EVENT2_EXPORT_SYMBOL void | evutil_secure_rng_add_bytes (const char *dat, size_t datlen) |
Seed the random number generator with extra random bytes. More... | |
EVENT2_EXPORT_SYMBOL void | evutil_secure_rng_get_bytes (void *buf, size_t n) |
Generate n bytes of secure pseudorandom data, and store them in buf. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_secure_rng_init (void) |
Seed the secure random number generator if needed, and return 0 on success or -1 on failure. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_secure_rng_set_urandom_device_file (char *fname) |
Set a filename to use in place of /dev/urandom for seeding the secure PRNG. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_set_tcp_keepalive (evutil_socket_t sock, int on, int timeout) |
Do platform-specific operations to set/unset TCP keep-alive options TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT on a socket. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_snprintf (char *buf, size_t buflen, const char *format,...) |
Replacement for snprintf to get consistent behavior on platforms for which the return value of snprintf does not conform to C99. | |
EVENT2_EXPORT_SYMBOL int | evutil_sockaddr_cmp (const struct sockaddr *sa1, const struct sockaddr *sa2, int include_port) |
Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1 preceeds sa2, or greater than 0 if sa1 follows sa2. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_socketpair (int domain, int type, int protocol, evutil_socket_t sv[2]) |
Create two new sockets that are connected to each other. More... | |
EVENT2_EXPORT_SYMBOL ev_int64_t | evutil_strtoll (const char *s, char **endptr, int base) |
Parse a 64-bit value from a string. More... | |
EVENT2_EXPORT_SYMBOL int | evutil_vsnprintf (char *buf, size_t buflen, const char *format, va_list ap) |
Replacement for vsnprintf to get consistent behavior on platforms for which the return value of snprintf does not conform to C99. | |
Common convenience functions for cross-platform portability and related socket manipulations.
#define EVUTIL_SOCKET_ERROR | ( | ) | ... |
Return the most recent socket error.
Not idempotent on all platforms.
#define evutil_socket_t int |
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_timeradd | ( | tvp, | |
uvp, | |||
vvp | |||
) |
#define evutil_timercmp | ( | tvp, | |
uvp, | |||
cmp | |||
) |
Return true iff the tvp is related to uvp according to the relational operator cmp.
Recognized values for cmp are ==, <=, <, >=, and >.
#define evutil_timersub | ( | tvp, | |
uvp, | |||
vvp | |||
) |
EVENT2_EXPORT_SYMBOL int evutil_ascii_strcasecmp | ( | const char * | str1, |
const char * | str2 | ||
) |
As strcasecmp, but always compares the characters in locale-independent ASCII.
That's useful if you're handling data in ASCII-based protocols. str1 and str2 must not be NULL.
EVENT2_EXPORT_SYMBOL int evutil_ascii_strncasecmp | ( | const char * | str1, |
const char * | str2, | ||
size_t | n | ||
) |
As strncasecmp, but always compares the characters in locale-independent ASCII.
That's useful if you're handling data in ASCII-based protocols.
EVENT2_EXPORT_SYMBOL int evutil_closesocket | ( | evutil_socket_t | sock | ) |
Do the platform-specific call needed to close a socket returned from socket() or accept().
sock | The socket to be closed |
EVENT2_EXPORT_SYMBOL int evutil_date_rfc1123 | ( | char * | date, |
const size_t | datelen, | ||
const struct tm * | tm | ||
) |
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.
EVENT2_EXPORT_SYMBOL int evutil_getaddrinfo | ( | const char * | nodename, |
const char * | servname, | ||
const struct evutil_addrinfo * | hints_in, | ||
struct evutil_addrinfo ** | res | ||
) |
This function clones getaddrinfo for systems that don't have it.
For full details, see RFC 3493, section 6.1.
Limitations:
For a nonblocking variant, see evdns_getaddrinfo.
EVENT2_EXPORT_SYMBOL int evutil_gettime_monotonic | ( | struct evutil_monotonic_timer * | timer, |
struct timeval * | tp | ||
) |
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 may be changed.
It is not safe to use this function on the same timer from multiple threads.
const EVENT2_EXPORT_SYMBOL char* evutil_inet_ntop | ( | int | af, |
const void * | src, | ||
char * | dst, | ||
size_t | len | ||
) |
Replacement for inet_ntop for platforms which lack it.
src must not be NULL.
EVENT2_EXPORT_SYMBOL int evutil_inet_pton | ( | int | af, |
const char * | src, | ||
void * | dst | ||
) |
Replacement for inet_pton for platforms which lack it.
src must not be NULL.
EVENT2_EXPORT_SYMBOL int evutil_inet_pton_scope | ( | int | af, |
const char * | src, | ||
void * | dst, | ||
unsigned * | indexp | ||
) |
Variation of inet_pton that also parses IPv6 scopes.
Public for unit tests. No reason to call this directly.
EVENT2_EXPORT_SYMBOL int evutil_make_listen_socket_ipv6only | ( | evutil_socket_t | sock | ) |
Set ipv6 only bind socket option to make listener work only in ipv6 sockets.
According to RFC3493 and most Linux distributions, default value for the sockets is to work in IPv4-mapped mode. In IPv4-mapped mode, it is not possible to bind same port from different IPv4 and IPv6 handlers.
On Windows the default value is instead to only work in IPv6 mode.
sock | The socket to make in ipv6only working mode |
EVENT2_EXPORT_SYMBOL int evutil_make_listen_socket_not_ipv6only | ( | evutil_socket_t | sock | ) |
Set ipv6 only bind socket option to make listener work in both ipv4 and ipv6 sockets.
According to RFC3493 and most Linux distributions, default value for the sockets is to work in IPv4-mapped mode. In IPv4-mapped mode, it is not possible to bind same port from different IPv4 and IPv6 handlers.
On Windows the default value is instead to only work in IPv6 mode.
sock | The socket to make in ipv6only working mode |
EVENT2_EXPORT_SYMBOL int evutil_make_listen_socket_reuseable | ( | 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.
sock | The socket to make reusable |
EVENT2_EXPORT_SYMBOL int evutil_make_listen_socket_reuseable_port | ( | evutil_socket_t | sock | ) |
Do platform-specific operations to make a listener port reusable.
Specifically, we want to make sure that multiple programs that also set the same socket option will be able to bind, and listen at the same time, for incoming connections/datagrams to be distributed evenly across all of the threads (or processes).
This feature is available only on Linux 3.9+, DragonFlyBSD 3.6+, FreeBSD 12.0+, Solaris 11.4, AIX 7.2.5 for now.
sock | The socket to make reusable |
EVENT2_EXPORT_SYMBOL int evutil_make_socket_closeonexec | ( | evutil_socket_t | sock | ) |
Do platform-specific operations as needed to close a socket upon a successful execution of one of the exec*() functions.
sock | The socket to be closed |
EVENT2_EXPORT_SYMBOL int evutil_make_socket_nonblocking | ( | evutil_socket_t | sock | ) |
Do platform-specific operations as needed to make a socket nonblocking.
sock | The socket to make nonblocking |
EVENT2_EXPORT_SYMBOL int evutil_make_tcp_listen_socket_deferred | ( | evutil_socket_t | sock | ) |
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.
sock | The listening socket to make deferred |
EVENT2_EXPORT_SYMBOL struct evutil_monotonic_timer* evutil_monotonic_timer_new | ( | void | ) |
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.
EVENT2_EXPORT_SYMBOL int evutil_parse_sockaddr_port | ( | const char * | str, |
struct sockaddr * | out, | ||
int * | outlen | ||
) |
Parse an IPv4 or IPv6 address, with optional port, from a string.
Recognized formats are:
If no port is specified, the port in the output is set to 0.
str | The string to parse. |
out | A struct sockaddr to hold the result. This should probably be a struct sockaddr_storage. |
outlen | A pointer to the number of bytes that that 'out' can safely hold. Set to the number of bytes used in 'out' on success. |
EVENT2_EXPORT_SYMBOL void evutil_secure_rng_add_bytes | ( | const char * | dat, |
size_t | datlen | ||
) |
Seed the random number generator with extra random bytes.
You should almost never need to call this function; it should be sufficient to invoke evutil_secure_rng_init(), or let Libevent take care of calling evutil_secure_rng_init() on its own.
If you call this function as a replacement for the regular entropy sources, then you need to be sure that your input contains a fairly large amount of strong entropy. Doing so is notoriously hard: most people who try get it wrong. Watch out!
This function does nothing when the system provides arc4random() function because it will provide proper entropy.
dat | a buffer full of a strong source of random numbers |
datlen | the number of bytes to read from datlen |
EVENT2_EXPORT_SYMBOL void evutil_secure_rng_get_bytes | ( | void * | buf, |
size_t | n | ||
) |
Generate n bytes of secure pseudorandom data, and store them in buf.
Current versions of Libevent use an ARC4-based random number generator, seeded using the platform's entropy source (/dev/urandom on Unix-like systems; BCryptGenRandom on Windows). This is not actually as secure as it should be: ARC4 is a pretty lousy cipher, and the current implementation provides only rudimentary prediction- and backtracking-resistance. Don't use this for serious cryptographic applications.
EVENT2_EXPORT_SYMBOL int evutil_secure_rng_init | ( | void | ) |
Seed the secure random number generator if needed, and return 0 on success or -1 on failure.
It is okay to call this function more than once; it will still return 0 if the RNG has been successfully seeded and -1 if it can't be seeded.
Ordinarily you don't need to call this function from your own code; Libevent will seed the RNG itself the first time it needs good random numbers. You only need to call it if (a) you want to double-check that one of the seeding methods did succeed, or (b) you plan to drop the capability to seed (by chrooting, or dropping capabilities, or whatever), and you want to make sure that seeding happens before your program loses the ability to do it.
EVENT2_EXPORT_SYMBOL int evutil_secure_rng_set_urandom_device_file | ( | char * | fname | ) |
Set a filename to use in place of /dev/urandom for seeding the secure PRNG.
Return 0 on success, -1 on failure.
Call this function BEFORE calling any other initialization or RNG functions.
(This string will NOT be copied internally. Do not free it while any user of the secure RNG might be running. Don't pass anything other than a real /dev/...random device file here, or you might lose security.)
This API is unstable, and might change in a future libevent version.
EVENT2_EXPORT_SYMBOL int evutil_set_tcp_keepalive | ( | evutil_socket_t | sock, |
int | on, | ||
int | timeout | ||
) |
Do platform-specific operations to set/unset TCP keep-alive options TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT on a socket.
sock | The socket to be set TCP keep-alive |
on | Nonzero value to enable TCP keep-alive, 0 to disable |
timeout | The timeout in seconds with no activity until the first keepalive probe is sent |
EVENT2_EXPORT_SYMBOL int evutil_sockaddr_cmp | ( | const struct sockaddr * | sa1, |
const struct sockaddr * | sa2, | ||
int | include_port | ||
) |
Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1 preceeds sa2, or greater than 0 if sa1 follows sa2.
If include_port is true, consider the port as well as the address. Only implemented for AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain the same between Libevent versions.
EVENT2_EXPORT_SYMBOL int evutil_socketpair | ( | int | domain, |
int | type, | ||
int | protocol, | ||
evutil_socket_t | sv[2] | ||
) |
Create two new sockets that are connected to each other.
On Unix, this simply calls socketpair() and creates an unnamed pair of connected sockets in the specified domain, of the specified type, and using the optionally specified protocol.
On Windows, it will try to use the AF_UNIX to create unix socket pair if available, otherwise it instead uses AF_INET to create socket pair, binding the loopback network interface 127.0.0.1.
Including the bitwise OR of the EVUTIL_SOCK_NONBLOCK and/or EVUTIL_SOCK_CLOEXEC in the type argument will apply to both file descriptors.
(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()
EVENT2_EXPORT_SYMBOL ev_int64_t evutil_strtoll | ( | const char * | s, |
char ** | endptr, | ||
int | base | ||
) |
Parse a 64-bit value from a string.
Arguments are as for strtol.