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

📄 iwconfig.c

📁 执行无线网卡的扫描命令的源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
            }

          /* Let's check if nothing (simply on) */
          if(info->retry.flags == IW_RETRY_ON)
            printf(":on");
         }
      printf("   ");
      tokens += 5;	/* Between 3 and 5, depend on flags */
    }

  /* Display the RTS threshold */
  if(info->has_rts)
    {
      /* Disabled ? */
      if(info->rts.disabled)
        printf("RTS thr:off   ");
      else
        {
          /* Fixed ? */
          if(info->rts.fixed)
            printf("RTS thr=");
          else
            printf("RTS thr:");

          printf("%d B   ", info->rts.value);
        }
      tokens += 3;
    }

  /* Display the fragmentation threshold */
  if(info->has_frag)
    {
      /* A bit of clever formatting */
      if(tokens > 10)
        {
          printf("\n          ");
          tokens = 0;
        }
      tokens +=4;

      /* Disabled ? */
      if(info->frag.disabled)
        printf("Fragment thr:off");
      else
        {
          /* Fixed ? */
          if(info->frag.fixed)
            printf("Fragment thr=");
          else
            printf("Fragment thr:");

          printf("%d B   ", info->frag.value);
        }
    }

  /* Formating */
  if(tokens > 0)
    printf("\n          ");

  /* Display encryption information */
  /* Note : we display only the "current" key, use iwlist to list all keys */
  if(info->b.has_key)
    {
      printf("Encryption key:");
      if((info->b.key_flags & IW_ENCODE_DISABLED) || (info->b.key_size == 0))
        printf("off");
      else
        {
          /* Display the key */
          iw_print_key(buffer, sizeof(buffer),
                       info->b.key, info->b.key_size, info->b.key_flags);
          printf("%s", buffer);

          /* Other info... */
          if((info->b.key_flags & IW_ENCODE_INDEX) > 1)
            printf(" [%d]", info->b.key_flags & IW_ENCODE_INDEX);
          if(info->b.key_flags & IW_ENCODE_RESTRICTED)
            printf("   Security mode:restricted");
          if(info->b.key_flags & IW_ENCODE_OPEN)
            printf("   Security mode:open");
         }
      printf("\n          ");
    }

#ifdef DISPLAY_WPA
  /* Display WPA/802.1x/802.11i security parameters */
  if(info->has_auth_key_mgmt || info->has_auth_cipher_pairwise ||
     info->has_auth_cipher_group)
    {
      printf("Auth params:");
      if(info->has_auth_key_mgmt)
        printf(" key_mgmt:0x%X ", info->auth_key_mgmt);
      if(info->has_auth_cipher_pairwise)
        printf(" cipher_pairwise:0x%X ", info->auth_cipher_pairwise);
      if(info->has_auth_cipher_group)
        printf(" cipher_group:0x%X ", info->auth_cipher_group);
      printf("\n          ");
    }
#endif

  /* Display Power Management information */
  /* Note : we display only one parameter, period or timeout. If a device
   * (such as HiperLan) has both, the user need to use iwlist... */
  if(info->has_power)	/* I hope the device has power ;-) */
    { 
      printf("Power Management");
      /* Disabled ? */
      if(info->power.disabled)
        printf(":off");
      else
        {
          /* Let's check the value and its type */
          if(info->power.flags & IW_POWER_TYPE)
            {
              iw_print_pm_value(buffer, sizeof(buffer),
                                info->power.value, info->power.flags);
              printf("%s  ", buffer);
            }

          /* Let's check the mode */
          iw_print_pm_mode(buffer, sizeof(buffer), info->power.flags);
          printf("%s", buffer);

          /* 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 int
print_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 int
set_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;
                    }
                }

⌨️ 快捷键说明

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