📄 ipaddress_ioctl.c
字号:
/* * Interface MIB architecture support * * $Id: ipaddress_ioctl.c 16381 2007-05-17 21:53:28Z hardaker $ */#include <net-snmp/net-snmp-config.h>#include <net-snmp/net-snmp-includes.h>#include "mibII/mibII_common.h"#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/data_access/ipaddress.h>#include <net-snmp/data_access/interface.h>#include "ip-mib/ipAddressTable/ipAddressTable_constants.h"#include "if-mib/data_access/interface_ioctl.h"#include <errno.h>#include <net/if.h>#include <sys/ioctl.h>#include "ipaddress_ioctl.h"static void _print_flags(short flags);#define LIST_TOKEN "ioctl_extras"/* * get extra structure * * @returns the extras structure from the entry */_ioctl_extras *netsnmp_ioctl_ipaddress_extras_get(netsnmp_ipaddress_entry *entry){ if ((NULL == entry) || (NULL == entry->arch_data)) return NULL; return netsnmp_get_list_data(entry->arch_data, LIST_TOKEN);}/** * initialize ioctl extras * * @returns _ioctl_extras pointer, or NULL on error */_ioctl_extras *netsnmp_ioctl_ipaddress_entry_init(netsnmp_ipaddress_entry *entry){ netsnmp_data_list *node; _ioctl_extras *extras; if (NULL == entry) return NULL; extras = SNMP_MALLOC_TYPEDEF(_ioctl_extras); if (NULL == extras) return NULL; node = netsnmp_create_data_list(LIST_TOKEN, extras, free); if (NULL == node) { free(extras); return NULL; } netsnmp_data_list_add_node( &entry->arch_data, node ); return extras;}/** * cleanup ioctl extras */voidnetsnmp_ioctl_ipaddress_entry_cleanup(netsnmp_ipaddress_entry *entry){ if (NULL == entry) { netsnmp_assert(NULL != entry); return; } if (NULL == entry->arch_data) { netsnmp_assert(NULL != entry->arch_data); return; } netsnmp_remove_list_node(&entry->arch_data, LIST_TOKEN);}/** * copy ioctl extras * * @retval 0: success * @retval <0: error */intnetsnmp_ioctl_ipaddress_entry_copy(netsnmp_ipaddress_entry *lhs, netsnmp_ipaddress_entry *rhs){ _ioctl_extras *lhs_extras, *rhs_extras; int rc = SNMP_ERR_NOERROR; if ((NULL == lhs) || (NULL == rhs)) { netsnmp_assert((NULL != lhs) && (NULL != rhs)); return -1; } rhs_extras = netsnmp_ioctl_ipaddress_extras_get(rhs); lhs_extras = netsnmp_ioctl_ipaddress_extras_get(lhs); if (NULL == rhs_extras) { if (NULL != lhs_extras) netsnmp_ioctl_ipaddress_entry_cleanup(lhs); } else { if (NULL == lhs_extras) lhs_extras = netsnmp_ioctl_ipaddress_entry_init(lhs); if (NULL != lhs_extras) memcpy(lhs_extras, rhs_extras, sizeof(_ioctl_extras)); else rc = -1; } return rc;}/** * load ipv4 address via ioctl */int_netsnmp_ioctl_ipaddress_container_load_v4(netsnmp_container *container, int idx_offset){ int i, sd, rc = 0, interfaces = 0; struct ifconf ifc; struct ifreq *ifrp; struct sockaddr save_addr; struct sockaddr_in * si; netsnmp_ipaddress_entry *entry; _ioctl_extras *extras; if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { snmp_log(LOG_ERR, "could not create socket\n"); return -1; } interfaces = netsnmp_access_ipaddress_ioctl_get_interface_count(sd, &ifc); if(interfaces < 0) { close(sd); return -2; } netsnmp_assert(NULL != ifc.ifc_buf); DEBUGMSGTL(("access:ipaddress:container", "processing %d interfaces\n", interfaces)); ifrp = ifc.ifc_req; for(i=0; i < interfaces; ++i, ++ifrp) { DEBUGMSGTL(("access:ipaddress:container", " interface %d, %s\n", i, ifrp->ifr_name)); /* */ entry = netsnmp_access_ipaddress_entry_create(); if(NULL == entry) { rc = -3; break; } entry->ns_ia_index = ++idx_offset; /* * save if name */ extras = netsnmp_ioctl_ipaddress_extras_get(entry); memcpy(extras->name, ifrp->ifr_name, sizeof(extras->name)); /* * each time we make an ioctl, we need to specify the address, but * it will be overwritten in the call. so we save address here. */ save_addr = ifrp->ifr_addr; /* * set indexes */ netsnmp_assert(AF_INET == ifrp->ifr_addr.sa_family); si = (struct sockaddr_in *) &ifrp->ifr_addr; entry->ia_address_len = sizeof(si->sin_addr.s_addr); memcpy(entry->ia_address, &si->sin_addr.s_addr, entry->ia_address_len); /* * get ifindex */ { /* * I think that Linux and Solaris both use ':' in the * interface name for aliases. When a new arch is added * that uses some other indicator, a new function, maybe * netsnmp_access_ipaddress_entry_name_alias_check(), will * need to be written. */ char *ptr = strchr(ifrp->ifr_name, ':'); if (NULL != ptr) { entry->flags |= NETSNMP_ACCESS_IPADDRESS_ISALIAS; *ptr = 0; } } entry->if_index = netsnmp_access_interface_ioctl_ifindex_get(sd, ifrp->ifr_name); if (0 == entry->if_index) { snmp_log(LOG_ERR,"no ifindex found for interface\n"); netsnmp_access_ipaddress_entry_free(entry); continue; } /* * get netmask */ ifrp->ifr_addr = save_addr; if (ioctl(sd, SIOCGIFNETMASK, ifrp) < 0) { snmp_log(LOG_ERR, "error getting netmask for interface %d\n", i); netsnmp_access_ipaddress_entry_free(entry); continue; } netsnmp_assert(AF_INET == ifrp->ifr_addr.sa_family); si = (struct sockaddr_in *) &ifrp->ifr_addr; entry->ia_prefix_len = netsnmp_ipaddress_ipv4_prefix_len(si->sin_addr.s_addr); /* * get flags */ ifrp->ifr_addr = save_addr; if (ioctl(sd, SIOCGIFFLAGS, ifrp) < 0) { snmp_log(LOG_ERR, "error getting if_flags for interface %d\n", i); netsnmp_access_ipaddress_entry_free(entry); continue; } extras->flags = ifrp->ifr_flags; entry->ia_type = IPADDRESSTYPE_UNICAST; /* assume unicast? */ /** entry->ia_prefix_oid ? */ /* * per the MIB: * In the absence of other information, an IPv4 address is * always preferred(1). */ entry->ia_status = IPADDRESSSTATUSTC_PREFERRED; /* * can we figure out if an address is from DHCP? * use manual until then... */ entry->ia_origin = IPADDRESSORIGINTC_MANUAL; DEBUGIF("access:ipaddress:container") { DEBUGMSGT_NC(("access:ipaddress:container", " if %d: addr len %d, index 0x%x\n", i, entry->ia_address_len, entry->if_index)); if (4 == entry->ia_address_len) DEBUGMSGT_NC(("access:ipaddress:container", " address %p\n", *((void**)entry->ia_address))); DEBUGMSGT_NC(("access:ipaddress:container", "flags 0x%x\n", extras->flags)); _print_flags(extras->flags); } /* * add entry to container */ if (CONTAINER_INSERT(container, entry) < 0) { DEBUGMSGTL(("access:ipaddress:container","error with ipaddress_entry: insert into container failed.\n")); netsnmp_access_ipaddress_entry_free(entry); continue; } } /* * clean up */ free(ifc.ifc_buf); close(sd); /* * return number of interfaces seen */ if(rc < 0) return rc; return idx_offset;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -