]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/include/lwip/sockets.h
Added sequential (socket API) function gethostbyname and the struct hostent it uses
[pes-rpp/rpp-lwip.git] / src / include / lwip / sockets.h
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 "lwip/ip_addr.h"
42 #include "lwip/inet.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 /* members are in network byte order */
49 struct sockaddr_in {
50   u8_t sin_len;
51   u8_t sin_family;
52   u16_t sin_port;
53   struct in_addr sin_addr;
54   char sin_zero[8];
55 };
56
57 struct sockaddr {
58   u8_t sa_len;
59   u8_t sa_family;
60   char sa_data[14];
61 };
62
63 #if LWIP_DNS
64 struct hostent {
65     char  *h_name;      /* Official name of the host. */
66     char **h_aliases;   /* A pointer to an array of pointers to alternative host names,
67                            terminated by a null pointer. */
68     int    h_addrtype;  /* Address type. */
69     int    h_length;    /* The length, in bytes, of the address. */
70     char **h_addr_list; /* A pointer to an array of pointers to network addresses (in
71                            network byte order) for the host, terminated by a null pointer. */
72 #define h_addr h_addr_list[0] /* for backward compatibility */
73 };
74 #endif /* LWIP_DNS */
75
76 #ifndef socklen_t
77 #  define socklen_t u32_t
78 #endif
79
80
81 #define SOCK_STREAM     1
82 #define SOCK_DGRAM      2
83 #define SOCK_RAW        3
84
85 /*
86  * Option flags per-socket.
87  */
88 #define  SO_DEBUG       0x0001 /* turn on debugging info recording */
89 #define  SO_ACCEPTCONN  0x0002 /* socket has had listen() */
90 #define  SO_REUSEADDR   0x0004 /* allow local address reuse */
91 #define  SO_KEEPALIVE   0x0008 /* keep connections alive */
92 #define  SO_DONTROUTE   0x0010 /* just use interface addresses */
93 #define  SO_BROADCAST   0x0020 /* permit sending of broadcast msgs */
94 #define  SO_USELOOPBACK 0x0040 /* bypass hardware when possible */
95 #define  SO_LINGER      0x0080 /* linger on close if data present */
96 #define  SO_OOBINLINE   0x0100 /* leave received OOB data in line */
97 #define  SO_REUSEPORT   0x0200 /* allow local address & port reuse */
98
99 #define SO_DONTLINGER   ((int)(~SO_LINGER))
100
101 /*
102  * Additional options, not kept in so_options.
103  */
104 #define SO_SNDBUF    0x1001    /* send buffer size */
105 #define SO_RCVBUF    0x1002    /* receive buffer size */
106 #define SO_SNDLOWAT  0x1003    /* send low-water mark */
107 #define SO_RCVLOWAT  0x1004    /* receive low-water mark */
108 #define SO_SNDTIMEO  0x1005    /* send timeout */
109 #define SO_RCVTIMEO  0x1006    /* receive timeout */
110 #define SO_ERROR     0x1007    /* get error status and clear */
111 #define SO_TYPE      0x1008    /* get socket type */
112 #define SO_CONTIMEO  0x1009    /* connect timeout */
113 #define SO_NO_CHECK  0x100a    /* don't create UDP checksum */
114
115
116 /*
117  * Structure used for manipulating linger option.
118  */
119 struct linger {
120        int l_onoff;                /* option on/off */
121        int l_linger;               /* linger time */
122 };
123
124 /*
125  * Level number for (get/set)sockopt() to apply to socket itself.
126  */
127 #define  SOL_SOCKET  0xfff    /* options for socket level */
128
129
130 #define AF_UNSPEC       0
131 #define AF_INET         2
132 #define PF_INET         AF_INET
133 #define PF_UNSPEC       AF_UNSPEC
134
135 #define IPPROTO_IP      0
136 #define IPPROTO_TCP     6
137 #define IPPROTO_UDP     17
138 #define IPPROTO_UDPLITE 136
139
140 #define INADDR_ANY       0
141 #define INADDR_BROADCAST 0xffffffff
142
143 /* Flags we can use with send and recv. */
144 #define MSG_PEEK       0x01    /* Peeks at an incoming message */
145 #define MSG_WAITALL    0x02    /* Requests that the function block until the full amount of data requested can be returned */
146 #define MSG_OOB        0x04    /* Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
147 #define MSG_DONTWAIT   0x08    /* Nonblocking i/o for this operation only */
148 #define MSG_MORE       0x10    /* Sender will send more */
149
150
151 /*
152  * Options for level IPPROTO_IP
153  */
154 #define IP_TOS             1
155 #define IP_TTL             2
156
157 #if LWIP_TCP
158 /*
159  * Options for level IPPROTO_TCP
160  */
161 #define TCP_NODELAY    0x01    /* don't delay send to coalesce packets */
162 #define TCP_KEEPALIVE  0x02    /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
163 #define TCP_KEEPIDLE   0x03    /* set pcb->keep_idle  - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
164 #define TCP_KEEPINTVL  0x04    /* set pcb->keep_intvl - Use seconds for get/setsockopt */
165 #define TCP_KEEPCNT    0x05    /* set pcb->keep_cnt   - Use number of probes sent for get/setsockopt */
166 #endif /* LWIP_TCP */
167
168 #if LWIP_UDP && LWIP_UDPLITE
169 /*
170  * Options for level IPPROTO_UDPLITE
171  */
172 #define UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */
173 #define UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */
174 #endif /* LWIP_UDP && LWIP_UDPLITE*/
175
176
177 #if LWIP_IGMP
178 /*
179  * Options and types for UDP multicast traffic handling
180  */
181 #define IP_ADD_MEMBERSHIP  3
182 #define IP_DROP_MEMBERSHIP 4
183 #define IP_MULTICAST_TTL   5
184 #define IP_MULTICAST_IF    6
185 #define IP_MULTICAST_LOOP  7
186
187 typedef struct ip_mreq {
188     struct in_addr imr_multiaddr; /* IP multicast address of group */
189     struct in_addr imr_interface; /* local IP address of interface */
190 } ip_mreq;
191 #endif /* LWIP_IGMP */
192
193 #define IPTOS_TOS_MASK          0x1E
194 #define IPTOS_TOS(tos)          ((tos) & IPTOS_TOS_MASK)
195 #define IPTOS_LOWDELAY          0x10
196 #define IPTOS_THROUGHPUT        0x08
197 #define IPTOS_RELIABILITY       0x04
198 #define IPTOS_LOWCOST           0x02
199 #define IPTOS_MINCOST           IPTOS_LOWCOST
200
201 /*
202  * Definitions for IP precedence (also in ip_tos) (hopefully unused)
203  */
204 #define IPTOS_PREC_MASK                 0xe0
205 #define IPTOS_PREC(tos)                ((tos) & IPTOS_PREC_MASK)
206 #define IPTOS_PREC_NETCONTROL           0xe0
207 #define IPTOS_PREC_INTERNETCONTROL      0xc0
208 #define IPTOS_PREC_CRITIC_ECP           0xa0
209 #define IPTOS_PREC_FLASHOVERRIDE        0x80
210 #define IPTOS_PREC_FLASH                0x60
211 #define IPTOS_PREC_IMMEDIATE            0x40
212 #define IPTOS_PREC_PRIORITY             0x20
213 #define IPTOS_PREC_ROUTINE              0x00
214
215
216 /*
217  * Commands for ioctlsocket(),  taken from the BSD file fcntl.h.
218  *
219  *
220  * Ioctl's have the command encoded in the lower word,
221  * and the size of any in or out parameters in the upper
222  * word.  The high 2 bits of the upper word are used
223  * to encode the in/out status of the parameter; for now
224  * we restrict parameters to at most 128 bytes.
225  */
226 #if !defined(FIONREAD) || !defined(FIONBIO)
227 #define IOCPARM_MASK    0x7fU           /* parameters must be < 128 bytes */
228 #define IOC_VOID        0x20000000UL    /* no parameters */
229 #define IOC_OUT         0x40000000UL    /* copy out parameters */
230 #define IOC_IN          0x80000000UL    /* copy in parameters */
231 #define IOC_INOUT       (IOC_IN|IOC_OUT)
232                                         /* 0x20000000 distinguishes new &
233                                            old ioctl's */
234 #define _IO(x,y)        (IOC_VOID|((x)<<8)|(y))
235
236 #define _IOR(x,y,t)     (IOC_OUT|(((long)sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y))
237
238 #define _IOW(x,y,t)     (IOC_IN|(((long)sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y))
239 #endif
240
241 #ifndef FIONREAD
242 #define FIONREAD    _IOR('f', 127, unsigned long) /* get # bytes to read */
243 #endif
244 #ifndef FIONBIO
245 #define FIONBIO     _IOW('f', 126, unsigned long) /* set/clear non-blocking i/o */
246 #endif
247
248 /* Socket I/O Controls */
249 #ifndef SIOCSHIWAT
250 #define SIOCSHIWAT  _IOW('s',  0, unsigned long)  /* set high watermark */
251 #define SIOCGHIWAT  _IOR('s',  1, unsigned long)  /* get high watermark */
252 #define SIOCSLOWAT  _IOW('s',  2, unsigned long)  /* set low watermark */
253 #define SIOCGLOWAT  _IOR('s',  3, unsigned long)  /* get low watermark */
254 #define SIOCATMARK  _IOR('s',  7, unsigned long)  /* at oob mark? */
255 #endif
256
257 #ifndef O_NONBLOCK
258 #define O_NONBLOCK    04000U
259 #endif
260
261 #ifndef FD_SET
262   #undef  FD_SETSIZE
263   /* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
264   #define FD_SETSIZE    MEMP_NUM_NETCONN
265   #define FD_SET(n, p)  ((p)->fd_bits[(n)/8] |=  (1 << ((n) & 7)))
266   #define FD_CLR(n, p)  ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7)))
267   #define FD_ISSET(n,p) ((p)->fd_bits[(n)/8] &   (1 << ((n) & 7)))
268   #define FD_ZERO(p)    memset((void*)(p),0,sizeof(*(p)))
269
270   typedef struct fd_set {
271           unsigned char fd_bits [(FD_SETSIZE+7)/8];
272         } fd_set;
273
274 /* 
275  * only define this in sockets.c so it does not interfere
276  * with other projects namespaces where timeval is present
277  */ 
278 #ifndef LWIP_TIMEVAL_PRIVATE
279 #define LWIP_TIMEVAL_PRIVATE 1
280 #endif
281
282 #if LWIP_TIMEVAL_PRIVATE
283   struct timeval {
284     long    tv_sec;         /* seconds */
285     long    tv_usec;        /* and microseconds */
286   };
287 #endif
288
289 #endif
290
291 void lwip_socket_init(void);
292
293 int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
294 int lwip_bind(int s, struct sockaddr *name, socklen_t namelen);
295 int lwip_shutdown(int s, int how);
296 int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen);
297 int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen);
298 int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
299 int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
300 int lwip_close(int s);
301 int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
302 int lwip_listen(int s, int backlog);
303 int lwip_recv(int s, void *mem, int len, unsigned int flags);
304 int lwip_read(int s, void *mem, int len);
305 int lwip_recvfrom(int s, void *mem, int len, unsigned int flags,
306       struct sockaddr *from, socklen_t *fromlen);
307 int lwip_send(int s, const void *dataptr, int size, unsigned int flags);
308 int lwip_sendto(int s, const void *dataptr, int size, unsigned int flags,
309     struct sockaddr *to, socklen_t tolen);
310 int lwip_socket(int domain, int type, int protocol);
311 int lwip_write(int s, const void *dataptr, int size);
312 int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
313                 struct timeval *timeout);
314 int lwip_ioctl(int s, long cmd, void *argp);
315
316 #if LWIP_DNS
317 struct hostent *gethostbyname(const char *name);
318 #endif /* LWIP_DNS */
319
320 #if LWIP_COMPAT_SOCKETS
321 #define accept(a,b,c)         lwip_accept(a,b,c)
322 #define bind(a,b,c)           lwip_bind(a,b,c)
323 #define shutdown(a,b)         lwip_shutdown(a,b)
324 #define closesocket(s)        lwip_close(s)
325 #define connect(a,b,c)        lwip_connect(a,b,c)
326 #define getsockname(a,b,c)    lwip_getsockname(a,b,c)
327 #define getpeername(a,b,c)    lwip_getpeername(a,b,c)
328 #define setsockopt(a,b,c,d,e) lwip_setsockopt(a,b,c,d,e)
329 #define getsockopt(a,b,c,d,e) lwip_getsockopt(a,b,c,d,e)
330 #define listen(a,b)           lwip_listen(a,b)
331 #define recv(a,b,c,d)         lwip_recv(a,b,c,d)
332 #define recvfrom(a,b,c,d,e,f) lwip_recvfrom(a,b,c,d,e,f)
333 #define send(a,b,c,d)         lwip_send(a,b,c,d)
334 #define sendto(a,b,c,d,e,f)   lwip_sendto(a,b,c,d,e,f)
335 #define socket(a,b,c)         lwip_socket(a,b,c)
336 #define select(a,b,c,d,e)     lwip_select(a,b,c,d,e)
337 #define ioctlsocket(a,b,c)    lwip_ioctl(a,b,c)
338
339 #if LWIP_POSIX_SOCKETS_IO_NAMES
340 #define read(a,b,c)           lwip_read(a,b,c)
341 #define write(a,b,c)          lwip_write(a,b,c)
342 #define close(s)              lwip_close(s)
343 #endif /* LWIP_POSIX_SOCKETS_IO_NAMES */
344
345 #endif /* LWIP_COMPAT_SOCKETS */
346
347 #ifdef __cplusplus
348 }
349 #endif
350
351 #endif /* LWIP_SOCKET */
352
353 #endif /* __LWIP_SOCKETS_H__ */