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
54
55
56
57
58
60
61
62
63
65
66
67
68
69
70
79
80
81
82
90
91
92
97
98
99
100
101
102
103
104
105
106
107
108
110
111
112
113
114
115
116
117
118
119
120
121
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
147
149
150
155
156
157
158
162
163
164
165
166
167
168
169
170
171
172
174
175
177
178
180
181
182
183
186
187
188
189
190
191
192
195
196
197
198
199
200
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
226
230
231
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
256
257
258
259
260
261
262
263
264
265
266
267
268
271
272
273
274
275
276
285
286
287
293
294
295
301
302
303
304
311
312
313
323
324
325
326
330
331
332
333
334
335
336
337
338
339
340
341
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
432
433
434
435
436
437
438
439
440
441
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
464
465
466
467
468
469
470
471
474
476
477
478
479
480
481
482
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
513
514
515
517
518
519
520
521
522
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
651
652
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/* ... */
/* ... */
#ifndef LWIP_HDR_SOCKETS_H
#define LWIP_HDR_SOCKETS_H
#include "lwip/opt.h"
#if LWIP_SOCKET
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/err.h"
#include "lwip/inet.h"
#include "lwip/errno.h"
#include <string.h>
6 includes
#ifdef __cplusplus
extern "C" {
#endif
/* ... */
#if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED)
typedef u8_t sa_family_t;
#endif
/* ... */
#if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED)
typedef u16_t in_port_t;
#endif
#if LWIP_IPV4
struct sockaddr_in {
u8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
#define SIN_ZERO_LEN 8
char sin_zero[SIN_ZERO_LEN];
...};/* ... */
#endif
#if LWIP_IPV6
struct sockaddr_in6 {
u8_t sin6_len;
sa_family_t sin6_family;
in_port_t sin6_port;
u32_t sin6_flowinfo;
struct in6_addr sin6_addr;
u32_t sin6_scope_id;
...};/* ... */
#endif
struct sockaddr {
u8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
...};
struct sockaddr_storage {
u8_t s2_len;
sa_family_t ss_family;
char s2_data1[2];
u32_t s2_data2[3];
#if LWIP_IPV6
u32_t s2_data3[3];
#endif
...};
/* ... */
#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
typedef u32_t socklen_t;
#endif
#if !defined IOV_MAX
#define IOV_MAX 0xFFFF
#elif IOV_MAX > 0xFFFF
#error "IOV_MAX larger than supported by LwIP"
#endif
#if !defined(iovec)
struct iovec {
void *iov_base;
size_t iov_len;
...};/* ... */
#endif
struct msghdr {
void *msg_name;
socklen_t msg_namelen;
struct iovec *msg_iov;
int msg_iovlen;
void *msg_control;
socklen_t msg_controllen;
int msg_flags;
...};
#define MSG_TRUNC 0x04
#define MSG_CTRUNC 0x08
struct cmsghdr {
socklen_t cmsg_len;
int cmsg_level;
int cmsg_type;
...};
/* ... */
/* ... */
#define ALIGN_H(size) (((size) + sizeof(long) - 1U) & ~(sizeof(long)-1U))
#define ALIGN_D(size) ALIGN_H(size)
#define CMSG_FIRSTHDR(mhdr) \
((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
(struct cmsghdr *)(mhdr)->msg_control : \
(struct cmsghdr *)NULL)...
#define CMSG_NXTHDR(mhdr, cmsg) \
(((cmsg) == NULL) ? CMSG_FIRSTHDR(mhdr) : \
(((u8_t *)(cmsg) + ALIGN_H((cmsg)->cmsg_len) \
+ ALIGN_D(sizeof(struct cmsghdr)) > \
(u8_t *)((mhdr)->msg_control) + (mhdr)->msg_controllen) ? \
(struct cmsghdr *)NULL : \
(struct cmsghdr *)((void*)((u8_t *)(cmsg) + \
ALIGN_H((cmsg)->cmsg_len)))))...
#define CMSG_DATA(cmsg) ((void*)((u8_t *)(cmsg) + \
ALIGN_D(sizeof(struct cmsghdr))))...
#define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
ALIGN_H(length))...
#define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
length)...
#define IFNAMSIZ NETIF_NAMESIZE
8 definesstruct ifreq {
char ifr_name[IFNAMSIZ];
...};
#define SOCK_STREAM 1
#define SOCK_DGRAM 2
#define SOCK_RAW 3
/* ... */
#define SO_REUSEADDR 0x0004
#define SO_KEEPALIVE 0x0008
#define SO_BROADCAST 0x0020
/* ... */
#define SO_DEBUG 0x0001
#define SO_ACCEPTCONN 0x0002
#define SO_DONTROUTE 0x0010
#define SO_USELOOPBACK 0x0040
#define SO_LINGER 0x0080
#define SO_DONTLINGER ((int)(~SO_LINGER))
#define SO_OOBINLINE 0x0100
#define SO_REUSEPORT 0x0200
#define SO_SNDBUF 0x1001
#define SO_RCVBUF 0x1002
#define SO_SNDLOWAT 0x1003
#define SO_RCVLOWAT 0x1004
#define SO_SNDTIMEO 0x1005
#define SO_RCVTIMEO 0x1006
#define SO_ERROR 0x1007
#define SO_TYPE 0x1008
#define SO_CONTIMEO 0x1009
#define SO_NO_CHECK 0x100a
#define SO_BINDTODEVICE 0x100b
25 defines
/* ... */
struct linger {
int l_onoff;
int l_linger;
...};
/* ... */
#define SOL_SOCKET 0xfff
#define AF_UNSPEC 0
#define AF_INET 2
#if LWIP_IPV6
#define AF_INET6 10
#else
#define AF_INET6 AF_UNSPEC
#endif
#define PF_INET AF_INET
#define PF_INET6 AF_INET6
#define PF_UNSPEC AF_UNSPEC
#define IPPROTO_IP 0
#define IPPROTO_ICMP 1
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
7 defines#if LWIP_IPV6
#define IPPROTO_IPV6 41
#define IPPROTO_ICMPV6 58
/* ... */#endif
#define IPPROTO_UDPLITE 136
#define IPPROTO_RAW 255
#define MSG_PEEK 0x01
#define MSG_WAITALL 0x02
#define MSG_OOB 0x04
#define MSG_DONTWAIT 0x08
#define MSG_MORE 0x10
#define MSG_NOSIGNAL 0x20
/* ... */
#define IP_TOS 1
#define IP_TTL 2
#define IP_PKTINFO 8
11 defines
#if LWIP_TCP
/* ... */
#define TCP_NODELAY 0x01
#define TCP_KEEPALIVE 0x02
#define TCP_KEEPIDLE 0x03
#define TCP_KEEPINTVL 0x04
#define TCP_KEEPCNT 0x05
5 defines#endif/* ... */
#if LWIP_IPV6
/* ... */
#define IPV6_CHECKSUM 7
#define IPV6_V6ONLY 27
/* ... */#endif
#if LWIP_UDP && LWIP_UDPLITE
/* ... */
#define UDPLITE_SEND_CSCOV 0x01
#define UDPLITE_RECV_CSCOV 0x02
/* ... */#endif
#if LWIP_MULTICAST_TX_OPTIONS
/* ... */
#define IP_MULTICAST_TTL 5
#define IP_MULTICAST_IF 6
#define IP_MULTICAST_LOOP 7
/* ... */#endif
#if LWIP_IGMP
/* ... */
#define IP_ADD_MEMBERSHIP 3
#define IP_DROP_MEMBERSHIP 4
typedef struct ip_mreq {
struct in_addr imr_multiaddr;
struct in_addr imr_interface;
...} ip_mreq;/* ... */
#endif
#if LWIP_IPV4
struct in_pktinfo {
unsigned int ipi_ifindex;
struct in_addr ipi_addr;
...};/* ... */
#endif
#if LWIP_IPV6_MLD
/* ... */
#define IPV6_JOIN_GROUP 12
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#define IPV6_LEAVE_GROUP 13
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
typedef struct ipv6_mreq {
struct in6_addr ipv6mr_multiaddr;
unsigned int ipv6mr_interface;
...} ipv6_mreq;/* ... */
#endif
/* ... */
#define IPTOS_TOS_MASK 0x1E
#define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
#define IPTOS_LOWDELAY 0x10
#define IPTOS_THROUGHPUT 0x08
#define IPTOS_RELIABILITY 0x04
#define IPTOS_LOWCOST 0x02
#define IPTOS_MINCOST IPTOS_LOWCOST
/* ... */
#define IPTOS_PREC_MASK 0xe0
#define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
#define IPTOS_PREC_NETCONTROL 0xe0
#define IPTOS_PREC_INTERNETCONTROL 0xc0
#define IPTOS_PREC_CRITIC_ECP 0xa0
#define IPTOS_PREC_FLASHOVERRIDE 0x80
#define IPTOS_PREC_FLASH 0x60
#define IPTOS_PREC_IMMEDIATE 0x40
#define IPTOS_PREC_PRIORITY 0x20
#define IPTOS_PREC_ROUTINE 0x00
17 defines
/* ... */
#if !defined(FIONREAD) || !defined(FIONBIO)
#define IOCPARM_MASK 0x7fU
#define IOC_VOID 0x20000000UL
#define IOC_OUT 0x40000000UL
#define IOC_IN 0x80000000UL
#define IOC_INOUT (IOC_IN|IOC_OUT)
/* ... */
#define _IO(x,y) ((long)(IOC_VOID|((x)<<8)|(y)))
#define _IOR(x,y,t) ((long)(IOC_OUT|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)))
#define _IOW(x,y,t) ((long)(IOC_IN|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)))
/* ... */#endif
#ifndef FIONREAD
#define FIONREAD _IOR('f', 127, unsigned long)
#endif
#ifndef FIONBIO
#define FIONBIO _IOW('f', 126, unsigned long)
#endif
#ifndef SIOCSHIWAT
#define SIOCSHIWAT _IOW('s', 0, unsigned long)
#define SIOCGHIWAT _IOR('s', 1, unsigned long)
#define SIOCSLOWAT _IOW('s', 2, unsigned long)
#define SIOCGLOWAT _IOR('s', 3, unsigned long)
#define SIOCATMARK _IOR('s', 7, unsigned long)
/* ... */#endif
#ifndef F_GETFL
#define F_GETFL 3
#endif
#ifndef F_SETFL
#define F_SETFL 4
#endif
/* ... */
#ifndef O_NONBLOCK
#define O_NONBLOCK 1
#endif
#ifndef O_NDELAY
#define O_NDELAY O_NONBLOCK
#endif
#ifndef O_RDONLY
#define O_RDONLY 2
#endif
#ifndef O_WRONLY
#define O_WRONLY 4
#endif
#ifndef O_RDWR
#define O_RDWR (O_RDONLY|O_WRONLY)
#endif
#ifndef SHUT_RD
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
/* ... */#endif
#ifndef FD_SET
#undef FD_SETSIZE
#define FD_SETSIZE MEMP_NUM_NETCONN
#define LWIP_SELECT_MAXNFDS (FD_SETSIZE + LWIP_SOCKET_OFFSET)
#define FDSETSAFESET(n, code) do { \
if (((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0)) { \
code; }if (((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0)) { ... }}... while(0)...
#define FDSETSAFEGET(n, code) (((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0) ?\
(code) : 0)...
#define FD_SET(n, p) FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] = (u8_t)((p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] | (1 << (((n)-LWIP_SOCKET_OFFSET) & 7))))
#define FD_CLR(n, p) FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] = (u8_t)((p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] & ~(1 << (((n)-LWIP_SOCKET_OFFSET) & 7))))
#define FD_ISSET(n,p) FDSETSAFEGET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] & (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
#define FD_ZERO(p) memset((void*)(p), 0, sizeof(*(p)))
8 defines
typedef struct fd_set
{
unsigned char fd_bits [(FD_SETSIZE+7)/8];
...} fd_set;
/* ... */
#elif FD_SETSIZE < (LWIP_SOCKET_OFFSET + MEMP_NUM_NETCONN)
#error "external FD_SETSIZE too small for number of sockets"
#else
#define LWIP_SELECT_MAXNFDS FD_SETSIZE
#endif
#if !defined(POLLIN) && !defined(POLLOUT)
#define POLLIN 0x1
#define POLLOUT 0x2
#define POLLERR 0x4
#define POLLNVAL 0x8
#define POLLRDNORM 0x10
#define POLLRDBAND 0x20
#define POLLPRI 0x40
#define POLLWRNORM 0x80
#define POLLWRBAND 0x100
#define POLLHUP 0x200
10 definestypedef unsigned int nfds_t;
struct pollfd
{
int fd;
short events;
short revents;
...};/* ... */
#endif
/* ... */
#ifndef LWIP_TIMEVAL_PRIVATE
#define LWIP_TIMEVAL_PRIVATE 1
#endif
#if LWIP_TIMEVAL_PRIVATE
struct timeval {
long tv_sec;
long tv_usec;
...};/* ... */
#endif
#define lwip_socket_init()
void lwip_socket_thread_init(void);
void lwip_socket_thread_cleanup(void);
#if LWIP_COMPAT_SOCKETS == 2
#define lwip_accept accept
#define lwip_bind bind
#define lwip_shutdown shutdown
#define lwip_getpeername getpeername
#define lwip_getsockname getsockname
#define lwip_setsockopt setsockopt
#define lwip_getsockopt getsockopt
#define lwip_close closesocket
#define lwip_connect connect
#define lwip_listen listen
#define lwip_recv recv
#define lwip_recvmsg recvmsg
#define lwip_recvfrom recvfrom
#define lwip_send send
#define lwip_sendmsg sendmsg
#define lwip_sendto sendto
#define lwip_socket socket
17 defines#if LWIP_SOCKET_SELECT
#define lwip_select select
#endif
#if LWIP_SOCKET_POLL
#define lwip_poll poll
#endif
#define lwip_ioctl ioctlsocket
#define lwip_inet_ntop inet_ntop
#define lwip_inet_pton inet_pton
#if LWIP_POSIX_SOCKETS_IO_NAMES
#define lwip_read read
#define lwip_readv readv
#define lwip_write write
#define lwip_writev writev
#undef lwip_close
#define lwip_close close
#define closesocket(s) close(s)
int fcntl(int s, int cmd, ...);
#undef lwip_ioctl
#define lwip_ioctl ioctl
#define ioctlsocket ioctl
/* ... */#endif /* ... */
#endif
int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
int lwip_shutdown(int s, int how);
int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen);
int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen);
int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
int lwip_close(int s);
int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
int lwip_listen(int s, int backlog);
ssize_t lwip_recv(int s, void *mem, size_t len, int flags);
ssize_t lwip_read(int s, void *mem, size_t len);
ssize_t lwip_readv(int s, const struct iovec *iov, int iovcnt);
ssize_t lwip_recvfrom(int s, void *mem, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen);
ssize_t lwip_recvmsg(int s, struct msghdr *message, int flags);
ssize_t lwip_send(int s, const void *dataptr, size_t size, int flags);
ssize_t lwip_sendmsg(int s, const struct msghdr *message, int flags);
ssize_t lwip_sendto(int s, const void *dataptr, size_t size, int flags,
const struct sockaddr *to, socklen_t tolen);
int lwip_socket(int domain, int type, int protocol);
ssize_t lwip_write(int s, const void *dataptr, size_t size);
ssize_t lwip_writev(int s, const struct iovec *iov, int iovcnt);
#if LWIP_SOCKET_SELECT
int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
struct timeval *timeout);/* ... */
#endif
#if LWIP_SOCKET_POLL
int lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout);
#endif
int lwip_ioctl(int s, long cmd, void *argp);
int lwip_fcntl(int s, int cmd, int val);
const char *lwip_inet_ntop(int af, const void *src, char *dst, socklen_t size);
int lwip_inet_pton(int af, const char *src, void *dst);
#if LWIP_COMPAT_SOCKETS
#if LWIP_COMPAT_SOCKETS != 2
#define accept(s,addr,addrlen) lwip_accept(s,addr,addrlen)
#define bind(s,name,namelen) lwip_bind(s,name,namelen)
#define shutdown(s,how) lwip_shutdown(s,how)
#define getpeername(s,name,namelen) lwip_getpeername(s,name,namelen)
#define getsockname(s,name,namelen) lwip_getsockname(s,name,namelen)
#define setsockopt(s,level,optname,opval,optlen) lwip_setsockopt(s,level,optname,opval,optlen)
#define getsockopt(s,level,optname,opval,optlen) lwip_getsockopt(s,level,optname,opval,optlen)
#define closesocket(s) lwip_close(s)
#define connect(s,name,namelen) lwip_connect(s,name,namelen)
#define listen(s,backlog) lwip_listen(s,backlog)
#define recv(s,mem,len,flags) lwip_recv(s,mem,len,flags)
#define recvmsg(s,message,flags) lwip_recvmsg(s,message,flags)
#define recvfrom(s,mem,len,flags,from,fromlen) lwip_recvfrom(s,mem,len,flags,from,fromlen)
#define send(s,dataptr,size,flags) lwip_send(s,dataptr,size,flags)
#define sendmsg(s,message,flags) lwip_sendmsg(s,message,flags)
#define sendto(s,dataptr,size,flags,to,tolen) lwip_sendto(s,dataptr,size,flags,to,tolen)
#define socket(domain,type,protocol) lwip_socket(domain,type,protocol)
17 defines#if LWIP_SOCKET_SELECT
#define select(maxfdp1,readset,writeset,exceptset,timeout) lwip_select(maxfdp1,readset,writeset,exceptset,timeout)
/* ... */#endif
#if LWIP_SOCKET_POLL
#define poll(fds,nfds,timeout) lwip_poll(fds,nfds,timeout)
/* ... */#endif
#define ioctlsocket(s,cmd,argp) lwip_ioctl(s,cmd,argp)
#define inet_ntop(af,src,dst,size) lwip_inet_ntop(af,src,dst,size)
#define inet_pton(af,src,dst) lwip_inet_pton(af,src,dst)
#if LWIP_POSIX_SOCKETS_IO_NAMES
#define read(s,mem,len) lwip_read(s,mem,len)
#define readv(s,iov,iovcnt) lwip_readv(s,iov,iovcnt)
#define write(s,dataptr,len) lwip_write(s,dataptr,len)
#define writev(s,iov,iovcnt) lwip_writev(s,iov,iovcnt)
#define close(s) lwip_close(s)
#define fcntl(s,cmd,val) lwip_fcntl(s,cmd,val)
#define ioctl(s,cmd,argp) lwip_ioctl(s,cmd,argp)
7 defines#endif/* ... */ /* ... */
#endif
/* ... */
#endif
#ifdef __cplusplus
}extern "C" { ... }
#endif
/* ... */
#endif
/* ... */
#endif