1 /**
2 * @file
3 * Socket API (to be used from non-TCPIP threads)
4 */
5
6 /*
7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 *
32 * This file is part of the lwIP TCP/IP stack.
33 *
34 * Author: Adam Dunkels <adam@sics.se>
35 *
36 */
37
38
39 #ifndef LWIP_HDR_SOCKETS_H
40 #define LWIP_HDR_SOCKETS_H
41
42 #include "lwip/opt.h"
43 #include "sys/poll.h"
44 #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
45
46 #include "lwip/ip_addr.h"
47 #include "lwip/netif.h"
48 #include "lwip/err.h"
49 #include "lwip/inet.h"
50 #include "lwip/errno.h"
51 #include "../../kal/libc/newlib/porting/include/sys/uio.h"
52
53 #include <string.h>
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 /* If your port already typedef's sa_family_t, define SA_FAMILY_T_DEFINED
60 to prevent this code from redefining it. */
61 #if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED)
62 typedef u8_t sa_family_t;
63 #endif
64 /* If your port already typedef's in_port_t, define IN_PORT_T_DEFINED
65 to prevent this code from redefining it. */
66 #if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED)
67 typedef u16_t in_port_t;
68 #endif
69
70 #if LWIP_IPV4
71 /* members are in network byte order */
72 struct sockaddr_in {
73 u8_t sin_len;
74 sa_family_t sin_family;
75 in_port_t sin_port;
76 struct in_addr sin_addr;
77 #define SIN_ZERO_LEN 8
78 char sin_zero[SIN_ZERO_LEN];
79 };
80 #endif /* LWIP_IPV4 */
81
82 #if LWIP_IPV6
83 struct sockaddr_in6 {
84 u8_t sin6_len; /* length of this structure */
85 sa_family_t sin6_family; /* AF_INET6 */
86 in_port_t sin6_port; /* Transport layer port # */
87 u32_t sin6_flowinfo; /* IPv6 flow information */
88 struct in6_addr sin6_addr; /* IPv6 address */
89 u32_t sin6_scope_id; /* Set of interfaces for scope */
90 };
91 #endif /* LWIP_IPV6 */
92
93 struct sockaddr {
94 u8_t sa_len;
95 sa_family_t sa_family;
96 char sa_data[14];
97 };
98
99 struct sockaddr_storage {
100 u8_t s2_len;
101 sa_family_t ss_family;
102 char s2_data1[2];
103 u32_t s2_data2[3];
104 #if LWIP_IPV6
105 u32_t s2_data3[3];
106 #endif /* LWIP_IPV6 */
107 };
108
109 /* If your port already typedef's socklen_t, define SOCKLEN_T_DEFINED
110 to prevent this code from redefining it. */
111 #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
112 typedef u32_t socklen_t;
113 #endif
114
115 #if !defined IOV_MAX
116 #define IOV_MAX 0xFFFF
117 #elif IOV_MAX > 0xFFFF
118 #error "IOV_MAX larger than supported by LwIP"
119 #endif /* IOV_MAX */
120
121 // #if !defined(iovec)
122 // struct iovec {
123 // void *iov_base;
124 // size_t iov_len;
125 // };
126 // #endif
127
128 struct msghdr {
129 void *msg_name;
130 socklen_t msg_namelen;
131 struct iovec *msg_iov;
132 int msg_iovlen;
133 void *msg_control;
134 socklen_t msg_controllen;
135 int msg_flags;
136 };
137
138 /* struct msghdr->msg_flags bit field values */
139 #define MSG_TRUNC 0x04
140 #define MSG_CTRUNC 0x08
141
142 /* RFC 3542, Section 20: Ancillary Data */
143 struct cmsghdr {
144 socklen_t cmsg_len; /* number of bytes, including header */
145 int cmsg_level; /* originating protocol */
146 int cmsg_type; /* protocol-specific type */
147 };
148 /* Data section follows header and possible padding, typically referred to as
149 unsigned char cmsg_data[]; */
150
151 /* cmsg header/data alignment. NOTE: we align to native word size (double word
152 size on 16-bit arch) so structures are not placed at an unaligned address.
153 16-bit arch needs double word to ensure 32-bit alignment because socklen_t
154 could be 32 bits. If we ever have cmsg data with a 64-bit variable, alignment
155 will need to increase long long */
156 #define ALIGN_H(size) (((size) + sizeof(long) - 1U) & ~(sizeof(long)-1U))
157 #define ALIGN_D(size) ALIGN_H(size)
158
159 #define CMSG_FIRSTHDR(mhdr) \
160 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
161 (struct cmsghdr *)(mhdr)->msg_control : \
162 (struct cmsghdr *)NULL)
163
164 #define CMSG_NXTHDR(mhdr, cmsg) \
165 (((cmsg) == NULL) ? CMSG_FIRSTHDR(mhdr) : \
166 (((u8_t *)(cmsg) + ALIGN_H((cmsg)->cmsg_len) \
167 + ALIGN_D(sizeof(struct cmsghdr)) > \
168 (u8_t *)((mhdr)->msg_control) + (mhdr)->msg_controllen) ? \
169 (struct cmsghdr *)NULL : \
170 (struct cmsghdr *)((void*)((u8_t *)(cmsg) + \
171 ALIGN_H((cmsg)->cmsg_len)))))
172
173 #define CMSG_DATA(cmsg) ((void*)((u8_t *)(cmsg) + \
174 ALIGN_D(sizeof(struct cmsghdr))))
175
176 #define CMSG_SPACE(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
177 ALIGN_H(length))
178
179 #define CMSG_LEN(length) (ALIGN_D(sizeof(struct cmsghdr)) + \
180 length)
181
182 /* Set socket options argument */
183 #define IFNAMSIZ NETIF_NAMESIZE
184 struct ifreq {
185 char ifr_name[IFNAMSIZ]; /* Interface name */
186 };
187
188 /* Socket protocol types (TCP/UDP/RAW) */
189 #define SOCK_STREAM 1
190 #define SOCK_DGRAM 2
191 #define SOCK_RAW 3
192
193 /*
194 * Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
195 */
196 #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
197 #define SO_KEEPALIVE 0x0008 /* keep connections alive */
198 #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
199
200
201 /*
202 * Additional options, not kept in so_options.
203 */
204 #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
205 #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
206 #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
207 #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
208 #define SO_LINGER 0x0080 /* linger on close if data present */
209 #define SO_DONTLINGER ((int)(~SO_LINGER))
210 #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
211 #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
212 #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
213 #define SO_RCVBUF 0x1002 /* receive buffer size */
214 #define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */
215 #define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */
216 #define SO_SNDTIMEO 0x1005 /* send timeout */
217 #define SO_RCVTIMEO 0x1006 /* receive timeout */
218 #define SO_ERROR 0x1007 /* get error status and clear */
219 #define SO_TYPE 0x1008 /* get socket type */
220 #define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */
221 #define SO_NO_CHECK 0x100a /* don't create UDP checksum */
222 #define SO_BINDTODEVICE 0x100b /* bind to device */
223
224 /*
225 * Structure used for manipulating linger option.
226 */
227 struct linger {
228 int l_onoff; /* option on/off */
229 int l_linger; /* linger time in seconds */
230 };
231
232 /*
233 * Level number for (get/set)sockopt() to apply to socket itself.
234 */
235 #define SOL_SOCKET 0xfff /* options for socket level */
236
237
238 #define AF_UNSPEC 0
239 #define AF_INET 2
240 #if LWIP_IPV6
241 #define AF_INET6 10
242 #else /* LWIP_IPV6 */
243 #define AF_INET6 AF_UNSPEC
244 #endif /* LWIP_IPV6 */
245 #define PF_INET AF_INET
246 #define PF_INET6 AF_INET6
247 #define PF_UNSPEC AF_UNSPEC
248
249 #define IPPROTO_IP 0
250 #define IPPROTO_ICMP 1
251 #define IPPROTO_TCP 6
252 #define IPPROTO_UDP 17
253 #if LWIP_IPV6
254 #define IPPROTO_IPV6 41
255 #define IPPROTO_ICMPV6 58
256 #endif /* LWIP_IPV6 */
257 #define IPPROTO_UDPLITE 136
258 #define IPPROTO_RAW 255
259
260 /* Flags we can use with send and recv. */
261 #define MSG_PEEK 0x01 /* Peeks at an incoming message */
262 #define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
263 #define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
264 #define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
265 #define MSG_MORE 0x10 /* Sender will send more */
266 #define MSG_NOSIGNAL 0x20 /* Uninmplemented: Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. */
267
268
269 /*
270 * Options for level IPPROTO_IP
271 */
272 #define IP_TOS 1
273 #define IP_TTL 2
274 #define IP_PKTINFO 8
275
276 #if LWIP_TCP
277 /*
278 * Options for level IPPROTO_TCP
279 */
280 #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
281 #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
282 #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
283 #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
284 #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
285 #endif /* LWIP_TCP */
286
287 #if LWIP_IPV6
288 /*
289 * Options for level IPPROTO_IPV6
290 */
291 #define IPV6_CHECKSUM 7 /* RFC3542: calculate and insert the ICMPv6 checksum for raw sockets. */
292 #define IPV6_V6ONLY 27 /* RFC3493: boolean control to restrict AF_INET6 sockets to IPv6 communications only. */
293 #endif /* LWIP_IPV6 */
294
295 #if LWIP_UDP && LWIP_UDPLITE
296 /*
297 * Options for level IPPROTO_UDPLITE
298 */
299 #define UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */
300 #define UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */
301 #endif /* LWIP_UDP && LWIP_UDPLITE*/
302
303
304 #if LWIP_MULTICAST_TX_OPTIONS
305 /*
306 * Options and types for UDP multicast traffic handling
307 */
308 #define IP_MULTICAST_TTL 5
309 #define IP_MULTICAST_IF 6
310 #define IP_MULTICAST_LOOP 7
311 #endif /* LWIP_MULTICAST_TX_OPTIONS */
312
313 #if LWIP_IGMP
314 /*
315 * Options and types related to multicast membership
316 */
317 #define IP_ADD_MEMBERSHIP 3
318 #define IP_DROP_MEMBERSHIP 4
319
320 typedef struct ip_mreq {
321 struct in_addr imr_multiaddr; /* IP multicast address of group */
322 struct in_addr imr_interface; /* local IP address of interface */
323 } ip_mreq;
324 #endif /* LWIP_IGMP */
325
326 #if LWIP_IPV4
327 struct in_pktinfo {
328 unsigned int ipi_ifindex; /* Interface index */
329 struct in_addr ipi_addr; /* Destination (from header) address */
330 };
331 #endif /* LWIP_IPV4 */
332
333 #if LWIP_IPV6_MLD
334 /*
335 * Options and types related to IPv6 multicast membership
336 */
337 #define IPV6_JOIN_GROUP 12
338 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
339 #define IPV6_LEAVE_GROUP 13
340 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
341
342 #if ESP_IPV6
343 #define IPV6_MULTICAST_IF 0x300
344 #define IPV6_MULTICAST_HOPS 0x301
345 #define IPV6_MULTICAST_LOOP 0x302
346 #endif
347
348 typedef struct ipv6_mreq {
349 struct in6_addr ipv6mr_multiaddr; /* IPv6 multicast addr */
350 unsigned int ipv6mr_interface; /* interface index, or 0 */
351 } ipv6_mreq;
352 #endif /* LWIP_IPV6_MLD */
353
354 /*
355 * The Type of Service provides an indication of the abstract
356 * parameters of the quality of service desired. These parameters are
357 * to be used to guide the selection of the actual service parameters
358 * when transmitting a datagram through a particular network. Several
359 * networks offer service precedence, which somehow treats high
360 * precedence traffic as more important than other traffic (generally
361 * by accepting only traffic above a certain precedence at time of high
362 * load). The major choice is a three way tradeoff between low-delay,
363 * high-reliability, and high-throughput.
364 * The use of the Delay, Throughput, and Reliability indications may
365 * increase the cost (in some sense) of the service. In many networks
366 * better performance for one of these parameters is coupled with worse
367 * performance on another. Except for very unusual cases at most two
368 * of these three indications should be set.
369 */
370 #define IPTOS_TOS_MASK 0x1E
371 #define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
372 #define IPTOS_LOWDELAY 0x10
373 #define IPTOS_THROUGHPUT 0x08
374 #define IPTOS_RELIABILITY 0x04
375 #define IPTOS_LOWCOST 0x02
376 #define IPTOS_MINCOST IPTOS_LOWCOST
377
378 /*
379 * The Network Control precedence designation is intended to be used
380 * within a network only. The actual use and control of that
381 * designation is up to each network. The Internetwork Control
382 * designation is intended for use by gateway control originators only.
383 * If the actual use of these precedence designations is of concern to
384 * a particular network, it is the responsibility of that network to
385 * control the access to, and use of, those precedence designations.
386 */
387 #define IPTOS_PREC_MASK 0xe0
388 #define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
389 #define IPTOS_PREC_NETCONTROL 0xe0
390 #define IPTOS_PREC_INTERNETCONTROL 0xc0
391 #define IPTOS_PREC_CRITIC_ECP 0xa0
392 #define IPTOS_PREC_FLASHOVERRIDE 0x80
393 #define IPTOS_PREC_FLASH 0x60
394 #define IPTOS_PREC_IMMEDIATE 0x40
395 #define IPTOS_PREC_PRIORITY 0x20
396 #define IPTOS_PREC_ROUTINE 0x00
397
398
399 /*
400 * Commands for ioctlsocket(), taken from the BSD file fcntl.h.
401 * lwip_ioctl only supports FIONREAD and FIONBIO, for now
402 *
403 * Ioctl's have the command encoded in the lower word,
404 * and the size of any in or out parameters in the upper
405 * word. The high 2 bits of the upper word are used
406 * to encode the in/out status of the parameter; for now
407 * we restrict parameters to at most 128 bytes.
408 */
409 #if !defined(FIONREAD) || !defined(FIONBIO)
410 #define IOCPARM_MASK 0x7fU /* parameters must be < 128 bytes */
411 #define IOC_VOID 0x20000000UL /* no parameters */
412 #define IOC_OUT 0x40000000UL /* copy out parameters */
413 #define IOC_IN 0x80000000UL /* copy in parameters */
414 #define IOC_INOUT (IOC_IN|IOC_OUT)
415 /* 0x20000000 distinguishes new &
416 old ioctl's */
417 #define _IO(x,y) ((long)(IOC_VOID|((x)<<8)|(y)))
418
419 #define _IOR(x,y,t) ((long)(IOC_OUT|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)))
420
421 #define _IOW(x,y,t) ((long)(IOC_IN|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)))
422 #endif /* !defined(FIONREAD) || !defined(FIONBIO) */
423
424 #ifndef FIONREAD
425 #define FIONREAD _IOR('f', 127, unsigned long) /* get # bytes to read */
426 #endif
427 #ifndef FIONBIO
428 #define FIONBIO _IOW('f', 126, unsigned long) /* set/clear non-blocking i/o */
429 #endif
430
431 /* Socket I/O Controls: unimplemented */
432 #ifndef SIOCSHIWAT
433 #define SIOCSHIWAT _IOW('s', 0, unsigned long) /* set high watermark */
434 #define SIOCGHIWAT _IOR('s', 1, unsigned long) /* get high watermark */
435 #define SIOCSLOWAT _IOW('s', 2, unsigned long) /* set low watermark */
436 #define SIOCGLOWAT _IOR('s', 3, unsigned long) /* get low watermark */
437 #define SIOCATMARK _IOR('s', 7, unsigned long) /* at oob mark? */
438 #endif
439
440 /* commands for fnctl */
441 #ifndef F_GETFL
442 #define F_GETFL 3
443 #endif
444 #ifndef F_SETFL
445 #define F_SETFL 4
446 #endif
447
448 /* File status flags and file access modes for fnctl,
449 these are bits in an int. */
450 #ifndef O_NONBLOCK
451 #define O_NONBLOCK 1 /* nonblocking I/O */
452 #endif
453 #ifndef O_NDELAY
454 #define O_NDELAY O_NONBLOCK /* same as O_NONBLOCK, for compatibility */
455 #endif
456 #ifndef O_RDONLY
457 #define O_RDONLY 2
458 #endif
459 #ifndef O_WRONLY
460 #define O_WRONLY 4
461 #endif
462 #ifndef O_RDWR
463 #define O_RDWR (O_RDONLY|O_WRONLY)
464 #endif
465
466 #ifndef SHUT_RD
467 #define SHUT_RD 0
468 #define SHUT_WR 1
469 #define SHUT_RDWR 2
470 #endif
471
472 /* FD_SET used for lwip_select */
473 #ifndef FD_SET
474 #if 0
475 #undef FD_SETSIZE
476 /* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
477 #define FD_SETSIZE MEMP_NUM_NETCONN
478 #define LWIP_SELECT_MAXNFDS (FD_SETSIZE + LWIP_SOCKET_OFFSET)
479 #define FDSETSAFESET(n, code) do { \
480 if (((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0)) { \
481 code; }} while(0)
482 #define FDSETSAFEGET(n, code) (((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0) ?\
483 (code) : 0)
484 #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))))
485 #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))))
486 #define FD_ISSET(n,p) FDSETSAFEGET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] & (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
487 #define FD_ZERO(p) memset((void*)(p), 0, sizeof(*(p)))
488
489 typedef struct fd_set
490 {
491 unsigned char fd_bits [(FD_SETSIZE+7)/8];
492 } fd_set;
493 #else
494 #define FD_SETSIZE 1024
495
496 typedef unsigned long fd_mask;
497
498 typedef struct {
499 unsigned long fds_bits[FD_SETSIZE / 8 / sizeof(long)];
500 } fd_set;
501
502 #define FD_ZERO(s) do { int __i; unsigned long *__b=(s)->fds_bits; for(__i=sizeof (fd_set)/sizeof (long); __i; __i--) *__b++=0; } while(0)
503 #define FD_SET(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] |= (1UL<<((d)%(8*sizeof(long)))))
504 #define FD_CLR(d, s) ((s)->fds_bits[(d)/(8*sizeof(long))] &= ~(1UL<<((d)%(8*sizeof(long)))))
505 #define FD_ISSET(d, s) !!((s)->fds_bits[(d)/(8*sizeof(long))] & (1UL<<((d)%(8*sizeof(long)))))
506 #endif
507
508 #elif FD_SETSIZE < (LWIP_SOCKET_OFFSET + MEMP_NUM_NETCONN)
509 #error "external FD_SETSIZE too small for number of sockets"
510 #else
511 #define LWIP_SELECT_MAXNFDS FD_SETSIZE
512 #endif /* FD_SET */
513
514 /* poll-related defines and types */
515 /* @todo: find a better way to guard the definition of these defines and types if already defined */
516 #if !defined(POLLIN) && !defined(POLLOUT)
517 #define POLLIN 0x1
518 #define POLLOUT 0x2
519 #define POLLERR 0x4
520 #define POLLNVAL 0x8
521 /* Below values are unimplemented */
522 #define POLLRDNORM 0x10
523 #define POLLRDBAND 0x20
524 #define POLLPRI 0x40
525 #define POLLWRNORM 0x80
526 #define POLLWRBAND 0x100
527 #define POLLHUP 0x200
528 #ifdef NO_POLLFD
529 typedef unsigned int nfds_t;
530 struct pollfd
531 {
532 int fd;
533 short events;
534 short revents;
535 };
536 #endif/* NO_POLLFD*/
537 #endif
538
539 /** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided
540 * by your system, set this to 0 and include <sys/time.h> in cc.h */
541 #ifndef LWIP_TIMEVAL_PRIVATE
542 #define LWIP_TIMEVAL_PRIVATE 1
543 #endif
544
545 #if LWIP_TIMEVAL_PRIVATE
546 struct timeval {
547 long tv_sec; /* seconds */
548 long tv_usec; /* and microseconds */
549 };
550 #endif /* LWIP_TIMEVAL_PRIVATE */
551
552 #define lwip_socket_init() /* Compatibility define, no init needed. */
553 void lwip_socket_thread_init(void); /* LWIP_NETCONN_SEM_PER_THREAD==1: initialize thread-local semaphore */
554 void lwip_socket_thread_cleanup(void); /* LWIP_NETCONN_SEM_PER_THREAD==1: destroy thread-local semaphore */
555
556 #if LWIP_COMPAT_SOCKETS == 2
557 /* This helps code parsers/code completion by not having the COMPAT functions as defines */
558 #define lwip_accept accept
559 #define lwip_bind bind
560 #define lwip_shutdown shutdown
561 #define lwip_getpeername getpeername
562 #define lwip_getsockname getsockname
563 #define lwip_setsockopt setsockopt
564 #define lwip_getsockopt getsockopt
565 #define lwip_close closesocket
566 #define lwip_connect connect
567 #define lwip_listen listen
568 #define lwip_recv recv
569 #define lwip_recvmsg recvmsg
570 #define lwip_recvfrom recvfrom
571 #define lwip_send send
572 #define lwip_sendmsg sendmsg
573 #define lwip_sendto sendto
574 #define lwip_socket socket
575 #if LWIP_SOCKET_SELECT
576 #define lwip_select select
577 #endif
578 #if LWIP_SOCKET_POLL
579 #define lwip_poll poll
580 #endif
581 #define lwip_ioctl ioctlsocket
582 #define lwip_inet_ntop inet_ntop
583 #define lwip_inet_pton inet_pton
584
585 #if LWIP_POSIX_SOCKETS_IO_NAMES
586 #define lwip_read read
587 #define lwip_readv readv
588 #define lwip_write write
589 #define lwip_writev writev
590 #undef lwip_close
591 #define lwip_close close
592 #define closesocket(s) close(s)
593 int fcntl(int s, int cmd, ...);
594 #undef lwip_ioctl
595 #define lwip_ioctl ioctl
596 #define ioctlsocket ioctl
597 #endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
598 #endif /* LWIP_COMPAT_SOCKETS == 2 */
599
600 int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
601 int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
602 int lwip_shutdown(int s, int how);
603 int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen);
604 int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen);
605 int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
606 int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
607 int lwip_close(int s);
608 int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
609 int lwip_listen(int s, int backlog);
610 ssize_t lwip_recv(int s, void *mem, size_t len, int flags);
611 ssize_t lwip_read(int s, void *mem, size_t len);
612 ssize_t lwip_readv(int s, const struct iovec *iov, int iovcnt);
613 ssize_t lwip_recvfrom(int s, void *mem, size_t len, int flags,
614 struct sockaddr *from, socklen_t *fromlen);
615 ssize_t lwip_recvmsg(int s, struct msghdr *message, int flags);
616 ssize_t lwip_send(int s, const void *dataptr, size_t size, int flags);
617 ssize_t lwip_sendmsg(int s, const struct msghdr *message, int flags);
618 ssize_t lwip_sendto(int s, const void *dataptr, size_t size, int flags,
619 const struct sockaddr *to, socklen_t tolen);
620 int lwip_socket(int domain, int type, int protocol);
621 ssize_t lwip_write(int s, const void *dataptr, size_t size);
622 ssize_t lwip_writev(int s, const struct iovec *iov, int iovcnt);
623 #if LWIP_SOCKET_SELECT
624 int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
625 struct timeval *timeout);
626 #endif
627 #if LWIP_SOCKET_POLL
628 int lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout);
629 #endif
630 int lwip_ioctl(int s, long cmd, void *argp);
631 int lwip_fcntl(int s, int cmd, int val);
632 const char *lwip_inet_ntop(int af, const void *src, char *dst, socklen_t size);
633 int lwip_inet_pton(int af, const char *src, void *dst);
634
635 #if LWIP_COMPAT_SOCKETS
636 #if LWIP_COMPAT_SOCKETS != 2
637
638 #if ESP_SOCKET
accept(int s,struct sockaddr * addr,socklen_t * addrlen)639 static inline int accept(int s,struct sockaddr *addr,socklen_t *addrlen)
640 { return lwip_accept(s,addr,addrlen); }
bind(int s,const struct sockaddr * name,socklen_t namelen)641 static inline int bind(int s,const struct sockaddr *name, socklen_t namelen)
642 { return lwip_bind(s,name,namelen); }
shutdown(int s,int how)643 static inline int shutdown(int s,int how)
644 { return lwip_shutdown(s,how); }
getpeername(int s,struct sockaddr * name,socklen_t * namelen)645 static inline int getpeername(int s,struct sockaddr *name,socklen_t *namelen)
646 { return lwip_getpeername(s,name,namelen); }
getsockname(int s,struct sockaddr * name,socklen_t * namelen)647 static inline int getsockname(int s,struct sockaddr *name,socklen_t *namelen)
648 { return lwip_getsockname(s,name,namelen); }
setsockopt(int s,int level,int optname,const void * opval,socklen_t optlen)649 static inline int setsockopt(int s,int level,int optname,const void *opval,socklen_t optlen)
650 { return lwip_setsockopt(s,level,optname,opval,optlen); }
getsockopt(int s,int level,int optname,void * opval,socklen_t * optlen)651 static inline int getsockopt(int s,int level,int optname,void *opval,socklen_t *optlen)
652 { return lwip_getsockopt(s,level,optname,opval,optlen); }
closesocket(int s)653 static inline int closesocket(int s)
654 { return lwip_close(s); }
connect(int s,const struct sockaddr * name,socklen_t namelen)655 static inline int connect(int s,const struct sockaddr *name,socklen_t namelen)
656 { return lwip_connect(s,name,namelen); }
listen(int s,int backlog)657 static inline int listen(int s,int backlog)
658 { return lwip_listen(s,backlog); }
recvmsg(int sockfd,struct msghdr * msg,int flags)659 static inline ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
660 { return lwip_recvmsg(sockfd, msg, flags); }
recv(int s,void * mem,size_t len,int flags)661 static inline ssize_t recv(int s,void *mem,size_t len,int flags)
662 { return lwip_recv(s,mem,len,flags); }
recvfrom(int s,void * mem,size_t len,int flags,struct sockaddr * from,socklen_t * fromlen)663 static inline ssize_t recvfrom(int s,void *mem,size_t len,int flags,struct sockaddr *from,socklen_t *fromlen)
664 { return lwip_recvfrom(s,mem,len,flags,from,fromlen); }
send(int s,const void * dataptr,size_t size,int flags)665 static inline ssize_t send(int s,const void *dataptr,size_t size,int flags)
666 { return lwip_send(s,dataptr,size,flags); }
sendmsg(int s,const struct msghdr * message,int flags)667 static inline ssize_t sendmsg(int s,const struct msghdr *message,int flags)
668 { return lwip_sendmsg(s,message,flags); }
sendto(int s,const void * dataptr,size_t size,int flags,const struct sockaddr * to,socklen_t tolen)669 static inline ssize_t sendto(int s,const void *dataptr,size_t size,int flags,const struct sockaddr *to,socklen_t tolen)
670 { return lwip_sendto(s,dataptr,size,flags,to,tolen); }
socket(int domain,int type,int protocol)671 static inline int socket(int domain,int type,int protocol)
672 { return lwip_socket(domain,type,protocol); }
673 #ifndef ESP_HAS_SELECT
select(int maxfdp1,fd_set * readset,fd_set * writeset,fd_set * exceptset,struct timeval * timeout)674 static inline int select(int maxfdp1,fd_set *readset,fd_set *writeset,fd_set *exceptset,struct timeval *timeout)
675 { return lwip_select(maxfdp1,readset,writeset,exceptset,timeout); }
676 #endif /* ESP_HAS_SELECT */
ioctlsocket(int s,long cmd,void * argp)677 static inline int ioctlsocket(int s,long cmd,void *argp)
678 { return lwip_ioctl(s,cmd,argp); }
679
680 #if LWIP_POSIX_SOCKETS_IO_NAMES
read(int s,void * mem,size_t len)681 static inline ssize_t read(int s,void *mem,size_t len)
682 { return lwip_read(s,mem,len); }
write(int s,const void * dataptr,size_t len)683 static inline ssize_t write(int s,const void *dataptr,size_t len)
684 { return lwip_write(s,dataptr,len); }
writev(int s,const struct iovec * iov,int iovcnt)685 static inline ssize_t writev(int s,const struct iovec *iov,int iovcnt)
686 { return lwip_writev(s,iov,iovcnt); }
close(int s)687 static inline int close(int s)
688 { return lwip_close(s); }
fcntl(int s,int cmd,int val)689 static inline int fcntl(int s,int cmd,int val)
690 { return lwip_fcntl(s,cmd,val); }
ioctl(int s,long cmd,void * argp)691 static inline int ioctl(int s,long cmd,void *argp)
692 { return lwip_ioctl(s,cmd,argp); }
693 #endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
694
695 #else
696
697 /** @ingroup socket */
698 #define accept(s,addr,addrlen) lwip_accept(s,addr,addrlen)
699 /** @ingroup socket */
700 #define bind(s,name,namelen) lwip_bind(s,name,namelen)
701 /** @ingroup socket */
702 #define shutdown(s,how) lwip_shutdown(s,how)
703 /** @ingroup socket */
704 #define getpeername(s,name,namelen) lwip_getpeername(s,name,namelen)
705 /** @ingroup socket */
706 #define getsockname(s,name,namelen) lwip_getsockname(s,name,namelen)
707 /** @ingroup socket */
708 #define setsockopt(s,level,optname,opval,optlen) lwip_setsockopt(s,level,optname,opval,optlen)
709 /** @ingroup socket */
710 #define getsockopt(s,level,optname,opval,optlen) lwip_getsockopt(s,level,optname,opval,optlen)
711 /** @ingroup socket */
712 #define closesocket(s) lwip_close(s)
713 /** @ingroup socket */
714 #define connect(s,name,namelen) lwip_connect(s,name,namelen)
715 /** @ingroup socket */
716 #define listen(s,backlog) lwip_listen(s,backlog)
717 /** @ingroup socket */
718 #define recv(s,mem,len,flags) lwip_recv(s,mem,len,flags)
719 /** @ingroup socket */
720 #define recvmsg(s,message,flags) lwip_recvmsg(s,message,flags)
721 /** @ingroup socket */
722 #define recvfrom(s,mem,len,flags,from,fromlen) lwip_recvfrom(s,mem,len,flags,from,fromlen)
723 /** @ingroup socket */
724 #define send(s,dataptr,size,flags) lwip_send(s,dataptr,size,flags)
725 /** @ingroup socket */
726 #define sendmsg(s,message,flags) lwip_sendmsg(s,message,flags)
727 /** @ingroup socket */
728 #define sendto(s,dataptr,size,flags,to,tolen) lwip_sendto(s,dataptr,size,flags,to,tolen)
729 /** @ingroup socket */
730 #define socket(domain,type,protocol) lwip_socket(domain,type,protocol)
731 #if LWIP_SOCKET_SELECT
732 /** @ingroup socket */
733 #define select(maxfdp1,readset,writeset,exceptset,timeout) lwip_select(maxfdp1,readset,writeset,exceptset,timeout)
734 #endif
735 #if LWIP_SOCKET_POLL
736 /** @ingroup socket */
737 #define poll(fds,nfds,timeout) lwip_poll(fds,nfds,timeout)
738 #endif
739 /** @ingroup socket */
740 #define ioctlsocket(s,cmd,argp) lwip_ioctl(s,cmd,argp)
741
742 #if LWIP_POSIX_SOCKETS_IO_NAMES
743 /** @ingroup socket */
744 #define read(s,mem,len) lwip_read(s,mem,len)
745 /** @ingroup socket */
746 #define readv(s,iov,iovcnt) lwip_readv(s,iov,iovcnt)
747 /** @ingroup socket */
748 #define write(s,dataptr,len) lwip_write(s,dataptr,len)
749 /** @ingroup socket */
750 #define writev(s,iov,iovcnt) lwip_writev(s,iov,iovcnt)
751 /** @ingroup socket */
752 #define close(s) lwip_close(s)
753 /** @ingroup socket */
754 #define fcntl(s,cmd,val) lwip_fcntl(s,cmd,val)
755 /** @ingroup socket */
756 #define ioctl(s,cmd,argp) lwip_ioctl(s,cmd,argp)
757 #endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
758
759 #endif /* ESP_SOCKET */
760 #endif /* LWIP_COMPAT_SOCKETS != 2 */
761
762 #if ESP_SOCKET
763 #if LWIP_COMPAT_SOCKET_INET == 1
764 /* Some libraries have problems with inet_... being macros, so please use this define
765 to declare normal functions */
inet_ntop(int af,const void * src,char * dst,socklen_t size)766 static inline const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
767 { return lwip_inet_ntop(af, src, dst, size); }
inet_pton(int af,const char * src,void * dst)768 static inline int inet_pton(int af, const char *src, void *dst)
769 { return lwip_inet_pton(af, src, dst); }
770 #else
771 /* By default fall back to original inet_... macros */
772 # define inet_ntop(a,b,c,d) lwip_inet_ntop(a,b,c,d)
773 # define inet_pton(a,b,c) lwip_inet_pton(a,b,c)
774 #endif /* LWIP_COMPAT_SOCKET_INET */
775
776 #endif /* ESP_SOCKET */
777 #endif /* LWIP_COMPAT_SOCKETS */
778
779 #ifdef __cplusplus
780 }
781 #endif
782
783 #endif /* LWIP_SOCKET */
784
785 #endif /* LWIP_HDR_SOCKETS_H */
786