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

📄 iwlist.c

📁 访问网卡的各种信息
💻 C
📖 第 1 页 / 共 4 页
字号:
/*
 *	Wireless Tools
 *
 *		Jean II - HPLB '99 - HPL 99->04
 *
 * This tool can access various piece of information on the card
 * not part of iwconfig...
 * You need to link this code against "iwlist.c" and "-lm".
 *
 * This file is released under the GPL license.
 *     Copyright (c) 1997-2004 Jean Tourrilhes <jt@hpl.hp.com>
 */

#include "iwlib.h"		/* Header */
#include <sys/time.h>

/****************************** TYPES ******************************/

/*
 * Scan state and meta-information, used to decode events...
 */
typedef struct iwscan_state
{
  /* State */
  int			ap_num;		/* Access Point number 1->N */
  int			val_index;	/* Value in table 0->(N-1) */
} iwscan_state;


/*********************** FREQUENCIES/CHANNELS ***********************/

/*------------------------------------------------------------------*/
/*
 * Print the number of channels and available frequency for the device
 */
static int
print_freq_info(int		skfd,
                char *		ifname,
                char *		args[],		/* Command line args */
                int		count)		/* Args count */
{
  struct iwreq		wrq;
  struct iw_range	range;
  double		freq;
  int			k;
  int			channel;
  char			buffer[128];	/* Temporary buffer */

  /* Avoid "Unused parameter" warning */
  args = args; count = count;

  /* Get list of frequencies / channels */
  if(iw_get_range_info(skfd, ifname, &range) < 0)
      fprintf(stderr, "%-8.16s  no frequency information.\n\n",
                      ifname);
  else
    {
      if(range.num_frequency > 0)
        {
          printf("%-8.16s  %d channels in total; available frequencies :\n",
                 ifname, range.num_channels);
          /* Print them all */
          for(k = 0; k < range.num_frequency; k++)
            {
              freq = iw_freq2float(&(range.freq[k]));
              iw_print_freq_value(buffer, sizeof(buffer), freq);
              printf("          Channel %.2d : %s\n",
                     range.freq[k].i, buffer);
            }
        }
      else
        printf("%-8.16s  %d channels\n",
               ifname, range.num_channels);

      /* Get current frequency / channel and display it */
      if(iw_get_ext(skfd, ifname, SIOCGIWFREQ, &wrq) >= 0)
        {
          freq = iw_freq2float(&(wrq.u.freq));
          channel = iw_freq_to_channel(freq, &range);
          iw_print_freq(buffer, sizeof(buffer),
                        freq, channel, wrq.u.freq.flags);
          printf("          Current %s\n\n", buffer);
        }
    }
  return(0);
}

/************************ ACCESS POINT LIST ************************/
/*
 * Note : now that we have scanning support, this is depracted and
 * won't survive long. Actually, next version it's out !
 */

/*------------------------------------------------------------------*/
/*
 * Display the list of ap addresses and the associated stats
 * Exacly the same as the spy list, only with different IOCTL and messages
 */
static int
print_ap_info(int	skfd,
              char *	ifname,
              char *	args[],		/* Command line args */
              int	count)		/* Args count */
{
  struct iwreq		wrq;
  char		buffer[(sizeof(struct iw_quality) +
                        sizeof(struct sockaddr)) * IW_MAX_AP];
  char		temp[128];
  struct sockaddr *	hwa;
  struct iw_quality *	qual;
  iwrange	range;
  int		has_range = 0;
  int		has_qual = 0;
  int		n;
  int		i;

  /* Avoid "Unused parameter" warning */
  args = args; count = count;

  /* Collect stats */
  wrq.u.data.pointer = (caddr_t) buffer;
  wrq.u.data.length = IW_MAX_AP;
  wrq.u.data.flags = 0;
  if(iw_get_ext(skfd, ifname, SIOCGIWAPLIST, &wrq) < 0)
    {
      fprintf(stderr, "%-8.16s  Interface doesn't have a list of Peers/Access-Points\n\n", ifname);
      return(-1);
    }

  /* Number of addresses */
  n = wrq.u.data.length;
  has_qual = wrq.u.data.flags;

  /* The two lists */
  hwa = (struct sockaddr *) buffer;
  qual = (struct iw_quality *) (buffer + (sizeof(struct sockaddr) * n));

  /* Check if we have valid mac address type */
  if(iw_check_mac_addr_type(skfd, ifname) < 0)
    {
      fprintf(stderr, "%-8.16s  Interface doesn't support MAC addresses\n\n", ifname);
      return(-2);
    }

  /* Get range info if we can */
  if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
    has_range = 1;

  /* Display it */
  if(n == 0)
    printf("%-8.16s  No Peers/Access-Point in range\n", ifname);
  else
    printf("%-8.16s  Peers/Access-Points in range:\n", ifname);
  for(i = 0; i < n; i++)
    {
      if(has_qual)
        {
          /* Print stats for this address */
          printf("    %s : ", iw_saether_ntop(&hwa[i], temp));
          iw_print_stats(temp, sizeof(buffer), &qual[i], &range, has_range);
          printf("%s\n", temp);
        }
      else
        /* Only print the address */
        printf("    %s\n", iw_saether_ntop(&hwa[i], temp));
    }
  printf("\n");
  return(0);
}

