📄 nit.c
字号:
/* Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group *//* See file COPYING 'GNU General Public Licence' for copyright details */#include <memory.h>#include <string.h>#include <stdio.h>#include <config.h> #include <dkvm.h>#include <mac.h>#include <chain.h>#include <sys/types.h>#include <sys/file.h>#include <sys/ioctl.h>#include <sys/socket.h>#include <sys/time.h>#include <net/if.h>#include <net/nit_if.h>#include <net/nit_pf.h>#include <net/nit_buf.h> #include "nit.h" extern Kvm *kvm;NIT_DESCR *nitDescrList = 0;static void Rcve(NW_DG *nw, char *addr, BYTE *frame, int length, void *parm);static BOOLEAN Stat(MAC_IFACE *iface, MAC_STAT *stats);static BOOLEAN Send(MAC_IFACE *iface, CHAIN *chain);static BOOLEAN GetIfnetOffset(CHAR *name, ULONG *offset);static BOOLEAN GetIfnet(ULONG offset, IFNET *ifnet);void NitExit(void){ NIT_DESCR *nit; while (nitDescrList!=0) { nit = nitDescrList; nitDescrList = nitDescrList->next; MacIfaceRemove(nit->mac); NwDgClose(nit->nw); DnpapFree(nit); }}BOOLEAN NitInit(void){ BYTE name[255]; BYTE addr[255]; CHAR *descr; MAC_IFACE *mac; NIT_DESCR *nit; WORD i; ULONG offset = 0; IFNET ifnet; NW_IFCONFIG ifconfig; static BOOLEAN init = FALSE; if (!init) { i = 1; sprintf(name, "nit.iface.name.%d", i); while (ConfigGetString(name, &descr)) { mac = DnpapMalloc(sizeof(MAC_IFACE)); if (mac!=0) { nit=DnpapMalloc(sizeof(NIT_DESCR)); memset(nit, 0, sizeof(NIT_DESCR)); if (nit!=0) { nit->mac=mac; sprintf(addr,"nit:%s:8192:0", descr); nit->nw=NwDgAccept(addr, Rcve, nit); if (nit->nw != NULL && NwDgIfConfig(nit->nw, &ifconfig) == TRUE) { if (GetIfnetOffset(descr, &offset) == FALSE || GetIfnet(offset, &ifnet) == FALSE) DnpapMessage(DMC_FATAL, NIT_ERROR, "nit: interface not found"); nit->offset = offset; mac->specific = nit; strcpy(mac->descr, descr); mac->Send = Send; mac->Stat = Stat; mac->type = ifconfig.type; mac->speed = ifconfig.speed; mac->mtu = ifconfig.mtu; mac->statusOper = TRUE; mac->statusAdmin = TRUE; mac->arp = MAC_ARP_NONE; memcpy(mac->addr, ifconfig.address, ifconfig.addrlen); memcpy(mac->addrBroadcast, ifconfig.broadcast, ifconfig.addrlen); mac->addrLength = ifconfig.addrlen; mac->frameId = 0; MacIfaceRegister(mac); } nit->next=nitDescrList; nitDescrList=nit; } } i++; sprintf(name, "nit.iface.name.%d", i); } DnpapAtExit(NitExit); init = TRUE; } return init;}static void Rcve(NW_DG *nw, char *addr, BYTE *frame, int length, void *parm){ NIT_DESCR *nit; CHAIN chain; MAC_INFO info; BYTE *p; struct nit_iftime *nitTime; struct nit_ifdrops *nitDrops; struct nit_iflen *nitLen; nit=(NIT_DESCR *)parm; p=frame; nitTime=(struct nit_iftime *)p; p+=sizeof(struct nit_iftime); nitDrops=(struct nit_ifdrops *)p; p+=sizeof(struct nit_ifdrops); nitLen=(struct nit_iflen *)p; p+=sizeof(struct nit_iflen); info.copied = length; info.length = nitLen->nh_pktlen; info.time = nitTime->nh_timestamp.tv_sec*1000000UL + nitTime->nh_timestamp.tv_usec; info.promiscuous = ((p[0] & (BYTE)0x01)!=(BYTE)0x01 && memcmp(p, nit->mac->addr, nit->mac->addrLength)!=0); nit->Pkts++; nit->Octets += length; nit->DiscardedPkts = nitDrops->nh_drops; if (info.length > info.copied) nit->TruncatedPkts++; if (!info.promiscuous) nit->StackedPkts++; if (memcmp(p,"\xff\xff\xff\xff\xff\xff",nit->mac->addrLength)==0) nit->BroadcastPkts++; else if(p[0] & (BYTE)0x01) nit->MulticastPkts++; if (length < 60) nit->UndersizePkts++; if (length > 1514) nit->OversizePkts++; if (ChainAlloc(&chain, p, length, length, 0, 0)==0) return; MacRcve(nit->mac, &chain, &info);} static BOOLEAN Send(MAC_IFACE *iface, CHAIN *chain){ NIT_DESCR *nit; BYTE frame[NIT_MTU]; WORD length; BYTE addr[32]; nit=(NIT_DESCR *)iface->specific; length = ChainLength(chain); if (!ChainCopy(chain, frame, length)) return FALSE; sprintf(addr,"nit:%s", iface->descr); nit->OutPkts++; if (memcmp(frame, "\xff\xff\xff\xff\xff\xff", iface->addrLength) == 0) nit->OutBroadcastPkts++; else if(frame[0] & (BYTE)0x01) nit->OutMulticastPkts++; if (!NwDgSendTo(0, addr, frame, length)) { nit->OutDiscards++; return FALSE; } nit->OutOctets += length; return TRUE;}static BOOLEAN Stat(MAC_IFACE *iface, MAC_STAT *stats){IFNET ifnet; NIT_DESCR *nit; nit=(NIT_DESCR *)iface->specific; if (nit->offset != 0) { if (GetIfnet(nit->offset, &ifnet) == FALSE) DnpapMessage(DMC_FATAL, NIT_ERROR, "nit: failed to get interface data"); else { nit->OutQLen = ifnet.if_snd.ifq_len; nit->InErrors = ifnet.if_ierrors; nit->OutErrors = ifnet.if_oerrors; nit->Collisions = ifnet.if_collisions; } } stats->LostPkts = nit->LostPkts ; stats->Octets = nit->Octets ; stats->Pkts = nit->Pkts ; stats->BroadcastPkts = nit->BroadcastPkts ; stats->MulticastPkts = nit->MulticastPkts ; stats->CRCAlignErrors = nit->CRCAlignErrors; stats->UndersizePkts = nit->UndersizePkts ; stats->OversizePkts = nit->OversizePkts ; stats->Fragments = nit->Fragments ; stats->Jabbers = nit->Jabbers ; stats->BufferedPkts = nit->BufferedPkts ; stats->StackedPkts = nit->StackedPkts ; stats->DiscardedPkts = nit->DiscardedPkts ; stats->TruncatedPkts = nit->TruncatedPkts ; stats->inPkts = nit->Pkts ; stats->inOctets = nit->Octets ; stats->inDiscards = nit->DiscardedPkts ; stats->inNUcastPkts = nit->BroadcastPkts + nit->MulticastPkts; stats->inUcastPkts = nit->Pkts - stats->inNUcastPkts; stats->outPkts = nit->OutPkts ; stats->outDiscards = nit->OutDiscards ; stats->outOctets = nit->OutOctets ; stats->outNUcastPkts = nit->OutBroadcastPkts + nit->OutMulticastPkts; stats->outUcastPkts = nit->OutPkts - stats->outNUcastPkts; stats->outQLen = nit->OutQLen ; stats->inErrors = nit->InErrors ; stats->outErrors = nit->OutErrors ; stats->Collisions = nit->Collisions ; return TRUE;}BOOLEAN GetIfnetOffset(CHAR *name, ULONG *offset){IFNET ifnet, *ifnetaddr;CHAR ifnetname[16];CHAR tmpname[32]; if (KvmReadSym(kvm, "_ifnet", &ifnetaddr, sizeof(IFNET *)) < 0) return FALSE; while (ifnetaddr != NULL) { if (KvmRead(kvm, (ULONG) ifnetaddr, &ifnet, sizeof(IFNET)) < 0) return FALSE; if (KvmRead(kvm, (ULONG) ifnet.if_name, (BYTE *) ifnetname, 16) < 0) return FALSE; sprintf(tmpname, "%.16s%d", ifnetname, ifnet.if_unit); if (strcmp(tmpname, name) == 0) { *offset = (ULONG) ifnetaddr; return TRUE; } ifnetaddr = ifnet.if_next; } *offset = 0; return FALSE;}BOOLEAN GetIfnet(ULONG offset, IFNET *ifnet){ if (KvmRead(kvm, offset, ifnet, sizeof(IFNET)) < 0) return FALSE; return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -