📄 a00204.html
字号:
<a name="l00320"></a>00320 <span class="keywordflow">break</span>;<a name="l00321"></a>00321 }<a name="l00322"></a>00322 <a name="l00323"></a>00323 <span class="keywordflow">return</span>;<a name="l00324"></a>00324 }<a name="l00325"></a>00325 <span class="comment">/*-----------------------------------------------------------------------------------*/</span><span class="comment"></span><a name="l00326"></a>00326 <span class="comment">/**</span><a name="l00327"></a>00327 <span class="comment"> * Prepend Ethernet header to an outbound IP packet and see if we need</span><a name="l00328"></a>00328 <span class="comment"> * to send out an ARP request.</span><a name="l00329"></a>00329 <span class="comment"> *</span><a name="l00330"></a>00330 <span class="comment"> * This function should be called before sending out an IP packet. The</span><a name="l00331"></a>00331 <span class="comment"> * function checks the destination IP address of the IP packet to see</span><a name="l00332"></a>00332 <span class="comment"> * what Ethernet MAC address that should be used as a destination MAC</span><a name="l00333"></a>00333 <span class="comment"> * address on the Ethernet.</span><a name="l00334"></a>00334 <span class="comment"> *</span><a name="l00335"></a>00335 <span class="comment"> * If the destination IP address is in the local network (determined</span><a name="l00336"></a>00336 <span class="comment"> * by logical ANDing of netmask and our IP address), the function</span><a name="l00337"></a>00337 <span class="comment"> * checks the ARP cache to see if an entry for the destination IP</span><a name="l00338"></a>00338 <span class="comment"> * address is found. If so, an Ethernet header is prepended and the</span><a name="l00339"></a>00339 <span class="comment"> * function returns. If no ARP cache entry is found for the</span><a name="l00340"></a>00340 <span class="comment"> * destination IP address, the packet in the uip_buf[] is replaced by</span><a name="l00341"></a>00341 <span class="comment"> * an ARP request packet for the IP address. The IP packet is dropped</span><a name="l00342"></a>00342 <span class="comment"> * and it is assumed that they higher level protocols (e.g., TCP)</span><a name="l00343"></a>00343 <span class="comment"> * eventually will retransmit the dropped packet.</span><a name="l00344"></a>00344 <span class="comment"> *</span><a name="l00345"></a>00345 <span class="comment"> * If the destination IP address is not on the local network, the IP</span><a name="l00346"></a>00346 <span class="comment"> * address of the default router is used instead.</span><a name="l00347"></a>00347 <span class="comment"> *</span><a name="l00348"></a>00348 <span class="comment"> * When the function returns, a packet is present in the uip_buf[]</span><a name="l00349"></a>00349 <span class="comment"> * buffer, and the length of the packet is in the global variable</span><a name="l00350"></a>00350 <span class="comment"> * uip_len.</span><a name="l00351"></a>00351 <span class="comment"> */</span><a name="l00352"></a>00352 <span class="comment">/*-----------------------------------------------------------------------------------*/</span><a name="l00353"></a>00353 <span class="keywordtype">void</span><a name="l00354"></a><a class="code" href="a00152.html#g54b27e45df15e10a0eb1a3e3a91417d2">00354</a> <a class="code" href="a00152.html#g54b27e45df15e10a0eb1a3e3a91417d2">uip_arp_out</a>(<span class="keywordtype">void</span>)<a name="l00355"></a>00355 {<a name="l00356"></a>00356 <span class="keyword">struct </span>arp_entry *tabptr;<a name="l00357"></a>00357 <a name="l00358"></a>00358 <span class="comment">/* Find the destination IP address in the ARP table and construct</span><a name="l00359"></a>00359 <span class="comment"> the Ethernet header. If the destination IP addres isn't on the</span><a name="l00360"></a>00360 <span class="comment"> local network, we use the default router's IP address instead.</span><a name="l00361"></a>00361 <span class="comment"></span><a name="l00362"></a>00362 <span class="comment"> If not ARP table entry is found, we overwrite the original IP</span><a name="l00363"></a>00363 <span class="comment"> packet with an ARP request for the IP address. */</span><a name="l00364"></a>00364 <a name="l00365"></a>00365 <span class="comment">/* First check if destination is a local broadcast. */</span><a name="l00366"></a>00366 <span class="keywordflow">if</span>(<a class="code" href="a00148.html#g210e629f7252e4bc8458cbdf260b3318">uip_ipaddr_cmp</a>(<a class="code" href="a00152.html#g9f2196e2705036869611962425e404bf">IPBUF</a>->destipaddr, broadcast_ipaddr)) {<a name="l00367"></a>00367 memcpy(<a class="code" href="a00152.html#g9f2196e2705036869611962425e404bf">IPBUF</a>->ethhdr.dest.addr, broadcast_ethaddr.<a class="code" href="a00089.html#aacd5fa1806e0f0dd0bc96dd36507ad8">addr</a>, 6);<a name="l00368"></a>00368 } <span class="keywordflow">else</span> {<a name="l00369"></a>00369 <span class="comment">/* Check if the destination address is on the local network. */</span><a name="l00370"></a>00370 <span class="keywordflow">if</span>(!<a class="code" href="a00148.html#g6b16e0bac41821c1fbe0c267071642f0">uip_ipaddr_maskcmp</a>(<a class="code" href="a00152.html#g9f2196e2705036869611962425e404bf">IPBUF</a>->destipaddr, <a class="code" href="a00150.html#g7d3673f52f5846b6961d23b150decd54">uip_hostaddr</a>, uip_netmask)) {<a name="l00371"></a>00371 <span class="comment">/* Destination address was not on the local network, so we need to</span><a name="l00372"></a>00372 <span class="comment"> use the default router's IP address instead of the destination</span><a name="l00373"></a>00373 <span class="comment"> address when determining the MAC address. */</span><a name="l00374"></a>00374 <a class="code" href="a00148.html#g769512993b7b27271909d6daa4748b60">uip_ipaddr_copy</a>(ipaddr, <a class="code" href="a00150.html#g20df5c82f2a15a508c19e505b5d9de2b">uip_draddr</a>);<a name="l00375"></a>00375 } <span class="keywordflow">else</span> {<a name="l00376"></a>00376 <span class="comment">/* Else, we use the destination IP address. */</span><a name="l00377"></a>00377 <a class="code" href="a00148.html#g769512993b7b27271909d6daa4748b60">uip_ipaddr_copy</a>(ipaddr, <a class="code" href="a00152.html#g9f2196e2705036869611962425e404bf">IPBUF</a>->destipaddr);<a name="l00378"></a>00378 }<a name="l00379"></a>00379 <a name="l00380"></a>00380 <span class="keywordflow">for</span>(i = 0; i < <a class="code" href="a00153.html#gb1455b27c06532a399cf06d2c1d6d08d">UIP_ARPTAB_SIZE</a>; ++i) {<a name="l00381"></a>00381 tabptr = &arp_table[i];<a name="l00382"></a>00382 <span class="keywordflow">if</span>(<a class="code" href="a00148.html#g210e629f7252e4bc8458cbdf260b3318">uip_ipaddr_cmp</a>(ipaddr, tabptr->ipaddr)) {<a name="l00383"></a>00383 <span class="keywordflow">break</span>;<a name="l00384"></a>00384 }<a name="l00385"></a>00385 }<a name="l00386"></a>00386 <a name="l00387"></a>00387 <span class="keywordflow">if</span>(i == <a class="code" href="a00153.html#gb1455b27c06532a399cf06d2c1d6d08d">UIP_ARPTAB_SIZE</a>) {<a name="l00388"></a>00388 <span class="comment">/* The destination address was not in our ARP table, so we</span><a name="l00389"></a>00389 <span class="comment"> overwrite the IP packet with an ARP request. */</span><a name="l00390"></a>00390 <a name="l00391"></a>00391 memset(<a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->ethhdr.dest.addr, 0xff, 6);<a name="l00392"></a>00392 memset(<a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->dhwaddr.addr, 0x00, 6);<a name="l00393"></a>00393 memcpy(<a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->ethhdr.src.addr, <a class="code" href="a00150.html#g499bb98a0b4ae9a98553ede81317606d">uip_ethaddr</a>.<a class="code" href="a00089.html#aacd5fa1806e0f0dd0bc96dd36507ad8">addr</a>, 6);<a name="l00394"></a>00394 memcpy(<a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->shwaddr.addr, <a class="code" href="a00150.html#g499bb98a0b4ae9a98553ede81317606d">uip_ethaddr</a>.<a class="code" href="a00089.html#aacd5fa1806e0f0dd0bc96dd36507ad8">addr</a>, 6);<a name="l00395"></a>00395 <a name="l00396"></a>00396 <a class="code" href="a00148.html#g769512993b7b27271909d6daa4748b60">uip_ipaddr_copy</a>(<a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->dipaddr, ipaddr);<a name="l00397"></a>00397 <a class="code" href="a00148.html#g769512993b7b27271909d6daa4748b60">uip_ipaddr_copy</a>(<a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->sipaddr, <a class="code" href="a00150.html#g7d3673f52f5846b6961d23b150decd54">uip_hostaddr</a>);<a name="l00398"></a>00398 <a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->opcode = <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(<a class="code" href="a00152.html#g7a7c46ffaba30477b8c9e3e61bd2e106">ARP_REQUEST</a>); <span class="comment">/* ARP request. */</span><a name="l00399"></a>00399 <a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->hwtype = <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(<a class="code" href="a00152.html#gbb56b549f7ab4d86e1cc39b8afc70d1e">ARP_HWTYPE_ETH</a>);<a name="l00400"></a>00400 <a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->protocol = <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(<a class="code" href="a00152.html#g03d140db75de3d3cdfbbab1c4fed8d8d">UIP_ETHTYPE_IP</a>);<a name="l00401"></a>00401 <a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->hwlen = 6;<a name="l00402"></a>00402 <a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->protolen = 4;<a name="l00403"></a>00403 <a class="code" href="a00150.html#g24f52ac52d6e714cb04a5aa01be3bdd0">BUF</a>->ethhdr.type = <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(<a class="code" href="a00152.html#g3e1562e8a6de32268e5df92a52152f91">UIP_ETHTYPE_ARP</a>);<a name="l00404"></a>00404 <a name="l00405"></a>00405 <a class="code" href="a00150.html#g561b8eda32e059d4e7397f776268cc63">uip_appdata</a> = &<a class="code" href="a00146.html#gb81e78f890dbbee50c533a9734b74fd9">uip_buf</a>[<a class="code" href="a00150.html#gee37386b2ab828787c05227eb109def7">UIP_TCPIP_HLEN</a> + <a class="code" href="a00153.html#ge6f4a2453dbd8bc60e6a82774552366a">UIP_LLH_LEN</a>];<a name="l00406"></a>00406 <a name="l00407"></a>00407 <a class="code" href="a00149.html#g12a33f0c09711167bdf3dd7d7cf8c5a1">uip_len</a> = <span class="keyword">sizeof</span>(<span class="keyword">struct </span>arp_hdr);<a name="l00408"></a>00408 <span class="keywordflow">return</span>;<a name="l00409"></a>00409 }<a name="l00410"></a>00410 <a name="l00411"></a>00411 <span class="comment">/* Build an ethernet header. */</span><a name="l00412"></a>00412 memcpy(<a class="code" href="a00152.html#g9f2196e2705036869611962425e404bf">IPBUF</a>->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);<a name="l00413"></a>00413 }<a name="l00414"></a>00414 memcpy(<a class="code" href="a00152.html#g9f2196e2705036869611962425e404bf">IPBUF</a>->ethhdr.src.addr, <a class="code" href="a00150.html#g499bb98a0b4ae9a98553ede81317606d">uip_ethaddr</a>.<a class="code" href="a00089.html#aacd5fa1806e0f0dd0bc96dd36507ad8">addr</a>, 6);<a name="l00415"></a>00415 <a name="l00416"></a>00416 <a class="code" href="a00152.html#g9f2196e2705036869611962425e404bf">IPBUF</a>->ethhdr.type = <a class="code" href="a00148.html#g69a7a4951ff21b302267532c21ee78fc">HTONS</a>(<a class="code" href="a00152.html#g03d140db75de3d3cdfbbab1c4fed8d8d">UIP_ETHTYPE_IP</a>);<a name="l00417"></a>00417 <a name="l00418"></a>00418 <a class="code" href="a00149.html#g12a33f0c09711167bdf3dd7d7cf8c5a1">uip_len</a> += <span class="keyword">sizeof</span>(<span class="keyword">struct </span><a class="code" href="a00090.html">uip_eth_hdr</a>);<a name="l00419"></a>00419 }<a name="l00420"></a>00420 <span class="comment">/*-----------------------------------------------------------------------------------*/</span><a name="l00421"></a>00421 <span class="comment"></span><a name="l00422"></a>00422 <span class="comment">/** @} */</span><span class="comment"></span><a name="l00423"></a>00423 <span class="comment">/** @} */</span></pre></div><hr size="1"><address style="align: right;"><small>Generated on Mon Jun 12 10:23:01 2006 for uIP 1.0 by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -