OSDN Git Service

v24
[android-x86/external-wireless-tools.git] / wireless_tools / iwspy.c
index ead9a62..6b02c1b 100644 (file)
@@ -4,12 +4,13 @@
  *             Jean II - HPLB '99
  *
  * This tool can manipulate the spy list : add addresses and display stat
- * You need to link this code against "iwcommon.c" and "-lm".
+ * You need to link this code against "iwlib.c" and "-lm".
  *
  * This file is released under the GPL license.
+ *     Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
  */
 
-#include "iwcommon.h"          /* Header */
+#include "iwlib.h"             /* Header */
 
 /************************* DISPLAY ROUTINES **************************/
 
 /*
  * Display the spy list of addresses and the associated stats
  */
-static void
+static int
 print_spy_info(int     skfd,
-              char *   ifname)
+              char *   ifname,
+              char *   args[],
+              int      count)
 {
   struct iwreq         wrq;
   char         buffer[(sizeof(struct iw_quality) +
                        sizeof(struct sockaddr)) * IW_MAX_SPY];
+  char         temp[128];
   struct sockaddr      hwa[IW_MAX_SPY];
   struct iw_quality    qual[IW_MAX_SPY];
   iwrange      range;
@@ -31,31 +35,31 @@ print_spy_info(int  skfd,
   int          n;
   int          i;
 
+  /* Avoid "Unused parameter" warning */
+  args = args; count = count;
+
   /* Collect stats */
-  strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
   wrq.u.data.pointer = (caddr_t) buffer;
-  wrq.u.data.length = 0;
+  wrq.u.data.length = IW_MAX_SPY;
   wrq.u.data.flags = 0;
-  if(ioctl(skfd, SIOCGIWSPY, &wrq) < 0)
+  if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
     {
       fprintf(stderr, "%-8.8s  Interface doesn't support wireless statistic collection\n\n", ifname);
-      return;
+      return(-1);
     }
 
   /* Number of addresses */
   n = wrq.u.data.length;
 
-
-
-  /* Check if we have valid address types */
-  if(check_addr_type(skfd, ifname) < 0)
+  /* Check if we have valid mac address type */
+  if(iw_check_mac_addr_type(skfd, ifname) < 0)
     {
-      fprintf(stderr, "%-8.8s  Interface doesn't support MAC & IP addresses\n\n", ifname);
-      return;
+      fprintf(stderr, "%-8.8s  Interface doesn't support MAC addresses\n\n", ifname);
+      return(-2);
     }
 
   /* Get range info if we can */
-  if(get_range_info(skfd, ifname, &(range)) >= 0)
+  if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
     has_range = 1;
 
   /* Display it */
@@ -72,37 +76,20 @@ print_spy_info(int  skfd,
   for(i = 0; i < n; i++)
     {
       /* Print stats for each address */
-      printf("    %s : ", pr_ether(hwa[i].sa_data));
-      print_stats(stdout, &qual[i], &range, has_range);
+      printf("    %s : ", iw_pr_ether(temp, hwa[i].sa_data));
+      iw_print_stats(temp, &qual[i], &range, has_range);
+      printf("%s\n", temp);
     }
-  printf("\n");
-}
-
-/*------------------------------------------------------------------*/
-/*
- * Get info on all devices and print it on the screen
- */
-static void
-print_spy_devices(int          skfd)
-{
-  char         buff[1024];
-  struct ifconf ifc;
-  struct ifreq *ifr;
-  int i;
-
-  /* Get list of active devices */
-  ifc.ifc_len = sizeof(buff);
-  ifc.ifc_buf = buff;
-  if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
+#if WIRELESS_EXT > 11
+  if((n > 0) && (has_range))
     {
-      fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
-      return;
+      iw_print_stats(temp, &range.avg_qual, &range, has_range);
+      printf("    typical/average   : %s\n", temp);
     }
-  ifr = ifc.ifc_req;
+#endif /* WIRELESS_EXT > 11 */
 
-  /* Print them */
-  for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
-    print_spy_info(skfd, ifr->ifr_name);
+  printf("\n");
+  return(0);
 }
 
 /************************* SETTING ROUTINES **************************/
@@ -124,70 +111,71 @@ set_spy_info(int          skfd,           /* The socket */
 
   /* Read command line */
   i = 0;       /* first arg to read */
-  nbr = 0;     /* Number of args readen so far */
-
-  /* Check if we have valid address types */
-  if(check_addr_type(skfd, ifname) < 0)
-    {
-      fprintf(stderr, "%-8.8s  Interface doesn't support MAC & IP addresses\n", ifname);
-      return(-1);
-    }
+  nbr = 0;     /* Number of args read so far */
 
   /* "off" : disable functionality (set 0 addresses) */
   if(!strcmp(args[0], "off"))
-    i = count; /* hack */
-
-  /* "+" : add all addresses already in the driver */
-  if(!strcmp(args[0], "+"))
+    i = 1;     /* skip the "off" */
+  else
     {
-      char     buffer[(sizeof(struct iw_quality) +
+      /* "+" : add all addresses already in the driver */
+      if(!strcmp(args[0], "+"))
+       {
+         char  buffer[(sizeof(struct iw_quality) +
                        sizeof(struct sockaddr)) * IW_MAX_SPY];
 
-      strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
-      wrq.u.data.pointer = (caddr_t) buffer;
-      wrq.u.data.length = 0;
-      wrq.u.data.flags = 0;
-      if(ioctl(skfd, SIOCGIWSPY, &wrq) < 0)
-       {
-         fprintf(stderr, "Interface doesn't accept reading addresses...\n");
-         fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
-         return(-1);
+         /* Check if we have valid mac address type */
+         if(iw_check_mac_addr_type(skfd, ifname) < 0)
+           {
+             fprintf(stderr, "%-8.8s  Interface doesn't support MAC addresses\n", ifname);
+             return(-1);
+           }
+
+         wrq.u.data.pointer = (caddr_t) buffer;
+         wrq.u.data.length = 0;
+         wrq.u.data.flags = 0;
+         if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
+           {
+             fprintf(stderr, "Interface doesn't accept reading addresses...\n");
+             fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
+             return(-1);
+           }
+
+         /* Copy old addresses */
+         nbr = wrq.u.data.length;
+         memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
+
+         i = 1;        /* skip the "+" */
        }
 
-      /* Copy old addresses */
-      nbr = wrq.u.data.length;
-      memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
-
-      i = 1;   /* skip the "+" */
-    }
-
-  /* Read other args on command line */
-  while((i < count) && (nbr < IW_MAX_SPY))
-    {
-      if(in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
-       continue;
-      nbr++;
-    }
+      /* Read other args on command line */
+      while((i < count) && (nbr < IW_MAX_SPY))
+       {
+         /* Get the address and check if the interface supports it */
+         if(iw_in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
+           continue;
+         nbr++;
+       }
 
-  /* Check the number of addresses */
-  if((nbr == 0) && strcmp(args[0], "off"))
-    {
-      fprintf(stderr, "No valid addresses found : exiting...\n");
-      exit(0);
+      /* Check the number of addresses */
+      if(nbr == 0)
+       {
+         fprintf(stderr, "No valid addresses found : exiting...\n");
+         return(-1);
+       }
     }
 
   /* Check if there is some remaining arguments */
   if(i < count)
     {
-      fprintf(stderr, "Got only the first %d addresses, remaining discarded\n", IW_MAX_SPY);
+      fprintf(stderr, "Got only the first %d arguments, remaining discarded\n", i);
     }
 
   /* Time to do send addresses to the driver */
-  strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
   wrq.u.data.pointer = (caddr_t) hw_address;
   wrq.u.data.length = nbr;
   wrq.u.data.flags = 0;
-  if(ioctl(skfd, SIOCSIWSPY, &wrq) < 0)
+  if(iw_set_ext(skfd, ifname, SIOCSIWSPY, &wrq) < 0)
     {
       fprintf(stderr, "Interface doesn't accept addresses...\n");
       fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno));
@@ -207,50 +195,36 @@ int
 main(int       argc,
      char **   argv)
 {
-  int skfd = -1;               /* generic raw socket desc.     */
+  int skfd;                    /* generic raw socket desc.     */
   int goterr = 0;
 
   /* Create a channel to the NET kernel. */
-  if((skfd = sockets_open()) < 0)
+  if((skfd = iw_sockets_open()) < 0)
     {
       perror("socket");
-      exit(-1);
+      return(-1);
     }
 
   /* No argument : show the list of all device + info */
   if(argc == 1)
-    {
-      print_spy_devices(skfd);
-      close(skfd);
-      exit(0);
-    }
-
-  /* Special cases take one... */
-  /* Help */
-  if((!strncmp(argv[1], "-h", 9)) ||
-     (!strcmp(argv[1], "--help")))
-    {
+    iw_enum_devices(skfd, &print_spy_info, NULL, 0);
+  else
+    /* Special cases take one... */
+    /* Help */
+    if((!strncmp(argv[1], "-h", 9)) ||
+       (!strcmp(argv[1], "--help")))
       fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n");
-      fprintf(stderr, "             interface [freq]\n");
-      fprintf(stderr, "             interface [ap]\n");
-      close(skfd);
-      exit(0);
-    }
-
-  /* The device name must be the first argument */
-  /* Name only : show spy list for that device only */
-  if(argc == 2)
-    {
-      print_spy_info(skfd, argv[1]);
-      close(skfd);
-      exit(0);
-    }
-
-  /* Otherwise, it's a list of address to set in the spy list */
-  goterr = set_spy_info(skfd, argv + 2, argc - 2, argv[1]);
+    else
+      /* The device name must be the first argument */
+      /* Name only : show spy list for that device only */
+      if(argc == 2)
+       print_spy_info(skfd, argv[1], NULL, 0);
+      else
+       /* Otherwise, it's a list of address to set in the spy list */
+       goterr = set_spy_info(skfd, argv + 2, argc - 2, argv[1]);
 
   /* Close the socket. */
   close(skfd);
 
-  return(1);
+  return(goterr);
 }