libevent
Main Page
Related Pages
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Pages
event2
util.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
6
* are met:
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
12
* 3. The name of the author may not be used to endorse or promote products
13
* derived from this software without specific prior written permission.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
*/
26
#ifndef _EVENT2_UTIL_H_
27
#define _EVENT2_UTIL_H_
28
36
#ifdef __cplusplus
37
extern
"C"
{
38
#endif
39
40
#include <event2/event-config.h>
41
#ifdef _EVENT_HAVE_SYS_TIME_H
42
#include <sys/time.h>
43
#endif
44
#ifdef _EVENT_HAVE_STDINT_H
45
#include <stdint.h>
46
#elif defined(_EVENT_HAVE_INTTYPES_H)
47
#include <inttypes.h>
48
#endif
49
#ifdef _EVENT_HAVE_SYS_TYPES_H
50
#include <sys/types.h>
51
#endif
52
#ifdef _EVENT_HAVE_STDDEF_H
53
#include <stddef.h>
54
#endif
55
#ifdef _MSC_VER
56
#include <BaseTsd.h>
57
#endif
58
#include <stdarg.h>
59
#ifdef _EVENT_HAVE_NETDB_H
60
#if !defined(_GNU_SOURCE)
61
#define _GNU_SOURCE
62
#endif
63
#include <netdb.h>
64
#endif
65
66
#ifdef WIN32
67
#include <winsock2.h>
68
#else
69
#include <sys/socket.h>
70
#endif
71
72
/* Some openbsd autoconf versions get the name of this macro wrong. */
73
#if defined(_EVENT_SIZEOF_VOID__) && !defined(_EVENT_SIZEOF_VOID_P)
74
#define _EVENT_SIZEOF_VOID_P _EVENT_SIZEOF_VOID__
75
#endif
76
106
#ifdef _EVENT_HAVE_UINT64_T
107
#define ev_uint64_t uint64_t
108
#define ev_int64_t int64_t
109
#elif defined(WIN32)
110
#define ev_uint64_t unsigned __int64
111
#define ev_int64_t signed __int64
112
#elif _EVENT_SIZEOF_LONG_LONG == 8
113
#define ev_uint64_t unsigned long long
114
#define ev_int64_t long long
115
#elif _EVENT_SIZEOF_LONG == 8
116
#define ev_uint64_t unsigned long
117
#define ev_int64_t long
118
#elif defined(_EVENT_IN_DOXYGEN)
119
#define ev_uint64_t ...
120
#define ev_int64_t ...
121
#else
122
#error "No way to define ev_uint64_t"
123
#endif
124
125
#ifdef _EVENT_HAVE_UINT32_T
126
#define ev_uint32_t uint32_t
127
#define ev_int32_t int32_t
128
#elif defined(WIN32)
129
#define ev_uint32_t unsigned int
130
#define ev_int32_t signed int
131
#elif _EVENT_SIZEOF_LONG == 4
132
#define ev_uint32_t unsigned long
133
#define ev_int32_t signed long
134
#elif _EVENT_SIZEOF_INT == 4
135
#define ev_uint32_t unsigned int
136
#define ev_int32_t signed int
137
#elif defined(_EVENT_IN_DOXYGEN)
138
#define ev_uint32_t ...
139
#define ev_int32_t ...
140
#else
141
#error "No way to define ev_uint32_t"
142
#endif
143
144
#ifdef _EVENT_HAVE_UINT16_T
145
#define ev_uint16_t uint16_t
146
#define ev_int16_t int16_t
147
#elif defined(WIN32)
148
#define ev_uint16_t unsigned short
149
#define ev_int16_t signed short
150
#elif _EVENT_SIZEOF_INT == 2
151
#define ev_uint16_t unsigned int
152
#define ev_int16_t signed int
153
#elif _EVENT_SIZEOF_SHORT == 2
154
#define ev_uint16_t unsigned short
155
#define ev_int16_t signed short
156
#elif defined(_EVENT_IN_DOXYGEN)
157
#define ev_uint16_t ...
158
#define ev_int16_t ...
159
#else
160
#error "No way to define ev_uint16_t"
161
#endif
162
163
#ifdef _EVENT_HAVE_UINT8_T
164
#define ev_uint8_t uint8_t
165
#define ev_int8_t int8_t
166
#elif defined(_EVENT_IN_DOXYGEN)
167
#define ev_uint8_t ...
168
#define ev_int8_t ...
169
#else
170
#define ev_uint8_t unsigned char
171
#define ev_int8_t signed char
172
#endif
173
174
#ifdef _EVENT_HAVE_UINTPTR_T
175
#define ev_uintptr_t uintptr_t
176
#define ev_intptr_t intptr_t
177
#elif _EVENT_SIZEOF_VOID_P <= 4
178
#define ev_uintptr_t ev_uint32_t
179
#define ev_intptr_t ev_int32_t
180
#elif _EVENT_SIZEOF_VOID_P <= 8
181
#define ev_uintptr_t ev_uint64_t
182
#define ev_intptr_t ev_int64_t
183
#elif defined(_EVENT_IN_DOXYGEN)
184
#define ev_uintptr_t ...
185
#define ev_intptr_t ...
186
#else
187
#error "No way to define ev_uintptr_t"
188
#endif
189
190
#ifdef _EVENT_ssize_t
191
#define ev_ssize_t _EVENT_ssize_t
192
#else
193
#define ev_ssize_t ssize_t
194
#endif
195
196
#ifdef WIN32
197
#define ev_off_t ev_int64_t
198
#else
199
#define ev_off_t off_t
200
#endif
201
203
/* Limits for integer types.
204
205
We're making two assumptions here:
206
- The compiler does constant folding properly.
207
- The platform does signed arithmetic in two's complement.
208
*/
209
218
#define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
219
#define EV_INT64_MAX ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
220
#define EV_INT64_MIN ((-EV_INT64_MAX) - 1)
221
#define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
222
#define EV_INT32_MAX ((ev_int32_t) 0x7fffffffL)
223
#define EV_INT32_MIN ((-EV_INT32_MAX) - 1)
224
#define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
225
#define EV_INT16_MAX ((ev_int16_t) 0x7fffL)
226
#define EV_INT16_MIN ((-EV_INT16_MAX) - 1)
227
#define EV_UINT8_MAX 255
228
#define EV_INT8_MAX 127
229
#define EV_INT8_MIN ((-EV_INT8_MAX) - 1)
230
237
#if _EVENT_SIZEOF_SIZE_T == 8
238
#define EV_SIZE_MAX EV_UINT64_MAX
239
#define EV_SSIZE_MAX EV_INT64_MAX
240
#elif _EVENT_SIZEOF_SIZE_T == 4
241
#define EV_SIZE_MAX EV_UINT32_MAX
242
#define EV_SSIZE_MAX EV_INT32_MAX
243
#elif defined(_EVENT_IN_DOXYGEN)
244
#define EV_SIZE_MAX ...
245
#define EV_SSIZE_MAX ...
246
#else
247
#error "No way to define SIZE_MAX"
248
#endif
249
250
#define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
251
253
#ifdef WIN32
254
#define ev_socklen_t int
255
#elif defined(_EVENT_socklen_t)
256
#define ev_socklen_t _EVENT_socklen_t
257
#else
258
#define ev_socklen_t socklen_t
259
#endif
260
261
#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
262
#if !defined(_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
263
&& !defined(ss_family)
264
#define ss_family __ss_family
265
#endif
266
#endif
267
271
#ifdef WIN32
272
#define evutil_socket_t intptr_t
273
#else
274
#define evutil_socket_t int
275
#endif
276
288
int
evutil_socketpair
(
int
d,
int
type,
int
protocol,
evutil_socket_t
sv[2]);
294
int
evutil_make_socket_nonblocking
(
evutil_socket_t
sock);
295
307
int
evutil_make_listen_socket_reuseable
(
evutil_socket_t
sock);
308
315
int
evutil_make_socket_closeonexec
(
evutil_socket_t
sock);
316
323
int
evutil_closesocket
(
evutil_socket_t
sock);
324
#define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
325
326
327
#ifdef WIN32
328
329
#define EVUTIL_SOCKET_ERROR() WSAGetLastError()
330
331
#define EVUTIL_SET_SOCKET_ERROR(errcode) \
332
do { WSASetLastError(errcode); } while (0)
333
334
int
evutil_socket_geterror
(
evutil_socket_t
sock);
336
const
char
*
evutil_socket_error_to_string
(
int
errcode);
337
#elif defined(_EVENT_IN_DOXYGEN)
338
353
#define EVUTIL_SOCKET_ERROR() ...
354
355
#define EVUTIL_SET_SOCKET_ERROR(errcode) ...
356
357
#define evutil_socket_geterror(sock) ...
358
359
#define evutil_socket_error_to_string(errcode) ...
360
361
#else
362
#define EVUTIL_SOCKET_ERROR() (errno)
363
#define EVUTIL_SET_SOCKET_ERROR(errcode) \
364
do { errno = (errcode); } while (0)
365
#define evutil_socket_geterror(sock) (errno)
366
#define evutil_socket_error_to_string(errcode) (strerror(errcode))
367
#endif
368
369
378
#ifdef _EVENT_HAVE_TIMERADD
379
#define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
380
#define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
381
#else
382
#define evutil_timeradd(tvp, uvp, vvp) \
383
do { \
384
(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
385
(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
386
if ((vvp)->tv_usec >= 1000000) { \
387
(vvp)->tv_sec++; \
388
(vvp)->tv_usec -= 1000000; \
389
} \
390
} while (0)
391
#define evutil_timersub(tvp, uvp, vvp) \
392
do { \
393
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
394
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
395
if ((vvp)->tv_usec < 0) { \
396
(vvp)->tv_sec--; \
397
(vvp)->tv_usec += 1000000; \
398
} \
399
} while (0)
400
#endif
/* !_EVENT_HAVE_HAVE_TIMERADD */
401
402
#ifdef _EVENT_HAVE_TIMERCLEAR
403
#define evutil_timerclear(tvp) timerclear(tvp)
404
#else
405
#define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
406
#endif
407
411
#define evutil_timercmp(tvp, uvp, cmp) \
412
(((tvp)->tv_sec == (uvp)->tv_sec) ? \
413
((tvp)->tv_usec cmp (uvp)->tv_usec) : \
414
((tvp)->tv_sec cmp (uvp)->tv_sec))
415
416
#ifdef _EVENT_HAVE_TIMERISSET
417
#define evutil_timerisset(tvp) timerisset(tvp)
418
#else
419
#define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
420
#endif
421
423
#ifdef offsetof
424
#define evutil_offsetof(type, field) offsetof(type, field)
425
#else
426
#define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
427
#endif
428
429
/* big-int related functions */
431
ev_int64_t
evutil_strtoll
(
const
char
*s,
char
**endptr,
int
base);
432
434
#ifdef _EVENT_HAVE_GETTIMEOFDAY
435
#define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
436
#else
437
struct
timezone;
438
int
evutil_gettimeofday(
struct
timeval *tv,
struct
timezone *tz);
439
#endif
440
444
int
evutil_snprintf
(
char
*buf,
size_t
buflen,
const
char
*format, ...)
445
#ifdef __GNUC__
446
__attribute__((format(printf, 3, 4)))
447
#endif
448
;
452
int
evutil_vsnprintf
(
char
*buf,
size_t
buflen,
const
char
*format, va_list ap);
453
455
const
char
*
evutil_inet_ntop
(
int
af,
const
void
*src,
char
*dst,
size_t
len);
457
int
evutil_inet_pton
(
int
af,
const
char
*src,
void
*dst);
458
struct
sockaddr;
459
480
int
evutil_parse_sockaddr_port
(
const
char
*str,
struct
sockaddr *out,
int
*outlen);
481
487
int
evutil_sockaddr_cmp
(
const
struct
sockaddr *sa1,
const
struct
sockaddr *sa2,
488
int
include_port);
489
493
int
evutil_ascii_strcasecmp
(
const
char
*str1,
const
char
*str2);
497
int
evutil_ascii_strncasecmp
(
const
char
*str1,
const
char
*str2,
size_t
n);
498
499
/* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
500
* if this system has no getaddrinfo(). */
501
#ifdef _EVENT_HAVE_STRUCT_ADDRINFO
502
#define evutil_addrinfo addrinfo
503
#else
504
509
struct
evutil_addrinfo
{
510
int
ai_flags;
/* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
511
int
ai_family;
/* PF_xxx */
512
int
ai_socktype;
/* SOCK_xxx */
513
int
ai_protocol;
/* 0 or IPPROTO_xxx for IPv4 and IPv6 */
514
size_t
ai_addrlen;
/* length of ai_addr */
515
char
*ai_canonname;
/* canonical name for nodename */
516
struct
sockaddr *ai_addr;
/* binary address */
517
struct
evutil_addrinfo
*ai_next;
/* next structure in linked list */
518
};
519
#endif
520
527
#ifdef EAI_ADDRFAMILY
528
#define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
529
#else
530
#define EVUTIL_EAI_ADDRFAMILY -901
531
#endif
532
#ifdef EAI_AGAIN
533
#define EVUTIL_EAI_AGAIN EAI_AGAIN
534
#else
535
#define EVUTIL_EAI_AGAIN -902
536
#endif
537
#ifdef EAI_BADFLAGS
538
#define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
539
#else
540
#define EVUTIL_EAI_BADFLAGS -903
541
#endif
542
#ifdef EAI_FAIL
543
#define EVUTIL_EAI_FAIL EAI_FAIL
544
#else
545
#define EVUTIL_EAI_FAIL -904
546
#endif
547
#ifdef EAI_FAMILY
548
#define EVUTIL_EAI_FAMILY EAI_FAMILY
549
#else
550
#define EVUTIL_EAI_FAMILY -905
551
#endif
552
#ifdef EAI_MEMORY
553
#define EVUTIL_EAI_MEMORY EAI_MEMORY
554
#else
555
#define EVUTIL_EAI_MEMORY -906
556
#endif
557
/* This test is a bit complicated, since some MS SDKs decide to
558
* remove NODATA or redefine it to be the same as NONAME, in a
559
* fun interpretation of RFC 2553 and RFC 3493. */
560
#if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
561
#define EVUTIL_EAI_NODATA EAI_NODATA
562
#else
563
#define EVUTIL_EAI_NODATA -907
564
#endif
565
#ifdef EAI_NONAME
566
#define EVUTIL_EAI_NONAME EAI_NONAME
567
#else
568
#define EVUTIL_EAI_NONAME -908
569
#endif
570
#ifdef EAI_SERVICE
571
#define EVUTIL_EAI_SERVICE EAI_SERVICE
572
#else
573
#define EVUTIL_EAI_SERVICE -909
574
#endif
575
#ifdef EAI_SOCKTYPE
576
#define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
577
#else
578
#define EVUTIL_EAI_SOCKTYPE -910
579
#endif
580
#ifdef EAI_SYSTEM
581
#define EVUTIL_EAI_SYSTEM EAI_SYSTEM
582
#else
583
#define EVUTIL_EAI_SYSTEM -911
584
#endif
585
586
#define EVUTIL_EAI_CANCEL -90001
587
588
#ifdef AI_PASSIVE
589
#define EVUTIL_AI_PASSIVE AI_PASSIVE
590
#else
591
#define EVUTIL_AI_PASSIVE 0x1000
592
#endif
593
#ifdef AI_CANONNAME
594
#define EVUTIL_AI_CANONNAME AI_CANONNAME
595
#else
596
#define EVUTIL_AI_CANONNAME 0x2000
597
#endif
598
#ifdef AI_NUMERICHOST
599
#define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
600
#else
601
#define EVUTIL_AI_NUMERICHOST 0x4000
602
#endif
603
#ifdef AI_NUMERICSERV
604
#define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
605
#else
606
#define EVUTIL_AI_NUMERICSERV 0x8000
607
#endif
608
#ifdef AI_V4MAPPED
609
#define EVUTIL_AI_V4MAPPED AI_V4MAPPED
610
#else
611
#define EVUTIL_AI_V4MAPPED 0x10000
612
#endif
613
#ifdef AI_ALL
614
#define EVUTIL_AI_ALL AI_ALL
615
#else
616
#define EVUTIL_AI_ALL 0x20000
617
#endif
618
#ifdef AI_ADDRCONFIG
619
#define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
620
#else
621
#define EVUTIL_AI_ADDRCONFIG 0x40000
622
#endif
623
625
struct
evutil_addrinfo
;
637
int
evutil_getaddrinfo
(
const
char
*nodename,
const
char
*servname,
638
const
struct
evutil_addrinfo
*hints_in,
struct
evutil_addrinfo
**res);
639
641
void
evutil_freeaddrinfo
(
struct
evutil_addrinfo
*ai);
642
643
const
char
*evutil_gai_strerror(
int
err);
644
651
void
evutil_secure_rng_get_bytes
(
void
*buf,
size_t
n);
652
669
int
evutil_secure_rng_init
(
void
);
670
685
void
evutil_secure_rng_add_bytes
(
const
char
*dat,
size_t
datlen);
686
687
#ifdef __cplusplus
688
}
689
#endif
690
691
#endif
/* _EVUTIL_H_ */
Generated by
1.8.1.1