OSDN Git Service

ead9a628bea4614f67d3a6a0a393f79a578f5a3d
[android-x86/external-wireless-tools.git] / wireless_tools / iwspy.c
1 /*
2  *      Wireless Tools
3  *
4  *              Jean II - HPLB '99
5  *
6  * This tool can manipulate the spy list : add addresses and display stat
7  * You need to link this code against "iwcommon.c" and "-lm".
8  *
9  * This file is released under the GPL license.
10  */
11
12 #include "iwcommon.h"           /* Header */
13
14 /************************* DISPLAY ROUTINES **************************/
15
16 /*------------------------------------------------------------------*/
17 /*
18  * Display the spy list of addresses and the associated stats
19  */
20 static void
21 print_spy_info(int      skfd,
22                char *   ifname)
23 {
24   struct iwreq          wrq;
25   char          buffer[(sizeof(struct iw_quality) +
26                         sizeof(struct sockaddr)) * IW_MAX_SPY];
27   struct sockaddr       hwa[IW_MAX_SPY];
28   struct iw_quality     qual[IW_MAX_SPY];
29   iwrange       range;
30   int           has_range = 0;
31   int           n;
32   int           i;
33
34   /* Collect stats */
35   strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
36   wrq.u.data.pointer = (caddr_t) buffer;
37   wrq.u.data.length = 0;
38   wrq.u.data.flags = 0;
39   if(ioctl(skfd, SIOCGIWSPY, &wrq) < 0)
40     {
41       fprintf(stderr, "%-8.8s  Interface doesn't support wireless statistic collection\n\n", ifname);
42       return;
43     }
44
45   /* Number of addresses */
46   n = wrq.u.data.length;
47
48
49
50   /* Check if we have valid address types */
51   if(check_addr_type(skfd, ifname) < 0)
52     {
53       fprintf(stderr, "%-8.8s  Interface doesn't support MAC & IP addresses\n\n", ifname);
54       return;
55     }
56
57   /* Get range info if we can */
58   if(get_range_info(skfd, ifname, &(range)) >= 0)
59     has_range = 1;
60
61   /* Display it */
62   if(n == 0)
63     printf("%-8.8s  No statistics to collect\n", ifname);
64   else
65     printf("%-8.8s  Statistics collected:\n", ifname);
66  
67   /* The two lists */
68
69   memcpy(hwa, buffer, n * sizeof(struct sockaddr));
70   memcpy(qual, buffer + n*sizeof(struct sockaddr), n*sizeof(struct iw_quality));
71
72   for(i = 0; i < n; i++)
73     {
74       /* Print stats for each address */
75       printf("    %s : ", pr_ether(hwa[i].sa_data));
76       print_stats(stdout, &qual[i], &range, has_range);
77     }
78   printf("\n");
79 }
80
81 /*------------------------------------------------------------------*/
82 /*
83  * Get info on all devices and print it on the screen
84  */
85 static void
86 print_spy_devices(int           skfd)
87 {
88   char          buff[1024];
89   struct ifconf ifc;
90   struct ifreq *ifr;
91   int i;
92
93   /* Get list of active devices */
94   ifc.ifc_len = sizeof(buff);
95   ifc.ifc_buf = buff;
96   if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
97     {
98       fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
99       return;
100     }
101   ifr = ifc.ifc_req;
102
103   /* Print them */
104   for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
105     print_spy_info(skfd, ifr->ifr_name);
106 }
107
108 /************************* SETTING ROUTINES **************************/
109
110 /*------------------------------------------------------------------*/
111 /*
112  * Set list of addresses specified on command line in the driver.
113  */
114 static int
115 set_spy_info(int                skfd,           /* The socket */
116              char *             args[],         /* Command line args */
117              int                count,          /* Args count */
118              char *             ifname)         /* Dev name */
119 {
120   struct iwreq          wrq;
121   int                   i;
122   int                   nbr;            /* Number of valid addresses */
123   struct sockaddr       hw_address[IW_MAX_SPY];
124
125   /* Read command line */
126   i = 0;        /* first arg to read */
127   nbr = 0;      /* Number of args readen so far */
128
129   /* Check if we have valid address types */
130   if(check_addr_type(skfd, ifname) < 0)
131     {
132       fprintf(stderr, "%-8.8s  Interface doesn't support MAC & IP addresses\n", ifname);
133       return(-1);
134     }
135
136   /* "off" : disable functionality (set 0 addresses) */
137   if(!strcmp(args[0], "off"))
138     i = count;  /* hack */
139
140   /* "+" : add all addresses already in the driver */
141   if(!strcmp(args[0], "+"))
142     {
143       char      buffer[(sizeof(struct iw_quality) +
144                         sizeof(struct sockaddr)) * IW_MAX_SPY];
145
146       strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
147       wrq.u.data.pointer = (caddr_t) buffer;
148       wrq.u.data.length = 0;
149       wrq.u.data.flags = 0;
150       if(ioctl(skfd, SIOCGIWSPY, &wrq) < 0)
151         {
152           fprintf(stderr, "Interface doesn't accept reading addresses...\n");
153           fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
154           return(-1);
155         }
156
157       /* Copy old addresses */
158       nbr = wrq.u.data.length;
159       memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
160
161       i = 1;    /* skip the "+" */
162     }
163
164   /* Read other args on command line */
165   while((i < count) && (nbr < IW_MAX_SPY))
166     {
167       if(in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
168         continue;
169       nbr++;
170     }
171
172   /* Check the number of addresses */
173   if((nbr == 0) && strcmp(args[0], "off"))
174     {
175       fprintf(stderr, "No valid addresses found : exiting...\n");
176       exit(0);
177     }
178
179   /* Check if there is some remaining arguments */
180   if(i < count)
181     {
182       fprintf(stderr, "Got only the first %d addresses, remaining discarded\n", IW_MAX_SPY);
183     }
184
185   /* Time to do send addresses to the driver */
186   strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
187   wrq.u.data.pointer = (caddr_t) hw_address;
188   wrq.u.data.length = nbr;
189   wrq.u.data.flags = 0;
190   if(ioctl(skfd, SIOCSIWSPY, &wrq) < 0)
191     {
192       fprintf(stderr, "Interface doesn't accept addresses...\n");
193       fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno));
194       return(-1);
195     }
196
197   return(0);
198 }
199
200 /******************************* MAIN ********************************/
201
202 /*------------------------------------------------------------------*/
203 /*
204  * The main !
205  */
206 int
207 main(int        argc,
208      char **    argv)
209 {
210   int skfd = -1;                /* generic raw socket desc.     */
211   int goterr = 0;
212
213   /* Create a channel to the NET kernel. */
214   if((skfd = sockets_open()) < 0)
215     {
216       perror("socket");
217       exit(-1);
218     }
219
220   /* No argument : show the list of all device + info */
221   if(argc == 1)
222     {
223       print_spy_devices(skfd);
224       close(skfd);
225       exit(0);
226     }
227
228   /* Special cases take one... */
229   /* Help */
230   if((!strncmp(argv[1], "-h", 9)) ||
231      (!strcmp(argv[1], "--help")))
232     {
233       fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n");
234       fprintf(stderr, "             interface [freq]\n");
235       fprintf(stderr, "             interface [ap]\n");
236       close(skfd);
237       exit(0);
238     }
239
240   /* The device name must be the first argument */
241   /* Name only : show spy list for that device only */
242   if(argc == 2)
243     {
244       print_spy_info(skfd, argv[1]);
245       close(skfd);
246       exit(0);
247     }
248
249   /* Otherwise, it's a list of address to set in the spy list */
250   goterr = set_spy_info(skfd, argv + 2, argc - 2, argv[1]);
251
252   /* Close the socket. */
253   close(skfd);
254
255   return(1);
256 }