⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlpi.c

📁 嵌入式RMON,RMON为Remote monitor的缩写,基于SNMP为网络提供主动监控及错误告警,智能交换路由必备协议
💻 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 <sys/types.h>#include <sys/file.h>#include <sys/socket.h>#include <sys/time.h>#include <sys/dlpi.h>#include <sys/bufmod.h>#include <config.h>   #include <mac.h>#include <chain.h>#include "dlpi.h" extern kstat_ctl_t *kstat;extern kid_t        kstatid;DLPI_DESCR *dlpiDescrList = NULL;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 GetIfnetKstat(DLPI_DESCR *dlpi, CHAR *name, kstat_t **stats, BYTE *buf, INT size);void DlpiExit(void){    DLPI_DESCR      *dlpi;        while (dlpiDescrList!=0)    {        dlpi = dlpiDescrList;        dlpiDescrList = dlpiDescrList->next;                            MacIfaceRemove(dlpi->mac);        NwDgClose(dlpi->nw);        DnpapFree(dlpi);    }}BOOLEAN DlpiInit(void){    BYTE            name[255];    BYTE            addr[255];    CHAR            *descr;    MAC_IFACE       *mac;    DLPI_DESCR      *dlpi;     WORD            i;	kstat_t         *stats = NULL;	BYTE			buf[DLPI_SIZE_IFNET];	NW_IFCONFIG		ifconfig;    static BOOLEAN  init = FALSE;        if (!init)    {        i = 1;        sprintf(name, "dlpi.iface.name.%d", i);        while (ConfigGetString(name, &descr))        {            mac = DnpapMalloc(sizeof(MAC_IFACE));            if (mac!=0)            {                                        dlpi=DnpapMalloc(sizeof(DLPI_DESCR));                 memset(dlpi, 0, sizeof(DLPI_DESCR));                if (dlpi!=0)                {                    dlpi->mac=mac;                                        sprintf(addr,"dlpi:%s:8192:0", descr);                    dlpi->nw=NwDgAccept(addr, Rcve, dlpi);                      if (dlpi->nw != NULL && NwDgIfConfig(dlpi->nw, &ifconfig) == TRUE)                    {						if (GetIfnetKstat(dlpi, descr, &stats, buf, sizeof(buf)) == FALSE)							DnpapMessage(DMC_FATAL, DLPI_ERROR, "dlpi: interface statistics not found");						dlpi->stats       = stats;                        mac->specific      = dlpi;                        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);                     }                                        dlpi->next=dlpiDescrList;                    dlpiDescrList=dlpi;                }            }                            i++;            sprintf(name, "dlpi.iface.name.%d", i);        }                DnpapAtExit(DlpiExit);        init = TRUE;    }    return init;}static void Rcve(NW_DG *nw, char *addr, BYTE *frame, int length, void *parm){    DLPI_DESCR          *dlpi;    CHAIN               chain;    MAC_INFO            info;     BYTE                *p;    struct sb_hdr       *hp;            dlpi = (DLPI_DESCR *)parm;        hp = (struct sb_hdr *)frame;          p = frame + sizeof(struct sb_hdr);        info.copied      = length;     info.length      = hp->sbh_origlen;    info.time        = hp->sbh_timestamp.tv_sec*1000000UL + hp->sbh_timestamp.tv_usec;    info.promiscuous = ((p[0] & (BYTE)0x01)!=(BYTE)0x01 &&                            memcmp(p, dlpi->mac->addr, 6)!=0);    dlpi->Pkts++;    dlpi->Octets += length;    dlpi->DiscardedPkts = hp->sbh_drops;                if (info.length > info.copied)        dlpi->TruncatedPkts++;    if (!info.promiscuous)        dlpi->StackedPkts++;    if (memcmp(p,"\xff\xff\xff\xff\xff\xff",6)==0)        dlpi->BroadcastPkts++;    else if(p[0] & (BYTE)0x01)        dlpi->MulticastPkts++;    if (length < 60)        dlpi->UndersizePkts++;               if (length > 1514)        dlpi->OversizePkts++;               if (ChainAlloc(&chain, p, length, length, 0, 0)==0)        return;      MacRcve(dlpi->mac, &chain, &info);}        static BOOLEAN Send(MAC_IFACE *iface, CHAIN *chain){    DLPI_DESCR  *dlpi;    BYTE       frame[DLPI_MTU];    WORD       length;    BYTE       addr[32];        dlpi=(DLPI_DESCR *)iface->specific;    length = ChainLength(chain);    if (!ChainCopy(chain, frame, length))        return FALSE;                       sprintf(addr,"dlpi:%s", iface->descr);                  dlpi->OutPkts++;    if (memcmp(frame, "\xff\xff\xff\xff\xff\xff", iface->addrLength) == 0)        dlpi->OutBroadcastPkts++;    else if(frame[0] & (BYTE)0x01)        dlpi->OutMulticastPkts++;    if (!NwDgSendTo(0, addr, frame, length))	{		dlpi->OutDiscards++;        return FALSE;	}	dlpi->OutOctets += length;    return TRUE;}static BOOLEAN Stat(MAC_IFACE *iface, MAC_STAT *stats){BYTE buf[DLPI_SIZE_IFNET];kstat_named_t *named;    DLPI_DESCR *dlpi;        dlpi=(DLPI_DESCR *)iface->specific;	if (GetIfnetKstat(dlpi, iface->descr, &dlpi->stats, buf, sizeof(buf)) == FALSE)		DnpapMessage(DMC_FATAL, DLPI_ERROR, "dlpi: failed to get interface data");	else	{		named = (kstat_named_t *)buf;		dlpi->InErrors       = named[dlpi->InErrorsOffset].value.ul;		dlpi->OutErrors      = named[dlpi->OutErrorsOffset].value.ul;		dlpi->Collisions     = named[dlpi->CollisionsOffset].value.ul;		dlpi->Frammings      = named[dlpi->FrammingsOffset].value.ul;		dlpi->CRCAlignErrors = named[dlpi->CRCsOffset].value.ul;	}	    stats->LostPkts         = dlpi->LostPkts      ;    stats->Octets           = dlpi->Octets        ;    stats->Pkts             = dlpi->Pkts          ;    stats->BroadcastPkts    = dlpi->BroadcastPkts ;    stats->MulticastPkts    = dlpi->MulticastPkts ;    stats->CRCAlignErrors   = dlpi->CRCAlignErrors;             stats->UndersizePkts    = dlpi->UndersizePkts ;                stats->OversizePkts     = dlpi->OversizePkts  ;                 stats->Fragments        = dlpi->Fragments     ;    stats->Jabbers          = dlpi->Jabbers       ;             stats->Collisions       = dlpi->Collisions    ;    stats->BufferedPkts     = dlpi->BufferedPkts  ;    stats->StackedPkts      = dlpi->StackedPkts   ;    stats->DiscardedPkts    = dlpi->DiscardedPkts ;    stats->TruncatedPkts    = dlpi->TruncatedPkts ;    stats->inPkts           = dlpi->Pkts          ;	stats->inOctets         = dlpi->Octets        ;    stats->inDiscards       = dlpi->DiscardedPkts ;    stats->inNUcastPkts     = dlpi->BroadcastPkts + dlpi->MulticastPkts;    stats->inUcastPkts      = dlpi->Pkts - stats->inNUcastPkts;    stats->outPkts          = dlpi->OutPkts       ;    stats->outDiscards      = dlpi->OutDiscards   ;	stats->outOctets        = dlpi->OutOctets     ;    stats->outNUcastPkts    = dlpi->OutBroadcastPkts + dlpi->OutMulticastPkts;    stats->outUcastPkts     = dlpi->OutPkts - stats->outNUcastPkts;    stats->inErrors         = dlpi->InErrors      ;    stats->outErrors        = dlpi->OutErrors     ;    stats->Collisions       = dlpi->Collisions    ;    return TRUE;}BOOLEAN GetIfnetKstat(DLPI_DESCR *dlpi, CHAR *name, kstat_t **stats, BYTE *buf, INT size){kstat_t *p;kstat_named_t *named;INT i, n;	if (kstat == NULL || *stats == NULL || kstatid != kstat->kc_chain_id)	{		/* assume module name consists of leading non-numerical characters */		if ((n = strcspn(name, "0123456789")) <= 0)			return FALSE;		for (p = kstat->kc_chain; p != NULL; p = p->ks_next)		{			if (strncmp(p->ks_module, name, n) == 0 &&				strcmp(p->ks_name, name) == 0)				break;		}		if (p == NULL)			return FALSE;		*stats = p;		kstatid = kstat->kc_chain_id;	}	if ((*stats)->ks_type != KSTAT_TYPE_NAMED || (*stats)->ks_data_size > size)		return FALSE;	(*stats)->ks_data = buf;	if (kstat_read(kstat, *stats, NULL) < 0)		return FALSE;	if (dlpi->Offsets == FALSE)	{		named = (*stats)->ks_data;		for (i = 0; i < (*stats)->ks_ndata; i++)		{			if (strcmp(named[i].name, "ipackets") == 0)				dlpi->InPktsOffset = i;			else			if (strcmp(named[i].name, "ierrors") == 0)				dlpi->InErrorsOffset = i;			else			if (strcmp(named[i].name, "opackets") == 0)				dlpi->OutPktsOffset = i;			else			if (strcmp(named[i].name, "oerrors") == 0)				dlpi->OutErrorsOffset = i;			else			if (strcmp(named[i].name, "collisions") == 0)				dlpi->CollisionsOffset = i;			else			if (strcmp(named[i].name, "framming") == 0)				dlpi->FrammingsOffset = i;			else			if (strcmp(named[i].name, "crc") == 0)				dlpi->CRCsOffset = i;		}		dlpi->Offsets = TRUE;	}	return TRUE;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -