EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
wpabuf.h
Go to the documentation of this file.
1 
13 #ifndef WPABUF_H
14 #define WPABUF_H
15 
16 #include "common.h"
17 #include "utils/allocs.h"
18 #include "utils/os.h"
19 
20 /* wpabuf::buf is a pointer to external data */
21 #define WPABUF_FLAG_EXT_DATA BIT(0)
22 
23 /*
24  * Internal data structure for wpabuf. Please do not touch this directly from
25  * elsewhere. This is only defined in header file to allow inline functions
26  * from this file to access data.
27  */
28 struct wpabuf {
29  size_t size; /* total size of the allocated buffer */
30  size_t used; /* length of data in the buffer */
31  u8 *buf; /* pointer to the head of the buffer */
32  unsigned int flags;
33  /* optionally followed by the allocated buffer */
34 };
35 
36 int wpabuf_resize(struct wpabuf **buf, size_t add_len);
37 struct wpabuf *wpabuf_alloc(size_t len);
38 struct wpabuf *wpabuf_alloc_ext_data(u8 *data, size_t len);
39 struct wpabuf *wpabuf_alloc_copy(const void *data, size_t len);
40 struct wpabuf *wpabuf_dup(const struct wpabuf *src);
41 void wpabuf_free(struct wpabuf *buf);
42 void wpabuf_clear_free(struct wpabuf *buf);
43 void *wpabuf_put(struct wpabuf *buf, size_t len);
44 struct wpabuf *wpabuf_concat(struct wpabuf *a, struct wpabuf *b);
45 struct wpabuf *wpabuf_zeropad(struct wpabuf *buf, size_t len);
46 void wpabuf_printf(struct wpabuf *buf, char *fmt, ...) PRINTF_FORMAT(2, 3);
47 struct wpabuf *wpabuf_parse_bin(const char *buf);
48 
54 static inline size_t wpabuf_size(const struct wpabuf *buf) { return buf->size; }
55 
61 static inline size_t wpabuf_len(const struct wpabuf *buf) { return buf->used; }
62 
68 static inline size_t wpabuf_tailroom(const struct wpabuf *buf) {
69  return buf->size - buf->used;
70 }
71 
78 static inline int wpabuf_cmp(const struct wpabuf *a, const struct wpabuf *b) {
79  if (!a && !b)
80  return 0;
81  if (a && b && wpabuf_size(a) == wpabuf_size(b))
82  return os_memcmp(a->buf, b->buf, wpabuf_size(a));
83  return -1;
84 }
85 
91 static inline const void *wpabuf_head(const struct wpabuf *buf) {
92  return buf->buf;
93 }
94 
95 static inline const u8 *wpabuf_head_u8(const struct wpabuf *buf) {
96  return (const u8 *)wpabuf_head(buf);
97 }
98 
104 static inline void *wpabuf_mhead(struct wpabuf *buf) { return buf->buf; }
105 
106 static inline u8 *wpabuf_mhead_u8(struct wpabuf *buf) {
107  return (u8 *)wpabuf_mhead(buf);
108 }
109 
110 static inline void wpabuf_put_u8(struct wpabuf *buf, u8 data) {
111  u8 *pos = (u8 *)wpabuf_put(buf, 1);
112  *pos = data;
113 }
114 
115 static inline void wpabuf_put_le16(struct wpabuf *buf, u16 data) {
116  u8 *pos = (u8 *)wpabuf_put(buf, 2);
117  WPA_PUT_LE16(pos, data);
118 }
119 
120 static inline void wpabuf_put_le32(struct wpabuf *buf, u32 data) {
121  u8 *pos = (u8 *)wpabuf_put(buf, 4);
122  WPA_PUT_LE32(pos, data);
123 }
124 
125 static inline void wpabuf_put_le64(struct wpabuf *buf, u64 data) {
126  u8 *pos = (u8 *)wpabuf_put(buf, 8);
127  WPA_PUT_LE64(pos, data);
128 }
129 
130 static inline void wpabuf_put_be16(struct wpabuf *buf, u16 data) {
131  u8 *pos = (u8 *)wpabuf_put(buf, 2);
132  WPA_PUT_BE16(pos, data);
133 }
134 
135 static inline void wpabuf_put_be24(struct wpabuf *buf, u32 data) {
136  u8 *pos = (u8 *)wpabuf_put(buf, 3);
137  WPA_PUT_BE24(pos, data);
138 }
139 
140 static inline void wpabuf_put_be32(struct wpabuf *buf, u32 data) {
141  u8 *pos = (u8 *)wpabuf_put(buf, 4);
142  WPA_PUT_BE32(pos, data);
143 }
144 
145 static inline void wpabuf_put_be64(struct wpabuf *buf, u64 data) {
146  u8 *pos = (u8 *)wpabuf_put(buf, 8);
147  WPA_PUT_BE64(pos, data);
148 }
149 
150 static inline void wpabuf_put_data(struct wpabuf *buf, const void *data,
151  size_t len) {
152  if (data)
153  os_memcpy(wpabuf_put(buf, len), data, len);
154 }
155 
156 static inline void wpabuf_put_buf(struct wpabuf *dst,
157  const struct wpabuf *src) {
158  wpabuf_put_data(dst, wpabuf_head(src), wpabuf_len(src));
159 }
160 
161 static inline void wpabuf_set(struct wpabuf *buf, const void *data,
162  size_t len) {
163  buf->buf = (u8 *)data;
164  buf->flags = WPABUF_FLAG_EXT_DATA;
165  buf->size = buf->used = len;
166 }
167 
168 static inline void wpabuf_put_str(struct wpabuf *dst, const char *str) {
169  wpabuf_put_data(dst, str, os_strlen(str));
170 }
171 
172 #endif /* WPABUF_H */
File containing the definition of the allocs functionalities.
#define os_memcpy(d, s, n)
Definition: allocs.h:63
#define os_memcmp(s1, s2, n)
Definition: allocs.h:72
File containing the common definitions used by radius and eap.
uint64_t u64
Definition: common.h:22
uint8_t u8
Definition: common.h:25
uint16_t u16
Definition: common.h:24
uint32_t u32
Definition: common.h:23
T data(T... args)
#define PRINTF_FORMAT(a, b)
Definition: log.h:50
File containing the definition of the os functionalities.
#define os_strlen(s)
Definition: os.h:53
Definition: wpabuf.h:28
size_t used
Definition: wpabuf.h:30
size_t size
Definition: wpabuf.h:29
unsigned int flags
Definition: wpabuf.h:32
u8 * buf
Definition: wpabuf.h:31
#define WPABUF_FLAG_EXT_DATA
Definition: wpabuf.h:21
struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len)
Definition: wpabuf.c:251
int wpabuf_resize(struct wpabuf **buf, size_t add_len)
Definition: wpabuf.c:46
struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b)
Definition: wpabuf.c:215
void wpabuf_printf(struct wpabuf *buf, char *fmt,...) PRINTF_FORMAT(2
struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len)
Definition: wpabuf.c:126
struct wpabuf * wpabuf_alloc(size_t len)
Definition: wpabuf.c:106
struct wpabuf * wpabuf_dup(const struct wpabuf *src)
Definition: wpabuf.c:156
void wpabuf_free(struct wpabuf *buf)
Definition: wpabuf.c:167
void struct wpabuf * wpabuf_parse_bin(const char *buf)
Definition: wpabuf.c:292
struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len)
Definition: wpabuf.c:149
void * wpabuf_put(struct wpabuf *buf, size_t len)
Definition: wpabuf.c:197
void wpabuf_clear_free(struct wpabuf *buf)
Definition: wpabuf.c:190