sock_var.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #ifndef _SYS_SOCK_VAR_H_
  2. #define _SYS_SOCK_VAR_H_
  3. /*
  4. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. * -
  35. * Portions Copyright (c) 1983, 1993 by
  36. * The Regents of the University of California. All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. * 1. Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * 2. Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in the
  45. * documentation and/or other materials provided with the distribution.
  46. * 3. Neither the name of the University nor the names of its contributors
  47. * may be used to endorse or promote products derived from this software
  48. * without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  51. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  54. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  56. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  57. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  60. * SUCH DAMAGE.
  61. */
  62. /*
  63. * $Log$
  64. * Revision 1.14 2009/02/22 12:37:27 olereinhardt
  65. * Added NutUdpError and NutUdpSetSocketError to set and retrieve socket
  66. * errors. As udp sockets aren't connection oriented those errors will be
  67. * anounced asynchronously on the next NutUdpSend or NutUdpReceive
  68. *
  69. * Include "include/errno.h" instead of "include/net/errno.h"
  70. *
  71. * Revision 1.13 2008/08/20 06:56:59 haraldkipp
  72. * Implemented IP demultiplexer.
  73. *
  74. * Revision 1.12 2008/08/11 07:00:27 haraldkipp
  75. * BSD types replaced by stdint types (feature request #1282721).
  76. *
  77. * Revision 1.11 2008/04/18 13:32:00 haraldkipp
  78. * Changed size parameter from u_short to int, which is easier to handle
  79. * for 32-bit targets. You need to recompile your ARM code. No impact on
  80. * AVR expected
  81. * I changed u_int to int at some places to avoid some warnings during
  82. * compilation of Nut/Net.
  83. * libs.
  84. *
  85. * Revision 1.10 2007/08/29 07:43:54 haraldkipp
  86. * Documentation updated and corrected.
  87. *
  88. * Revision 1.9 2006/03/21 21:22:19 drsung
  89. * Enhancement made to TCP state machine. Now TCP options
  90. * are read from peer and at least the maximum segment size is stored.
  91. *
  92. * Revision 1.8 2005/08/02 17:46:49 haraldkipp
  93. * Major API documentation update.
  94. *
  95. * Revision 1.7 2005/06/05 16:48:25 haraldkipp
  96. * Additional parameter enables NutUdpInput() to avoid responding to UDP
  97. * broadcasts with ICMP unreachable messages. Fixes bug #1215192.
  98. *
  99. * Revision 1.6 2004/07/30 19:54:46 drsung
  100. * Some code of TCP stack redesigned. Round trip time calculation is now
  101. * supported. Fixed several bugs in TCP state machine. Now TCP connections
  102. * should be more reliable under heavy traffic or poor physical connections.
  103. *
  104. * Revision 1.5 2004/03/16 16:48:44 haraldkipp
  105. * Added Jan Dubiec's H8/300 port.
  106. *
  107. * Revision 1.4 2004/01/14 19:33:13 drsung
  108. * New TCP output buffer handling
  109. *
  110. * Revision 1.3 2003/11/24 21:00:21 drsung
  111. * Packet queue added for UDP sockets.
  112. *
  113. * Revision 1.2 2003/07/13 19:32:12 haraldkipp
  114. * Faster TCP transfers by changing receive buffer
  115. *
  116. * Revision 1.1.1.1 2003/05/09 14:41:22 haraldkipp
  117. * Initial using 3.2.1
  118. *
  119. * Revision 1.10 2003/02/04 18:00:53 harald
  120. * Version 3 released
  121. *
  122. * Revision 1.9 2002/09/03 17:50:18 harald
  123. * Configurable receive buffer size
  124. *
  125. * Revision 1.8 2002/08/16 17:54:19 harald
  126. * Count out of sequence drops
  127. *
  128. * Revision 1.7 2002/06/26 17:29:29 harald
  129. * First pre-release with 2.4 stack
  130. *
  131. */
  132. #include <compiler.h>
  133. #include <stdint.h>
  134. #include <sys/types.h>
  135. #include <sys/device.h>
  136. #include <dev/netbuf.h>
  137. /*!
  138. * \file sys/sock_var.h
  139. * \brief UDP and TCP socket interface definitions.
  140. */
  141. /*********************************************************************\
  142. * UDP
  143. \*********************************************************************/
  144. /*!
  145. * \addtogroup xgUdpSocket
  146. */
  147. /*@{*/
  148. /*!
  149. * \brief UDP socket type.
  150. */
  151. typedef struct udp_socket UDPSOCKET;
  152. /*!
  153. * \brief UDP socket information structure.
  154. *
  155. * Applications should not rely on the content of this structure.
  156. * It may change without notice.
  157. */
  158. struct udp_socket {
  159. UDPSOCKET *so_next; /*!< \brief Link to next tcp socket structure. */
  160. uint16_t so_local_port; /*!< \brief Local port number in net byte order. */
  161. NETBUF *so_rx_nb; /*!< \brief Received, but not read by application. */
  162. HANDLE so_rx_rdy; /*!< \brief Receiver event queue. */
  163. int so_rx_cnt; /*!< \brief Number of data bytes in the receive buffer. */
  164. int so_rx_bsz; /*!< \brief Receive buffer size. */
  165. uint16_t so_last_error; /*!< \brief Last error of socket */
  166. uint32_t so_remote_addr; /*!< \brief Remote IP address in net byte order. Important just in case of an error */
  167. uint16_t so_remote_port; /*!< \brief Remote port number in net byte order. Important just in case of an error */
  168. };
  169. /*@}*/
  170. extern int NutUdpInput(NUTDEVICE * dev, NETBUF *nb);
  171. extern int NutUdpOutput(UDPSOCKET *sock, uint32_t dest, uint16_t port, NETBUF *nb);
  172. /*********************************************************************\
  173. * TCP
  174. \*********************************************************************/
  175. /*!
  176. * \addtogroup xgTcpSocket
  177. */
  178. /*@{*/
  179. /*!
  180. * \brief TCP socket type.
  181. */
  182. typedef struct tcp_socket TCPSOCKET;
  183. /*!
  184. * \brief TCP socket information structure.
  185. *
  186. * Applications should not rely on the content of this structure.
  187. * It may change without notice.
  188. *
  189. * However: This structure is casted to NUTVIRTUALDEVICE by some of the crt
  190. * functions like (fread/frwrite/fdopen/fopen/fclose/ etc.) So it always
  191. * have to be made sure that so_device is the first entry, and so_next the
  192. * second. These functions will check so_device and handle the struct as
  193. * "virtual device" if so_device == NULL.
  194. */
  195. struct tcp_socket {
  196. void *so_device; /*!< \brief Always zero. */
  197. TCPSOCKET *so_next; /*!< \brief Link to next tcp socket structure. */
  198. uint8_t so_devtype; /*!< \brief Device type, always IFTYP_TCPSOCK. */
  199. int (*so_devread) (TCPSOCKET *, void *, int); /*!< \brief Read from device. */
  200. int (*so_devwrite) (TCPSOCKET *, const void *, int); /*!< \brief Write to device. */
  201. #ifdef __HARVARD_ARCH__
  202. int (*so_devwrite_P) (TCPSOCKET *, PGM_P, int); /*!< \brief Write to device. */
  203. #endif
  204. int (*so_devioctl) (TCPSOCKET *, int, void *); /*!< \brief Driver control function. */
  205. int (*so_devselect) (TCPSOCKET *, int, HANDLE *, select_cmd_t cmd); /*!< \brief Select function. */
  206. uint16_t so_devocnt; /*!< \brief Number of data bytes in output buffer. */
  207. uint8_t *so_devobuf; /*!< \brief Pointer to output buffer. */
  208. uint16_t so_devobsz; /*!< \brief Output buffer size. */
  209. volatile uint8_t so_state; /*!< \brief Connection state, see tcp_fsm.h */
  210. uint32_t so_local_addr; /*!< \brief Local IP address in net byte order. */
  211. uint16_t so_local_port; /*!< \brief Local port number in net byte order. */
  212. uint32_t so_remote_addr; /*!< \brief Remote IP address in net byte order. */
  213. uint16_t so_remote_port; /*!< \brief Remote port number in net byte order. */
  214. uint8_t so_tx_flags; /*!< \brief Flags used during transmissions - see below */
  215. uint32_t so_tx_isn; /*!< \brief Initial sequence number. */
  216. uint32_t so_tx_una; /*!< \brief Unacknowledged sequence number. */
  217. uint32_t so_tx_nxt; /*!< \brief Next sequence number to send. */
  218. uint32_t so_tx_wl1; /*!< \brief Sequence number of last window update. */
  219. uint32_t so_tx_wl2; /*!< \brief Acknowledged sequence of last window update. */
  220. uint16_t so_tx_win; /*!< \brief Peer's receive window. */
  221. uint8_t so_tx_dup; /*!< \brief Duplicate ACK counter. */
  222. NETBUF *so_tx_nbq; /*!< \brief Network buffers waiting to be acknowledged. */
  223. HANDLE so_tx_tq; /*!< \brief Threads waiting for transmit buffer space. */
  224. WQLIST *so_tx_wq_list; /*!< \brief TX buffer wait queue list. Needed for select */
  225. uint32_t so_rx_isn; /*!< \brief Initial sequence number of remote. */
  226. uint32_t so_rx_nxt; /*!< \brief Next sequence number to receive. */
  227. uint16_t so_rx_win; /*!< \brief Local receive window. */
  228. int so_rx_cnt; /*!< \brief Number of data bytes in the receive buffer. */
  229. int so_rx_bsz; /*!< \brief Receive buffer size. */
  230. int so_rd_cnt; /*!< \brief Number of bytes read from buffer top. */
  231. int_fast8_t so_rx_apc; /*!< \brief Number of packets received in advance. */
  232. NETBUF *so_rx_buf; /*!< \brief Data waiting to be read by application. */
  233. HANDLE so_rx_tq; /*!< \brief Threads waiting for received data. */
  234. WQLIST *so_rx_wq_list; /*!< \brief RX buffer wait queue list. Needed for select */
  235. NETBUF *so_rx_nbq; /*!< \brief Network buffers received in advance. */
  236. uint16_t so_mss; /*!< \brief MSS, limited by remote option or MTU. */
  237. uint32_t so_rtt_seq; /*!< \brief Sequence number for RTT calculation. */
  238. uint16_t so_rtto; /*!< \brief Current retransmission timeout. */
  239. uint16_t so_retransmits; /*!< \brief Number of retransmits. */
  240. uint16_t so_time_wait; /*!< \brief Time wait counter. */
  241. uint16_t so_retran_time; /*!< \brief Retransmit time counter. */
  242. uint16_t so_last_error; /*!< \brief Last error code. */
  243. HANDLE so_pc_tq; /*!< \brief Listening thread. */
  244. HANDLE so_ac_tq; /*!< \brief Connecting thread. */
  245. uint32_t so_read_to; /*!< \brief Read timeout. */
  246. uint32_t so_write_to; /*!< \brief Write timeout. */
  247. uint32_t so_oos_drop; /*!< \brief Out of sequence dropped. */
  248. };
  249. /*
  250. * TCP send flags.
  251. */
  252. #define SO_FIN 0x01 /*!< \brief Socket transmit flag. Send FIN after all data has been transmitted. */
  253. #define SO_SYN 0x02 /*!< \brief Socket transmit flag. Send SYN first. */
  254. #define SO_FORCE 0x08 /*!< \brief Socket transmit flag. Force sending ACK. */
  255. #define SO_ACK 0x10 /*!< \brief Socket transmit flag. Send ACK. */
  256. /*@}*/
  257. #include <netinet/tcp_fsm.h>
  258. extern int NutTcpOutput(TCPSOCKET *sock, const uint8_t *data, uint16_t size);
  259. extern int NutTcpReject(NETBUF *nb);
  260. #endif