libevent
2.2.1
Event notification library
|
Go to the documentation of this file.
26 #ifndef EVENT2_UTIL_H_INCLUDED_
27 #define EVENT2_UTIL_H_INCLUDED_
35 #include <event2/visibility.h>
41 #include <event2/event-config.h>
42 #ifdef EVENT__HAVE_SYS_TIME_H
45 #ifdef EVENT__HAVE_STDINT_H
47 #elif defined(EVENT__HAVE_INTTYPES_H)
50 #ifdef EVENT__HAVE_SYS_TYPES_H
51 #include <sys/types.h>
53 #ifdef EVENT__HAVE_STDDEF_H
60 #ifdef EVENT__HAVE_NETDB_H
66 #ifdef EVENT__HAVE_GETADDRINFO
71 #ifdef EVENT__HAVE_ERRNO_H
74 #include <sys/socket.h>
88 #define EVENT__SOLARIS_11_4 (1)
90 #define EVENT__SOLARIS_11_4 (0)
95 #if defined(EVENT__SIZEOF_VOID__) && !defined(EVENT__SIZEOF_VOID_P)
96 #define EVENT__SIZEOF_VOID_P EVENT__SIZEOF_VOID__
128 #ifdef EVENT__HAVE_UINT64_T
129 #define ev_uint64_t uint64_t
130 #define ev_int64_t int64_t
131 #elif defined(_WIN32)
132 #define ev_uint64_t unsigned __int64
133 #define ev_int64_t signed __int64
134 #elif EVENT__SIZEOF_LONG_LONG == 8
135 #define ev_uint64_t unsigned long long
136 #define ev_int64_t long long
137 #elif EVENT__SIZEOF_LONG == 8
138 #define ev_uint64_t unsigned long
139 #define ev_int64_t long
140 #elif defined(EVENT_IN_DOXYGEN_)
141 #define ev_uint64_t ...
142 #define ev_int64_t ...
144 #error "No way to define ev_uint64_t"
147 #ifdef EVENT__HAVE_UINT32_T
148 #define ev_uint32_t uint32_t
149 #define ev_int32_t int32_t
150 #elif defined(_WIN32)
151 #define ev_uint32_t unsigned int
152 #define ev_int32_t signed int
153 #elif EVENT__SIZEOF_LONG == 4
154 #define ev_uint32_t unsigned long
155 #define ev_int32_t signed long
156 #elif EVENT__SIZEOF_INT == 4
157 #define ev_uint32_t unsigned int
158 #define ev_int32_t signed int
159 #elif defined(EVENT_IN_DOXYGEN_)
160 #define ev_uint32_t ...
161 #define ev_int32_t ...
163 #error "No way to define ev_uint32_t"
166 #ifdef EVENT__HAVE_UINT16_T
167 #define ev_uint16_t uint16_t
168 #define ev_int16_t int16_t
169 #elif defined(_WIN32)
170 #define ev_uint16_t unsigned short
171 #define ev_int16_t signed short
172 #elif EVENT__SIZEOF_INT == 2
173 #define ev_uint16_t unsigned int
174 #define ev_int16_t signed int
175 #elif EVENT__SIZEOF_SHORT == 2
176 #define ev_uint16_t unsigned short
177 #define ev_int16_t signed short
178 #elif defined(EVENT_IN_DOXYGEN_)
179 #define ev_uint16_t ...
180 #define ev_int16_t ...
182 #error "No way to define ev_uint16_t"
185 #ifdef EVENT__HAVE_UINT8_T
186 #define ev_uint8_t uint8_t
187 #define ev_int8_t int8_t
188 #elif defined(EVENT_IN_DOXYGEN_)
189 #define ev_uint8_t ...
190 #define ev_int8_t ...
192 #define ev_uint8_t unsigned char
193 #define ev_int8_t signed char
196 #ifdef EVENT__HAVE_UINTPTR_T
197 #define ev_uintptr_t uintptr_t
198 #define ev_intptr_t intptr_t
199 #elif EVENT__SIZEOF_VOID_P <= 4
200 #define ev_uintptr_t ev_uint32_t
201 #define ev_intptr_t ev_int32_t
202 #elif EVENT__SIZEOF_VOID_P <= 8
203 #define ev_uintptr_t ev_uint64_t
204 #define ev_intptr_t ev_int64_t
205 #elif defined(EVENT_IN_DOXYGEN_)
206 #define ev_uintptr_t ...
207 #define ev_intptr_t ...
209 #error "No way to define ev_uintptr_t"
212 #ifdef EVENT__ssize_t
213 #define ev_ssize_t EVENT__ssize_t
215 #define ev_ssize_t ssize_t
226 #define ev_off_t ev_int64_t
227 #elif EVENT__SIZEOF_OFF_T == 8
228 #define ev_off_t ev_int64_t
229 #elif EVENT__SIZEOF_OFF_T == 4
230 #define ev_off_t ev_int32_t
231 #elif defined(EVENT_IN_DOXYGEN_)
234 #define ev_off_t off_t
253 #ifndef EVENT__HAVE_STDINT_H
254 #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
255 #define EV_INT64_MAX ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
256 #define EV_INT64_MIN ((-EV_INT64_MAX) - 1)
257 #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
258 #define EV_INT32_MAX ((ev_int32_t) 0x7fffffffL)
259 #define EV_INT32_MIN ((-EV_INT32_MAX) - 1)
260 #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
261 #define EV_INT16_MAX ((ev_int16_t) 0x7fffL)
262 #define EV_INT16_MIN ((-EV_INT16_MAX) - 1)
263 #define EV_UINT8_MAX 255
264 #define EV_INT8_MAX 127
265 #define EV_INT8_MIN ((-EV_INT8_MAX) - 1)
267 #define EV_UINT64_MAX UINT64_MAX
268 #define EV_INT64_MAX INT64_MAX
269 #define EV_INT64_MIN INT64_MIN
270 #define EV_UINT32_MAX UINT32_MAX
271 #define EV_INT32_MAX INT32_MAX
272 #define EV_INT32_MIN INT32_MIN
273 #define EV_UINT16_MAX UINT16_MAX
274 #define EV_INT16_MIN INT16_MIN
275 #define EV_INT16_MAX INT16_MAX
276 #define EV_UINT8_MAX UINT8_MAX
277 #define EV_INT8_MAX INT8_MAX
278 #define EV_INT8_MIN INT8_MIN
288 #if EVENT__SIZEOF_SIZE_T == 8
289 #define EV_SIZE_MAX EV_UINT64_MAX
290 #define EV_SSIZE_MAX EV_INT64_MAX
291 #elif EVENT__SIZEOF_SIZE_T == 4
292 #define EV_SIZE_MAX EV_UINT32_MAX
293 #define EV_SSIZE_MAX EV_INT32_MAX
294 #elif defined(EVENT_IN_DOXYGEN_)
295 #define EV_SIZE_MAX ...
296 #define EV_SSIZE_MAX ...
298 #error "No way to define SIZE_MAX"
301 #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
305 #define ev_socklen_t int
306 #elif defined(EVENT__socklen_t)
307 #define ev_socklen_t EVENT__socklen_t
309 #define ev_socklen_t socklen_t
312 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
313 #if !defined(EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
314 && !defined(ss_family)
315 #define ss_family __ss_family
323 #define evutil_socket_t intptr_t
325 #define evutil_socket_t int
341 #ifdef EVENT_IN_DOXYGEN_
346 #define EV_MONOT_PRECISE 1
347 #define EV_MONOT_FALLBACK 2
355 EVENT2_EXPORT_SYMBOL
int
495 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
525 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
527 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
528 do { WSASetLastError(errcode); } while (0)
535 #define EVUTIL_INVALID_SOCKET INVALID_SOCKET
536 #elif defined(EVENT_IN_DOXYGEN_)
552 #define EVUTIL_SOCKET_ERROR() ...
554 #define EVUTIL_SET_SOCKET_ERROR(errcode) ...
556 #define evutil_socket_geterror(sock) ...
558 #define evutil_socket_error_to_string(errcode) ...
559 #define EVUTIL_INVALID_SOCKET -1
562 #define EVUTIL_SOCKET_ERROR() (errno)
563 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
564 do { errno = (errcode); } while (0)
565 #define evutil_socket_geterror(sock) (errno)
566 #define evutil_socket_error_to_string(errcode) (strerror(errcode))
567 #define EVUTIL_INVALID_SOCKET -1
579 #ifdef EVENT__HAVE_TIMERADD
580 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
581 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
583 #define evutil_timeradd(tvp, uvp, vvp) \
585 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
586 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
587 if ((vvp)->tv_usec >= 1000000) { \
589 (vvp)->tv_usec -= 1000000; \
592 #define evutil_timersub(tvp, uvp, vvp) \
594 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
595 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
596 if ((vvp)->tv_usec < 0) { \
598 (vvp)->tv_usec += 1000000; \
603 #ifdef EVENT__HAVE_TIMERCLEAR
604 #define evutil_timerclear(tvp) timerclear(tvp)
606 #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
612 #define evutil_timercmp(tvp, uvp, cmp) \
613 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
614 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
615 ((tvp)->tv_sec cmp (uvp)->tv_sec))
617 #ifdef EVENT__HAVE_TIMERISSET
618 #define evutil_timerisset(tvp) timerisset(tvp)
620 #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
625 #define evutil_offsetof(type, field) offsetof(type, field)
627 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
633 ev_int64_t
evutil_strtoll(
const char *s,
char **endptr,
int base);
636 #ifdef EVENT__HAVE_GETTIMEOFDAY
637 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
641 int evutil_gettimeofday(
struct timeval *tv,
struct timezone *tz);
650 __attribute__((format(printf, 3, 4)))
657 int evutil_vsnprintf(
char *buf,
size_t buflen,
const char *format, va_list ap)
659 __attribute__((format(printf, 3, 0)))
665 const char *
evutil_inet_ntop(
int af,
const void *src,
char *dst,
size_t len);
723 #ifdef EVENT__HAVE_STRUCT_ADDRINFO
724 #define evutil_addrinfo addrinfo
738 struct sockaddr *ai_addr;
749 #if defined(EAI_ADDRFAMILY) && defined(EVENT__HAVE_GETADDRINFO)
750 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
752 #define EVUTIL_EAI_ADDRFAMILY -901
754 #if defined(EAI_AGAIN) && defined(EVENT__HAVE_GETADDRINFO)
755 #define EVUTIL_EAI_AGAIN EAI_AGAIN
757 #define EVUTIL_EAI_AGAIN -902
759 #if defined(EAI_BADFLAGS) && defined(EVENT__HAVE_GETADDRINFO)
760 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
762 #define EVUTIL_EAI_BADFLAGS -903
764 #if defined(EAI_FAIL) && defined(EVENT__HAVE_GETADDRINFO)
765 #define EVUTIL_EAI_FAIL EAI_FAIL
767 #define EVUTIL_EAI_FAIL -904
769 #if defined(EAI_FAMILY) && defined(EVENT__HAVE_GETADDRINFO)
770 #define EVUTIL_EAI_FAMILY EAI_FAMILY
772 #define EVUTIL_EAI_FAMILY -905
774 #if defined(EAI_MEMORY) && defined(EVENT__HAVE_GETADDRINFO)
775 #define EVUTIL_EAI_MEMORY EAI_MEMORY
777 #define EVUTIL_EAI_MEMORY -906
782 #if defined(EAI_NODATA) && defined(EVENT__HAVE_GETADDRINFO) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
783 #define EVUTIL_EAI_NODATA EAI_NODATA
785 #define EVUTIL_EAI_NODATA -907
787 #if defined(EAI_NONAME) && defined(EVENT__HAVE_GETADDRINFO)
788 #define EVUTIL_EAI_NONAME EAI_NONAME
790 #define EVUTIL_EAI_NONAME -908
792 #if defined(EAI_SERVICE) && defined(EVENT__HAVE_GETADDRINFO)
793 #define EVUTIL_EAI_SERVICE EAI_SERVICE
795 #define EVUTIL_EAI_SERVICE -909
797 #if defined(EAI_SOCKTYPE) && defined(EVENT__HAVE_GETADDRINFO)
798 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
800 #define EVUTIL_EAI_SOCKTYPE -910
802 #if defined(EAI_SYSTEM) && defined(EVENT__HAVE_GETADDRINFO)
803 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
805 #define EVUTIL_EAI_SYSTEM -911
808 #define EVUTIL_EAI_CANCEL -90001
810 #if defined(AI_PASSIVE) && defined(EVENT__HAVE_GETADDRINFO)
811 #define EVUTIL_AI_PASSIVE AI_PASSIVE
813 #define EVUTIL_AI_PASSIVE 0x1000
815 #if defined(AI_CANONNAME) && defined(EVENT__HAVE_GETADDRINFO)
816 #define EVUTIL_AI_CANONNAME AI_CANONNAME
818 #define EVUTIL_AI_CANONNAME 0x2000
820 #if defined(AI_NUMERICHOST) && defined(EVENT__HAVE_GETADDRINFO)
821 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
823 #define EVUTIL_AI_NUMERICHOST 0x4000
825 #if defined(AI_NUMERICSERV) && defined(EVENT__HAVE_GETADDRINFO)
826 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
828 #define EVUTIL_AI_NUMERICSERV 0x8000
830 #if defined(AI_V4MAPPED) && defined(EVENT__HAVE_GETADDRINFO)
831 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
833 #define EVUTIL_AI_V4MAPPED 0x10000
835 #if defined(AI_ALL) && defined(EVENT__HAVE_GETADDRINFO)
836 #define EVUTIL_AI_ALL AI_ALL
838 #define EVUTIL_AI_ALL 0x20000
840 #if defined(AI_ADDRCONFIG) && defined(EVENT__HAVE_GETADDRINFO)
841 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
843 #define EVUTIL_AI_ADDRCONFIG 0x40000
868 const char *evutil_gai_strerror(
int err);
Structure to hold information about a monotonic timer.
Definition: util.h:340
EVENT2_EXPORT_SYMBOL int evutil_inet_pton(int af, const char *src, void *dst)
Replacement for inet_pton for platforms which lack it.
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).
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 evut...
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 T...
EVENT2_EXPORT_SYMBOL int evutil_ascii_strcasecmp(const char *str1, const char *str2)
As strcasecmp, but always compares the characters in locale-independent ASCII.
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.
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.
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.
EVENT2_EXPORT_SYMBOL int evutil_make_socket_nonblocking(evutil_socket_t sock)
Do platform-specific operations as needed to make a socket nonblocking.
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.
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.
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.
#define evutil_socket_error_to_string(errcode)
Convert a socket error to a string.
Definition: util.h:558
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.
#define evutil_socket_geterror(sock)
Return the most recent socket error to occur on sock.
Definition: util.h:556
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 snprin...
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.
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...
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_socketpair(int domain, int type, int protocol, evutil_socket_t sv[2])
Create two new sockets that are connected to each other.
EVENT2_EXPORT_SYMBOL void evutil_freeaddrinfo(struct evutil_addrinfo *ai)
Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo.
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 e...
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.
EVENT2_EXPORT_SYMBOL ev_int64_t evutil_strtoll(const char *s, char **endptr, int base)
Parse a 64-bit value from a string.
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 the...
EVENT2_EXPORT_SYMBOL void evutil_secure_rng_add_bytes(const char *dat, size_t datlen)
Seed the random number generator with extra random bytes.
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,...
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().
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().
A definition of struct addrinfo for systems that lack it.
Definition: util.h:731
EVENT2_EXPORT_SYMBOL int evutil_make_listen_socket_reuseable_port(evutil_socket_t sock)
Do platform-specific operations to make a listener port reusable.
EVENT2_EXPORT_SYMBOL int evutil_make_listen_socket_reuseable(evutil_socket_t sock)
Do platform-specific operations to make a listener socket reusable.
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 snpri...
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.
#define evutil_socket_t
A type wide enough to hold the output of "socket()" or "accept()".
Definition: util.h:325