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 "iwcommon.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];
27 struct sockaddr hwa[IW_MAX_SPY];
28 struct iw_quality qual[IW_MAX_SPY];
35 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
36 wrq.u.data.pointer = (caddr_t) buffer;
37 wrq.u.data.length = 0;
39 if(ioctl(skfd, SIOCGIWSPY, &wrq) < 0)
41 fprintf(stderr, "%-8.8s Interface doesn't support wireless statistic collection\n\n", ifname);
45 /* Number of addresses */
46 n = wrq.u.data.length;
50 /* Check if we have valid address types */
51 if(check_addr_type(skfd, ifname) < 0)
53 fprintf(stderr, "%-8.8s Interface doesn't support MAC & IP addresses\n\n", ifname);
57 /* Get range info if we can */
58 if(get_range_info(skfd, ifname, &(range)) >= 0)
63 printf("%-8.8s No statistics to collect\n", ifname);
65 printf("%-8.8s Statistics collected:\n", ifname);
69 memcpy(hwa, buffer, n * sizeof(struct sockaddr));
70 memcpy(qual, buffer + n*sizeof(struct sockaddr), n*sizeof(struct iw_quality));
72 for(i = 0; i < n; i++)
74 /* Print stats for each address */
75 printf(" %s : ", pr_ether(hwa[i].sa_data));
76 print_stats(stdout, &qual[i], &range, has_range);
81 /*------------------------------------------------------------------*/
83 * Get info on all devices and print it on the screen
86 print_spy_devices(int skfd)
93 /* Get list of active devices */
94 ifc.ifc_len = sizeof(buff);
96 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
98 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
104 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
105 print_spy_info(skfd, ifr->ifr_name);
108 /************************* SETTING ROUTINES **************************/
110 /*------------------------------------------------------------------*/
112 * Set list of addresses specified on command line in the driver.
115 set_spy_info(int skfd, /* The socket */
116 char * args[], /* Command line args */
117 int count, /* Args count */
118 char * ifname) /* Dev name */
122 int nbr; /* Number of valid addresses */
123 struct sockaddr hw_address[IW_MAX_SPY];
125 /* Read command line */
126 i = 0; /* first arg to read */
127 nbr = 0; /* Number of args readen so far */
129 /* Check if we have valid address types */
130 if(check_addr_type(skfd, ifname) < 0)
132 fprintf(stderr, "%-8.8s Interface doesn't support MAC & IP addresses\n", ifname);
136 /* "off" : disable functionality (set 0 addresses) */
137 if(!strcmp(args[0], "off"))
138 i = count; /* hack */
140 /* "+" : add all addresses already in the driver */
141 if(!strcmp(args[0], "+"))
143 char buffer[(sizeof(struct iw_quality) +
144 sizeof(struct sockaddr)) * IW_MAX_SPY];
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)
152 fprintf(stderr, "Interface doesn't accept reading addresses...\n");
153 fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
157 /* Copy old addresses */
158 nbr = wrq.u.data.length;
159 memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
161 i = 1; /* skip the "+" */
164 /* Read other args on command line */
165 while((i < count) && (nbr < IW_MAX_SPY))
167 if(in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
172 /* Check the number of addresses */
173 if((nbr == 0) && strcmp(args[0], "off"))
175 fprintf(stderr, "No valid addresses found : exiting...\n");
179 /* Check if there is some remaining arguments */
182 fprintf(stderr, "Got only the first %d addresses, remaining discarded\n", IW_MAX_SPY);
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)
192 fprintf(stderr, "Interface doesn't accept addresses...\n");
193 fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno));
200 /******************************* MAIN ********************************/
202 /*------------------------------------------------------------------*/
210 int skfd = -1; /* generic raw socket desc. */
213 /* Create a channel to the NET kernel. */
214 if((skfd = sockets_open()) < 0)
220 /* No argument : show the list of all device + info */
223 print_spy_devices(skfd);
228 /* Special cases take one... */
230 if((!strncmp(argv[1], "-h", 9)) ||
231 (!strcmp(argv[1], "--help")))
233 fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n");
234 fprintf(stderr, " interface [freq]\n");
235 fprintf(stderr, " interface [ap]\n");
240 /* The device name must be the first argument */
241 /* Name only : show spy list for that device only */
244 print_spy_info(skfd, argv[1]);
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]);
252 /* Close the socket. */