X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fnameif.c;h=046e308c5fe423fc45a7f29dc2da7af65e994b40;hb=765b0eed3ef29a80115708c3249d3a541509cd24;hp=f0b8d11b0871df45f0253443dc1c9dd8f8150ff6;hpb=3f9c84857617b0cf0d1824664e371fb6a4cac2e3;p=oweals%2Fbusybox.git diff --git a/networking/nameif.c b/networking/nameif.c index f0b8d11b0..046e308c5 100644 --- a/networking/nameif.c +++ b/networking/nameif.c @@ -5,6 +5,7 @@ * Written 2000 by Andi Kleen. * Busybox port 2002 by Nick Fedchik * Glenn McGrath + * Extended matching support 2008 by Nico Erfurth * * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */ @@ -15,16 +16,11 @@ #include #include -/* Older versions of net/if.h do not appear to define IF_NAMESIZE. */ -#ifndef IF_NAMESIZE -# ifdef IFNAMSIZ -# define IF_NAMESIZE IFNAMSIZ -# else -# define IF_NAMESIZE 16 -# endif +#ifndef IFNAMSIZ +#define IFNAMSIZ 16 #endif -/* take from linux/sockios.h */ +/* Taken from linux/sockios.h */ #define SIOCSIFNAME 0x8923 /* set interface name */ /* Octets in one Ethernet addr, from */ @@ -55,7 +51,7 @@ struct ethtool_drvinfo { char version[32]; /* driver version string */ char fw_version[32]; /* firmware version string, if applicable */ char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */ - /* For PCI devices, use pci_dev->slot_name. */ + /* For PCI devices, use pci_dev->slot_name. */ char reserved1[32]; char reserved2[16]; uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */ @@ -93,12 +89,10 @@ static void nameif_parse_selector(ethtable_t *ch, char *selector) found_selector++; } else { #endif - lmac = ether_aton(selector + (strncmp(selector, "mac=", 4) == 0 ? 4 : 0)); - /* Check ascii selector, convert and copy to *mac */ - if (lmac == NULL) - bb_error_msg_and_die("cannot parse %s", selector); - ch->mac = xmalloc(ETH_ALEN); - memcpy(ch->mac, lmac, ETH_ALEN); + lmac = xmalloc(ETH_ALEN); + ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) != 0 ? 0 : 4), lmac); + if (ch->mac == NULL) + bb_error_msg_and_die("can't parse %s", selector); #if ENABLE_FEATURE_NAMEIF_EXTENDED found_selector++; }; @@ -112,10 +106,10 @@ static void nameif_parse_selector(ethtable_t *ch, char *selector) static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector) { ethtable_t *ch; - if (strlen(ifname) >= IF_NAMESIZE) + if (strlen(ifname) >= IFNAMSIZ) bb_error_msg_and_die("interface name '%s' too long", ifname); ch = xzalloc(sizeof(*ch)); - ch->ifname = ifname; + ch->ifname = xstrdup(ifname); nameif_parse_selector(ch, selector); ch->next = *clist; if (*clist) @@ -123,21 +117,36 @@ static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *select *clist = ch; } +#if ENABLE_FEATURE_CLEAN_UP +static void delete_eth_table(ethtable_t *ch) +{ + free(ch->ifname); +#if ENABLE_FEATURE_NAMEIF_EXTENDED + free(ch->bus_info); + free(ch->driver); +#endif + free(ch->mac); + free(ch); +}; +#else +void delete_eth_table(ethtable_t *ch); +#endif + int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int nameif_main(int argc, char **argv) { ethtable_t *clist = NULL; - FILE *ifh; const char *fname = "/etc/mactab"; - char *line; - char *line_ptr; - int linenum; int ctl_sk; ethtable_t *ch; + parser_t *parser; + char *token[2]; if (1 & getopt32(argv, "sc:", &fname)) { openlog(applet_name, 0, LOG_LOCAL0); - logmode = LOGMODE_SYSLOG; + /* Why not just "="? I assume logging to stderr + * can't hurt. 2>/dev/null if you don't like it: */ + logmode |= LOGMODE_SYSLOG; } argc -= optind; argv += optind; @@ -151,46 +160,26 @@ int nameif_main(int argc, char **argv) prepend_new_eth_table(&clist, ifname, *argv++); } } else { - ifh = xfopen(fname, "r"); - while ((line = xmalloc_fgets(ifh)) != NULL) { - char *next; - - line_ptr = skip_whitespace(line); - if ((line_ptr[0] == '#') || (line_ptr[0] == '\n')) { - free(line); - continue; - } - next = skip_non_whitespace(line_ptr); - if (*next) - *next++ = '\0'; - prepend_new_eth_table(&clist, line_ptr, next); - free(line); - } - fclose(ifh); + parser = config_open(fname); + while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) + prepend_new_eth_table(&clist, token[0], token[1]); + config_close(parser); } ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0); - ifh = xfopen("/proc/net/dev", "r"); + parser = config_open2("/proc/net/dev", xfopen_for_read); - linenum = 0; - while (clist) { + while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) { struct ifreq ifr; #if ENABLE_FEATURE_NAMEIF_EXTENDED struct ethtool_drvinfo drvinfo; #endif - - line = xmalloc_fgets(ifh); - if (line == NULL) - break; /* Seems like we're done */ - if (linenum++ < 2 ) - goto next_line; /* Skip the first two lines */ + if (parser->lineno < 2) + continue; /* Skip the first two lines */ /* Find the current interface name and copy it to ifr.ifr_name */ - line_ptr = skip_whitespace(line); - *skip_non_whitespace(line_ptr) = '\0'; - memset(&ifr, 0, sizeof(struct ifreq)); - strncpy(ifr.ifr_name, line_ptr, sizeof(ifr.ifr_name)); + strncpy_IFNAMSIZ(ifr.ifr_name, token[0]); #if ENABLE_FEATURE_NAMEIF_EXTENDED /* Check for driver etc. */ @@ -213,15 +202,16 @@ int nameif_main(int argc, char **argv) if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0) continue; /* if we came here, all selectors have matched */ - goto found; + break; } /* Nothing found for current interface */ - goto next_line; - found: + if (!ch) + continue; + if (strcmp(ifr.ifr_name, ch->ifname) != 0) { strcpy(ifr.ifr_newname, ch->ifname); ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr, - "cannot change ifname %s to %s", + "can't change ifname %s to %s", ifr.ifr_name, ch->ifname); } /* Remove list entry of renamed interface */ @@ -231,16 +221,13 @@ int nameif_main(int argc, char **argv) clist = ch->next; if (ch->next != NULL) ch->next->prev = ch->prev; - if (ENABLE_FEATURE_CLEAN_UP) { - free(ch->ifname); - free(ch->mac); - free(ch); - } - next_line: - free(line); + if (ENABLE_FEATURE_CLEAN_UP) + delete_eth_table(ch); } if (ENABLE_FEATURE_CLEAN_UP) { - fclose(ifh); + for (ch = clist; ch; ch = ch->next) + delete_eth_table(ch); + config_close(parser); }; return 0;