📄 ipaddress_ioctl.c
字号:
}/** * find unused alias number */static int_next_alias(const char *if_name){ int i, j, k, sd, interfaces = 0, len; struct ifconf ifc; struct ifreq *ifrp; char *alias; int *alias_list; if (NULL == if_name) return -1; len = strlen(if_name); 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)); alias_list = malloc(interfaces * sizeof(int)); if (NULL == alias_list) { close(sd); return -2; } ifrp = ifc.ifc_req; for(i=0,j=0; i < interfaces; ++i, ++ifrp) { if (strncmp(ifrp->ifr_name, if_name, len) != 0) continue; DEBUGMSGTL(("access:ipaddress:container", " interface %d, %s\n", i, ifrp->ifr_name)); alias = strchr(ifrp->ifr_name, ':'); if (NULL == alias) continue; ++alias; /* skip ':' */ alias_list[j++] = atoi(alias); } /* * clean up */ free(ifc.ifc_buf); close(sd); /* * return first unused alias */ for(i=1; i<=interfaces; ++i) { for(k=0;k<j;++k) if (alias_list[k] == i) break; if (k == j) return i; } return interfaces + 1;}/** * * @retval 0 : no error * @retval -1 : bad parameter * @retval -2 : couldn't create socket * @retval -3 : ioctl failed */int_netsnmp_ioctl_ipaddress_set_v4(netsnmp_ipaddress_entry * entry){ struct ifreq ifrq; struct sockaddr_in *sin; int rc, fd = -1; _ioctl_extras *extras; if (NULL == entry) return -1; netsnmp_assert(4 == entry->ia_address_len); extras = netsnmp_ioctl_ipaddress_extras_get(entry); if (NULL == extras) return -1; fd = socket(AF_INET, SOCK_DGRAM, 0); if(fd < 0) { snmp_log(LOG_ERR,"couldn't create socket\n"); return -2; } memset(&ifrq, 0, sizeof(ifrq)); if ('\0' == extras->name[0]) { const char *name = netsnmp_access_interface_name_find(entry->if_index); int alias_idx; if (NULL == name) { DEBUGMSGT(("access:ipaddress:set", "cant find name for index %d\n", entry->if_index)); close(fd); return -1; } /* * search for unused alias */ alias_idx = _next_alias(name); snprintf(ifrq.ifr_name,sizeof(ifrq.ifr_name), "%s:%d", name, alias_idx); } else strncpy(ifrq.ifr_name, extras->name, sizeof(ifrq.ifr_name)); ifrq.ifr_name[ sizeof(ifrq.ifr_name)-1 ] = 0; sin = (struct sockaddr_in*)&ifrq.ifr_addr; sin->sin_family = AF_INET; memcpy(&sin->sin_addr.s_addr, entry->ia_address, entry->ia_address_len); rc = ioctl(fd, SIOCSIFADDR, &ifrq); close(fd); if(rc < 0) { snmp_log(LOG_ERR,"error setting address\n"); return -3; } return 0;}/** * * @retval 0 : no error * @retval -1 : bad parameter * @retval -2 : couldn't create socket * @retval -3 : ioctl failed */int_netsnmp_ioctl_ipaddress_delete_v4(netsnmp_ipaddress_entry * entry){ struct ifreq ifrq; int rc, fd = -1; _ioctl_extras *extras; if (NULL == entry) return -1; netsnmp_assert(4 == entry->ia_address_len); extras = netsnmp_ioctl_ipaddress_extras_get(entry); if (NULL == extras) return -1; fd = socket(AF_INET, SOCK_DGRAM, 0); if(fd < 0) { snmp_log(LOG_ERR,"couldn't create socket\n"); return -2; } memset(&ifrq, 0, sizeof(ifrq)); strncpy(ifrq.ifr_name, extras->name, sizeof(ifrq.ifr_name)); ifrq.ifr_name[ sizeof(ifrq.ifr_name)-1 ] = 0; ifrq.ifr_flags = 0; rc = ioctl(fd, SIOCSIFFLAGS, &ifrq); close(fd); if(rc < 0) { snmp_log(LOG_ERR,"error deleting address\n"); return -3; } return 0;}/** * get the interface count and populate the ifc_buf * * Note: the caller assumes responsbility for the ifc->ifc_buf * memory, and should free() it when done. * * @retval -1 : malloc error */intnetsnmp_access_ipaddress_ioctl_get_interface_count(int sd, struct ifconf * ifc){ int lastlen = 0, i; struct ifconf ifc_tmp; if (NULL == ifc) { memset(&ifc_tmp, 0x0, sizeof(ifc_tmp)); ifc = &ifc_tmp; } /* * Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF * on some platforms; see W. R. Stevens, ``Unix Network Programming * Volume I'', p.435. */ for (i = 8;; i *= 2) { ifc->ifc_buf = calloc(i, sizeof(struct ifreq)); if (NULL == ifc->ifc_buf) { snmp_log(LOG_ERR, "could not allocate memory for %d interfaces\n", i); return -1; } ifc->ifc_len = i * sizeof(struct ifreq); if (ioctl(sd, SIOCGIFCONF, (char *) ifc) < 0) { if (errno != EINVAL || lastlen != 0) { /* * Something has gone genuinely wrong. */ snmp_log(LOG_ERR, "bad rc from ioctl, errno %d", errno); SNMP_FREE(ifc->ifc_buf); break; } /* * Otherwise, it could just be that the buffer is too small. */ } else { if (ifc->ifc_len == lastlen) { /* * The length is the same as the last time; we're done. */ break; } lastlen = ifc->ifc_len; } free(ifc->ifc_buf); /* no SNMP_FREE, getting ready to reassign */ } if (ifc == &ifc_tmp) free(ifc_tmp.ifc_buf); return ifc->ifc_len / sizeof(struct ifreq);}/** */static void_print_flags(short flags){/** Standard interface flags. */ struct { short flag; const char *name; } map[] = { { IFF_UP, "interface is up"}, { IFF_BROADCAST, "broadcast address valid"}, { IFF_DEBUG, "turn on debugging"}, { IFF_LOOPBACK, "is a loopback net"}, { IFF_POINTOPOINT, "interface is has p-p link"}, { IFF_NOTRAILERS, "avoid use of trailers"}, { IFF_RUNNING, "resources allocated"}, { IFF_NOARP, "no ARP protocol"}, { IFF_PROMISC, "receive all packets"}, { IFF_ALLMULTI, "receive all multicast packets"}, { IFF_MASTER, "master of a load balancer"}, { IFF_SLAVE, "slave of a load balancer"}, { IFF_MULTICAST, "Supports multicast"}, { IFF_PORTSEL, "can set media type"}, { IFF_AUTOMEDIA, "auto media select active"}, }; short unknown = flags; int i; for(i = 0; i < sizeof(map)/sizeof(map[0]); ++i) if(flags & map[i].flag) { DEBUGMSGT_NC(("access:ipaddress:container"," %s\n", map[i].name)); unknown &= ~map[i].flag; } if(unknown) DEBUGMSGT_NC(("access:ipaddress:container"," unknown 0x%x\n", unknown));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -