?? icmp.c
字號:
/************************************************************
* File name : Icmp.c *
* By : hugang, hgx2000@mail.china.com *
************************************************************/
#include "armnet.h"
#include "..\inc\44blib.h"
#include <string.h>
extern NODE locnode;
/********************************************************************************************************
* Return ICMP data length (-1 if no data), 0 if not ICMP *
********************************************************************************************************/
short IsIcmp(IPKT *ip, short len)
{
ICMPKT *icmp;
unsigned short sum;
short dlen = 0;
if (ip->i.pcol==PICMP && len>=sizeof(ICMPHDR))
{
icmp = (ICMPKT *)ip;
if ((sum=csum((unsigned char *)&icmp->c, (unsigned short)len)) == 0xffff)
{
SwapIcmp(icmp);
dlen = len>sizeof(ICMPHDR) ? len-sizeof(ICMPHDR) : -1;
}
}
return(dlen);
}
/********************************************************************************************************
* Make an ICMP packet *
********************************************************************************************************/
short MakeIcmp(ETHERFRAME *efp, NODE *srcep, NODE *destp, unsigned char type, unsigned char codetype, unsigned short ident, unsigned short seq, unsigned short dlen)
{
ICMPKT *icmp;
unsigned short len;
icmp = (ICMPKT *)(efp->edata);
icmp->c.type = type;
icmp->c.codetype = codetype;
icmp->c.ident = ident;
icmp->c.seq = seq;
SwapIcmp(icmp);
icmp->c.check = 0;
len = (unsigned short)(dlen + sizeof(ICMPHDR));
icmp->c.check = ~csum((unsigned char *)&icmp->c, len);
return(MakeIp(efp, srcep, destp, PICMP, len));
}
/********************************************************************************************************
* Swap byte order of ints in ICMP header *
********************************************************************************************************/
void SwapIcmp(ICMPKT *icmp)
{
icmp->c.ident = swapw(icmp->c.ident);
icmp->c.seq = swapw(icmp->c.seq);
}
/********************************************************************************************************
* ICMP handler *
********************************************************************************************************/
void IcmpRcvPacket(ETHERFRAME *efp)
{
ICMPKT *icmp;
NODE desnode;
short txlen;
icmp = (ICMPKT *)(efp->edata);
if(icmp->c.type == ICREQ)
{
DEBUGF(ICMP_DEBUG,("Get ICMP ECHO_REQUEST\n"));
if (READ_PACKED(icmp->i.dip) != locnode.ip) return;
desnode.ip = READ_PACKED(icmp->i.sip);
DEBUGF(ICMP_DEBUG, ("Finding ip %x in arp chache\n", desnode.ip));
if(ArpFindCache(desnode.ip, desnode.mac))
{
DEBUGF(ARP_DEBUG, ("Fail to find ip %x in arp cache\n", desnode.ip));
DEBUGF(ARP_DEBUG, ("Send Arp Request to ip %x\n",desnode.ip));
ArpSendReq(efp, &desnode);
}
else
{
txlen = MakeIcmp(efp, &locnode, &desnode, ICREP, 0, icmp->c.ident, icmp->c.seq, 0);
DEBUGF(ICMP_DEBUG,("Put ICMP ECHO_REPLY\n"));
PutEthernet(efp, txlen); /* Send packet */
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -