2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
34 #ifndef __LWIP_SOCKETS_H__
35 #define __LWIP_SOCKETS_H__
39 #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
41 #include <stddef.h> /* for size_t */
43 #include "lwip/ip_addr.h"
44 #include "lwip/inet.h"
45 #include "lwip/inet6.h"
51 /* members are in network byte order */
56 struct in_addr sin_addr;
57 #define SIN_ZERO_LEN 8
58 char sin_zero[SIN_ZERO_LEN];
63 u8_t sin6_len; /* length of this structure */
64 u8_t sin6_family; /* AF_INET6 */
65 u16_t sin6_port; /* Transport layer port # */
66 u32_t sin6_flowinfo; /* IPv6 flow information */
67 struct in6_addr sin6_addr; /* IPv6 address */
69 #endif /* LWIP_IPV6 */
78 #endif /* LWIP_IPV6 */
82 # define socklen_t u32_t
85 /* Socket protocol types (TCP/UDP/RAW) */
91 * Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
93 #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
94 #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
95 #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
96 #define SO_KEEPALIVE 0x0008 /* keep connections alive */
97 #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
98 #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
99 #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
100 #define SO_LINGER 0x0080 /* linger on close if data present */
101 #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
102 #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
104 #define SO_DONTLINGER ((int)(~SO_LINGER))
107 * Additional options, not kept in so_options.
109 #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
110 #define SO_RCVBUF 0x1002 /* receive buffer size */
111 #define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */
112 #define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */
113 #define SO_SNDTIMEO 0x1005 /* Unimplemented: send timeout */
114 #define SO_RCVTIMEO 0x1006 /* receive timeout */
115 #define SO_ERROR 0x1007 /* get error status and clear */
116 #define SO_TYPE 0x1008 /* get socket type */
117 #define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */
118 #define SO_NO_CHECK 0x100a /* don't create UDP checksum */
122 * Structure used for manipulating linger option.
125 int l_onoff; /* option on/off */
126 int l_linger; /* linger time */
130 * Level number for (get/set)sockopt() to apply to socket itself.
132 #define SOL_SOCKET 0xfff /* options for socket level */
139 #else /* LWIP_IPV6 */
140 #define AF_INET6 AF_UNSPEC
141 #endif /* LWIP_IPV6 */
142 #define PF_INET AF_INET
143 #define PF_INET6 AF_INET6
144 #define PF_UNSPEC AF_UNSPEC
147 #define IPPROTO_TCP 6
148 #define IPPROTO_UDP 17
149 #define IPPROTO_UDPLITE 136
151 /* Flags we can use with send and recv. */
152 #define MSG_PEEK 0x01 /* Peeks at an incoming message */
153 #define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
154 #define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
155 #define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
156 #define MSG_MORE 0x10 /* Sender will send more */
160 * Options for level IPPROTO_IP
167 * Options for level IPPROTO_TCP
169 #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
170 #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
171 #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
172 #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
173 #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
174 #endif /* LWIP_TCP */
176 #if LWIP_UDP && LWIP_UDPLITE
178 * Options for level IPPROTO_UDPLITE
180 #define UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */
181 #define UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */
182 #endif /* LWIP_UDP && LWIP_UDPLITE*/
187 * Options and types for UDP multicast traffic handling
189 #define IP_ADD_MEMBERSHIP 3
190 #define IP_DROP_MEMBERSHIP 4
191 #define IP_MULTICAST_TTL 5
192 #define IP_MULTICAST_IF 6
193 #define IP_MULTICAST_LOOP 7
195 typedef struct ip_mreq {
196 struct in_addr imr_multiaddr; /* IP multicast address of group */
197 struct in_addr imr_interface; /* local IP address of interface */
199 #endif /* LWIP_IGMP */
202 * The Type of Service provides an indication of the abstract
203 * parameters of the quality of service desired. These parameters are
204 * to be used to guide the selection of the actual service parameters
205 * when transmitting a datagram through a particular network. Several
206 * networks offer service precedence, which somehow treats high
207 * precedence traffic as more important than other traffic (generally
208 * by accepting only traffic above a certain precedence at time of high
209 * load). The major choice is a three way tradeoff between low-delay,
210 * high-reliability, and high-throughput.
211 * The use of the Delay, Throughput, and Reliability indications may
212 * increase the cost (in some sense) of the service. In many networks
213 * better performance for one of these parameters is coupled with worse
214 * performance on another. Except for very unusual cases at most two
215 * of these three indications should be set.
217 #define IPTOS_TOS_MASK 0x1E
218 #define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
219 #define IPTOS_LOWDELAY 0x10
220 #define IPTOS_THROUGHPUT 0x08
221 #define IPTOS_RELIABILITY 0x04
222 #define IPTOS_LOWCOST 0x02
223 #define IPTOS_MINCOST IPTOS_LOWCOST
226 * The Network Control precedence designation is intended to be used
227 * within a network only. The actual use and control of that
228 * designation is up to each network. The Internetwork Control
229 * designation is intended for use by gateway control originators only.
230 * If the actual use of these precedence designations is of concern to
231 * a particular network, it is the responsibility of that network to
232 * control the access to, and use of, those precedence designations.
234 #define IPTOS_PREC_MASK 0xe0
235 #define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
236 #define IPTOS_PREC_NETCONTROL 0xe0
237 #define IPTOS_PREC_INTERNETCONTROL 0xc0
238 #define IPTOS_PREC_CRITIC_ECP 0xa0
239 #define IPTOS_PREC_FLASHOVERRIDE 0x80
240 #define IPTOS_PREC_FLASH 0x60
241 #define IPTOS_PREC_IMMEDIATE 0x40
242 #define IPTOS_PREC_PRIORITY 0x20
243 #define IPTOS_PREC_ROUTINE 0x00
247 * Commands for ioctlsocket(), taken from the BSD file fcntl.h.
248 * lwip_ioctl only supports FIONREAD and FIONBIO, for now
250 * Ioctl's have the command encoded in the lower word,
251 * and the size of any in or out parameters in the upper
252 * word. The high 2 bits of the upper word are used
253 * to encode the in/out status of the parameter; for now
254 * we restrict parameters to at most 128 bytes.
256 #if !defined(FIONREAD) || !defined(FIONBIO)
257 #define IOCPARM_MASK 0x7fU /* parameters must be < 128 bytes */
258 #define IOC_VOID 0x20000000UL /* no parameters */
259 #define IOC_OUT 0x40000000UL /* copy out parameters */
260 #define IOC_IN 0x80000000UL /* copy in parameters */
261 #define IOC_INOUT (IOC_IN|IOC_OUT)
262 /* 0x20000000 distinguishes new &
264 #define _IO(x,y) (IOC_VOID|((x)<<8)|(y))
266 #define _IOR(x,y,t) (IOC_OUT|(((long)sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y))
268 #define _IOW(x,y,t) (IOC_IN|(((long)sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y))
269 #endif /* !defined(FIONREAD) || !defined(FIONBIO) */
272 #define FIONREAD _IOR('f', 127, unsigned long) /* get # bytes to read */
275 #define FIONBIO _IOW('f', 126, unsigned long) /* set/clear non-blocking i/o */
278 /* Socket I/O Controls: unimplemented */
280 #define SIOCSHIWAT _IOW('s', 0, unsigned long) /* set high watermark */
281 #define SIOCGHIWAT _IOR('s', 1, unsigned long) /* get high watermark */
282 #define SIOCSLOWAT _IOW('s', 2, unsigned long) /* set low watermark */
283 #define SIOCGLOWAT _IOR('s', 3, unsigned long) /* get low watermark */
284 #define SIOCATMARK _IOR('s', 7, unsigned long) /* at oob mark? */
287 /* commands for fnctl */
295 /* File status flags and file access modes for fnctl,
296 these are bits in an int. */
298 #define O_NONBLOCK 1 /* nonblocking I/O */
301 #define O_NDELAY 1 /* same as O_NONBLOCK, for compatibility */
310 /* FD_SET used for lwip_select */
313 /* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
314 #define FD_SETSIZE MEMP_NUM_NETCONN
315 #define FD_SET(n, p) ((p)->fd_bits[(n)/8] |= (1 << ((n) & 7)))
316 #define FD_CLR(n, p) ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7)))
317 #define FD_ISSET(n,p) ((p)->fd_bits[(n)/8] & (1 << ((n) & 7)))
318 #define FD_ZERO(p) memset((void*)(p),0,sizeof(*(p)))
320 typedef struct fd_set {
321 unsigned char fd_bits [(FD_SETSIZE+7)/8];
326 /** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided
327 * by your system, set this to 0 and include <sys/time.h> in cc.h */
328 #ifndef LWIP_TIMEVAL_PRIVATE
329 #define LWIP_TIMEVAL_PRIVATE 1
332 #if LWIP_TIMEVAL_PRIVATE
334 long tv_sec; /* seconds */
335 long tv_usec; /* and microseconds */
337 #endif /* LWIP_TIMEVAL_PRIVATE */
339 void lwip_socket_init(void);
341 int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
342 int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
343 int lwip_shutdown(int s, int how);
344 int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen);
345 int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen);
346 int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
347 int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
348 int lwip_close(int s);
349 int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
350 int lwip_listen(int s, int backlog);
351 int lwip_recv(int s, void *mem, size_t len, int flags);
352 int lwip_read(int s, void *mem, size_t len);
353 int lwip_recvfrom(int s, void *mem, size_t len, int flags,
354 struct sockaddr *from, socklen_t *fromlen);
355 int lwip_send(int s, const void *dataptr, size_t size, int flags);
356 int lwip_sendto(int s, const void *dataptr, size_t size, int flags,
357 const struct sockaddr *to, socklen_t tolen);
358 int lwip_socket(int domain, int type, int protocol);
359 int lwip_write(int s, const void *dataptr, size_t size);
360 int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
361 struct timeval *timeout);
362 int lwip_ioctl(int s, long cmd, void *argp);
363 int lwip_fcntl(int s, int cmd, int val);
365 #if LWIP_COMPAT_SOCKETS
366 #define accept(a,b,c) lwip_accept(a,b,c)
367 #define bind(a,b,c) lwip_bind(a,b,c)
368 #define shutdown(a,b) lwip_shutdown(a,b)
369 #define closesocket(s) lwip_close(s)
370 #define connect(a,b,c) lwip_connect(a,b,c)
371 #define getsockname(a,b,c) lwip_getsockname(a,b,c)
372 #define getpeername(a,b,c) lwip_getpeername(a,b,c)
373 #define setsockopt(a,b,c,d,e) lwip_setsockopt(a,b,c,d,e)
374 #define getsockopt(a,b,c,d,e) lwip_getsockopt(a,b,c,d,e)
375 #define listen(a,b) lwip_listen(a,b)
376 #define recv(a,b,c,d) lwip_recv(a,b,c,d)
377 #define recvfrom(a,b,c,d,e,f) lwip_recvfrom(a,b,c,d,e,f)
378 #define send(a,b,c,d) lwip_send(a,b,c,d)
379 #define sendto(a,b,c,d,e,f) lwip_sendto(a,b,c,d,e,f)
380 #define socket(a,b,c) lwip_socket(a,b,c)
381 #define select(a,b,c,d,e) lwip_select(a,b,c,d,e)
382 #define ioctlsocket(a,b,c) lwip_ioctl(a,b,c)
384 #if LWIP_POSIX_SOCKETS_IO_NAMES
385 #define read(a,b,c) lwip_read(a,b,c)
386 #define write(a,b,c) lwip_write(a,b,c)
387 #define close(s) lwip_close(s)
388 #endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
390 #endif /* LWIP_COMPAT_SOCKETS */
396 #endif /* LWIP_SOCKET */
398 #endif /* __LWIP_SOCKETS_H__ */