/***************************** BITRATES *****************************/

/*------------------------------------------------------------------*/
/*
 * Print the number of available bitrates for the device
 */
static int
print_bitrate_info(int		skfd,
                   char *	ifname,
                   char *	args[],		/* Command line args */
                   int		count)		/* Args count */
{
  struct iwreq		wrq;
  struct iw_range	range;
  int			k;
  char			buffer[128];

  /* Avoid "Unused parameter" warning */
  args = args; count = count;

  /* Extract range info */
  if(iw_get_range_info(skfd, ifname, &range) < 0)
      fprintf(stderr, "%-8.16s  no bit-rate information.\n\n",
                      ifname);
  else
    {
      if((range.num_bitrates > 0) && (range.num_bitrates <= IW_MAX_BITRATES))
        {
          printf("%-8.16s  %d available bit-rates :\n",
                 ifname, range.num_bitrates);
          /* Print them all */
          for(k = 0; k < range.num_bitrates; k++)
            {
              iw_print_bitrate(buffer, sizeof(buffer), range.bitrate[k]);
              /* Maybe this should be %10s */
              printf("\t  %s\n", buffer);
            }
        }
      else
        printf("%-8.16s  unknown bit-rate information.\n", ifname);

      /* Get current bit rate */
      if(iw_get_ext(skfd, ifname, SIOCGIWRATE, &wrq) >= 0)
        {
          iw_print_bitrate(buffer, sizeof(buffer), wrq.u.bitrate.value);
          printf("          Current Bit Rate%c%s\n\n",
                 (wrq.u.bitrate.fixed ? '=' : ':'), buffer);
        }
    }
  return(0);
}

/************************* ENCRYPTION KEYS *************************/

/*------------------------------------------------------------------*/
/*
 * Print the number of available encryption key for the device
 */
