2 * Copyright (c) 2007 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 Common convenience functions for cross-platform portability and
33 related socket manipulations.
41 #include "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>
55 #ifdef _EVENT_HAVE_UINT64_T
56 #define ev_uint64_t uint64_t
57 #define ev_int64_t int64_t
59 #define ev_uint64_t unsigned __int64
60 #define ev_int64_t signed __int64
61 #elif _EVENT_SIZEOF_LONG_LONG == 8
62 #define ev_uint64_t unsigned long long
63 #define ev_int64_t long long
64 #elif _EVENT_SIZEOF_LONG == 8
65 #define ev_uint64_t unsigned long
66 #define ev_int64_t long
68 #error "No way to define ev_uint64_t"
71 #ifdef _EVENT_HAVE_UINT32_T
72 #define ev_uint32_t uint32_t
74 #define ev_uint32_t unsigned int
75 #elif _EVENT_SIZEOF_LONG == 4
76 #define ev_uint32_t unsigned long
77 #elif _EVENT_SIZEOF_INT == 4
78 #define ev_uint32_t unsigned int
80 #error "No way to define ev_uint32_t"
83 #ifdef _EVENT_HAVE_UINT16_T
84 #define ev_uint16_t uint16_t
86 #define ev_uint16_t unsigned short
87 #elif _EVENT_SIZEOF_INT == 2
88 #define ev_uint16_t unsigned int
89 #elif _EVENT_SIZEOF_SHORT == 2
90 #define ev_uint16_t unsigned short
92 #error "No way to define ev_uint16_t"
95 #ifdef _EVENT_HAVE_UINT8_T
96 #define ev_uint8_t uint8_t
98 #define ev_uint8_t unsigned char
101 int evutil_socketpair(int d
, int type
, int protocol
, int sv
[2]);
102 int evutil_make_socket_nonblocking(int sock
);
104 #define EVUTIL_CLOSESOCKET(s) closesocket(s)
106 #define EVUTIL_CLOSESOCKET(s) close(s)
110 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
111 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
112 do { WSASetLastError(errcode); } while (0)
114 #define EVUTIL_SOCKET_ERROR() (errno)
115 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
116 do { errno = (errcode); } while (0)
120 * Manipulation functions for struct timeval
122 #ifdef _EVENT_HAVE_TIMERADD
123 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
124 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
126 #define evutil_timeradd(tvp, uvp, vvp) \
128 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
129 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
130 if ((vvp)->tv_usec >= 1000000) { \
132 (vvp)->tv_usec -= 1000000; \
135 #define evutil_timersub(tvp, uvp, vvp) \
137 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
138 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
139 if ((vvp)->tv_usec < 0) { \
141 (vvp)->tv_usec += 1000000; \
144 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */
146 #ifdef _EVENT_HAVE_TIMERCLEAR
147 #define evutil_timerclear(tvp) timerclear(tvp)
149 #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
152 #define evutil_timercmp(tvp, uvp, cmp) \
153 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
154 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
155 ((tvp)->tv_sec cmp (uvp)->tv_sec))
157 #ifdef _EVENT_HAVE_TIMERISSET
158 #define evutil_timerisset(tvp) timerisset(tvp)
160 #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
164 /* big-int related functions */
165 ev_int64_t
evutil_strtoll(const char *s
, char **endptr
, int base
);
168 #ifdef _EVENT_HAVE_GETTIMEOFDAY
169 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
172 int evutil_gettimeofday(struct timeval
*tv
, struct timezone
*tz
);
175 int evutil_snprintf(char *buf
, size_t buflen
, const char *format
, ...)
177 __attribute__((format(printf
, 3, 4)))
180 int evutil_vsnprintf(char *buf
, size_t buflen
, const char *format
, va_list ap
);
186 #endif /* _EVUTIL_H_ */