ZealOS/docs/Home/Net/Utilities/Ping.ZC.html
2021-12-30 22:21:33 -05:00

108 lines
9.2 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.08">
<style type="text/css">
body {background-color:#1f1f1f;}
.cF0{color:#e3e3e3;background-color:#1f1f1f;}
.cF1{color:#4f84a6;background-color:#1f1f1f;}
.cF2{color:#73a255;background-color:#1f1f1f;}
.cF3{color:#297582;background-color:#1f1f1f;}
.cF4{color:#b34f4b;background-color:#1f1f1f;}
.cF5{color:#8a52c3;background-color:#1f1f1f;}
.cF6{color:#b7822f;background-color:#1f1f1f;}
.cF7{color:#444444;background-color:#1f1f1f;}
.cF8{color:#6d6d6d;background-color:#1f1f1f;}
.cF9{color:#94bfde;background-color:#1f1f1f;}
.cFA{color:#a1ce97;background-color:#1f1f1f;}
.cFB{color:#6db4be;background-color:#1f1f1f;}
.cFC{color:#e88e88;background-color:#1f1f1f;}
.cFD{color:#ca94e8;background-color:#1f1f1f;}
.cFE{color:#d4b475;background-color:#1f1f1f;}
.cFF{color:#1f1f1f;background-color:#1f1f1f;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF9>I64</span><span class=cF0> Ping(</span><span class=cF1>U8</span><span class=cF0> *hostname, </span><span class=cF9>I64</span><span class=cF0> timeout=</span><span class=cF3>JIFFY_FREQ</span><span class=cF0>)
<a name="l2"></a>{
<a name="l3"></a> </span><span class=cF9>U32</span><span class=cF0> addr;
<a name="l4"></a> CAddressInfo *current;
<a name="l5"></a> CAddressInfo *result = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l6"></a> </span><span class=cF9>I64</span><span class=cF0> error;
<a name="l7"></a> </span><span class=cF9>I64</span><span class=cF0> i = </span><span class=cFE>0</span><span class=cF0>, t, delay, sent = </span><span class=cFE>0</span><span class=cF0>, min_delay = </span><span class=cF3>I64_MAX</span><span class=cF0>, max_delay = </span><span class=cF3>I64_MIN</span><span class=cF0>, sum_delay = </span><span class=cFE>0</span><span class=cF0>;
<a name="l8"></a> CSocketAddressIPV4 *ipv4_address;
<a name="l9"></a> </span><span class=cF9>U16</span><span class=cF0> sequence_number = </span><span class=cFE>1</span><span class=cF0>, identifier = </span><span class=cF5>RandU16</span><span class=cF0>, count = </span><span class=cFE>0</span><span class=cF0>;
<a name="l10"></a> </span><span class=cF1>U8</span><span class=cF0> *payload;
<a name="l11"></a>
<a name="l12"></a> </span><span class=cF1>if</span><span class=cF0> (!IPV4AddressParse</span><span class=cF7>(</span><span class=cF0>hostname, &amp;addr</span><span class=cF7>)</span><span class=cF0>)
<a name="l13"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l14"></a> error = DNSAddressInfoGet(hostname, </span><span class=cF3>NULL</span><span class=cF0>, &amp;result);
<a name="l15"></a> </span><span class=cF1>if</span><span class=cF0> (error &lt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l16"></a> {
<a name="l17"></a> NetErr(</span><span class=cF6>&quot;Ping: Failed at DNS Get Address Info.&quot;</span><span class=cF0>);
<a name="l18"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>;
<a name="l19"></a> }
<a name="l20"></a> current = result;
<a name="l21"></a> </span><span class=cF1>while</span><span class=cF0> (current)
<a name="l22"></a> {
<a name="l23"></a> </span><span class=cF1>if</span><span class=cF0> (current-&gt;family == AF_INET)
<a name="l24"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l25"></a> ipv4_address = current-&gt;address;
<a name="l26"></a> addr = </span><span class=cF5>EndianU32</span><span class=cF0>(ipv4_address-&gt;address); </span><span class=cF2>// why does it need EndianU32</span><span class=cF0>
<a name="l27"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l28"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l29"></a> current = current-&gt;next;
<a name="l30"></a> }
<a name="l31"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l32"></a>
<a name="l33"></a> </span><span class=cF1>if</span><span class=cF0> (!current)
<a name="l34"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l35"></a> NetErr(</span><span class=cF6>&quot;Ping: Failed to resolve address.&quot;</span><span class=cF0>);
<a name="l36"></a> </span><span class=cF1>return</span><span class=cF0> -</span><span class=cFE>1</span><span class=cF0>;
<a name="l37"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l38"></a>
<a name="l39"></a> </span><span class=cF6>&quot;\n&quot;</span><span class=cF0>;
<a name="l40"></a> </span><span class=cF1>while</span><span class=cF0> (!</span><span class=cF5>CharScan</span><span class=cF0>)
<a name="l41"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l42"></a> payload = </span><span class=cF5>MAlloc</span><span class=cF0>(</span><span class=cFE>64</span><span class=cF0>);
<a name="l43"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; </span><span class=cFE>64</span><span class=cF0>; i++)
<a name="l44"></a> payload[i] = </span><span class=cF5>RandU8</span><span class=cF0>;
<a name="l45"></a>
<a name="l46"></a> </span><span class=cF6>&quot;Sending Ping request #%d\n&quot;</span><span class=cF0>, sequence_number;
<a name="l47"></a> ICMPRequestSend(addr, identifier, </span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>sequence_number</span><span class=cF7>)</span><span class=cF0>, payload, </span><span class=cFE>64</span><span class=cF0>);
<a name="l48"></a> sent++;
<a name="l49"></a>
<a name="l50"></a> t = </span><span class=cFB>counts</span><span class=cF0>.jiffies;
<a name="l51"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cFB>counts</span><span class=cF0>.jiffies &lt; t + timeout)
<a name="l52"></a> {
<a name="l53"></a> </span><span class=cF1>if</span><span class=cF0> (icmp_reply.identifier == identifier &amp;&amp; icmp_reply.sequence_number == </span><span class=cF5>EndianU16</span><span class=cF7>(</span><span class=cF0>sequence_number</span><span class=cF7>)</span><span class=cF0>)
<a name="l54"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l55"></a> delay = </span><span class=cFB>counts</span><span class=cF0>.jiffies - t;
<a name="l56"></a> min_delay = </span><span class=cF5>MinI64</span><span class=cF0>(min_delay, delay);
<a name="l57"></a> max_delay = </span><span class=cF5>MaxI64</span><span class=cF0>(max_delay, delay);
<a name="l58"></a> sum_delay += delay; </span><span class=cF2>// sum up delays, divide through by count during stat report to get average</span><span class=cF0>
<a name="l59"></a> count++;
<a name="l60"></a> </span><span class=cF6>&quot;\tReceived reply, delay: %dms\n&quot;</span><span class=cF0>, delay;
<a name="l61"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l62"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l63"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>1</span><span class=cF0>);
<a name="l64"></a> }
<a name="l65"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cFB>counts</span><span class=cF0>.jiffies &lt; t + </span><span class=cF3>JIFFY_FREQ</span><span class=cF0>)
<a name="l66"></a> </span><span class=cF5>Sleep</span><span class=cF0>(</span><span class=cFE>1</span><span class=cF0>);
<a name="l67"></a>
<a name="l68"></a> sequence_number++;
<a name="l69"></a> </span><span class=cF5>Free</span><span class=cF0>(payload);
<a name="l70"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l71"></a>
<a name="l72"></a> </span><span class=cF6>&quot;\nPing Statistics:\n&quot;</span><span class=cF0>;
<a name="l73"></a> </span><span class=cF6>&quot;\tSent: %d, Received: %d, Lost: %d (%0.02f %%)\n&quot;</span><span class=cF0>, sent, count, sent - count, </span><span class=cFE>100</span><span class=cF0>.</span><span class=cFE>0</span><span class=cF0> - </span><span class=cFE>100</span><span class=cF0>.</span><span class=cFE>0</span><span class=cF0> * count / sent;
<a name="l74"></a> </span><span class=cF1>if</span><span class=cF0> (min_delay != </span><span class=cF3>I64_MAX</span><span class=cF0> &amp;&amp; max_delay != </span><span class=cF3>I64_MIN</span><span class=cF0> &amp;&amp; sum_delay != </span><span class=cFE>0</span><span class=cF0>)
<a name="l75"></a> </span><span class=cF6>&quot;\tMin: %dms, Max: %dms, Avg: %dms\n&quot;</span><span class=cF0>, min_delay, max_delay, sum_delay / count;
<a name="l76"></a> </span><span class=cF6>&quot;\n&quot;</span><span class=cF0>;
<a name="l77"></a>
<a name="l78"></a> </span><span class=cF1>return</span><span class=cF0> count;
<a name="l79"></a>
<a name="l80"></a>}</span></pre></body>
</html>