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

📄 udp.c

📁 非常不错的网管开发包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  UDP MIB group implementation - udp.c * */#include <config.h>#if HAVE_STRING_H#include <string.h>#endif#if HAVE_STDLIB_H#include <stdlib.h>#endif#include <sys/types.h>#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#include <sys/socket.h>#if TIME_WITH_SYS_TIME# ifdef WIN32#  include <sys/timeb.h># else#  include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H#  include <sys/time.h># else#  include <time.h># endif#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_SYS_SYSMP_H#include <sys/sysmp.h>#endif#if HAVE_SYS_TCPIPSTATS_H#include <sys/tcpipstats.h>#endif#if defined(IFNET_NEEDS_KERNEL) && !defined(_KERNEL)#define _KERNEL 1#define _I_DEFINED_KERNEL#endif#include <net/if.h>#if HAVE_NET_IF_VAR_H#include <net/if_var.h>#endif#ifdef _I_DEFINED_KERNEL#undef _KERNEL#endif#if HAVE_SYS_STREAM_H#include <sys/stream.h>#endif#if HAVE_NET_ROUTE_H#include <net/route.h>#endif#if HAVE_NETINET_IN_SYSTM_H#include <netinet/in_systm.h>#endif#include <netinet/ip.h>#if HAVE_SYS_QUEUE_H#include <sys/queue.h>#endif#if HAVE_SYS_SOCKETVAR_H#include <sys/socketvar.h>#endif#if HAVE_NETINET_IN_PCB_H#include <netinet/in_pcb.h>#endif#if HAVE_NETINET_IP_VAR_H#include <netinet/ip_var.h>#endif#include <netinet/udp.h>#if HAVE_NETINET_UDP_VAR_H#include <netinet/udp_var.h>#endif#if HAVE_INET_MIB2_H#include <inet/mib2.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include "mibincl.h"#ifdef solaris2#include "kernel_sunos5.h"#else#include "kernel.h"#endif#include "system.h"#include "asn1.h"#include "snmp_debug.h"#include "auto_nlist.h"#ifdef hpux#include <sys/mib.h>#include <netinet/mib_kern.h>#endif /* hpux *//* #include "../common_header.h" */#ifdef linux#include "tcp.h"#endif#include "udp.h"#include "sysORTable.h"#ifdef CAN_USE_SYSCTL#include <sys/sysctl.h>#endif	/*********************	 *	 *  Kernel & interface information,	 *   and internal forward declarations	 *	 *********************/#ifndef solaris2static void UDP_Scan_Init (void);static int UDP_Scan_Next (struct inpcb *);#endif#ifdef linuxstatic void linux_read_udp_stat (struct udp_mib *);#endif	/*********************	 *	 *  Initialisation & common implementation functions	 *	 *********************/struct variable8 udp_variables[] = {    {UDPINDATAGRAMS, ASN_COUNTER, RONLY, var_udp, 1, {1}},    {UDPNOPORTS, ASN_COUNTER, RONLY, var_udp, 1, {2}},    {UDPINERRORS, ASN_COUNTER, RONLY, var_udp, 1, {3}},    {UDPOUTDATAGRAMS, ASN_COUNTER, RONLY, var_udp, 1, {4}},    {UDPLOCALADDRESS, ASN_IPADDRESS, RONLY, var_udpEntry, 3, {5, 1, 1}},    {UDPLOCALPORT, ASN_INTEGER, RONLY, var_udpEntry, 3, {5, 1, 2}}};/* Define the OID pointer to the top of the mib tree that we're   registering underneath, and the OID for the MIB module */oid udp_variables_oid[] = { SNMP_OID_MIB2,7 };oid udp_module_oid[]    = { SNMP_OID_MIB2,50 };void init_udp(void){  /* register ourselves with the agent to handle our mib tree */  REGISTER_MIB("mibII/udp", udp_variables, variable8, udp_variables_oid);  REGISTER_SYSOR_ENTRY( udp_module_oid,		"The MIB module for managing UDP implementations");#ifdef UDPSTAT_SYMBOL  auto_nlist( UDPSTAT_SYMBOL,0,0 );#endif#ifdef UDB_SYMBOL  auto_nlist( UDB_SYMBOL,0,0 );#endif}/*  header_udp(...  Arguments:  vp	  IN      - pointer to variable entry that points here  name    IN/OUT  - IN/name requested, OUT/name found  length  IN/OUT  - length of IN/OUT oid's   exact   IN      - TRUE if an exact match was requested  var_len OUT     - length of variable or 0 if function returned  write_method  */static intheader_udp(struct variable *vp,	   oid *name,	   size_t *length,	   int exact,	   size_t *var_len,	   WriteMethod **write_method){#define UDP_NAME_LENGTH	8    oid newname[MAX_OID_LEN];    int result;    DEBUGMSGTL(("mibII/udp", "var_udp: "));    DEBUGMSGOID(("mibII/udp", name, *length));    DEBUGMSG(("mibII/udp"," %d\n", exact));    memcpy( (char *)newname,(char *)vp->name, (int)vp->namelen * sizeof(oid));    newname[UDP_NAME_LENGTH] = 0;    result = snmp_oid_compare(name, *length, newname, (int)vp->namelen + 1);    if ((exact && (result != 0)) || (!exact && (result >= 0)))        return(MATCH_FAILED);    memcpy( (char *)name,(char *)newname, ((int)vp->namelen + 1) * sizeof(oid));    *length = vp->namelen + 1;    *write_method = 0;    *var_len = sizeof(long);	/* default to 'long' results */    return(MATCH_SUCCEEDED);}	/*********************	 *	 *  System specific implementation functions	 *	 *********************/#ifndef solaris2 #ifndef MIB_UDPCOUNTER_SYMBOL#ifndef HAVE_SYS_TCPIPSTATS_Hu_char *var_udp(struct variable *vp,	oid *name,	size_t *length,	int exact,	size_t *var_len,	WriteMethod **write_method){#ifdef linux    static struct udp_mib udpstat;#else    static struct udpstat udpstat;#endif    if (header_udp(vp, name, length, exact, var_len, write_method) == MATCH_FAILED )	return NULL;    /*     *        Get the UDP statistics from the kernel...     */#if !defined(CAN_USE_SYSCTL) || !defined(UDPCTL_STATS)#ifndef linux    auto_nlist(UDPSTAT_SYMBOL, (char *)&udpstat, sizeof (udpstat));#else    linux_read_udp_stat(&udpstat);#endif#else /* can use sysctl(3) and have net.inet.udp.stats */    {	    static int sname[] 		    = { CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS };	    size_t len;	    len = sizeof udpstat;	    if (sysctl(sname, 4, &udpstat, &len, 0, 0) < 0)		    return NULL;    }#endif    switch (vp->magic) {	case UDPINDATAGRAMS:#if defined(freebsd2) || defined(netbsd1) || defined(openbsd2)	    long_return = udpstat.udps_ipackets;#else#if defined(linux)	    long_return = udpstat.UdpInDatagrams;#else#if NO_DUMMY_VALUES	    return NULL;#endif	    long_return = 0;#endif#endif	    return (u_char *) &long_return;	case UDPNOPORTS:#if defined(freebsd2) || defined(netbsd1) || defined(openbsd2)	    long_return = udpstat.udps_noport;#else#if defined(linux)	    long_return = udpstat.UdpNoPorts;#else#if NO_DUMMY_VALUES	    return NULL;#endif	    long_return = 0;#endif#endif	    return (u_char *) &long_return;	case UDPOUTDATAGRAMS:#if defined(freebsd2) || defined(netbsd1) || defined(openbsd2)	    long_return = udpstat.udps_opackets;#else#if defined(linux)	    long_return = udpstat.UdpOutDatagrams;#else#if NO_DUMMY_VALUES	    return NULL;#endif	    long_return = 0;#endif#endif	    return (u_char *) &long_return;	case UDPINERRORS:#ifndef linux	    long_return = udpstat.udps_hdrops + udpstat.udps_badsum +#ifdef STRUCT_UDPSTAT_HAS_UDPS_DISCARD                      + udpstat.udps_discard +#endif			  udpstat.udps_badlen;	    return (u_char *) &long_return;#else /* linux */      	    return (u_char *) &udpstat.UdpInErrors;#endif /* linux */	default:	    DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_udp\n", vp->magic));    }    return NULL;}#else /* HAVE_SYS_TCPIPSTATS_H */u_char *var_udp(struct variable *vp,	oid *name,	size_t *length,	int exact,	size_t *var_len,	WriteMethod **write_method){    static struct kna tcpipstats;    if (header_udp(vp, name, length, exact, var_len, write_method) == MATCH_FAILED )	return NULL;    if (sysmp (MP_SAGET, MPSA_TCPIPSTATS, &tcpipstats, sizeof tcpipstats) == -1) {	snmp_log_perror ("sysmp(MP_SAGET)(MPSA_TCPIPSTATS)");    }#define udpstat tcpipstats.udpstat    switch (vp->magic){	case UDPINDATAGRAMS:	    long_return = udpstat.udps_ipackets;	    return (u_char *) &long_return;	case UDPNOPORTS:	    long_return = udpstat.udps_noport;	    return (u_char *) &long_return;	case UDPOUTDATAGRAMS:	    long_return = udpstat.udps_opackets;	    return (u_char *) &long_return;	case UDPINERRORS:	    long_return = udpstat.udps_hdrops + udpstat.udps_badsum +	      udpstat.udps_badlen;	    return (u_char *) &long_return;	default:	    DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_udp\n", vp->magic));    }    return NULL;}#endif /* HAVE_SYS_TCPIPSTATS_H */#else /* hpux */u_char *var_udp(struct variable *vp,	oid *name,	size_t *length,	int exact,	size_t *var_len,	WriteMethod **write_method){    static struct udpstat udpstat;    static	counter MIB_udpcounter[MIB_udpMAXCTR+1];    if (header_udp(vp, name, length, exact, var_len, write_method) == MATCH_FAILED )	return NULL;

⌨️ 快捷键说明

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