Imported Upstream version 20070115
[aiccu.git] / common / common.h
blob575d8549583f4f08b02e5cb838ad1508db862a74
1 /**********************************************************
2 SixXS - Automatic IPv6 Connectivity Configuration Utility
3 ***********************************************************
4 Copyright 2003-2005 SixXS - http://www.sixxs.net
5 ***********************************************************
6 common/common.h - Common Definitions
7 ***********************************************************
8 $Author: jeroen $
9 $Id: common.h,v 1.23 2007-01-11 14:50:51 jeroen Exp $
10 $Date: 2007-01-11 14:50:51 $
11 **********************************************************/
13 #ifndef AICCU_COMMON_H
14 #define AICCU_COMMON_H "H5K7:W3NDY5UU5N1K1N1C0l3"
16 #ifdef _DEBUG
17 #define D(x) x
18 #else
19 #define D(x) {}
20 #endif
22 #ifndef _OPENBSD
23 #ifndef _SUNOS
24 #ifndef _AIX
25 #ifndef _XOPEN_SOURCE
26 #define _XOPEN_SOURCE
27 #endif
28 #endif
29 #endif
30 #endif
31 #define __STRICT_ANSI__
33 /* Don't deprecate strncat etc. */
34 #ifdef _WIN32
35 #define _CRT_SECURE_NO_DEPRECATE
36 #endif
38 #ifdef _AIX
39 #define _H_ARPA_ONAMESER_COMPAT "AICCU workaround"
40 #include <net/net_globals.h>
41 #endif
43 #include <stdio.h>
44 #include <errno.h>
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <ctype.h>
49 #include <time.h>
50 #include <signal.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
55 #if defined(_SUNOS) || defined(_AIX) || defined(_DARWIN)
56 /* Include this as it knows quite a bit about endianess */
57 #include <arpa/nameser_compat.h>
58 #else
59 #ifndef _WIN32
60 #if defined(_OPENBSD) || defined(_DFBSD) || defined(_FREEBSD) || defined(_NETBSD)
61 #include <sys/endian.h>
62 #else
63 #include <endian.h>
64 #endif
65 #endif
66 #endif
68 /* For MD5 routines */
69 #define __USE_BSD 1
70 #include <sys/types.h>
72 #ifndef _WIN32
73 /* Unix Specifics */
75 #ifndef linux
76 #include <netinet/in_systm.h>
77 #endif
79 #ifdef _DARWIN
80 #define _BSD_SOCKLEN_T_
81 #endif
83 #include <sys/socket.h>
84 #include <netinet/in.h>
85 #include <netdb.h>
86 #include <unistd.h>
87 #include <strings.h>
88 #include <syslog.h>
89 #include <arpa/inet.h>
90 #include <sys/ioctl.h>
91 #include <sys/select.h>
93 #include <net/if.h>
94 #include <netinet/if_ether.h>
95 #ifdef linux
96 #include <netpacket/packet.h>
97 #include <linux/if_tun.h>
98 #else
99 #ifdef _DFBSD
100 #include <net/tun/if_tun.h>
101 #else
102 #ifdef _DARWIN
104 * Darwin doesn't have TUN/TAP support per default
105 * It is available from http://www-user.rhrk.uni-kl.de/~nissler/tuntap/
106 * which is a port made by Mattias Nissler
107 * for compiling convienience we have included the ioctl's here
109 #define TUNSIFHEAD _IOW('t', 96, int)
110 #define TUNGIFHEAD _IOR('t', 97, int)
111 #else
112 #ifndef _AIX
113 #include <net/if_tun.h>
114 /* endif for !_AIX */
115 #endif
116 /* endif for _DARWIN else */
117 #endif
118 /* endif for _DFBSD else */
119 #endif
120 /* endif for linux else */
121 #endif
122 #include <netinet/ip.h>
123 #include <netinet/ip6.h>
124 #include <netinet/icmp6.h>
125 #include <netinet/tcp.h>
126 #include <netinet/udp.h>
127 #include <netinet/ip_icmp.h>
128 #include <sys/ioctl.h>
130 #if defined(_OPENBSD) || defined(_DARWIN) || defined(_FREEBSD) || defined(_DFBSD)
131 #include <sys/uio.h>
132 #endif
134 #include <pthread.h>
137 * Windows abstracts sockets to a different
138 * type, as this is actually pretty nice
139 * we'll do it too
141 #ifndef SOCKET
142 typedef int SOCKET;
143 #endif
145 /* closesocket() -> close() on unices */
146 #define closesocket close
149 * Expect a BSD style in6_addr who puts
150 * this between a #ifdef _KERNEL...
151 * Probably against people doing stuff in userspace?
153 #ifndef s6_addr
154 #ifndef _SUNOS
155 #define s6_addr __u6_addr.__u6_addr8
156 #else
157 #define s6_addr _S6_un._S6_u8
158 #endif
159 #endif
160 #else
162 /* Winsock */
163 #include <winsock2.h>
164 #include <ws2tcpip.h>
166 /* Windows Specifics */
167 #include <io.h>
170 * Some weird M$ person thought it was
171 * funny to underscore common functions !?
173 #define snprintf _snprintf
174 #define vsnprintf _vsnprintf
175 #define strcasecmp _stricmp
176 #define strncasecmp _strnicmp
177 #define strdup _strdup
180 * Capitalize this one
181 * Sleep() is in milliseconds
183 #define sleep(x) Sleep(x*1000)
185 /* No syslog on Windows */
186 #define LOG_DEBUG 1
187 #define LOG_ERR 2
188 #define LOG_WARNING 3
189 #define LOG_INFO 4
191 typedef unsigned long u_int32_t;
192 typedef unsigned long long u_int64_t;
194 typedef unsigned char u_int8_t;
195 typedef unsigned __int16 u_int16_t;
196 typedef unsigned __int64 u_int64_t;
198 /* Not available in the Winsock2 includes */
199 #define IPPROTO_NONE 59 /* IPv6 no next header */
201 #define BIG_ENDIAN 4321
202 #define LITTLE_ENDIAN 1234
204 #define __BIG_ENDIAN BIG_ENDIAN
205 #define __LITTLE_ENDIAN LITTLE_ENDIAN
207 /* Fix byte order */
208 #define __BYTE_ORDER __LITTLE_ENDIAN
209 #define BYTE_ORDER LITTLE_ENDIAN
211 #define s6_addr16 _S6_un.Word
212 #define SHUT_RDWR SD_BOTH
213 #define uint8_t u_int8_t
214 #define uint16_t u_int16_t
215 #define uint32_t u_int32_t
216 #define uint64_t u_int64_t
218 struct ether
220 uint16_t ether_dhost[3];
221 uint16_t ether_shost[3];
222 uint16_t ether_type;
225 /* The IPv6 Header */
226 struct ip6_hdr
228 union
230 struct ip6_hdrctl
232 uint32_t ip6_un1_flow; /* 4 bits version, 8 bits TC, 20 bits flow-ID */
233 uint16_t ip6_un1_plen; /* payload length */
234 uint8_t ip6_un1_nxt; /* next header */
235 uint8_t ip6_un1_hlim; /* hop limit */
236 } ip6_un1;
237 uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits tclass */
238 } ip6_ctlun;
239 struct in6_addr ip6_src; /* source address */
240 struct in6_addr ip6_dst; /* destination address */
243 /* ICMPv6 */
244 struct icmp6_hdr
246 uint8_t icmp6_type; /* type field */
247 uint8_t icmp6_code; /* code field */
248 uint16_t icmp6_cksum; /* checksum field */
249 union
251 uint32_t icmp6_un_data32[1]; /* type-specific field */
252 uint16_t icmp6_un_data16[2]; /* type-specific field */
253 uint8_t icmp6_un_data8[4]; /* type-specific field */
254 } icmp6_dataun;
257 #define ND_NEIGHBOR_SOLICIT 135
258 #define ND_NEIGHBOR_ADVERT 136
260 struct nd_neighbor_solicit
262 struct in6_addr nd_ns_target; /* target address */
263 /* could be followed by options */
266 struct nd_neighbor_advert
268 struct in6_addr nd_na_target; /* target address */
269 /* could be followed by options */
270 uint8_t nd_no_type; /* Option providing the target MAC address */
271 uint8_t nd_no_len; /* Length (1) */
272 uint8_t nd_no_mac[6]; /* MAC address */
276 const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
277 int inet_pton(int af, const char *src, void *dst);
278 #endif /* WIN32 */
281 #ifndef MSG_NOSIGNAL
282 #define MSG_NOSIGNAL 0
283 #endif
285 #ifndef LITTLE_ENDIAN
286 #define LITTLE_ENDIAN __LITTLE_ENDIAN
287 #endif
288 #ifndef BIG_ENDIAN
289 #define BIG_ENDIAN __BIG_ENDIAN
290 #endif
291 #ifndef PDP_ENDIAN
292 #define PDP_ENDIAN __PDP_ENDIAN
293 #endif
294 #ifndef BYTE_ORDER
295 #define BYTE_ORDER __BYTE_ORDER
296 #endif
298 /* Boolean support */
299 #ifndef bool
300 #define bool uint32_t
301 #endif
302 #ifndef false
303 #define false 0
304 #endif
305 #ifndef true
306 #define true (!false)
307 #endif
309 #ifndef offsetof
310 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
311 #endif
313 /* Include MD5 and SHA1 support */
314 #include "hash_md5.h"
315 #include "hash_sha1.h"
317 /* Resolver includes */
318 #ifndef _WIN32
319 #include <netinet/in.h>
320 #include <arpa/nameser.h>
321 #include <resolv.h>
322 #include <netdb.h>
323 #endif
325 #ifdef AICCU_GNUTLS
326 #include <gnutls/gnutls.h>
327 #endif
329 #ifndef NS_GET16SZ
330 #define NS_INT32SZ 4 /* #/bytes of data in a u_int32_t */
331 #endif
333 #ifndef NS_GET32SZ
334 #define NS_INT16SZ 2 /* #/bytes of data in a u_int16_t */
335 #endif
338 #ifndef NS_GET16
339 #define NS_GET16(s, cp) do { \
340 register u_char *t_cp = (u_char *)(cp); \
341 (s) = ((u_int16_t)t_cp[0] << 8) \
342 | ((u_int16_t)t_cp[1]) \
344 (cp) += NS_INT16SZ; \
345 } while (0)
346 #endif
348 #ifndef NS_GET32
349 #define NS_GET32(l, cp) do { \
350 register u_char *t_cp = (u_char *)(cp); \
351 (l) = ((u_int32_t)t_cp[0] << 24) \
352 | ((u_int32_t)t_cp[1] << 16) \
353 | ((u_int32_t)t_cp[2] << 8) \
354 | ((u_int32_t)t_cp[3]) \
356 (cp) += NS_INT32SZ; \
357 } while (0)
358 #endif
360 /* parseline() rules */
361 enum pl_ruletype
363 PLRT_STRING, /* Offset points to a String (strdup()) */
364 PLRT_INTEGER, /* Offset points to a Integer (unsigned int) */
365 PLRT_BOOL, /* Offset points to a Boolean. */
366 PLRT_IPV4, /* Offset points to a IPv4 address (inet_pton(..., AF_INET)) */
367 PLRT_IPV6, /* Offset points to a IPv6 address (inet_pton(..., AF_INET6)) */
368 PLRT_END /* End of rules */
371 struct pl_rule
373 const char *title;
374 unsigned int type;
375 unsigned int offset;
379 struct tlssocket
381 SOCKET socket;
382 #ifdef AICCU_GNUTLS
383 bool tls_active; /* TLS active? */
384 gnutls_session session; /* The GnuTLS sesision */
385 #endif
388 typedef struct tlssocket * TLSSOCKET;
390 /* Common Functions */
391 void dologA(int level, const char *fmt, va_list ap);
392 void dolog(int level, const char *fmt, ...);
394 #ifdef _AIX
395 void vsyslog(int priority, const char *format, va_list ap);
396 #endif
398 /* Networking functions */
399 void sock_printf(TLSSOCKET sock, const char *fmt, ...);
400 int sock_getline(TLSSOCKET sock, char *rbuf, unsigned int rbuflen, unsigned int *filled, char *ubuf, unsigned int ubuflen);
401 TLSSOCKET connect_client(const char *hostname, const char *service, int family, int socktype);
402 TLSSOCKET listen_server(const char *description, const char *hostname, const char *service, int family, int socktype);
403 void sock_free(TLSSOCKET sock);
404 #ifdef AICCU_GNUTLS
405 bool sock_gotls(TLSSOCKET sock);
406 #endif
408 /* Parsing functions */
409 unsigned int countfields(char *s);
410 bool copyfield(char *s, unsigned int n, char *buf, unsigned int buflen);
411 bool parseline(char *line, const char *split, struct pl_rule *rules, void *data);
413 /* Convienience */
414 void MD5String(const char *sString, char *sSignature, unsigned int siglen);
415 bool is_rfc1918(char *ipv4);
417 #endif /* AICCU_COMMON_H */