Forward port changelog
[tor.git] / src / common / compat.h
blobd1b5c80aed3f45206cabb4aed45f44742d970fc9
1 /* Copyright 2003-2004 Roger Dingledine; Copyright 2004 Nick Mathewson */
2 /* See LICENSE for licensing information */
3 /* $Id$ */
5 #ifndef __COMPAT_H
6 #define __COMPAT_H
7 #define COMPAT_H_ID "$Id$"
9 #include "orconfig.h"
10 #include "torint.h"
11 #ifdef MS_WINDOWS
12 #define WIN32_WINNT 0x400
13 #define _WIN32_WINNT 0x400
14 #define WIN32_LEAN_AND_MEAN
15 #if (_MSC_VER <= 1300)
16 #include <winsock.h>
17 #else
18 #include <winsock2.h>
19 #include <ws2tcpip.h>
20 #endif
21 #endif
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25 #ifdef HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #include <stdarg.h>
30 #ifndef NULL_REP_IS_ZERO_BYTES
31 #error "It seems your platform does not represent NULL as zero. We can't cope."
32 #endif
34 /* ===== Compiler compatibility */
36 /* GCC can check printf types on arbitrary functions. */
37 #ifdef __GNUC__
38 #define CHECK_PRINTF(formatIdx, firstArg) \
39 __attribute__ ((format(printf, formatIdx, firstArg)))
40 #else
41 #define CHECK_PRINTF(formatIdx, firstArg)
42 #endif
44 /* inline is __inline on windows. */
45 #ifdef MS_WINDOWS
46 #define INLINE __inline
47 #else
48 #define INLINE inline
49 #endif
51 /* Windows compilers before VC7 don't have __FUNCTION__. */
52 #if defined(_MSC_VER) && _MSC_VER < 1300
53 #define __FUNCTION__ "???"
54 #endif
56 #if defined(__sgi) && !defined(__GNUC__) && defined(__c99)
57 #define __FUNCTION__ __func__
58 #endif
60 /* ===== String compatibility */
61 #ifdef MS_WINDOWS
62 /* Windows names string functions differently from most other platforms. */
63 #define strncasecmp strnicmp
64 #define strcasecmp stricmp
65 #endif
66 #ifndef HAVE_STRLCAT
67 size_t strlcat(char *dst, const char *src, size_t siz);
68 #endif
69 #ifndef HAVE_STRLCPY
70 size_t strlcpy(char *dst, const char *src, size_t siz);
71 #endif
73 #ifdef MS_WINDOWS
74 #define U64_PRINTF_ARG(a) (a)
75 #define U64_SCANF_ARG(a) (a)
76 #define U64_FORMAT "%I64u"
77 #define U64_LITERAL(n) (n ## ui64)
78 #else
79 #define U64_PRINTF_ARG(a) ((long long unsigned int)a)
80 #define U64_SCANF_ARG(a) ((long long unsigned int*)a)
81 #define U64_FORMAT "%llu"
82 #define U64_LITERAL(n) (n ## llu)
83 #endif
85 int tor_snprintf(char *str, size_t size, const char *format, ...)
86 CHECK_PRINTF(3,4);
87 int tor_vsnprintf(char *str, size_t size, const char *format, va_list args);
89 #define TOR_ISSPACE(c) isspace((int)(unsigned char)(c))
90 #define TOR_ISXDIGIT(c) isxdigit((int)(unsigned char)(c))
91 #define TOR_ISDIGIT(c) isdigit((int)(unsigned char)(c))
92 #define TOR_ISPRINT(c) isprint((int)(unsigned char)(c))
94 #define _SHORT_FILE_ (_tor_fix_source_file(__FILE__))
95 const char *_tor_fix_source_file(const char *fname);
97 /* ===== Time compatibility */
98 #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
99 struct timeval {
100 time_t tv_sec;
101 unsigned int tv_usec;
103 #endif
105 void tor_gettimeofday(struct timeval *timeval);
107 /* ===== File compatibility */
108 int replace_file(const char *from, const char *to);
110 /* ===== Net compatibility */
111 #ifdef MS_WINDOWS
112 /** On windows, you have to call close() on fds returned by open(),
113 * and closesocket() on fds returned by socket(). On Unix, everything
114 * gets close()'d. We abstract this difference by always using
115 * tor_close_socket to close sockets, and always using close() on
116 * files.
118 #define tor_close_socket(s) closesocket(s)
119 #else
120 #define tor_close_socket(s) close(s)
121 #endif
123 /* Now that we use libevent, all real sockets are safe for polling ... or
124 * if they aren't, libevent will help us. */
125 #define SOCKET_IS_POLLABLE(fd) ((fd)>=0)
127 struct in_addr;
128 int tor_inet_aton(const char *cp, struct in_addr *addr);
129 int tor_lookup_hostname(const char *name, uint32_t *addr);
130 void set_socket_nonblocking(int socket);
131 int tor_socketpair(int family, int type, int protocol, int fd[2]);
132 int network_init(void);
133 /* For stupid historical reasons, windows sockets have an independent
134 * set of errnos, and an independent way to get them. Also, you can't
135 * always believe WSAEWOULDBLOCK. Use the macros below to compare
136 * errnos against expected values, and use tor_socket_errno to find
137 * the actual errno after a socket operation fails.
139 #ifdef MS_WINDOWS
140 /** Return true if e is EAGAIN or the local equivalent. */
141 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN || (e) == WSAEWOULDBLOCK)
142 /** Return true if e is EINPROGRESS or the local equivalent. */
143 #define ERRNO_IS_EINPROGRESS(e) ((e) == WSAEINPROGRESS)
144 /** Return true if e is EINPROGRESS or the local equivalent as returned by
145 * a call to connect(). */
146 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == WSAEINPROGRESS || (e)== WSAEINVAL || (e) == WSAEWOULDBLOCK)
147 /** Return true if e is EAGAIN or another error indicating that a call to
148 * accept() has no pending connections to return. */
149 #define ERRNO_IS_ACCEPT_EAGAIN(e) ERRNO_IS_EAGAIN(e)
150 /** Return true if e is EMFILE or another error indicating that a call to
151 * accept() has failed because we're out of fds or something. */
152 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
153 ((e) == WSAEMFILE || (e) == WSAENOBUFS)
154 int tor_socket_errno(int sock);
155 const char *tor_socket_strerror(int e);
156 #else
157 #define ERRNO_IS_EAGAIN(e) ((e) == EAGAIN)
158 #define ERRNO_IS_EINPROGRESS(e) ((e) == EINPROGRESS)
159 #define ERRNO_IS_CONN_EINPROGRESS(e) ((e) == EINPROGRESS)
160 #define ERRNO_IS_ACCEPT_EAGAIN(e) ((e) == EAGAIN || (e) == ECONNABORTED)
161 #define ERRNO_IS_ACCEPT_RESOURCE_LIMIT(e) \
162 ((e) == EMFILE || (e) == ENFILE || (e) == ENOBUFS || (e) == ENOMEM)
163 #define tor_socket_errno(sock) (errno)
164 #define tor_socket_strerror(e) strerror(e)
165 #endif
167 /* ===== OS compatibility */
168 const char *get_uname(void);
170 /* Some platforms segfault when you try to access a multi-byte type
171 * that isn't aligned to a word boundary. The macros and/or functions
172 * below can be used to access unaligned data on any platform.
174 #ifdef UNALIGNED_INT_ACCESS_OK
175 #define get_uint16(cp) (*(uint16_t*)(cp))
176 #define get_uint32(cp) (*(uint32_t*)(cp))
177 #define set_uint16(cp,v) do { *(uint16_t*)(cp) = (v); } while (0)
178 #define set_uint32(cp,v) do { *(uint32_t*)(cp) = (v); } while (0)
179 #else
180 uint16_t get_uint16(const char *cp);
181 uint32_t get_uint32(const char *cp);
182 void set_uint16(char *cp, uint16_t v);
183 void set_uint32(char *cp, uint32_t v);
184 #endif
186 int set_max_file_descriptors(unsigned int required_min);
187 int switch_id(char *user, char *group);
188 #ifdef HAVE_PWD_H
189 char *get_user_homedir(const char *username);
190 #endif
192 int spawn_func(int (*func)(void *), void *data);
193 void spawn_exit(void);
195 /* Because we use threads instead of processes on Windows, we need locking on
196 * Windows. On Unixy platforms, these functions are no-ops. */
197 typedef struct tor_mutex_t tor_mutex_t;
198 tor_mutex_t *tor_mutex_new(void);
199 void tor_mutex_acquire(tor_mutex_t *m);
200 void tor_mutex_release(tor_mutex_t *m);
201 void tor_mutex_free(tor_mutex_t *m);
203 #if defined(MS_WINDOWS)
204 #define USE_WIN32_THREADS
205 #define TOR_IS_MULTITHREADED 1
206 #elif defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_CREATE)
207 #define USE_PTHREADS
208 #define TOR_IS_MULTITHREADED 1
209 #else
210 #undef TOR_IS_MULTITHREADED
211 #endif
213 #endif