?lang_form?
?lang_select?
?lang_submit?
?lang_endform?
{HEADER END}
{FILE START}
library
?curdirlinks? - Rev 6
?prevdifflink? - Blame - ?getfile?
<!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>Procyon AVRlib: net/arp.c Source File</title>
<link href="dox.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.2 -->
<div class="qindex"><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="dirs.html">Directories</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></div>
<div class="nav">
<a class="el" href="dir_000001.html">net</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">00001 <span class="comment">/*! \file arp.c \brief ARP Protocol Library. */</span>
00002 <span class="comment">//*****************************************************************************</span>
00003 <span class="comment">//</span>
00004 <span class="comment">// File Name : 'arp.c'</span>
00005 <span class="comment">// Title : ARP Protocol Library</span>
00006 <span class="comment">// Author : Pascal Stang</span>
00007 <span class="comment">// Created : 9/7/2004</span>
00008 <span class="comment">// Revised : 7/3/2005</span>
00009 <span class="comment">// Version : 0.1</span>
00010 <span class="comment">// Target MCU : Atmel AVR series</span>
00011 <span class="comment">// Editor Tabs : 4</span>
00012 <span class="comment">//</span>
00013 <span class="comment">//*****************************************************************************</span>
00014
00015 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>
00016 <span class="preprocessor">#include "<a class="code" href="net_8h.html">net.h</a>"</span>
00017 <span class="preprocessor">#include "<a class="code" href="nic_8h.html">nic.h</a>"</span>
00018 <span class="preprocessor">#include "<a class="code" href="arp_8h.html">arp.h</a>"</span>
00019
00020 <span class="preprocessor">#include "<a class="code" href="rprintf_8h.html">rprintf.h</a>"</span>
00021
00022 <span class="comment">// global variables</span>
00023 <span class="comment"></span>
00024 <span class="comment">/// Single ARP table entry/record</span>
00025 <span class="comment"></span><span class="keyword">struct </span>ArpEntry
00026 {
00027 uint32_t ipaddr; <span class="comment">///< remote-note IP address</span>
00028 <span class="comment"></span> <span class="keyword">struct </span>netEthAddr ethaddr; <span class="comment">///< remote-node ethernet (hardware/mac) address</span>
00029 <span class="comment"></span> uint8_t time; <span class="comment">///< time to live (in ARP table); this is decremented by arpTimer()</span>
00030 <span class="comment"></span>};
00031
<a name="l00032"></a><a class="code" href="arp_8c.html#a0">00032</a> <span class="keyword">struct </span>ArpEntry ArpMyAddr; <span class="comment">///< my local interface information (IP and MAC address)</span>
<a name="l00033"></a><a class="code" href="arp_8c.html#a1">00033</a> <span class="comment"></span><span class="keyword">struct </span>ArpEntry ArpTable[ARP_TABLE_SIZE]; <span class="comment">///< ARP table of matched IP<->MAC associations</span>
00034 <span class="comment"></span>
00035
<a name="l00036"></a><a class="code" href="group__arp.html#ga0">00036</a> <span class="keywordtype">void</span> <a class="code" href="group__arp.html#ga0">arpInit</a>(<span class="keywordtype">void</span>)
00037 {
00038 u08 i;
00039 <span class="comment">// initialize all ArpTable elements to unused</span>
00040 <span class="keywordflow">for</span>(i=0; i<ARP_TABLE_SIZE; i++)
00041 {
00042 ArpTable[i].ipaddr = 0;
00043 ArpTable[i].time = 0;
00044 }
00045 }
00046
<a name="l00047"></a><a class="code" href="group__arp.html#ga1">00047</a> <span class="keywordtype">void</span> <a class="code" href="group__arp.html#ga1">arpSetAddress</a>(<span class="keyword">struct</span> netEthAddr* myeth, uint32_t myip)
00048 {
00049 <span class="comment">// set local address record</span>
00050 ArpMyAddr.ethaddr = *myeth;
00051 ArpMyAddr.ipaddr = myip;
00052 }
00053
<a name="l00054"></a><a class="code" href="group__arp.html#ga2">00054</a> <span class="keywordtype">void</span> <a class="code" href="group__arp.html#ga2">arpArpIn</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len, <span class="keyword">struct</span> netEthArpHeader* packet)
00055 {
00056 <span class="preprocessor"> #ifdef ARP_DEBUG</span>
00057 <span class="preprocessor"></span> <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Received ARP Request\r\n"</span>);
00058 <a class="code" href="group__arp.html#ga7">arpPrintHeader</a>( &packet->arp );
00059 <span class="preprocessor"> #endif</span>
00060 <span class="preprocessor"></span>
00061 <span class="comment">// for now, we just reply to requests</span>
00062 <span class="comment">// need to add ARP cache</span>
00063 <span class="keywordflow">if</span>( (packet->arp.dipaddr == <a class="code" href="group__net.html#ga34">HTONL</a>(ArpMyAddr.ipaddr)) &&
00064 (packet->arp.opcode == <a class="code" href="group__net.html#ga2">htons</a>(ARP_OPCODE_REQUEST)) )
00065 {
00066 <span class="comment">// in ARP header</span>
00067 <span class="comment">// copy sender's address info to dest. fields</span>
00068 packet->arp.dhwaddr = packet->arp.shwaddr;
00069 packet->arp.dipaddr = packet->arp.sipaddr;
00070 <span class="comment">// fill in our information</span>
00071 packet->arp.shwaddr = ArpMyAddr.ethaddr;
00072 packet->arp.sipaddr = <a class="code" href="group__net.html#ga34">HTONL</a>(ArpMyAddr.ipaddr);
00073 <span class="comment">// change op to reply</span>
00074 packet->arp.opcode = <a class="code" href="group__net.html#ga2">htons</a>(ARP_OPCODE_REPLY);
00075
00076 <span class="comment">// in ethernet header</span>
00077 packet->eth.dest = packet->eth.src;
00078 packet->eth.src = ArpMyAddr.ethaddr;
00079
00080 <span class="preprocessor"> #ifdef ARP_DEBUG</span>
00081 <span class="preprocessor"></span> <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Sending ARP Reply\r\n"</span>);
00082 <a class="code" href="group__arp.html#ga7">arpPrintHeader</a>( &packet->arp );
00083 <span class="preprocessor"> #endif</span>
00084 <span class="preprocessor"></span>
00085 <span class="comment">// send reply!</span>
00086 <a class="code" href="group__nic.html#ga1">nicSend</a>(len, (<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)packet);
00087 }
00088 }
00089
<a name="l00090"></a><a class="code" href="group__arp.html#ga3">00090</a> <span class="keywordtype">void</span> <a class="code" href="group__arp.html#ga3">arpIpIn</a>(<span class="keyword">struct</span> netEthIpHeader* packet)
00091 {
00092 int8_t index;
00093
00094 <span class="comment">// check if sender is already present in arp table</span>
00095 index = <a class="code" href="group__arp.html#ga6">arpMatchIp</a>(<a class="code" href="group__net.html#ga34">HTONL</a>(packet->ip.srcipaddr));
00096 <span class="keywordflow">if</span>(index != -1)
00097 {
00098 <span class="comment">// sender's IP address found, update ARP entry</span>
00099 ArpTable[index].ethaddr = packet->eth.src;
00100 <span class="comment">// and we're done</span>
00101 <span class="keywordflow">return</span>;
00102 }
00103
00104 <span class="comment">// sender was not present in table,</span>
00105 <span class="comment">// must add in empty/expired slot</span>
00106 <span class="keywordflow">for</span>(index=0; index<ARP_TABLE_SIZE; index++)
00107 {
00108 <span class="keywordflow">if</span>(!ArpTable[index].time)
00109 {
00110 <span class="comment">// write entry</span>
00111 ArpTable[index].ethaddr = packet->eth.src;
00112 ArpTable[index].ipaddr = <a class="code" href="group__net.html#ga34">HTONL</a>(packet->ip.srcipaddr);
00113 ArpTable[index].time = ARP_CACHE_TIME_TO_LIVE;
00114 <span class="comment">// and we're done</span>
00115 <span class="keywordflow">return</span>;
00116 }
00117 }
00118
00119 <span class="comment">// no space in table, we give up</span>
00120 }
00121
<a name="l00122"></a><a class="code" href="group__arp.html#ga4">00122</a> <span class="keywordtype">void</span> <a class="code" href="group__arp.html#ga4">arpIpOut</a>(<span class="keyword">struct</span> netEthIpHeader* packet, uint32_t phyDstIp)
00123 {
00124 <span class="keywordtype">int</span> index;
00125 <span class="comment">// check if destination is already present in arp table</span>
00126 <span class="comment">// use the physical dstIp if it's provided, otherwise the dstIp in packet</span>
00127 <span class="keywordflow">if</span>(phyDstIp)
00128 index = <a class="code" href="group__arp.html#ga6">arpMatchIp</a>(phyDstIp);
00129 <span class="keywordflow">else</span>
00130 index = <a class="code" href="group__arp.html#ga6">arpMatchIp</a>(<a class="code" href="group__net.html#ga34">HTONL</a>(packet->ip.destipaddr));
00131 <span class="comment">// fill in ethernet info</span>
00132 <span class="keywordflow">if</span>(index != -1)
00133 {
00134 <span class="comment">// ARP entry present, fill eth address(es)</span>
00135 packet->eth.src = ArpMyAddr.ethaddr;
00136 packet->eth.dest = ArpTable[index].ethaddr;
00137 packet->eth.type = <a class="code" href="group__net.html#ga33">HTONS</a>(ETHTYPE_IP);
00138 }
00139 <span class="keywordflow">else</span>
00140 {
00141 <span class="comment">// not in table, must send ARP request</span>
00142 packet->eth.src = ArpMyAddr.ethaddr;
00143 <span class="comment">// MUST CHANGE, but for now, send this one broadcast</span>
00144 packet->eth.dest.addr[0] = 0xFF;
00145 packet->eth.dest.addr[1] = 0xFF;
00146 packet->eth.dest.addr[2] = 0xFF;
00147 packet->eth.dest.addr[3] = 0xFF;
00148 packet->eth.dest.addr[4] = 0xFF;
00149 packet->eth.dest.addr[5] = 0xFF;
00150 packet->eth.type = <a class="code" href="group__net.html#ga33">HTONS</a>(ETHTYPE_IP);
00151 }
00152 }
00153
<a name="l00154"></a><a class="code" href="group__arp.html#ga5">00154</a> <span class="keywordtype">void</span> <a class="code" href="group__arp.html#ga5">arpTimer</a>(<span class="keywordtype">void</span>)
00155 {
00156 <span class="keywordtype">int</span> index;
00157 <span class="comment">// this function meant to be called on a regular time interval</span>
00158
00159 <span class="comment">// decrement time-to-live for all entries</span>
00160 <span class="keywordflow">for</span>(index=0; index<ARP_TABLE_SIZE; index++)
00161 {
00162 <span class="keywordflow">if</span>(ArpTable[index].time)
00163 ArpTable[index].time--;
00164 }
00165 }
00166
<a name="l00167"></a><a class="code" href="group__arp.html#ga6">00167</a> <span class="keywordtype">int</span> <a class="code" href="group__arp.html#ga6">arpMatchIp</a>(uint32_t ipaddr)
00168 {
00169 uint8_t i;
00170
00171 <span class="comment">// check if IP address is present in arp table</span>
00172 <span class="keywordflow">for</span>(i=0; i<ARP_TABLE_SIZE; i++)
00173 {
00174 <span class="keywordflow">if</span>(ArpTable[i].ipaddr == ipaddr)
00175 {
00176 <span class="comment">// IP address found</span>
00177 <span class="keywordflow">return</span> i;
00178 }
00179 }
00180
00181 <span class="comment">// no match</span>
00182 <span class="keywordflow">return</span> -1;
00183 }
00184
00185 <span class="preprocessor">#ifdef ARP_DEBUG_PRINT</span>
00186 <span class="preprocessor"></span><span class="keywordtype">void</span> <a class="code" href="group__arp.html#ga7">arpPrintHeader</a>(<span class="keyword">struct</span> netArpHeader* packet)
00187 {
00188 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"ARP Packet:\r\n"</span>);
00189 <span class="comment">//debugPrintHexTable(60, (unsigned char*)&packet);</span>
00190 <span class="comment">// print operation type</span>
00191 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Operation : "</span>);
00192 <span class="keywordflow">if</span>(packet->opcode == <a class="code" href="group__net.html#ga2">htons</a>(ARP_OPCODE_REQUEST))
00193 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"REQUEST"</span>);
00194 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(packet->opcode == <a class="code" href="group__net.html#ga2">htons</a>(ARP_OPCODE_REPLY))
00195 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"REPLY"</span>);
00196 <span class="keywordflow">else</span>
00197 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"UNKNOWN"</span>);
00198 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00199 <span class="comment">// print source hardware address</span>
00200 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"SrcHwAddr : "</span>); <a class="code" href="group__net.html#ga5">netPrintEthAddr</a>(&packet->shwaddr); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00201 <span class="comment">// print source protocol address</span>
00202 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"SrcProtoAddr: "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(<a class="code" href="group__net.html#ga34">HTONL</a>(packet->sipaddr)); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00203 <span class="comment">// print target hardware address</span>
00204 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DstHwAddr : "</span>); <a class="code" href="group__net.html#ga5">netPrintEthAddr</a>(&packet->dhwaddr); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00205 <span class="comment">// print target protocol address</span>
00206 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"DstProtoAddr: "</span>); <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(<a class="code" href="group__net.html#ga34">HTONL</a>(packet->dipaddr)); <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00207 }
00208
00209
00210 <span class="keywordtype">void</span> <a class="code" href="group__arp.html#ga8">arpPrintTable</a>(<span class="keywordtype">void</span>)
00211 {
00212 uint8_t i;
00213
00214 <span class="comment">// print ARP table</span>
00215 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"Time Eth Address IP Address\r\n"</span>);
00216 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">"---------------------------------------\r\n"</span>);
00217 <span class="keywordflow">for</span>(i=0; i<ARP_TABLE_SIZE; i++)
00218 {
00219 <a class="code" href="group__rprintf.html#ga7">rprintfu08</a>(ArpTable[i].time);
00220 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);
00221 <a class="code" href="group__net.html#ga5">netPrintEthAddr</a>(&ArpTable[i].ethaddr);
00222 <a class="code" href="group__rprintf.html#ga15">rprintfProgStrM</a>(<span class="stringliteral">" "</span>);
00223 <a class="code" href="group__net.html#ga6">netPrintIPAddr</a>(ArpTable[i].ipaddr);
00224 <a class="code" href="group__rprintf.html#ga5">rprintfCRLF</a>();
00225 }
00226 }
00227 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.2 </small></address>
</body>
</html>
|
{FILE END}
{FOOTER START}
Powered by WebSVN v2.8.3