1
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
52
53
54
55
56
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
80
82
83
84
89
90
93
94
95
96
97
98
99
100
105
109
110
113
114
115
116
117
118
120
121
122
123
124
125
126
127
128
129
135
136
137
138
139
140
142
143
144
145
147
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
188
189
190
195
196
197
198
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
230
231
232
233
235
236
238
239
240
243
244
245
248
249
250
251
252
253
254
257
258
259
262
263
264
271
272
273
275
276
277
278
279
283
284
285
286
287
288
293
299
300
301
304
305
306
307
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
350
351
352
353
354
358
359
360
367
368
369
370
371
372
373
374
375
376
377
381
382
383
384
385
397
398
399
404
405
410
411
416
417
418
420
421
424
425
426
427
428
429
430
431
/* ... */
/* ... */
#ifndef LWIP_HDR_API_H
#define LWIP_HDR_API_H
#include "lwip/opt.h"
#if LWIP_NETCONN || LWIP_SOCKET
/* ... */
#include "lwip/arch.h"
#include "lwip/netbuf.h"
#include "lwip/sys.h"
#include "lwip/ip_addr.h"
#include "lwip/err.h"
5 includes
#ifdef __cplusplus
extern "C" {
#endif
/* ... */
#define NETCONN_NOFLAG 0x00
#define NETCONN_NOCOPY 0x00
#define NETCONN_COPY 0x01
#define NETCONN_MORE 0x02
#define NETCONN_DONTBLOCK 0x04
#define NETCONN_NOAUTORCVD 0x08
#define NETCONN_NOFIN 0x10
#define NETCONN_FLAG_MBOXCLOSED 0x01
#define NETCONN_FLAG_NON_BLOCKING 0x02
#define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04
10 defines#if LWIP_NETCONN_FULLDUPLEX
#define NETCONN_FLAG_MBOXINVALID 0x08
/* ... */#endif
/* ... */
#define NETCONN_FLAG_CHECK_WRITESPACE 0x10
#if LWIP_IPV6
/* ... */
#define NETCONN_FLAG_IPV6_V6ONLY 0x20
/* ... */#endif
#if LWIP_NETBUF_RECVINFO
#define NETCONN_FLAG_PKTINFO 0x40
/* ... */#endif
#define NETCONN_FIN_RX_PENDING 0x80
#define NETCONNTYPE_GROUP(t) ((t)&0xF0)
#define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0)
#if LWIP_IPV6
#define NETCONN_TYPE_IPV6 0x08
#define NETCONNTYPE_ISIPV6(t) (((t)&NETCONN_TYPE_IPV6) != 0)
#define NETCONNTYPE_ISUDPLITE(t) (((t)&0xF3) == NETCONN_UDPLITE)
#define NETCONNTYPE_ISUDPNOCHKSUM(t) (((t)&0xF3) == NETCONN_UDPNOCHKSUM)
/* ... */#else
#define NETCONNTYPE_ISIPV6(t) (0)
#define NETCONNTYPE_ISUDPLITE(t) ((t) == NETCONN_UDPLITE)
#define NETCONNTYPE_ISUDPNOCHKSUM(t) ((t) == NETCONN_UDPNOCHKSUM)
/* ... */#endif
/* ... */
enum netconn_type {
NETCONN_INVALID = 0,
NETCONN_TCP = 0x10,
#if LWIP_IPV6
NETCONN_TCP_IPV6 = NETCONN_TCP | NETCONN_TYPE_IPV6 ,/* ... */
#endif
NETCONN_UDP = 0x20,
NETCONN_UDPLITE = 0x21,
NETCONN_UDPNOCHKSUM = 0x22,
#if LWIP_IPV6
NETCONN_UDP_IPV6 = NETCONN_UDP | NETCONN_TYPE_IPV6 ,
NETCONN_UDPLITE_IPV6 = NETCONN_UDPLITE | NETCONN_TYPE_IPV6 ,
NETCONN_UDPNOCHKSUM_IPV6 = NETCONN_UDPNOCHKSUM | NETCONN_TYPE_IPV6 ,/* ... */
#endif
NETCONN_RAW = 0x40
#if LWIP_IPV6
, NETCONN_RAW_IPV6 = NETCONN_RAW | NETCONN_TYPE_IPV6 /* ... */
#endif
...};
/* ... */
enum netconn_state {
NETCONN_NONE,
NETCONN_WRITE,
NETCONN_LISTEN,
NETCONN_CONNECT,
NETCONN_CLOSE
...};
/* ... */
enum netconn_evt {
NETCONN_EVT_RCVPLUS,
NETCONN_EVT_RCVMINUS,
NETCONN_EVT_SENDPLUS,
NETCONN_EVT_SENDMINUS,
NETCONN_EVT_ERROR
...};
#if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
enum netconn_igmp {
NETCONN_JOIN,
NETCONN_LEAVE
...};/* ... */
#endif
#if LWIP_DNS
#define NETCONN_DNS_DEFAULT NETCONN_DNS_IPV4_IPV6
#define NETCONN_DNS_IPV4 0
#define NETCONN_DNS_IPV6 1
#define NETCONN_DNS_IPV4_IPV6 2
#define NETCONN_DNS_IPV6_IPV4 3
5 defines#endif/* ... */
struct ip_pcb;
struct tcp_pcb;
struct udp_pcb;
struct raw_pcb;
struct netconn;
struct api_msg;
typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
struct netconn {
enum netconn_type type;
enum netconn_state state;
union {
struct ip_pcb *ip;
struct tcp_pcb *tcp;
struct udp_pcb *udp;
struct raw_pcb *raw;
...} pcb;
err_t pending_err;
#if !LWIP_NETCONN_SEM_PER_THREAD
sys_sem_t op_completed;/* ... */
#endif
/* ... */
sys_mbox_t recvmbox;
#if LWIP_TCP
/* ... */
sys_mbox_t acceptmbox;/* ... */
#endif
#if LWIP_NETCONN_FULLDUPLEX
/* ... */
int mbox_threads_waiting;/* ... */
#endif
#if LWIP_SOCKET
int socket;
#endif
#if LWIP_SO_SNDTIMEO
/* ... */
s32_t send_timeout;/* ... */
#endif
#if LWIP_SO_RCVTIMEO
/* ... */
u32_t recv_timeout;/* ... */
#endif
#if LWIP_SO_RCVBUF
/* ... */
int recv_bufsize;
/* ... */
int recv_avail;/* ... */
#endif
#if LWIP_SO_LINGER
s16_t linger;/* ... */
#endif
u8_t flags;
#if LWIP_TCP
/* ... */
struct api_msg *current_msg;/* ... */
#endif
netconn_callback callback;
...};
/* ... */
struct netvector {
const void *ptr;
size_t len;
...};
#define API_EVENT(c,e,l) if (c->callback) { \
(*c->callback)(c, e, l); \
}...
/* ... */
#define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
#define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
struct netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
netconn_callback callback);
err_t netconn_prepare_delete(struct netconn *conn);
err_t netconn_delete(struct netconn *conn);
#define netconn_type(conn) (conn->type)
err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr,
u16_t *port, u8_t local);
#define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
#define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
err_t netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port);
err_t netconn_bind_if(struct netconn *conn, u8_t if_idx);
err_t netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port);
err_t netconn_disconnect (struct netconn *conn);
err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
#define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
err_t netconn_accept(struct netconn *conn, struct netconn **new_conn);
err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf);
err_t netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf);
err_t netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags);
err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf);
err_t netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags);
err_t netconn_tcp_recvd(struct netconn *conn, size_t len);
err_t netconn_sendto(struct netconn *conn, struct netbuf *buf,
const ip_addr_t *addr, u16_t port);
err_t netconn_send(struct netconn *conn, struct netbuf *buf);
err_t netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
u8_t apiflags, size_t *bytes_written);
err_t netconn_write_vectors_partly(struct netconn *conn, struct netvector *vectors, u16_t vectorcnt,
u8_t apiflags, size_t *bytes_written);
#define netconn_write(conn, dataptr, size, apiflags) \
netconn_write_partly(conn, dataptr, size, apiflags, NULL)...
err_t netconn_close(struct netconn *conn);
err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx);
#if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
err_t netconn_join_leave_group(struct netconn *conn, const ip_addr_t *multiaddr,
const ip_addr_t *netif_addr, enum netconn_igmp join_or_leave);
err_t netconn_join_leave_group_netif(struct netconn *conn, const ip_addr_t *multiaddr,
u8_t if_idx, enum netconn_igmp join_or_leave);/* ... */
#endif
#if LWIP_DNS
#if LWIP_IPV4 && LWIP_IPV6
err_t netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype);
#define netconn_gethostbyname(name, addr) netconn_gethostbyname_addrtype(name, addr, NETCONN_DNS_DEFAULT)
/* ... */#else
err_t netconn_gethostbyname(const char *name, ip_addr_t *addr);
#define netconn_gethostbyname_addrtype(name, addr, dns_addrtype) netconn_gethostbyname(name, addr)
/* ... */#endif /* ... */
#endif
err_t netconn_err(struct netconn *conn);
#define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
#define netconn_set_flags(conn, set_flags) do { (conn)->flags = (u8_t)((conn)->flags | (set_flags)); } while(0)
#define netconn_clear_flags(conn, clr_flags) do { (conn)->flags = (u8_t)((conn)->flags & (u8_t)(~(clr_flags) & 0xff)); } while(0)
#define netconn_is_flag_set(conn, flag) (((conn)->flags & (flag)) != 0)
#define netconn_set_nonblocking(conn, val) do { if(val) { \
netconn_set_flags(conn, NETCONN_FLAG_NON_BLOCKING); \
}if (val) { ... } else { \
netconn_clear_flags(conn, NETCONN_FLAG_NON_BLOCKING); }else { ... }}... while(0)...
#define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
6 defines
#if LWIP_IPV6
/* ... */
#define netconn_set_ipv6only(conn, val) do { if(val) { \
netconn_set_flags(conn, NETCONN_FLAG_IPV6_V6ONLY); \
}if (val) { ... } else { \
netconn_clear_flags(conn, NETCONN_FLAG_IPV6_V6ONLY); }else { ... }}... while(0)...
/* ... */
#define netconn_get_ipv6only(conn) (((conn)->flags & NETCONN_FLAG_IPV6_V6ONLY) != 0)
/* ... */#endif
#if LWIP_SO_SNDTIMEO
#define netconn_set_sendtimeout(conn, timeout) ((conn)->send_timeout = (timeout))
#define netconn_get_sendtimeout(conn) ((conn)->send_timeout)
/* ... */#endif
#if LWIP_SO_RCVTIMEO
#define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout))
#define netconn_get_recvtimeout(conn) ((conn)->recv_timeout)
/* ... */#endif
#if LWIP_SO_RCVBUF
#define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize))
#define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize)
/* ... */#endif
#if LWIP_NETCONN_SEM_PER_THREAD
void netconn_thread_init(void);
void netconn_thread_cleanup(void);/* ... */
#else
#define netconn_thread_init()
#define netconn_thread_cleanup()
/* ... */#endif
#ifdef __cplusplus
}extern "C" { ... }
#endif
/* ... */
#endif
/* ... */
#endif