EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
common.h
Go to the documentation of this file.
1 
13 #ifndef COMMON_H
14 #define COMMON_H
15 
16 #include <stddef.h>
17 
18 #include "../utils/allocs.h"
19 #include "../utils/attributes.h"
20 #include "../utils/log.h"
21 
22 typedef uint64_t u64;
23 typedef uint32_t u32;
24 typedef uint16_t u16;
25 typedef uint8_t u8;
26 typedef int64_t s64;
27 typedef int32_t s32;
28 typedef int16_t s16;
29 typedef int8_t s8;
30 
31 /*
32  * Definitions for sparse validation
33  * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
34  */
35 #ifdef __CHECKER__
36 #define __force __attribute__((force))
37 #undef __bitwise
38 #define __bitwise __attribute__((bitwise))
39 #else
40 #define __force
41 #undef __bitwise
42 #define __bitwise
43 #endif
44 
45 typedef u16 __bitwise be16;
46 typedef u16 __bitwise le16;
47 typedef u32 __bitwise be32;
48 typedef u32 __bitwise le32;
49 typedef u64 __bitwise be64;
50 typedef u64 __bitwise le64;
51 
52 #ifndef BIT
53 #define BIT(x) (1U << (x))
54 #endif
55 
56 #ifndef bswap_16
57 #define bswap_16(a) ((((u16)(a) << 8) & 0xff00) | (((u16)(a) >> 8) & 0xff))
58 #endif
59 
60 #if __BYTE_ORDER == __LITTLE_ENDIAN
61 #define le_to_host16(n) ((__force u16)(le16)(n))
62 #define host_to_le16(n) ((__force le16)(u16)(n))
63 #define be_to_host16(n) bswap_16((__force u16)(be16)(n))
64 #define host_to_be16(n) ((__force be16)bswap_16((n)))
65 #define le_to_host32(n) ((__force u32)(le32)(n))
66 #define host_to_le32(n) ((__force le32)(u32)(n))
67 #define be_to_host32(n) bswap_32((__force u32)(be32)(n))
68 #define host_to_be32(n) ((__force be32)bswap_32((n)))
69 #define le_to_host64(n) ((__force u64)(le64)(n))
70 #define host_to_le64(n) ((__force le64)(u64)(n))
71 #define be_to_host64(n) bswap_64((__force u64)(be64)(n))
72 #define host_to_be64(n) ((__force be64)bswap_64((n)))
73 #elif __BYTE_ORDER == __BIG_ENDIAN
74 #define le_to_host16(n) bswap_16(n)
75 #define host_to_le16(n) bswap_16(n)
76 #define be_to_host16(n) (n)
77 #define host_to_be16(n) (n)
78 #define le_to_host32(n) bswap_32(n)
79 #define host_to_le32(n) bswap_32(n)
80 #define be_to_host32(n) (n)
81 #define host_to_be32(n) (n)
82 #define le_to_host64(n) bswap_64(n)
83 #define host_to_le64(n) bswap_64(n)
84 #define be_to_host64(n) (n)
85 #define host_to_be64(n) (n)
86 #ifndef WORDS_BIGENDIAN
87 #define WORDS_BIGENDIAN
88 #endif
89 #else
90 #error Could not determine CPU byte order
91 #endif
92 
93 static inline void WPA_PUT_LE16(u8 *a, u16 val) {
94  a[1] = val >> 8;
95  a[0] = val & 0xff;
96 }
97 
98 static inline void WPA_PUT_LE32(u8 *a, u32 val) {
99  a[3] = (val >> 24) & 0xff;
100  a[2] = (val >> 16) & 0xff;
101  a[1] = (val >> 8) & 0xff;
102  a[0] = val & 0xff;
103 }
104 
105 static inline void WPA_PUT_LE64(u8 *a, u64 val) {
106  a[7] = val >> 56;
107  a[6] = val >> 48;
108  a[5] = val >> 40;
109  a[4] = val >> 32;
110  a[3] = val >> 24;
111  a[2] = val >> 16;
112  a[1] = val >> 8;
113  a[0] = val & 0xff;
114 }
115 
116 static inline void WPA_PUT_BE16(u8 *a, u16 val) {
117  a[0] = val >> 8;
118  a[1] = val & 0xff;
119 }
120 
121 static inline void WPA_PUT_BE24(u8 *a, u32 val) {
122  a[0] = (val >> 16) & 0xff;
123  a[1] = (val >> 8) & 0xff;
124  a[2] = val & 0xff;
125 }
126 
127 static inline void WPA_PUT_BE32(u8 *a, u32 val) {
128  a[0] = (val >> 24) & 0xff;
129  a[1] = (val >> 16) & 0xff;
130  a[2] = (val >> 8) & 0xff;
131  a[3] = val & 0xff;
132 }
133 
134 static inline void WPA_PUT_BE64(u8 *a, u64 val) {
135  a[0] = val >> 56;
136  a[1] = val >> 48;
137  a[2] = val >> 40;
138  a[3] = val >> 32;
139  a[4] = val >> 24;
140  a[5] = val >> 16;
141  a[6] = val >> 8;
142  a[7] = val & 0xff;
143 }
144 
145 static inline u32 WPA_GET_BE32(const u8 *a) {
146  return ((u32)a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
147 }
148 
149 static inline u32 WPA_GET_BE24(const u8 *a) {
150  return (a[0] << 16) | (a[1] << 8) | a[2];
151 }
152 
166 static inline int os_snprintf_error(size_t size, int res) {
167  return res < 0 || (unsigned int)res >= size;
168 }
169 
185 static inline void bin_clear_free(void *bin, size_t len) {
186  if (bin) {
187  // may be optimized out by a smart compiler, we should use
188  // memset_s (C11 Annex K) or memset_explicit (C23) instead
189  os_memset(bin, '\0', len);
190  os_free(bin);
191  }
192 }
193 
201 
209  uint8_t type;
210  struct wpabuf *val;
212 };
213 
225 };
226 
234 #define wpa_printf(level, ...) \
235  log_levels(level, __FILENAME__, __LINE__, __VA_ARGS__)
236 
250 #define wpa_snprintf_hex(buf, buf_size, data, len) \
251  printf_hex(buf, buf_size, data, len, false)
252 
265 static inline void wpa_hexdump_ascii(enum hostap_log_level level,
266  const char *title, const void *buf,
267  size_t len) {
268  char hex_buf[33];
269  printf_hex(hex_buf, sizeof(hex_buf), buf, len, false);
270  log_levels(level, __FILENAME__, __LINE__, "%s - hexdump(len=%zu):%s", title,
271  len, hex_buf);
272 }
273 
274 static inline void printf_encode(char *txt, size_t maxlen, const uint8_t *data,
275  size_t len) {
276  char *end = txt + maxlen;
277  size_t i;
278 
279  for (i = 0; i < len; i++) {
280  if (txt + 4 >= end)
281  break;
282 
283  switch (data[i]) {
284  case '\"':
285  *txt++ = '\\';
286  *txt++ = '\"';
287  break;
288  case '\\':
289  *txt++ = '\\';
290  *txt++ = '\\';
291  break;
292  case '\033':
293  *txt++ = '\\';
294  *txt++ = 'e';
295  break;
296  case '\n':
297  *txt++ = '\\';
298  *txt++ = 'n';
299  break;
300  case '\r':
301  *txt++ = '\\';
302  *txt++ = 'r';
303  break;
304  case '\t':
305  *txt++ = '\\';
306  *txt++ = 't';
307  break;
308  default:
309  // check if value is a valid printable ASCII char
310  // this also confirms that we can safely cast unsigned data to signed
311  // char
312  if (data[i] >= 32 && data[i] <= 126) {
313  *txt++ = (char)data[i];
314  } else {
315  // guaranteed to be positive and to have 4 chars left, as otherwise
316  // loop will break
317  size_t max_chars_to_print = (size_t)(end - txt);
318  txt += snprintf(txt, max_chars_to_print, "\\x%02x", data[i]);
319  }
320  break;
321  }
322  }
323 
324  *txt = '\0';
325 }
326 
327 #ifndef wpa_trace_show
338 #define wpa_trace_show(s) log_trace("%s", s)
339 #endif /* wpa_trace_show */
340 
347 #define TEST_FAIL() 0
348 
349 #endif /* COMMON_H */
#define os_free(p)
Definition: allocs.h:52
#define os_memset(s, c, n)
Definition: allocs.h:69
#define __bitwise
Definition: common.h:42
int64_t s64
Definition: common.h:26
u16 __bitwise be16
Definition: common.h:45
uint64_t u64
Definition: common.h:22
u32 __bitwise le32
Definition: common.h:48
ieee802_11_reason_code
Definition: common.h:200
@ WLAN_REASON_IEEE_802_1X_AUTH_FAILED
Definition: common.h:200
u64 __bitwise be64
Definition: common.h:49
u16 __bitwise le16
Definition: common.h:46
u32 __bitwise be32
Definition: common.h:47
uint8_t u8
Definition: common.h:25
int8_t s8
Definition: common.h:29
int16_t s16
Definition: common.h:28
hostap_log_level
Definition: common.h:218
@ MSG_MSGDUMP
Definition: common.h:220
@ MSG_ERROR
Definition: common.h:224
@ MSG_DEBUG
Definition: common.h:221
@ MSG_EXCESSIVE
Definition: common.h:219
@ MSG_WARNING
Definition: common.h:223
@ MSG_INFO
Definition: common.h:222
uint16_t u16
Definition: common.h:24
u64 __bitwise le64
Definition: common.h:50
int32_t s32
Definition: common.h:27
uint32_t u32
Definition: common.h:23
T end(T... args)
T snprintf(T... args)
size_t printf_hex(char *buf, size_t buf_size, const uint8_t *data, size_t len, bool uppercase)
Prints the data in data to buf as hex.
Definition: log.c:324
void log_levels(uint8_t level, const char *file, uint32_t line, const char *format,...)
Definition: log.c:259
uint8_t level
Definition: log.c:57
@ LOGC_WARN
Definition: log.h:29
@ LOGC_DEBUG
Definition: log.h:29
@ LOGC_ERROR
Definition: log.h:29
@ LOGC_TRACE
Definition: log.h:29
@ LOGC_INFO
Definition: log.h:29
#define __FILENAME__
Definition: log.h:24
Definition: common.h:208
uint8_t type
Definition: common.h:209
struct wpabuf * val
Definition: common.h:210
struct hostapd_radius_attr * next
Definition: common.h:211
Definition: wpabuf.h:28