Home | History | Annotate | Download | only in libevent
      1 /*
      2  * Copyright (c) 2007 Niels Provos <provos (at) 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 
     30 /** @file evutil.h
     31 
     32   Common convenience functions for cross-platform portability and
     33   related socket manipulations.
     34 
     35  */
     36 
     37 #ifdef __cplusplus
     38 extern "C" {
     39 #endif
     40 
     41 #include "event-config.h"
     42 #ifdef _EVENT_HAVE_SYS_TIME_H
     43 #include <sys/time.h>
     44 #endif
     45 #ifdef _EVENT_HAVE_STDINT_H
     46 #include <stdint.h>
     47 #elif defined(_EVENT_HAVE_INTTYPES_H)
     48 #include <inttypes.h>
     49 #endif
     50 #ifdef _EVENT_HAVE_SYS_TYPES_H
     51 #include <sys/types.h>
     52 #endif
     53 #include <stdarg.h>
     54 
     55 #ifdef _EVENT_HAVE_UINT64_T
     56 #define ev_uint64_t uint64_t
     57 #define ev_int64_t int64_t
     58 #elif defined(WIN32)
     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
     67 #else
     68 #error "No way to define ev_uint64_t"
     69 #endif
     70 
     71 #ifdef _EVENT_HAVE_UINT32_T
     72 #define ev_uint32_t uint32_t
     73 #elif defined(WIN32)
     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
     79 #else
     80 #error "No way to define ev_uint32_t"
     81 #endif
     82 
     83 #ifdef _EVENT_HAVE_UINT16_T
     84 #define ev_uint16_t uint16_t
     85 #elif defined(WIN32)
     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
     91 #else
     92 #error "No way to define ev_uint16_t"
     93 #endif
     94 
     95 #ifdef _EVENT_HAVE_UINT8_T
     96 #define ev_uint8_t uint8_t
     97 #else
     98 #define ev_uint8_t unsigned char
     99 #endif
    100 
    101 int evutil_socketpair(int d, int type, int protocol, int sv[2]);
    102 int evutil_make_socket_nonblocking(int sock);
    103 #ifdef WIN32
    104 #define EVUTIL_CLOSESOCKET(s) closesocket(s)
    105 #else
    106 #define EVUTIL_CLOSESOCKET(s) close(s)
    107 #endif
    108 
    109 #ifdef WIN32
    110 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
    111 #define EVUTIL_SET_SOCKET_ERROR(errcode)		\
    112 	do { WSASetLastError(errcode); } while (0)
    113 #else
    114 #define EVUTIL_SOCKET_ERROR() (errno)
    115 #define EVUTIL_SET_SOCKET_ERROR(errcode)		\
    116 		do { errno = (errcode); } while (0)
    117 #endif
    118 
    119 /*
    120  * Manipulation functions for struct timeval
    121  */
    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))
    125 #else
    126 #define evutil_timeradd(tvp, uvp, vvp)							\
    127 	do {														\
    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) {						\
    131 			(vvp)->tv_sec++;									\
    132 			(vvp)->tv_usec -= 1000000;							\
    133 		}														\
    134 	} while (0)
    135 #define	evutil_timersub(tvp, uvp, vvp)						\
    136 	do {													\
    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) {							\
    140 			(vvp)->tv_sec--;								\
    141 			(vvp)->tv_usec += 1000000;						\
    142 		}													\
    143 	} while (0)
    144 #endif /* !_EVENT_HAVE_HAVE_TIMERADD */
    145 
    146 #ifdef _EVENT_HAVE_TIMERCLEAR
    147 #define evutil_timerclear(tvp) timerclear(tvp)
    148 #else
    149 #define	evutil_timerclear(tvp)	(tvp)->tv_sec = (tvp)->tv_usec = 0
    150 #endif
    151 
    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))
    156 
    157 #ifdef _EVENT_HAVE_TIMERISSET
    158 #define evutil_timerisset(tvp) timerisset(tvp)
    159 #else
    160 #define	evutil_timerisset(tvp)	((tvp)->tv_sec || (tvp)->tv_usec)
    161 #endif
    162 
    163 
    164 /* big-int related functions */
    165 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
    166 
    167 
    168 #ifdef _EVENT_HAVE_GETTIMEOFDAY
    169 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
    170 #else
    171 struct timezone;
    172 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
    173 #endif
    174 
    175 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
    176 #ifdef __GNUC__
    177 	__attribute__((format(printf, 3, 4)))
    178 #endif
    179 	;
    180 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap);
    181 
    182 #ifdef __cplusplus
    183 }
    184 #endif
    185 
    186 #endif /* _EVUTIL_H_ */
    187