6 * Just print the ESSID or NWID...
8 * This file is released under the GPL license.
11 #include "iwlib.h" /* Header */
13 #define FORMAT_DEFAULT 0 /* Nice looking display for the user */
14 #define FORMAT_SCHEME 1 /* To be used as a Pcmcia Scheme */
17 * Note on Pcmcia Schemes :
18 * ----------------------
19 * The purpose of this tool is to use the ESSID discovery mechanism
20 * to select the appropriate Pcmcia Scheme. The card tell us which
21 * ESSID it has found, and we can then select the appropriate Pcmcia
22 * Scheme for this ESSID (Wireless config (encrypt keys) and IP config).
23 * The way to do it is as follows :
24 * cardctl scheme "essidany"
26 * $scheme = iwgetid --scheme
27 * cardctl scheme $scheme
28 * Of course, you need to add a scheme called "essidany" with the
34 * This can also be integrated int he Pcmcia scripts.
35 * Some drivers don't activate the card up to "ifconfig up".
36 * Therefore, they wont scan ESSID up to this point, so we can't
37 * read it reliably in Pcmcia scripts.
38 * I guess the proper way to write the network script is as follows :
39 * if($scheme == "iwgetid") {
40 * iwconfig $name essid any
41 * iwconfig $name nwid any
44 * $scheme = iwgetid $name --scheme
48 * This is pseudo code, but you get an idea...
49 * The "ifconfig up" activate the card.
50 * The "delay" is necessary to let time for the card scan the
51 * frequencies and associate with the AP.
52 * The "ifconfig down" is necessary to allow the driver to optimise
53 * the wireless parameters setting (minimise number of card resets).
55 * Another cute idea is to have a list of Pcmcia Schemes to try
56 * and to keep the first one that associate (AP address != 0). This
57 * would be necessary for closed networks and cards that can't
63 /************************ DISPLAY ESSID/NWID ************************/
65 /*------------------------------------------------------------------*/
67 * Display the ESSID if possible
75 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID */
76 char pessid[IW_ESSID_MAX_SIZE + 1]; /* Pcmcia format */
81 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
82 wrq.u.essid.pointer = (caddr_t) essid;
83 wrq.u.essid.length = 0;
84 wrq.u.essid.flags = 0;
85 if(ioctl(skfd, SIOCGIWESSID, &wrq) < 0)
91 /* Stip all white space and stuff */
93 for(i = 0; i < strlen(essid); i++)
95 pessid[j++] = essid[i];
97 if((j == 0) || (j > 32))
99 printf("%s\n", pessid);
103 printf("%-8.8s ESSID:\"%s\"\n", ifname, essid);
110 /*------------------------------------------------------------------*/
112 * Display the NWID if possible
122 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
123 if(ioctl(skfd, SIOCGIWNWID, &wrq) < 0)
129 /* Prefix with nwid to avoid name space collisions */
130 printf("nwid%X\n", wrq.u.nwid.value);
134 printf("%-8.8s NWID:%X\n", ifname, wrq.u.nwid.value);
141 /*------------------------------------------------------------------*/
143 * Try the various devices until one return something we can use
146 scan_devices(int skfd,
155 /* Get list of active devices */
156 ifc.ifc_len = sizeof(buff);
158 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
160 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
165 /* Print the first match */
166 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
168 /* Try to print an ESSID */
169 ret = print_essid(skfd, ifr->ifr_name, format);
171 return(0); /* Success */
173 /* Try to print a nwid */
174 ret = print_nwid(skfd, ifr->ifr_name, format);
176 return(0); /* Success */
181 /*************************** SUBROUTINES ***************************/
183 /*------------------------------------------------------------------*/
185 * Display an Ethernet address in readable format.
188 pr_ether(char * buffer,
191 sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X",
192 (ptr[0] & 0xFF), (ptr[1] & 0xFF), (ptr[2] & 0xFF),
193 (ptr[3] & 0xFF), (ptr[4] & 0xFF), (ptr[5] & 0xFF)
198 /*------------------------------------------------------------------*/
201 * Depending on the protocol present, open the right socket. The socket
202 * will allow us to talk to the driver.
207 int ipx_sock = -1; /* IPX socket */
208 int ax25_sock = -1; /* AX.25 socket */
209 int inet_sock = -1; /* INET socket */
210 int ddp_sock = -1; /* Appletalk DDP socket */
212 inet_sock=socket(AF_INET, SOCK_DGRAM, 0);
213 ipx_sock=socket(AF_IPX, SOCK_DGRAM, 0);
214 ax25_sock=socket(AF_AX25, SOCK_DGRAM, 0);
215 ddp_sock=socket(AF_APPLETALK, SOCK_DGRAM, 0);
217 * Now pick any (exisiting) useful socket family for generic queries
226 * If this is -1 we have no known network layers and its time to jump.
232 /**************************** AP ADDRESS ****************************/
234 /*------------------------------------------------------------------*/
236 * Display the NWID if possible
247 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
248 if(ioctl(skfd, SIOCGIWAP, &wrq) < 0)
252 printf("%-8.8s Access Point: %s\n", ifname,
253 pr_ether(buffer, wrq.u.ap_addr.sa_data));
258 /*------------------------------------------------------------------*/
260 * Try the various devices until one return something we can use
272 /* Get list of active devices */
273 ifc.ifc_len = sizeof(buff);
275 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
277 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
282 /* Print the first match */
283 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
285 /* Try to print an ESSID */
286 ret = print_ap(skfd, ifr->ifr_name, format);
288 return(0); /* Success */
293 /******************************* MAIN ********************************/
295 /*------------------------------------------------------------------*/
303 int skfd = -1; /* generic raw socket desc. */
304 int format = FORMAT_DEFAULT;
307 /* Create a channel to the NET kernel. */
308 if((skfd = sockets_open()) < 0)
317 /* Look on all devices */
318 ret = scan_devices(skfd, format);
323 /* Only ask for first AP address */
324 if((!strcmp(argv[1], "--ap")) || (!strcmp(argv[1], "-a")))
326 /* Look on all devices */
327 ret = scan_ap(skfd, format);
332 /* Only the format, no interface name */
333 if((!strncmp(argv[1], "--scheme", 4)) || (!strcmp(argv[1], "-s")))
335 /* Look on all devices */
336 format = FORMAT_SCHEME;
337 ret = scan_devices(skfd, format);
344 (!strncmp(argv[1], "-h", 9)) || (!strcmp(argv[1], "--help")))
346 fprintf(stderr, "Usage: iwgetid [interface]\n");
347 fprintf(stderr, " [interface] --scheme\n");
351 /* If at least a device name */
354 /* Check extra format argument */
357 /* Only ask for first AP address */
358 if((!strcmp(argv[2], "--ap")) || (!strcmp(argv[2], "-a")))
360 ret = print_ap(skfd, argv[1], format);
365 /* Want scheme format */
366 if((!strncmp(argv[2], "--scheme", 4)) || (!strcmp(argv[2], "-s")))
367 format = FORMAT_SCHEME;
370 /* Try to print an ESSID */
371 ret = print_essid(skfd, argv[1], format);
375 /* Try to print a nwid */
376 ret = print_nwid(skfd, argv[1], format);
380 /* Close the socket. */