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

📄 ip.c

📁 wm PNE 3.3 source code, running at more than vxworks6.x version.
💻 C
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/unxagent/sun/ip.c,v 1.2 2001/11/09 21:49:02 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved.  Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** *  Copyright 1988-1997 Epilogue Technology Corporation. *  Copyright 1998 Integrated Systems, Inc. *  All rights reserved. ****************************************************************************//* * $Log: ip.c,v $ * Revision 1.2  2001/11/09 21:49:02  josh * unxagent demo path adjustment, first pass * * Revision 1.1.1.1  2001/11/05 17:49:17  tneale * Tornado shuffle * * Revision 7.6  2001/01/19 22:25:02  paul * Update copyright. * * Revision 7.5  2000/03/17 00:15:22  meister * Update copyright message * * Revision 7.4  1998/02/25 04:58:20  sra * Update copyrights. * * Revision 7.3  1997/03/20 06:54:10  sra * DFARS-safe copyright text.  Zap! * * Revision 7.2  1997/02/25 10:58:16  sra * Update copyright notice, dust under the bed. * * Revision 7.1  1997/01/08 23:01:49  sar * Updated copyright and modified include files to use envoy/h as * appropriate * * Revision 7.0  1996/03/15  22:07:57  sar * Updated revision to 7.0 and copyright to 96 * * Revision 6.0  1995/05/31  21:49:44  sra * Release 6.0. * * Revision 5.5  1995/05/09  17:47:51  sar * Modified the next routine to use OIDC_T's internally and to get rid of * the use of -1 as a tag value allowing us to use the entire 4g range. * * Revision 5.4  1995/05/04  23:08:32  sar * Corrected the type of ipreasmtimeouts. * * Revision 5.3  1995/05/02  23:25:51  sar * Minor cleanup to make compilers happier. * * Revision 5.2  1995/04/28  22:24:48  sar * Dropped the static/dymanic flag from the nextproc_no_instance call * * Revision 5.1  1995/03/21  19:36:16  sar * Updated method routines to use new api & scheme * * Revision 5.0  1994/05/16  16:20:43  sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 4.0  1993/06/24  17:31:48  sar * Updated rev to 4.0 copyright to 93 * and made the if defined(install..) checks be if install * * Revision 3.2  1993/05/13  22:22:28  sar * Added some defines, includes (<sysent.h> <errno.h> <libc.h>), casts * (struct sockaddr *) and changed memfoo to MEMFOO and objidcmp to * llist_cmp to get rid of warnings from the compiler. * * Revision 3.1  1992/09/01  11:37:53  dab * Fixed up for Mach386 * * Revision 3.0  92/04/03  19:53:44  dab * Release 3.0 *  * Revision 2.101  92/02/04  10:46:48  dab * Updated for release 3.0 of SNMP. *  * Revision 2.100  92/02/03  16:46:10  dab * Generic unix SNMP agent. *  *  *    Rev 2.0   31 Mar 1990 15:34:22 * Initial revision. * *//* [clearcase]modification history-------------------01a,19apr05,job  update copyright notices*/#include <stdio.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/auxfuncs.h>#include  <sys/param.h>#include  <sys/types.h>#include  <sys/socket.h>#include  <net/if.h>#include  <net/route.h>#include  <netinet/in.h>#include  <netinet/in_systm.h>#include  <netinet/ip.h>#include  <netinet/in_pcb.h>#include  <netinet/if_ether.h>#include  <netinet/ip_var.h>#include "snmpvars.h"#include "general.h"#define LM_ipForwarding		 1#define LM_ipDefaultTTL		 2#define LM_ipInReceives		 3#define LM_ipInHdrErrors	 4#define LM_ipInAddrErrors	 5#define LM_ipForwDatagrams	 6#define LM_ipInUnknownProtos	 7#define LM_ipInDiscards		 8#define LM_ipInDelivers		 9#define LM_ipOutRequests	10#define LM_ipOutDiscards	11#define LM_ipOutNoRoutes	12#define LM_ipReasmTimeout	13#define LM_ipReasmReqds		14#define LM_ipReasmOKs		15#define LM_ipReasmFails		16#define LM_ipFragOKs		17#define LM_ipFragFails		18#define LM_ipFragCreates	19#define LM_ipRoutingDiscards	23#define LM_ipAdEntAddr		1#define LM_ipAdEntIfIndex	2#define LM_ipAdEntNetMask	3#define LM_ipAdEntBcastAddr	4#define LM_ipAdEntReasmMaxSize	5#define INSTANCE_LEN 4#define CACHE_LIFETIME 4static time_t ip_cache_time = 0;static struct kernel_symbol ipstat_sym = { "_ipstat", 0, 0};static struct ipstat ips;static time_t ipforw_cache_time = 0;#if (defined(INSTALL_on_mach386) && INSTALL_on_mach386)static struct kernel_symbol ipforw = { "_ipforwarding", 0, 0 };#endif#if (defined(INSTALL_on_sunos) && INSTALL_on_sunos)static struct kernel_symbol ipforw = { "_ip_forwarding", 0, 0 };#endif#if (defined(INSTALL_on_netbsd) && INSTALL_on_netbsd)static struct kernel_symbol ipforw = { "_ipforwarding", 0, 0 };#endifstatic int    ipforw_val = 0;staticintread_ip_cache(){time_t		now;if ((time(&now) - ip_cache_time) <= CACHE_LIFETIME)   return 0;ip_cache_time = now;if (find_loc(&ipstat_sym) == 0)  return -1;read_bytes((off_t)ipstat_sym.offset, (char *)&ips,	   sizeof(struct ipstat));return 0;}/****************************************************************************NAME:  get_ip_infoPURPOSE:  Find the appropriate entry in the ip struct and attach information	  from it to the vbp using the getproc_got_* functions.	  If we can't find an entry indicate that by calling getproc_nosuchins.PARAMETERS:	OIDC_T		Last component of the object id leading to			the leaf node in the MIB.  This is usually			the identifier for the particular attribute			in the table.	int		Number of components in the unused part of the			object identifier	OIDC_T *	Unused part of the object identifier	SNMP_PKT_T *	SNMP packet currently being processed.	VB_T *		Variable being processed.RETURNS: void****************************************************************************//*ARGSUSED*/void  get_ip_info(OIDC_T		 last_match,	      int		 tcount,	      OIDC_T		*tlist,	      SNMP_PKT_T	*pktp,	      VB_T		*vbp){time_t now;/* test that we have an instance with a single oid which is equal to 0 */if ((tcount != 1) || (*tlist != 0)) {    getproc_nosuchins(pktp, vbp);    return;    }if (read_ip_cache() == -1) {    getproc_error(pktp, vbp, GEN_ERR);    return;    }switch(last_match) {    case LM_ipForwarding:	if ((time(&now) - ipforw_cache_time) > CACHE_LIFETIME) {	    ipforw_cache_time = now;	    if (find_loc(&ipforw) != 0)		ipforw_val = read_int((off_t)ipforw.offset);	    }	getproc_got_int32(pktp, vbp, (INT_32_T)(ipforw_val == 0 ? 2 : 1));	break;    case LM_ipDefaultTTL:	getproc_got_int32(pktp, vbp, MAXTTL);	break;    case LM_ipInReceives:	getproc_got_uint32(pktp, vbp, (UINT_32_T)ips.ips_total, VT_COUNTER);	break;    case LM_ipInHdrErrors:	getproc_got_uint32(pktp, vbp,			   (UINT_32_T)(ips.ips_badsum + ips.ips_tooshort +				       ips.ips_toosmall + ips.ips_badhlen +				       ips.ips_badlen), VT_COUNTER);	break;    case LM_ipInAddrErrors:	getproc_got_uint32(pktp, vbp, 0, VT_COUNTER);	break;    case LM_ipForwDatagrams:	getproc_got_uint32(pktp, vbp, (UINT_32_T)ips.ips_forward, VT_COUNTER);	break;    case LM_ipInUnknownProtos:    case LM_ipInDiscards:    case LM_ipOutRequests:    case LM_ipOutDiscards:	getproc_got_uint32(pktp, vbp, 0, VT_COUNTER);	break;    case LM_ipInDelivers:	getproc_got_uint32(pktp, vbp,			   (UINT_32_T)(ips.ips_total -				       (ips.ips_badsum + ips.ips_tooshort +					ips.ips_toosmall + ips.ips_badhlen +					ips.ips_badlen + ips.ips_fragments +					ips.ips_forward + ips.ips_cantforward +					ips.ips_redirectsent)),			   VT_COUNTER);	break;    case LM_ipOutNoRoutes:	getproc_got_uint32(pktp, vbp, (UINT_32_T)ips.ips_cantforward,			   VT_COUNTER);	break;    case LM_ipReasmTimeout:	getproc_got_int32(pktp, vbp, (UINT_32_T)ips.ips_fragtimeout);	break;    case LM_ipReasmReqds:	getproc_got_uint32(pktp, vbp, (UINT_32_T)ips.ips_fragments,			   VT_COUNTER);	break;    case LM_ipReasmOKs:	getproc_got_uint32(pktp, vbp, 0, VT_COUNTER);	break;    case LM_ipReasmFails:	getproc_got_uint32(pktp, vbp,			   (UINT_32_T)(ips.ips_fragtimeout +				       ips.ips_fragdropped), VT_COUNTER);	break;    case LM_ipFragOKs:    case LM_ipFragFails:    case LM_ipFragCreates:	getproc_got_uint32(pktp, vbp, 0, VT_COUNTER);	break;    case LM_ipRoutingDiscards:	getproc_got_uint32(pktp, vbp, 0, VT_COUNTER);	break;    }return;}intip_init(){if (find_loc(&ipstat_sym) == 0) return -1;if (find_loc(&ipforw) == 0) return -1;return 0;}/****************************************************************************NAME:  ipAdEntTable_getPURPOSE:  Find the appropriate entry in the ip address entry table struct	  and attach information from it to the vbp using the	  getproc_got_* functions.  If we can't find an entry indicate	  that by calling getproc_nosuchins.PARAMETERS:	OIDC_T		Last component of the object id leading to			the leaf node in the MIB.  This is usually			the identifier for the particular attribute			in the table.	int		Number of components in the unused part of the			object identifier	OIDC_T *	Unused part of the object identifier	SNMP_PKT_T *	SNMP packet currently being processed.	VB_T *		Variable being processed.RETURNS: void****************************************************************************//*ARGSUSED*/void  ipAdEntTable_get(OIDC_T	 last_match,		   int		 tcount,		   OIDC_T	*tlist,		   SNMP_PKT_T	*pktp,		   VB_T		*vbp){struct in_addr	ipaddr;struct lif *	lifp;int indx;/* See if the instance is an acceptable form,   There must be exactly 4 unused components and   the components should be a valid ip address */if ((tcount != INSTANCE_LEN) || oid_to_ip(4, tlist, &ipaddr.s_addr)) {    getproc_nosuchins(pktp, vbp);    return;    }for (lifp = lif, indx = 1; lifp; lifp = lifp->next, indx++) {    read_if(lifp);    if (lifp->ifp->if_addrlist == 0)	continue;    if (ipaddr.s_addr ==	((struct sockaddr_in *)&(lifp->ifp->if_addrlist->ifa_addr))->	sin_addr.s_addr)	break;    }if (lifp == 0) {    getproc_nosuchins(pktp, vbp);    return;    }switch(last_match) {    case LM_ipAdEntAddr:  	getproc_got_ip_address(pktp, vbp, ipaddr.s_addr);	break;    case LM_ipAdEntIfIndex:	getproc_got_int32(pktp, vbp, indx);	break;    case LM_ipAdEntNetMask:  	getproc_got_ip_address(pktp, vbp, lifp->netmask.s_addr);	break;    case LM_ipAdEntBcastAddr:	getproc_got_int32(pktp, vbp, 1);	break;    case LM_ipAdEntReasmMaxSize:	getproc_got_int32(pktp, vbp, 65535);	break;    }return;   }/****************************************************************************NAME:  ipAdEntTable_nextPURPOSE:  Locate the "next" object in the IP Address tablePARAMETERS:	OIDC_T		Last component of the object id leading to			the leaf node in the MIB.  This is usually			the identifier for the particular attribute			in the table.	int		Number of components in the unused part of the			object identifier	OIDC_T *	Unused part of the object identifier	SNMP_PKT_T *	SNMP packet currently being processed.	VB_T *		Variable being processed.RETURNS: void****************************************************************************//*ARGSUSED*/void  ipAdEntTable_next(OIDC_T	 last_match,		    int		 tcount,		    OIDC_T	*tlist,		    SNMP_PKT_T	*pktp,		    VB_T	*vbp){struct	lif *lifp, *best = 0;OIDC_T	bra[INSTANCE_LEN], pra[INSTANCE_LEN];int	i, j, indx = 0;/* Now find the lowest value larger than the given instance */for (lifp = lif, j = 1; lifp; lifp = lifp->next, j++) {    read_if(lifp);    if (lifp->ifp->if_addrlist == 0)	continue;    ip_to_rlist(((struct sockaddr_in *)		 (&(lifp->ifp->if_addrlist->ifa_addr)))->sin_addr.s_addr,		pra);    if (oidcmp2(INSTANCE_LEN, pra, tcount, tlist) <= 0)	continue;    if (!best || (oidorder(pra, bra, INSTANCE_LEN) < 0)) {	best = lifp;	indx = j;	for (i = 0; i < INSTANCE_LEN; i++)	    bra[i] = pra[i];	}    }if (best) {    nextproc_next_instance(pktp, vbp, INSTANCE_LEN, bra);    switch(last_match) {	case LM_ipAdEntAddr:	    getproc_got_ip_address(pktp, vbp,((struct sockaddr_in *)&(best->ifp->if_addrlist->ifa_addr))->sin_addr.s_addr);	    break;	case LM_ipAdEntIfIndex:	    getproc_got_int32(pktp, vbp, indx);	    break;	case LM_ipAdEntNetMask:	    getproc_got_ip_address(pktp, vbp, best->netmask.s_addr);	    break;	case LM_ipAdEntBcastAddr:	    getproc_got_int32(pktp, vbp, 1);	    break;	case LM_ipAdEntReasmMaxSize:	    getproc_got_int32(pktp, vbp, 65535);	    break;	    }    }else    nextproc_no_next(pktp, vbp);return;}

⌨️ 快捷键说明

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