evutil.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2007 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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.
  15. *
  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.
  26. */
  27. #ifndef _EVUTIL_H_
  28. #define _EVUTIL_H_
  29. /** @file evutil.h
  30. Common convenience functions for cross-platform portability and
  31. related socket manipulations.
  32. */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #include "event-config.h"
  37. #ifdef _EVENT_HAVE_SYS_TIME_H
  38. #include <sys/time.h>
  39. #endif
  40. #ifdef _EVENT_HAVE_STDINT_H
  41. #include <stdint.h>
  42. #elif defined(_EVENT_HAVE_INTTYPES_H)
  43. #include <inttypes.h>
  44. #endif
  45. #ifdef _EVENT_HAVE_SYS_TYPES_H
  46. #include <sys/types.h>
  47. #endif
  48. #include <stdarg.h>
  49. #ifdef _EVENT_HAVE_UINT64_T
  50. #define ev_uint64_t uint64_t
  51. #define ev_int64_t int64_t
  52. #elif defined(WIN32)
  53. #define ev_uint64_t unsigned __int64
  54. #define ev_int64_t signed __int64
  55. #elif _EVENT_SIZEOF_LONG_LONG == 8
  56. #define ev_uint64_t unsigned long long
  57. #define ev_int64_t long long
  58. #elif _EVENT_SIZEOF_LONG == 8
  59. #define ev_uint64_t unsigned long
  60. #define ev_int64_t long
  61. #else
  62. #error "No way to define ev_uint64_t"
  63. #endif
  64. #ifdef _EVENT_HAVE_UINT32_T
  65. #define ev_uint32_t uint32_t
  66. #elif defined(WIN32)
  67. #define ev_uint32_t unsigned int
  68. #elif _EVENT_SIZEOF_LONG == 4
  69. #define ev_uint32_t unsigned long
  70. #elif _EVENT_SIZEOF_INT == 4
  71. #define ev_uint32_t unsigned int
  72. #else
  73. #error "No way to define ev_uint32_t"
  74. #endif
  75. #ifdef _EVENT_HAVE_UINT16_T
  76. #define ev_uint16_t uint16_t
  77. #elif defined(WIN32)
  78. #define ev_uint16_t unsigned short
  79. #elif _EVENT_SIZEOF_INT == 2
  80. #define ev_uint16_t unsigned int
  81. #elif _EVENT_SIZEOF_SHORT == 2
  82. #define ev_uint16_t unsigned short
  83. #else
  84. #error "No way to define ev_uint16_t"
  85. #endif
  86. #ifdef _EVENT_HAVE_UINT8_T
  87. #define ev_uint8_t uint8_t
  88. #else
  89. #define ev_uint8_t unsigned char
  90. #endif
  91. int evutil_socketpair(int d, int type, int protocol, int sv[2]);
  92. int evutil_make_socket_nonblocking(int sock);
  93. #ifdef WIN32
  94. #define EVUTIL_CLOSESOCKET(s) closesocket(s)
  95. #else
  96. #define EVUTIL_CLOSESOCKET(s) close(s)
  97. #endif
  98. #ifdef WIN32
  99. #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
  100. #define EVUTIL_SET_SOCKET_ERROR(errcode) \
  101. do { WSASetLastError(errcode); } while (0)
  102. #else
  103. #define EVUTIL_SOCKET_ERROR() (errno)
  104. #define EVUTIL_SET_SOCKET_ERROR(errcode) \
  105. do { errno = (errcode); } while (0)
  106. #endif
  107. /*
  108. * Manipulation functions for struct timeval
  109. */
  110. #ifdef _EVENT_HAVE_TIMERADD
  111. #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
  112. #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
  113. #else
  114. #define evutil_timeradd(tvp, uvp, vvp) \
  115. do { \
  116. (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
  117. (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
  118. if ((vvp)->tv_usec >= 1000000) { \
  119. (vvp)->tv_sec++; \
  120. (vvp)->tv_usec -= 1000000; \
  121. } \
  122. } while (0)
  123. #define evutil_timersub(tvp, uvp, vvp) \
  124. do { \
  125. (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
  126. (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
  127. if ((vvp)->tv_usec < 0) { \
  128. (vvp)->tv_sec--; \
  129. (vvp)->tv_usec += 1000000; \
  130. } \
  131. } while (0)
  132. #endif /* !_EVENT_HAVE_HAVE_TIMERADD */
  133. #ifdef _EVENT_HAVE_TIMERCLEAR
  134. #define evutil_timerclear(tvp) timerclear(tvp)
  135. #else
  136. #define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
  137. #endif
  138. #define evutil_timercmp(tvp, uvp, cmp) \
  139. (((tvp)->tv_sec == (uvp)->tv_sec) ? \
  140. ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
  141. ((tvp)->tv_sec cmp (uvp)->tv_sec))
  142. #ifdef _EVENT_HAVE_TIMERISSET
  143. #define evutil_timerisset(tvp) timerisset(tvp)
  144. #else
  145. #define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
  146. #endif
  147. /* big-int related functions */
  148. ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
  149. #ifdef _EVENT_HAVE_GETTIMEOFDAY
  150. #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
  151. #else
  152. struct timezone;
  153. int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
  154. #endif
  155. int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
  156. #ifdef __GNUC__
  157. __attribute__((format(printf, 3, 4)))
  158. #endif
  159. ;
  160. int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap);
  161. #ifdef __cplusplus
  162. }
  163. #endif
  164. #endif /* _EVUTIL_H_ */