OpenMPI  0.1.1
util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2010 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 
29 /** @file event2/util.h
30 
31  Common convenience functions for cross-platform portability and
32  related socket manipulations.
33 
34  */
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**** OMPI CHANGE ****/
41 #include "opal_rename.h"
42 /**** END OMPI CHANGE ****/
43 
44 #include <event2/event-config.h>
45 #ifdef _EVENT_HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
48 #ifdef _EVENT_HAVE_STDINT_H
49 #include <stdint.h>
50 #elif defined(_EVENT_HAVE_INTTYPES_H)
51 #include <inttypes.h>
52 #endif
53 #ifdef _EVENT_HAVE_SYS_TYPES_H
54 #include <sys/types.h>
55 #endif
56 #ifdef _EVENT_HAVE_STDDEF_H
57 #include <stddef.h>
58 #endif
59 #ifdef _MSC_VER
60 #include <BaseTsd.h>
61 #endif
62 #include <stdarg.h>
63 #ifdef _EVENT_HAVE_NETDB_H
64 #if !defined(_GNU_SOURCE)
65 #define _GNU_SOURCE
66 #endif
67 #include <netdb.h>
68 #endif
69 
70 #ifdef WIN32
71 #include <winsock2.h>
72 #else
73 #include <sys/socket.h>
74 #endif
75 
76 /* Some openbsd autoconf versions get the name of this macro wrong. */
77 #if defined(_EVENT_SIZEOF_VOID__) && !defined(_EVENT_SIZEOF_VOID_P)
78 #define _EVENT_SIZEOF_VOID_P _EVENT_SIZEOF_VOID__
79 #endif
80 
81 /**
82  * @name Standard integer types.
83  *
84  * Integer type definitions for types that are supposed to be defined in the
85  * C99-specified stdint.h. Shamefully, some platforms do not include
86  * stdint.h, so we need to replace it. (If you are on a platform like this,
87  * your C headers are now over 10 years out of date. You should bug them to
88  * do something about this.)
89  *
90  * We define:
91  *
92  * <dl>
93  * <dt>ev_uint64_t, ev_uint32_t, ev_uint16_t, ev_uint8_t</dt>
94  * <dd>unsigned integer types of exactly 64, 32, 16, and 8 bits
95  * respectively.</dd>
96  * <dt>ev_int64_t, ev_int32_t, ev_int16_t, ev_int8_t</dt>
97  * <dd>signed integer types of exactly 64, 32, 16, and 8 bits
98  * respectively.</dd>
99  * <dt>ev_uintptr_t, ev_intptr_t</dt>
100  * <dd>unsigned/signed integers large enough
101  * to hold a pointer without loss of bits.</dd>
102  * <dt>ev_ssize_t</dt>
103  * <dd>A signed type of the same size as size_t</dd>
104  * <dt>ev_off_t</dt>
105  * <dd>A signed type typically used to represent offsets within a
106  * (potentially large) file</dd>
107  *
108  * @{
109  */
110 #ifdef _EVENT_HAVE_UINT64_T
111 #define ev_uint64_t uint64_t
112 #define ev_int64_t int64_t
113 #elif defined(WIN32)
114 #define ev_uint64_t unsigned __int64
115 #define ev_int64_t signed __int64
116 #elif _EVENT_SIZEOF_LONG_LONG == 8
117 #define ev_uint64_t unsigned long long
118 #define ev_int64_t long long
119 #elif _EVENT_SIZEOF_LONG == 8
120 #define ev_uint64_t unsigned long
121 #define ev_int64_t long
122 #elif defined(_EVENT_IN_DOXYGEN)
123 #define ev_uint64_t ...
124 #define ev_int64_t ...
125 #else
126 #error "No way to define ev_uint64_t"
127 #endif
128 
129 #ifdef _EVENT_HAVE_UINT32_T
130 #define ev_uint32_t uint32_t
131 #define ev_int32_t int32_t
132 #elif defined(WIN32)
133 #define ev_uint32_t unsigned int
134 #define ev_int32_t signed int
135 #elif _EVENT_SIZEOF_LONG == 4
136 #define ev_uint32_t unsigned long
137 #define ev_int32_t signed long
138 #elif _EVENT_SIZEOF_INT == 4
139 #define ev_uint32_t unsigned int
140 #define ev_int32_t signed int
141 #elif defined(_EVENT_IN_DOXYGEN)
142 #define ev_uint32_t ...
143 #define ev_int32_t ...
144 #else
145 #error "No way to define ev_uint32_t"
146 #endif
147 
148 #ifdef _EVENT_HAVE_UINT16_T
149 #define ev_uint16_t uint16_t
150 #define ev_int16_t int16_t
151 #elif defined(WIN32)
152 #define ev_uint16_t unsigned short
153 #define ev_int16_t signed short
154 #elif _EVENT_SIZEOF_INT == 2
155 #define ev_uint16_t unsigned int
156 #define ev_int16_t signed int
157 #elif _EVENT_SIZEOF_SHORT == 2
158 #define ev_uint16_t unsigned short
159 #define ev_int16_t signed short
160 #elif defined(_EVENT_IN_DOXYGEN)
161 #define ev_uint16_t ...
162 #define ev_int16_t ...
163 #else
164 #error "No way to define ev_uint16_t"
165 #endif
166 
167 #ifdef _EVENT_HAVE_UINT8_T
168 #define ev_uint8_t uint8_t
169 #define ev_int8_t int8_t
170 #elif defined(_EVENT_IN_DOXYGEN)
171 #define ev_uint8_t ...
172 #define ev_int8_t ...
173 #else
174 #define ev_uint8_t unsigned char
175 #define ev_int8_t signed char
176 #endif
177 
178 #ifdef _EVENT_HAVE_UINTPTR_T
179 #define ev_uintptr_t uintptr_t
180 #define ev_intptr_t intptr_t
181 #elif _EVENT_SIZEOF_VOID_P <= 4
182 #define ev_uintptr_t ev_uint32_t
183 #define ev_intptr_t ev_int32_t
184 #elif _EVENT_SIZEOF_VOID_P <= 8
185 #define ev_uintptr_t ev_uint64_t
186 #define ev_intptr_t ev_int64_t
187 #elif defined(_EVENT_IN_DOXYGEN)
188 #define ev_uintptr_t ...
189 #define ev_intptr_t ...
190 #else
191 #error "No way to define ev_uintptr_t"
192 #endif
193 
194 #ifdef _EVENT_ssize_t
195 #define ev_ssize_t _EVENT_ssize_t
196 #else
197 #define ev_ssize_t ssize_t
198 #endif
199 
200 #ifdef WIN32
201 #define ev_off_t ev_int64_t
202 #else
203 #define ev_off_t off_t
204 #endif
205 /**@}*/
206 
207 /* Limits for integer types.
208 
209  We're making two assumptions here:
210  - The compiler does constant folding properly.
211  - The platform does signed arithmetic in two's complement.
212 */
213 
214 /**
215  @name Limits for integer types
216 
217  These macros hold the largest or smallest values possible for the
218  ev_[u]int*_t types.
219 
220  @{
221 */
222 #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
223 #define EV_INT64_MAX ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
224 #define EV_INT64_MIN ((-EV_INT64_MAX) - 1)
225 #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
226 #define EV_INT32_MAX ((ev_int32_t) 0x7fffffffL)
227 #define EV_INT32_MIN ((-EV_INT32_MAX) - 1)
228 #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
229 #define EV_INT16_MAX ((ev_int16_t) 0x7fffL)
230 #define EV_INT16_MIN ((-EV_INT16_MAX) - 1)
231 #define EV_UINT8_MAX 255
232 #define EV_INT8_MAX 127
233 #define EV_INT8_MIN ((-EV_INT8_MAX) - 1)
234 /** @} */
235 
236 /**
237  @name Limits for SIZE_T and SSIZE_T
238 
239  @{
240 */
241 #if _EVENT_SIZEOF_SIZE_T == 8
242 #define EV_SIZE_MAX EV_UINT64_MAX
243 #define EV_SSIZE_MAX EV_INT64_MAX
244 #elif _EVENT_SIZEOF_SIZE_T == 4
245 #define EV_SIZE_MAX EV_UINT32_MAX
246 #define EV_SSIZE_MAX EV_INT32_MAX
247 #elif defined(_EVENT_IN_DOXYGEN)
248 #define EV_SIZE_MAX ...
249 #define EV_SSIZE_MAX ...
250 #else
251 #error "No way to define SIZE_MAX"
252 #endif
253 
254 #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
255 /**@}*/
256 
257 #ifdef WIN32
258 #define ev_socklen_t int
259 #elif defined(_EVENT_socklen_t)
260 #define ev_socklen_t _EVENT_socklen_t
261 #else
262 #define ev_socklen_t socklen_t
263 #endif
264 
265 #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
266 #if !defined(_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
267  && !defined(ss_family)
268 #define ss_family __ss_family
269 #endif
270 #endif
271 
272 /**
273  * A type wide enough to hold the output of "socket()" or "accept()". On
274  * Windows, this is an intptr_t; elsewhere, it is an int. */
275 #ifdef WIN32
276 #define evutil_socket_t intptr_t
277 #else
278 #define evutil_socket_t int
279 #endif
280 
281 /** Create two new sockets that are connected to each other.
282 
283  On Unix, this simply calls socketpair(). On Windows, it uses the
284  loopback network interface on 127.0.0.1, and only
285  AF_INET,SOCK_STREAM are supported.
286 
287  (This may fail on some Windows hosts where firewall software has cleverly
288  decided to keep 127.0.0.1 from talking to itself.)
289 
290  Parameters and return values are as for socketpair()
291 */
292 int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
293 /** Do platform-specific operations as needed to make a socket nonblocking.
294 
295  @param sock The socket to make nonblocking
296  @return 0 on success, -1 on failure
297  */
299 
300 /** Do platform-specific operations to make a listener socket reusable.
301 
302  Specifically, we want to make sure that another program will be able
303  to bind this address right after we've closed the listener.
304 
305  This differs from Windows's interpretation of "reusable", which
306  allows multiple listeners to bind the same address at the same time.
307 
308  @param sock The socket to make reusable
309  @return 0 on success, -1 on failure
310  */
312 
313 /** Do platform-specific operations as needed to close a socket upon a
314  successful execution of one of the exec*() functions.
315 
316  @param sock The socket to be closed
317  @return 0 on success, -1 on failure
318  */
320 
321 /** Do the platform-specific call needed to close a socket returned from
322  socket() or accept().
323 
324  @param sock The socket to be closed
325  @return 0 on success, -1 on failure
326  */
328 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
329 
330 
331 #ifdef WIN32
332 /** Return the most recent socket error. Not idempotent on all platforms. */
333 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
334 /** Replace the most recent socket error with errcode */
335 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
336  do { WSASetLastError(errcode); } while (0)
337 /** Return the most recent socket error to occur on sock. */
338 int evutil_socket_geterror(evutil_socket_t sock);
339 /** Convert a socket error to a string. */
340 const char *evutil_socket_error_to_string(int errcode);
341 #elif defined(_EVENT_IN_DOXYGEN)
342 /**
343  @name Socket error functions
344 
345  These functions are needed for making programs compatible between
346  Windows and Unix-like platforms.
347 
348  You see, Winsock handles socket errors differently from the rest of
349  the world. Elsewhere, a socket error is like any other error and is
350  stored in errno. But winsock functions require you to retrieve the
351  error with a special function, and don't let you use strerror for
352  the error codes. And handling EWOULDBLOCK is ... different.
353 
354  @{
355 */
356 /** Return the most recent socket error. Not idempotent on all platforms. */
357 #define EVUTIL_SOCKET_ERROR() ...
358 /** Replace the most recent socket error with errcode */
359 #define EVUTIL_SET_SOCKET_ERROR(errcode) ...
360 /** Return the most recent socket error to occur on sock. */
361 #define evutil_socket_geterror(sock) ...
362 /** Convert a socket error to a string. */
363 #define evutil_socket_error_to_string(errcode) ...
364 /**@}*/
365 #else
366 #define EVUTIL_SOCKET_ERROR() (errno)
367 #define EVUTIL_SET_SOCKET_ERROR(errcode) \
368  do { errno = (errcode); } while (0)
369 #define evutil_socket_geterror(sock) (errno)
370 #define evutil_socket_error_to_string(errcode) (strerror(errcode))
371 #endif
372 
373 
374 /**
375  * @name Manipulation macros for struct timeval.
376  *
377  * We define replacements
378  * for timeradd, timersub, timerclear, timercmp, and timerisset.
379  *
380  * @{
381  */
382 #ifdef _EVENT_HAVE_TIMERADD
383 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
384 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
385 #else
386 #define evutil_timeradd(tvp, uvp, vvp) \
387  do { \
388  (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
389  (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
390  if ((vvp)->tv_usec >= 1000000) { \
391  (vvp)->tv_sec++; \
392  (vvp)->tv_usec -= 1000000; \
393  } \
394  } while (0)
395 #define evutil_timersub(tvp, uvp, vvp) \
396  do { \
397  (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
398  (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
399  if ((vvp)->tv_usec < 0) { \
400  (vvp)->tv_sec--; \
401  (vvp)->tv_usec += 1000000; \
402  } \
403  } while (0)
404 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */
405 
406 #ifdef _EVENT_HAVE_TIMERCLEAR
407 #define evutil_timerclear(tvp) timerclear(tvp)
408 #else
409 #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
410 #endif
411 /**@}*/
412 
413 /** Return true iff the tvp is related to uvp according to the relational
414  * operator cmp. Recognized values for cmp are ==, <=, <, >=, and >. */
415 #define evutil_timercmp(tvp, uvp, cmp) \
416  (((tvp)->tv_sec == (uvp)->tv_sec) ? \
417  ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
418  ((tvp)->tv_sec cmp (uvp)->tv_sec))
419 
420 #ifdef _EVENT_HAVE_TIMERISSET
421 #define evutil_timerisset(tvp) timerisset(tvp)
422 #else
423 #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
424 #endif
425 
426 /** Replacement for offsetof on platforms that don't define it. */
427 #ifdef offsetof
428 #define evutil_offsetof(type, field) offsetof(type, field)
429 #else
430 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
431 #endif
432 
433 /* big-int related functions */
434 /** Parse a 64-bit value from a string. Arguments are as for strtol. */
435 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
436 
437 /** Replacement for gettimeofday on platforms that lack it. */
438 #ifdef _EVENT_HAVE_GETTIMEOFDAY
439 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
440 #else
441 struct timezone;
442 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
443 #endif
444 
445 /** Replacement for snprintf to get consistent behavior on platforms for
446  which the return value of snprintf does not conform to C99.
447  */
448 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
449 #ifdef __GNUC__
450  __attribute__((format(printf, 3, 4)))
451 #endif
452 ;
453 /** Replacement for vsnprintf to get consistent behavior on platforms for
454  which the return value of snprintf does not conform to C99.
455  */
456 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap);
457 
458 /** Replacement for inet_ntop for platforms which lack it. */
459 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
460 /** Replacement for inet_pton for platforms which lack it. */
461 int evutil_inet_pton(int af, const char *src, void *dst);
462 struct sockaddr;
463 
464 /** Parse an IPv4 or IPv6 address, with optional port, from a string.
465 
466  Recognized formats are:
467  - [IPv6Address]:port
468  - [IPv6Address]
469  - IPv6Address
470  - IPv4Address:port
471  - IPv4Address
472 
473  If no port is specified, the port in the output is set to 0.
474 
475  @param str The string to parse.
476  @param out A struct sockaddr to hold the result. This should probably be
477  a struct sockaddr_storage.
478  @param outlen A pointer to the number of bytes that that 'out' can safely
479  hold. Set to the number of bytes used in 'out' on success.
480  @return -1 if the address is not well-formed, if the port is out of range,
481  or if out is not large enough to hold the result. Otherwise returns
482  0 on success.
483 */
484 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
485 
486 /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
487  * preceeds sa2, or greater than 0 if sa1 follows sa2. If include_port is
488  * true, consider the port as well as the address. Only implemented for
489  * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
490  * the same between Libevent versions. */
491 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
492  int include_port);
493 
494 /** As strcasecmp, but always compares the characters in locale-independent
495  ASCII. That's useful if you're handling data in ASCII-based protocols.
496  */
497 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
498 /** As strncasecmp, but always compares the characters in locale-independent
499  ASCII. That's useful if you're handling data in ASCII-based protocols.
500  */
501 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
502 
503 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
504  * if this system has no getaddrinfo(). */
505 #ifdef _EVENT_HAVE_STRUCT_ADDRINFO
506 #define evutil_addrinfo addrinfo
507 #else
508 /** A definition of struct addrinfo for systems that lack it.
509 
510  (This is just an alias for struct addrinfo if the system defines
511  struct addrinfo.)
512 */
514  int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
515  int ai_family; /* PF_xxx */
516  int ai_socktype; /* SOCK_xxx */
517  int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
518  size_t ai_addrlen; /* length of ai_addr */
519  char *ai_canonname; /* canonical name for nodename */
520  struct sockaddr *ai_addr; /* binary address */
521  struct evutil_addrinfo *ai_next; /* next structure in linked list */
522 };
523 #endif
524 /** @name evutil_getaddrinfo() error codes
525 
526  These values are possible error codes for evutil_getaddrinfo() and
527  related functions.
528 
529  @{
530 */
531 #ifdef EAI_ADDRFAMILY
532 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
533 #else
534 #define EVUTIL_EAI_ADDRFAMILY -901
535 #endif
536 #ifdef EAI_AGAIN
537 #define EVUTIL_EAI_AGAIN EAI_AGAIN
538 #else
539 #define EVUTIL_EAI_AGAIN -902
540 #endif
541 #ifdef EAI_BADFLAGS
542 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
543 #else
544 #define EVUTIL_EAI_BADFLAGS -903
545 #endif
546 #ifdef EAI_FAIL
547 #define EVUTIL_EAI_FAIL EAI_FAIL
548 #else
549 #define EVUTIL_EAI_FAIL -904
550 #endif
551 #ifdef EAI_FAMILY
552 #define EVUTIL_EAI_FAMILY EAI_FAMILY
553 #else
554 #define EVUTIL_EAI_FAMILY -905
555 #endif
556 #ifdef EAI_MEMORY
557 #define EVUTIL_EAI_MEMORY EAI_MEMORY
558 #else
559 #define EVUTIL_EAI_MEMORY -906
560 #endif
561 /* This test is a bit complicated, since some MS SDKs decide to
562  * remove NODATA or redefine it to be the same as NONAME, in a
563  * fun interpretation of RFC 2553 and RFC 3493. */
564 #if defined(EAI_NODATA) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
565 #define EVUTIL_EAI_NODATA EAI_NODATA
566 #else
567 #define EVUTIL_EAI_NODATA -907
568 #endif
569 #ifdef EAI_NONAME
570 #define EVUTIL_EAI_NONAME EAI_NONAME
571 #else
572 #define EVUTIL_EAI_NONAME -908
573 #endif
574 #ifdef EAI_SERVICE
575 #define EVUTIL_EAI_SERVICE EAI_SERVICE
576 #else
577 #define EVUTIL_EAI_SERVICE -909
578 #endif
579 #ifdef EAI_SOCKTYPE
580 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
581 #else
582 #define EVUTIL_EAI_SOCKTYPE -910
583 #endif
584 #ifdef EAI_SYSTEM
585 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
586 #else
587 #define EVUTIL_EAI_SYSTEM -911
588 #endif
589 
590 #define EVUTIL_EAI_CANCEL -90001
591 
592 #ifdef AI_PASSIVE
593 #define EVUTIL_AI_PASSIVE AI_PASSIVE
594 #else
595 #define EVUTIL_AI_PASSIVE 0x1000
596 #endif
597 #ifdef AI_CANONNAME
598 #define EVUTIL_AI_CANONNAME AI_CANONNAME
599 #else
600 #define EVUTIL_AI_CANONNAME 0x2000
601 #endif
602 #ifdef AI_NUMERICHOST
603 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
604 #else
605 #define EVUTIL_AI_NUMERICHOST 0x4000
606 #endif
607 #ifdef AI_NUMERICSERV
608 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
609 #else
610 #define EVUTIL_AI_NUMERICSERV 0x8000
611 #endif
612 #ifdef AI_V4MAPPED
613 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
614 #else
615 #define EVUTIL_AI_V4MAPPED 0x10000
616 #endif
617 #ifdef AI_ALL
618 #define EVUTIL_AI_ALL AI_ALL
619 #else
620 #define EVUTIL_AI_ALL 0x20000
621 #endif
622 #ifdef AI_ADDRCONFIG
623 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
624 #else
625 #define EVUTIL_AI_ADDRCONFIG 0x40000
626 #endif
627 /**@}*/
628 
629 struct evutil_addrinfo;
630 /**
631  * This function clones getaddrinfo for systems that don't have it. For full
632  * details, see RFC 3493, section 6.1.
633  *
634  * Limitations:
635  * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
636  * gethostbyname, with their attendant issues.
637  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
638  *
639  * For a nonblocking variant, see evdns_getaddrinfo.
640  */
641 int evutil_getaddrinfo(const char *nodename, const char *servname,
642  const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
643 
644 /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
645 void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
646 
647 const char *evutil_gai_strerror(int err);
648 
649 /** Generate n bytes of secure pseudorandom data, and store them in buf.
650  *
651  * By default, Libevent uses an ARC4-based random number generator, seeded
652  * using the platform's entropy source (/dev/urandom on Unix-like systems;
653  * CryptGenRandom on Windows).
654  */
655 void evutil_secure_rng_get_bytes(void *buf, size_t n);
656 
657 /**
658  * Seed the secure random number generator if needed, and return 0 on
659  * success or -1 on failure.
660  *
661  * It is okay to call this function more than once; it will still return
662  * 0 if the RNG has been successfully seeded and -1 if it can't be
663  * seeded.
664  *
665  * Ordinarily you don't need to call this function from your own code;
666  * Libevent will seed the RNG itself the first time it needs good random
667  * numbers. You only need to call it if (a) you want to double-check
668  * that one of the seeding methods did succeed, or (b) you plan to drop
669  * the capability to seed (by chrooting, or dropping capabilities, or
670  * whatever), and you want to make sure that seeding happens before your
671  * program loses the ability to do it.
672  */
673 int evutil_secure_rng_init(void);
674 
675 /** Seed the random number generator with extra random bytes.
676 
677  You should almost never need to call this function; it should be
678  sufficient to invoke evutil_secure_rng_init(), or let Libevent take
679  care of calling evutil_secure_rng_init() on its own.
680 
681  If you call this function as a _replacement_ for the regular
682  entropy sources, then you need to be sure that your input
683  contains a fairly large amount of strong entropy. Doing so is
684  notoriously hard: most people who try get it wrong. Watch out!
685 
686  @param dat a buffer full of a strong source of random numbers
687  @param datlen the number of bytes to read from datlen
688  */
689 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
690 
691 #ifdef __cplusplus
692 }
693 #endif
694 
695 #endif /* _EVUTIL_H_ */
ev_int64_t evutil_strtoll(const char *s, char **endptr, int base)
Parse a 64-bit value from a string.
Definition: evutil.c:339
int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen)
Parse an IPv4 or IPv6 address, with optional port, from a string.
Definition: evutil.c:1770
int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2, int include_port)
Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1 preceeds sa2...
Definition: evutil.c:1902
int evutil_secure_rng_init(void)
Seed the secure random number generator if needed, and return 0 on success or -1 on failure...
Definition: evutil_rand.c:113
int evutil_make_socket_closeonexec(evutil_socket_t sock)
Do platform-specific operations as needed to close a socket upon a successful execution of one of the...
Definition: evutil.c:312
int evutil_ascii_strcasecmp(const char *str1, const char *str2)
As strcasecmp, but always compares the characters in locale-independent ASCII.
Definition: evutil.c:2019
#define evutil_socket_t
A type wide enough to hold the output of "socket()" or "accept()".
Definition: util.h:278
int evutil_make_socket_nonblocking(evutil_socket_t sock)
Do platform-specific operations as needed to make a socket nonblocking.
Definition: evutil.c:270
void evutil_freeaddrinfo(struct evutil_addrinfo *ai)
Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo.
Definition: evutil.c:1353
int evutil_snprintf(char *buf, size_t buflen, const char *format,...)
Replacement for snprintf to get consistent behavior on platforms for which the return value of snprin...
Definition: evutil.c:1524
int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n)
As strncasecmp, but always compares the characters in locale-independent ASCII.
Definition: evutil.c:2033
int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2])
Create two new sockets that are connected to each other.
Definition: evutil.c:158
void evutil_secure_rng_add_bytes(const char *dat, size_t datlen)
Seed the random number generator with extra random bytes.
Definition: evutil_rand.c:134
A definition of struct addrinfo for systems that lack it.
Definition: util.h:513
int evutil_getaddrinfo(const char *nodename, const char *servname, const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res)
This function clones getaddrinfo for systems that don't have it.
Definition: evutil.c:1162
int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
Replacement for vsnprintf to get consistent behavior on platforms for which the return value of snpri...
Definition: evutil.c:1535
const char * evutil_inet_ntop(int af, const void *src, char *dst, size_t len)
Replacement for inet_ntop for platforms which lack it.
Definition: evutil.c:1562
void evutil_secure_rng_get_bytes(void *buf, size_t n)
Generate n bytes of secure pseudorandom data, and store them in buf.
Definition: evutil_rand.c:128
int evutil_make_listen_socket_reuseable(evutil_socket_t sock)
Do platform-specific operations to make a listener socket reusable.
Definition: evutil.c:297
Definition: ompi_time.h:172
int evutil_inet_pton(int af, const char *src, void *dst)
Replacement for inet_pton for platforms which lack it.
Definition: evutil.c:1658
int evutil_closesocket(evutil_socket_t sock)
Do the platform-specific call needed to close a socket returned from socket() or accept().
Definition: evutil.c:329