📄 icmp_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/icmp.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/icmp.c</h1><a href="icmp_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 00072 <span class="preprocessor">#include <<a class="code" href="debug_8h.html">inet/debug.h</a>></span>00073 <span class="preprocessor">#include <<a class="code" href="datatypes_8h.html">inet/datatypes.h</a>></span>00074 <span class="preprocessor">#include <<a class="code" href="ethernet_8h.html">inet/ethernet.h</a>></span>00075 <span class="preprocessor">#include <<a class="code" href="ip_8h.html">inet/ip.h</a>></span>00076 <span class="preprocessor">#include <<a class="code" href="tcp__ip_8h.html">inet/tcp_ip.h</a>></span>00077 <span class="preprocessor">#include <<a class="code" href="system_8h.html">inet/system.h</a>></span>00078 <a name="l00097"></a><a class="code" href="icmp_8c.html#a2">00097</a> INT16 <a class="code" href="icmp_8c.html#a2">process_icmp_in</a> (<span class="keyword">struct</span> <a class="code" href="structip__frame.html">ip_frame</a>* frame, UINT16 len) 00098 {00099 UINT8 type;00100 UINT8 code;00101 UINT16 checksum;00102 UINT16 i;00103 UINT16 j;00104 UINT8 tbuf[16];00105 00106 <span class="comment">/* Is this ICMP? */</span>00107 00108 ICMP_DEBUGOUT(<span class="stringliteral">"Processing ICMP...\n\r"</span>);00109 00110 <span class="keywordflow">if</span>( frame-><a class="code" href="structip__frame.html#m6">protocol</a> != <a class="code" href="ip_8h.html#a1">IP_ICMP</a> ) {00111 ICMP_DEBUGOUT(<span class="stringliteral">"ERROR: The protocol is not ICMP\n\r"</span>);00112 <span class="keywordflow">return</span>(-1);00113 }00114 00115 <span class="comment">/* Calculate checksum for received packet */</span>00116 00117 checksum = 0;00118 i = len;00119 00120 <a class="code" href="system_8h.html#a14">NETWORK_RECEIVE_INITIALIZE</a>(frame-><a class="code" href="structip__frame.html#m11">buf_index</a>);00121 00122 <span class="keywordflow">while</span>(i>15){00123 00124 <a class="code" href="system_8h.html#a10">RECEIVE_NETWORK_BUF</a>(tbuf, 16); 00125 00126 checksum = <a class="code" href="ip_8c.html#a8">ip_checksum_buf</a>(checksum, tbuf,16);00127 00128 i -= 16; 00129 }00130 00131 <span class="keywordflow">for</span>(j=0; j < i; j++)00132 checksum = <a class="code" href="ip_8c.html#a7">ip_checksum</a>(checksum, <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>(), (UINT8)j);00133 00134 checksum = ~ checksum;00135 00136 <span class="keywordflow">if</span>(checksum != IP_GOOD_CS) {00137 ICMP_DEBUGOUT(<span class="stringliteral">"ERROR: ICMP Checksum failed!\n\r"</span>);00138 <span class="keywordflow">return</span> (-1);00139 }00140 00141 ICMP_DEBUGOUT(<span class="stringliteral">"ICMP Checksum OK\n\r"</span>);00142 00143 <span class="comment">/* Start processing the message */</span>00144 00145 <a class="code" href="system_8h.html#a14">NETWORK_RECEIVE_INITIALIZE</a>(frame-><a class="code" href="structip__frame.html#m11">buf_index</a>);00146 00147 type = <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>();00148 code = <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>();00149 00150 <span class="comment">/* We have already checked the CS, skip it */</span>00151 00152 <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>();00153 <a class="code" href="system_8h.html#a9">RECEIVE_NETWORK_B</a>();00154 00155 <span class="keywordflow">switch</span>(type) {00156 <span class="keywordflow">case</span> ICMP_ECHO_REQUEST:00157 00158 <span class="keywordflow">if</span>(code != 0) {00159 ICMP_DEBUGOUT(<span class="stringliteral">"ERROR:Misformed ICMP ECHO Request\n\r"</span>);00160 <span class="keywordflow">return</span>(-1);00161 }00162 00163 ICMP_DEBUGOUT(<span class="stringliteral">"ICMP ECHO Request received\n\r"</span>);00164 00165 <span class="comment">/* Is it a packet for setting temporary IP? */</span>00166 00167 <span class="keywordflow">if</span>(len == (ICMP_ECHOREQ_HLEN + ICMP_TEMPIPSET_DATALEN) )00168 {00169 <span class="comment">/* Yep, set temporary IP address */</span>00170 00171 ICMP_DEBUGOUT(<span class="stringliteral">"PING with 102 bytes of data, getting temp. IP\r\n"</span>); 00172 localmachine.localip = frame-><a class="code" href="structip__frame.html#m9">dip</a>;00173 localmachine.defgw = frame-><a class="code" href="structip__frame.html#m8">sip</a>;00174 localmachine.netmask = 0;00175 }00176 00177 00178 <span class="comment">/* Same IP? */</span>00179 00180 <span class="keywordflow">if</span>(localmachine.localip != frame-><a class="code" href="structip__frame.html#m9">dip</a>)00181 <span class="keywordflow">return</span>(-1);00182 00183 <span class="comment">/* Reply it */</span>00184 00185 <a class="code" href="system_8h.html#a5">TXBUF</a>[0] = ICMP_ECHO_REPLY;00186 <a class="code" href="system_8h.html#a5">TXBUF</a>[1] = 0;00187 <a class="code" href="system_8h.html#a5">TXBUF</a>[2] = 0;00188 <a class="code" href="system_8h.html#a5">TXBUF</a>[3] = 0;00189 00190 <span class="comment">/* Copy with truncate if needed */</span>00191 00192 <span class="keywordflow">if</span>(len > <a class="code" href="group__opentcp__config.html#a4">NETWORK_TX_BUFFER_SIZE</a>)00193 len = <a class="code" href="group__opentcp__config.html#a4">NETWORK_TX_BUFFER_SIZE</a>;00194 00195 <a class="code" href="system_8h.html#a10">RECEIVE_NETWORK_BUF</a>(&<a class="code" href="system_8h.html#a5">TXBUF</a>[4], len);00196 00197 <span class="comment">/* Calculate Checksum for packet to be sent */</span>00198 00199 checksum = 0;00200 00201 checksum = <a class="code" href="ip_8c.html#a8">ip_checksum_buf</a>(checksum, &<a class="code" href="system_8h.html#a5">TXBUF</a>[0], len);00202 00203 checksum = ~ checksum;00204 00205 <span class="comment">/* Put the checksum on place */</span>00206 00207 <a class="code" href="system_8h.html#a5">TXBUF</a>[2] = (UINT8)(checksum>>8);00208 <a class="code" href="system_8h.html#a5">TXBUF</a>[3] = (UINT8)checksum;00209 00210 <span class="comment">/* Send it */</span>00211 00212 <a class="code" href="ip_8c.html#a4">process_ip_out</a>(frame-><a class="code" href="structip__frame.html#m8">sip</a>, <a class="code" href="ip_8h.html#a1">IP_ICMP</a>, 0, 100, &<a class="code" href="system_8h.html#a5">TXBUF</a>[0], len);00213 00214 ICMP_DEBUGOUT(<span class="stringliteral">"ICMP Reply sent\n\r"</span>);00215 00216 <span class="keywordflow">return</span>(0);00217 00218 00219 <span class="keywordflow">break</span>;00220 00221 <span class="keywordflow">case</span> ICMP_ECHO_REPLY:00222 00223 <span class="keywordflow">break</span>;00224 00225 <span class="keywordflow">default</span>: <span class="comment">/* Unrecognized ICMP message */</span>00226 00227 ICMP_DEBUGOUT(<span class="stringliteral">"Unregognized ICMP message\n\r"</span>);00228 <span class="keywordflow">return</span>(-1); 00229 }00230 00231 }00232 00233 00234 00235 00236 </pre></div><hr><address style="align: right;"><small>Generated on Sun Aug 3 20:32:59 2003 for OpenTCP by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.18 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -