📄 tcp_8c-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>/opentcp/tcp.c Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.2.18 --><center><a class="qindex" href="main.html">Main Page</a> <a class="qindex" href="modules.html">Modules</a> <a class="qindex" href="annotated.html">Data Structures</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Data Fields</a> <a class="qindex" href="globals.html">Globals</a> <a class="qindex" href="pages.html">Related Pages</a> <a class="qindex" href="examples.html">Examples</a> </center><hr><h1>/opentcp/tcp.c</h1><a href="tcp_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*</span>00002 <span class="comment"> *Copyright (c) 2000-2002 Viola Systems Ltd.</span>00003 <span class="comment"> *All rights reserved.</span>00004 <span class="comment"> *</span>00005 <span class="comment"> *Redistribution and use in source and binary forms, with or without </span>00006 <span class="comment"> *modification, are permitted provided that the following conditions </span>00007 <span class="comment"> *are met:</span>00008 <span class="comment"> *</span>00009 <span class="comment"> *1. Redistributions of source code must retain the above copyright </span>00010 <span class="comment"> *notice, this list of conditions and the following disclaimer.</span>00011 <span class="comment"> *</span>00012 <span class="comment"> *2. Redistributions in binary form must reproduce the above copyright </span>00013 <span class="comment"> *notice, this list of conditions and the following disclaimer in the </span>00014 <span class="comment"> *documentation and/or other materials provided with the distribution.</span>00015 <span class="comment"> *</span>00016 <span class="comment"> *3. The end-user documentation included with the redistribution, if </span>00017 <span class="comment"> *any, must include the following acknowledgment:</span>00018 <span class="comment"> * "This product includes software developed by Viola </span>00019 <span class="comment"> * Systems (http://www.violasystems.com/)."</span>00020 <span class="comment"> *</span>00021 <span class="comment"> *Alternately, this acknowledgment may appear in the software itself, </span>00022 <span class="comment"> *if and wherever such third-party acknowledgments normally appear.</span>00023 <span class="comment"> *</span>00024 <span class="comment"> *4. The names "OpenTCP" and "Viola Systems" must not be used to </span>00025 <span class="comment"> *endorse or promote products derived from this software without prior </span>00026 <span class="comment"> *written permission. For written permission, please contact </span>00027 <span class="comment"> *opentcp@opentcp.org.</span>00028 <span class="comment"> *</span>00029 <span class="comment"> *5. Products derived from this software may not be called "OpenTCP", </span>00030 <span class="comment"> *nor may "OpenTCP" appear in their name, without prior written </span>00031 <span class="comment"> *permission of the Viola Systems Ltd.</span>00032 <span class="comment"> *</span>00033 <span class="comment"> *THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED </span>00034 <span class="comment"> *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF </span>00035 <span class="comment"> *MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. </span>00036 <span class="comment"> *IN NO EVENT SHALL VIOLA SYSTEMS LTD. OR ITS CONTRIBUTORS BE LIABLE </span>00037 <span class="comment"> *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR </span>00038 <span class="comment"> *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF </span>00039 <span class="comment"> *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR </span>00040 <span class="comment"> *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, </span>00041 <span class="comment"> *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE </span>00042 <span class="comment"> *OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, </span>00043 <span class="comment"> *EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>00044 <span class="comment"> *====================================================================</span>00045 <span class="comment"> *</span>00046 <span class="comment"> *OpenTCP is the unified open source TCP/IP stack available on a series </span>00047 <span class="comment"> *of 8/16-bit microcontrollers, please see <http://www.opentcp.org>.</span>00048 <span class="comment"> *</span>00049 <span class="comment"> *For more information on how to network-enable your devices, or how to </span>00050 <span class="comment"> *obtain commercial technical support for OpenTCP, please see </span>00051 <span class="comment"> *<http://www.violasystems.com/>.</span>00052 <span class="comment"> */</span>00053 00085 <span class="preprocessor">#include <<a class="code" href="debug_8h.html">inet/debug.h</a>></span>00086 <span class="preprocessor">#include <<a class="code" href="datatypes_8h.html">inet/datatypes.h</a>></span>00087 <span class="preprocessor">#include <<a class="code" href="timers_8h.html">inet/timers.h</a>></span>00088 <span class="preprocessor">#include <<a class="code" href="ethernet_8h.html">inet/ethernet.h</a>></span>00089 <span class="preprocessor">#include <<a class="code" href="ip_8h.html">inet/ip.h</a>></span>00090 <span class="preprocessor">#include <<a class="code" href="tcp__ip_8h.html">inet/tcp_ip.h</a>></span>00091 <span class="preprocessor">#include <<a class="code" href="system_8h.html">inet/system.h</a>></span>00092 <a name="l00102"></a><a class="code" href="tcp_8c.html#a0">00102</a> <span class="keyword">struct </span><a class="code" href="structtcp__frame.html">tcp_frame</a> received_tcp_packet;00103 <a name="l00118"></a><a class="code" href="tcp_8c.html#a1">00118</a> <span class="keyword">struct </span><a class="code" href="structtcb.html">tcb</a> tcp_socket[NO_OF_TCPSOCKETS + 1]; 00119 <a name="l00120"></a><a class="code" href="tcp_8c.html#a2">00120</a> UINT8 <a class="code" href="tcp_8c.html#a2">tcp_tempbuf</a>[MIN_TCP_HLEN + 1]; 00123 <span class="comment">/***********************************************************************/</span>00124 <span class="comment">/******* TCP API functions ********/</span>00125 <span class="comment">/***********************************************************************/</span>00126 <a name="l00155"></a><a class="code" href="tcp_8c.html#a3">00155</a> INT8 <a class="code" href="tcp_8c.html#a3">tcp_getsocket</a> (UINT8 soctype, UINT8 tos, UINT16 tout, INT32 (*listener)(INT8, UINT8, UINT32, UINT32) )00156 {00157 INT8 i;00158 <span class="keyword">struct </span><a class="code" href="structtcb.html">tcb</a>* soc;00159 00160 <span class="keywordflow">if</span>( NO_OF_TCPSOCKETS < 0 )00161 <span class="keywordflow">return</span>(-1);00162 00163 <span class="keywordflow">if</span>( NO_OF_TCPSOCKETS == 0 )00164 <span class="keywordflow">return</span>(-1);00165 00166 <span class="keywordflow">if</span>( (soctype != <a class="code" href="tcp__ip_8h.html#a39">TCP_TYPE_SERVER</a>) &&00167 (soctype != <a class="code" href="tcp__ip_8h.html#a40">TCP_TYPE_CLIENT</a>) &&00168 (soctype != <a class="code" href="tcp__ip_8h.html#a41">TCP_TYPE_CLIENT_SERVER</a>) &&00169 (soctype != <a class="code" href="tcp__ip_8h.html#a38">TCP_TYPE_NONE</a>) ) {00170 TCP_DEBUGOUT(<span class="stringliteral">"Invalid socket type requested\r\n"</span>);00171 <span class="keywordflow">return</span>(-1);00172 }00173 00174 <span class="keywordflow">if</span>(listener == 0) {00175 TCP_DEBUGOUT(<span class="stringliteral">"ERROR:Event listener function not specified\r\n"</span>);00176 <span class="keywordflow">return</span>(-1);00177 }00178 00179 TCP_DEBUGOUT(<span class="stringliteral">"Searching for free TCP socket...\r\n"</span>);00180 00181 <span class="keywordflow">for</span>(i=0; i < NO_OF_TCPSOCKETS; i++) {00182 soc = &tcp_socket[i]; <span class="comment">/* Get Socket */</span>00183 00184 <span class="keywordflow">if</span>(soc-><a class="code" href="structtcb.html#m0">state</a> == <a class="code" href="tcp__ip_8h.html#a42">TCP_STATE_FREE</a>) {00185 <span class="comment">/* We found it */</span>00186 00187 TCP_DEBUGOUT(<span class="stringliteral">"Free socket found\r\n"</span>);00188 00189 soc-><a class="code" href="structtcb.html#m0">state</a> = <a class="code" href="tcp__ip_8h.html#a43">TCP_STATE_RESERVED</a>;00190 soc-><a class="code" href="structtcb.html#m1">type</a> = soctype;00191 soc-><a class="code" href="structtcb.html#m11">tos</a> = tos;00192 soc-><a class="code" href="structtcb.html#m16">event_listener</a> = listener;00193 soc-><a class="code" href="structtcb.html#m3">rem_ip</a> = 0;00194 soc-><a class="code" href="structtcb.html#m4">remport</a> = 0;00195 soc-><a class="code" href="structtcb.html#m5">locport</a> = 0;00196 soc-><a class="code" href="structtcb.html#m2">flags</a> = 0;00197 soc-><a class="code" href="structtcb.html#m10">tout</a> = tout*<a class="code" href="timers_8h.html#a1">TIMERTIC</a>;00198 00199 <span class="keywordflow">return</span>(i);00200 }00201 00202 }00203 00204 <span class="comment">/* We are there so no socket found */</span>00205 00206 TCP_DEBUGOUT(<span class="stringliteral">"No socket found\r\n"</span>);00207 <span class="keywordflow">return</span>(-1);00208 00209 }00210 <a name="l00228"></a><a class="code" href="tcp_8c.html#a4">00228</a> INT8 <a class="code" href="tcp_8c.html#a4">tcp_releasesocket</a> (INT8 sochandle)00229 {00230 <span class="keyword">struct </span><a class="code" href="structtcb.html">tcb</a>* soc;00231 00232 <span class="keywordflow">if</span>( NO_OF_TCPSOCKETS < 0 )00233 <span class="keywordflow">return</span>(-1);00234 00235 <span class="keywordflow">if</span>( NO_OF_TCPSOCKETS == 0 )00236 <span class="keywordflow">return</span>(-1);00237 00238 <span class="keywordflow">if</span>( sochandle > NO_OF_TCPSOCKETS ) {00239 TCP_DEBUGOUT(<span class="stringliteral">"Socket handle non-valid\r\n"</span>);00240 <span class="keywordflow">return</span>(-1);00241 }00242 00243 <span class="keywordflow">if</span>( sochandle < 0 ) {00244 TCP_DEBUGOUT(<span class="stringliteral">"Socket handle non-valid\r\n"</span>);00245 <span class="keywordflow">return</span>(-1);00246 }00247 00248 soc = &tcp_socket[sochandle]; <span class="comment">/* Get referense */</span>00249 00250 <span class="keywordflow">if</span>( (soc-><a class="code" href="structtcb.html#m0">state</a> != <a class="code" href="tcp__ip_8h.html#a42">TCP_STATE_FREE</a>) &&00251 (soc-><a class="code" href="structtcb.html#m0">state</a> != <a class="code" href="tcp__ip_8h.html#a43">TCP_STATE_RESERVED</a>) &&00252 (soc-><a class="code" href="structtcb.html#m0">state</a> != <a class="code" href="tcp__ip_8h.html#a44">TCP_STATE_CLOSED</a>) ) {00253 TCP_DEBUGOUT(<span class="stringliteral">"Socket is not on valid state to be released\r\n"</span>);00254 <span class="keywordflow">return</span>(-1);00255 }00256 00257 <span class="comment">/* We are there so all OK */</span>00258 00259 soc-><a class="code" href="structtcb.html#m0">state</a> = <a class="code" href="tcp__ip_8h.html#a42">TCP_STATE_FREE</a>;00260 soc-><a class="code" href="structtcb.html#m1">type</a> = <a class="code" href="tcp__ip_8h.html#a38">TCP_TYPE_NONE</a>;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -