ZealOS/docs/Home/Net/Protocols/ICMP.CC.html
2021-08-17 02:47:37 -04:00

109 lines
8 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V1.00">
<style type="text/css">
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF0>#</span><span class=cF1>define</span><span class=cF0> ICMP_TYPE_ECHO_REPLY </span><span class=cFE>0</span><span class=cF0>
<a name="l2"></a>#</span><span class=cF1>define</span><span class=cF0> ICMP_TYPE_ECHO_REQUEST </span><span class=cFE>8</span><span class=cF0>
<a name="l3"></a>#</span><span class=cF1>define</span><span class=cF0> ICMP_CODE_ECHO </span><span class=cFE>0</span><span class=cF0> </span><span class=cF2>// RFC 792: &quot;Echo or Echo Reply Message&quot;. 0 is the only code explicitly defined for Echo.</span><span class=cF0>
<a name="l4"></a>
<a name="l5"></a></span><span class=cF1>class</span><span class=cF0> CICMPHeader
<a name="l6"></a>{
<a name="l7"></a> </span><span class=cF1>U8</span><span class=cF0> type;
<a name="l8"></a> </span><span class=cF1>U8</span><span class=cF0> code;
<a name="l9"></a> </span><span class=cF9>U16</span><span class=cF0> checksum;
<a name="l10"></a>
<a name="l11"></a> </span><span class=cF9>U16</span><span class=cF0> identifier;
<a name="l12"></a> </span><span class=cF9>U16</span><span class=cF0> sequence_number;
<a name="l13"></a>};
<a name="l14"></a>
<a name="l15"></a>
<a name="l16"></a></span><span class=cF1>U0</span><span class=cF0> ICMPReplySend(</span><span class=cF9>U32</span><span class=cF0> destination_ip_address,
<a name="l17"></a> </span><span class=cF9>U16</span><span class=cF0> identifier,
<a name="l18"></a> </span><span class=cF9>U16</span><span class=cF0> sequence_number,
<a name="l19"></a> </span><span class=cF9>U16</span><span class=cF0> request_checksum,
<a name="l20"></a> </span><span class=cF1>U8</span><span class=cF0> *payload,
<a name="l21"></a> </span><span class=cF9>I64</span><span class=cF0> length)
<a name="l22"></a>{
<a name="l23"></a> </span><span class=cF1>U8</span><span class=cF0> *icmp_frame;
<a name="l24"></a> </span><span class=cF9>I64</span><span class=cF0> de_index;
<a name="l25"></a> CICMPHeader *header;
<a name="l26"></a>
<a name="l27"></a> de_index = IPV4PacketAllocate(&amp;icmp_frame,
<a name="l28"></a> IP_PROTOCOL_ICMP,
<a name="l29"></a> IPV4AddressGet,
<a name="l30"></a> destination_ip_address,
<a name="l31"></a> </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CICMPHeader</span><span class=cF7>)</span><span class=cF0> + length);
<a name="l32"></a> </span><span class=cF1>if</span><span class=cF0> (de_index &lt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l33"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l34"></a> NetErr(</span><span class=cF6>&quot;ICMP SEND REPLY: Failed to allocate IPV4 packet.&quot;</span><span class=cF0>);
<a name="l35"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l36"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l37"></a>
<a name="l38"></a> header = icmp_frame;
<a name="l39"></a>
<a name="l40"></a> header-&gt;type = ICMP_TYPE_ECHO_REPLY;
<a name="l41"></a> header-&gt;code = </span><span class=cFE>0</span><span class=cF0>; </span><span class=cF2>// why is 0 okay?</span><span class=cF0>
<a name="l42"></a> header-&gt;checksum = </span><span class=cF5>EndianU16</span><span class=cF0>(</span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>request_checksum</span><span class=cF7>)</span><span class=cF0> + </span><span class=cFE>0x0800</span><span class=cF0>);
<a name="l43"></a> header-&gt;identifier = identifier;
<a name="l44"></a> header-&gt;sequence_number = sequence_number;
<a name="l45"></a> </span><span class=cF2>// TODO: header checksum is awful. Shrine says hack alert.</span><span class=cF0>
<a name="l46"></a>
<a name="l47"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(icmp_frame + </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CICMPHeader</span><span class=cF7>)</span><span class=cF0>, payload, length);
<a name="l48"></a>
<a name="l49"></a> IPV4PacketFinish(de_index);
<a name="l50"></a>}
<a name="l51"></a>
<a name="l52"></a></span><span class=cF9>I64</span><span class=cF0> ICMPHandler(CIPV4Packet *packet)
<a name="l53"></a>{
<a name="l54"></a> CICMPHeader *header;
<a name="l55"></a>
<a name="l56"></a> </span><span class=cF1>if</span><span class=cF0> (packet-&gt;length &lt; </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CICMPHeader</span><span class=cF7>)</span><span class=cF0>)
<a name="l57"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l58"></a> NetErr(</span><span class=cF6>&quot;ICMP HANDLER: Caught wrong IPV4 length.&quot;</span><span class=cF0>);
<a name="l59"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>;
<a name="l60"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l61"></a>
<a name="l62"></a> header = packet-&gt;data;
<a name="l63"></a>
<a name="l64"></a> </span><span class=cF1>if</span><span class=cF0> (header-&gt;type == ICMP_TYPE_ECHO_REQUEST &amp;&amp; header-&gt;code == ICMP_CODE_ECHO)
<a name="l65"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l66"></a> ARPCachePut(packet-&gt;source_ip_address, packet-&gt;ethernet_frame-&gt;source_address);
<a name="l67"></a>
<a name="l68"></a> ICMPReplySend(packet-&gt;source_ip_address,
<a name="l69"></a> header-&gt;identifier,
<a name="l70"></a> header-&gt;sequence_number,
<a name="l71"></a> header-&gt;checksum,
<a name="l72"></a> packet-&gt;data + </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CICMPHeader</span><span class=cF7>)</span><span class=cF0>, </span><span class=cF2>// Data payload at IPV4Packet data location after the ICMP header</span><span class=cF0>
<a name="l73"></a> packet-&gt;length - </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>CICMPHeader</span><span class=cF7>)</span><span class=cF0>);</span><span class=cF2>// Payload length is size of packet after dropping header.</span><span class=cF0>
<a name="l74"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l75"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l76"></a> NetWarn(</span><span class=cF6>&quot;ICMP HANDLER: Unhandled ICMP packet. type, code: 0x%X, 0x%X&quot;</span><span class=cF0>, header-&gt;type, header-&gt;code);
<a name="l77"></a>
<a name="l78"></a> NetLog(</span><span class=cF6>&quot;ICMP HANDLER: Exiting.&quot;</span><span class=cF0>);
<a name="l79"></a>
<a name="l80"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cFE>0</span><span class=cF0>;
<a name="l81"></a>}</span></pre></body>
</html>