libevent

include/event2/util.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  * 3. The name of the author may not be used to endorse or promote products
00013  *    derived from this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00017  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00018  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00019  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00020  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00024  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025  */
00026 #ifndef _EVENT2_UTIL_H_
00027 #define _EVENT2_UTIL_H_
00028 
00036 #ifdef __cplusplus
00037 extern "C" {
00038 #endif
00039 
00040 #include <event2/event-config.h>
00041 #ifdef _EVENT_HAVE_SYS_TIME_H
00042 #include <sys/time.h>
00043 #endif
00044 #ifdef _EVENT_HAVE_STDINT_H
00045 #include <stdint.h>
00046 #elif defined(_EVENT_HAVE_INTTYPES_H)
00047 #include <inttypes.h>
00048 #endif
00049 #ifdef _EVENT_HAVE_SYS_TYPES_H
00050 #include <sys/types.h>
00051 #endif
00052 #ifdef _EVENT_HAVE_STDDEF_H
00053 #include <stddef.h>
00054 #endif
00055 #ifdef _MSC_VER
00056 #include <BaseTsd.h>
00057 #endif
00058 #include <stdarg.h>
00059 #ifdef _EVENT_HAVE_NETDB_H
00060 #if !defined(_GNU_SOURCE)
00061 #define _GNU_SOURCE
00062 #endif
00063 #include <netdb.h>
00064 #endif
00065 
00066 #ifdef WIN32
00067 #include <winsock2.h>
00068 #else
00069 #include <sys/socket.h>
00070 #endif
00071 
00072 /* Integer type definitions for types that are supposed to be defined in the
00073  * C99-specified stdint.h.  Shamefully, some platforms do not include
00074  * stdint.h, so we need to replace it.  (If you are on a platform like this,
00075  * your C headers are now over 10 years out of date.  You should bug them to
00076  * do something about this.)
00077  *
00078  * We define:
00079  *    ev_uint64_t, ev_uint32_t, ev_uint16_t, ev_uint8_t -- unsigned integer
00080  *      types of exactly 64, 32, 16, and 8 bits respectively.
00081  *    ev_int64_t, ev_int32_t, ev_int16_t, ev_int8_t -- signed integer
00082  *      types of exactly 64, 32, 16, and 8 bits respectively.
00083  *    ev_uintptr_t, ev_intptr_t -- unsigned/signed integers large enough
00084  *      to hold a pointer without loss of bits.
00085  */
00086 #ifdef _EVENT_HAVE_UINT64_T
00087 #define ev_uint64_t uint64_t
00088 #define ev_int64_t int64_t
00089 #elif defined(WIN32)
00090 #define ev_uint64_t unsigned __int64
00091 #define ev_int64_t signed __int64
00092 #elif _EVENT_SIZEOF_LONG_LONG == 8
00093 #define ev_uint64_t unsigned long long
00094 #define ev_int64_t long long
00095 #elif _EVENT_SIZEOF_LONG == 8
00096 #define ev_uint64_t unsigned long
00097 #define ev_int64_t long
00098 #else
00099 #error "No way to define ev_uint64_t"
00100 #endif
00101 
00102 #ifdef _EVENT_HAVE_UINT32_T
00103 #define ev_uint32_t uint32_t
00104 #define ev_int32_t int32_t
00105 #elif defined(WIN32)
00106 #define ev_uint32_t unsigned int
00107 #define ev_int32_t signed int
00108 #elif _EVENT_SIZEOF_LONG == 4
00109 #define ev_uint32_t unsigned long
00110 #define ev_int32_t signed long
00111 #elif _EVENT_SIZEOF_INT == 4
00112 #define ev_uint32_t unsigned int
00113 #define ev_int32_t signed int
00114 #else
00115 #error "No way to define ev_uint32_t"
00116 #endif
00117 
00118 #ifdef _EVENT_HAVE_UINT16_T
00119 #define ev_uint16_t uint16_t
00120 #define ev_int16_t  int16_t
00121 #elif defined(WIN32)
00122 #define ev_uint16_t unsigned short
00123 #define ev_int16_t  signed short
00124 #elif _EVENT_SIZEOF_INT == 2
00125 #define ev_uint16_t unsigned int
00126 #define ev_int16_t  signed int
00127 #elif _EVENT_SIZEOF_SHORT == 2
00128 #define ev_uint16_t unsigned short
00129 #define ev_int16_t  signed short
00130 #else
00131 #error "No way to define ev_uint16_t"
00132 #endif
00133 
00134 #ifdef _EVENT_HAVE_UINT8_T
00135 #define ev_uint8_t uint8_t
00136 #define ev_int8_t int8_t
00137 #else
00138 #define ev_uint8_t unsigned char
00139 #define ev_int8_t signed char
00140 #endif
00141 
00142 /* Some openbsd autoconf versions get the name of this macro wrong. */
00143 #if defined(_EVENT_SIZEOF_VOID__) && !defined(_EVENT_SIZEOF_VOID_P)
00144 #define _EVENT_SIZEOF_VOID_P _EVENT_SIZEOF_VOID__
00145 #endif
00146 
00147 #ifdef _EVENT_HAVE_UINTPTR_T
00148 #define ev_uintptr_t uintptr_t
00149 #define ev_intptr_t intptr_t
00150 #elif _EVENT_SIZEOF_VOID_P <= 4
00151 #define ev_uintptr_t ev_uint32_t
00152 #define ev_intptr_t ev_int32_t
00153 #elif _EVENT_SIZEOF_VOID_P <= 8
00154 #define ev_uintptr_t ev_uint64_t
00155 #define ev_intptr_t ev_int64_t
00156 #else
00157 #error "No way to define ev_uintptr_t"
00158 #endif
00159 
00160 #ifdef _EVENT_ssize_t
00161 #define ev_ssize_t _EVENT_ssize_t
00162 #else
00163 #define ev_ssize_t ssize_t
00164 #endif
00165 
00166 #ifdef WIN32
00167 #define ev_off_t ev_int64_t
00168 #else
00169 #define ev_off_t off_t
00170 #endif
00171 
00172 /* Limits for integer types.
00173 
00174    We're making two assumptions here:
00175      - The compiler does constant folding properly.
00176      - The platform does signed arithmetic in two's complement.
00177 */
00178 
00179 #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
00180 #define EV_INT64_MAX  ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
00181 #define EV_INT64_MIN  ((-EV_INT64_MAX) - 1)
00182 #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
00183 #define EV_INT32_MAX  ((ev_int32_t) 0x7fffffffL)
00184 #define EV_INT32_MIN  ((-EV_INT32_MAX) - 1)
00185 #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
00186 #define EV_INT16_MAX  ((ev_int16_t) 0x7fffL)
00187 #define EV_INT16_MIN  ((-EV_INT16_MAX) - 1)
00188 #define EV_UINT8_MAX  255
00189 #define EV_INT8_MAX   127
00190 #define EV_INT8_MIN   ((-EV_INT8_MAX) - 1)
00191 
00192 #if _EVENT_SIZEOF_SIZE_T == 8
00193 #define EV_SIZE_MAX EV_UINT64_MAX
00194 #define EV_SSIZE_MAX EV_INT64_MAX
00195 #elif _EVENT_SIZEOF_SIZE_T == 4
00196 #define EV_SIZE_MAX EV_UINT32_MAX
00197 #define EV_SSIZE_MAX EV_INT32_MAX
00198 #else
00199 #error "No way to define SIZE_MAX"
00200 #endif
00201 
00202 #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
00203 
00204 #ifdef WIN32
00205 #define ev_socklen_t int
00206 #elif defined(_EVENT_socklen_t)
00207 #define ev_socklen_t _EVENT_socklen_t
00208 #else
00209 #define ev_socklen_t socklen_t
00210 #endif
00211 
00212 #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
00213 #if !defined(_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
00214  && !defined(ss_family)
00215 #define ss_family __ss_family
00216 #endif
00217 #endif
00218 
00219 #ifdef WIN32
00220 
00222 #define evutil_socket_t intptr_t
00223 #else
00224 #define evutil_socket_t int
00225 #endif
00226 
00236 int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
00242 int evutil_make_socket_nonblocking(evutil_socket_t sock);
00243 
00251 int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
00252 
00259 int evutil_make_socket_closeonexec(evutil_socket_t sock);
00260 
00267 int evutil_closesocket(evutil_socket_t sock);
00268 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
00269 
00270 /* Winsock handles socket errors differently from the rest of the world.
00271  * Elsewhere, a socket error is like any other error and is stored in errno.
00272  * But winsock functions require you to retrieve the error with a special
00273  * function, and don't let you use strerror for the error codes.  And handling
00274  * EWOULDBLOCK is ... different. */
00275 
00276 #ifdef WIN32
00277 
00278 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
00279 
00280 #define EVUTIL_SET_SOCKET_ERROR(errcode)                \
00281         do { WSASetLastError(errcode); } while (0)
00282 
00283 int evutil_socket_geterror(evutil_socket_t sock);
00285 const char *evutil_socket_error_to_string(int errcode);
00286 #else
00287 #define EVUTIL_SOCKET_ERROR() (errno)
00288 #define EVUTIL_SET_SOCKET_ERROR(errcode)                \
00289                 do { errno = (errcode); } while (0)
00290 #define evutil_socket_geterror(sock) (errno)
00291 #define evutil_socket_error_to_string(errcode) (strerror(errcode))
00292 #endif
00293 
00294 /*
00295  * Manipulation macros for struct timeval.  We define replacements
00296  * for timeradd, timersub, timerclear, timercmp, and timerisset.
00297  */
00298 #ifdef _EVENT_HAVE_TIMERADD
00299 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
00300 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
00301 #else
00302 #define evutil_timeradd(tvp, uvp, vvp)                                  \
00303         do {                                                            \
00304                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
00305                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
00306                 if ((vvp)->tv_usec >= 1000000) {                        \
00307                         (vvp)->tv_sec++;                                \
00308                         (vvp)->tv_usec -= 1000000;                      \
00309                 }                                                       \
00310         } while (0)
00311 #define evutil_timersub(tvp, uvp, vvp)                                  \
00312         do {                                                            \
00313                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
00314                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
00315                 if ((vvp)->tv_usec < 0) {                               \
00316                         (vvp)->tv_sec--;                                \
00317                         (vvp)->tv_usec += 1000000;                      \
00318                 }                                                       \
00319         } while (0)
00320 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */
00321 
00322 #ifdef _EVENT_HAVE_TIMERCLEAR
00323 #define evutil_timerclear(tvp) timerclear(tvp)
00324 #else
00325 #define evutil_timerclear(tvp)  (tvp)->tv_sec = (tvp)->tv_usec = 0
00326 #endif
00327 
00330 #define evutil_timercmp(tvp, uvp, cmp)                                  \
00331         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
00332          ((tvp)->tv_usec cmp (uvp)->tv_usec) :                          \
00333          ((tvp)->tv_sec cmp (uvp)->tv_sec))
00334 
00335 #ifdef _EVENT_HAVE_TIMERISSET
00336 #define evutil_timerisset(tvp) timerisset(tvp)
00337 #else
00338 #define evutil_timerisset(tvp)  ((tvp)->tv_sec || (tvp)->tv_usec)
00339 #endif
00340 
00341 /* Replacement for offsetof on platforms that don't define it. */
00342 #ifdef offsetof
00343 #define evutil_offsetof(type, field) offsetof(type, field)
00344 #else
00345 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
00346 #endif
00347 
00348 /* big-int related functions */
00350 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
00351 
00352 /* Replacement for gettimeofday on platforms that lack it. */
00353 #ifdef _EVENT_HAVE_GETTIMEOFDAY
00354 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
00355 #else
00356 struct timezone;
00357 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
00358 #endif
00359 
00363 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
00364 #ifdef __GNUC__
00365         __attribute__((format(printf, 3, 4)))
00366 #endif
00367 ;
00368 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap);
00369 
00371 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
00373 int evutil_inet_pton(int af, const char *src, void *dst);
00374 struct sockaddr;
00375 
00396 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
00397 
00403 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
00404     int include_port);
00405 
00409 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
00413 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
00414 
00415 /* Here we define evutil_addrinfo to the native addrinfo type, or redefinte it
00416  * if this system has no getaddrinfo(). */
00417 #ifdef _EVENT_HAVE_STRUCT_ADDRINFO
00418 #define evutil_addrinfo addrinfo
00419 #else
00420 struct evutil_addrinfo {
00421         int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
00422         int     ai_family;    /* PF_xxx */
00423         int     ai_socktype;  /* SOCK_xxx */
00424         int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
00425         size_t  ai_addrlen;   /* length of ai_addr */
00426         char   *ai_canonname; /* canonical name for nodename */
00427         struct sockaddr  *ai_addr; /* binary address */
00428         struct evutil_addrinfo  *ai_next; /* next structure in linked list */
00429 };
00430 #endif
00431 #ifdef EAI_ADDRFAMILY
00432 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
00433 #else
00434 #define EVUTIL_EAI_ADDRFAMILY -901
00435 #endif
00436 #ifdef EAI_AGAIN
00437 #define EVUTIL_EAI_AGAIN EAI_AGAIN
00438 #else
00439 #define EVUTIL_EAI_AGAIN -902
00440 #endif
00441 #ifdef EAI_BADFLAGS
00442 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
00443 #else
00444 #define EVUTIL_EAI_BADFLAGS -903
00445 #endif
00446 #ifdef EAI_FAIL
00447 #define EVUTIL_EAI_FAIL EAI_FAIL
00448 #else
00449 #define EVUTIL_EAI_FAIL -904
00450 #endif
00451 #ifdef EAI_FAMILY
00452 #define EVUTIL_EAI_FAMILY EAI_FAMILY
00453 #else
00454 #define EVUTIL_EAI_FAMILY -905
00455 #endif
00456 #ifdef EAI_MEMORY
00457 #define EVUTIL_EAI_MEMORY EAI_MEMORY
00458 #else
00459 #define EVUTIL_EAI_MEMORY -906
00460 #endif
00461 /* This test is a bit complicated, since some MS SDKs decide to
00462  * remove NODATA or redefine it to be the same as NONAME, in a
00463  * fun interpretation of RFC 2553 and RFC 3493. */
00464 #if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
00465 #define EVUTIL_EAI_NODATA EAI_NODATA
00466 #else
00467 #define EVUTIL_EAI_NODATA -907
00468 #endif
00469 #ifdef EAI_NONAME
00470 #define EVUTIL_EAI_NONAME EAI_NONAME
00471 #else
00472 #define EVUTIL_EAI_NONAME -908
00473 #endif
00474 #ifdef EAI_SERVICE
00475 #define EVUTIL_EAI_SERVICE EAI_SERVICE
00476 #else
00477 #define EVUTIL_EAI_SERVICE -909
00478 #endif
00479 #ifdef EAI_SOCKTYPE
00480 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
00481 #else
00482 #define EVUTIL_EAI_SOCKTYPE -910
00483 #endif
00484 #ifdef EAI_SYSTEM
00485 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
00486 #else
00487 #define EVUTIL_EAI_SYSTEM -911
00488 #endif
00489 
00490 #define EVUTIL_EAI_CANCEL -90001
00491 
00492 #ifdef AI_PASSIVE
00493 #define EVUTIL_AI_PASSIVE AI_PASSIVE
00494 #else
00495 #define EVUTIL_AI_PASSIVE 0x1000
00496 #endif
00497 #ifdef AI_CANONNAME
00498 #define EVUTIL_AI_CANONNAME AI_CANONNAME
00499 #else
00500 #define EVUTIL_AI_CANONNAME 0x2000
00501 #endif
00502 #ifdef AI_NUMERICHOST
00503 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
00504 #else
00505 #define EVUTIL_AI_NUMERICHOST 0x4000
00506 #endif
00507 #ifdef AI_NUMERICSERV
00508 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
00509 #else
00510 #define EVUTIL_AI_NUMERICSERV 0x8000
00511 #endif
00512 #ifdef AI_V4MAPPED
00513 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
00514 #else
00515 #define EVUTIL_AI_V4MAPPED 0x10000
00516 #endif
00517 #ifdef AI_ALL
00518 #define EVUTIL_AI_ALL AI_ALL
00519 #else
00520 #define EVUTIL_AI_ALL 0x20000
00521 #endif
00522 #ifdef AI_ADDRCONFIG
00523 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
00524 #else
00525 #define EVUTIL_AI_ADDRCONFIG 0x40000
00526 #endif
00527 
00528 struct evutil_addrinfo;
00529 /* This function clones getaddrinfo for systems that don't have it.  For full
00530  * details, see RFC 3493, section 6.1.
00531  *
00532  * Limitations:
00533  * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
00534  *   gethostbyname, with their attendant issues.
00535  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
00536  *
00537  * For a nonblocking variant, see evdns_getaddrinfo.
00538  */
00539 int evutil_getaddrinfo(const char *nodename, const char *servname,
00540     const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
00541 
00542 /* Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
00543 void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
00544 
00545 const char *evutil_gai_strerror(int err);
00546 
00547 /* Generate n bytes of secure pseudorandom data, and store them in buf.
00548  *
00549  * By default, Libevent uses an ARC4-based random number generator, seeded
00550  * using the platform's entropy source (/dev/urandom on Unix-like systems;
00551  * CryptGenRandom on Windows).
00552  */
00553 void evutil_secure_rng_get_bytes(void *buf, size_t n);
00554 
00571 int evutil_secure_rng_init(void);
00572 
00587 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
00588 
00589 #ifdef __cplusplus
00590 }
00591 #endif
00592 
00593 #endif /* _EVUTIL_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines