📄 moarp.cpp
字号:
/*! \file MoArp.cpp \brief ARP僾儘僩僐儖幚憰僋儔僗 Copyright (c) 2004 Yamami All rights reserved. License=MIT/X License \author Yamami \version $Revision: 1.3 $ \date create:2004/08/28 update:$Date: 2004/10/31 14:29:43 $*//*! \class MoArp * \brief ARP僾儘僩僐儖幚憰僋儔僗 */#include "MoArp.h"#include "MonesConfig.h"#include "MonesGlobal.h"/*! \brief initialize MoArp 僐儞僗僩儔僋僞 \author Yamami \date create:2004/08/28 update:*/MoArp::MoArp(){ //ARP僉儍僢僔儏弶婜壔 ArpCache = new HashMap<char*>(ARP_CACHE_NUM);}/*! \brief initialize MoArp initArp \author Yamami \param AbstractMonic *pminsNic [in] NIC僋儔僗傊偺億僀儞僞 \date create:2004/08/28 update:*/void MoArp::initArp(AbstractMonic *pminsNic ) { //NIC僋儔僗傪曐帩 insAbstractNic = pminsNic; return;}/*! \brief initialize MoArp 僨僗僋僩儔僋僞 \author Yamami \date create:2004/08/28 update:*/MoArp::~MoArp() {}/*! \brief receiveArp ARP僾儘僩僐儖庴怣 張棟 \param ARP_HEADER *arpHead [in] ARP僿僢僟傊偺億僀儞僞 \return int 寢壥 \author Yamami \date create:2004/08/28 update:2004/08/29*/int MoArp::receiveArp(ARP_HEADER *arpHead){ int rest=0; // IP偺妋擣 // 帺屓IP埗偺梫媮偱柍偗傟偽丄壗傕偟側偄丅 if(arpHead->dstIp!=MoPacUtl::swapLong(G_MonesCon.getGl_myIpAdr() )){ return 0; } // 僆儁儗乕僔儑儞僐乕僪偺妋擣 switch(MoPacUtl::swapShort(arpHead->opeCode)) { //ARP梫媮 case OPE_CODE_ARP_REQ: transArp(arpHead->srcIp,arpHead->srcMac, OPE_CODE_ARP_REP); //僉儍僢僔儏偵搊榐 addArpCache(arpHead->srcIp,(char*)arpHead->srcMac); break; //ARP墳摎 case OPE_CODE_ARP_REP: //枹幚憰 //rest=receiveReply(arpHead->srcIp,arpHead->srcMac); break; //RARP梫媮 case OPE_CODE_RARP_REQ: //RARP墳摎 case OPE_CODE_RARP_REP: break; } return rest;}static char broadcastMac[]={0xff,0xff,0xff,0xff,0xff,0xff};/*! \brief transArp ARP墳摎張棟 張棟 \param dword dstip [in] 憲怣愭IP傾僪儗僗 \param byte *dstmac [in] 憲怣愭MAC傾僪儗僗 \param word opecode [in] 僆儁儗乕僔儑儞僐乕僪 \return void 柍偟 \author Yamami \date create:2004/08/28 update:*/void MoArp::transArp(dword dstip, byte *dstmac, word opecode){ ARP_HEADER head; //墳摎梡 ARP僿僢僟僼儗乕儉嶌惉 head.hardType=MoPacUtl::swapShort(HARD_TYPE_ETHER); head.protType=MoPacUtl::swapShort(PROT_TYPE_IP); head.hardAddrLen=6; head.protAddrLen=4; head.opeCode=MoPacUtl::swapShort(opecode); memcpy(head.srcMac,insAbstractNic->ether_mac_addr,6); head.srcIp=MoPacUtl::swapLong(G_MonesCon.getGl_myIpAdr() ); if(memcmp(dstmac,broadcastMac,6)==0){ head.srcIp=MoPacUtl::swapLong(G_MonesCon.getGl_myIpAdr() ); }else{ memcpy(head.dstMac,dstmac,6); } head.dstIp=dstip; //偙偙偼庴偗庢偭偨傑傑曉媝偡傞偺偱丄僄儞僨傿傾儞曄姺偼晄梫 //憲怣張棟 偙偙偱偼丄捈愙僪儔僀僶傪僐乕儖 insAbstractNic->frame_output((byte *)&head , dstmac , sizeof(head) , ETHER_PROTO_ARP);}/*! \brief addArpCache ARP僉儍僢僔儏搊榐張棟 \param dword ip [in] 搊榐IP傾僪儗僗 \param char *mac [in] 搊榐MAC傾僪儗僗 \return void 柍偟 \author Yamami \date create:2004/09/19 update:*/void MoArp::addArpCache(dword ip, char *mac){ char IpKey[10]; //IP傾僪儗僗僉乕 //IP傾僪儗僗傪丄MAP偺僉乕壔(String壔) sprintf(IpKey , "%08x",ip); ArpCache->put(IpKey , mac);}/*! \brief searchCache ARP僉儍僢僔儏専嶕張棟 \param dword ip [in] 搊榐IP傾僪儗僗 \return char *mac [in] 専嶕寢壥MAC傾僪儗僗 尒偮偐傜側偄応崌偼NULL \author Yamami \date create:2004/09/19 update:*/char* MoArp::searchCache(dword ip){ char IpKey[10]; //IP傾僪儗僗僉乕 char *RetMacValue; //HashMap偐傜専嶕 //IP傾僪儗僗傪丄MAP偺僉乕壔(String壔) sprintf(IpKey , "%08x",ip); RetMacValue = ArpCache->get(IpKey); return RetMacValue;}/*! \brief getMac ARP IP傾僪儗僗夝寛張棟 \param dword ip [in] IP傾僪儗僗 \param byte *mac [OUT] 夝寛MAC傾僪儗僗傊偺億僀儞僞 \return void 柍偟 \author Yamami \date create:2004/09/20 update:*/int MoArp::getMac(dword ip,char *mac){ enum{ RETRY_COUNT=3, // 儕僩儔僀憲怣夞悢 REQUEST_TIMEOUT=2000, // 儕僋僄僗僩僞僀儉傾僂僩儈儕昩 }; char *retmac; //int rest; //REPLY_WAIT *wait; //int i; //REPLY_WAIT *p; // 僉儍僢僔儏傪専嶕 if((retmac=searchCache(ip))!=NULL) { //僉儍僢僔儏偐傜尒偮偐傟偽丄曉偡丅 memcpy(mac,retmac,6); return 0; }/* // 憲怣弨旛 if((wait=kmalloc(sizeof(REPLY_WAIT)))==NULL)return -ENOMEM; wait->ip=ip; wait->repFlg=0; wait->next=replyWait; replyWait=wait; // 儕僋僄僗僩ARP傪憲怣 for(i=0;;) { transArp(num,ip,broadcastMac,OPE_CODE_ARP_REQ); wait->wait.flag=0; wait_intr(&wait->wait,REQUEST_TIMEOUT,TASK_SIGNAL_WAIT); if(wait->repFlg) { addCache(ip,wait->mac); memcpy(mac,wait->mac,6); rest=0; break; } if(++i>=RETRY_COUNT) { rest=-ENETUNREACH; break; } } // 憲怣屻張棟 if(replyWait==wait)replyWait=wait->next; else { for(p=replyWait;p->next!=wait;p=p->next); p->next=wait->next; } kfree(wait); return rest;*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -