uip源码分析Word下载.docx
《uip源码分析Word下载.docx》由会员分享,可在线阅读,更多相关《uip源码分析Word下载.docx(66页珍藏版)》请在冰豆网上搜索。
};
只有三个项,很简单
第一项是ip地址,16*2=4*8位的,保存四个八位组.
第二项是MAC地址.
第三项是缓存更新时间.
下来是ARP请求发送函数:
L325
/*-----------------------------------------------------------------------------------*/
/**
*PrependEthernetheadertoanoutboundIPpacketandseeifweneed
*tosendoutanARPrequest.
*为传出的IP包添加以太网头并看是否需要发送ARP请求.
*ThisfunctionshouldbecalledbeforesendingoutanIPpacket.The
*functionchecksthedestinationIPaddressoftheIPpackettosee
*whatEthernetMACaddressthatshouldbeusedasadestinationMAC
*addressontheEthernet.
*此函数应该在发送IP包时调用,它会检查IP包的目的IP地址,看看以太网应该使用什么目的MAC地址.
*IfthedestinationIPaddressisinthelocalnetwork(determined
*bylogicalANDingofnetmaskandourIPaddress),thefunction
*checkstheARPcachetoseeifanentryforthedestinationIP
*addressisfound.Ifso,anEthernetheaderisprependedandthe
*functionreturns.IfnoARPcacheentryisfoundforthe
*destinationIPaddress,thepacketintheuip_buf[]isreplacedby
*anARPrequestpacketfortheIPaddress.TheIPpacketisdropped
*anditisassumedthattheyhigherlevelprotocols.,TCP)
*eventuallywillretransmitthedroppedpacket.
*如果目的IP地址是在局域网中(由IP地址与子网掩码的与逻辑决定),函数就会从ARP缓存表中查找有
*无对应项.若有,就取对应的MAC地址,加上以太网头,并返回,否则uip_buf[]中的数据包会被替换成一个
*目的IP在址的ARP请求.原来的IP包会被简单的仍掉,此函数假设高层协议(如TCP)会最终重传扔掉的包.
*IfthedestinationIPaddressisnotonthelocalnetwork,theIP
*addressofthedefaultrouterisusedinstead.
*如果目标IP地址并非一个局域网IP,则会使用默认路由的IP地址.
*Whenthefunctionreturns,apacketispresentintheuip_buf[]
*buffer,andthelengthofthepacketisintheglobalvariable
*uip_len.函数返回时,uip_buf[]中已经有了一个包,其长度由uip_len指定.
*/
void
uip_arp_out(void)
{
structarp_entry*tabptr;
/*FindthedestinationIPaddressintheARPtableandconstruct
theEthernetheader.IfthedestinationIPaddresisn'
tonthe
localnetwork,weusethedefaultrouter'
sIPaddressinstead.
果目的IP地址不在局域网中,则使用默认IfnotARPtableentryisfound,weoverwritetheoriginalIP
packetwithanARPrequestfortheIPaddress.*/
/*Firstcheckifdestinationisalocalbroadcast.首先检查目标是不是广播*/
if(uip_ipaddr_cmp(IPBUF->
destipaddr,broadcast_ipaddr)){
memcpy(IPBUF->
6);
}else{
/*Checkifthedestinationaddressisonthelocalnetwork.检查目标地址是否在局域网内*/
if(!
uip_ipaddr_maskcmp(IPBUF->
destipaddr,uip_hostaddr,uip_netmask)){
/*Destinationaddresswasnotonthelocalnetwork,soweneedto
usethedefaultrouter'
sIPaddressinsteadofthedestination
addresswhendeterminingtheMACaddress.目的地址不在局域网内,所以保用默认路由器的地址来确在MAC地址*/
uip_ipaddr_copy(ipaddr,uip_draddr);
/*Else,weusethedestinationIPaddress.否则,使用目标IP地址*/
uip_ipaddr_copy(ipaddr,IPBUF->
destipaddr);
}
for(i=0;
i<
UIP_ARPTAB_SIZE;
++i){tabptr=&
arp_table;
if(uip_ipaddr_cmp(ipaddr,tabptr->
ipaddr)){
break;
if(i==UIP_ARPTAB_SIZE){
/*ThedestinationaddresswasnotinourARPtable,sowe
overwritetheIPpacketwithanARPrequest.如果遍历到头没找到,将原IP包替换为ARP请求并返回*/
memset(BUF->
0xff,6);
0x00,6);
memcpy(BUF->
,6);
uip_ipaddr_copy(BUF->
dipaddr,ipaddr);
sipaddr,uip_hostaddr);
BUF->
opcode=HTONS(ARP_REQUEST);
/*ARPrequest.*/
hwtype=HTONS(ARP_HWTYPE_ETH);
protocol=HTONS(UIP_ETHTYPE_IP);
hwlen=6;
protolen=4;
=HTONS(UIP_ETHTYPE_ARP);
uip_appdata=&
uip_buf[UIP_TCPIP_HLEN+UIP_LLH_LEN];
uip_len=sizeof(structarp_hdr);
return;
/*Buildanethernetheader.如果是在局域网中,且在ARP缓存中找到了(如果没找到进行不到这一步,在上面就返回了),则构建以太网头*/
tabptr->
6);
IPBUF->
=HTONS(UIP_ETHTYPE_IP);
uip_len+=sizeof(structuip_eth_hdr);
以上内容自325行起.下面再总结一下其基本顺序:
用IPBUF->
来存储目的IP地址,它有可能是局域网内一主机IP,也可能是路由器IP(如果是发往外网,即原来的目的IP不在局域网内),还有可能是广播专用的IP.
先看是不是在广播:
如果是广播,将IPBUF->
设为广播IP.
再看是不是在局域网内:
如果不是,则将IPBUF->
设为路由器IP.
如果在局域网内,查看是否已经存在于ARP缓存表中:
如果不在,将要发送的换成ARP请求,返回.
如果已存在,则查找使用查找到的MAC地址为IP包添加以太网头.
这里还要解释一些细节问题,主要是:
1.如何在IP包上添加以太网头
2.如果将IP包替换成ARP请求,ARP请求的格式是什么.
3.广播地址
这些问题将在二楼来说.
将IP包替换成ARP请求部分代码(实际上IP包是放在uip_buf[]里的,这里只是将uip_buf[]填充上ARP请求即可),于的388行:
overwritetheIPpacketwithanARPrequest.*/
memset(BUF->
memcpy(BUF->
uip_ipaddr_copy(BUF->
BUF->
hwle