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

📄 iwconfig.c

📁 This package contains the Wireless tools, used to manipulate the Wireless Extensions. The Wireless
💻 C
📖 第 1 页 / 共 3 页
字号:
	  /* Let's check if nothing (simply on) */	  if(info->power.flags == IW_POWER_ON)	    printf(":on"); 	}      printf("\n          ");    }  /* Display statistics */  if(info->has_stats)    {      iw_print_stats(buffer, sizeof(buffer),		     &info->stats.qual, &info->range, info->has_range);      printf("Link %s\n", buffer);      if(info->range.we_version_compiled > 11)	printf("          Rx invalid nwid:%d  Rx invalid crypt:%d  Rx invalid frag:%d\n          Tx excessive retries:%d  Invalid misc:%d   Missed beacon:%d\n",	       info->stats.discard.nwid,	       info->stats.discard.code,	       info->stats.discard.fragment,	       info->stats.discard.retries,	       info->stats.discard.misc,	       info->stats.miss.beacon);      else	printf("          Rx invalid nwid:%d  invalid crypt:%d  invalid misc:%d\n",	       info->stats.discard.nwid,	       info->stats.discard.code,	       info->stats.discard.misc);    }  printf("\n");}/*------------------------------------------------------------------*//* * Print on the screen in a neat fashion all the info we have collected * on a device. */static intprint_info(int		skfd,	   char *	ifname,	   char *	args[],	   int		count){  struct wireless_info	info;  int			rc;  /* Avoid "Unused parameter" warning */  args = args; count = count;  rc = get_info(skfd, ifname, &info);  switch(rc)    {    case 0:	/* Success */      /* Display it ! */      display_info(&info, ifname);      break;    case -ENOTSUP:      fprintf(stderr, "%-8.16s  no wireless extensions.\n\n",	      ifname);      break;    default:      fprintf(stderr, "%-8.16s  %s\n\n", ifname, strerror(-rc));    }  return(rc);}/************************* SETTING ROUTINES **************************//*------------------------------------------------------------------*//* * Macro to handle errors when setting WE * Print a nice error message and exit... * We define them as macro so that "return" do the right thing. * The "do {...} while(0)" is a standard trick */#define ERR_SET_EXT(rname, request) \	fprintf(stderr, "Error for wireless request \"%s\" (%X) :\n", \		rname, request)#define ABORT_ARG_NUM(rname, request) \	do { \		ERR_SET_EXT(rname, request); \		fprintf(stderr, "    too few arguments.\n"); \		return(-1); \	} while(0)#define ABORT_ARG_TYPE(rname, request, arg) \	do { \		ERR_SET_EXT(rname, request); \		fprintf(stderr, "    invalid argument \"%s\".\n", arg); \		return(-2); \	} while(0)#define ABORT_ARG_SIZE(rname, request, max) \	do { \		ERR_SET_EXT(rname, request); \		fprintf(stderr, "    argument too big (max %d)\n", max); \		return(-3); \	} while(0)/*------------------------------------------------------------------*//* * Wrapper to push some Wireless Parameter in the driver * Use standard wrapper and add pretty error message if fail... */#define IW_SET_EXT_ERR(skfd, ifname, request, wrq, rname) \	do { \	if(iw_set_ext(skfd, ifname, request, wrq) < 0) { \		ERR_SET_EXT(rname, request); \		fprintf(stderr, "    SET failed on device %-1.16s ; %s.\n", \			ifname, strerror(errno)); \		return(-5); \	} } while(0)/*------------------------------------------------------------------*//* * Wrapper to extract some Wireless Parameter out of the driver * Use standard wrapper and add pretty error message if fail... */#define IW_GET_EXT_ERR(skfd, ifname, request, wrq, rname) \	do { \	if(iw_get_ext(skfd, ifname, request, wrq) < 0) { \		ERR_SET_EXT(rname, request); \		fprintf(stderr, "    GET failed on device %-1.16s ; %s.\n", \			ifname, strerror(errno)); \		return(-6); \	} } while(0)/*------------------------------------------------------------------*//* * Set the wireless options requested on command line * This function is too long and probably should be split, * because it look like the perfect definition of spaghetti code, * but I'm way to lazy */static intset_info(int		skfd,		/* The socket */	 char *		args[],		/* Command line args */	 int		count,		/* Args count */	 char *		ifname)		/* Dev name */{  struct iwreq		wrq;  int			i;  /* if nothing after the device name - will never happen */  if(count < 1)    {      fprintf(stderr, "Error : too few arguments.\n");      return(-1);    }  /* The other args on the line specify options to be set... */  for(i = 0; i < count; i++)    {      /* ---------- Commit changes to driver ---------- */      if(!strncmp(args[i], "commit", 6))	{	  /* No args */	  IW_SET_EXT_ERR(skfd, ifname, SIOCSIWCOMMIT, &wrq,			 "Commit changes");	  continue;	}      /* ---------- Set network ID ---------- */      if((!strcasecmp(args[i], "nwid")) ||	 (!strcasecmp(args[i], "domain")))	{	  i++;	  if(i >= count)	    ABORT_ARG_NUM("Set NWID", SIOCSIWNWID);	  if((!strcasecmp(args[i], "off")) ||	     (!strcasecmp(args[i], "any")))	    wrq.u.nwid.disabled = 1;	  else	    if(!strcasecmp(args[i], "on"))	      {		/* Get old nwid */		IW_GET_EXT_ERR(skfd, ifname, SIOCGIWNWID, &wrq,			       "Set NWID");		wrq.u.nwid.disabled = 0;	      }	    else	      if(sscanf(args[i], "%lX", (unsigned long *) &(wrq.u.nwid.value))		 != 1)		ABORT_ARG_TYPE("Set NWID", SIOCSIWNWID, args[i]);	      else		wrq.u.nwid.disabled = 0;	  wrq.u.nwid.fixed = 1;	  /* Set new nwid */	  IW_SET_EXT_ERR(skfd, ifname, SIOCSIWNWID, &wrq,			 "Set NWID");	  continue;	}      /* ---------- Set frequency / channel ---------- */      if((!strncmp(args[i], "freq", 4)) ||	 (!strcmp(args[i], "channel")))	{	  double		freq;	  if(++i >= count)	    ABORT_ARG_NUM("Set Frequency", SIOCSIWFREQ);	  if(!strcasecmp(args[i], "auto"))	    {	      wrq.u.freq.m = -1;	      wrq.u.freq.e = 0;	      wrq.u.freq.flags = 0;	    }	  else	    {	      if(!strcasecmp(args[i], "fixed"))		{		  /* Get old bitrate */		  IW_GET_EXT_ERR(skfd, ifname, SIOCGIWFREQ, &wrq,				 "Set Bit Rate");		  wrq.u.freq.flags = IW_FREQ_FIXED;		}	      else			/* Should be a numeric value */		{		  if(sscanf(args[i], "%lg", &(freq)) != 1)		    ABORT_ARG_TYPE("Set Frequency", SIOCSIWFREQ, args[i]);		  if(index(args[i], 'G')) freq *= GIGA;		  if(index(args[i], 'M')) freq *= MEGA;		  if(index(args[i], 'k')) freq *= KILO;		  iw_float2freq(freq, &(wrq.u.freq));		  wrq.u.freq.flags = IW_FREQ_FIXED;		  /* Check for an additional argument */		  if(((i+1) < count) &&		     (!strcasecmp(args[i+1], "auto")))		    {		      wrq.u.freq.flags = 0;		      ++i;		    }		  if(((i+1) < count) &&		     (!strcasecmp(args[i+1], "fixed")))		    {		      wrq.u.freq.flags = IW_FREQ_FIXED;		      ++i;		    }		}	    }	  IW_SET_EXT_ERR(skfd, ifname, SIOCSIWFREQ, &wrq,			 "Set Frequency");	  continue;	}      /* ---------- Set sensitivity ---------- */      if(!strncmp(args[i], "sens", 4))	{	  if(++i >= count)	    ABORT_ARG_NUM("Set Sensitivity", SIOCSIWSENS);	  if(sscanf(args[i], "%i", &(wrq.u.sens.value)) != 1)	    ABORT_ARG_TYPE("Set Sensitivity", SIOCSIWSENS, args[i]);	  IW_SET_EXT_ERR(skfd, ifname, SIOCSIWSENS, &wrq,			 "Set Sensitivity");	  continue;	}      /* ---------- Set encryption stuff ---------- */      if((!strncmp(args[i], "enc", 3)) ||	 (!strcmp(args[i], "key")))	{	  unsigned char	key[IW_ENCODING_TOKEN_MAX];	  if(++i >= count)	    ABORT_ARG_NUM("Set Encode", SIOCSIWENCODE);	  if(!strcasecmp(args[i], "on"))	    {	      /* Get old encryption information */	      wrq.u.data.pointer = (caddr_t) key;	      wrq.u.data.length = IW_ENCODING_TOKEN_MAX;	      wrq.u.data.flags = 0;	      IW_GET_EXT_ERR(skfd, ifname, SIOCGIWENCODE, &wrq,			     "Set Encode");	      wrq.u.data.flags &= ~IW_ENCODE_DISABLED;	/* Enable */	    }	  else	    {	      int	gotone = 0;	      int	oldone;	      int	keylen;	      int	temp;	      wrq.u.data.pointer = (caddr_t) NULL;	      wrq.u.data.flags = 0;	      wrq.u.data.length = 0;	      /* Allow arguments in any order (it's safe) */	      do		{		  oldone = gotone;		  /* -- Check for the key -- */		  if(i < count)		    {		      keylen = iw_in_key_full(skfd, ifname,					      args[i], key, &wrq.u.data.flags);		      if(keylen > 0)			{			  wrq.u.data.length = keylen;			  wrq.u.data.pointer = (caddr_t) key;			  ++i;			  gotone++;			}		    }		  /* -- Check for token index -- */		  if((i < count) &&		     (sscanf(args[i], "[%i]", &temp) == 1) &&		     (temp > 0) && (temp < IW_ENCODE_INDEX))		    {		      wrq.u.encoding.flags |= temp;		      ++i;		      gotone++;		    }		  /* -- Check the various flags -- */		  if((i < count) && (!strcasecmp(args[i], "off")))		    {		      wrq.u.data.flags |= IW_ENCODE_DISABLED;		      ++i;		      gotone++;		    }		  if((i < count) && (!strcasecmp(args[i], "open")))		    {		      wrq.u.data.flags |= IW_ENCODE_OPEN;		      ++i;		      gotone++;		    }		  if((i < count) && (!strncasecmp(args[i], "restricted", 5)))		    {		      wrq.u.data.flags |= IW_ENCODE_RESTRICTED;		      ++i;		      gotone++;		    }		  if((i < count) && (!strncasecmp(args[i], "temporary", 4)))		    {		      wrq.u.data.flags |= IW_ENCODE_TEMP;		      ++i;		      gotone++;		    }		}	      while(gotone != oldone);	      /* Pointer is absent in new API */	      if(wrq.u.data.pointer == NULL)		wrq.u.data.flags |= IW_ENCODE_NOKEY;	      /* Check if we have any invalid argument */	      if(!gotone)		ABORT_ARG_TYPE("Set Encode", SIOCSIWENCODE, args[i]);	      /* Get back to last processed argument */	      --i;	    }	  IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq,			 "Set Encode");	  continue;  	}      /* ---------- Set ESSID ---------- */      if(!strcasecmp(args[i], "essid"))	{	  char		essid[IW_ESSID_MAX_SIZE + 1];	  int		we_kernel_version;	  i++;	  if(i >= count)	    ABORT_ARG_NUM("Set ESSID", SIOCSIWESSID);	  if((!strcasecmp(args[i], "off")) ||	     (!strcasecmp(args[i], "any")))	    {	      wrq.u.essid.flags = 0;	      essid[0] = '\0';	    }	  else	    if(!strcasecmp(args[i], "on"))	      {		/* Get old essid */		memset(essid, '\0', sizeof(essid));		wrq.u.essid.pointer = (caddr_t) essid;		wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;		wrq.u.essid.flags = 0;		IW_GET_EXT_ERR(skfd, ifname, SIOCGIWESSID, &wrq,			       "Set ESSID");		wrq.u.essid.flags = 1;	      }	    else	      {		/* '-' or '--' allow to escape the ESSID string, allowing		 * to set it to the string "any" or "off".		 * This is a big ugly, but it will do for now */		if((!strcmp(args[i], "-")) || (!strcmp(args[i], "--")))		  {		    i++;		    if(i >= count)		      ABORT_ARG_NUM("Set ESSID", SIOCSIWESSID);		  }		/* Check the size of what the user passed us to avoid		 * buffer overflows */		if(strlen(args[i]) > IW_ESSID_MAX_SIZE)		  ABORT_ARG_SIZE("Set ESSID", SIOCSIWESSID, IW_ESSID_MAX_SIZE);		else		  {		    int		temp;		    wrq.u.essid.flags = 1;		    strcpy(essid, args[i]);	/* Size checked, all clear */		    /* Check for ESSID index */		    if(((i+1) < count) &&		       (sscanf(args[i+1], "[%i]", &temp) == 1) &&		       (temp > 0) && (temp < IW_ENCODE_INDEX))		      {			wrq.u.essid.flags = temp;			++i;		      }		  }	      }	  /* Get version from kernel, device may not have range... */	  we_kernel_version = iw_get_kernel_we_version();	  /* Finally set the ESSID value */	  wrq.u.essid.pointer = (caddr_t) essid;	  wrq.u.essid.length = strlen(essid) + 1;	  if(we_kernel_version > 20)	    wrq.u.essid.length--;	  IW_SET_EXT_ERR(skfd, ifname, SIOCSIWESSID, &wrq,			 "Set ESSID");	  continue;	}      /* ---------- Set AP address ---------- */      if(!strcasecmp(args[i], "ap"))	{	  if(++i >= count)	    ABORT_ARG_NUM("Set AP Address", SIOCSIWAP);	  if((!strcasecmp(args[i], "auto")) ||	     (!strcasecmp(args[i], "any")))	    {	      /* Send a broadcast address */	      iw_broad_ether(&(wrq.u.ap_addr));	    }	  else	    {	      if(!strcasecmp(args[i], "off"))		{		  /* Send a NULL address */		  iw_null_ether(&(wrq.u.ap_addr));		}	      else		{		  /* Get the address and check if the interface supports it */		  if(iw_in_addr(skfd, ifname, args[i++], &(wrq.u.ap_addr)) < 0)		    ABORT_ARG_TYPE("Set AP Address", SIOCSIWAP, args[i-1]);		}	    }	  IW_SET_EXT_ERR(skfd, ifname, SIOCSIWAP, &wrq,			 "Set AP Address");	  continue;	}      /* ---------- Set NickName ---------- */      if(!strncmp(args[i], "nick", 4))	{	  int		we_kernel_version;	  i++;	  if(i >= count)	    ABORT_ARG_NUM("Set Nickname", SIOCSIWNICKN);	  if(strlen(args[i]) > IW_ESSID_MAX_SIZE)	    ABORT_ARG_SIZE("Set Nickname", SIOCSIWNICKN, IW_ESSID_MAX_SIZE);	  we_kernel_version = iw_get_kernel_we_version();	  wrq.u.essid.pointer = (caddr_t) args[i];	  wrq.u.essid.length = strlen(args[i]) + 1;

⌨️ 快捷键说明

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