PageRenderTime 71ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/a00423_source.html

https://github.com/krackeroda/contiki-doxygen
HTML | 2007 lines | 2006 code | 0 blank | 1 comment | 0 complexity | 44e6f90b79e9e03f4ee7920aec28d317 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <title>Contiki 2.5: core/net/uip.c Source File</title>
  6. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  7. <link href="doxygen.css" rel="stylesheet" type="text/css"/>
  8. </head>
  9. <body>
  10. <!-- Generated by Doxygen 1.6.1 -->
  11. <div class="navigation" id="top">
  12. <div class="tabs">
  13. <ul>
  14. <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
  15. <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
  16. <li><a href="modules.html"><span>Modules</span></a></li>
  17. <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
  18. <li class="current"><a href="files.html"><span>Files</span></a></li>
  19. <li><a href="dirs.html"><span>Directories</span></a></li>
  20. <li><a href="examples.html"><span>Examples</span></a></li>
  21. </ul>
  22. </div>
  23. <div class="tabs">
  24. <ul>
  25. <li><a href="files.html"><span>File&nbsp;List</span></a></li>
  26. <li><a href="globals.html"><span>Globals</span></a></li>
  27. </ul>
  28. </div>
  29. <div class="navpath"><a class="el" href="dir_783dcb2b4bcfa12326370fa26c216025.html">core</a>&nbsp;&raquo&nbsp;<a class="el" href="dir_0966c75b31335e5cb5b37dbb7a80da85.html">net</a>
  30. </div>
  31. </div>
  32. <div class="contents">
  33. <h1>uip.c</h1><a href="a00423.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#define DEBUG_PRINTF(...) </span><span class="comment">/*printf(__VA_ARGS__)*/</span>
  34. <a name="l00002"></a>00002 <span class="comment"></span>
  35. <a name="l00003"></a>00003 <span class="comment">/**</span>
  36. <a name="l00004"></a>00004 <span class="comment"> * \addtogroup uip</span>
  37. <a name="l00005"></a>00005 <span class="comment"> * @{</span>
  38. <a name="l00006"></a>00006 <span class="comment"> */</span>
  39. <a name="l00007"></a>00007 <span class="comment"></span>
  40. <a name="l00008"></a>00008 <span class="comment">/**</span>
  41. <a name="l00009"></a>00009 <span class="comment"> * \file</span>
  42. <a name="l00010"></a>00010 <span class="comment"> * The uIP TCP/IP stack code.</span>
  43. <a name="l00011"></a>00011 <span class="comment"> * \author Adam Dunkels &lt;adam@dunkels.com&gt;</span>
  44. <a name="l00012"></a>00012 <span class="comment"> */</span>
  45. <a name="l00013"></a>00013
  46. <a name="l00014"></a>00014 <span class="comment">/*</span>
  47. <a name="l00015"></a>00015 <span class="comment"> * Copyright (c) 2001-2003, Adam Dunkels.</span>
  48. <a name="l00016"></a>00016 <span class="comment"> * All rights reserved.</span>
  49. <a name="l00017"></a>00017 <span class="comment"> *</span>
  50. <a name="l00018"></a>00018 <span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
  51. <a name="l00019"></a>00019 <span class="comment"> * modification, are permitted provided that the following conditions</span>
  52. <a name="l00020"></a>00020 <span class="comment"> * are met:</span>
  53. <a name="l00021"></a>00021 <span class="comment"> * 1. Redistributions of source code must retain the above copyright</span>
  54. <a name="l00022"></a>00022 <span class="comment"> * notice, this list of conditions and the following disclaimer.</span>
  55. <a name="l00023"></a>00023 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
  56. <a name="l00024"></a>00024 <span class="comment"> * notice, this list of conditions and the following disclaimer in the</span>
  57. <a name="l00025"></a>00025 <span class="comment"> * documentation and/or other materials provided with the distribution.</span>
  58. <a name="l00026"></a>00026 <span class="comment"> * 3. The name of the author may not be used to endorse or promote</span>
  59. <a name="l00027"></a>00027 <span class="comment"> * products derived from this software without specific prior</span>
  60. <a name="l00028"></a>00028 <span class="comment"> * written permission.</span>
  61. <a name="l00029"></a>00029 <span class="comment"> *</span>
  62. <a name="l00030"></a>00030 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS&apos;&apos; AND ANY EXPRESS</span>
  63. <a name="l00031"></a>00031 <span class="comment"> * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED</span>
  64. <a name="l00032"></a>00032 <span class="comment"> * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
  65. <a name="l00033"></a>00033 <span class="comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY</span>
  66. <a name="l00034"></a>00034 <span class="comment"> * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span>
  67. <a name="l00035"></a>00035 <span class="comment"> * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE</span>
  68. <a name="l00036"></a>00036 <span class="comment"> * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS</span>
  69. <a name="l00037"></a>00037 <span class="comment"> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,</span>
  70. <a name="l00038"></a>00038 <span class="comment"> * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING</span>
  71. <a name="l00039"></a>00039 <span class="comment"> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS</span>
  72. <a name="l00040"></a>00040 <span class="comment"> * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
  73. <a name="l00041"></a>00041 <span class="comment"> *</span>
  74. <a name="l00042"></a>00042 <span class="comment"> * This file is part of the uIP TCP/IP stack.</span>
  75. <a name="l00043"></a>00043 <span class="comment"> *</span>
  76. <a name="l00044"></a>00044 <span class="comment"> * $Id: uip.c,v 1.30 2010/10/19 18:29:04 adamdunkels Exp $</span>
  77. <a name="l00045"></a>00045 <span class="comment"> *</span>
  78. <a name="l00046"></a>00046 <span class="comment"> */</span>
  79. <a name="l00047"></a>00047
  80. <a name="l00048"></a>00048 <span class="comment">/*</span>
  81. <a name="l00049"></a>00049 <span class="comment"> * uIP is a small implementation of the IP, UDP and TCP protocols (as</span>
  82. <a name="l00050"></a>00050 <span class="comment"> * well as some basic ICMP stuff). The implementation couples the IP,</span>
  83. <a name="l00051"></a>00051 <span class="comment"> * UDP, TCP and the application layers very tightly. To keep the size</span>
  84. <a name="l00052"></a>00052 <span class="comment"> * of the compiled code down, this code frequently uses the goto</span>
  85. <a name="l00053"></a>00053 <span class="comment"> * statement. While it would be possible to break the uip_process()</span>
  86. <a name="l00054"></a>00054 <span class="comment"> * function into many smaller functions, this would increase the code</span>
  87. <a name="l00055"></a>00055 <span class="comment"> * size because of the overhead of parameter passing and the fact that</span>
  88. <a name="l00056"></a>00056 <span class="comment"> * the optimier would not be as efficient.</span>
  89. <a name="l00057"></a>00057 <span class="comment"> *</span>
  90. <a name="l00058"></a>00058 <span class="comment"> * The principle is that we have a small buffer, called the uip_buf,</span>
  91. <a name="l00059"></a>00059 <span class="comment"> * in which the device driver puts an incoming packet. The TCP/IP</span>
  92. <a name="l00060"></a>00060 <span class="comment"> * stack parses the headers in the packet, and calls the</span>
  93. <a name="l00061"></a>00061 <span class="comment"> * application. If the remote host has sent data to the application,</span>
  94. <a name="l00062"></a>00062 <span class="comment"> * this data is present in the uip_buf and the application read the</span>
  95. <a name="l00063"></a>00063 <span class="comment"> * data from there. It is up to the application to put this data into</span>
  96. <a name="l00064"></a>00064 <span class="comment"> * a byte stream if needed. The application will not be fed with data</span>
  97. <a name="l00065"></a>00065 <span class="comment"> * that is out of sequence.</span>
  98. <a name="l00066"></a>00066 <span class="comment"> *</span>
  99. <a name="l00067"></a>00067 <span class="comment"> * If the application whishes to send data to the peer, it should put</span>
  100. <a name="l00068"></a>00068 <span class="comment"> * its data into the uip_buf. The uip_appdata pointer points to the</span>
  101. <a name="l00069"></a>00069 <span class="comment"> * first available byte. The TCP/IP stack will calculate the</span>
  102. <a name="l00070"></a>00070 <span class="comment"> * checksums, and fill in the necessary header fields and finally send</span>
  103. <a name="l00071"></a>00071 <span class="comment"> * the packet back to the peer.</span>
  104. <a name="l00072"></a>00072 <span class="comment">*/</span>
  105. <a name="l00073"></a>00073
  106. <a name="l00074"></a>00074 <span class="preprocessor">#include &quot;<a class="code" href="a00424.html" title="Header file for the uIP TCP/IP stack.">net/uip.h</a>&quot;</span>
  107. <a name="l00075"></a>00075 <span class="preprocessor">#include &quot;<a class="code" href="a00431.html" title="Configuration options for uIP.">net/uipopt.h</a>&quot;</span>
  108. <a name="l00076"></a>00076 <span class="preprocessor">#include &quot;<a class="code" href="a00428.html" title="Macros and definitions for the ARP module.">net/uip_arp.h</a>&quot;</span>
  109. <a name="l00077"></a>00077 <span class="preprocessor">#include &quot;<a class="code" href="a00426.html" title="Declarations of architecture specific functions.">net/uip_arch.h</a>&quot;</span>
  110. <a name="l00078"></a>00078
  111. <a name="l00079"></a>00079 <span class="preprocessor">#if !UIP_CONF_IPV6 </span><span class="comment">/* If UIP_CONF_IPV6 is defined, we compile the</span>
  112. <a name="l00080"></a>00080 <span class="comment"> uip6.c file instead of this one. Therefore</span>
  113. <a name="l00081"></a>00081 <span class="comment"> this #ifndef removes the entire compilation</span>
  114. <a name="l00082"></a>00082 <span class="comment"> output of the uip.c file */</span>
  115. <a name="l00083"></a>00083
  116. <a name="l00084"></a>00084
  117. <a name="l00085"></a>00085 <span class="preprocessor">#if UIP_CONF_IPV6</span>
  118. <a name="l00086"></a>00086 <span class="preprocessor"></span><span class="preprocessor">#include &quot;<a class="code" href="a00414.html" title="Header file for database of link-local neighbors, used by IPv6 code and to be used...">net/uip-neighbor.h</a>&quot;</span>
  119. <a name="l00087"></a>00087 <span class="preprocessor">#endif </span><span class="comment">/* UIP_CONF_IPV6 */</span>
  120. <a name="l00088"></a>00088
  121. <a name="l00089"></a>00089 <span class="preprocessor">#include &lt;string.h&gt;</span>
  122. <a name="l00090"></a>00090
  123. <a name="l00091"></a>00091 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  124. <a name="l00092"></a>00092 <span class="comment">/* Variable definitions. */</span>
  125. <a name="l00093"></a>00093
  126. <a name="l00094"></a>00094
  127. <a name="l00095"></a>00095 <span class="comment">/* The IP address of this host. If it is defined to be fixed (by</span>
  128. <a name="l00096"></a>00096 <span class="comment"> setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set</span>
  129. <a name="l00097"></a>00097 <span class="comment"> here. Otherwise, the address */</span>
  130. <a name="l00098"></a>00098 <span class="preprocessor">#if UIP_FIXEDADDR &gt; 0</span>
  131. <a name="l00099"></a>00099 <span class="preprocessor"></span><span class="keyword">const</span> <a class="code" href="a00106.html" title="Representation of an IP address.">uip_ipaddr_t</a> uip_hostaddr =
  132. <a name="l00100"></a>00100 { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 };
  133. <a name="l00101"></a>00101 <span class="keyword">const</span> <a class="code" href="a00106.html" title="Representation of an IP address.">uip_ipaddr_t</a> uip_draddr =
  134. <a name="l00102"></a>00102 { UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3 };
  135. <a name="l00103"></a>00103 <span class="keyword">const</span> <a class="code" href="a00106.html" title="Representation of an IP address.">uip_ipaddr_t</a> uip_netmask =
  136. <a name="l00104"></a>00104 { UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3 };
  137. <a name="l00105"></a>00105 <span class="preprocessor">#else</span>
  138. <a name="l00106"></a>00106 <span class="preprocessor"></span><a class="code" href="a00106.html" title="Representation of an IP address.">uip_ipaddr_t</a> uip_hostaddr, uip_draddr, uip_netmask;
  139. <a name="l00107"></a>00107 <span class="preprocessor">#endif </span><span class="comment">/* UIP_FIXEDADDR */</span>
  140. <a name="l00108"></a>00108
  141. <a name="l00109"></a>00109 <span class="keyword">const</span> <a class="code" href="a00106.html" title="Representation of an IP address.">uip_ipaddr_t</a> uip_broadcast_addr =
  142. <a name="l00110"></a>00110 <span class="preprocessor">#if UIP_CONF_IPV6</span>
  143. <a name="l00111"></a>00111 <span class="preprocessor"></span> { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  144. <a name="l00112"></a>00112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
  145. <a name="l00113"></a>00113 <span class="preprocessor">#else </span><span class="comment">/* UIP_CONF_IPV6 */</span>
  146. <a name="l00114"></a>00114 { { 0xff, 0xff, 0xff, 0xff } };
  147. <a name="l00115"></a>00115 <span class="preprocessor">#endif </span><span class="comment">/* UIP_CONF_IPV6 */</span>
  148. <a name="l00116"></a>00116 <span class="keyword">const</span> <a class="code" href="a00106.html" title="Representation of an IP address.">uip_ipaddr_t</a> uip_all_zeroes_addr = { { 0x0, <span class="comment">/* rest is 0 */</span> } };
  149. <a name="l00117"></a>00117
  150. <a name="l00118"></a>00118 <span class="preprocessor">#if UIP_FIXEDETHADDR</span>
  151. <a name="l00119"></a>00119 <span class="preprocessor"></span><span class="keyword">const</span> <span class="keyword">struct </span><a class="code" href="a00102.html" title="802.3 address">uip_eth_addr</a> uip_ethaddr = {{UIP_ETHADDR0,
  152. <a name="l00120"></a>00120 UIP_ETHADDR1,
  153. <a name="l00121"></a>00121 UIP_ETHADDR2,
  154. <a name="l00122"></a>00122 UIP_ETHADDR3,
  155. <a name="l00123"></a>00123 UIP_ETHADDR4,
  156. <a name="l00124"></a>00124 UIP_ETHADDR5}};
  157. <a name="l00125"></a>00125 <span class="preprocessor">#else</span>
  158. <a name="l00126"></a>00126 <span class="preprocessor"></span><span class="keyword">struct </span><a class="code" href="a00102.html" title="802.3 address">uip_eth_addr</a> uip_ethaddr = {{0,0,0,0,0,0}};
  159. <a name="l00127"></a>00127 <span class="preprocessor">#endif</span>
  160. <a name="l00128"></a>00128 <span class="preprocessor"></span>
  161. <a name="l00129"></a>00129 <span class="comment">/* The packet buffer that contains incoming packets. */</span>
  162. <a name="l00130"></a><a class="code" href="a01574.html#ga1ff9096f765144168470cfe7ebf75fec">00130</a> <a class="code" href="a00091.html" title="The uIP packet buffer.">uip_buf_t</a> uip_aligned_buf;
  163. <a name="l00131"></a>00131
  164. <a name="l00132"></a><a class="code" href="a01684.html#gad5848369ab5b02f2fb02d8390ccbaf79">00132</a> <span class="keywordtype">void</span> *<a class="code" href="a01684.html#ga561b8eda32e059d4e7397f776268cc63" title="Pointer to the application data in the packet buffer.">uip_appdata</a>; <span class="comment">/* The uip_appdata pointer points to</span>
  165. <a name="l00133"></a>00133 <span class="comment"> application data. */</span>
  166. <a name="l00134"></a>00134 <span class="keywordtype">void</span> *uip_sappdata; <span class="comment">/* The uip_appdata pointer points to</span>
  167. <a name="l00135"></a>00135 <span class="comment"> the application data which is to</span>
  168. <a name="l00136"></a>00136 <span class="comment"> be sent. */</span>
  169. <a name="l00137"></a>00137 <span class="preprocessor">#if UIP_URGDATA &gt; 0</span>
  170. <a name="l00138"></a>00138 <span class="preprocessor"></span><span class="keywordtype">void</span> *uip_urgdata; <span class="comment">/* The uip_urgdata pointer points to</span>
  171. <a name="l00139"></a>00139 <span class="comment"> urgent data (out-of-band data), if</span>
  172. <a name="l00140"></a>00140 <span class="comment"> present. */</span>
  173. <a name="l00141"></a>00141 <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> uip_urglen, uip_surglen;
  174. <a name="l00142"></a>00142 <span class="preprocessor">#endif </span><span class="comment">/* UIP_URGDATA &gt; 0 */</span>
  175. <a name="l00143"></a>00143
  176. <a name="l00144"></a><a class="code" href="a01577.html#gac83200aec50ca08f657225b3c5b5c8dc">00144</a> <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> <a class="code" href="a01684.html#ga12a33f0c09711167bdf3dd7d7cf8c5a1" title="The length of the packet in the uip_buf buffer.">uip_len</a>, uip_slen;
  177. <a name="l00145"></a>00145 <span class="comment">/* The uip_len is either 8 or 16 bits,</span>
  178. <a name="l00146"></a>00146 <span class="comment"> depending on the maximum packet</span>
  179. <a name="l00147"></a>00147 <span class="comment"> size. */</span>
  180. <a name="l00148"></a>00148
  181. <a name="l00149"></a>00149 <a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> uip_flags; <span class="comment">/* The uip_flags variable is used for</span>
  182. <a name="l00150"></a>00150 <span class="comment"> communication between the TCP/IP stack</span>
  183. <a name="l00151"></a>00151 <span class="comment"> and the application program. */</span>
  184. <a name="l00152"></a><a class="code" href="a01684.html#ga76f4b1021e1a18d4d857ca8c121ec6e6">00152</a> <span class="keyword">struct </span><a class="code" href="a00092.html" title="Representation of a uIP TCP connection.">uip_conn</a> *<a class="code" href="a00092.html" title="Representation of a uIP TCP connection.">uip_conn</a>; <span class="comment">/* uip_conn always points to the current</span>
  185. <a name="l00153"></a>00153 <span class="comment"> connection. */</span>
  186. <a name="l00154"></a>00154
  187. <a name="l00155"></a>00155 <span class="keyword">struct </span>uip_conn uip_conns[<a class="code" href="a01585.html#gaf5fe83be78b78b9e7d9e7f1e34ab1cc5" title="The maximum number of simultaneously open TCP connections.">UIP_CONNS</a>];
  188. <a name="l00156"></a>00156 <span class="comment">/* The uip_conns array holds all TCP</span>
  189. <a name="l00157"></a>00157 <span class="comment"> connections. */</span>
  190. <a name="l00158"></a>00158 <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> uip_listenports[<a class="code" href="a01585.html#ga8f4ebd8ef6c0ea665ed351d87fec09fd" title="The maximum number of simultaneously listening TCP ports.">UIP_LISTENPORTS</a>];
  191. <a name="l00159"></a>00159 <span class="comment">/* The uip_listenports list all currently</span>
  192. <a name="l00160"></a>00160 <span class="comment"> listning ports. */</span>
  193. <a name="l00161"></a>00161 <span class="preprocessor">#if UIP_UDP</span>
  194. <a name="l00162"></a><a class="code" href="a01684.html#ga210f227119fc972e6222c9cb452e15a9">00162</a> <span class="preprocessor"></span><span class="keyword">struct </span><a class="code" href="a00119.html" title="Representation of a uIP UDP connection.">uip_udp_conn</a> *<a class="code" href="a00119.html" title="Representation of a uIP UDP connection.">uip_udp_conn</a>;
  195. <a name="l00163"></a>00163 <span class="keyword">struct </span>uip_udp_conn uip_udp_conns[<a class="code" href="a01584.html#ga196379ceb1219a99f4495e41ccc9bbfb" title="The maximum amount of concurrent UDP connections.">UIP_UDP_CONNS</a>];
  196. <a name="l00164"></a>00164 <span class="preprocessor">#endif </span><span class="comment">/* UIP_UDP */</span>
  197. <a name="l00165"></a>00165
  198. <a name="l00166"></a>00166 <span class="keyword">static</span> <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> ipid; <span class="comment">/* Ths ipid variable is an increasing</span>
  199. <a name="l00167"></a>00167 <span class="comment"> number that is used for the IP ID</span>
  200. <a name="l00168"></a>00168 <span class="comment"> field. */</span>
  201. <a name="l00169"></a>00169
  202. <a name="l00170"></a><a class="code" href="a01573.html#ga22f140b02c354dfebcc7ad481c3bcd68">00170</a> <span class="keywordtype">void</span> <a class="code" href="a01684.html#ga22f140b02c354dfebcc7ad481c3bcd68" title="uIP initialization function.">uip_setipid</a>(<a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> <span class="keywordtype">id</span>) { ipid = id; }
  203. <a name="l00171"></a>00171
  204. <a name="l00172"></a>00172 <span class="keyword">static</span> <a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> iss[4]; <span class="comment">/* The iss variable is used for the TCP</span>
  205. <a name="l00173"></a>00173 <span class="comment"> initial sequence number. */</span>
  206. <a name="l00174"></a>00174
  207. <a name="l00175"></a>00175 <span class="preprocessor">#if UIP_ACTIVE_OPEN || UIP_UDP</span>
  208. <a name="l00176"></a>00176 <span class="preprocessor"></span><span class="keyword">static</span> <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> lastport; <span class="comment">/* Keeps track of the last port used for</span>
  209. <a name="l00177"></a>00177 <span class="comment"> a new connection. */</span>
  210. <a name="l00178"></a>00178 <span class="preprocessor">#endif </span><span class="comment">/* UIP_ACTIVE_OPEN || UIP_UDP */</span>
  211. <a name="l00179"></a>00179
  212. <a name="l00180"></a>00180 <span class="comment">/* Temporary variables. */</span>
  213. <a name="l00181"></a><a class="code" href="a01578.html#ga2a0cf5d86c58fab216414ce59bf1fea1">00181</a> <a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> <a class="code" href="a01684.html#ga2a0cf5d86c58fab216414ce59bf1fea1" title="4-byte array used for the 32-bit sequence number calculations.">uip_acc32</a>[4];
  214. <a name="l00182"></a>00182 <span class="keyword">static</span> <a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> c, opt;
  215. <a name="l00183"></a>00183 <span class="keyword">static</span> <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> tmp16;
  216. <a name="l00184"></a>00184
  217. <a name="l00185"></a>00185 <span class="comment">/* Structures and definitions. */</span>
  218. <a name="l00186"></a>00186 <span class="preprocessor">#define TCP_FIN 0x01</span>
  219. <a name="l00187"></a>00187 <span class="preprocessor"></span><span class="preprocessor">#define TCP_SYN 0x02</span>
  220. <a name="l00188"></a>00188 <span class="preprocessor"></span><span class="preprocessor">#define TCP_RST 0x04</span>
  221. <a name="l00189"></a>00189 <span class="preprocessor"></span><span class="preprocessor">#define TCP_PSH 0x08</span>
  222. <a name="l00190"></a>00190 <span class="preprocessor"></span><span class="preprocessor">#define TCP_ACK 0x10</span>
  223. <a name="l00191"></a>00191 <span class="preprocessor"></span><span class="preprocessor">#define TCP_URG 0x20</span>
  224. <a name="l00192"></a>00192 <span class="preprocessor"></span><span class="preprocessor">#define TCP_CTL 0x3f</span>
  225. <a name="l00193"></a>00193 <span class="preprocessor"></span>
  226. <a name="l00194"></a>00194 <span class="preprocessor">#define TCP_OPT_END 0 </span><span class="comment">/* End of TCP options list */</span>
  227. <a name="l00195"></a>00195 <span class="preprocessor">#define TCP_OPT_NOOP 1 </span><span class="comment">/* &quot;No-operation&quot; TCP option */</span>
  228. <a name="l00196"></a>00196 <span class="preprocessor">#define TCP_OPT_MSS 2 </span><span class="comment">/* Maximum segment size TCP option */</span>
  229. <a name="l00197"></a>00197
  230. <a name="l00198"></a>00198 <span class="preprocessor">#define TCP_OPT_MSS_LEN 4 </span><span class="comment">/* Length of TCP MSS option. */</span>
  231. <a name="l00199"></a>00199
  232. <a name="l00200"></a>00200 <span class="preprocessor">#define ICMP_ECHO_REPLY 0</span>
  233. <a name="l00201"></a>00201 <span class="preprocessor"></span><span class="preprocessor">#define ICMP_ECHO 8</span>
  234. <a name="l00202"></a>00202 <span class="preprocessor"></span>
  235. <a name="l00203"></a>00203 <span class="preprocessor">#define ICMP_DEST_UNREACHABLE 3</span>
  236. <a name="l00204"></a>00204 <span class="preprocessor"></span><span class="preprocessor">#define ICMP_PORT_UNREACHABLE 3</span>
  237. <a name="l00205"></a>00205 <span class="preprocessor"></span>
  238. <a name="l00206"></a>00206 <span class="preprocessor">#define ICMP6_ECHO_REPLY 129</span>
  239. <a name="l00207"></a>00207 <span class="preprocessor"></span><span class="preprocessor">#define ICMP6_ECHO 128</span>
  240. <a name="l00208"></a>00208 <span class="preprocessor"></span><span class="preprocessor">#define ICMP6_NEIGHBOR_SOLICITATION 135</span>
  241. <a name="l00209"></a>00209 <span class="preprocessor"></span><span class="preprocessor">#define ICMP6_NEIGHBOR_ADVERTISEMENT 136</span>
  242. <a name="l00210"></a>00210 <span class="preprocessor"></span>
  243. <a name="l00211"></a>00211 <span class="preprocessor">#define ICMP6_FLAG_S (1 &lt;&lt; 6)</span>
  244. <a name="l00212"></a>00212 <span class="preprocessor"></span>
  245. <a name="l00213"></a>00213 <span class="preprocessor">#define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1</span>
  246. <a name="l00214"></a>00214 <span class="preprocessor"></span><span class="preprocessor">#define ICMP6_OPTION_TARGET_LINK_ADDRESS 2</span>
  247. <a name="l00215"></a>00215 <span class="preprocessor"></span>
  248. <a name="l00216"></a>00216
  249. <a name="l00217"></a>00217 <span class="comment">/* Macros. */</span>
  250. <a name="l00218"></a>00218 <span class="preprocessor">#define BUF ((struct uip_tcpip_hdr *)&amp;uip_buf[UIP_LLH_LEN])</span>
  251. <a name="l00219"></a>00219 <span class="preprocessor"></span><span class="preprocessor">#define FBUF ((struct uip_tcpip_hdr *)&amp;uip_reassbuf[0])</span>
  252. <a name="l00220"></a>00220 <span class="preprocessor"></span><span class="preprocessor">#define ICMPBUF ((struct uip_icmpip_hdr *)&amp;uip_buf[UIP_LLH_LEN])</span>
  253. <a name="l00221"></a>00221 <span class="preprocessor"></span><span class="preprocessor">#define UDPBUF ((struct uip_udpip_hdr *)&amp;uip_buf[UIP_LLH_LEN])</span>
  254. <a name="l00222"></a>00222 <span class="preprocessor"></span>
  255. <a name="l00223"></a>00223
  256. <a name="l00224"></a>00224 <span class="preprocessor">#if UIP_STATISTICS == 1</span>
  257. <a name="l00225"></a>00225 <span class="preprocessor"></span><span class="keyword">struct </span><a class="code" href="a00118.html" title="The structure holding the TCP/IP statistics that are gathered if UIP_STATISTICS is...">uip_stats</a> uip_stat;
  258. <a name="l00226"></a>00226 <span class="preprocessor">#define UIP_STAT(s) s</span>
  259. <a name="l00227"></a>00227 <span class="preprocessor"></span><span class="preprocessor">#else</span>
  260. <a name="l00228"></a>00228 <span class="preprocessor"></span><span class="preprocessor">#define UIP_STAT(s)</span>
  261. <a name="l00229"></a>00229 <span class="preprocessor"></span><span class="preprocessor">#endif </span><span class="comment">/* UIP_STATISTICS == 1 */</span>
  262. <a name="l00230"></a>00230
  263. <a name="l00231"></a>00231 <span class="preprocessor">#if UIP_LOGGING == 1</span>
  264. <a name="l00232"></a>00232 <span class="preprocessor"></span><span class="preprocessor">#include &lt;stdio.h&gt;</span>
  265. <a name="l00233"></a>00233 <span class="keywordtype">void</span> <a class="code" href="a01589.html#gab58e1ceb7cb73ca2bcd73146b6c1b4e7" title="Print out a uIP log message.">uip_log</a>(<span class="keywordtype">char</span> *msg);
  266. <a name="l00234"></a>00234 <span class="preprocessor">#define UIP_LOG(m) uip_log(m)</span>
  267. <a name="l00235"></a>00235 <span class="preprocessor"></span><span class="preprocessor">#else</span>
  268. <a name="l00236"></a>00236 <span class="preprocessor"></span><span class="preprocessor">#define UIP_LOG(m)</span>
  269. <a name="l00237"></a>00237 <span class="preprocessor"></span><span class="preprocessor">#endif </span><span class="comment">/* UIP_LOGGING == 1 */</span>
  270. <a name="l00238"></a>00238
  271. <a name="l00239"></a>00239 <span class="preprocessor">#if ! UIP_ARCH_ADD32</span>
  272. <a name="l00240"></a>00240 <span class="preprocessor"></span><span class="keywordtype">void</span>
  273. <a name="l00241"></a><a class="code" href="a01578.html#ga6832e4d2d046536b6472f7ac92340f68">00241</a> <a class="code" href="a01684.html#ga6832e4d2d046536b6472f7ac92340f68" title="Carry out a 32-bit addition.">uip_add32</a>(<a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> *op32, <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> op16)
  274. <a name="l00242"></a>00242 {
  275. <a name="l00243"></a>00243 <a class="code" href="a01684.html#ga2a0cf5d86c58fab216414ce59bf1fea1" title="4-byte array used for the 32-bit sequence number calculations.">uip_acc32</a>[3] = op32[3] + (op16 &amp; 0xff);
  276. <a name="l00244"></a>00244 <a class="code" href="a01684.html#ga2a0cf5d86c58fab216414ce59bf1fea1" title="4-byte array used for the 32-bit sequence number calculations.">uip_acc32</a>[2] = op32[2] + (op16 &gt;&gt; 8);
  277. <a name="l00245"></a>00245 uip_acc32[1] = op32[1];
  278. <a name="l00246"></a>00246 uip_acc32[0] = op32[0];
  279. <a name="l00247"></a>00247
  280. <a name="l00248"></a>00248 <span class="keywordflow">if</span>(uip_acc32[2] &lt; (op16 &gt;&gt; 8)) {
  281. <a name="l00249"></a>00249 ++uip_acc32[1];
  282. <a name="l00250"></a>00250 <span class="keywordflow">if</span>(uip_acc32[1] == 0) {
  283. <a name="l00251"></a>00251 ++uip_acc32[0];
  284. <a name="l00252"></a>00252 }
  285. <a name="l00253"></a>00253 }
  286. <a name="l00254"></a>00254
  287. <a name="l00255"></a>00255
  288. <a name="l00256"></a>00256 <span class="keywordflow">if</span>(uip_acc32[3] &lt; (op16 &amp; 0xff)) {
  289. <a name="l00257"></a>00257 ++uip_acc32[2];
  290. <a name="l00258"></a>00258 <span class="keywordflow">if</span>(uip_acc32[2] == 0) {
  291. <a name="l00259"></a>00259 ++uip_acc32[1];
  292. <a name="l00260"></a>00260 <span class="keywordflow">if</span>(uip_acc32[1] == 0) {
  293. <a name="l00261"></a>00261 ++uip_acc32[0];
  294. <a name="l00262"></a>00262 }
  295. <a name="l00263"></a>00263 }
  296. <a name="l00264"></a>00264 }
  297. <a name="l00265"></a>00265 }
  298. <a name="l00266"></a>00266
  299. <a name="l00267"></a>00267 <span class="preprocessor">#endif </span><span class="comment">/* UIP_ARCH_ADD32 */</span>
  300. <a name="l00268"></a>00268
  301. <a name="l00269"></a>00269 <span class="preprocessor">#if ! UIP_ARCH_CHKSUM</span>
  302. <a name="l00270"></a>00270 <span class="preprocessor"></span><span class="comment">/*---------------------------------------------------------------------------*/</span>
  303. <a name="l00271"></a>00271 <span class="keyword">static</span> <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>
  304. <a name="l00272"></a>00272 chksum(<a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> sum, <span class="keyword">const</span> <a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> *data, <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> len)
  305. <a name="l00273"></a>00273 {
  306. <a name="l00274"></a>00274 <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> t;
  307. <a name="l00275"></a>00275 <span class="keyword">const</span> <a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> *dataptr;
  308. <a name="l00276"></a>00276 <span class="keyword">const</span> <a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> *last_byte;
  309. <a name="l00277"></a>00277
  310. <a name="l00278"></a>00278 dataptr = data;
  311. <a name="l00279"></a>00279 last_byte = data + len - 1;
  312. <a name="l00280"></a>00280
  313. <a name="l00281"></a>00281 <span class="keywordflow">while</span>(dataptr &lt; last_byte) { <span class="comment">/* At least two more bytes */</span>
  314. <a name="l00282"></a>00282 t = (dataptr[0] &lt;&lt; 8) + dataptr[1];
  315. <a name="l00283"></a>00283 sum += t;
  316. <a name="l00284"></a>00284 <span class="keywordflow">if</span>(sum &lt; t) {
  317. <a name="l00285"></a>00285 sum++; <span class="comment">/* carry */</span>
  318. <a name="l00286"></a>00286 }
  319. <a name="l00287"></a>00287 dataptr += 2;
  320. <a name="l00288"></a>00288 }
  321. <a name="l00289"></a>00289
  322. <a name="l00290"></a>00290 <span class="keywordflow">if</span>(dataptr == last_byte) {
  323. <a name="l00291"></a>00291 t = (dataptr[0] &lt;&lt; 8) + 0;
  324. <a name="l00292"></a>00292 sum += t;
  325. <a name="l00293"></a>00293 <span class="keywordflow">if</span>(sum &lt; t) {
  326. <a name="l00294"></a>00294 sum++; <span class="comment">/* carry */</span>
  327. <a name="l00295"></a>00295 }
  328. <a name="l00296"></a>00296 }
  329. <a name="l00297"></a>00297
  330. <a name="l00298"></a>00298 <span class="comment">/* Return sum in host byte order. */</span>
  331. <a name="l00299"></a>00299 <span class="keywordflow">return</span> sum;
  332. <a name="l00300"></a>00300 }
  333. <a name="l00301"></a>00301 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  334. <a name="l00302"></a>00302 <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>
  335. <a name="l00303"></a>00303 <a class="code" href="a01684.html#gab6683dd83fe1c8de9a24086d4b69e907" title="Calculate the Internet checksum over a buffer.">uip_chksum</a>(<a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> *data, <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> len)
  336. <a name="l00304"></a>00304 {
  337. <a name="l00305"></a>00305 <span class="keywordflow">return</span> <a class="code" href="a01684.html#gae4394b6b3cd81f73b0e64a93e0558e00" title="Convert 16-bit quantity from host byte order to network byte order.">uip_htons</a>(chksum(0, (<a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> *)data, len));
  338. <a name="l00306"></a>00306 }
  339. <a name="l00307"></a>00307 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  340. <a name="l00308"></a>00308 <span class="preprocessor">#ifndef UIP_ARCH_IPCHKSUM</span>
  341. <a name="l00309"></a>00309 <span class="preprocessor"></span><a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>
  342. <a name="l00310"></a>00310 <a class="code" href="a01684.html#ga2addf34c7d457c1a7899a7e2171ef1e9" title="Calculate the IP header checksum of the packet header in uip_buf.">uip_ipchksum</a>(<span class="keywordtype">void</span>)
  343. <a name="l00311"></a>00311 {
  344. <a name="l00312"></a>00312 <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> sum;
  345. <a name="l00313"></a>00313
  346. <a name="l00314"></a>00314 sum = chksum(0, &amp;uip_buf[<a class="code" href="a01589.html#gae6f4a2453dbd8bc60e6a82774552366a" title="The link level header length.">UIP_LLH_LEN</a>], UIP_IPH_LEN);
  347. <a name="l00315"></a>00315 DEBUG_PRINTF(<span class="stringliteral">&quot;uip_ipchksum: sum 0x%04x\n&quot;</span>, sum);
  348. <a name="l00316"></a>00316 <span class="keywordflow">return</span> (sum == 0) ? 0xffff : <a class="code" href="a01684.html#gae4394b6b3cd81f73b0e64a93e0558e00" title="Convert 16-bit quantity from host byte order to network byte order.">uip_htons</a>(sum);
  349. <a name="l00317"></a>00317 }
  350. <a name="l00318"></a>00318 <span class="preprocessor">#endif</span>
  351. <a name="l00319"></a>00319 <span class="preprocessor"></span><span class="comment">/*---------------------------------------------------------------------------*/</span>
  352. <a name="l00320"></a>00320 <span class="keyword">static</span> <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>
  353. <a name="l00321"></a>00321 upper_layer_chksum(<a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> proto)
  354. <a name="l00322"></a>00322 {
  355. <a name="l00323"></a>00323 <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> upper_layer_len;
  356. <a name="l00324"></a>00324 <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> sum;
  357. <a name="l00325"></a>00325
  358. <a name="l00326"></a>00326 <span class="preprocessor">#if UIP_CONF_IPV6</span>
  359. <a name="l00327"></a>00327 <span class="preprocessor"></span> upper_layer_len = (((<a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>)(BUF-&gt;len[0]) &lt;&lt; 8) + BUF-&gt;len[1]);
  360. <a name="l00328"></a>00328 <span class="preprocessor">#else </span><span class="comment">/* UIP_CONF_IPV6 */</span>
  361. <a name="l00329"></a>00329 upper_layer_len = (((<a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>)(BUF-&gt;len[0]) &lt;&lt; 8) + BUF-&gt;len[1]) - UIP_IPH_LEN;
  362. <a name="l00330"></a>00330 <span class="preprocessor">#endif </span><span class="comment">/* UIP_CONF_IPV6 */</span>
  363. <a name="l00331"></a>00331
  364. <a name="l00332"></a>00332 <span class="comment">/* First sum pseudoheader. */</span>
  365. <a name="l00333"></a>00333
  366. <a name="l00334"></a>00334 <span class="comment">/* IP protocol and length fields. This addition cannot carry. */</span>
  367. <a name="l00335"></a>00335 sum = upper_layer_len + proto;
  368. <a name="l00336"></a>00336 <span class="comment">/* Sum IP source and destination addresses. */</span>
  369. <a name="l00337"></a>00337 sum = chksum(sum, (<a class="code" href="a01668.html#ga4caecabca98b43919dd11be1c0d4cd8e" title="The 8-bit unsigned data type.">u8_t</a> *)&amp;BUF-&gt;srcipaddr, 2 * <span class="keyword">sizeof</span>(<a class="code" href="a00106.html" title="Representation of an IP address.">uip_ipaddr_t</a>));
  370. <a name="l00338"></a>00338
  371. <a name="l00339"></a>00339 <span class="comment">/* Sum TCP header and data. */</span>
  372. <a name="l00340"></a>00340 sum = chksum(sum, &amp;uip_buf[UIP_IPH_LEN + <a class="code" href="a01589.html#gae6f4a2453dbd8bc60e6a82774552366a" title="The link level header length.">UIP_LLH_LEN</a>],
  373. <a name="l00341"></a>00341 upper_layer_len);
  374. <a name="l00342"></a>00342
  375. <a name="l00343"></a>00343 <span class="keywordflow">return</span> (sum == 0) ? 0xffff : <a class="code" href="a01684.html#gae4394b6b3cd81f73b0e64a93e0558e00" title="Convert 16-bit quantity from host byte order to network byte order.">uip_htons</a>(sum);
  376. <a name="l00344"></a>00344 }
  377. <a name="l00345"></a>00345 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  378. <a name="l00346"></a>00346 <span class="preprocessor">#if UIP_CONF_IPV6</span>
  379. <a name="l00347"></a>00347 <span class="preprocessor"></span><a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>
  380. <a name="l00348"></a>00348 <a class="code" href="a01684.html#ga5e6741b8bd3e0961fb87e3f85da27a7f" title="Calculate the ICMP checksum of the packet in uip_buf.">uip_icmp6chksum</a>(<span class="keywordtype">void</span>)
  381. <a name="l00349"></a>00349 {
  382. <a name="l00350"></a>00350 <span class="keywordflow">return</span> upper_layer_chksum(UIP_PROTO_ICMP6);
  383. <a name="l00351"></a>00351
  384. <a name="l00352"></a>00352 }
  385. <a name="l00353"></a>00353 <span class="preprocessor">#endif </span><span class="comment">/* UIP_CONF_IPV6 */</span>
  386. <a name="l00354"></a>00354 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  387. <a name="l00355"></a>00355 <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>
  388. <a name="l00356"></a>00356 <a class="code" href="a01684.html#ga85b65e38aa74eba18979156f97a94a87" title="Calculate the TCP checksum of the packet in uip_buf and uip_appdata.">uip_tcpchksum</a>(<span class="keywordtype">void</span>)
  389. <a name="l00357"></a>00357 {
  390. <a name="l00358"></a>00358 <span class="keywordflow">return</span> upper_layer_chksum(UIP_PROTO_TCP);
  391. <a name="l00359"></a>00359 }
  392. <a name="l00360"></a>00360 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  393. <a name="l00361"></a>00361 <span class="preprocessor">#if UIP_UDP_CHECKSUMS</span>
  394. <a name="l00362"></a>00362 <span class="preprocessor"></span><a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a>
  395. <a name="l00363"></a>00363 <a class="code" href="a01684.html#ga7023a34ba9e9d03b5fbedbcb32924453" title="Calculate the UDP checksum of the packet in uip_buf and uip_appdata.">uip_udpchksum</a>(<span class="keywordtype">void</span>)
  396. <a name="l00364"></a>00364 {
  397. <a name="l00365"></a>00365 <span class="keywordflow">return</span> upper_layer_chksum(UIP_PROTO_UDP);
  398. <a name="l00366"></a>00366 }
  399. <a name="l00367"></a>00367 <span class="preprocessor">#endif </span><span class="comment">/* UIP_UDP_CHECKSUMS */</span>
  400. <a name="l00368"></a>00368 <span class="preprocessor">#endif </span><span class="comment">/* UIP_ARCH_CHKSUM */</span>
  401. <a name="l00369"></a>00369 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  402. <a name="l00370"></a>00370 <span class="keywordtype">void</span>
  403. <a name="l00371"></a><a class="code" href="a01573.html#gac48ed5f0d27721ef62a3ed02a5ad8d2e">00371</a> <a class="code" href="a01684.html#gac48ed5f0d27721ef62a3ed02a5ad8d2e" title="uIP initialization function.">uip_init</a>(<span class="keywordtype">void</span>)
  404. <a name="l00372"></a>00372 {
  405. <a name="l00373"></a>00373 <span class="keywordflow">for</span>(c = 0; c &lt; <a class="code" href="a01585.html#ga8f4ebd8ef6c0ea665ed351d87fec09fd" title="The maximum number of simultaneously listening TCP ports.">UIP_LISTENPORTS</a>; ++c) {
  406. <a name="l00374"></a>00374 uip_listenports[c] = 0;
  407. <a name="l00375"></a>00375 }
  408. <a name="l00376"></a>00376 <span class="keywordflow">for</span>(c = 0; c &lt; <a class="code" href="a01585.html#gaf5fe83be78b78b9e7d9e7f1e34ab1cc5" title="The maximum number of simultaneously open TCP connections.">UIP_CONNS</a>; ++c) {
  409. <a name="l00377"></a>00377 uip_conns[c].<a class="code" href="a00092.html#aa5f58074435cdc180f17de69651beebd" title="TCP state and flags.">tcpstateflags</a> = UIP_CLOSED;
  410. <a name="l00378"></a>00378 }
  411. <a name="l00379"></a>00379 <span class="preprocessor">#if UIP_ACTIVE_OPEN || UIP_UDP</span>
  412. <a name="l00380"></a>00380 <span class="preprocessor"></span> lastport = 1024;
  413. <a name="l00381"></a>00381 <span class="preprocessor">#endif </span><span class="comment">/* UIP_ACTIVE_OPEN || UIP_UDP */</span>
  414. <a name="l00382"></a>00382
  415. <a name="l00383"></a>00383 <span class="preprocessor">#if UIP_UDP</span>
  416. <a name="l00384"></a>00384 <span class="preprocessor"></span> <span class="keywordflow">for</span>(c = 0; c &lt; <a class="code" href="a01584.html#ga196379ceb1219a99f4495e41ccc9bbfb" title="The maximum amount of concurrent UDP connections.">UIP_UDP_CONNS</a>; ++c) {
  417. <a name="l00385"></a>00385 uip_udp_conns[c].<a class="code" href="a00119.html#a981392e295db4d024eea95805c51c371" title="The local port number in network byte order.">lport</a> = 0;
  418. <a name="l00386"></a>00386 }
  419. <a name="l00387"></a>00387 <span class="preprocessor">#endif </span><span class="comment">/* UIP_UDP */</span>
  420. <a name="l00388"></a>00388
  421. <a name="l00389"></a>00389
  422. <a name="l00390"></a>00390 <span class="comment">/* IPv4 initialization. */</span>
  423. <a name="l00391"></a>00391 <span class="preprocessor">#if UIP_FIXEDADDR == 0</span>
  424. <a name="l00392"></a>00392 <span class="preprocessor"></span> <span class="comment">/* uip_hostaddr[0] = uip_hostaddr[1] = 0;*/</span>
  425. <a name="l00393"></a>00393 <span class="preprocessor">#endif </span><span class="comment">/* UIP_FIXEDADDR */</span>
  426. <a name="l00394"></a>00394
  427. <a name="l00395"></a>00395 }
  428. <a name="l00396"></a>00396 <span class="comment">/*---------------------------------------------------------------------------*/</span>
  429. <a name="l00397"></a>00397 <span class="preprocessor">#if UIP_ACTIVE_OPEN</span>
  430. <a name="l00398"></a>00398 <span class="preprocessor"></span><span class="keyword">struct </span>uip_conn *
  431. <a name="l00399"></a>00399 <a class="code" href="a01575.html#ga8096b0c4b543dc408f4dd031ddae7240" title="Connect to a remote host using TCP.">uip_connect</a>(<a class="code" href="a00106.html" title="Representation of an IP address.">uip_ipaddr_t</a> *<a class="code" href="a00092.html#a79510aa86d3fa0a0fc6cfc49b1da7279" title="The IP address of the remote host.">ripaddr</a>, <a class="code" href="a01668.html#ga77570ac4fcab86864fa1916e55676da2" title="The 16-bit unsigned data type.">u16_t</a> <a class="code" href="a00092.html#a1df6aa054ef2fa634ac4c6f418228285" title="The local remote TCP port, in network byte order.">rport</a>)
  432. <a name="l00400"></a>00400 {
  433. <a name="l00401"></a>00401 <span class="keyword">register</span> <span class="keyword">struct </span>uip_conn *conn, *cconn;
  434. <a name="l00402"></a>00402
  435. <a name="l00403"></a>00403 <span class="comment">/* Find an unused local port. */</span>
  436. <a name="l00404"></a>00404 again:
  437. <a name="l00405"></a>00405 ++lastport;
  438. <a name="l00406"></a>00406
  439. <a name="l00407"></a>00407 <span class="keywordflow">if</span>(lastport &gt;= 32000) {
  440. <a name="l00408"></a>00408 lastport = 4096;
  441. <a name="l00409"></a>00409 }
  442. <a name="l00410"></a>00410
  443. <a name="l00411"></a>00411 <span class="comment">/* Check if this port is already in use, and if so try to find</span>
  444. <a name="l00412"></a>00412 <span class="comment"> another one. */</span>
  445. <a name="l00413"></a>00413 <span class="keywordflow">for</span>(c = 0; c &lt; <a class="code" href="a01585.html#gaf5fe83be78b78b9e7d9e7f1e34ab1cc5" title="The maximum number of simultaneously open TCP connections.">UIP_CONNS</a>; ++c) {
  446. <a name="l00414"></a>00414 conn = &amp;uip_conns[c];
  447. <a name="l00415"></a>00415 <span class="keywordflow">if</span>(conn-&gt;<a class="code" href="a00092.html#aa5f58074435cdc180f17de69651beebd" title="TCP state and flags.">tcpstateflags</a> != UIP_CLOSED &amp;&amp;
  448. <a name="l00416"></a>00416 conn-&gt;<a class="code" href="a00092.html#a0cd09beee671e7e9efb0b4aced10249e" title="The local TCP port, in network byte order.">lport</a> == <a class="code" href="a01684.html#gae4394b6b3cd81f73b0e64a93e0558e00" title="Convert 16-bit quantity from host byte order to network byte order.">uip_htons</a>(lastport)) {
  449. <a name="l00417"></a>00417 <span class="keywordflow">goto</span> again;
  450. <a name="l00418"></a>00418 }
  451. <a name="l00419"></a>00419 }
  452. <a name="l00420"></a>00420
  453. <a name="l00421"></a>00421 conn = 0;
  454. <a name="l00422"></a>00422 <span class="keywordflow">for</span>(c = 0; c &lt; UIP_CONNS; ++c) {
  455. <a name="l00423"></a>00423 cconn = &amp;uip_conns[c];
  456. <a name="l00424"></a>00424 <span class="keywordflow">if</span>(cconn-&gt;<a class="code" href="a00092.html#aa5f58074435cdc180f17de69651beebd" title="TCP state and flags.">tcpstateflags</a> == UIP_CLOSED) {
  457. <a name="l00425"></a>00425 conn = cconn;
  458. <a name="l00426"></a>00426 <span class="keywordflow">break</span>;
  459. <a name="l00427"></a>00427 }
  460. <a name="l00428"></a>00428 <span class="keywordflow">if</span>(cconn-&gt;<a class="code" href="a00092.html#aa5f58074435cdc180f17de69651beebd" title="TCP state and flags.">tcpstateflags</a> == UIP_TIME_WAIT) {
  461. <a name="l00429"></a>00429 <span class="keywordflow">if</span>(conn == 0 ||
  462. <a name="l00430"></a>00430 cconn-&gt;<a class="code" href="a00092.html#a2d9732cf5752d30bd11cb25dc7d0c8d3" title="The retransmission timer.">timer</a> &gt; conn-&gt;<a class="code" href="a00092.html#a2d9732cf5752d30bd11cb25dc7d0c8d3" title="The retransmission timer.">timer</a>) {
  463. <a name="l00431"></a>00431 conn = cconn;
  464. <a name="l00432"></a>00432 }
  465. <a name="l00433"></a>00433 }
  466. <a name="l00434"></a>00434 }
  467. <a name="l00435"></a>00435
  468. <a name="l00436"></a>00436 <span class="keywordflow">if</span>(conn == 0) {
  469. <a name="l00437"></a>00437 <span class="keywordflow">return</span> 0;
  470. <a name="l00438"></a>00438 }
  471. <a name="l00439"></a>00439
  472. <a name="l00440"></a>00440 conn-&gt;<a class="code" href="a00092.html#aa5f58074435cdc180f17de69651beebd" title="TCP state and flags.">tcpstateflags</a> = UIP_SYN_SENT;
  473. <a name="l00441"></a>00441
  474. <a name="l00442"></a>00442 conn-&gt;<a class="code" href="a00092.html#a8f6b08a5ba2a…

Large files files are truncated, but you can click here to view the full file