1 /*********************************************************************************************************
2 * Software License Agreement (BSD License) *
3 * Author: Sebastien Decugis <sdecugis@freediameter.net> *
4 * *
5 * Copyright (c) 2011, WIDE Project and NICT *
6 * All rights reserved. *
7 * *
8 * Redistribution and use of this software in source and binary forms, with or without modification, are *
9 * permitted provided that the following conditions are met: *
10 * *
11 * * Redistributions of source code must retain the above *
12 * copyright notice, this list of conditions and the *
13 * following disclaimer. *
14 * *
15 * * Redistributions in binary form must reproduce the above *
16 * copyright notice, this list of conditions and the *
17 * following disclaimer in the documentation and/or other *
18 * materials provided with the distribution. *
19 * *
20 * * Neither the name of the WIDE Project or NICT nor the *
21 * names of its contributors may be used to endorse or *
22 * promote products derived from this software without *
23 * specific prior written permission of WIDE Project and *
24 * NICT. *
25 * *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34 *********************************************************************************************************/
35
36 /* This file contains compatibility bindings for hostap files.
37 Most of the definitions here come from different files from the hostap project.
38
39 We don't care for OS-specific definitions since we are only compatible with POSIX systems.
40
41 */
42
43 #ifndef _HOSTAP_COMPAT_H
44 #define _HOSTAP_COMPAT_H
45
46 #include <sys/time.h>
47
48 typedef uint64_t u64;
49 typedef uint32_t u32;
50 typedef uint16_t u16;
51 typedef uint8_t u8;
52 typedef int64_t s64;
53 typedef int32_t s32;
54 typedef int16_t s16;
55 typedef int8_t s8;
56
57 /* md5.c uses a different macro name than freeDiameter for endianness */
58 #ifdef HOST_BIG_ENDIAN
59 # define WORDS_BIGENDIAN HOST_BIG_ENDIAN
60 #else /* HOST_BIG_ENDIAN */
61 # undef WORDS_BIGENDIAN
62 #endif /* HOST_BIG_ENDIAN */
63
64 /* freeDiameter uses the POSIX API, so we don't provide alternatives. This may be changed later as needed */
65 #define os_malloc(s) malloc((s))
66 #define os_realloc(p, s) realloc((p), (s))
67 #define os_free(p) free((p))
68
69 #define os_memcpy(d, s, n) memcpy((d), (s), (n))
70 #define os_memmove(d, s, n) memmove((d), (s), (n))
71 #define os_memset(s, c, n) memset(s, c, n)
72 #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
73
74 #define os_strdup(s) strdup(s)
75 #define os_strlen(s) strlen(s)
76 #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
77 #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
78 #define os_strchr(s, c) strchr((s), (c))
79 #define os_strcmp(s1, s2) strcmp((s1), (s2))
80 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
81 #define os_strncpy(d, s, n) strncpy((d), (s), (n))
82 #define os_strrchr(s, c) strrchr((s), (c))
83 #define os_strstr(h, n) strstr((h), (n))
84 #define os_snprintf snprintf
85
86 #define os_random() random()
87
os_zalloc(size_t size)88 static __inline__ void * os_zalloc(size_t size)
89 {
90 void *n = os_malloc(size);
91 if (n)
92 os_memset(n, 0, size);
93 return n;
94 }
95
96 typedef long os_time_t;
97 struct os_time {
98 os_time_t sec;
99 os_time_t usec;
100 };
101
os_get_time(struct os_time * t)102 static __inline__ int os_get_time(struct os_time *t)
103 {
104 int res;
105 struct timeval tv;
106 res = gettimeofday(&tv, NULL);
107 t->sec = tv.tv_sec;
108 t->usec = tv.tv_usec;
109 return res;
110 }
111
112 /* Macros for handling unaligned memory accesses */
113 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
114 #define WPA_PUT_BE16(a, val) \
115 do { \
116 (a)[0] = ((u16) (val)) >> 8; \
117 (a)[1] = ((u16) (val)) & 0xff; \
118 } while (0)
119
120 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
121 #define WPA_PUT_LE16(a, val) \
122 do { \
123 (a)[1] = ((u16) (val)) >> 8; \
124 (a)[0] = ((u16) (val)) & 0xff; \
125 } while (0)
126
127 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
128 ((u32) (a)[2]))
129 #define WPA_PUT_BE24(a, val) \
130 do { \
131 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
132 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
133 (a)[2] = (u8) (((u32) (val)) & 0xff); \
134 } while (0)
135
136 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
137 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
138 #define WPA_PUT_BE32(a, val) \
139 do { \
140 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
141 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
142 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
143 (a)[3] = (u8) (((u32) (val)) & 0xff); \
144 } while (0)
145
146 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
147 (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
148 #define WPA_PUT_LE32(a, val) \
149 do { \
150 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
151 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
152 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
153 (a)[0] = (u8) (((u32) (val)) & 0xff); \
154 } while (0)
155
156 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
157 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
158 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
159 (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
160 #define WPA_PUT_BE64(a, val) \
161 do { \
162 (a)[0] = (u8) (((u64) (val)) >> 56); \
163 (a)[1] = (u8) (((u64) (val)) >> 48); \
164 (a)[2] = (u8) (((u64) (val)) >> 40); \
165 (a)[3] = (u8) (((u64) (val)) >> 32); \
166 (a)[4] = (u8) (((u64) (val)) >> 24); \
167 (a)[5] = (u8) (((u64) (val)) >> 16); \
168 (a)[6] = (u8) (((u64) (val)) >> 8); \
169 (a)[7] = (u8) (((u64) (val)) & 0xff); \
170 } while (0)
171
172 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
173 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
174 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
175 (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
176
177
178 /* Packing structures to avoid padding */
179 #ifdef __GNUC__
180 #define STRUCT_PACKED __attribute__ ((packed))
181 #else
182 #define STRUCT_PACKED
183 #endif
184
185 /* For md5.c file */
186 #define INTERNAL_MD5
187 #define CONFIG_CRYPTO_INTERNAL
188
189 /* For radius.c file */
190 #define CONFIG_IPV6
191 #include <arpa/inet.h>
192
193
194 #endif /* _HOSTAP_COMPAT_H */
195