socket.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef _SYS_SOCKET_H_
  2. /*
  3. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. * -
  34. * Portions Copyright (c) 1983, 1993 by
  35. * The Regents of the University of California. All rights reserved.
  36. *
  37. * Redistribution and use in source and binary forms, with or without
  38. * modification, are permitted provided that the following conditions
  39. * are met:
  40. * 1. Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * 2. Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in the
  44. * documentation and/or other materials provided with the distribution.
  45. * 3. Neither the name of the University nor the names of its contributors
  46. * may be used to endorse or promote products derived from this software
  47. * without specific prior written permission.
  48. *
  49. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  50. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  53. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. * SUCH DAMAGE.
  60. */
  61. /*
  62. * $Log$
  63. * Revision 1.12 2009/02/22 12:37:26 olereinhardt
  64. * Added NutUdpError and NutUdpSetSocketError to set and retrieve socket
  65. * errors. As udp sockets aren't connection oriented those errors will be
  66. * anounced asynchronously on the next NutUdpSend or NutUdpReceive
  67. *
  68. * Include "include/errno.h" instead of "include/net/errno.h"
  69. *
  70. * Revision 1.11 2008/08/20 06:56:59 haraldkipp
  71. * Implemented IP demultiplexer.
  72. *
  73. * Revision 1.10 2008/08/11 07:00:27 haraldkipp
  74. * BSD types replaced by stdint types (feature request #1282721).
  75. *
  76. * Revision 1.9 2008/04/18 13:32:00 haraldkipp
  77. * Changed size parameter from u_short to int, which is easier to handle
  78. * for 32-bit targets. You need to recompile your ARM code. No impact on
  79. * AVR expected
  80. * I changed u_int to int at some places to avoid some warnings during
  81. * compilation of Nut/Net.
  82. * libs.
  83. *
  84. * Revision 1.8 2005/07/26 15:49:59 haraldkipp
  85. * Cygwin support added.
  86. *
  87. * Revision 1.7 2005/04/08 10:01:21 freckle
  88. * removed #defines from unix emulation as provided by system headers
  89. *
  90. * Revision 1.6 2005/04/04 19:33:54 freckle
  91. * added creation of include/netdb_orig.h, include/sys/socket_orig.h and
  92. * include/netinet/in_orig.h to allow unix emulation to use tcp/ip sockets
  93. *
  94. * Revision 1.5 2004/07/30 19:54:46 drsung
  95. * Some code of TCP stack redesigned. Round trip time calculation is now
  96. * supported. Fixed several bugs in TCP state machine. Now TCP connections
  97. * should be more reliable under heavy traffic or poor physical connections.
  98. *
  99. * Revision 1.4 2004/03/16 16:48:44 haraldkipp
  100. * Added Jan Dubiec's H8/300 port.
  101. *
  102. * Revision 1.3 2004/02/02 19:03:09 drsung
  103. * Some more ICMP support added.
  104. *
  105. * Revision 1.2 2003/11/24 21:00:21 drsung
  106. * Packet queue added for UDP sockets.
  107. *
  108. * Revision 1.1.1.1 2003/05/09 14:41:21 haraldkipp
  109. * Initial using 3.2.1
  110. *
  111. * Revision 1.7 2003/02/04 18:00:53 harald
  112. * Version 3 released
  113. *
  114. * Revision 1.6 2002/09/15 16:46:57 harald
  115. * Error prototype added
  116. *
  117. * Revision 1.5 2002/06/26 17:29:29 harald
  118. * First pre-release with 2.4 stack
  119. *
  120. */
  121. #include <cfg/udp.h>
  122. #if defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__)
  123. /* use native version on unix emulation */
  124. #include <sys/socket_orig.h>
  125. #else /* embedded systems */
  126. #include <compiler.h>
  127. /*!
  128. * \addtogroup xgSocket
  129. */
  130. /*
  131. * Types
  132. */
  133. #define SOCK_STREAM 1 /*!< \brief Stream socket */
  134. #define SOCK_DGRAM 2 /*!< \brief Datagram socket */
  135. #define SOCK_RAW 3 /*!< \brief Raw-protocol interface */
  136. /*
  137. * Option flags per-socket.
  138. */
  139. #define SO_DEBUG 0x0001 /*!< \brief Turn on debugging info recording */
  140. #define SO_ACCEPTCONN 0x0002 /*!< \brief Socket has had listen() */
  141. #define SO_REUSEADDR 0x0004 /*!< \brief Allow local address reuse */
  142. #define SO_KEEPALIVE 0x0008 /*!< \brief Keep connections alive */
  143. #define SO_DONTROUTE 0x0010 /*!< \brief Just use interface addresses */
  144. #define SO_BROADCAST 0x0020 /*!< \brief Permit sending of broadcast msgs */
  145. #define SO_USELOOPBACK 0x0040 /*!< \brief Bypass hardware when possible */
  146. #define SO_LINGER 0x0080 /*!< \brief Linger on close if data present */
  147. #define SO_OOBINLINE 0x0100 /*!< \brief Leave received OOB data in line */
  148. #define SO_REUSEPORT 0x0200 /*!< \brief Allow local address & port reuse */
  149. /*
  150. * Additional options, not kept in so_options.
  151. */
  152. #define SO_SNDBUF 0x1001 /*!< \brief Send buffer size */
  153. #define SO_RCVBUF 0x1002 /*!< \brief Receive buffer size */
  154. #define SO_SNDLOWAT 0x1003 /*!< \brief Send low-water mark */
  155. #define SO_RCVLOWAT 0x1004 /*!< \brief Receive low-water mark */
  156. #define SO_SNDTIMEO 0x1005 /*!< \brief Send timeout */
  157. #define SO_RCVTIMEO 0x1006 /*!< \brief Receive timeout */
  158. #define SO_ERROR 0x1007 /*!< \brief Get error status and clear */
  159. #define SO_TYPE 0x1008 /*!< \brief Get socket type */
  160. /*
  161. * Address families.
  162. */
  163. #define AF_INET 2 /*!< \brief internetwork: UDP, TCP, etc. */
  164. #endif /* unix / embedded */
  165. /* assure _SYS_SOCKET_H_ is set */
  166. #undef _SYS_SOCKET_H_
  167. #define _SYS_SOCKET_H_
  168. #include <cfg/crt.h>
  169. #include <sys/sock_var.h>
  170. /*!
  171. * \file sys/socket.h
  172. * \brief UDP and TCP socket interface definitions.
  173. */
  174. extern TCPSOCKET *NutTcpCreateSocket(void);
  175. extern int NutTcpSetSockOpt(TCPSOCKET *sock, int optname, const void *optval, int optlen);
  176. extern int NutTcpGetSockOpt(TCPSOCKET *sock, int optname, void *optval, int optlen);
  177. extern int NutTcpConnect(TCPSOCKET *sock, uint32_t addr, uint16_t port);
  178. extern int NutTcpAccept(TCPSOCKET *sock, uint16_t port);
  179. extern int NutTcpInput(NUTDEVICE * dev, NETBUF *nb);
  180. extern int NutTcpSend(TCPSOCKET *sock, const void *data, int len);
  181. #ifdef __HARVARD_ARCH__
  182. extern int NutTcpSend_P(TCPSOCKET *sock, PGM_P data, int len);
  183. #endif
  184. extern int NutTcpCloseSocket(TCPSOCKET *sock);
  185. extern void NutTcpDestroySocket(TCPSOCKET *sock);
  186. extern int NutTcpReceive(TCPSOCKET *sock, void *data, int size);
  187. extern TCPSOCKET *NutTcpFindSocket(uint16_t lport, uint16_t rport, uint32_t raddr);
  188. extern int NutTcpError(TCPSOCKET *sock);
  189. extern int NutTcpAbortSocket(TCPSOCKET *sock, uint16_t last_error);
  190. extern void NutTcpDiscardBuffers(TCPSOCKET * sock);
  191. extern int NutTcpDeviceRead(TCPSOCKET *sock, void *buffer, int size);
  192. extern int NutTcpDeviceWrite(TCPSOCKET *sock, const void *buffer, int size);
  193. #ifdef __HARVARD_ARCH__
  194. extern int NutTcpDeviceWrite_P(TCPSOCKET *sock, PGM_P buffer, int size);
  195. #endif
  196. extern int NutTcpDeviceIOCtl(TCPSOCKET *sock, int cmd, void *param);
  197. #ifndef CRT_DISABLE_SELECT_POLL
  198. extern int NutTcpDeviceSelect (TCPSOCKET * sock, int flags, HANDLE *wq, select_cmd_t cmd);
  199. #else
  200. #define NutTcpDeviceSelect NULL
  201. #endif
  202. extern int NutTcpDeviceClose(TCPSOCKET *sock);
  203. extern UDPSOCKET *NutUdpCreateSocket(uint16_t port);
  204. extern int NutUdpSendTo(UDPSOCKET *sock, uint32_t addr, uint16_t port, void *data, int len);
  205. extern int NutUdpReceiveFrom(UDPSOCKET *sock, uint32_t *addr, uint16_t *port, void *data, int size, uint32_t timeout);
  206. extern int NutUdpDestroySocket(UDPSOCKET *sock);
  207. extern UDPSOCKET *NutUdpFindSocket(uint16_t port);
  208. extern int NutUdpSetSockOpt(UDPSOCKET *sock, int optname, const void *optval, int optlen);
  209. extern int NutUdpGetSockOpt(UDPSOCKET *sock, int optname, void *optval, int optlen);
  210. #ifndef NUT_UDP_ICMP_EXCLUDE
  211. extern int NutUdpSetSocketError(UDPSOCKET * sock, uint32_t remote_addr, uint16_t remote_port, uint16_t error);
  212. extern int NutUdpError(UDPSOCKET * sock, uint32_t * addr, uint16_t * port);
  213. #endif
  214. #endif