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

📄 ifrename.c

📁 wireless tools是linux下的一套对无线网卡进行配置的工具
💻 C
📖 第 1 页 / 共 4 页
字号:
  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,		   int			linenum){  /* 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,	       int			linenum){  size_t	n;  /* 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);  return(target->active[SELECT_PCMCIASLOT] ? 0 : -1);}/*********************** MAPPING MANAGEMENTS ***********************//* * Manage interface mappings. * Each mapping tell us how to identify a specific interface name. * It is composed of a bunch of selector values. *//*------------------------------------------------------------------*//* * Create a new interface mapping and verify its name */static struct if_mapping *mapping_create(char *	pos,	       int	len,	       int	linenum){  struct if_mapping *	ifnode;  char *		star;  /* Check overflow. */  if(len > IFNAMSIZ)    {      fprintf(stderr, "Error: Interface name `%.*s' too long at line %d\n",	      (int) len, pos, linenum);        return(NULL);    }  /* Create mapping, zero it */  ifnode = calloc(1, sizeof(if_mapping));  if(!ifnode)    {      fprintf(stderr, "Error: Can't allocate interface mapping.\n");        return(NULL);    }  /* Set the name, terminates it */  memcpy(ifnode->ifname, pos, len);   ifnode->ifname[len] = '\0';   /* Check the interface name and issue various pedantic warnings */  if((!strcmp(ifnode->ifname, "eth0")) || (!strcmp(ifnode->ifname, "wlan0")))    fprintf(stderr,	    "Warning: Interface name is `%s' at line %d, can't be mapped reliably.\n",	    ifnode->ifname, linenum);  if(strchr(ifnode->ifname, ':'))    fprintf(stderr, "Warning: Alias device `%s' at line %d probably can't be mapped.\n",	    ifnode->ifname, linenum);  /* Check for wildcard interface name, such as 'eth*' or 'wlan*'...   * This require specific kernel support (2.6.2-rc1 and later).   * We externally use '*', but the kernel doesn't know about that,   * so convert it to something it knows about... */  star = strchr(ifnode->ifname, '*');  if(star != NULL)    {      /* We need an extra char */      if(len >= IFNAMSIZ)	{	  fprintf(stderr,		  "Error: Interface wildcard `%s' too long at line %d\n",		  ifnode->ifname, linenum);  	  free(ifnode);	  return(NULL);	}      /* Replace '*' with '%d' */      memmove(star + 2, star + 1, len + 1 - (star - ifnode->ifname));      star[0] = '%';      star[1] = 'd';    }  if(verbose)    fprintf(stderr, "Parsing : Added Mapping `%s' from line %d.\n",	    ifnode->ifname, linenum);  /* Done */  return(ifnode);}/*------------------------------------------------------------------*//* * Find the most appropriate selector matching a given selector name */static inline const struct mapping_selector *selector_find(const char *	string,	      size_t		slen,	      int		linenum){  const struct mapping_selector *	found = NULL;  int			ambig = 0;  int			i;  /* Go through all selectors */  for(i = 0; selector_list[i].name != NULL; ++i)    {      /* No match -> next one */      if(strncasecmp(selector_list[i].name, string, slen) != 0)	continue;      /* Exact match -> perfect */      if(slen == strlen(selector_list[i].name))	return &selector_list[i];      /* Partial match */      if(found == NULL)	/* First time */	found = &selector_list[i];      else	/* Another time */	if (selector_list[i].add_fn != found->add_fn)	  ambig = 1;    }  if(found == NULL)    {      fprintf(stderr, "Error: Unknown selector `%.*s' at line %d.\n",	      (int) slen, string, linenum);      return NULL;    }  if(ambig)    {      fprintf(stderr, "Selector `%.*s'at line %d is ambiguous.\n",	      (int) slen, string, linenum);      return NULL;    }  return found;}/*------------------------------------------------------------------*//* * Read the configuration file and extract all valid mappings and their * selectors. */static intmapping_readfile(const char *	filename){  FILE *	stream;  char *	linebuf = NULL;  size_t	linelen = 0;   int		linenum = 0;   /* Reset the list of filters */  bzero(selector_active, sizeof(selector_active));  /* Check filename */  if(!strcmp(filename, "-"))    {      /* Read from stdin */      stream = stdin;    }  else    {      /* Open the file for reading */      stream = fopen(filename, "r");      if(!stream) 	{	  fprintf(stderr, "Error: Can't open configuration file `%s': %s\n",		  filename, 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)    {      struct if_mapping *	ifnode;      char *			p;      char *			e;      size_t			n;      int			ret = -13;	/* Complain if no selectors */      /* Keep track of line number */      linenum++;      /* Every comments terminates parsing */      if((p = strchr(linebuf,'#')) != NULL)	*p = '\0';      /* Get interface name */      p = linebuf;      while(isspace(*p))	++p;       if(*p == '\0')	continue;	/* Line ended */      n = strcspn(p, " \t\n");      /* Create mapping */      ifnode = mapping_create(p, n, linenum);      if(!ifnode)	continue;	/* Ignore this line */      p += n;      p += strspn(p, " \t\n");       /* Loop on all selectors */      while(*p != '\0')	{	  const struct mapping_selector *	selector = NULL;	  /* Selector name length */	  n = strcspn(p, " \t\n");	  /* Find it */	  selector = selector_find(p, n, linenum);	  if(!selector)	    {	      ret = -1;	      break;	    }	  /* Get to selector value */	  p += n;	  p += strspn(p, " \t\n"); 	  if(*p == '\0')	    {	      fprintf(stderr, "Error: no value for selector `%s' on line %d\n",		      selector->name, linenum);	      ret = -1;	      break;	/* Line ended */	    }	  /* Check for quoted arguments */	  if(*p == '"')	    {	      p++;	      e = strchr(p, '"');	      if(e == NULL)		{		  fprintf(stderr,			  "Error: unterminated quoted value on line %d\n",			  linenum);		  ret = -1;		  break;	/* Line ended */		}	      n = e - p;	      e++;	    }	  else	    {	      /* Just end at next blank */	      n = strcspn(p, " \t\n");	      e = p + n;

⌨️ 快捷键说明

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