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 */
16 #define FORMAT_DEFAULT 0 /* Nice looking display for the user */
17 #define FORMAT_SCHEME 1 /* To be used as a Pcmcia Scheme */
18 #define WTYPE_ESSID 0 /* Display ESSID or NWID */
19 #define WTYPE_AP 1 /* Display AP/Cell Address */
20 #define WTYPE_FREQ 2 /* Display frequency/channel */
21 #define WTYPE_MODE 3 /* Display mode */
22 #define WTYPE_PROTO 4 /* Display protocol name */
25 * Note on Pcmcia Schemes :
26 * ----------------------
27 * The purpose of this tool is to use the ESSID discovery mechanism
28 * to select the appropriate Pcmcia Scheme. The card tell us which
29 * ESSID it has found, and we can then select the appropriate Pcmcia
30 * Scheme for this ESSID (Wireless config (encrypt keys) and IP config).
31 * The way to do it is as follows :
32 * cardctl scheme "essidany"
34 * $scheme = iwgetid --scheme
35 * cardctl scheme $scheme
36 * Of course, you need to add a scheme called "essidany" with the
42 * This can also be integrated int he Pcmcia scripts.
43 * Some drivers don't activate the card up to "ifconfig up".
44 * Therefore, they wont scan ESSID up to this point, so we can't
45 * read it reliably in Pcmcia scripts.
46 * I guess the proper way to write the network script is as follows :
47 * if($scheme == "iwgetid") {
48 * iwconfig $name essid any
49 * iwconfig $name nwid any
52 * $scheme = iwgetid $name --scheme
56 * This is pseudo code, but you get an idea...
57 * The "ifconfig up" activate the card.
58 * The "delay" is necessary to let time for the card scan the
59 * frequencies and associate with the AP.
60 * The "ifconfig down" is necessary to allow the driver to optimise
61 * the wireless parameters setting (minimise number of card resets).
63 * Another cute idea is to have a list of Pcmcia Schemes to try
64 * and to keep the first one that associate (AP address != 0). This
65 * would be necessary for closed networks and cards that can't
71 /*************************** SUBROUTINES ***************************/
73 * Just for the heck of it, let's try to not link with iwlib.
74 * This will keep the binary small and tiny...
76 * Note : maybe it's time to admit that we have lost the battle
77 * and we start using iwlib ? Maybe we should default to dynamic
81 /*------------------------------------------------------------------*/
84 * Depending on the protocol present, open the right socket. The socket
85 * will allow us to talk to the driver.
90 int ipx_sock = -1; /* IPX socket */
91 int ax25_sock = -1; /* AX.25 socket */
92 int inet_sock = -1; /* INET socket */
93 int ddp_sock = -1; /* Appletalk DDP socket */
96 * Now pick any (exisiting) useful socket family for generic queries
97 * Note : don't open all the socket, only returns when one matches,
98 * all protocols might not be valid.
99 * Workaround by Jim Kaba <jkaba@sarnoff.com>
100 * Note : in 99% of the case, we will just open the inet_sock.
101 * The remaining 1% case are not fully correct...
103 inet_sock=socket(AF_INET, SOCK_DGRAM, 0);
106 ipx_sock=socket(AF_IPX, SOCK_DGRAM, 0);
109 ax25_sock=socket(AF_AX25, SOCK_DGRAM, 0);
112 ddp_sock=socket(AF_APPLETALK, SOCK_DGRAM, 0);
114 * If this is -1 we have no known network layers and its time to jump.
119 /*------------------------------------------------------------------*/
121 * Display an Ethernet address in readable format.
124 iw_ether_ntop(const struct ether_addr* eth, char* buf)
126 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
127 eth->ether_addr_octet[0], eth->ether_addr_octet[1],
128 eth->ether_addr_octet[2], eth->ether_addr_octet[3],
129 eth->ether_addr_octet[4], eth->ether_addr_octet[5]);
132 /*------------------------------------------------------------------*/
134 * Convert our internal representation of frequencies to a floating point.
137 iw_freq2float(iwfreq * in)
140 /* Version without libm : slower */
142 double res = (double) in->m;
143 for(i = 0; i < in->e; i++)
146 #else /* WE_NOLIBM */
147 /* Version with libm : faster */
148 return ((double) in->m) * pow(10,in->e);
149 #endif /* WE_NOLIBM */
152 /*------------------------------------------------------------------*/
154 * Output a frequency with proper scaling
157 iw_print_freq(char * buffer,
161 sprintf(buffer, "Channel:%g", freq);
165 sprintf(buffer, "Frequency:%gGHz", freq / GIGA);
169 sprintf(buffer, "Frequency:%gMHz", freq / MEGA);
171 sprintf(buffer, "Frequency:%gkHz", freq / KILO);
176 /*------------------------------------------------------------------*/
177 const char * const iw_operation_mode[] = { "Auto",
185 /************************ DISPLAY ESSID/NWID ************************/
187 /*------------------------------------------------------------------*/
189 * Display the ESSID if possible
192 print_essid(int skfd,
197 char essid[IW_ESSID_MAX_SIZE + 1]; /* ESSID */
198 char pessid[IW_ESSID_MAX_SIZE + 1]; /* Pcmcia format */
203 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
204 wrq.u.essid.pointer = (caddr_t) essid;
205 wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1;
206 wrq.u.essid.flags = 0;
207 if(ioctl(skfd, SIOCGIWESSID, &wrq) < 0)
213 /* Strip all white space and stuff */
215 for(i = 0; i < strlen(essid); i++)
216 if(isalnum(essid[i]))
217 pessid[j++] = essid[i];
219 if((j == 0) || (j > 32))
221 printf("%s\n", pessid);
224 printf("%-8.8s ESSID:\"%s\"\n", ifname, essid);
231 /*------------------------------------------------------------------*/
233 * Display the NWID if possible
243 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
244 if(ioctl(skfd, SIOCGIWNWID, &wrq) < 0)
250 /* Prefix with nwid to avoid name space collisions */
251 printf("nwid%X\n", wrq.u.nwid.value);
254 printf("%-8.8s NWID:%X\n", ifname, wrq.u.nwid.value);
261 /**************************** AP ADDRESS ****************************/
263 /*------------------------------------------------------------------*/
265 * Display the AP Address if possible
276 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
277 if(ioctl(skfd, SIOCGIWAP, &wrq) < 0)
281 iw_ether_ntop((const struct ether_addr *) wrq.u.ap_addr.sa_data, buffer);
285 /* I think ':' are not problematic, because Pcmcia scripts
286 * seem to handle them properly... */
287 printf("%s\n", buffer);
290 printf("%-8.8s Access Point/Cell: %s\n", ifname, buffer);
297 /****************************** OTHER ******************************/
299 /*------------------------------------------------------------------*/
301 * Display the frequency (or channel) if possible
312 /* Get frequency / channel */
313 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
314 if(ioctl(skfd, SIOCGIWFREQ, &wrq) < 0)
318 freq = iw_freq2float(&(wrq.u.freq));
322 printf("%g\n", freq);
325 iw_print_freq(buffer, freq);
326 printf("%-8.8s %s\n", ifname, buffer);
333 /*------------------------------------------------------------------*/
335 * Display the mode if possible
344 /* Get frequency / channel */
345 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
346 if(ioctl(skfd, SIOCGIWMODE, &wrq) < 0)
348 if(wrq.u.mode >= IW_NUM_OPER_MODE)
355 printf("%d\n", wrq.u.mode);
358 printf("%-8.8s Mode:%s\n", ifname, iw_operation_mode[wrq.u.mode]);
365 /*------------------------------------------------------------------*/
367 * Display the ESSID if possible
370 print_protocol(int skfd,
375 char proto[IFNAMSIZ + 1]; /* Protocol */
376 char pproto[IFNAMSIZ + 1]; /* Pcmcia format */
380 /* Get Protocol name */
381 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
382 if(ioctl(skfd, SIOCGIWNAME, &wrq) < 0)
384 strncpy(proto, wrq.u.name, IFNAMSIZ);
385 proto[IFNAMSIZ] = '\0';
390 /* Strip all white space and stuff */
392 for(i = 0; i < strlen(proto); i++)
393 if(isalnum(proto[i]))
394 pproto[j++] = proto[i];
396 if((j == 0) || (j > 32))
398 printf("%s\n", pproto);
401 printf("%-8.8s Protocol Name:\"%s\"\n", ifname, proto);
408 /******************************* MAIN ********************************/
410 /*------------------------------------------------------------------*/
412 * Check options and call the proper handler
415 print_one_device(int skfd,
426 /* Try to print an AP */
427 ret = print_ap(skfd, ifname, format);
431 /* Try to print frequency */
432 ret = print_freq(skfd, ifname, format);
436 /* Try to print the mode */
437 ret = print_mode(skfd, ifname, format);
441 /* Try to print the protocol */
442 ret = print_protocol(skfd, ifname, format);
446 /* Try to print an ESSID */
447 ret = print_essid(skfd, ifname, format);
450 /* Try to print a nwid */
451 ret = print_nwid(skfd, ifname, format);
458 /*------------------------------------------------------------------*/
460 * Try the various devices until one return something we can use
463 scan_devices(int skfd,
472 /* Get list of active devices */
473 ifc.ifc_len = sizeof(buff);
475 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
477 perror("SIOCGIFCONF");
482 /* Print the first match */
483 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
485 if(print_one_device(skfd, format, wtype, ifr->ifr_name) >= 0)
491 /*------------------------------------------------------------------*/
498 fputs("Usage iwgetid [OPTIONS] [ifname]\n"
500 " -a,--ap Print the access point address\n"
501 " -f,--freq Print the current frequency\n"
502 " -m,--mode Print the current mode\n"
503 " -p,--protocol Print the protocol name\n"
504 " -s,--scheme Format the output as a PCMCIA scheme identifier\n"
505 " -h,--help Print this message\n",
506 status ? stderr : stdout);
510 static const struct option long_opts[] = {
511 { "ap", no_argument, NULL, 'a' },
512 { "freq", no_argument, NULL, 'f' },
513 { "mode", no_argument, NULL, 'm' },
514 { "protocol", no_argument, NULL, 'p' },
515 { "help", no_argument, NULL, 'h' },
516 { "scheme", no_argument, NULL, 's' },
520 /*------------------------------------------------------------------*/
528 int skfd; /* generic raw socket desc. */
529 int format = FORMAT_DEFAULT;
530 int wtype = WTYPE_ESSID;
534 /* Check command line arguments */
535 while((opt = getopt_long(argc, argv, "afhmps", long_opts, NULL)) > 0)
540 /* User wants AP/Cell Address */
545 /* User wants frequency/channel */
550 /* User wants the mode */
555 /* User wants the protocol */
564 /* User wants a Scheme format */
565 format = FORMAT_SCHEME;
573 if(optind + 1 < argc) {
574 fputs("Too many arguments.\n", stderr);
578 /* Create a channel to the NET kernel. */
579 if((skfd = iw_sockets_open()) < 0)
585 /* Check if first argument is a device name */
588 /* Yes : query only this device */
589 ret = print_one_device(skfd, format, wtype, argv[optind]);
593 /* No : query all devices and print first found */
594 ret = scan_devices(skfd, format, wtype);