6 * Just print the ESSID or NWID...
8 * This file is released under the GPL license.
9 * Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
12 #include "iwlib.h" /* Header */
14 #define FORMAT_DEFAULT 0 /* Nice looking display for the user */
15 #define FORMAT_SCHEME 1 /* To be used as a Pcmcia Scheme */
18 * Note on Pcmcia Schemes :
19 * ----------------------
20 * The purpose of this tool is to use the ESSID discovery mechanism
21 * to select the appropriate Pcmcia Scheme. The card tell us which
22 * ESSID it has found, and we can then select the appropriate Pcmcia
23 * Scheme for this ESSID (Wireless config (encrypt keys) and IP config).
24 * The way to do it is as follows :
25 * cardctl scheme "essidany"
27 * $scheme = iwgetid --scheme
28 * cardctl scheme $scheme
29 * Of course, you need to add a scheme called "essidany" with the
35 * This can also be integrated int he Pcmcia scripts.
36 * Some drivers don't activate the card up to "ifconfig up".
37 * Therefore, they wont scan ESSID up to this point, so we can't
38 * read it reliably in Pcmcia scripts.
39 * I guess the proper way to write the network script is as follows :
40 * if($scheme == "iwgetid") {
41 * iwconfig $name essid any
42 * iwconfig $name nwid any
45 * $scheme = iwgetid $name --scheme
49 * This is pseudo code, but you get an idea...
50 * The "ifconfig up" activate the card.
51 * The "delay" is necessary to let time for the card scan the
52 * frequencies and associate with the AP.
53 * The "ifconfig down" is necessary to allow the driver to optimise
54 * the wireless parameters setting (minimise number of card resets).
56 * Another cute idea is to have a list of Pcmcia Schemes to try
57 * and to keep the first one that associate (AP address != 0). This
58 * would be necessary for closed networks and cards that can't
64 /************************ DISPLAY ESSID/NWID ************************/
66 /*------------------------------------------------------------------*/
68 * Display the ESSID if possible
76 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID */
77 char pessid[IW_ESSID_MAX_SIZE + 1]; /* Pcmcia format */
82 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
83 wrq.u.essid.pointer = (caddr_t) essid;
84 wrq.u.essid.length = 0;
85 wrq.u.essid.flags = 0;
86 if(ioctl(skfd, SIOCGIWESSID, &wrq) < 0)
92 /* Stip all white space and stuff */
94 for(i = 0; i < strlen(essid); i++)
96 pessid[j++] = essid[i];
98 if((j == 0) || (j > 32))
100 printf("%s\n", pessid);
104 printf("%-8.8s ESSID:\"%s\"\n", ifname, essid);
111 /*------------------------------------------------------------------*/
113 * Display the NWID if possible
123 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
124 if(ioctl(skfd, SIOCGIWNWID, &wrq) < 0)
130 /* Prefix with nwid to avoid name space collisions */
131 printf("nwid%X\n", wrq.u.nwid.value);
135 printf("%-8.8s NWID:%X\n", ifname, wrq.u.nwid.value);
142 /*------------------------------------------------------------------*/
144 * Try the various devices until one return something we can use
147 scan_devices(int skfd,
156 /* Get list of active devices */
157 ifc.ifc_len = sizeof(buff);
159 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
161 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
166 /* Print the first match */
167 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
169 /* Try to print an ESSID */
170 ret = print_essid(skfd, ifr->ifr_name, format);
172 return(0); /* Success */
174 /* Try to print a nwid */
175 ret = print_nwid(skfd, ifr->ifr_name, format);
177 return(0); /* Success */
182 /*************************** SUBROUTINES ***************************/
184 /*------------------------------------------------------------------*/
186 * Display an Ethernet address in readable format.
189 pr_ether(char * buffer,
192 sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X",
193 (ptr[0] & 0xFF), (ptr[1] & 0xFF), (ptr[2] & 0xFF),
194 (ptr[3] & 0xFF), (ptr[4] & 0xFF), (ptr[5] & 0xFF)
199 /*------------------------------------------------------------------*/
202 * Depending on the protocol present, open the right socket. The socket
203 * will allow us to talk to the driver.
208 int ipx_sock = -1; /* IPX socket */
209 int ax25_sock = -1; /* AX.25 socket */
210 int inet_sock = -1; /* INET socket */
211 int ddp_sock = -1; /* Appletalk DDP socket */
213 inet_sock=socket(AF_INET, SOCK_DGRAM, 0);
214 ipx_sock=socket(AF_IPX, SOCK_DGRAM, 0);
215 ax25_sock=socket(AF_AX25, SOCK_DGRAM, 0);
216 ddp_sock=socket(AF_APPLETALK, SOCK_DGRAM, 0);
218 * Now pick any (exisiting) useful socket family for generic queries
227 * If this is -1 we have no known network layers and its time to jump.
233 /**************************** AP ADDRESS ****************************/
235 /*------------------------------------------------------------------*/
237 * Display the NWID if possible
248 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
249 if(ioctl(skfd, SIOCGIWAP, &wrq) < 0)
253 printf("%-8.8s Access Point: %s\n", ifname,
254 pr_ether(buffer, wrq.u.ap_addr.sa_data));
259 /*------------------------------------------------------------------*/
261 * Try the various devices until one return something we can use
273 /* Get list of active devices */
274 ifc.ifc_len = sizeof(buff);
276 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
278 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
283 /* Print the first match */
284 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
286 /* Try to print an ESSID */
287 ret = print_ap(skfd, ifr->ifr_name, format);
289 return(0); /* Success */
294 /******************************* MAIN ********************************/
296 /*------------------------------------------------------------------*/
304 int skfd = -1; /* generic raw socket desc. */
305 int format = FORMAT_DEFAULT;
308 /* Create a channel to the NET kernel. */
309 if((skfd = sockets_open()) < 0)
318 /* Look on all devices */
319 ret = scan_devices(skfd, format);
324 /* Only ask for first AP address */
325 if((!strcmp(argv[1], "--ap")) || (!strcmp(argv[1], "-a")))
327 /* Look on all devices */
328 ret = scan_ap(skfd, format);
333 /* Only the format, no interface name */
334 if((!strncmp(argv[1], "--scheme", 4)) || (!strcmp(argv[1], "-s")))
336 /* Look on all devices */
337 format = FORMAT_SCHEME;
338 ret = scan_devices(skfd, format);
345 (!strncmp(argv[1], "-h", 9)) || (!strcmp(argv[1], "--help")))
347 fprintf(stderr, "Usage: iwgetid [interface]\n");
348 fprintf(stderr, " [interface] --scheme\n");
352 /* If at least a device name */
355 /* Check extra format argument */
358 /* Only ask for first AP address */
359 if((!strcmp(argv[2], "--ap")) || (!strcmp(argv[2], "-a")))
361 ret = print_ap(skfd, argv[1], format);
366 /* Want scheme format */
367 if((!strncmp(argv[2], "--scheme", 4)) || (!strcmp(argv[2], "-s")))
368 format = FORMAT_SCHEME;
371 /* Try to print an ESSID */
372 ret = print_essid(skfd, argv[1], format);
376 /* Try to print a nwid */
377 ret = print_nwid(skfd, argv[1], format);
381 /* Close the socket. */