4 * Jean II - HPLB '99 - HPL 99->04
6 * This tool can manipulate the spy list : add addresses and display stat
7 * You need to link this code against "iwlib.c" and "-lm".
9 * This file is released under the GPL license.
10 * Copyright (c) 1997-2004 Jean Tourrilhes <jt@hpl.hp.com>
13 #include "iwlib.h" /* Header */
15 /************************* DISPLAY ROUTINES **************************/
17 /*------------------------------------------------------------------*/
19 * Display the spy list of addresses and the associated stats
22 print_spy_info(int skfd,
28 char buffer[(sizeof(struct iw_quality) +
29 sizeof(struct sockaddr)) * IW_MAX_SPY];
31 struct sockaddr * hwa;
32 struct iw_quality * qual;
38 /* Avoid "Unused parameter" warning */
39 args = args; count = count;
42 wrq.u.data.pointer = (caddr_t) buffer;
43 wrq.u.data.length = IW_MAX_SPY;
45 if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
47 fprintf(stderr, "%-8.16s Interface doesn't support wireless statistic collection\n\n", ifname);
51 /* Number of addresses */
52 n = wrq.u.data.length;
54 /* Check if we have valid mac address type */
55 if(iw_check_mac_addr_type(skfd, ifname) < 0)
57 fprintf(stderr, "%-8.16s Interface doesn't support MAC addresses\n\n", ifname);
61 /* Get range info if we can */
62 if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
67 printf("%-8.16s No statistics to collect\n", ifname);
69 printf("%-8.16s Statistics collected:\n", ifname);
72 hwa = (struct sockaddr *) buffer;
73 qual = (struct iw_quality *) (buffer + (sizeof(struct sockaddr) * n));
75 for(i = 0; i < n; i++)
77 /* Print stats for each address */
78 printf(" %s : ", iw_pr_ether(temp, hwa[i].sa_data));
79 iw_print_stats(temp, sizeof(temp), &qual[i], &range, has_range);
83 if((n > 0) && (has_range) && (range.we_version_compiled > 11))
87 /* Get /proc/net/wireless */
88 if(iw_get_stats(skfd, ifname, &stats, &range, has_range) >= 0)
90 iw_print_stats(temp, sizeof(temp), &stats.qual, &range, has_range);
91 printf(" Link/Cell/AP : %s\n", temp);
92 /* Display the static data */
93 iw_print_stats(temp, sizeof(temp),
94 &range.avg_qual, &range, has_range);
95 printf(" Typical/Reference : %s\n", temp);
103 /*------------------------------------------------------------------*/
105 * Get spy thresholds from the driver and display
108 get_spy_threshold(int skfd, /* The socket */
109 char * ifname, /* Dev name */
110 char * args[], /* Command line args */
111 int count) /* Args count */
114 struct iw_thrspy threshold;
118 /* Avoid "Unused parameter" warning */
119 args = args; count = count;
121 /* Time to send thresholds to the driver */
122 wrq.u.data.pointer = (caddr_t) &threshold;
123 wrq.u.data.length = 1;
124 wrq.u.data.flags = 0;
125 if(iw_set_ext(skfd, ifname, SIOCGIWTHRSPY, &wrq) < 0)
127 fprintf(stderr, "Interface doesn't support thresholds...\n");
128 fprintf(stderr, "SIOCGIWTHRSPY: %s\n", strerror(errno));
132 /* Get range info if we can */
133 if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
136 /* Display thresholds */
137 if((has_range) && (threshold.low.level))
139 /* If the statistics are in dBm */
140 if(threshold.low.level > range.max_qual.level)
142 /* Statistics are in dBm (absolute power measurement) */
143 printf("%-8.16s Low threshold:%d dBm High threshold:%d dBm\n\n",
145 threshold.low.level - 0x100, threshold.high.level - 0x100);
149 /* Statistics are relative values (0 -> max) */
150 printf("%-8.16s Low threshold:%d/%d High threshold:%d/%d\n\n",
152 threshold.low.level, range.max_qual.level,
153 threshold.high.level, range.max_qual.level);
158 /* We can't read the range, so we don't know... */
159 printf("%-8.16s Low threshold:%d High threshold:%d\n\n",
161 threshold.low.level, threshold.high.level);
167 /************************* SETTING ROUTINES **************************/
169 /*------------------------------------------------------------------*/
171 * Set list of addresses specified on command line in the driver.
174 set_spy_info(int skfd, /* The socket */
175 char * ifname, /* Dev name */
176 char * args[], /* Command line args */
177 int count) /* Args count */
181 int nbr; /* Number of valid addresses */
182 struct sockaddr hw_address[IW_MAX_SPY];
184 /* Read command line */
185 i = 0; /* first arg to read */
186 nbr = 0; /* Number of args read so far */
188 /* "off" : disable functionality (set 0 addresses) */
189 if(!strcmp(args[0], "off"))
190 i = 1; /* skip the "off" */
193 /* "+" : add all addresses already in the driver */
194 if(!strcmp(args[0], "+"))
196 char buffer[(sizeof(struct iw_quality) +
197 sizeof(struct sockaddr)) * IW_MAX_SPY];
199 /* Check if we have valid mac address type */
200 if(iw_check_mac_addr_type(skfd, ifname) < 0)
202 fprintf(stderr, "%-8.16s Interface doesn't support MAC addresses\n", ifname);
206 wrq.u.data.pointer = (caddr_t) buffer;
207 wrq.u.data.length = IW_MAX_SPY;
208 wrq.u.data.flags = 0;
209 if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
211 fprintf(stderr, "Interface doesn't accept reading addresses...\n");
212 fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
216 /* Copy old addresses */
217 nbr = wrq.u.data.length;
218 memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
220 i = 1; /* skip the "+" */
223 /* Read other args on command line */
224 while((i < count) && (nbr < IW_MAX_SPY))
226 /* Get the address and check if the interface supports it */
227 if(iw_in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
232 /* Check the number of addresses */
235 fprintf(stderr, "No valid addresses found : exiting...\n");
240 /* Check if there is some remaining arguments */
243 fprintf(stderr, "Got only the first %d arguments, remaining discarded\n", i);
246 /* Time to do send addresses to the driver */
247 wrq.u.data.pointer = (caddr_t) hw_address;
248 wrq.u.data.length = nbr;
249 wrq.u.data.flags = 0;
250 if(iw_set_ext(skfd, ifname, SIOCSIWSPY, &wrq) < 0)
252 fprintf(stderr, "Interface doesn't accept addresses...\n");
253 fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno));
260 /*------------------------------------------------------------------*/
262 * Set spy thresholds in the driver from command line
265 set_spy_threshold(int skfd, /* The socket */
266 char * ifname, /* Dev name */
267 char * args[], /* Command line args */
268 int count) /* Args count */
271 struct iw_thrspy threshold;
276 memset(&threshold, '\0', sizeof(threshold));
278 /* "off" : disable functionality (set 0 addresses) */
279 if(!strcmp(args[0], "off"))
281 /* Just send null threshold, will disable it */
285 /* Try to get our threshold */
288 fprintf(stderr, "%-8.16s Need two threshold values\n", ifname);
291 if((sscanf(args[0], "%i", &low_thr) != 1) ||
292 (sscanf(args[1], "%i", &high_thr) != 1))
294 fprintf(stderr, "%-8.16s Invalid threshold values\n", ifname);
297 /* Basic sanity check */
298 if(high_thr < low_thr)
300 fprintf(stderr, "%-8.16s Inverted threshold range\n", ifname);
303 /* Copy thresholds */
304 threshold.low.level = low_thr;
305 threshold.low.updated = 0x2;
306 threshold.high.level = high_thr;
307 threshold.high.updated = 0x2;
310 /* Time to send thresholds to the driver */
311 wrq.u.data.pointer = (caddr_t) &threshold;
312 wrq.u.data.length = 1;
313 wrq.u.data.flags = 0;
314 if(iw_set_ext(skfd, ifname, SIOCSIWTHRSPY, &wrq) < 0)
316 fprintf(stderr, "Interface doesn't accept thresholds...\n");
317 fprintf(stderr, "SIOCSIWTHRSPY: %s\n", strerror(errno));
324 /******************************* MAIN ********************************/
326 /*------------------------------------------------------------------*/
334 int skfd; /* generic raw socket desc. */
337 /* Create a channel to the NET kernel. */
338 if((skfd = iw_sockets_open()) < 0)
344 /* No argument : show the list of all device + info */
346 iw_enum_devices(skfd, &print_spy_info, NULL, 0);
348 /* Special cases take one... */
350 if((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help")))
351 fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n");
354 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))
355 goterr = iw_print_version_info("iwspy");
357 /* The device name must be the first argument */
358 /* Name only : show spy list for that device only */
360 print_spy_info(skfd, argv[1], NULL, 0);
362 /* Special commands */
363 if(!strcmp(argv[2], "setthr"))
364 goterr = set_spy_threshold(skfd, argv[1], argv + 3, argc - 3);
366 if(!strcmp(argv[2], "getthr"))
367 goterr = get_spy_threshold(skfd, argv[1], argv + 3, argc - 3);
369 /* Otherwise, it's a list of address to set in the spy list */
370 goterr = set_spy_info(skfd, argv[1], argv + 2, argc - 2);
372 /* Close the socket. */
373 iw_sockets_close(skfd);