📄 icmp.c
字号:
/* Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group *//* See file COPYING 'GNU General Public Licence' for copyright details */#include <stdio.h>#include <ip.h>#include <nw.h>#include <sys/types.h>#include <arpa/inet.h>#include <netdb.h>#include <netinet/in.h>#include <dinet.h>#include <chain.h>#include "icmp.h"typedef struct _HDR HDR;struct _HDR{ BYTE type; BYTE code; WORD check;};static void Rcve(NW_DG *dg, char *addr, BYTE *frame, int length, void *parm);static CHAIN *HdrDecode(CHAIN *chain, ICMP_HDR *icmpHdr);static CHAIN *HdrEncode(CHAIN *chain, ICMP_HDR *icmpHdr);static ICMP_DESCR *DescrFind(ICMP_HDR *icmpHdr, IP_HDR *ipHdr);static WORD Wildcards(ICMP_DESCR *icmp);ICMP_DESCR *icmpDescrList = 0;NW_DG *icmpNw=0;BOOLEAN IcmpInit(void){ static BOOLEAN init=FALSE; if (!init) { icmpNw=NwDgAccept("icmp", Rcve, 0); if (icmpNw==0) return FALSE; init=TRUE; } return TRUE;}BOOLEAN IcmpRegister(ICMP_DESCR *icmp){ ICMP_DESCR **p; for (p=&icmpDescrList; *p!=0; p=&(*p)->next) { if (Wildcards(*p) > Wildcards(icmp)) break; } icmp->next = *p; *p = icmp; return TRUE;}BOOLEAN IcmpRemove(ICMP_DESCR *icmp){ ICMP_DESCR **p; for (p=&icmpDescrList; *p!=0; p=&(*p)->next) { if (*p == icmp) *p = icmp->next; return TRUE; } return FALSE;}BOOLEAN IcmpSend(CHAIN *chain, ICMP_HDR *icmpHdr, IP_HDR *ipHdr){ CHAIN *new; BYTE addr[32]; int length; BYTE *frame; BOOLEAN success = FALSE; new = HdrEncode(chain, icmpHdr); if (new != 0) { length = ChainLength(chain); frame = DnpapMalloc(length); if (frame!=0) { if (ChainCopy(chain, frame, length)) { sprintf(addr,"icmp:%s",Inet_NtoA(ipHdr->dst)); if (NwDgSendTo(0, addr, frame, length)) success = FALSE; } DnpapFree(frame); } } return success;}static CHAIN *HdrDecode(CHAIN *chain, ICMP_HDR *icmpHdr){ HDR *h; if (IpHdrCheck(chain, ChainLength(chain)) != 0) return 0; h = (HDR *)ChainPop(&chain, sizeof(HDR)); if (h==0) return 0; icmpHdr->type = h->type; icmpHdr->code = h->code; icmpHdr->check = IpN2HWord(h->check); return chain;}static CHAIN *HdrEncode(CHAIN *chain, ICMP_HDR *icmpHdr){ HDR *h; h = (HDR *)ChainPush(&chain, sizeof(HDR)); if (h==0) return 0; h->type = icmpHdr->type; h->code = icmpHdr->code; h->check = 0; h->check = IpHdrCheck(chain, ChainLength(chain)); return chain;}static ICMP_DESCR *DescrFind(ICMP_HDR *icmpHdr, IP_HDR *ipHdr){ ICMP_DESCR *p; for (p=icmpDescrList; p!=0; p=p->next) { if ( (p->locAddr==ICMP_ADDR_ANY || p->locAddr==ipHdr->dst) && (p->remAddr==ICMP_ADDR_ANY || p->remAddr==ipHdr->src) && (p->type==ICMP_TYPE_ANY || p->type==icmpHdr->type) && (p->code==ICMP_CODE_ANY || p->type==icmpHdr->code) ) { return p; } } return 0;} static WORD Wildcards(ICMP_DESCR *icmp){ WORD n=0; if (icmp->locAddr == ICMP_ADDR_ANY) n++; if (icmp->remAddr == ICMP_ADDR_ANY) n++; if (icmp->type == ICMP_TYPE_ANY) n++; if (icmp->code == ICMP_CODE_ANY) n++; return n;}static void Rcve(NW_DG *dg, char *addr, BYTE *frame, int length, void *parm){ CHAIN *new, *chain; ICMP_HDR icmpHdr; IP_HDR ipHdr; ICMP_DESCR *descr; BOOLEAN success = FALSE; if (Addr2Host(addr, &(ipHdr.src)) { chain=ChainAlloc(0, frame, length, length, 0, 0); if (chain!=0) { new = HdrDecode(chain, &icmpHdr); if (new != 0) { descr = DescrFind(&icmpHdr, &ipHdr); if(descr) { if(descr->Rcve(descr, chain, &icmpHdr, &ipHdr)) { success = TRUE; } } if (new != chain) ChainFree(new); } ChainFree(chain); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -