📄 ifrename.c
字号:
}/*------------------------------------------------------------------*//* * Compare the Bus-Info of two mappings */static intmapping_cmpfirmware(struct if_mapping * ifnode, struct if_mapping * target){ /* Do wildcard matching, case insensitive */ return(fnmatch(ifnode->fw_version, target->fw_version, FNM_CASEFOLD));}/*------------------------------------------------------------------*//* * Extract the Driver name and Bus-Info from a live interface */static intmapping_getdriverbusinfo(int skfd, const char * ifname, struct if_mapping * target, int flag){ struct ifreq ifr; struct ethtool_drvinfo drvinfo; int ret; /* Avoid "Unused parameter" warning */ flag = flag; /* We may come here twice or more, so do the job only once */ if(target->active[SELECT_DRIVER] || target->active[SELECT_BUSINFO] || target->active[SELECT_FIRMWARE]) return(0); /* Prepare request */ bzero(&ifr, sizeof(struct ifreq)); bzero(&drvinfo, sizeof(struct ethtool_drvinfo)); strncpy(ifr.ifr_name, ifname, IFNAMSIZ); drvinfo.cmd = ETHTOOL_GDRVINFO; ifr.ifr_data = (caddr_t) &drvinfo; /* Do it */ ret = ioctl(skfd, SIOCETHTOOL, &ifr); if(ret < 0) { /* Most drivers don't support that, keep quiet for now */ if(verbose) fprintf(stderr, "Error: Can't read driver/bus-info on interface `%s' : %s\n", ifname, strerror(errno)); return(-1); } /* Copy over */ strcpy(target->driver, drvinfo.driver); strcpy(target->bus_info, drvinfo.bus_info); strcpy(target->fw_version, drvinfo.fw_version); /* Activate */ target->active[SELECT_DRIVER] = 1; target->active[SELECT_BUSINFO] = 1; target->active[SELECT_FIRMWARE] = 1; if(verbose) fprintf(stderr, "Querying %s : Got Driver name `%s', Bus Info `%s' and Firmware `%s'.\n", ifname, target->driver, target->bus_info, target->fw_version); return(0);}/*------------------------------------------------------------------*//* * Add a Base Address selector to a mapping */static intmapping_addbaseaddr(struct if_mapping * ifnode, int * active, char * string, size_t len, struct add_extra * extra, int linenum){ size_t n; unsigned int address; /* Avoid "Unused parameter" warning */ extra = extra; /* Verify validity of string */ n = strspn(string, "0123456789ABCDEFabcdefx"); if((n < len) || (sscanf(string, "0x%X", &address) != 1)) { fprintf(stderr, "Error: Invalid Base Address `%s' at line %d\n", string, linenum); return(-1); } /* Copy */ ifnode->base_addr = (unsigned short) address; /* Activate */ ifnode->active[SELECT_BASEADDR] = 1; active[SELECT_BASEADDR] = 1; if(verbose) fprintf(stderr, "Parsing : Added Base Address `0x%X' from line %d.\n", ifnode->base_addr, linenum); return(0);}/*------------------------------------------------------------------*//* * Compare the Base Address of two mappings */static intmapping_cmpbaseaddr(struct if_mapping * ifnode, struct if_mapping * target){ /* Do wildcard matching, case insensitive */ return(!(ifnode->base_addr == target->base_addr));}/*------------------------------------------------------------------*//* * Add a IRQ selector to a mapping */static intmapping_addirq(struct if_mapping * ifnode, int * active, char * string, size_t len, struct add_extra * extra, int linenum){ size_t n; unsigned int irq; /* Avoid "Unused parameter" warning */ extra = extra; /* Verify validity of string */ n = strspn(string, "0123456789"); if((n < len) || (sscanf(string, "%d", &irq) != 1)) { fprintf(stderr, "Error: Invalid Base Address `%s' at line %d\n", string, linenum); return(-1); } /* Copy */ ifnode->irq = (unsigned char) irq; /* Activate */ ifnode->active[SELECT_IRQ] = 1; active[SELECT_IRQ] = 1; if(verbose) fprintf(stderr, "Parsing : Added IRQ `%d' from line %d.\n", ifnode->irq, linenum); return(0);}/*------------------------------------------------------------------*//* * Compare the IRQ of two mappings */static intmapping_cmpirq(struct if_mapping * ifnode, struct if_mapping * target){ /* Do wildcard matching, case insensitive */ return(!(ifnode->irq == target->irq));}/*------------------------------------------------------------------*//* * Extract the Driver name and Bus-Info from a live interface */static intmapping_getbaseaddrirq(int skfd, const char * ifname, struct if_mapping * target, int flag){ struct ifreq ifr; struct ifmap map; /* hardware setup */ int ret; /* Avoid "Unused parameter" warning */ flag = flag; /* We may come here twice, so do the job only once */ if(target->active[SELECT_BASEADDR] || target->active[SELECT_IRQ]) return(0); /* Prepare request */ bzero(&ifr, sizeof(struct ifreq)); bzero(&map, sizeof(struct ifmap)); strncpy(ifr.ifr_name, ifname, IFNAMSIZ); /* Do it */ ret = ioctl(skfd, SIOCGIFMAP, &ifr); if(ret < 0) { /* Don't know if every interface has that, so keep quiet... */ if(verbose) fprintf(stderr, "Error: Can't read base address/irq on interface `%s' : %s\n", ifname, strerror(errno)); return(-1); } /* Copy over, activate */ if(ifr.ifr_map.base_addr >= 0x100) { target->base_addr = ifr.ifr_map.base_addr; target->active[SELECT_BASEADDR] = 1; } target->irq = ifr.ifr_map.irq; target->active[SELECT_IRQ] = 1; if(verbose) fprintf(stderr, "Querying %s : Got Base Address `0x%X' and IRQ `%d'.\n", ifname, target->base_addr, target->irq); return(0);}/*------------------------------------------------------------------*//* * Add a Wireless Protocol selector to a mapping */static intmapping_addiwproto(struct if_mapping * ifnode, int * active, char * string, size_t len, struct add_extra * extra, int linenum){ /* Avoid "Unused parameter" warning */ extra = extra; /* Verify validity of string */ if(len >= sizeof(ifnode->iwproto)) { fprintf(stderr, "Wireless Protocol too long at line %d\n", linenum); return(-1); } /* Copy */ memcpy(ifnode->iwproto, string, len + 1); /* Activate */ ifnode->active[SELECT_IWPROTO] = 1; active[SELECT_IWPROTO] = 1; if(verbose) fprintf(stderr, "Parsing : Added Wireless Protocol `%s' from line %d.\n", ifnode->iwproto, linenum); return(0);}/*------------------------------------------------------------------*//* * Compare the Wireless Protocol of two mappings */static intmapping_cmpiwproto(struct if_mapping * ifnode, struct if_mapping * target){ /* Do wildcard matching, case insensitive */ return(fnmatch(ifnode->iwproto, target->iwproto, FNM_CASEFOLD));}/*------------------------------------------------------------------*//* * Extract the Wireless Protocol from a live interface */static intmapping_getiwproto(int skfd, const char * ifname, struct if_mapping * target, int flag){ struct iwreq wrq; /* Avoid "Unused parameter" warning */ flag = flag; /* Get wireless name */ if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0) /* Don't complain about it, Ethernet cards will never support this */ return(-1); strncpy(target->iwproto, wrq.u.name, IFNAMSIZ); target->iwproto[IFNAMSIZ] = '\0'; /* Activate */ target->active[SELECT_IWPROTO] = 1; if(verbose) fprintf(stderr, "Querying %s : Got Wireless Protocol `%s'.\n", ifname, target->iwproto); return(0);}/*------------------------------------------------------------------*//* * Add a Pcmcia Slot selector to a mapping */static intmapping_addpcmciaslot(struct if_mapping * ifnode, int * active, char * string, size_t len, struct add_extra * extra, int linenum){ size_t n; /* Avoid "Unused parameter" warning */ extra = extra; /* Verify validity of string, convert to int */ n = strspn(string, "0123456789"); if((n < len) || (sscanf(string, "%d", &ifnode->pcmcia_slot) != 1)) { fprintf(stderr, "Error: Invalid Pcmcia Slot `%s' at line %d\n", string, linenum); return(-1); } ifnode->active[SELECT_PCMCIASLOT] = 1; active[SELECT_PCMCIASLOT] = 1; if(verbose) fprintf(stderr, "Parsing : Added Pcmcia Slot `%d' from line %d.\n", ifnode->pcmcia_slot, linenum); return(0);}/*------------------------------------------------------------------*//* * Compare the Pcmcia Slot of two mappings */static intmapping_cmppcmciaslot(struct if_mapping * ifnode, struct if_mapping * target){ return(!(ifnode->pcmcia_slot == target->pcmcia_slot));}/*------------------------------------------------------------------*//* * Extract the Pcmcia Slot of an interface * Note that this works only for cards fully managed by cardmgr. * With the kernel pcmcia modules, 32 bits cards (CardBus) are not managed * by cardmgr, and therefore won't have a valid slot number. For those * cards, you should use Bus Info (when the driver exports it). * In the long term, 16 bits card as well will no longer be managed by * cardmgr. Currently, Bus Info for 16 bit cards don't have any information * enabling to locate their physical location on the system, but I hope that * this will change. * When that happen, we can drop this code... */static intmapping_getpcmciaslot(int skfd, const char * ifname, struct if_mapping * target, int flag){ FILE * stream; char * linebuf = NULL; size_t linelen = 0; int linenum = 0; /* Avoid "Unused parameter" warning */ skfd = skfd; flag = flag; /* Open the stab file for reading */ stream = fopen(PCMCIA_STAB1, "r"); if(!stream) { /* Try again, alternate location */ stream = fopen(PCMCIA_STAB2, "r"); if(!stream) { fprintf(stderr, "Error: Can't open PCMCIA Stab file `%s' or `%s': %s\n", PCMCIA_STAB1, PCMCIA_STAB2, strerror(errno)); return(-1); } } /* Read each line of file * getline is a GNU extension :-( The buffer is recycled and increased * as needed by getline. */ while(getline(&linebuf, &linelen, stream) > 0) { char * p; size_t n; size_t k; int pcmcia_slot; int i; /* Keep track of line number */ linenum++; /* Get Pcmcia socket number */ p = linebuf; while(isspace(*p)) ++p; if(*p == '\0') continue; /* Line ended */ n = strcspn(p, " \t\n"); k = strspn(p, "0123456789"); if((k < n) || (sscanf(p, "%d", &pcmcia_slot) != 1)) /* Next line */ continue; /* Skip socket number */ /* Skip socket number ; device class ; driver name ; instance */ for(i = 0; i < 4; i++) { /* Skip item */ p += n; /* Skip space */ p += strspn(p, " \t\n"); if(*p == '\0') break; /* Line ended */ /* Next item size */ n = strcspn(p, " \t\n"); } if(*p == '\0') continue; /* Line ended */ /* Terminate dev name */ p[n] = '\0'; /* Compare to interface name */ if(!strcmp(p, ifname)) { /* Save */ target->pcmcia_slot = pcmcia_slot; /* Activate */ target->active[SELECT_PCMCIASLOT] = 1; if(verbose) fprintf(stderr, "Querying %s : Got Pcmcia Slot `%d'.\n", ifname, target->pcmcia_slot); /* Exit loop, found it */ break; } /* Finished -> next line */ } /* Cleanup */ free(linebuf); fclose(stream); return(target->active[SELECT_PCMCIASLOT] ? 0 : -1);}/*------------------------------------------------------------------*//* * Add a sysfs selector to a mapping */static intmapping_addsysfs(struct if_mapping * ifnode, int * active, char * string, size_t len, struct add_extra * extra, int linenum){ int findex; /* filename index */ char * sdup; /* Check if we have a modifier */ if((extra == NULL) || (extra->modif_pos == NULL)) { fprintf(stderr, "Error: No SYSFS filename at line %d\n", linenum); return(-1); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -