⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ifrename.c

📁 This package contains the Wireless tools, used to manipulate the Wireless Extensions. The Wireless
💻 C
📖 第 1 页 / 共 5 页
字号:
  struct ifreq	ifr;  int		ret;  /* Compute name for victim interface */  len = strlen(victimname);  memcpy(autoname, victimname, len + 1);  if(len > (IFNAMSIZ - 2))    len = IFNAMSIZ - 2;		/* Make sure we have at least two char */  len--;			/* Convert to index */  while(isdigit(autoname[len]))    len--;			/* Scrap all trailing digits */  strcpy(autoname + len + 1, "%d");  if(verbose)    fprintf(stderr, "Takeover : moving interface `%s' to `%s'.\n",	    victimname, autoname);  /* Prepare request */  bzero(&ifr, sizeof(struct ifreq));  strncpy(ifr.ifr_name, victimname, IFNAMSIZ);   strncpy(ifr.ifr_newname, autoname, IFNAMSIZ);   /* Rename victim interface */  ret = ioctl(skfd, SIOCSIFNAME, &ifr);  if(!ret)    num_takeover++;  return(ret);}/*------------------------------------------------------------------*//* * Ask the kernel to change the name of an interface. * That's what we want to do. All the rest is to make sure we call this * appropriately. */static intif_set_name(int			skfd,	    const char *	oldname,	    const char *	newname,	    char *		retname){  struct ifreq	ifr;  int		ret;  /* The kernel doesn't check is the interface already has the correct   * name and may return an error, so check ourselves.   * In the case of wildcard, the result can be weird : if oldname='eth0'   * and newname='eth*', retname would be 'eth1'.   * So, if the oldname value matches the newname pattern, just return   * success. */  if(!if_match_ifname(newname, oldname))    {      if(verbose)	fprintf(stderr, "Setting : Interface `%s' already matches `%s'.\n",		oldname, newname);      strcpy(retname, oldname);      return(0);    }  /* Prepare request */  bzero(&ifr, sizeof(struct ifreq));  strncpy(ifr.ifr_name, oldname, IFNAMSIZ);   strncpy(ifr.ifr_newname, newname, IFNAMSIZ);   /* Do it */  ret = ioctl(skfd, SIOCSIFNAME, &ifr);  /* Takeover support : grab interface name from another interface */  if(ret && (errno == EEXIST) && force_takeover)    {      /* Push things around */      ret = if_takeover_name(skfd, newname);      if(!ret)	/* Second try */	ret = ioctl(skfd, SIOCSIFNAME, &ifr);    }  if(!ret)    {      /* Get the real new name (in case newname is a wildcard) */      strcpy(retname, ifr.ifr_newname);      if(verbose)	fprintf(stderr, "Setting : Interface `%s' renamed to `%s'.\n",		oldname, retname);    }  return(ret);}/************************ SELECTOR HANDLING ************************//* * Handle the various selector we support *//*------------------------------------------------------------------*//* * Add a MAC address selector to a mapping */static intmapping_addmac(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 */  if(len >= sizeof(ifnode->mac_filter))    {       fprintf(stderr, "MAC address too long at line %d\n", linenum);        return(-1);    }  n = strspn(string, "0123456789ABCDEFabcdef:*");   if(n < len)    {      fprintf(stderr, "Error: Invalid MAC address `%s' at line %d\n",	      string, linenum);      return(-1);    }  /* Copy as filter in all cases */  memcpy(ifnode->mac_filter, string, len + 1);   /* Check the type of MAC address */  if (strchr(ifnode->mac_filter, '*') != NULL)    {      /* This is a wilcard. Usual format : "01:23:45:*"       * Unfortunately, we can't do proper parsing. */      ifnode->active[SELECT_MAC] = HAS_MAC_FILTER;      active[SELECT_MAC] = HAS_MAC_FILTER;    }  else    {      /* Not a wildcard : "01:23:45:67:89:AB" */      ifnode->mac_len = iw_mac_aton(ifnode->mac_filter,				    ifnode->mac, MAX_MAC_LEN);      if(ifnode->mac_len == 0)	{	  fprintf(stderr, "Error: Invalid MAC address `%s' at line %d\n",		  ifnode->mac_filter, linenum);	  return(-1);	}      /* Check that it's not NULL */      if((ifnode->mac_len == 6) && (!memcmp(&ifnode->mac, &zero_mac, 6)))	{	  fprintf(stderr,		  "Warning: MAC address is null at line %d, this is dangerous...\n",		  linenum);	}      ifnode->active[SELECT_MAC] = HAS_MAC_EXACT;      if(active[SELECT_MAC] == 0)	active[SELECT_MAC] = HAS_MAC_EXACT;    }  if(verbose)    fprintf(stderr,	    "Parsing : Added %s MAC address `%s' from line %d.\n",	    ifnode->active[SELECT_MAC] == HAS_MAC_FILTER ? "filter" : "exact",	    ifnode->mac_filter, linenum);  return(0);}/*------------------------------------------------------------------*//* * Compare the mac address of two mappings */static intmapping_cmpmac(struct if_mapping *	ifnode,	       struct if_mapping *	target){  /* Check for wildcard matching */  if(ifnode->active[SELECT_MAC] == HAS_MAC_FILTER)    /* Do wildcard matching, case insensitive */    return(fnmatch(ifnode->mac_filter, target->mac_filter, FNM_CASEFOLD));  else    /* Exact matching, in hex */    return((ifnode->mac_len != target->mac_len) ||	   memcmp(ifnode->mac, target->mac, ifnode->mac_len));}/*------------------------------------------------------------------*//* * Extract the MAC address and Link Type of an interface */static intmapping_getmac(int			skfd,	       const char *		ifname,	       struct if_mapping *	target,	       int			flag){  struct ifreq	ifr;  int		ret;  int		i;  /* Get MAC address */  bzero(&ifr, sizeof(struct ifreq));  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);  ret = ioctl(skfd, SIOCGIFHWADDR, &ifr);  if(ret < 0)    {      fprintf(stderr, "Error: Can't read MAC address on interface `%s' : %s\n",	      ifname, strerror(errno));      return(-1);    }  /* Extract ARP type */  target->hw_type = ifr.ifr_hwaddr.sa_family;  /* Calculate address length */  target->mac_len = 6;  for(i = 0; i < weird_mac_len_num; i++)    if(weird_mac_len[i][0] == ifr.ifr_hwaddr.sa_family)      {	target->mac_len = weird_mac_len[i][1];	break;      }  /* Extract MAC address bytes */  memcpy(target->mac, ifr.ifr_hwaddr.sa_data, target->mac_len);  /* Check the type of comparison */  if((flag == HAS_MAC_FILTER) || verbose)    {      /* Convert to ASCII */      iw_mac_ntop(target->mac, target->mac_len,		  target->mac_filter, sizeof(target->mac_filter));    }  target->active[SELECT_MAC] = flag;  target->active[SELECT_ARP] = 1;  if(verbose)    fprintf(stderr,	    "Querying %s : Got MAC address `%s' and ARP/Link Type `%d'.\n",	    ifname, target->mac_filter, target->hw_type);  return(0);}/*------------------------------------------------------------------*//* * Add a ARP/Link type selector to a mapping */static intmapping_addarp(struct if_mapping *	ifnode,	       int *			active,	       char *			string,	       size_t			len,	       struct add_extra *	extra,	       int			linenum){  size_t	n;  unsigned int	type;  /* Avoid "Unused parameter" warning */  extra = extra;  /* Verify validity of string, convert to int */  n = strspn(string, "0123456789");   if((n < len) || (sscanf(string, "%d", &type) != 1))    {      fprintf(stderr, "Error: Invalid ARP/Link Type `%s' at line %d\n",	      string, linenum);      return(-1);    }  ifnode->hw_type = (unsigned short) type;  ifnode->active[SELECT_ARP] = 1;  active[SELECT_ARP] = 1;  if(verbose)    fprintf(stderr, "Parsing : Added ARP/Link Type `%d' from line %d.\n",	    ifnode->hw_type, linenum);  return(0);}/*------------------------------------------------------------------*//* * Compare the ARP/Link type of two mappings */static intmapping_cmparp(struct if_mapping *	ifnode,	       struct if_mapping *	target){  return(!(ifnode->hw_type == target->hw_type));}/*------------------------------------------------------------------*//* * Extract the ARP/Link type of an interface */static intmapping_getarp(int			skfd,	       const char *		ifname,	       struct if_mapping *	target,	       int			flag){  /* We may have already extracted the MAC address */  if(target->active[SELECT_MAC])    return(0);  /* Otherwise just do it */  return(mapping_getmac(skfd, ifname, target, flag));}/*------------------------------------------------------------------*//* * Add a Driver name selector to a mapping */static intmapping_adddriver(struct if_mapping *	ifnode,		  int *			active,		  char *		string,		  size_t		len,		  struct add_extra *	extra,		  int			linenum){  /* Avoid "Unused parameter" warning */  extra = extra;  /* Plain string, minimal verification */  if(len >= sizeof(ifnode->driver))    {       fprintf(stderr, "Error: Driver name too long at line %d\n", linenum);        return(-1);    }  /* Copy */  memcpy(ifnode->driver, string, len + 1);   /* Activate */  ifnode->active[SELECT_DRIVER] = 1;  active[SELECT_DRIVER] = 1;  if(verbose)    fprintf(stderr,	    "Parsing : Added Driver name `%s' from line %d.\n",	    ifnode->driver, linenum);  return(0);}/*------------------------------------------------------------------*//* * Compare the Driver name of two mappings */static intmapping_cmpdriver(struct if_mapping *	ifnode,		  struct if_mapping *	target){  /* Do wildcard matching, case insensitive */  return(fnmatch(ifnode->driver, target->driver, FNM_CASEFOLD));}/*------------------------------------------------------------------*//* * Add a Bus-Info selector to a mapping */static intmapping_addbusinfo(struct if_mapping *	ifnode,		   int *		active,		   char *		string,		   size_t		len,		   struct add_extra *	extra,		   int			linenum){#if 0  size_t	n;#endif  /* Avoid "Unused parameter" warning */  extra = extra;  /* Verify validity of string */  if(len >= sizeof(ifnode->bus_info))    {       fprintf(stderr, "Bus Info too long at line %d\n", linenum);        return(-1);    }#if 0  /* Hum... This doesn's seem true for non-PCI bus-info */  n = strspn(string, "0123456789ABCDEFabcdef:.*");   if(n < len)    {      fprintf(stderr, "Error: Invalid Bus Info `%s' at line %d\n",	      string, linenum);      return(-1);    }#endif  /* Copy */  memcpy(ifnode->bus_info, string, len + 1);   /* Activate */  ifnode->active[SELECT_BUSINFO] = 1;  active[SELECT_BUSINFO] = 1;  if(verbose)    fprintf(stderr,	    "Parsing : Added Bus Info `%s' from line %d.\n",	    ifnode->bus_info, linenum);  return(0);}/*------------------------------------------------------------------*//* * Compare the Bus-Info of two mappings */static intmapping_cmpbusinfo(struct if_mapping *	ifnode,		   struct if_mapping *	target){  /* Do wildcard matching, case insensitive */  return(fnmatch(ifnode->bus_info, target->bus_info, FNM_CASEFOLD));}/*------------------------------------------------------------------*//* * Add a Firmare revision selector to a mapping */static intmapping_addfirmware(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->fw_version))    {       fprintf(stderr, "Firmware revision too long at line %d\n", linenum);        return(-1);    }  /* Copy */  memcpy(ifnode->fw_version, string, len + 1);   /* Activate */  ifnode->active[SELECT_FIRMWARE] = 1;  active[SELECT_FIRMWARE] = 1;  if(verbose)    fprintf(stderr,	    "Parsing : Added Firmware Revision `%s' from line %d.\n",	    ifnode->fw_version, linenum);  return(0);}/*------------------------------------------------------------------*//* * 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])

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -