PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

lib/liblwip/include/lwip/sockets.h

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