00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _EVENT2_UTIL_H_
00027 #define _EVENT2_UTIL_H_
00028
00036 #ifdef __cplusplus
00037 extern "C" {
00038 #endif
00039
00040 #include <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 #include <stdarg.h>
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #ifdef _EVENT_HAVE_UINT64_T
00070 #define ev_uint64_t uint64_t
00071 #define ev_int64_t int64_t
00072 #elif defined(WIN32)
00073 #define ev_uint64_t unsigned __int64
00074 #define ev_int64_t signed __int64
00075 #elif _EVENT_SIZEOF_LONG_LONG == 8
00076 #define ev_uint64_t unsigned long long
00077 #define ev_int64_t long long
00078 #elif _EVENT_SIZEOF_LONG == 8
00079 #define ev_uint64_t unsigned long
00080 #define ev_int64_t long
00081 #else
00082 #error "No way to define ev_uint64_t"
00083 #endif
00084
00085 #ifdef _EVENT_HAVE_UINT32_T
00086 #define ev_uint32_t uint32_t
00087 #elif defined(WIN32)
00088 #define ev_uint32_t unsigned int
00089 #elif _EVENT_SIZEOF_LONG == 4
00090 #define ev_uint32_t unsigned long
00091 #elif _EVENT_SIZEOF_INT == 4
00092 #define ev_uint32_t unsigned int
00093 #else
00094 #error "No way to define ev_uint32_t"
00095 #endif
00096
00097 #ifdef _EVENT_HAVE_UINT16_T
00098 #define ev_uint16_t uint16_t
00099 #elif defined(WIN32)
00100 #define ev_uint16_t unsigned short
00101 #elif _EVENT_SIZEOF_INT == 2
00102 #define ev_uint16_t unsigned int
00103 #elif _EVENT_SIZEOF_SHORT == 2
00104 #define ev_uint16_t unsigned short
00105 #else
00106 #error "No way to define ev_uint16_t"
00107 #endif
00108
00109 #ifdef _EVENT_HAVE_UINT8_T
00110 #define ev_uint8_t uint8_t
00111 #else
00112 #define ev_uint8_t unsigned char
00113 #endif
00114
00115 #ifdef WIN32
00116
00118 #define evutil_socket_t intptr_t
00119 #else
00120 #define evutil_socket_t int
00121 #endif
00122
00132 int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
00138 int evutil_make_socket_nonblocking(evutil_socket_t sock);
00139
00147 int evutil_make_listen_socket_reuseable(evutil_socket_t);
00148
00149 #ifdef WIN32
00150
00152 #define EVUTIL_CLOSESOCKET(s) closesocket(s)
00153 #else
00154
00156 #define EVUTIL_CLOSESOCKET(s) close(s)
00157 #endif
00158
00159
00160
00161
00162
00163
00164
00165 #ifdef WIN32
00166
00167 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
00168
00169 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
00170 do { WSASetLastError(errcode); } while (0)
00171
00172 int evutil_socket_geterror(evutil_socket_t sock);
00174 const char *evutil_socket_error_to_string(int errcode);
00175 #else
00176 #define EVUTIL_SOCKET_ERROR() (errno)
00177 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
00178 do { errno = (errcode); } while (0)
00179 #define evutil_socket_geterror(sock) (errno)
00180 #define evutil_socket_error_to_string(errcode) (strerror(errcode))
00181 #endif
00182
00183
00184
00185
00186
00187 #ifdef _EVENT_HAVE_TIMERADD
00188 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
00189 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
00190 #else
00191 #define evutil_timeradd(tvp, uvp, vvp) \
00192 do { \
00193 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
00194 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
00195 if ((vvp)->tv_usec >= 1000000) { \
00196 (vvp)->tv_sec++; \
00197 (vvp)->tv_usec -= 1000000; \
00198 } \
00199 } while (0)
00200 #define evutil_timersub(tvp, uvp, vvp) \
00201 do { \
00202 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
00203 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
00204 if ((vvp)->tv_usec < 0) { \
00205 (vvp)->tv_sec--; \
00206 (vvp)->tv_usec += 1000000; \
00207 } \
00208 } while (0)
00209 #endif
00210
00211 #ifdef _EVENT_HAVE_TIMERCLEAR
00212 #define evutil_timerclear(tvp) timerclear(tvp)
00213 #else
00214 #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
00215 #endif
00216
00219 #define evutil_timercmp(tvp, uvp, cmp) \
00220 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
00221 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
00222 ((tvp)->tv_sec cmp (uvp)->tv_sec))
00223
00224 #ifdef _EVENT_HAVE_TIMERISSET
00225 #define evutil_timerisset(tvp) timerisset(tvp)
00226 #else
00227 #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
00228 #endif
00229
00230
00231 #ifdef offsetof
00232 #define evutil_offsetof(type, field) offsetof(type, field)
00233 #else
00234 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
00235 #endif
00236
00237
00239 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
00240
00241
00242 #ifdef _EVENT_HAVE_GETTIMEOFDAY
00243 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
00244 #else
00245 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
00246 #endif
00247
00248 #ifdef __GNUC__
00249
00252 #define EVUTIL_CHECK_FMT(a,b) __attribute__((format(printf, a, b)))
00253 #else
00254 #define EVUTIL_CHECK_FMT(a,b)
00255 #endif
00256
00260 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
00261 EVUTIL_CHECK_FMT(3,4);
00262 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap);
00263
00265 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
00267 int evutil_inet_pton(int af, const char *src, void *dst);
00268 struct sockaddr;
00269
00290 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
00291
00292
00293 #ifdef __cplusplus
00294 }
00295 #endif
00296
00297 #endif