-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathwolfip.h
More file actions
329 lines (286 loc) · 9.47 KB
/
wolfip.h
File metadata and controls
329 lines (286 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#ifndef WOLFIP_H
#define WOLFIP_H
#include <stdint.h>
#if !defined(_SIZE_T) && !defined(_SIZE_T_DEFINED) && !defined(_SIZE_T_DECLARED) && \
!defined(_BSD_SIZE_T_DEFINED_) && !defined(__DEFINED_size_t) && \
!defined(__size_t_defined)
#if defined(__SIZE_TYPE__)
typedef __SIZE_TYPE__ size_t;
#elif defined(_MSC_VER)
#ifdef _WIN64
typedef unsigned __int64 size_t;
#else
typedef unsigned int size_t;
#endif
#else
typedef unsigned long size_t;
#endif
#define _SIZE_T
#define _SIZE_T_DEFINED
#define _SIZE_T_DECLARED
#define _BSD_SIZE_T_DEFINED_
#define __DEFINED_size_t
#define __size_t_defined
#endif
#ifndef WOLFIP_SOL_IP
#ifdef SOL_IP
#define WOLFIP_SOL_IP SOL_IP
#else
#define WOLFIP_SOL_IP 0
#endif
#endif
#ifndef WOLFIP_IP_RECVTTL
#ifdef IP_RECVTTL
#define WOLFIP_IP_RECVTTL IP_RECVTTL
#else
#define WOLFIP_IP_RECVTTL 12
#endif
#endif
/* Types */
struct wolfIP;
typedef uint32_t ip4;
/* Macros, compiler specific. */
#define PACKED __attribute__((packed))
#define ee16(x) __builtin_bswap16(x)
#define ee32(x) __builtin_bswap32(x)
#define DEBUG
#ifndef WOLFIP_EAGAIN
#ifdef EAGAIN
#define WOLFIP_EAGAIN EAGAIN
#else
#define WOLFIP_EAGAIN (11)
#endif
#endif
#ifndef WOLFIP_EINVAL
#ifdef EINVAL
#define WOLFIP_EINVAL EINVAL
#else
#define WOLFIP_EINVAL (22)
#endif
#endif
#ifndef WOLFIP_ENOMEM
#ifdef ENOMEM
#define WOLFIP_ENOMEM ENOMEM
#else
#define WOLFIP_ENOMEM (12)
#endif
#endif
#ifndef WOLFIP_EACCES
#ifdef EACCES
#define WOLFIP_EACCES EACCES
#else
#define WOLFIP_EACCES (13)
#endif
#endif
#ifdef DEBUG
#include <stdio.h>
#define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define LOG(fmt, ...) do{}while(0)
#endif
/* Device driver interface */
/* Struct to contain link-layer (ll) device description
*/
struct wolfIP_ll_dev {
uint8_t mac[6];
char ifname[16];
uint8_t non_ethernet;
/* poll function */
int (*poll)(struct wolfIP_ll_dev *ll, void *buf, uint32_t len);
/* send function */
int (*send)(struct wolfIP_ll_dev *ll, void *buf, uint32_t len);
};
/* Struct to contain an IP device configuration */
struct ipconf {
struct wolfIP_ll_dev *ll;
ip4 ip;
ip4 mask;
ip4 gw;
};
/* Socket interface */
#define MARK_TCP_SOCKET 0x100 /* Mark a socket as TCP */
#define MARK_UDP_SOCKET 0x200 /* Mark a socket as UDP */
#define MARK_ICMP_SOCKET 0x400 /* Mark a socket as ICMP */
#define IS_SOCKET_TCP(fd) (((fd) & MARK_TCP_SOCKET) == MARK_TCP_SOCKET)
#define IS_SOCKET_UDP(fd) (((fd) & MARK_UDP_SOCKET) == MARK_UDP_SOCKET)
#define IS_SOCKET_ICMP(fd)(((fd) & MARK_ICMP_SOCKET) == MARK_ICMP_SOCKET)
#define SOCKET_UNMARK(fd) ((fd) & 0xFF)
/* Compile-time sanity check for socket marks & number of sockets */
#if (MARK_TCP_SOCKET >= MARK_UDP_SOCKET)
#error "MARK_TCP_SOCKET must be less than MARK_UDP_SOCKET"
#endif
#if (MARK_UDP_SOCKET >= MARK_ICMP_SOCKET)
#error "MARK_UDP_SOCKET must be less than MARK_ICMP_SOCKET"
#endif
#if MAX_TCPSOCKETS > 255
#error "MAX_TCPSOCKETS must be less than 256"
#endif
#if MAX_UDPSOCKETS > 255
#error "MAX_UDPSOCKETS must be less than 256"
#endif
#if MAX_ICMPSOCKETS > 255
#error "MAX_ICMPSOCKETS must be less than 256"
#endif
#ifndef WOLF_POSIX
#define IPSTACK_SOCK_STREAM 1
#define IPSTACK_SOCK_DGRAM 2
struct wolfIP_sockaddr_in {
uint16_t sin_family;
uint16_t sin_port;
struct sin_addr { uint32_t s_addr; } sin_addr;
};
struct wolfIP_sockaddr { uint16_t sa_family; };
typedef uint32_t socklen_t;
#if defined(__has_include)
#if __has_include(<sys/socket.h>)
#include <sys/socket.h>
#include <sys/uio.h>
#define WOLFIP_HAVE_POSIX_TYPES 1
#endif
#endif
#ifndef WOLFIP_HAVE_POSIX_TYPES
struct iovec { void *iov_base; size_t iov_len; };
struct msghdr {
void *msg_name;
socklen_t msg_namelen;
struct iovec *msg_iov;
size_t msg_iovlen;
void *msg_control;
size_t msg_controllen;
int msg_flags;
};
#endif
#ifndef AF_INET
#define AF_INET 2
#endif
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/uio.h>
#define wolfIP_sockaddr_in sockaddr_in
#define wolfIP_sockaddr sockaddr
#endif
int wolfIP_sock_socket(struct wolfIP *s, int domain, int type, int protocol);
int wolfIP_sock_bind(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr *addr,
socklen_t addrlen);
int wolfIP_sock_listen(struct wolfIP *s, int sockfd, int backlog);
int wolfIP_sock_accept(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr,
socklen_t *addrlen);
int wolfIP_sock_connect(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr *addr,
socklen_t addrlen);
int wolfIP_sock_sendto(struct wolfIP *s, int sockfd, const void *buf, size_t len,
int flags, const struct wolfIP_sockaddr *dest_addr,
socklen_t addrlen);
int wolfIP_sock_send(struct wolfIP *s, int sockfd, const void *buf, size_t len,
int flags);
int wolfIP_sock_write(struct wolfIP *s, int sockfd, const void *buf, size_t len);
int wolfIP_sock_recvfrom(struct wolfIP *s, int sockfd, void *buf, size_t len,
int flags, struct wolfIP_sockaddr *src_addr, socklen_t *addrlen);
int wolfIP_sock_recv(struct wolfIP *s, int sockfd, void *buf, size_t len, int flags);
int wolfIP_sock_sendmsg(struct wolfIP *s, int sockfd, const struct msghdr *msg,
int flags);
int wolfIP_sock_recvmsg(struct wolfIP *s, int sockfd, struct msghdr *msg,
int flags);
int wolfIP_dns_ptr_lookup(struct wolfIP *s, uint32_t ip, uint16_t *id,
void (*lookup_cb)(const char *name));
int wolfIP_sock_get_recv_ttl(struct wolfIP *s, int sockfd, int *ttl);
int wolfIP_sock_setsockopt(struct wolfIP *s, int sockfd, int level, int optname,
const void *optval, socklen_t optlen);
int wolfIP_sock_getsockopt(struct wolfIP *s, int sockfd, int level, int optname,
void *optval, socklen_t *optlen);
int wolfIP_sock_read(struct wolfIP *s, int sockfd, void *buf, size_t len);
int wolfIP_sock_close(struct wolfIP *s, int sockfd);
int wolfIP_sock_getpeername(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr,
const socklen_t *addrlen);
int wolfIP_sock_getsockname(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr,
const socklen_t *addrlen);
int wolfIP_sock_can_read(struct wolfIP *s, int sockfd);
int wolfIP_sock_can_write(struct wolfIP *s, int sockfd);
int dhcp_client_init(struct wolfIP *s);
int dhcp_bound(struct wolfIP *s);
int dhcp_client_is_running(struct wolfIP *s);
/* DNS client */
int nslookup(struct wolfIP *s, const char *name, uint16_t *id,
void (*lookup_cb)(uint32_t ip));
#if CONFIG_IPFILTER
#include "wolfip-filter.h"
#endif
/* IP stack interface */
void wolfIP_init(struct wolfIP *s);
void wolfIP_init_static(struct wolfIP **s);
size_t wolfIP_instance_size(void);
int wolfIP_poll(struct wolfIP *s, uint64_t now);
void wolfIP_recv(struct wolfIP *s, void *buf, uint32_t len);
void wolfIP_recv_ex(struct wolfIP *s, unsigned int if_idx, void *buf, uint32_t len);
void wolfIP_ipconfig_set(struct wolfIP *s, ip4 ip, ip4 mask, ip4 gw);
void wolfIP_ipconfig_get(struct wolfIP *s, ip4 *ip, ip4 *mask, ip4 *gw);
struct wolfIP_ll_dev *wolfIP_getdev(struct wolfIP *s);
struct wolfIP_ll_dev *wolfIP_getdev_ex(struct wolfIP *s, unsigned int if_idx);
void wolfIP_ipconfig_set_ex(struct wolfIP *s, unsigned int if_idx, ip4 ip, ip4 mask, ip4 gw);
void wolfIP_ipconfig_get_ex(struct wolfIP *s, unsigned int if_idx, ip4 *ip, ip4 *mask, ip4 *gw);
/* Callback flags */
#define CB_EVENT_READABLE 0x01 /* Accepted connection or data available */
#define CB_EVENT_TIMEOUT 0x02 /* Timeout */
#define CB_EVENT_WRITABLE 0x04 /* Connected or space available to send */
#define CB_EVENT_CLOSED 0x10 /* Connection closed by peer */
typedef void (*tsocket_cb)(int sock_fd, uint16_t events, void *arg);
void wolfIP_register_callback(struct wolfIP *s, int sock_fd, tsocket_cb cb,
void *arg);
/* External requirements */
uint32_t wolfIP_getrandom(void);
/* Inline utility functions */
static inline uint32_t atou(const char *s)
{
uint32_t ret = 0;
while (*s >= '0' && *s <= '9') {
ret = (ret * 10u) + (uint32_t)(*s - '0');
s++;
}
return ret;
}
static inline ip4 atoip4(const char *ip)
{
ip4 ret = 0;
int i = 0;
int j = 0;
for (i = 0; i < 4; i++) {
ret |= (atou(ip + j) << (24 - i * 8));
while (ip[j] != '.' && ip[j] != '\0') j++;
if (ip[j] == '\0') break;
j++;
}
return ret;
}
static inline void iptoa(ip4 ip, char *buf)
{
int i, j = 0;
buf[0] = 0;
for (i = 0; i < 4; i++) {
uint8_t x = (ip >> (24 - i * 8)) & 0xFF;
if (x > 99) buf[j++] = (char)(x / 100 + '0');
if (x > 9) buf[j++] = (char)(((x / 10) % 10) + '0');
buf[j++] = (char)((x % 10) + '0');
if (i < 3) buf[j++] = '.';
}
buf[j] = 0;
}
#ifdef WOLFSSL_WOLFIP
#ifdef WOLFSSL_USER_SETTINGS
#include "user_settings.h"
#else
#include <wolfssl/options.h>
#endif /* WOLFSSL_USER_SETTINGS */
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/ssl.h>
int wolfSSL_SetIO_wolfIP(WOLFSSL* ssl, int fd);
int wolfSSL_SetIO_wolfIP_CTX(WOLFSSL_CTX *ctx, struct wolfIP *s);
#ifdef WOLFIP_ESP
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/random.h>
#endif /* WOLFIP_ESP */
#endif /* WOLFSSL_WOLFIP */
#endif /* !WOLFIP_H */