static int
print_keys_info(int		skfd,
                char *		ifname,
                char *		args[],		/* Command line args */
                int		count)		/* Args count */
{
  struct iwreq		wrq;
  struct iw_range	range;
  unsigned char		key[IW_ENCODING_TOKEN_MAX];
  int			k;
  char			buffer[128];

  /* Avoid "Unused parameter" warning */
  args = args; count = count;

  /* Extract range info */
  if(iw_get_range_info(skfd, ifname, &range) < 0)
      fprintf(stderr, "%-8.16s  no encryption keys information.\n\n",
                      ifname);
  else
    {
      printf("%-8.16s  ", ifname);
      /* Print key sizes */
      if((range.num_encoding_sizes > 0) &&
         (range.num_encoding_sizes < IW_MAX_ENCODING_SIZES))
        {
          printf("%d key sizes : %d", range.num_encoding_sizes,
                 range.encoding_size[0] * 8);
          /* Print them all */
          for(k = 1; k < range.num_encoding_sizes; k++)
            printf(", %d", range.encoding_size[k] * 8);
          printf("bits\n          ");
        }
      /* Print the keys and associate mode */
      printf("%d keys available :\n", range.max_encoding_tokens);
      for(k = 1; k <= range.max_encoding_tokens; k++)
        {
          wrq.u.data.pointer = (caddr_t) key;
          wrq.u.data.length = IW_ENCODING_TOKEN_MAX;
          wrq.u.data.flags = k;
          if(iw_get_ext(skfd, ifname, SIOCGIWENCODE, &wrq) < 0)
            {
              fprintf(stderr, "Error reading wireless keys (SIOCGIWENCODE): %s\n", strerror(errno));
              break;
            }
          if((wrq.u.data.flags & IW_ENCODE_DISABLED) ||
             (wrq.u.data.length == 0))
            printf("\t\t[%d]: off\n", k);
          else
            {
              /* Display the key */
              iw_print_key(buffer, sizeof(buffer),
                           key, wrq.u.data.length, wrq.u.data.flags);
              printf("\t\t[%d]: %s", k, buffer);

              /* Other info... */
              printf(" (%d bits)", wrq.u.data.length * 8);
              printf("\n");
            }
        }
      /* Print current key and mode */
      wrq.u.data.pointer = (caddr_t) key;
      wrq.u.data.length = IW_ENCODING_TOKEN_MAX;
      wrq.u.data.flags = 0;	/* Set index to zero to get current */
      if(iw_get_ext(skfd, ifname, SIOCGIWENCODE, &wrq) >= 0)
        {
          /* Note : if above fails, we have already printed an error
           * message int the loop above */
          printf("          Current Transmit Key: [%d]\n",
                 wrq.u.data.flags & IW_ENCODE_INDEX);
          if(wrq.u.data.flags & IW_ENCODE_RESTRICTED)
            printf("          Security mode:restricted\n");
          if(wrq.u.data.flags & IW_ENCODE_OPEN)
            printf("          Security mode:open\n");
        }

      /* Print WPA/802.1x/802.11i security parameters */
      if(range.we_version_compiled > 17)
        {
          /* Display advance encryption capabilities */
          if(range.enc_capa)
            {
              const char *	auth_string[] = { "WPA",
                                                  "WPA2",
                                                  "CIPHER TKIP",
                                                  "CIPHER CCMP" };
              const int		auth_num = (sizeof(auth_string) /
                                            sizeof(auth_string[1]));
              int		i;
              int		mask = 0x1;

              printf("          Authentication capabilities :\n");
              for(i = 0; i < auth_num; i++)
                {
                  if(range.enc_capa & mask)
                    printf("\t\t%s\n", auth_string[i]);
                  mask <<= 1;
                }
            }

          /* Current values for authentication */
          wrq.u.param.flags = IW_AUTH_KEY_MGMT;
          if(iw_get_ext(skfd, ifname, SIOCGIWAUTH, &wrq) >= 0)
              printf("          Current key_mgmt:0x%X\n",
                     wrq.u.param.value);

          wrq.u.param.flags = IW_AUTH_CIPHER_PAIRWISE;
          if(iw_get_ext(skfd, ifname, SIOCGIWAUTH, &wrq) >= 0)
              printf("          Current cipher_pairwise:0x%X\n",
                     wrq.u.param.value);

          wrq.u.param.flags = IW_AUTH_CIPHER_GROUP;
          if(iw_get_ext(skfd, ifname, SIOCGIWAUTH, &wrq) >= 0)
            printf("          Current cipher_group:0x%X\n",
                   wrq.u.param.value);
        }

     printf("\n\n");
    }
  return(0);
}

/************************* POWER MANAGEMENT *************************/

/*------------------------------------------------------------------*/
/*
 * Print Power Management info for each device
 */
static inline int
get_pm_value(int		skfd,
             char *		ifname,
             struct iwreq *	pwrq,
             int		flags,
             char *		buffer,
             int                buflen)
{
  /* Get Another Power Management value */
  pwrq->u.power.flags = flags;
  if(iw_get_ext(skfd, ifname, SIOCGIWPOWER, pwrq) >= 0)
    {
      /* Let's check the value and its type */
      if(pwrq->u.power.flags & IW_POWER_TYPE)
        {
          iw_print_pm_value(buffer, buflen,
                            pwrq->u.power.value, pwrq->u.power.flags);
          printf("\n                 %s", buffer);
        }
    }
  return(pwrq->u.power.flags);
}

/*------------------------------------------------------------------*/
/*
 * Print Power Management info for each device
 */
static int
print_pm_info(int		skfd,
              char *		ifname,
              char *		args[],		/* Command line args */
              int		count)		/* Args count */
{
  struct iwreq		wrq;
  struct iw_range	range;
  char			buffer[128];

  /* Avoid "Unused parameter" warning */
  args = args; count = count;

  /* Extract range info */
  if((iw_get_range_info(skfd, ifname, &range) < 0) ||
     (range.we_version_compiled < 10))
      fprintf(stderr, "%-8.16s  no power management information.\n\n",
                      ifname);
  else
    {
      printf("%-8.16s  ", ifname);

      /* Display modes availables */
      if(range.pm_capa & IW_POWER_MODE)
        {
          printf("Supported modes :\n          ");
          if(range.pm_capa & (IW_POWER_UNICAST_R | IW_POWER_MULTICAST_R))
            printf("\t\to Receive all packets (unicast & multicast)\n          ");
          if(range.pm_capa & IW_POWER_UNICAST_R)
            printf("\t\to Receive Unicast only (discard multicast)\n          ");

⌨️ 快捷键说明

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