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 /* Backward compatibility */
16 #ifndef IW_MAX_GET_SPY
17 #define IW_MAX_GET_SPY 64
18 #endif /* IW_MAX_GET_SPY */
20 /************************* DISPLAY ROUTINES **************************/
22 /*------------------------------------------------------------------*/
24 * Display the spy list of addresses and the associated stats
27 print_spy_info(int skfd,
33 char buffer[(sizeof(struct iw_quality) +
34 sizeof(struct sockaddr)) * IW_MAX_GET_SPY];
36 struct sockaddr * hwa;
37 struct iw_quality * qual;
43 /* Avoid "Unused parameter" warning */
44 args = args; count = count;
47 wrq.u.data.pointer = (caddr_t) buffer;
48 wrq.u.data.length = IW_MAX_GET_SPY;
50 if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
52 fprintf(stderr, "%-8.8s Interface doesn't support wireless statistic collection\n\n", ifname);
56 /* Number of addresses */
57 n = wrq.u.data.length;
59 /* Check if we have valid mac address type */
60 if(iw_check_mac_addr_type(skfd, ifname) < 0)
62 fprintf(stderr, "%-8.8s Interface doesn't support MAC addresses\n\n", ifname);
66 /* Get range info if we can */
67 if(iw_get_range_info(skfd, ifname, &(range)) >= 0)
72 printf("%-8.8s No statistics to collect\n", ifname);
74 printf("%-8.8s Statistics collected:\n", ifname);
77 hwa = (struct sockaddr *) buffer;
78 qual = (struct iw_quality *) (buffer + (sizeof(struct sockaddr) * n));
80 for(i = 0; i < n; i++)
82 /* Print stats for each address */
83 printf(" %s : ", iw_pr_ether(temp, hwa[i].sa_data));
84 iw_print_stats(temp, &qual[i], &range, has_range);
88 if((n > 0) && (has_range))
90 iw_print_stats(temp, &range.avg_qual, &range, has_range);
91 printf(" typical/average : %s\n", temp);
93 #endif /* WIRELESS_EXT > 11 */
99 /************************* SETTING ROUTINES **************************/
101 /*------------------------------------------------------------------*/
103 * Set list of addresses specified on command line in the driver.
106 set_spy_info(int skfd, /* The socket */
107 char * args[], /* Command line args */
108 int count, /* Args count */
109 char * ifname) /* Dev name */
113 int nbr; /* Number of valid addresses */
114 struct sockaddr hw_address[IW_MAX_SPY];
116 /* Read command line */
117 i = 0; /* first arg to read */
118 nbr = 0; /* Number of args read so far */
120 /* "off" : disable functionality (set 0 addresses) */
121 if(!strcmp(args[0], "off"))
122 i = 1; /* skip the "off" */
125 /* "+" : add all addresses already in the driver */
126 if(!strcmp(args[0], "+"))
128 char buffer[(sizeof(struct iw_quality) +
129 sizeof(struct sockaddr)) * IW_MAX_SPY];
131 /* Check if we have valid mac address type */
132 if(iw_check_mac_addr_type(skfd, ifname) < 0)
134 fprintf(stderr, "%-8.8s Interface doesn't support MAC addresses\n", ifname);
138 wrq.u.data.pointer = (caddr_t) buffer;
139 wrq.u.data.length = 0;
140 wrq.u.data.flags = 0;
141 if(iw_get_ext(skfd, ifname, SIOCGIWSPY, &wrq) < 0)
143 fprintf(stderr, "Interface doesn't accept reading addresses...\n");
144 fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno));
148 /* Copy old addresses */
149 nbr = wrq.u.data.length;
150 memcpy(hw_address, buffer, nbr * sizeof(struct sockaddr));
152 i = 1; /* skip the "+" */
155 /* Read other args on command line */
156 while((i < count) && (nbr < IW_MAX_SPY))
158 /* Get the address and check if the interface supports it */
159 if(iw_in_addr(skfd, ifname, args[i++], &(hw_address[nbr])) < 0)
164 /* Check the number of addresses */
167 fprintf(stderr, "No valid addresses found : exiting...\n");
172 /* Check if there is some remaining arguments */
175 fprintf(stderr, "Got only the first %d arguments, remaining discarded\n", i);
178 /* Time to do send addresses to the driver */
179 wrq.u.data.pointer = (caddr_t) hw_address;
180 wrq.u.data.length = nbr;
181 wrq.u.data.flags = 0;
182 if(iw_set_ext(skfd, ifname, SIOCSIWSPY, &wrq) < 0)
184 fprintf(stderr, "Interface doesn't accept addresses...\n");
185 fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno));
192 /******************************* MAIN ********************************/
194 /*------------------------------------------------------------------*/
202 int skfd; /* generic raw socket desc. */
205 /* Create a channel to the NET kernel. */
206 if((skfd = iw_sockets_open()) < 0)
212 /* No argument : show the list of all device + info */
214 iw_enum_devices(skfd, &print_spy_info, NULL, 0);
216 /* Special cases take one... */
218 if((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help")))
219 fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n");
222 if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))
223 goterr = iw_print_version_info("iwspy");
225 /* The device name must be the first argument */
226 /* Name only : show spy list for that device only */
228 print_spy_info(skfd, argv[1], NULL, 0);
230 /* Otherwise, it's a list of address to set in the spy list */
231 goterr = set_spy_info(skfd, argv + 2, argc - 2, argv[1]);
233 /* Close the socket. */