📄 udptable.c
字号:
/* * UDP MIB group Table implementation - udpTable.c * */#include <config.h>#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <sys/types.h>#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_SYS_PARAM_H#include <sys/param.h>#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#if HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#if HAVE_NET_IF_H#include <net/if.h>#endif#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#if HAVE_NETINET_IP_H#include <netinet/ip.h>#endif#if HAVE_SYS_QUEUE_H#include <sys/queue.h>#endif#if HAVE_SYS_SOCKETVAR_H#include <sys/socketvar.h>#endif#if HAVE_NETINET_IP_VAR_H#include <netinet/ip_var.h>#endif#ifdef INET6#if HAVE_NETINET6_IP6_VAR_H#include <netinet6/ip6_var.h>#endif#endif#if HAVE_NETINET_IN_PCB_H#include <netinet/in_pcb.h>#endif#if HAVE_NETINET_UDP_H#include <netinet/udp.h>#endif#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#ifdef solaris2#include "kernel_sunos5.h"#else#include "kernel.h"#endif#ifdef cygwin#define WIN32#include <windows.h>#endif#include "system.h"#include "asn1.h"#include "snmp_debug.h"#include "mibincl.h"#include "auto_nlist.h"#ifdef hpux#include <sys/mib.h>#include <netinet/mib_kern.h>#endif /* hpux */#ifdef linux#include "tcpTable.h"#endif#include "udp.h"#include "udpTable.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);#ifdef hpux11static int UDP_Scan_Next (mib_udpLsnEnt *);#elsestatic int UDP_Scan_Next (struct inpcb *);#endif#endif /********************* * * Initialisation & common implementation functions * *********************/ /********************* * * System specific implementation functions * *********************/#ifndef WIN32 #ifndef solaris2 u_char *var_udpEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ int i; oid newname[MAX_OID_LEN], lowest[MAX_OID_LEN], *op; u_char *cp; int LowState;#ifdef hpux11 static mib_udpLsnEnt udp, Lowudp;#else static struct inpcb inpcb, Lowinpcb;#endif memcpy( (char *)newname,(char *)vp->name, (int)vp->namelen * sizeof(oid)); /* find the "next" pseudo-connection */#ifndef hpux11Again:#endifLowState = -1; /* UDP doesn't have 'State', but it's a useful flag */ UDP_Scan_Init(); for (;;) {#ifdef hpux11 if ((i = UDP_Scan_Next(&udp)) == 0) break; /* Done */ cp = (u_char *)&udp.LocalAddress;#else /* hpux11 */ if ((i = UDP_Scan_Next(&inpcb)) < 0) goto Again; if (i == 0) break; /* Done */ cp = (u_char *)&inpcb.inp_laddr.s_addr;#endif /* hpux11 */ op = newname + 10; *op++ = *cp++; *op++ = *cp++; *op++ = *cp++; *op++ = *cp++; #ifdef hpux11 newname[14] = (unsigned short)udp.LocalPort;#else newname[14] = ntohs(inpcb.inp_lport);#endif if (exact){ if (snmp_oid_compare(newname, 15, name, *length) == 0){ memcpy( (char *)lowest,(char *)newname, 15 * sizeof(oid)); LowState = 0;#ifdef hpux11 Lowudp = udp;#else Lowinpcb = inpcb;#endif break; /* no need to search further */ } } else { if ((snmp_oid_compare(newname, 15, name, *length) > 0) && ((LowState < 0) || (snmp_oid_compare(newname, 15, lowest, 15) < 0))){ /* * if new one is greater than input and closer to input than * previous lowest, save this one as the "next" one. */ memcpy( (char *)lowest,(char *)newname, 15 * sizeof(oid)); LowState = 0;#ifdef hpux11 Lowudp = udp;#else Lowinpcb = inpcb;#endif } } } if (LowState < 0) return(NULL); memcpy( (char *)name,(char *)lowest, ((int)vp->namelen + 10) * sizeof(oid)); *length = vp->namelen + 5; *write_method = 0; *var_len = sizeof(long); switch (vp->magic) { case UDPLOCALADDRESS:#ifdef hpux11 return (u_char *) &Lowudp.LocalAddress;#else return (u_char *) &Lowinpcb.inp_laddr.s_addr;#endif case UDPLOCALPORT:#ifdef hpux11 long_return = (unsigned short)Lowudp.LocalPort;#else long_return = ntohs(Lowinpcb.inp_lport);#endif return (u_char *) &long_return; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_udpEntry\n", vp->magic)); } return NULL;}#else /* solaris2 - udp */static intUDP_Cmp(void *addr, void *ep){ if (memcmp((mib2_udpEntry_t *)ep, (mib2_udpEntry_t *)addr, sizeof(mib2_udpEntry_t)) == 0) return (0); else return (1);}u_char *var_udpEntry(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ oid newname[MAX_OID_LEN], lowest[MAX_OID_LEN], *op; u_char *cp;#define UDP_LISTEN_LENGTH 15#define UDP_LOCADDR_OFF 10#define UDP_LOCPORT_OFF 14 mib2_udpEntry_t Lowentry, Nextentry, entry; req_e req_type; int Found = 0; memset (&Lowentry, 0, sizeof (Lowentry)); memcpy( (char *)newname,(char *)vp->name, vp->namelen * sizeof(oid)); if (*length == UDP_LISTEN_LENGTH) /* Assume that the input name is the lowest */ memcpy( (char *)lowest,(char *)name, UDP_LISTEN_LENGTH * sizeof(oid)); for (Nextentry.udpLocalAddress = (u_long)-1, req_type = GET_FIRST; ; req_type = GET_NEXT) { if (getMibstat(MIB_UDP_LISTEN, &entry, sizeof(mib2_udpEntry_t), req_type, &UDP_Cmp, &entry) != 0) break; if (entry.udpEntryInfo.ue_state != MIB2_UDP_idle) continue; /* we only want to get listen ports */ COPY_IPADDR(cp, (u_char *)&entry.udpLocalAddress, op, newname + UDP_LOCADDR_OFF); newname[UDP_LOCPORT_OFF] = entry.udpLocalPort; if (exact) { if (snmp_oid_compare(newname, UDP_LISTEN_LENGTH, name, *length) == 0){ memcpy( (char *)lowest,(char *)newname, UDP_LISTEN_LENGTH * sizeof(oid)); Lowentry = entry; Found++; break; /* no need to search further */ } } else { if ((snmp_oid_compare(newname, UDP_LISTEN_LENGTH, name, *length) > 0) && ((Nextentry.udpLocalAddress == (u_long)-1) || (snmp_oid_compare(newname, UDP_LISTEN_LENGTH, lowest, UDP_LISTEN_LENGTH) < 0) || (snmp_oid_compare(name, *length, lowest, UDP_LISTEN_LENGTH) == 0))){ /* if new one is greater than input and closer to input than * previous lowest, and is not equal to it, save this one as * the "next" one. */ memcpy( (char *)lowest,(char *)newname, UDP_LISTEN_LENGTH * sizeof(oid)); Lowentry = entry; Found++; } } Nextentry = entry; } if (Found == 0) return(NULL); memcpy((char *)name, (char *)lowest, (vp->namelen + UDP_LISTEN_LENGTH - UDP_LOCADDR_OFF) * sizeof(oid)); *length = vp->namelen + UDP_LISTEN_LENGTH - UDP_LOCADDR_OFF; *write_method = 0; *var_len = sizeof(long); switch (vp->magic) { case UDPLOCALADDRESS: long_return = Lowentry.udpLocalAddress; return (u_char *) &long_return; case UDPLOCALPORT: long_return = Lowentry.udpLocalPort; return (u_char *) &long_return; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_udpEntry\n", vp->magic)); } return NULL;}#endif /* solaris2 - udp */ /********************* * * Internal implementation functions
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -