<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>UbixOS V2: src/sys/net/netif/arp.c Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.4.7 --> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="classes.html"><span>Data Structures</span></a></li> <li id="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> <li> <form action="search.php" method="get"> <table cellspacing="0" cellpadding="0" border="0"> <tr> <td><label> <u>S</u>earch for </label></td> <td><input type="text" name="query" value="" size="20" accesskey="s"/></td> </tr> </table> </form> </li> </ul></div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul></div> <div class="nav"> <a class="el" href="dir_897b6a2d7bab147dd1db58381aad3984.html">src</a> » <a class="el" href="dir_832905b1f7f5feaf61a306b40c0ac817.html">sys</a> » <a class="el" href="dir_da977d215fccd664f66e7711fda26f76.html">net</a> » <a class="el" href="dir_becc64c1c5cdfab2e137aa1ca4b91e6b.html">netif</a></div> <h1>arp.c</h1><a href="arp_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * Copyright (c) 2001, Swedish Institute of Computer Science.</span> <a name="l00003"></a>00003 <span class="comment"> * All rights reserved. </span> <a name="l00004"></a>00004 <span class="comment"> *</span> <a name="l00005"></a>00005 <span class="comment"> * Redistribution and use in source and binary forms, with or without </span> <a name="l00006"></a>00006 <span class="comment"> * modification, are permitted provided that the following conditions </span> <a name="l00007"></a>00007 <span class="comment"> * are met: </span> <a name="l00008"></a>00008 <span class="comment"> * 1. Redistributions of source code must retain the above copyright </span> <a name="l00009"></a>00009 <span class="comment"> * notice, this list of conditions and the following disclaimer. </span> <a name="l00010"></a>00010 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright </span> <a name="l00011"></a>00011 <span class="comment"> * notice, this list of conditions and the following disclaimer in the </span> <a name="l00012"></a>00012 <span class="comment"> * documentation and/or other materials provided with the distribution. </span> <a name="l00013"></a>00013 <span class="comment"> * 3. Neither the name of the Institute nor the names of its contributors </span> <a name="l00014"></a>00014 <span class="comment"> * may be used to endorse or promote products derived from this software </span> <a name="l00015"></a>00015 <span class="comment"> * without specific prior written permission. </span> <a name="l00016"></a>00016 <span class="comment"> *</span> <a name="l00017"></a>00017 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND </span> <a name="l00018"></a>00018 <span class="comment"> * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE </span> <a name="l00019"></a>00019 <span class="comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE </span> <a name="l00020"></a>00020 <span class="comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE </span> <a name="l00021"></a>00021 <span class="comment"> * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL </span> <a name="l00022"></a>00022 <span class="comment"> * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS </span> <a name="l00023"></a>00023 <span class="comment"> * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) </span> <a name="l00024"></a>00024 <span class="comment"> * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT </span> <a name="l00025"></a>00025 <span class="comment"> * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY </span> <a name="l00026"></a>00026 <span class="comment"> * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF </span> <a name="l00027"></a>00027 <span class="comment"> * SUCH DAMAGE. </span> <a name="l00028"></a>00028 <span class="comment"> *</span> <a name="l00029"></a>00029 <span class="comment"> * This file is part of the lwIP TCP/IP stack.</span> <a name="l00030"></a>00030 <span class="comment"> * </span> <a name="l00031"></a>00031 <span class="comment"> * Author: Adam Dunkels <adam@sics.se></span> <a name="l00032"></a>00032 <span class="comment"> *</span> <a name="l00033"></a>00033 <span class="comment"> * $Id$</span> <a name="l00034"></a>00034 <span class="comment"> *</span> <a name="l00035"></a>00035 <span class="comment"> */</span> <a name="l00036"></a>00036 <a name="l00037"></a>00037 <span class="preprocessor">#include <<a class="code" href="types_8h.html">ubixos/types.h</a>></span> <a name="l00038"></a>00038 <span class="preprocessor">#include <<a class="code" href="kprintf_8h.html">lib/kprintf.h</a>></span> <a name="l00039"></a>00039 <a name="l00040"></a>00040 <span class="preprocessor">#include "<a class="code" href="debug_8h.html">net/debug.h</a>"</span> <a name="l00041"></a>00041 <span class="preprocessor">#include "<a class="code" href="ipv4_2inet_8h.html">net/ipv4/inet.h</a>"</span> <a name="l00042"></a>00042 <span class="preprocessor">#include "<a class="code" href="arp_8h.html">netif/arp.h</a>"</span> <a name="l00043"></a>00043 <span class="preprocessor">#include "<a class="code" href="ipv4_2ip_8h.html">net/ipv4/ip.h</a>"</span> <a name="l00044"></a>00044 <a name="l00045"></a>00045 <a name="l00046"></a><a class="code" href="arp_8c.html#741a0710dc126ed3ae9e305472df3432">00046</a> <span class="preprocessor">#define ARP_MAXAGE 2 </span><span class="comment">/* 120 * 10 seconds = 20 minutes. */</span> <a name="l00047"></a>00047 <a name="l00048"></a><a class="code" href="arp_8c.html#28a28d3e575df3e75985e2313cc2d51a">00048</a> <span class="preprocessor">#define HWTYPE_ETHERNET 1</span> <a name="l00049"></a>00049 <span class="preprocessor"></span> <a name="l00050"></a><a class="code" href="arp_8c.html#7a7c46ffaba30477b8c9e3e61bd2e106">00050</a> <span class="preprocessor">#define ARP_REQUEST 1</span> <a name="l00051"></a><a class="code" href="arp_8c.html#06ba7b414e718081998f2814090debf1">00051</a> <span class="preprocessor"></span><span class="preprocessor">#define ARP_REPLY 2</span> <a name="l00052"></a>00052 <span class="preprocessor"></span> <a name="l00053"></a>00053 <span class="comment">/* MUST be compiled with "pack structs" or equivalent! */</span> <a name="l00054"></a>00054 <a class="code" href="cc_8h.html#73199061891adf1b912d20835c7d5e96">PACK_STRUCT_BEGIN</a> <a name="l00055"></a><a class="code" href="structarp__hdr.html">00055</a> <span class="keyword">struct </span><a class="code" href="structarp__hdr.html">arp_hdr</a> { <a name="l00056"></a>00056 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<span class="keyword">struct</span> <a class="code" href="structeth__hdr.html">eth_hdr</a> ethhdr); <a name="l00057"></a>00057 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<a class="code" href="types_8h.html#3b65128d2644e9b80cec9a69bfa7e094">uInt16</a> hwtype); <a name="l00058"></a>00058 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<a class="code" href="types_8h.html#3b65128d2644e9b80cec9a69bfa7e094">uInt16</a> proto); <a name="l00059"></a>00059 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<a class="code" href="types_8h.html#3b65128d2644e9b80cec9a69bfa7e094">uInt16</a> _hwlen_protolen); <a name="l00060"></a>00060 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<a class="code" href="types_8h.html#3b65128d2644e9b80cec9a69bfa7e094">uInt16</a> opcode); <a name="l00061"></a>00061 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<span class="keyword">struct</span> <a class="code" href="structeth__addr.html">eth_addr</a> shwaddr); <a name="l00062"></a>00062 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<span class="keyword">struct</span> <a class="code" href="structip__addr.html">ip_addr</a> sipaddr); <a name="l00063"></a>00063 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<span class="keyword">struct</span> <a class="code" href="structeth__addr.html">eth_addr</a> dhwaddr); <a name="l00064"></a>00064 <a class="code" href="structarp__hdr.html#bf562b78f962f27cb93c538f42c6fe79">PACK_STRUCT_FIELD</a>(<span class="keyword">struct</span> <a class="code" href="structip__addr.html">ip_addr</a> dipaddr); <a name="l00065"></a>00065 } <a class="code" href="cc_8h.html#33623da05bb55497534c3dc26ebebeb2">PACK_STRUCT_STRUCT</a>; <a name="l00066"></a>00066 <a class="code" href="cc_8h.html#465fef70f294e21cbf4ea51fc342f20e">PACK_STRUCT_END</a> <a name="l00067"></a>00067 <a name="l00068"></a><a class="code" href="arp_8c.html#06faf2eae13476a702524b13ea6ecd7f">00068</a> <span class="preprocessor">#define ARPH_HWLEN(hdr) (NTOHS((hdr)->_hwlen_protolen) >> 8)</span> <a name="l00069"></a><a class="code" href="arp_8c.html#8c416438814934662a28d7e3c891a175">00069</a> <span class="preprocessor"></span><span class="preprocessor">#define ARPH_PROTOLEN(hdr) (NTOHS((hdr)->_hwlen_protolen) & 0xff)</span> <a name="l00070"></a>00070 <span class="preprocessor"></span> <a name="l00071"></a>00071 <a name="l00072"></a><a class="code" href="arp_8c.html#f463dc7a7aaf3ea0b6bc472b261380ac">00072</a> <span class="preprocessor">#define ARPH_HWLEN_SET(hdr, len) (hdr)->_hwlen_protolen = HTONS(ARPH_PROTOLEN(hdr) | ((len) << 8))</span> <a name="l00073"></a><a class="code" href="arp_8c.html#edb2ded9225b09da0358754f859d72d6">00073</a> <span class="preprocessor"></span><span class="preprocessor">#define ARPH_PROTOLEN_SET(hdr, len) (hdr)->_hwlen_protolen = HTONS((len) | (ARPH_HWLEN(hdr) << 8))</span> <a name="l00074"></a>00074 <span class="preprocessor"></span> <a name="l00075"></a>00075 <a class="code" href="cc_8h.html#73199061891adf1b912d20835c7d5e96">PACK_STRUCT_BEGIN</a> <a name="l00076"></a><a class="code" href="structethip__hdr.html">00076</a> <span class="keyword">struct </span><a class="code" href="structethip__hdr.html">ethip_hdr</a> { <a name="l00077"></a>00077 <a class="code" href="structethip__hdr.html#eaff5bdb00c5618ea94cc70081ba27d9">PACK_STRUCT_FIELD</a>(<span class="keyword">struct</span> <a class="code" href="structeth__hdr.html">eth_hdr</a> eth); <a name="l00078"></a>00078 <a class="code" href="structethip__hdr.html#eaff5bdb00c5618ea94cc70081ba27d9">PACK_STRUCT_FIELD</a>(<span class="keyword">struct</span> <a class="code" href="structip__hdr.html">ip_hdr</a> ip); <a name="l00079"></a>00079 }; <a name="l00080"></a>00080 <a class="code" href="cc_8h.html#465fef70f294e21cbf4ea51fc342f20e">PACK_STRUCT_END</a> <a name="l00081"></a>00081 <a name="l00082"></a><a class="code" href="structarp__entry.html">00082</a> <span class="keyword">struct </span><a class="code" href="structarp__entry.html">arp_entry</a> { <a name="l00083"></a><a class="code" href="structarp__entry.html#b9ee1643a380bd563aac0dea83973459">00083</a> <span class="keyword">struct </span><a class="code" href="structip__addr.html">ip_addr</a> <a class="code" href="structarp__entry.html#b9ee1643a380bd563aac0dea83973459">ipaddr</a>; <a name="l00084"></a><a class="code" href="structarp__entry.html#4cd78dd42071f584f2b8a5ead9f70eaf">00084</a> <span class="keyword">struct </span><a class="code" href="structeth__addr.html">eth_addr</a> <a class="code" href="structarp__entry.html#4cd78dd42071f584f2b8a5ead9f70eaf">ethaddr</a>; <a name="l00085"></a><a class="code" href="structarp__entry.html#49945e8299d4bf7241c0fd2b5e4d84df">00085</a> <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> <a class="code" href="structarp__entry.html#49945e8299d4bf7241c0fd2b5e4d84df">ctime</a>; <a name="l00086"></a>00086 }; <a name="l00087"></a>00087 <a name="l00088"></a><a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">00088</a> <span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="structarp__entry.html">arp_entry</a> <a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[<a class="code" href="lwipopts_8h.html#924936a814564dbdb0bc950d255a83b9">ARP_TABLE_SIZE</a>]; <a name="l00089"></a><a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">00089</a> <span class="keyword">static</span> <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> <a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a>; <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="comment">/*-----------------------------------------------------------------------------------*/</span> <a name="l00092"></a>00092 <span class="keywordtype">void</span> <a name="l00093"></a><a class="code" href="arp_8c.html#c8ad5a32790d172fcec2eac97a87ee9f">00093</a> <a class="code" href="arp_8h.html#c8ad5a32790d172fcec2eac97a87ee9f">arp_init</a>(<span class="keywordtype">void</span>) <a name="l00094"></a>00094 { <a name="l00095"></a>00095 <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> i; <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="keywordflow">for</span>(i = 0; i < <a class="code" href="lwipopts_8h.html#924936a814564dbdb0bc950d255a83b9">ARP_TABLE_SIZE</a>; ++i) { <a name="l00098"></a>00098 <a class="code" href="ipv4_2ip__addr_8h.html#40db7feae2b92f7a7a01d65a85b06e18">ip_addr_set</a>(&(<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#b9ee1643a380bd563aac0dea83973459">ipaddr</a>), <a name="l00099"></a>00099 <a class="code" href="ipv4_2ip__addr_8h.html#3e5e67b7292b156034560fef2202776c">IP_ADDR_ANY</a>); <a name="l00100"></a>00100 } <a name="l00101"></a>00101 } <a name="l00102"></a>00102 <span class="comment">/*-----------------------------------------------------------------------------------*/</span> <a name="l00103"></a>00103 <span class="keywordtype">void</span> <a name="l00104"></a><a class="code" href="arp_8c.html#2cc298ed4c68289160da1376faf20938">00104</a> <a class="code" href="arp_8h.html#2cc298ed4c68289160da1376faf20938">arp_tmr</a>(<span class="keywordtype">void</span>) <a name="l00105"></a>00105 { <a name="l00106"></a>00106 <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> i; <a name="l00107"></a>00107 <a name="l00108"></a>00108 ++<a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a>; <a name="l00109"></a>00109 <span class="keywordflow">for</span>(i = 0; i < <a class="code" href="lwipopts_8h.html#924936a814564dbdb0bc950d255a83b9">ARP_TABLE_SIZE</a>; ++i) { <a name="l00110"></a>00110 <span class="keywordflow">if</span>(!<a class="code" href="ipv4_2ip__addr_8h.html#0d51c2bb5f2af51392540af1c5df511a">ip_addr_isany</a>(&<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#b9ee1643a380bd563aac0dea83973459">ipaddr</a>) && <a name="l00111"></a>00111 <a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a> - <a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a> >= <a class="code" href="arp_8c.html#741a0710dc126ed3ae9e305472df3432">ARP_MAXAGE</a>) { <a name="l00112"></a>00112 <a class="code" href="debug_8h.html#8b52c1fca5671ffbfe068c8d50ca0065">DEBUGF</a>(<a class="code" href="debug_8h.html#7fa8be0d28d77b4ab4f44d924d4d69e2">ARP_DEBUG</a>, (<span class="stringliteral">"arp_timer: expired entry %d.\n"</span>, i)); <a name="l00113"></a>00113 <a class="code" href="ipv4_2ip__addr_8h.html#40db7feae2b92f7a7a01d65a85b06e18">ip_addr_set</a>(&(<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].ipaddr), <a name="l00114"></a>00114 <a class="code" href="ipv4_2ip__addr_8h.html#3e5e67b7292b156034560fef2202776c">IP_ADDR_ANY</a>); <a name="l00115"></a>00115 } <a name="l00116"></a>00116 } <a name="l00117"></a>00117 } <a name="l00118"></a>00118 <span class="comment">/*-----------------------------------------------------------------------------------*/</span> <a name="l00119"></a>00119 <span class="keyword">static</span> <span class="keywordtype">void</span> <a name="l00120"></a><a class="code" href="arp_8c.html#038bb8fc3bcc993d8b7724632e43ed0f">00120</a> <a class="code" href="arp_8c.html#038bb8fc3bcc993d8b7724632e43ed0f">add_arp_entry</a>(<span class="keyword">struct</span> <a class="code" href="structip__addr.html">ip_addr</a> *<a class="code" href="structarp__entry.html#b9ee1643a380bd563aac0dea83973459">ipaddr</a>, <span class="keyword">struct</span> <a class="code" href="structeth__addr.html">eth_addr</a> *<a class="code" href="structarp__entry.html#4cd78dd42071f584f2b8a5ead9f70eaf">ethaddr</a>) <a name="l00121"></a>00121 { <a name="l00122"></a>00122 <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> i, j, k; <a name="l00123"></a>00123 <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> maxtime; <a name="l00124"></a>00124 <a name="l00125"></a>00125 <span class="comment">/* Walk through the ARP mapping table and try to find an entry to</span> <a name="l00126"></a>00126 <span class="comment"> update. If none is found, the IP -> MAC address mapping is</span> <a name="l00127"></a>00127 <span class="comment"> inserted in the ARP table. */</span> <a name="l00128"></a>00128 <span class="keywordflow">for</span>(i = 0; i < <a class="code" href="lwipopts_8h.html#924936a814564dbdb0bc950d255a83b9">ARP_TABLE_SIZE</a>; ++i) { <a name="l00129"></a>00129 <a name="l00130"></a>00130 <span class="comment">/* Only check those entries that are actually in use. */</span> <a name="l00131"></a>00131 <span class="keywordflow">if</span>(!<a class="code" href="ipv4_2ip__addr_8h.html#0d51c2bb5f2af51392540af1c5df511a">ip_addr_isany</a>(&<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#b9ee1643a380bd563aac0dea83973459">ipaddr</a>)) { <a name="l00132"></a>00132 <span class="comment">/* Check if the source IP address of the incoming packet matches</span> <a name="l00133"></a>00133 <span class="comment"> the IP address in this ARP table entry. */</span> <a name="l00134"></a>00134 <span class="keywordflow">if</span>(<a class="code" href="ipv4_2ip__addr_8h.html#008c9c92d770cc030ea054e6b259835a">ip_addr_cmp</a>(ipaddr, &<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].ipaddr)) { <a name="l00135"></a>00135 <span class="comment">/* An old entry found, update this and return. */</span> <a name="l00136"></a>00136 <span class="keywordflow">for</span>(k = 0; k < 6; ++k) { <a name="l00137"></a>00137 <a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#4cd78dd42071f584f2b8a5ead9f70eaf">ethaddr</a>.addr[k] = <a class="code" href="structarp__entry.html#4cd78dd42071f584f2b8a5ead9f70eaf">ethaddr</a>->addr[k]; <a name="l00138"></a>00138 } <a name="l00139"></a>00139 <a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#49945e8299d4bf7241c0fd2b5e4d84df">ctime</a> = <a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a>; <a name="l00140"></a>00140 <span class="keywordflow">return</span>; <a name="l00141"></a>00141 } <a name="l00142"></a>00142 } <a name="l00143"></a>00143 } <a name="l00144"></a>00144 <a name="l00145"></a>00145 <span class="comment">/* If we get here, no existing ARP table entry was found, so we</span> <a name="l00146"></a>00146 <span class="comment"> create one. */</span> <a name="l00147"></a>00147 <a name="l00148"></a>00148 <span class="comment">/* First, we try to find an unused entry in the ARP table. */</span> <a name="l00149"></a>00149 <span class="keywordflow">for</span>(i = 0; i < ARP_TABLE_SIZE; ++i) { <a name="l00150"></a>00150 <span class="keywordflow">if</span>(<a class="code" href="ipv4_2ip__addr_8h.html#0d51c2bb5f2af51392540af1c5df511a">ip_addr_isany</a>(&<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#b9ee1643a380bd563aac0dea83973459">ipaddr</a>)) { <a name="l00151"></a>00151 <span class="keywordflow">break</span>; <a name="l00152"></a>00152 } <a name="l00153"></a>00153 } <a name="l00154"></a>00154 <a name="l00155"></a>00155 <span class="comment">/* If no unused entry is found, we try to find the oldest entry and</span> <a name="l00156"></a>00156 <span class="comment"> throw it away. */</span> <a name="l00157"></a>00157 <span class="keywordflow">if</span>(i == ARP_TABLE_SIZE) { <a name="l00158"></a>00158 maxtime = 0; <a name="l00159"></a>00159 j = 0; <a name="l00160"></a>00160 <span class="keywordflow">for</span>(i = 0; i < ARP_TABLE_SIZE; ++i) { <a name="l00161"></a>00161 <span class="keywordflow">if</span>(<a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a> - <a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a> > maxtime) { <a name="l00162"></a>00162 maxtime = <a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a> - <a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a>; <a name="l00163"></a>00163 j = i; <a name="l00164"></a>00164 } <a name="l00165"></a>00165 } <a name="l00166"></a>00166 i = j; <a name="l00167"></a>00167 } <a name="l00168"></a>00168 <a name="l00169"></a>00169 <span class="comment">/* Now, i is the ARP table entry which we will fill with the new</span> <a name="l00170"></a>00170 <span class="comment"> information. */</span> <a name="l00171"></a>00171 <a class="code" href="ipv4_2ip__addr_8h.html#40db7feae2b92f7a7a01d65a85b06e18">ip_addr_set</a>(&<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#b9ee1643a380bd563aac0dea83973459">ipaddr</a>, ipaddr); <a name="l00172"></a>00172 <span class="keywordflow">for</span>(k = 0; k < 6; ++k) { <a name="l00173"></a>00173 <a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#4cd78dd42071f584f2b8a5ead9f70eaf">ethaddr</a>.addr[k] = <a class="code" href="structarp__entry.html#4cd78dd42071f584f2b8a5ead9f70eaf">ethaddr</a>->addr[k]; <a name="l00174"></a>00174 } <a name="l00175"></a>00175 <a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].<a class="code" href="structarp__entry.html#49945e8299d4bf7241c0fd2b5e4d84df">ctime</a> = <a class="code" href="arp_8c.html#9b78800fdadcc20482aacd10da639621">ctime</a>; <a name="l00176"></a>00176 <span class="keywordflow">return</span>; <a name="l00177"></a>00177 <a name="l00178"></a>00178 } <a name="l00179"></a>00179 <span class="comment">/*-----------------------------------------------------------------------------------*/</span> <a name="l00180"></a>00180 <span class="keywordtype">void</span> <a name="l00181"></a><a class="code" href="arp_8c.html#916d08dbb8f4a38e1e904a8a018b000c">00181</a> <a class="code" href="arp_8h.html#916d08dbb8f4a38e1e904a8a018b000c">arp_ip_input</a>(<span class="keyword">struct</span> <a class="code" href="structnetif.html">netif</a> *<a class="code" href="structnetif.html">netif</a>, <span class="keyword">struct</span> <a class="code" href="structpbuf.html">pbuf</a> *p) <a name="l00182"></a>00182 { <a name="l00183"></a>00183 <span class="keyword">struct </span><a class="code" href="structethip__hdr.html">ethip_hdr</a> *hdr; <a name="l00184"></a>00184 <a name="l00185"></a>00185 hdr = p-><a class="code" href="structpbuf.html#8d32dc3e964369d4eec638fc37fbc460">payload</a>; <a name="l00186"></a>00186 <a name="l00187"></a>00187 <span class="comment">/* Only insert/update an entry if the source IP address of the</span> <a name="l00188"></a>00188 <span class="comment"> incoming IP packet comes from a host on the local network. */</span> <a name="l00189"></a>00189 <span class="keywordflow">if</span>(!<a class="code" href="ipv4_2ip__addr_8h.html#f656f30425e386d96d0e1a1b2dcb3952">ip_addr_maskcmp</a>(&(hdr->ip.src), &(netif-><a class="code" href="structnetif.html#d666097c02e7d7e82ea838b0c721470d">ip_addr</a>), &(netif-><a class="code" href="structnetif.html#c4b9556c67758bde9a2b29f57f3e8cf2">netmask</a>))) { <a name="l00190"></a>00190 <span class="keywordflow">return</span>; <a name="l00191"></a>00191 } <a name="l00192"></a>00192 <a class="code" href="debug_8h.html#8b52c1fca5671ffbfe068c8d50ca0065">DEBUGF</a>(<a class="code" href="debug_8h.html#7fa8be0d28d77b4ab4f44d924d4d69e2">ARP_DEBUG</a>, (<span class="stringliteral">"arp_ip_input: updating ARP table.\n"</span>)); <a name="l00193"></a>00193 <a class="code" href="arp_8c.html#038bb8fc3bcc993d8b7724632e43ed0f">add_arp_entry</a>(&(hdr->ip.src), &(hdr->eth.src)); <a name="l00194"></a>00194 } <a name="l00195"></a>00195 <span class="comment">/*-----------------------------------------------------------------------------------*/</span> <a name="l00196"></a>00196 <span class="keyword">struct </span><a class="code" href="structpbuf.html">pbuf</a> * <a name="l00197"></a><a class="code" href="arp_8c.html#57a25dd51cf73f048ad5364ecc2f040b">00197</a> <a class="code" href="arp_8h.html#57a25dd51cf73f048ad5364ecc2f040b">arp_arp_input</a>(<span class="keyword">struct</span> <a class="code" href="structnetif.html">netif</a> *<a class="code" href="structnetif.html">netif</a>, <span class="keyword">struct</span> <a class="code" href="structeth__addr.html">eth_addr</a> *ethaddr, <span class="keyword">struct</span> <a class="code" href="structpbuf.html">pbuf</a> *p) <a name="l00198"></a>00198 { <a name="l00199"></a>00199 <span class="keyword">struct </span><a class="code" href="structarp__hdr.html">arp_hdr</a> *hdr; <a name="l00200"></a>00200 <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> i; <a name="l00201"></a>00201 <a name="l00202"></a>00202 <span class="keywordflow">if</span>(p-><a class="code" href="structpbuf.html#3b42932c0b26c9ff2b8fe88bd25fb127">tot_len</a> < <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structarp__hdr.html">arp_hdr</a>)) { <a name="l00203"></a>00203 <a class="code" href="kprint_8h.html#b2761bdf0cca73ad0fb5880895210cd8">kprintf</a>(<span class="stringliteral">"arp_arp_input: packet too short (%d/%d)\n"</span>, p-><a class="code" href="structpbuf.html#3b42932c0b26c9ff2b8fe88bd25fb127">tot_len</a>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> arp_hdr)); <a name="l00204"></a>00204 <a class="code" href="pbuf_8h.html#334112aa2ac402c254d604c0dbbbb746">pbuf_free</a>(p); <a name="l00205"></a>00205 <span class="keywordflow">return</span> <a class="code" href="def_8h.html#070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>; <a name="l00206"></a>00206 } <a name="l00207"></a>00207 <a name="l00208"></a>00208 hdr = p-><a class="code" href="structpbuf.html#8d32dc3e964369d4eec638fc37fbc460">payload</a>; <a name="l00209"></a>00209 <a name="l00210"></a>00210 <span class="keywordflow">switch</span>(<a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(hdr->opcode)) { <a name="l00211"></a>00211 <span class="keywordflow">case</span> <a class="code" href="arp_8c.html#7a7c46ffaba30477b8c9e3e61bd2e106">ARP_REQUEST</a>: <a name="l00212"></a>00212 <span class="comment">/* ARP request. If it asked for our address, we send out a</span> <a name="l00213"></a>00213 <span class="comment"> reply. */</span> <a name="l00214"></a>00214 <span class="keywordflow">if</span>(<a class="code" href="ipv4_2ip__addr_8h.html#008c9c92d770cc030ea054e6b259835a">ip_addr_cmp</a>(&(hdr->dipaddr), &(netif-><a class="code" href="structnetif.html#d666097c02e7d7e82ea838b0c721470d">ip_addr</a>))) { <a name="l00215"></a>00215 hdr->opcode = <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(<a class="code" href="arp_8c.html#06ba7b414e718081998f2814090debf1">ARP_REPLY</a>); <a name="l00216"></a>00216 <a name="l00217"></a>00217 <a class="code" href="ipv4_2ip__addr_8h.html#40db7feae2b92f7a7a01d65a85b06e18">ip_addr_set</a>(&(hdr->dipaddr), &(hdr->sipaddr)); <a name="l00218"></a>00218 <a class="code" href="ipv4_2ip__addr_8h.html#40db7feae2b92f7a7a01d65a85b06e18">ip_addr_set</a>(&(hdr->sipaddr), &(netif-><a class="code" href="structnetif.html#d666097c02e7d7e82ea838b0c721470d">ip_addr</a>)); <a name="l00219"></a>00219 <a name="l00220"></a>00220 <span class="keywordflow">for</span>(i = 0; i < 6; ++i) { <a name="l00221"></a>00221 hdr->dhwaddr.addr[i] = hdr->shwaddr.addr[i]; <a name="l00222"></a>00222 hdr->shwaddr.addr[i] = ethaddr->addr[i]; <a name="l00223"></a>00223 hdr->ethhdr.dest.addr[i] = hdr->dhwaddr.addr[i]; <a name="l00224"></a>00224 hdr->ethhdr.src.addr[i] = ethaddr->addr[i]; <a name="l00225"></a>00225 } <a name="l00226"></a>00226 <a name="l00227"></a>00227 hdr->hwtype = <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(<a class="code" href="arp_8c.html#28a28d3e575df3e75985e2313cc2d51a">HWTYPE_ETHERNET</a>); <a name="l00228"></a>00228 <a class="code" href="arp_8c.html#f463dc7a7aaf3ea0b6bc472b261380ac">ARPH_HWLEN_SET</a>(hdr, 6); <a name="l00229"></a>00229 <a name="l00230"></a>00230 hdr->proto = <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(<a class="code" href="arp_8h.html#392234c1299a5f5c264e511d88a66e56">ETHTYPE_IP</a>); <a name="l00231"></a>00231 <a class="code" href="arp_8c.html#edb2ded9225b09da0358754f859d72d6">ARPH_PROTOLEN_SET</a>(hdr, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structip__addr.html">ip_addr</a>)); <a name="l00232"></a>00232 <a name="l00233"></a>00233 hdr->ethhdr.type = <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(<a class="code" href="arp_8h.html#dbc208bed02b2900ca2639a25d3f6198">ETHTYPE_ARP</a>); <a name="l00234"></a>00234 <span class="keywordflow">return</span> p; <a name="l00235"></a>00235 } <a name="l00236"></a>00236 <span class="keywordflow">break</span>; <a name="l00237"></a>00237 <span class="keywordflow">case</span> <a class="code" href="arp_8c.html#06ba7b414e718081998f2814090debf1">ARP_REPLY</a>: <a name="l00238"></a>00238 <span class="comment">/* ARP reply. We insert or update the ARP table. */</span> <a name="l00239"></a>00239 <span class="keywordflow">if</span>(<a class="code" href="ipv4_2ip__addr_8h.html#008c9c92d770cc030ea054e6b259835a">ip_addr_cmp</a>(&(hdr->dipaddr), &(netif-><a class="code" href="structnetif.html#d666097c02e7d7e82ea838b0c721470d">ip_addr</a>))) { <a name="l00240"></a>00240 <a class="code" href="arp_8c.html#038bb8fc3bcc993d8b7724632e43ed0f">add_arp_entry</a>(&(hdr->sipaddr), &(hdr->shwaddr)); <a name="l00241"></a>00241 } <a name="l00242"></a>00242 <span class="keywordflow">break</span>; <a name="l00243"></a>00243 <span class="keywordflow">default</span>: <a name="l00244"></a>00244 <a class="code" href="kprint_8h.html#b2761bdf0cca73ad0fb5880895210cd8">kprintf</a>(<span class="stringliteral">"arp_arp_input: unknown type %d\n"</span>, <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(hdr->opcode)); <a name="l00245"></a>00245 <span class="keywordflow">break</span>; <a name="l00246"></a>00246 } <a name="l00247"></a>00247 <a name="l00248"></a>00248 <a class="code" href="pbuf_8h.html#334112aa2ac402c254d604c0dbbbb746">pbuf_free</a>(p); <a name="l00249"></a>00249 <span class="keywordflow">return</span> <a class="code" href="def_8h.html#070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>; <a name="l00250"></a>00250 } <a name="l00251"></a>00251 <span class="comment">/*-----------------------------------------------------------------------------------*/</span> <a name="l00252"></a>00252 <span class="keyword">struct </span><a class="code" href="structeth__addr.html">eth_addr</a> * <a name="l00253"></a><a class="code" href="arp_8c.html#eec8fff4f036cca2ac1b5635fa46d21b">00253</a> <a class="code" href="arp_8h.html#eec8fff4f036cca2ac1b5635fa46d21b">arp_lookup</a>(<span class="keyword">struct</span> <a class="code" href="structip__addr.html">ip_addr</a> *ipaddr) <a name="l00254"></a>00254 { <a name="l00255"></a>00255 <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> i; <a name="l00256"></a>00256 <a name="l00257"></a>00257 <span class="keywordflow">for</span>(i = 0; i < <a class="code" href="lwipopts_8h.html#924936a814564dbdb0bc950d255a83b9">ARP_TABLE_SIZE</a>; ++i) { <a name="l00258"></a>00258 <span class="keywordflow">if</span>(<a class="code" href="ipv4_2ip__addr_8h.html#008c9c92d770cc030ea054e6b259835a">ip_addr_cmp</a>(ipaddr, &<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].ipaddr)) { <a name="l00259"></a>00259 <span class="keywordflow">return</span> &<a class="code" href="arp_8c.html#f695765e961406ffcbd5a25a4dca7a55">arp_table</a>[i].ethaddr; <a name="l00260"></a>00260 } <a name="l00261"></a>00261 } <a name="l00262"></a>00262 <span class="keywordflow">return</span> <a class="code" href="def_8h.html#070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>; <a name="l00263"></a>00263 } <a name="l00264"></a>00264 <span class="comment">/*-----------------------------------------------------------------------------------*/</span> <a name="l00265"></a>00265 <span class="keyword">struct </span><a class="code" href="structpbuf.html">pbuf</a> * <a name="l00266"></a><a class="code" href="arp_8c.html#9d1287b9a80c370797b2ac5c63f70e2e">00266</a> <a class="code" href="arp_8h.html#9d1287b9a80c370797b2ac5c63f70e2e">arp_query</a>(<span class="keyword">struct</span> <a class="code" href="structnetif.html">netif</a> *<a class="code" href="structnetif.html">netif</a>, <span class="keyword">struct</span> <a class="code" href="structeth__addr.html">eth_addr</a> *ethaddr, <span class="keyword">struct</span> <a class="code" href="structip__addr.html">ip_addr</a> *ipaddr) <a name="l00267"></a>00267 { <a name="l00268"></a>00268 <span class="keyword">struct </span><a class="code" href="structarp__hdr.html">arp_hdr</a> *hdr; <a name="l00269"></a>00269 <span class="keyword">struct </span><a class="code" href="structpbuf.html">pbuf</a> *p; <a name="l00270"></a>00270 <a class="code" href="types_8h.html#a4e0f27a9aca905e340c06d2dcae843c">uInt8</a> i; <a name="l00271"></a>00271 <a name="l00272"></a>00272 p = <a class="code" href="pbuf_8h.html#bb6c8efa7f7739e71cb286c6dd4a47ef">pbuf_alloc</a>(<a class="code" href="pbuf_8h.html#ee1baa59bb2f85ba575b5a8619ac1ebfb4de441e737330558b609a990cd17346">PBUF_LINK</a>, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structarp__hdr.html">arp_hdr</a>), <a class="code" href="pbuf_8h.html#b3c589840cc5a202e7278f41463f97e7c5e9f28455bca98944a030d4b84ecfab">PBUF_RAM</a>); <a name="l00273"></a>00273 <span class="keywordflow">if</span>(p == <a class="code" href="def_8h.html#070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>) { <a name="l00274"></a>00274 <span class="keywordflow">return</span> <a class="code" href="def_8h.html#070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>; <a name="l00275"></a>00275 } <a name="l00276"></a>00276 <a name="l00277"></a>00277 hdr = p-><a class="code" href="structpbuf.html#8d32dc3e964369d4eec638fc37fbc460">payload</a>; <a name="l00278"></a>00278 <a name="l00279"></a>00279 hdr->opcode = <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(<a class="code" href="arp_8c.html#7a7c46ffaba30477b8c9e3e61bd2e106">ARP_REQUEST</a>); <a name="l00280"></a>00280 <a name="l00281"></a>00281 <span class="keywordflow">for</span>(i = 0; i < 6; ++i) { <a name="l00282"></a>00282 hdr->dhwaddr.addr[i] = 0x00; <a name="l00283"></a>00283 hdr->shwaddr.addr[i] = ethaddr->addr[i]; <a name="l00284"></a>00284 } <a name="l00285"></a>00285 <a name="l00286"></a>00286 <a class="code" href="ipv4_2ip__addr_8h.html#40db7feae2b92f7a7a01d65a85b06e18">ip_addr_set</a>(&(hdr->dipaddr), ipaddr); <a name="l00287"></a>00287 <a class="code" href="ipv4_2ip__addr_8h.html#40db7feae2b92f7a7a01d65a85b06e18">ip_addr_set</a>(&(hdr->sipaddr), &(netif-><a class="code" href="structnetif.html#d666097c02e7d7e82ea838b0c721470d">ip_addr</a>)); <a name="l00288"></a>00288 <a name="l00289"></a>00289 hdr->hwtype = <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(<a class="code" href="arp_8c.html#28a28d3e575df3e75985e2313cc2d51a">HWTYPE_ETHERNET</a>); <a name="l00290"></a>00290 <a class="code" href="arp_8c.html#f463dc7a7aaf3ea0b6bc472b261380ac">ARPH_HWLEN_SET</a>(hdr, 6); <a name="l00291"></a>00291 <a name="l00292"></a>00292 hdr->proto = <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(<a class="code" href="arp_8h.html#392234c1299a5f5c264e511d88a66e56">ETHTYPE_IP</a>); <a name="l00293"></a>00293 <a class="code" href="arp_8c.html#edb2ded9225b09da0358754f859d72d6">ARPH_PROTOLEN_SET</a>(hdr, <span class="keyword">sizeof</span>(<span class="keyword">struct</span> <a class="code" href="structip__addr.html">ip_addr</a>)); <a name="l00294"></a>00294 <a name="l00295"></a>00295 <span class="keywordflow">for</span>(i = 0; i < 6; ++i) { <a name="l00296"></a>00296 hdr->ethhdr.dest.addr[i] = 0xff; <a name="l00297"></a>00297 hdr->ethhdr.src.addr[i] = ethaddr->addr[i]; <a name="l00298"></a>00298 } <a name="l00299"></a>00299 <a name="l00300"></a>00300 hdr->ethhdr.type = <a class="code" href="ipv4_2inet_8h.html#b7fffd06ef1d1a7d34f99eedbb9f44dd">htons</a>(<a class="code" href="arp_8h.html#dbc208bed02b2900ca2639a25d3f6198">ETHTYPE_ARP</a>); <a name="l00301"></a>00301 <span class="keywordflow">return</span> p; <a name="l00302"></a>00302 } <a name="l00303"></a>00303 <span class="comment">/*-----------------------------------------------------------------------------------*/</span> <a name="l00304"></a>00304 <a name="l00305"></a>00305 <a name="l00306"></a>00306 <a name="l00307"></a>00307 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Dec 12 08:52:07 2006 for UbixOS V2 by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.7 </small></address> </body> </html>