1/*
2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
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.
15 *
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
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <adam@sics.se>
30 *
31 */
32
33
34#ifndef __LWIP_SOCKETS_H__
35#define __LWIP_SOCKETS_H__
36
37#include "lwip/opt.h"
38
39#if LWIP_SOCKET                 /* don't build if not configured for use in lwipopts.h */
40
41#include <stddef.h>             /* for size_t */
42#include <fcntl.h>
43/* We use our own verions of FD_SETSIZE, FD_SET, FD_ISSET, FD_ZERO, FD_CLR */
44#include <sys/select.h>
45#include <sys/socket.h>
46#include <netinet/in.h>
47
48#include "lwip/ip_addr.h"
49#include "lwip/inet.h"
50
51// #ifdef __cplusplus
52// extern "C" {
53// #endif
54//
55// /* Socket protocol types (TCP/UDP/RAW) */
56// #define SOCK_STREAM     1
57// #define SOCK_DGRAM      2
58// #define SOCK_RAW        3
59//
60// /*
61//  * Option flags per-socket. These must match the SOF_ flags in ip.h!
62//  */
63// #define  SO_DEBUG       0x0001  /* Unimplemented: turn on debugging info recording */
64// #define  SO_ACCEPTCONN  0x0002  /* socket has had listen() */
65// #define  SO_REUSEADDR   0x0004 /* Unimplemented: allow local address reuse */
66// #define  SO_KEEPALIVE   0x0008  /* keep connections alive */
67// #define  SO_DONTROUTE   0x0010  /* Unimplemented: just use interface addresses */
68// #define  SO_BROADCAST   0x0020  /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
69// #define  SO_USELOOPBACK 0x0040  /* Unimplemented: bypass hardware when possible */
70// #define  SO_LINGER      0x0080  /* linger on close if data present */
71// #define  SO_OOBINLINE   0x0100  /* Unimplemented: leave received OOB data in line */
72// #define  SO_REUSEPORT   0x0200 /* Unimplemented: allow local address & port reuse */
73//
74// #define SO_DONTLINGER   ((int)(~SO_LINGER))
75//
76// /*
77//  * Additional options, not kept in so_options.
78//  */
79// #define SO_SNDBUF    0x1001     /* Unimplemented: send buffer size */
80// #define SO_RCVBUF    0x1002     /* receive buffer size */
81// #define SO_SNDLOWAT  0x1003     /* Unimplemented: send low-water mark */
82// #define SO_RCVLOWAT  0x1004     /* Unimplemented: receive low-water mark */
83// #define SO_SNDTIMEO  0x1005     /* Unimplemented: send timeout */
84// #define SO_RCVTIMEO  0x1006     /* receive timeout */
85// #define SO_ERROR     0x1007     /* get error status and clear */
86// #define SO_TYPE      0x1008     /* get socket type */
87// #define SO_CONTIMEO  0x1009     /* Unimplemented: connect timeout */
88#define SO_NO_CHECK  0x100a     /* don't create UDP checksum */
89//
90//
91// /*
92//  * Structure used for manipulating linger option.
93//  */
94//     struct linger {
95//         int l_onoff;            /* option on/off */
96//         int l_linger;           /* linger time */
97//     };
98//
99// /*
100//  * Level number for (get/set)sockopt() to apply to socket itself.
101//  */
102// #define  SOL_SOCKET  0xfff      /* options for socket level */
103//
104//
105// #define AF_UNSPEC       0
106// #define AF_INET         2
107// #define PF_INET         AF_INET
108// #define PF_UNSPEC       AF_UNSPEC
109//
110// #define IPPROTO_IP      0
111// #define IPPROTO_TCP     6
112// #define IPPROTO_UDP     17
113// #define IPPROTO_UDPLITE 136
114//
115/* Flags we can use with send and recv. */
116// #define MSG_PEEK       0x01     /* Peeks at an incoming message */
117// #define MSG_WAITALL    0x02     /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
118// #define MSG_OOB        0x04     /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
119// #define MSG_DONTWAIT   0x08     /* Nonblocking i/o for this operation only */
120#define MSG_MORE       0x10     /* Sender will send more */
121//
122//
123// /*
124//  * Options for level IPPROTO_IP
125//  */
126// #define IP_TOS             1
127// #define IP_TTL             2
128//
129#if LWIP_TCP
130/*
131 * Options for level IPPROTO_TCP
132 */
133#define TCP_NODELAY    0x01     /* don't delay send to coalesce packets */
134#define TCP_KEEPALIVE  0x02     /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
135#define TCP_KEEPIDLE   0x03     /* set pcb->keep_idle  - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
136#define TCP_KEEPINTVL  0x04     /* set pcb->keep_intvl - Use seconds for get/setsockopt */
137#define TCP_KEEPCNT    0x05     /* set pcb->keep_cnt   - Use number of probes sent for get/setsockopt */
138#endif                          /* LWIP_TCP */
139//
140// #if LWIP_UDP && LWIP_UDPLITE
141// /*
142//  * Options for level IPPROTO_UDPLITE
143//  */
144// #define UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */
145// #define UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */
146// #endif                          /* LWIP_UDP && LWIP_UDPLITE */
147//
148//
149// #if LWIP_IGMP
150// /*
151//  * Options and types for UDP multicast traffic handling
152//  */
153// #define IP_ADD_MEMBERSHIP  3
154// #define IP_DROP_MEMBERSHIP 4
155// #define IP_MULTICAST_TTL   5
156// #define IP_MULTICAST_IF    6
157// #define IP_MULTICAST_LOOP  7
158//
159//     typedef struct ip_mreq {
160//         struct in_addr imr_multiaddr;   /* IP multicast address of group */
161//         struct in_addr imr_interface;   /* local IP address of interface */
162//     } ip_mreq;
163// #endif                          /* LWIP_IGMP */
164//
165// /*
166//  * The Type of Service provides an indication of the abstract
167//  * parameters of the quality of service desired.  These parameters are
168//  * to be used to guide the selection of the actual service parameters
169//  * when transmitting a datagram through a particular network.  Several
170//  * networks offer service precedence, which somehow treats high
171//  * precedence traffic as more important than other traffic (generally
172//  * by accepting only traffic above a certain precedence at time of high
173//  * load).  The major choice is a three way tradeoff between low-delay,
174//  * high-reliability, and high-throughput.
175//  * The use of the Delay, Throughput, and Reliability indications may
176//  * increase the cost (in some sense) of the service.  In many networks
177//  * better performance for one of these parameters is coupled with worse
178//  * performance on another.  Except for very unusual cases at most two
179//  * of these three indications should be set.
180//  */
181// #define IPTOS_TOS_MASK          0x1E
182// #define IPTOS_TOS(tos)          ((tos) & IPTOS_TOS_MASK)
183// #define IPTOS_LOWDELAY          0x10
184// #define IPTOS_THROUGHPUT        0x08
185// #define IPTOS_RELIABILITY       0x04
186// #define IPTOS_LOWCOST           0x02
187// #define IPTOS_MINCOST           IPTOS_LOWCOST
188//
189// /*
190//  * The Network Control precedence designation is intended to be used
191//  * within a network only.  The actual use and control of that
192//  * designation is up to each network. The Internetwork Control
193//  * designation is intended for use by gateway control originators only.
194//  * If the actual use of these precedence designations is of concern to
195//  * a particular network, it is the responsibility of that network to
196//  * control the access to, and use of, those precedence designations.
197//  */
198// #define IPTOS_PREC_MASK                 0xe0
199// #define IPTOS_PREC(tos)                ((tos) & IPTOS_PREC_MASK)
200// #define IPTOS_PREC_NETCONTROL           0xe0
201// #define IPTOS_PREC_INTERNETCONTROL      0xc0
202// #define IPTOS_PREC_CRITIC_ECP           0xa0
203// #define IPTOS_PREC_FLASHOVERRIDE        0x80
204// #define IPTOS_PREC_FLASH                0x60
205// #define IPTOS_PREC_IMMEDIATE            0x40
206// #define IPTOS_PREC_PRIORITY             0x20
207// #define IPTOS_PREC_ROUTINE              0x00
208//
209//
210// /*
211//  * Commands for ioctlsocket(),  taken from the BSD file fcntl.h.
212//  * lwip_ioctl only supports FIONREAD and FIONBIO, for now
213//  *
214//  * Ioctl's have the command encoded in the lower word,
215//  * and the size of any in or out parameters in the upper
216//  * word.  The high 2 bits of the upper word are used
217//  * to encode the in/out status of the parameter; for now
218//  * we restrict parameters to at most 128 bytes.
219//  */
220// #if !defined(FIONREAD) || !defined(FIONBIO)
221// #include <sys/ioccom.h>
222// #endif                          /* !defined(FIONREAD) || !defined(FIONBIO) */
223//
224// #ifndef FIONREAD
225// #define FIONREAD    _IOR('f', 127, unsigned long)       /* get # bytes to read */
226// #endif
227// #ifndef FIONBIO
228// #define FIONBIO     _IOW('f', 126, unsigned long)       /* set/clear non-blocking i/o */
229// #endif
230//
231// /* Socket I/O Controls: unimplemented */
232// #ifndef SIOCSHIWAT
233// #define SIOCSHIWAT  _IOW('s',  0, unsigned long)        /* set high watermark */
234// #define SIOCGHIWAT  _IOR('s',  1, unsigned long)        /* get high watermark */
235// #define SIOCSLOWAT  _IOW('s',  2, unsigned long)        /* set low watermark */
236// #define SIOCGLOWAT  _IOR('s',  3, unsigned long)        /* get low watermark */
237// #define SIOCATMARK  _IOR('s',  7, unsigned long)        /* at oob mark? */
238// #endif
239//
240// /* Socket flags: */
241// #ifndef O_NONBLOCK
242// #define O_NONBLOCK    04000U
243// #endif
244// #ifndef O_NDELAY
245// #define O_NDELAY      O_NONBLOCK /* same as O_NONBLOCK, for compatibility */
246// #endif
247//
248// #ifndef SHUT_RD
249//   #define SHUT_RD   0
250//   #define SHUT_WR   1
251//   #define SHUT_RDWR 2
252// #endif
253//
254// /* FD_SET used for lwip_select */
255// #ifndef FD_SET
256// #undef  FD_SETSIZE
257//     /* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
258// #define FD_SETSIZE    MEMP_NUM_NETCONN
259// #define FD_SET(n, p)  ((p)->fd_bits[(n)/8] |=  (1 << ((n) & 7)))
260// #define FD_CLR(n, p)  ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7)))
261// #define FD_ISSET(n,p) ((p)->fd_bits[(n)/8] &   (1 << ((n) & 7)))
262// #define FD_ZERO(p)    memset((void*)(p),0,sizeof(*(p)))
263//
264//     typedef struct fd_set {
265//         unsigned char fd_bits[(FD_SETSIZE + 7) / 8];
266//     } fd_set;
267//
268// #endif                          /* FD_SET */
269
270/** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided
271 * by your system, set this to 0 and include <sys/time.h> in cc.h */
272#ifndef LWIP_TIMEVAL_PRIVATE
273#define LWIP_TIMEVAL_PRIVATE 0
274#endif
275
276#if LWIP_TIMEVAL_PRIVATE
277    struct timeval {
278        long tv_sec;            /* seconds */
279        long tv_usec;           /* and microseconds */
280    };
281#endif                          /* LWIP_TIMEVAL_PRIVATE */
282
283struct msghdr;
284
285    void lwip_socket_init(void);
286
287    int lwip_accept(int s, struct sockaddr *addr, socklen_t * addrlen);
288    int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
289    int lwip_shutdown(int s, int how);
290    int lwip_getpeername(int s, struct sockaddr *name, socklen_t * namelen);
291    int lwip_getsockname(int s, struct sockaddr *name, socklen_t * namelen);
292    int lwip_getsockopt(int s, int level, int optname, void *optval,
293                        socklen_t * optlen);
294    int lwip_setsockopt(int s, int level, int optname, const void *optval,
295                        socklen_t optlen);
296    int lwip_close(int s);
297    int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
298    int lwip_listen(int s, int backlog);
299    int lwip_recv(int s, void *mem, size_t len, int flags);
300    int lwip_read(int s, void *mem, size_t len);
301    int lwip_recvfrom(int s, void *mem, size_t len, int flags,
302                      struct sockaddr *from, socklen_t * fromlen);
303    int lwip_send(int s, const void *dataptr, size_t size, int flags);
304    int lwip_sendto(int s, const void *dataptr, size_t size, int flags,
305                    const struct sockaddr *to, socklen_t tolen);
306    int lwip_sendmsg(int s, const struct msghdr *msg, int flags);
307    int lwip_socket(int domain, int type, int protocol);
308    int lwip_write(int s, const void *dataptr, size_t size);
309    int lwip_select(int maxfdp1, fd_set * readset, fd_set * writeset,
310                    fd_set * exceptset, struct timeval *timeout);
311    int lwip_ioctl(int s, long cmd, void *argp);
312    int lwip_fcntl(int s, int cmd, int val);
313
314#if LWIP_COMPAT_SOCKETS
315#define accept(a,b,c)         lwip_accept(a,b,c)
316#define bind(a,b,c)           lwip_bind(a,b,c)
317#define shutdown(a,b)         lwip_shutdown(a,b)
318#define closesocket(s)        lwip_close(s)
319#define connect(a,b,c)        lwip_connect(a,b,c)
320#define getsockname(a,b,c)    lwip_getsockname(a,b,c)
321#define getpeername(a,b,c)    lwip_getpeername(a,b,c)
322#define setsockopt(a,b,c,d,e) lwip_setsockopt(a,b,c,d,e)
323#define getsockopt(a,b,c,d,e) lwip_getsockopt(a,b,c,d,e)
324#define listen(a,b)           lwip_listen(a,b)
325#define recv(a,b,c,d)         lwip_recv(a,b,c,d)
326#define recvfrom(a,b,c,d,e,f) lwip_recvfrom(a,b,c,d,e,f)
327#define send(a,b,c,d)         lwip_send(a,b,c,d)
328#define sendto(a,b,c,d,e,f)   lwip_sendto(a,b,c,d,e,f)
329#define socket(a,b,c)         lwip_socket(a,b,c)
330#define select(a,b,c,d,e)     lwip_select(a,b,c,d,e)
331#define ioctlsocket(a,b,c)    lwip_ioctl(a,b,c)
332
333#if LWIP_POSIX_SOCKETS_IO_NAMES
334#define read(a,b,c)           lwip_read(a,b,c)
335#define write(a,b,c)          lwip_write(a,b,c)
336#define close(s)              lwip_close(s)
337#endif                          /* LWIP_POSIX_SOCKETS_IO_NAMES */
338
339#endif                          /* LWIP_COMPAT_SOCKETS */
340
341// #ifdef __cplusplus
342// }
343// #endif
344#endif                          /* LWIP_SOCKET */
345#endif                          /* __LWIP_SOCKETS_H__ */
346