event2/util.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007-2012 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 /* Some openbsd autoconf versions get the name of this macro wrong. */
00073 #if defined(_EVENT_SIZEOF_VOID__) && !defined(_EVENT_SIZEOF_VOID_P)
00074 #define _EVENT_SIZEOF_VOID_P _EVENT_SIZEOF_VOID__
00075 #endif
00076 
00106 #ifdef _EVENT_HAVE_UINT64_T
00107 #define ev_uint64_t uint64_t
00108 #define ev_int64_t int64_t
00109 #elif defined(WIN32)
00110 #define ev_uint64_t unsigned __int64
00111 #define ev_int64_t signed __int64
00112 #elif _EVENT_SIZEOF_LONG_LONG == 8
00113 #define ev_uint64_t unsigned long long
00114 #define ev_int64_t long long
00115 #elif _EVENT_SIZEOF_LONG == 8
00116 #define ev_uint64_t unsigned long
00117 #define ev_int64_t long
00118 #elif defined(_EVENT_IN_DOXYGEN)
00119 #define ev_uint64_t ...
00120 #define ev_int64_t ...
00121 #else
00122 #error "No way to define ev_uint64_t"
00123 #endif
00124 
00125 #ifdef _EVENT_HAVE_UINT32_T
00126 #define ev_uint32_t uint32_t
00127 #define ev_int32_t int32_t
00128 #elif defined(WIN32)
00129 #define ev_uint32_t unsigned int
00130 #define ev_int32_t signed int
00131 #elif _EVENT_SIZEOF_LONG == 4
00132 #define ev_uint32_t unsigned long
00133 #define ev_int32_t signed long
00134 #elif _EVENT_SIZEOF_INT == 4
00135 #define ev_uint32_t unsigned int
00136 #define ev_int32_t signed int
00137 #elif defined(_EVENT_IN_DOXYGEN)
00138 #define ev_uint32_t ...
00139 #define ev_int32_t ...
00140 #else
00141 #error "No way to define ev_uint32_t"
00142 #endif
00143 
00144 #ifdef _EVENT_HAVE_UINT16_T
00145 #define ev_uint16_t uint16_t
00146 #define ev_int16_t  int16_t
00147 #elif defined(WIN32)
00148 #define ev_uint16_t unsigned short
00149 #define ev_int16_t  signed short
00150 #elif _EVENT_SIZEOF_INT == 2
00151 #define ev_uint16_t unsigned int
00152 #define ev_int16_t  signed int
00153 #elif _EVENT_SIZEOF_SHORT == 2
00154 #define ev_uint16_t unsigned short
00155 #define ev_int16_t  signed short
00156 #elif defined(_EVENT_IN_DOXYGEN)
00157 #define ev_uint16_t ...
00158 #define ev_int16_t ...
00159 #else
00160 #error "No way to define ev_uint16_t"
00161 #endif
00162 
00163 #ifdef _EVENT_HAVE_UINT8_T
00164 #define ev_uint8_t uint8_t
00165 #define ev_int8_t int8_t
00166 #elif defined(_EVENT_IN_DOXYGEN)
00167 #define ev_uint8_t ...
00168 #define ev_int8_t ...
00169 #else
00170 #define ev_uint8_t unsigned char
00171 #define ev_int8_t signed char
00172 #endif
00173 
00174 #ifdef _EVENT_HAVE_UINTPTR_T
00175 #define ev_uintptr_t uintptr_t
00176 #define ev_intptr_t intptr_t
00177 #elif _EVENT_SIZEOF_VOID_P <= 4
00178 #define ev_uintptr_t ev_uint32_t
00179 #define ev_intptr_t ev_int32_t
00180 #elif _EVENT_SIZEOF_VOID_P <= 8
00181 #define ev_uintptr_t ev_uint64_t
00182 #define ev_intptr_t ev_int64_t
00183 #elif defined(_EVENT_IN_DOXYGEN)
00184 #define ev_uintptr_t ...
00185 #define ev_intptr_t ...
00186 #else
00187 #error "No way to define ev_uintptr_t"
00188 #endif
00189 
00190 #ifdef _EVENT_ssize_t
00191 #define ev_ssize_t _EVENT_ssize_t
00192 #else
00193 #define ev_ssize_t ssize_t
00194 #endif
00195 
00196 #ifdef WIN32
00197 #define ev_off_t ev_int64_t
00198 #else
00199 #define ev_off_t off_t
00200 #endif
00201 
00203 /* Limits for integer types.
00204 
00205    We're making two assumptions here:
00206      - The compiler does constant folding properly.
00207      - The platform does signed arithmetic in two's complement.
00208 */
00209 
00218 #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
00219 #define EV_INT64_MAX  ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
00220 #define EV_INT64_MIN  ((-EV_INT64_MAX) - 1)
00221 #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
00222 #define EV_INT32_MAX  ((ev_int32_t) 0x7fffffffL)
00223 #define EV_INT32_MIN  ((-EV_INT32_MAX) - 1)
00224 #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
00225 #define EV_INT16_MAX  ((ev_int16_t) 0x7fffL)
00226 #define EV_INT16_MIN  ((-EV_INT16_MAX) - 1)
00227 #define EV_UINT8_MAX  255
00228 #define EV_INT8_MAX   127
00229 #define EV_INT8_MIN   ((-EV_INT8_MAX) - 1)
00230 
00237 #if _EVENT_SIZEOF_SIZE_T == 8
00238 #define EV_SIZE_MAX EV_UINT64_MAX
00239 #define EV_SSIZE_MAX EV_INT64_MAX
00240 #elif _EVENT_SIZEOF_SIZE_T == 4
00241 #define EV_SIZE_MAX EV_UINT32_MAX
00242 #define EV_SSIZE_MAX EV_INT32_MAX
00243 #elif defined(_EVENT_IN_DOXYGEN)
00244 #define EV_SIZE_MAX ...
00245 #define EV_SSIZE_MAX ...
00246 #else
00247 #error "No way to define SIZE_MAX"
00248 #endif
00249 
00250 #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
00251 
00253 #ifdef WIN32
00254 #define ev_socklen_t int
00255 #elif defined(_EVENT_socklen_t)
00256 #define ev_socklen_t _EVENT_socklen_t
00257 #else
00258 #define ev_socklen_t socklen_t
00259 #endif
00260 
00261 #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
00262 #if !defined(_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
00263  && !defined(ss_family)
00264 #define ss_family __ss_family
00265 #endif
00266 #endif
00267 
00271 #ifdef WIN32
00272 #define evutil_socket_t intptr_t
00273 #else
00274 #define evutil_socket_t int
00275 #endif
00276 
00288 int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
00294 int evutil_make_socket_nonblocking(evutil_socket_t sock);
00295 
00307 int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
00308 
00315 int evutil_make_socket_closeonexec(evutil_socket_t sock);
00316 
00323 int evutil_closesocket(evutil_socket_t sock);
00324 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
00325 
00326 
00327 #ifdef WIN32
00328 
00329 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
00330 
00331 #define EVUTIL_SET_SOCKET_ERROR(errcode)                \
00332         do { WSASetLastError(errcode); } while (0)
00333 
00334 int evutil_socket_geterror(evutil_socket_t sock);
00336 const char *evutil_socket_error_to_string(int errcode);
00337 #elif defined(_EVENT_IN_DOXYGEN)
00338 
00353 #define EVUTIL_SOCKET_ERROR() ...
00354 
00355 #define EVUTIL_SET_SOCKET_ERROR(errcode) ...
00356 
00357 #define evutil_socket_geterror(sock) ...
00358 
00359 #define evutil_socket_error_to_string(errcode) ...
00360 
00361 #else
00362 #define EVUTIL_SOCKET_ERROR() (errno)
00363 #define EVUTIL_SET_SOCKET_ERROR(errcode)                \
00364                 do { errno = (errcode); } while (0)
00365 #define evutil_socket_geterror(sock) (errno)
00366 #define evutil_socket_error_to_string(errcode) (strerror(errcode))
00367 #endif
00368 
00369 
00378 #ifdef _EVENT_HAVE_TIMERADD
00379 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
00380 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
00381 #else
00382 #define evutil_timeradd(tvp, uvp, vvp)                                  \
00383         do {                                                            \
00384                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
00385                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
00386                 if ((vvp)->tv_usec >= 1000000) {                        \
00387                         (vvp)->tv_sec++;                                \
00388                         (vvp)->tv_usec -= 1000000;                      \
00389                 }                                                       \
00390         } while (0)
00391 #define evutil_timersub(tvp, uvp, vvp)                                  \
00392         do {                                                            \
00393                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
00394                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
00395                 if ((vvp)->tv_usec < 0) {                               \
00396                         (vvp)->tv_sec--;                                \
00397                         (vvp)->tv_usec += 1000000;                      \
00398                 }                                                       \
00399         } while (0)
00400 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */
00401 
00402 #ifdef _EVENT_HAVE_TIMERCLEAR
00403 #define evutil_timerclear(tvp) timerclear(tvp)
00404 #else
00405 #define evutil_timerclear(tvp)  (tvp)->tv_sec = (tvp)->tv_usec = 0
00406 #endif
00407 
00411 #define evutil_timercmp(tvp, uvp, cmp)                                  \
00412         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
00413          ((tvp)->tv_usec cmp (uvp)->tv_usec) :                          \
00414          ((tvp)->tv_sec cmp (uvp)->tv_sec))
00415 
00416 #ifdef _EVENT_HAVE_TIMERISSET
00417 #define evutil_timerisset(tvp) timerisset(tvp)
00418 #else
00419 #define evutil_timerisset(tvp)  ((tvp)->tv_sec || (tvp)->tv_usec)
00420 #endif
00421 
00423 #ifdef offsetof
00424 #define evutil_offsetof(type, field) offsetof(type, field)
00425 #else
00426 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
00427 #endif
00428 
00429 /* big-int related functions */
00431 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
00432 
00434 #ifdef _EVENT_HAVE_GETTIMEOFDAY
00435 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
00436 #else
00437 struct timezone;
00438 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
00439 #endif
00440 
00444 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
00445 #ifdef __GNUC__
00446         __attribute__((format(printf, 3, 4)))
00447 #endif
00448 ;
00452 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
00453 #ifdef __GNUC__
00454         __attribute__((format(printf, 3, 0)))
00455 #endif
00456 ;
00457 
00459 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
00461 int evutil_inet_pton(int af, const char *src, void *dst);
00462 struct sockaddr;
00463 
00484 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
00485 
00491 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
00492     int include_port);
00493 
00497 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
00501 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
00502 
00503 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
00504  * if this system has no getaddrinfo(). */
00505 #ifdef _EVENT_HAVE_STRUCT_ADDRINFO
00506 #define evutil_addrinfo addrinfo
00507 #else
00508 
00513 struct evutil_addrinfo {
00514         int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
00515         int     ai_family;    /* PF_xxx */
00516         int     ai_socktype;  /* SOCK_xxx */
00517         int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
00518         size_t  ai_addrlen;   /* length of ai_addr */
00519         char   *ai_canonname; /* canonical name for nodename */
00520         struct sockaddr  *ai_addr; /* binary address */
00521         struct evutil_addrinfo  *ai_next; /* next structure in linked list */
00522 };
00523 #endif
00524 
00531 #ifdef EAI_ADDRFAMILY
00532 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
00533 #else
00534 #define EVUTIL_EAI_ADDRFAMILY -901
00535 #endif
00536 #ifdef EAI_AGAIN
00537 #define EVUTIL_EAI_AGAIN EAI_AGAIN
00538 #else
00539 #define EVUTIL_EAI_AGAIN -902
00540 #endif
00541 #ifdef EAI_BADFLAGS
00542 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
00543 #else
00544 #define EVUTIL_EAI_BADFLAGS -903
00545 #endif
00546 #ifdef EAI_FAIL
00547 #define EVUTIL_EAI_FAIL EAI_FAIL
00548 #else
00549 #define EVUTIL_EAI_FAIL -904
00550 #endif
00551 #ifdef EAI_FAMILY
00552 #define EVUTIL_EAI_FAMILY EAI_FAMILY
00553 #else
00554 #define EVUTIL_EAI_FAMILY -905
00555 #endif
00556 #ifdef EAI_MEMORY
00557 #define EVUTIL_EAI_MEMORY EAI_MEMORY
00558 #else
00559 #define EVUTIL_EAI_MEMORY -906
00560 #endif
00561 /* This test is a bit complicated, since some MS SDKs decide to
00562  * remove NODATA or redefine it to be the same as NONAME, in a
00563  * fun interpretation of RFC 2553 and RFC 3493. */
00564 #if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
00565 #define EVUTIL_EAI_NODATA EAI_NODATA
00566 #else
00567 #define EVUTIL_EAI_NODATA -907
00568 #endif
00569 #ifdef EAI_NONAME
00570 #define EVUTIL_EAI_NONAME EAI_NONAME
00571 #else
00572 #define EVUTIL_EAI_NONAME -908
00573 #endif
00574 #ifdef EAI_SERVICE
00575 #define EVUTIL_EAI_SERVICE EAI_SERVICE
00576 #else
00577 #define EVUTIL_EAI_SERVICE -909
00578 #endif
00579 #ifdef EAI_SOCKTYPE
00580 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
00581 #else
00582 #define EVUTIL_EAI_SOCKTYPE -910
00583 #endif
00584 #ifdef EAI_SYSTEM
00585 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
00586 #else
00587 #define EVUTIL_EAI_SYSTEM -911
00588 #endif
00589 
00590 #define EVUTIL_EAI_CANCEL -90001
00591 
00592 #ifdef AI_PASSIVE
00593 #define EVUTIL_AI_PASSIVE AI_PASSIVE
00594 #else
00595 #define EVUTIL_AI_PASSIVE 0x1000
00596 #endif
00597 #ifdef AI_CANONNAME
00598 #define EVUTIL_AI_CANONNAME AI_CANONNAME
00599 #else
00600 #define EVUTIL_AI_CANONNAME 0x2000
00601 #endif
00602 #ifdef AI_NUMERICHOST
00603 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
00604 #else
00605 #define EVUTIL_AI_NUMERICHOST 0x4000
00606 #endif
00607 #ifdef AI_NUMERICSERV
00608 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
00609 #else
00610 #define EVUTIL_AI_NUMERICSERV 0x8000
00611 #endif
00612 #ifdef AI_V4MAPPED
00613 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
00614 #else
00615 #define EVUTIL_AI_V4MAPPED 0x10000
00616 #endif
00617 #ifdef AI_ALL
00618 #define EVUTIL_AI_ALL AI_ALL
00619 #else
00620 #define EVUTIL_AI_ALL 0x20000
00621 #endif
00622 #ifdef AI_ADDRCONFIG
00623 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
00624 #else
00625 #define EVUTIL_AI_ADDRCONFIG 0x40000
00626 #endif
00627 
00629 struct evutil_addrinfo;
00641 int evutil_getaddrinfo(const char *nodename, const char *servname,
00642     const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
00643 
00645 void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
00646 
00647 const char *evutil_gai_strerror(int err);
00648 
00658 void evutil_secure_rng_get_bytes(void *buf, size_t n);
00659 
00676 int evutil_secure_rng_init(void);
00677 
00691 int evutil_secure_rng_set_urandom_device_file(char *fname);
00692 
00707 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
00708 
00709 #ifdef __cplusplus
00710 }
00711 #endif
00712 
00713 #endif /* _EVUTIL_H_ */

Generated on 15 Apr 2015 for libevent by  doxygen 1.4.7