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".
9 * This file is released under the GPL license.
12 #include "iwlib.h" /* Header */
14 /************************* DISPLAY ROUTINES **************************/
16 /*------------------------------------------------------------------*/
18 * Display the spy list of addresses and the associated stats
21 print_spy_info(int skfd,
25 char buffer[(sizeof(struct iw_quality) +
26 sizeof(struct sockaddr)) * IW_MAX_SPY];
28 struct sockaddr hwa[IW_MAX_SPY];
29 struct iw_quality qual[IW_MAX_SPY];
36 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
37 wrq.u.data.pointer = (caddr_t) buffer;
38 wrq.u.data.length = 0;
40 if(ioctl(skfd, SIOCGIWSPY, &wrq) < 0)
42 fprintf(stderr, "%-8.8s Interface doesn't support wireless statistic collection\n\n", ifname);
46 /* Number of addresses */
47 n = wrq.u.data.length;
51 /* Check if we have valid address types */
52 if(iw_check_addr_type(skfd, ifname) < 0)
54 fprintf(stderr, "%-8.8s Interface doesn't support MAC & IP addresses\n\n", ifname);
58 /* Get range info if we can */
59 if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
64 printf("%-8.8s No statistics to collect\n", ifname);
66 printf("%-8.8s Statistics collected:\n", ifname);
70 memcpy(hwa, buffer, n * sizeof(struct sockaddr));
71 memcpy(qual, buffer + n*sizeof(struct sockaddr), n*sizeof(struct iw_quality));
73 for(i = 0; i < n; i++)
75 /* Print stats for each address */
76 printf(" %s : ", iw_pr_ether(temp, hwa[i].sa_data));
77 iw_print_stats(temp, &qual[i], &range, has_range);
81 if((n > 0) && (has_range))
83 iw_print_stats(temp, &range.avg_qual, &range, has_range);
84 printf(" typical/average : %s\n", temp);
86 #endif /* WIRELESS_EXT > 11 */
91 /*------------------------------------------------------------------*/
93 * Get info on all devices and print it on the screen
96 print_spy_devices(int skfd)
103 /* Get list of active devices */
104 ifc.ifc_len = sizeof(buff);
106 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
108 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
114 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
115 print_spy_info(skfd, ifr->ifr_name);
118 /************************* SETTING ROUTINES **************************/
120 /*------------------------------------------------------------------*/
122 * Set list of addresses specified on command line in the driver.
125 set_spy_info(int skfd, /* The socket */
126 char * args[], /* Command line args */
127 int count, /* Args count */
128 char * ifname) /* Dev name */
132 int nbr; /* Number of valid addresses */
133 struct sockaddr hw_address[IW_MAX_SPY];
135 /* Read command line */
136 i = 0; /* first arg to read */
137 nbr = 0; /* Number of args readen so far */
139 /* Check if we have valid address types */
140 if(iw_check_addr_type(skfd, ifname) < 0)
142 fprintf(stderr, "%-8.8s Interface doesn't support MAC & IP addresses\n", ifname);
146 /* "off" : disable functionality (set 0 addresses) */
147 if(!strcmp(args[0], "off"))
148 i = count; /* hack */
150 /* "+" : add all addresses already in the driver */
151 if(!strcmp(args[0], "+"))
153 char buffer[(sizeof(struct iw_quality) +
154 sizeof(struct sockaddr)) * IW_MAX_SPY];
156 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
157 wrq.u.data.pointer = (caddr_t) buffer;
158 wrq.u.data.length = 0;
159 wrq.u.data.flags = 0;
160 if(ioctl(skfd, SIOCGIWSPY, &wrq) < 0)
162 fprintf(stderr, "Interface doesn't accept reading addresses...\n");
163 fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
167 /* Copy old addresses */
168 nbr = wrq.u.data.length;
169 memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
171 i = 1; /* skip the "+" */
174 /* Read other args on command line */
175 while((i < count) && (nbr < IW_MAX_SPY))
177 if(iw_in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
182 /* Check the number of addresses */
183 if((nbr == 0) && strcmp(args[0], "off"))
185 fprintf(stderr, "No valid addresses found : exiting...\n");
189 /* Check if there is some remaining arguments */
192 fprintf(stderr, "Got only the first %d addresses, remaining discarded\n", IW_MAX_SPY);
195 /* Time to do send addresses to the driver */
196 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
197 wrq.u.data.pointer = (caddr_t) hw_address;
198 wrq.u.data.length = nbr;
199 wrq.u.data.flags = 0;
200 if(ioctl(skfd, SIOCSIWSPY, &wrq) < 0)
202 fprintf(stderr, "Interface doesn't accept addresses...\n");
203 fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno));
210 /******************************* MAIN ********************************/
212 /*------------------------------------------------------------------*/
220 int skfd = -1; /* generic raw socket desc. */
223 /* Create a channel to the NET kernel. */
224 if((skfd = iw_sockets_open()) < 0)
230 /* No argument : show the list of all device + info */
233 print_spy_devices(skfd);
238 /* Special cases take one... */
240 if((!strncmp(argv[1], "-h", 9)) ||
241 (!strcmp(argv[1], "--help")))
243 fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n");
248 /* The device name must be the first argument */
249 /* Name only : show spy list for that device only */
252 print_spy_info(skfd, argv[1]);
257 /* Otherwise, it's a list of address to set in the spy list */
258 goterr = set_spy_info(skfd, argv + 2, argc - 2, argv[1]);
260 /* Close the socket. */