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-2002 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,
26 char buffer[(sizeof(struct iw_quality) +
27 sizeof(struct sockaddr)) * IW_MAX_SPY];
29 struct sockaddr hwa[IW_MAX_SPY];
30 struct iw_quality qual[IW_MAX_SPY];
37 wrq.u.data.pointer = (caddr_t) buffer;
38 wrq.u.data.length = IW_MAX_SPY;
40 if(iw_get_ext(skfd, ifname, 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;
49 /* Check if we have valid mac address type */
50 if(iw_check_mac_addr_type(skfd, ifname) < 0)
52 fprintf(stderr, "%-8.8s Interface doesn't support MAC addresses\n\n", ifname);
56 /* Get range info if we can */
57 if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
62 printf("%-8.8s No statistics to collect\n", ifname);
64 printf("%-8.8s Statistics collected:\n", ifname);
68 memcpy(hwa, buffer, n * sizeof(struct sockaddr));
69 memcpy(qual, buffer + n*sizeof(struct sockaddr), n*sizeof(struct iw_quality));
71 for(i = 0; i < n; i++)
73 /* Print stats for each address */
74 printf(" %s : ", iw_pr_ether(temp, hwa[i].sa_data));
75 iw_print_stats(temp, &qual[i], &range, has_range);
79 if((n > 0) && (has_range))
81 iw_print_stats(temp, &range.avg_qual, &range, has_range);
82 printf(" typical/average : %s\n", temp);
84 #endif /* WIRELESS_EXT > 11 */
89 /*------------------------------------------------------------------*/
91 * Get info on all devices and print it on the screen
94 print_spy_devices(int skfd)
101 /* Get list of active devices */
102 ifc.ifc_len = sizeof(buff);
104 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
106 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
112 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
113 print_spy_info(skfd, ifr->ifr_name);
116 /************************* SETTING ROUTINES **************************/
118 /*------------------------------------------------------------------*/
120 * Set list of addresses specified on command line in the driver.
123 set_spy_info(int skfd, /* The socket */
124 char * args[], /* Command line args */
125 int count, /* Args count */
126 char * ifname) /* Dev name */
130 int nbr; /* Number of valid addresses */
131 struct sockaddr hw_address[IW_MAX_SPY];
133 /* Read command line */
134 i = 0; /* first arg to read */
135 nbr = 0; /* Number of args readen so far */
137 /* "off" : disable functionality (set 0 addresses) */
138 if(!strcmp(args[0], "off"))
139 i = count; /* hack */
141 /* "+" : add all addresses already in the driver */
142 if(!strcmp(args[0], "+"))
144 char buffer[(sizeof(struct iw_quality) +
145 sizeof(struct sockaddr)) * IW_MAX_SPY];
147 /* Check if we have valid mac address type */
148 if(iw_check_mac_addr_type(skfd, ifname) < 0)
150 fprintf(stderr, "%-8.8s Interface doesn't support MAC addresses\n", ifname);
154 wrq.u.data.pointer = (caddr_t) buffer;
155 wrq.u.data.length = 0;
156 wrq.u.data.flags = 0;
157 if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
159 fprintf(stderr, "Interface doesn't accept reading addresses...\n");
160 fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
164 /* Copy old addresses */
165 nbr = wrq.u.data.length;
166 memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
168 i = 1; /* skip the "+" */
171 /* Read other args on command line */
172 while((i < count) && (nbr < IW_MAX_SPY))
174 /* Get the address and check if the interface supports it */
175 if(iw_in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
180 /* Check the number of addresses */
181 if((nbr == 0) && strcmp(args[0], "off"))
183 fprintf(stderr, "No valid addresses found : exiting...\n");
187 /* Check if there is some remaining arguments */
190 fprintf(stderr, "Got only the first %d addresses, remaining discarded\n", IW_MAX_SPY);
193 /* Time to do send addresses to the driver */
194 wrq.u.data.pointer = (caddr_t) hw_address;
195 wrq.u.data.length = nbr;
196 wrq.u.data.flags = 0;
197 if(iw_set_ext(skfd, ifname, SIOCSIWSPY, &wrq) < 0)
199 fprintf(stderr, "Interface doesn't accept addresses...\n");
200 fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno));
207 /******************************* MAIN ********************************/
209 /*------------------------------------------------------------------*/
217 int skfd = -1; /* generic raw socket desc. */
220 /* Create a channel to the NET kernel. */
221 if((skfd = iw_sockets_open()) < 0)
227 /* No argument : show the list of all device + info */
230 print_spy_devices(skfd);
235 /* Special cases take one... */
237 if((!strncmp(argv[1], "-h", 9)) ||
238 (!strcmp(argv[1], "--help")))
240 fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n");
245 /* The device name must be the first argument */
246 /* Name only : show spy list for that device only */
249 print_spy_info(skfd, argv[1]);
254 /* Otherwise, it's a list of address to set in the spy list */
255 goterr = set_spy_info(skfd, argv + 2, argc - 2, argv[1]);
257 /* Close the socket. */