int tcpTable_load(netsnmp_cache *cache, void *vmagic) { int fd; struct nmparms p; int val = 0; unsigned int ulen; int ret; int i; tcpTable_free(NULL, NULL); if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) >= 0) { p.objid = ID_tcpConnNumEnt; p.buffer = (void *) &val; ulen = sizeof(int); p.len = &ulen; if ((ret = get_mib_info(fd, &p)) == 0) tcp_size = val; if (tcp_size > 0) { ulen = (unsigned) tcp_size *sizeof(mib_tcpConnEnt); tcp_head = (mib_tcpConnEnt *) malloc(ulen); p.objid = ID_tcpConnTable; p.buffer = (void *) tcp_head; p.len = &ulen; if ((ret = get_mib_info(fd, &p)) < 0) { tcp_size = 0; } }
int get_if_speed(char *ifstring) { int speed=ERR_IFACE_NO_SPEED, buffer, fd, val, ret = -1; unsigned int len, i; struct nmparms params; mib_ifEntry * if_buf; for (i=0; i <= ifdata.if_amount; i++) { if ((fd = open_mib("/dev/lan", O_RDWR, i, 0)) >= 0) { if ((if_buf = (mib_ifEntry *) malloc (sizeof(mib_ifEntry))) != 0) { params.objid = ID_ifEntry; params.buffer = if_buf; len = sizeof(mib_ifEntry); params.len = &len; if_buf->ifIndex = i+1; if ((ret = get_mib_info(fd, ¶ms)) == 0) { if ( i+1 == ifdata.if_id) if (if_buf->ifOper == 1) speed = if_buf->ifSpeed/1000; else speed ERR_IFACE_DOWN; } } } free(if_buf); close_mib(fd); } return speed; }
static int sigar_get_mib_info(sigar_t *sigar, struct nmparms *parms) { if (sigar->mib < 0) { if ((sigar->mib = open_mib("/dev/ip", O_RDONLY, 0, 0)) < 0) { return errno; } } return get_mib_info(sigar->mib, parms); }
int tcpTable_load(netsnmp_cache *cache, void *vmagic) { int fd; struct nmparms p; int val = 0; unsigned int ulen; int ret; int i; tcpTable_free(NULL, NULL); if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) >= 0) { p.objid = ID_tcpConnNumEnt; p.buffer = (void *) &val; ulen = sizeof(int); p.len = &ulen; if ((ret = get_mib_info(fd, &p)) == 0) tcp_size = val; if (tcp_size > 0) { ulen = (unsigned) tcp_size *sizeof(mib_tcpConnEnt); tcp_head = (mib_tcpConnEnt *) malloc(ulen); p.objid = ID_tcpConnTable; p.buffer = (void *) tcp_head; p.len = &ulen; if ((ret = get_mib_info(fd, &p)) < 0) { tcp_size = 0; } } close_mib(fd); } /* * Count the number of established connections * Probably not actually necessary for HP-UX */ for (i = 0; i < tcp_size; i++) { if (tcp_head[i].State == 5 /* established */ || tcp_head[i].State == 8 /* closeWait */ ) tcp_estab++; } if (tcp_size > 0) { DEBUGMSGTL(("mibII/tcpTable", "Loaded TCP Table (hpux11)\n")); return 0; } DEBUGMSGTL(("mibII/tcpTable", "Failed to load TCP Table (hpux11)\n")); return -1; }
void _getifdata() { int buffer, fd, val, ret = -1; unsigned int len, i; char tmpinterfacestring[sizeof(ifdata.if_name)+1],*strstrmatch; struct nmparms params; mib_ifEntry * if_buf; /* * The interface description is more then the pure devicename. * Let's do some formating to allow a propper pattern matching */ strcpy(tmpinterfacestring,ifdata.if_name); strcat(tmpinterfacestring," "); for (i=0; i <= ifdata.if_amount; i++) { if ((fd = open_mib("/dev/lan", O_RDWR, i, 0)) >= 0) { if ((if_buf = (mib_ifEntry *) malloc (sizeof(mib_ifEntry))) != 0) { params.objid = ID_ifEntry; params.buffer = if_buf; len = sizeof(mib_ifEntry); params.len = &len; if_buf->ifIndex = i+1; if ((ret = get_mib_info(fd, ¶ms)) == 0) { /* * The interface given by the user must start at the * beginning of if_buf->ifDescr. If that's the case, * strstrmatch is equal to if_buf->ifDescr. If not, * strstrmatch might be a subset of if_buf->ifDescr, * or NULL */ strstrmatch = strstr(if_buf->ifDescr, (char *)tmpinterfacestring); if ( strstrmatch && (strcmp(strstrmatch,if_buf->ifDescr)== 0)) { ifdata.if_valid = 1; ifdata.if_id = i+1; break; } } } } free(if_buf); close_mib(fd); } return; }
int arp_loop(arp_t *r, arp_handler callback, void *arg) { struct nmparms nm; struct arp_entry entry; mib_ipNetToMediaEnt arpentries[MAX_ARPENTRIES]; int fd, i, n, ret; if ((fd = open_mib("/dev/ip", O_RDWR, 0 /* XXX */, 0)) < 0) return (-1); nm.objid = ID_ipNetToMediaTable; nm.buffer = arpentries; n = sizeof(arpentries); nm.len = &n; if (get_mib_info(fd, &nm) < 0) { close_mib(fd); return (-1); } close_mib(fd); entry.arp_pa.addr_type = ADDR_TYPE_IP; entry.arp_pa.addr_bits = IP_ADDR_BITS; entry.arp_ha.addr_type = ADDR_TYPE_ETH; entry.arp_ha.addr_bits = ETH_ADDR_BITS; n /= sizeof(*arpentries); ret = 0; for (i = 0; i < n; i++) { if (arpentries[i].Type == INTM_INVALID || arpentries[i].PhysAddr.o_length != ETH_ADDR_LEN) continue; entry.arp_pa.addr_ip = arpentries[i].NetAddr; memcpy(&entry.arp_ha.addr_eth, arpentries[i].PhysAddr.o_bytes, ETH_ADDR_LEN); if ((ret = callback(&entry, arg)) != 0) break; } return (ret); }
int udp_load(netsnmp_cache *cache, void *vmagic) { int fd; struct nmparms p; unsigned int ulen; int ret; int magic = (int) vmagic; if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) < 0) { DEBUGMSGTL(("mibII/udpScalar", "Failed to load UDP object %d (hpux11)\n", magic)); return (-1); /* error */ } switch (magic) { case UDPINDATAGRAMS: p.objid = ID_udpInDatagrams; break; case UDPNOPORTS: p.objid = ID_udpNoPorts; break; case UDPOUTDATAGRAMS: p.objid = ID_udpOutDatagrams; break; case UDPINERRORS: p.objid = ID_udpInErrors; break; default: udpstat = 0; close_mib(fd); return -1; } p.buffer = (void *)&udpstat; ulen = sizeof(UDP_STAT_STRUCTURE); p.len = &ulen; ret = get_mib_info(fd, &p); close_mib(fd); DEBUGMSGTL(("mibII/udpScalar", "%s UDP object %d (hpux11)\n", (ret < 0 ? "Failed to load" : "Loaded"), magic)); return (ret); /* 0: ok, < 0: error */ }
int udpTable_load(netsnmp_cache *cache, void *vmagic) { int fd; struct nmparms p; int val = 0; unsigned int ulen; int ret; udpTable_free(NULL, NULL); if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) >= 0) { p.objid = ID_udpLsnNumEnt; p.buffer = (void *) &val; ulen = sizeof(int); p.len = &ulen; if ((ret = get_mib_info(fd, &p)) == 0) udp_size = val; if (udp_size > 0) { ulen = (unsigned) udp_size *sizeof(mib_udpLsnEnt); udp_head = (mib_udpLsnEnt *) malloc(ulen); p.objid = ID_udpLsnTable; p.buffer = (void *) udp_head; p.len = &ulen; if ((ret = get_mib_info(fd, &p)) < 0) { udp_size = 0; } } close_mib(fd); } if (udp_size > 0) { DEBUGMSGTL(("mibII/udpTable", "Loaded UDP Table\n")); return 0; } DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (hpux11)\n")); return -1; }
int icmp_load(netsnmp_cache *cache, void *vmagic) { int fd; struct nmparms p; unsigned int ulen; int ret; int magic = (int) vmagic; if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) < 0) { DEBUGMSGTL(("mibII/icmp", "Failed to load ICMP object %d (hpux11)\n", magic)); return (-1); /* error */ } switch (magic) { case ICMPINMSGS: p.objid = ID_icmpInMsgs; break; case ICMPINERRORS: p.objid = ID_icmpInErrors; break; case ICMPINDESTUNREACHS: p.objid = ID_icmpInDestUnreachs; break; case ICMPINTIMEEXCDS: p.objid = ID_icmpInTimeExcds; break; case ICMPINPARMPROBS: p.objid = ID_icmpInParmProbs; break; case ICMPINSRCQUENCHS: p.objid = ID_icmpInSrcQuenchs; break; case ICMPINREDIRECTS: p.objid = ID_icmpInRedirects; break; case ICMPINECHOS: p.objid = ID_icmpInEchos; break; case ICMPINECHOREPS: p.objid = ID_icmpInEchoReps; break; case ICMPINTIMESTAMPS: p.objid = ID_icmpInTimestamps; break; case ICMPINTIMESTAMPREPS: p.objid = ID_icmpInTimestampReps; break; case ICMPINADDRMASKS: p.objid = ID_icmpInAddrMasks; break; case ICMPINADDRMASKREPS: p.objid = ID_icmpInAddrMaskReps; break; case ICMPOUTMSGS: p.objid = ID_icmpOutMsgs; break; case ICMPOUTERRORS: p.objid = ID_icmpOutErrors; break; case ICMPOUTDESTUNREACHS: p.objid = ID_icmpOutDestUnreachs; break; case ICMPOUTTIMEEXCDS: p.objid = ID_icmpOutTimeExcds; break; case ICMPOUTPARMPROBS: p.objid = ID_icmpOutParmProbs; break; case ICMPOUTSRCQUENCHS: p.objid = ID_icmpOutSrcQuenchs; break; case ICMPOUTREDIRECTS: p.objid = ID_icmpOutRedirects; break; case ICMPOUTECHOS: p.objid = ID_icmpOutEchos; break; case ICMPOUTECHOREPS: p.objid = ID_icmpOutEchoReps; break; case ICMPOUTTIMESTAMPS: p.objid = ID_icmpOutTimestamps; break; case ICMPOUTTIMESTAMPREPS: p.objid = ID_icmpOutTimestampReps; break; case ICMPOUTADDRMASKS: p.objid = ID_icmpOutAddrMasks; break; case ICMPOUTADDRMASKREPS: p.objid = ID_icmpOutAddrMaskReps; break; default: icmpstat = 0; close_mib(fd); return (0); } p.buffer = (void *)&icmpstat; ulen = sizeof(ICMP_STAT_STRUCTURE); p.len = &ulen; ret = get_mib_info(fd, &p); close_mib(fd); DEBUGMSGTL(("mibII/icmp", "%s ICMP object %d (hpux11)\n", (ret < 0 ? "Failed to load" : "Loaded"), magic)); return (ret); /* 0: ok, < 0: error */ }
long read_udp_stat(UDP_STAT_STRUCTURE * udpstat, int magic) { long ret_value = -1; #if (defined(CAN_USE_SYSCTL) && defined(UDPCTL_STATS)) static int sname[4] = { CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS }; size_t len = sizeof(*udpstat); #endif #ifdef solaris2 static mib2_ip_t ipstat; #endif #ifdef hpux11 int fd; struct nmparms p; unsigned int ulen; if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) < 0) return (-1); /* error */ switch (magic) { case UDPINDATAGRAMS: p.objid = ID_udpInDatagrams; break; case UDPNOPORTS: p.objid = ID_udpNoPorts; break; case UDPOUTDATAGRAMS: p.objid = ID_udpOutDatagrams; break; case UDPINERRORS: p.objid = ID_udpInErrors; break; default: *udpstat = 0; close_mib(fd); return (0); } p.buffer = (void *) udpstat; ulen = sizeof(UDP_STAT_STRUCTURE); p.len = &ulen; ret_value = get_mib_info(fd, &p); close_mib(fd); return (ret_value); /* 0: ok, < 0: error */ #else /* hpux11 */ if (udp_stats_cache_marker && (!atime_ready (udp_stats_cache_marker, UDP_STATS_CACHE_TIMEOUT * 1000))) #ifdef solaris2 return (magic == UDPNOPORTS ? ipstat.udpNoPorts : 0); #else return 0; #endif if (udp_stats_cache_marker) atime_setMarker(udp_stats_cache_marker); else udp_stats_cache_marker = atime_newMarker(); #ifdef linux ret_value = linux_read_udp_stat(udpstat); #endif #ifdef WIN32 ret_value = GetUdpStatistics(udpstat); #endif #ifdef solaris2 if (magic == UDPNOPORTS) { if (getMibstat (MIB_IP, &ipstat, sizeof(mib2_ip_t), GET_FIRST, &Get_everything, NULL) < 0) ret_value = -1; else ret_value = ipstat.udpNoPorts; } else ret_value = getMibstat(MIB_UDP, udpstat, sizeof(mib2_udp_t), GET_FIRST, &Get_everything, NULL); #endif #ifdef HAVE_SYS_TCPIPSTATS_H ret_value = sysmp(MP_SAGET, MPSA_TCPIPSTATS, udpstat, sizeof *udpstat); #endif #if defined(CAN_USE_SYSCTL) && defined(UDPCTL_STATS) ret_value = sysctl(sname, 4, udpstat, &len, 0, 0); #endif #ifdef UDPSTAT_SYMBOL if (auto_nlist(UDPSTAT_SYMBOL, (char *) udpstat, sizeof(*udpstat))) ret_value = 0; #endif if (ret_value == -1) { free(udp_stats_cache_marker); udp_stats_cache_marker = NULL; } return ret_value; #endif /* hpux11 */ }
/* ================================================================================ NAME : InterfaceList Constructor DESCRIPTION : Build the list IP Interfaces ASSUMPTIONS : None PRE-CONDITIONS : POST-CONDITIONS : NOTES : ================================================================================ */ InterfaceList::InterfaceList() { int fd, // file descriptor i, j, // general purpose indicies numif, // number of interfaces numip; // number of IP addresses struct ifconf ifconf; // interface configuration unsigned int len; // length of get_mib_info() buffer struct nmparms parms; // get_mib_info() arguments mib_ipAdEnt * addr_buf; // IP Address Buffer struct in_addr t; // temporary variable for extracting // IP Address Buffer contents struct sockaddr_in *sin; // temporary variable for extracting // interface name #ifdef DEBUG cout << "InterfaceList::InterfaceList()" << endl; #endif // Load the interface name structures. if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { throw CIMOperationFailedException("Error in opening socket: " + String(strerror(errno))); } if (ioctl(fd, SIOCGIFNUM, &numif) < 0) { throw CIMOperationFailedException ("Error in ioctl() request SIOCGIFNUM: " + String(strerror(errno))); } ifconf.ifc_len = numif * sizeof (struct ifreq); ifconf.ifc_req = (struct ifreq *) calloc(numif, sizeof (struct ifreq)); if (ioctl (fd, SIOCGIFCONF, &ifconf) < 0) { free (ifconf.ifc_req); throw CIMOperationFailedException ("Error in ioctl() request SIOCGIFCONF: " + String(strerror(errno))); } close(fd); if ((fd = open_mib("/dev/ip", O_RDONLY, 0, 0)) < 0) { free (ifconf.ifc_req); throw CIMOperationFailedException("Can't open /dev/ip: " + String(strerror(errno))); } parms.objid = ID_ipAddrNumEnt; parms.buffer = (char *) &numip; len = sizeof(numip); parms.len = &len; if (get_mib_info (fd, &parms) < 0) { free (ifconf.ifc_req); throw CIMOperationFailedException( "Can't get ID_ipAddrNumEnt from get_mib_info(): " + String(strerror(errno))); } addr_buf = (mib_ipAdEnt *)malloc(numip*sizeof(mib_ipAdEnt)); if (addr_buf == 0) { free (ifconf.ifc_req); free (addr_buf); throw CIMOperationFailedException( "Error in allocating space for the kernel interface table: " + String(strerror(errno))); } parms.objid = ID_ipAddrTable; parms.buffer = (char *) addr_buf; len = numip * sizeof(mib_ipAdEnt); parms.len = &len; if (get_mib_info (fd, &parms) < 0) { free (ifconf.ifc_req); free (addr_buf); throw CIMOperationFailedException( "Can't get ID_ipAddrTable from get_mib_info(): " + String(strerror(errno))); } // Create the interface list entries for (i=0; i < numip ; i++) { IPInterface _ipif; t.s_addr = addr_buf[i].Addr; _ipif.set_address(inet_ntoa(t)); // ATTN-LEW-2002-07-30: Enhance this to deal with IPv6 too. _ipif.set_protocol(PROTOCOL_IPV4); for (j = 0; j < (int)(numif-1); j++) { sin = reinterpret_cast<struct sockaddr_in*>( &ifconf.ifc_req[j].ifr_addr); if (sin->sin_addr.s_addr == t.s_addr) { _ipif.set_simpleIfName(ifconf.ifc_req[j].ifr_name); } } /* for */ t.s_addr = addr_buf[i].NetMask; _ipif.set_subnetMask(inet_ntoa(t)); _ifl.push_back(_ipif); // Add another IP interface to the list } /* for */ close_mib(fd); free (ifconf.ifc_req); free (addr_buf); #ifdef DEBUG cout << "InterfaceList::InterfaceList() -- done" << endl; #endif }
int get_stat(void) { int i,fd, ret=-1; static int wait_pcks_counter=WAIT_PCKS_COUNTER+1; unsigned int len; unsigned long rx_o, tx_o; struct nmparms params, params2; mib_ifEntry *if_buf; static nmapi_logstat *if_ptr = (nmapi_logstat *) 0; if (ifdata.if_valid == 1 && (fd = open_mib("/dev/lan", O_RDWR, 0, 0)) >= 0) { if ((if_buf = (mib_ifEntry *) malloc (sizeof(mib_ifEntry))) != 0) { if_buf->ifIndex = ifdata.if_id; params.objid = ID_ifEntry; params.buffer = if_buf; len = (unsigned int) sizeof(mib_ifEntry); params.len = &len; if ((ret = get_mib_info(fd, ¶ms)) == 0) { rx_o = stats.rx_bytes; tx_o = stats.tx_bytes; stats.tx_bytes = if_buf->ifOutOctets; stats.rx_bytes = if_buf->ifInOctets; stats.tx_errors = if_buf->ifOutErrors; stats.rx_errors = if_buf->ifInErrors; if (rx_o > stats.rx_bytes) stats.rx_over++; if (tx_o > stats.tx_bytes) stats.tx_over++; } } free(if_buf); /* * Getting the tx/rx packets every run often hurts to much performance * With WAIT_PCKS_COUNTER=15 i save on my system 43% cpu usage.instead of * WAIT_PCKS_COUNTER=0 */ if( wait_pcks_counter > WAIT_PCKS_COUNTER ) { if ((if_ptr = (nmapi_logstat *) malloc(sizeof(nmapi_logstat) * ifdata.if_amount)) != 0 ) { len = (unsigned int) ifdata.if_amount *sizeof(nmapi_logstat); if ((ret = get_logical_stat(if_ptr, &len)) == 0) { for (i=0; i <= ifdata.if_amount; i++) { if(if_ptr[i].ifindex == ifdata.if_id) { stats.tx_packets = if_ptr[i].out_packets; stats.rx_packets = if_ptr[i].in_packets; } } } } free(if_ptr); wait_pcks_counter=0; } else { wait_pcks_counter++; } } close_mib(fd); return(0); }
int tcp_load(netsnmp_cache *cache, void *vmagic) { int fd; struct nmparms p; unsigned int ulen; int ret; int magic = (int) vmagic; if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) < 0) { DEBUGMSGTL(("mibII/tcpScalar", "Failed to load TCP object %d (hpux11)\n", magic)); return (-1); /* error */ } switch (magic) { case TCPRTOALGORITHM: p.objid = ID_tcpRtoAlgorithm; break; case TCPRTOMIN: p.objid = ID_tcpRtoMin; break; case TCPRTOMAX: p.objid = ID_tcpRtoMax; break; case TCPMAXCONN: p.objid = ID_tcpMaxConn; break; case TCPACTIVEOPENS: p.objid = ID_tcpActiveOpens; break; case TCPPASSIVEOPENS: p.objid = ID_tcpPassiveOpens; break; case TCPATTEMPTFAILS: p.objid = ID_tcpAttemptFails; break; case TCPESTABRESETS: p.objid = ID_tcpEstabResets; break; case TCPCURRESTAB: p.objid = ID_tcpCurrEstab; break; case TCPINSEGS: p.objid = ID_tcpInSegs; break; case TCPOUTSEGS: p.objid = ID_tcpOutSegs; break; case TCPRETRANSSEGS: p.objid = ID_tcpRetransSegs; break; case TCPINERRS: p.objid = ID_tcpInErrs; break; case TCPOUTRSTS: p.objid = ID_tcpOutRsts; break; default: tcpstat = 0; close_mib(fd); return -1; } p.buffer = (void *)&tcpstat; ulen = sizeof(TCP_STAT_STRUCTURE); p.len = &ulen; ret = get_mib_info(fd, &p); close_mib(fd); DEBUGMSGTL(("mibII/tcpScalar", "%s TCP object %d (hpux11)\n", (ret < 0 ? "Failed to load" : "Loaded"), magic)); return (ret); /* 0: ok, < 0: error */ }
int ip_load(netsnmp_cache *cache, void *vmagic) { int fd; struct nmparms p; unsigned int ulen; int ret; int magic = (int) vmagic; if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) < 0) { DEBUGMSGTL(("mibII/ip", "Failed to load IP object %d (hpux11)\n", magic)); return (-1); /* error */ } switch (magic) { case IPFORWARDING: p.objid = ID_ipForwarding; break; case IPDEFAULTTTL: p.objid = ID_ipDefaultTTL; break; case IPINRECEIVES: p.objid = ID_ipInReceives; break; case IPINHDRERRORS: p.objid = ID_ipInHdrErrors; break; case IPINADDRERRORS: p.objid = ID_ipInAddrErrors; break; case IPFORWDATAGRAMS: p.objid = ID_ipForwDatagrams; break; case IPINUNKNOWNPROTOS: p.objid = ID_ipInUnknownProtos; break; case IPINDISCARDS: p.objid = ID_ipInDiscards; break; case IPINDELIVERS: p.objid = ID_ipInDelivers; break; case IPOUTREQUESTS: p.objid = ID_ipOutRequests; break; case IPOUTDISCARDS: p.objid = ID_ipOutDiscards; break; case IPOUTNOROUTES: p.objid = ID_ipOutNoRoutes; break; case IPREASMTIMEOUT: p.objid = ID_ipReasmTimeout; break; case IPREASMREQDS: p.objid = ID_ipReasmReqds; break; case IPREASMOKS: p.objid = ID_ipReasmOKs; break; case IPREASMFAILS: p.objid = ID_ipReasmFails; break; case IPFRAGOKS: p.objid = ID_ipFragOKs; break; case IPFRAGFAILS: p.objid = ID_ipFragFails; break; case IPFRAGCREATES: p.objid = ID_ipFragCreates; break; case IPROUTEDISCARDS: p.objid = ID_ipRoutingDiscards; break; default: ipstat = 0; close_mib(fd); return (0); } p.buffer = (void *)&ipstat; ulen = sizeof(IP_STAT_STRUCTURE); p.len = &ulen; ret = get_mib_info(fd, &p); close_mib(fd); DEBUGMSGTL(("mibII/ip", "%s IP object %d (hpux11)\n", (ret < 0 ? "Failed to load" : "Loaded"), magic)); return (ret); /* 0: ok, < 0: error */ }
/* ================================================================================ NAME : RouteList Constructor DESCRIPTION : Build the list of IP Routes ASSUMPTIONS : None PRE-CONDITIONS : POST-CONDITIONS : NOTES : ================================================================================ */ RouteList::RouteList() { int fd, // file descriptor i, // general purpose indicies count; // number of raw IP Routes struct nmparms parms; // get_mib_info() arguments mib_ipRouteEnt * route_buf; // IP Route Buffer unsigned int len; // length of get_mib_info() buffer struct in_addr t; // temporary variable for extracting // IP route buffer contents #ifdef DEBUG cout << "RouteList::RouteList()" << endl; #endif // Load the interface name structures. if ((fd = open_mib("/dev/ip", O_RDONLY, 0, 0)) < 0) { throw CIMOperationFailedException("Can't open /dev/ip: " + String(strerror(errno))); } parms.objid = ID_ipRouteNumEnt; parms.buffer = (char *) &count; len = sizeof(count); parms.len = (unsigned int *) &len; if (get_mib_info (fd, &parms) < 0) { throw CIMOperationFailedException( "Can't get ID_ipRouteNumEnt from get_mib_info(): " + String(strerror(errno))); } route_buf = (mib_ipRouteEnt *)malloc(count*sizeof(mib_ipRouteEnt)); if (route_buf == 0) { free (route_buf); throw CIMOperationFailedException( "Error in allocating space for the kernel interface table: " + String(strerror(errno))); } parms.objid = ID_ipRouteTable; parms.buffer = (char *) route_buf; len = count * sizeof(mib_ipRouteEnt); parms.len = &len; if (get_mib_info (fd, &parms) < 0) { free (route_buf); throw CIMOperationFailedException( "Can't get ID_ipRouteTable from get_mib_info(): " + String(strerror(errno))); } // Create the IP Route List entries for (i=0; i < count ; i++) { IPRoute _ipr; // check to see that this is a valid type to represent if (route_buf[i].Type == 3 || route_buf[i].Type == 4) { t.s_addr = route_buf[i].Dest; _ipr.set_destAddr(inet_ntoa(t)); t.s_addr = route_buf[i].Mask; _ipr.set_destMask(inet_ntoa(t)); t.s_addr = route_buf[i].NextHop; _ipr.set_nextHop(inet_ntoa(t)); // ATTN-LEW-2002-09-13: Enhance this to deal with IPv6 too. _ipr.set_protocolType(PROTOCOL_IPV4); _iprl.push_back(_ipr); // Add another IP Route to the list } } /* for */ close_mib(fd); free (route_buf); #ifdef DEBUG cout << "RouteList::RouteList() -- done" << endl; #endif }
static void ARP_Scan_Init(void) { #ifndef NETSNMP_CAN_USE_SYSCTL #ifndef linux #ifdef hpux11 int fd; struct nmparms p; int val; unsigned int ulen; int ret; if (at) free(at); at = (mib_ipNetToMediaEnt *) 0; arptab_size = 0; if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) >= 0) { p.objid = ID_ipNetToMediaTableNum; p.buffer = (void *) &val; ulen = sizeof(int); p.len = &ulen; if ((ret = get_mib_info(fd, &p)) == 0) arptab_size = val; if (arptab_size > 0) { ulen = (unsigned) arptab_size *sizeof(mib_ipNetToMediaEnt); at = (mib_ipNetToMediaEnt *) malloc(ulen); p.objid = ID_ipNetToMediaTable; p.buffer = (void *) at; p.len = &ulen; if ((ret = get_mib_info(fd, &p)) < 0) arptab_size = 0; } close_mib(fd); } arptab_current = 0; #else /* hpux11 */ if (!at) { #ifdef ARPTAB_SIZE_SYMBOL auto_nlist(ARPTAB_SIZE_SYMBOL, (char *) &arptab_size, sizeof arptab_size); #ifdef STRUCT_ARPHD_HAS_AT_NEXT at = (struct arphd *) malloc(arptab_size * sizeof(struct arphd)); #else at = (struct arptab *) malloc(arptab_size * sizeof(struct arptab)); #endif #else return; #endif } #ifdef STRUCT_ARPHD_HAS_AT_NEXT auto_nlist(ARPTAB_SYMBOL, (char *) at, arptab_size * sizeof(struct arphd)); at_ptr = at[0].at_next; #else auto_nlist(ARPTAB_SYMBOL, (char *) at, arptab_size * sizeof(struct arptab)); #endif arptab_current = 0; #endif /* hpux11 */ #else /* linux */ static time_t tm = 0; /* Time of last scan */ FILE *in; int i, j; char line[128]; int za, zb, zc, zd; char ifname[21]; char mac[3*MAX_MAC_ADDR_LEN+1]; char *tok; arptab_current = 0; /* Anytime this is called we need to reset 'current' */ if (time(NULL) < tm + 1) { /*Our cool one second cache implementation :-) */ return; } in = fopen("/proc/net/arp", "r"); if (!in) { snmp_log(LOG_ERR, "snmpd: Cannot open /proc/net/arp\n"); arptab_size = 0; return; } /* * Get rid of the header line */ fgets(line, sizeof(line), in); i = 0; while (fgets(line, sizeof(line), in)) { u_long tmp_a; int tmp_flags; if (i >= arptab_curr_max_size) { struct arptab *newtab = (struct arptab *) realloc(at, (sizeof(struct arptab) * (arptab_curr_max_size + ARP_CACHE_INCR))); if (newtab == at) { snmp_log(LOG_ERR, "Error allocating more space for arpcache. " "Cache will continue to be limited to %d entries", arptab_curr_max_size); break; } else { arptab_curr_max_size += ARP_CACHE_INCR; at = newtab; } } if (7 != sscanf(line, "%d.%d.%d.%d 0x%*x 0x%x %s %*[^ ] %20s\n", &za, &zb, &zc, &zd, &tmp_flags, mac, ifname)) { snmp_log(LOG_ERR, "Bad line in /proc/net/arp: %s", line); continue; } /* * Invalidated entries have their flag set to 0. * * We want to ignore them */ if (tmp_flags == 0) { continue; } ifname[sizeof(ifname)-1] = 0; /* make sure name is null terminated */ at[i].at_flags = tmp_flags; tmp_a = ((u_long) za << 24) | ((u_long) zb << 16) | ((u_long) zc << 8) | ((u_long) zd); at[i].at_iaddr.s_addr = htonl(tmp_a); at[i].if_index = netsnmp_access_interface_index_find(ifname); for (j=0,tok=strtok(mac, ":"); tok != NULL; tok=strtok(NULL, ":"),j++) { at[i].at_enaddr[j] = strtol(tok, NULL, 16); } at[i].at_enaddr_len = j; i++; } arptab_size = i; fclose(in); time(&tm); #endif /* linux */ #else /* NETSNMP_CAN_USE_SYSCTL */ int mib[6]; size_t needed; mib[0] = CTL_NET; mib[1] = PF_ROUTE; mib[2] = 0; mib[3] = AF_INET; mib[4] = NET_RT_FLAGS; mib[5] = RTF_LLINFO; if (at) free(at); rtnext = lim = at = 0; if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) snmp_log_perror("route-sysctl-estimate"); else { if ((at = malloc(needed ? needed : 1)) == NULL) snmp_log_perror("malloc"); else { if (sysctl(mib, 6, at, &needed, NULL, 0) < 0) snmp_log_perror("actual retrieval of routing table"); else { lim = at + needed; rtnext = at; } } } #endif /* NETSNMP_CAN_USE_SYSCTL */ }
long read_ip_stat(IP_STAT_STRUCTURE * ipstat, int magic) { long ret_value = 0; #if (defined(CAN_USE_SYSCTL) && defined(IPCTL_STATS)) int i; #endif #if !(defined (linux) || defined(solaris2)) static int ttl, forward; #endif #ifdef hpux11 int fd; struct nmparms p; unsigned int ulen; #endif #if (defined(CAN_USE_SYSCTL) && defined(IPCTL_STATS)) static int sname[4] = { CTL_NET, PF_INET, IPPROTO_IP, 0 }; size_t len; #endif #ifdef hpux11 if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) < 0) return (-1); /* error */ switch (magic) { case IPFORWARDING: p.objid = ID_ipForwarding; break; case IPDEFAULTTTL: p.objid = ID_ipDefaultTTL; break; case IPINRECEIVES: p.objid = ID_ipInReceives; break; case IPINHDRERRORS: p.objid = ID_ipInHdrErrors; break; case IPINADDRERRORS: p.objid = ID_ipInAddrErrors; break; case IPFORWDATAGRAMS: p.objid = ID_ipForwDatagrams; break; case IPINUNKNOWNPROTOS: p.objid = ID_ipInUnknownProtos; break; case IPINDISCARDS: p.objid = ID_ipInDiscards; break; case IPINDELIVERS: p.objid = ID_ipInDelivers; break; case IPOUTREQUESTS: p.objid = ID_ipOutRequests; break; case IPOUTDISCARDS: p.objid = ID_ipOutDiscards; break; case IPOUTNOROUTES: p.objid = ID_ipOutNoRoutes; break; case IPREASMTIMEOUT: p.objid = ID_ipReasmTimeout; break; case IPREASMREQDS: p.objid = ID_ipReasmReqds; break; case IPREASMOKS: p.objid = ID_ipReasmOKs; break; case IPREASMFAILS: p.objid = ID_ipReasmFails; break; case IPFRAGOKS: p.objid = ID_ipFragOKs; break; case IPFRAGFAILS: p.objid = ID_ipFragFails; break; case IPFRAGCREATES: p.objid = ID_ipFragCreates; break; case IPROUTEDISCARDS: p.objid = ID_ipRoutingDiscards; break; default: *ipstat = 0; close_mib(fd); return (0); } p.buffer = (void *) ipstat; ulen = sizeof(IP_STAT_STRUCTURE); p.len = &ulen; ret_value = get_mib_info(fd, &p); close_mib(fd); return (ret_value); /* 0: ok, < 0: error */ #else /* hpux11 */ if (ip_stats_cache_marker && (!atime_ready (ip_stats_cache_marker, IP_STATS_CACHE_TIMEOUT * 1000))) #if !(defined(linux) || defined(solaris2)) return ((magic == IPFORWARDING ? forward : (magic == IPDEFAULTTTL ? ttl : 0))); #else return 0; #endif if (ip_stats_cache_marker) atime_setMarker(ip_stats_cache_marker); else ip_stats_cache_marker = atime_newMarker(); #ifdef linux ret_value = linux_read_ip_stat(ipstat); #endif #ifdef solaris2 ret_value = getMibstat(MIB_IP, ipstat, sizeof(mib2_ip_t), GET_FIRST, &Get_everything, NULL); #endif #ifdef WIN32 ret_value = GetIpStatistics(ipstat); #endif #if !(defined(linux) || defined(solaris2) || defined(WIN32)) if (magic == IPFORWARDING) { #if defined(CAN_USE_SYSCTL) && defined(IPCTL_STATS) len = sizeof i; sname[3] = IPCTL_FORWARDING; if (sysctl(sname, 4, &i, &len, 0, 0) < 0) forward = -1; else forward = (i ? 1 /* GATEWAY */ : 2 /* HOST */ ); #else if (!auto_nlist (IP_FORWARDING_SYMBOL, (char *) &ret_value, sizeof(ret_value))) forward = -1; else forward = (ret_value ? 1 /* GATEWAY */ : 2 /* HOST */ ); #endif if (forward == -1) { free(ip_stats_cache_marker); ip_stats_cache_marker = NULL; } return forward; } if (magic == IPDEFAULTTTL) { #if (defined(CAN_USE_SYSCTL) && defined(IPCTL_STATS)) len = sizeof i; sname[3] = IPCTL_DEFTTL; if (sysctl(sname, 4, &i, &len, 0, 0) < 0) ttl = -1; else ttl = i; #else if (!auto_nlist (TCP_TTL_SYMBOL, (char *) &ret_value, sizeof(ret_value))) ttl = -1; else ttl = ret_value; #endif if (ttl == -1) { free(ip_stats_cache_marker); ip_stats_cache_marker = NULL; } return ttl; } #ifdef HAVE_SYS_TCPIPSTATS_H ret_value = sysmp(MP_SAGET, MPSA_TCPIPSTATS, ipstat, sizeof *ipstat); #endif #if (defined(CAN_USE_SYSCTL) && defined(IPCTL_STATS)) len = sizeof *ipstat; sname[3] = IPCTL_STATS; ret_value = sysctl(sname, 4, ipstat, &len, 0, 0); #endif #ifdef IPSTAT_SYMBOL if (auto_nlist(IPSTAT_SYMBOL, (char *) ipstat, sizeof(*ipstat))) ret_value = 0; #endif #endif /* !(defined(linux) || defined(solaris2)) */ if (ret_value == -1) { free(ip_stats_cache_marker); ip_stats_cache_marker = NULL; } return ret_value; #endif /* hpux11 */ }