4 * Jean II - HPLB 97->99 - HPL 99->01
6 * Main code for "iwconfig". This is the generic tool for most
8 * You need to link this code against "iwcommon.c" and "-lm".
10 * This file is released under the GPL license.
13 #include "iwcommon.h" /* Header */
15 /**************************** VARIABLES ****************************/
16 char * operation_mode[] = { "Auto",
23 /************************* MISC SUBROUTINES **************************/
25 /*------------------------------------------------------------------*/
32 fprintf(stderr, "Usage: iwconfig interface [essid {NN|on|off}]\n");
33 fprintf(stderr, " [nwid {NN|on|off}]\n");
34 fprintf(stderr, " [freq N.NNNN[k|M|G]]\n");
35 fprintf(stderr, " [channel N]\n");
36 fprintf(stderr, " [sens N]\n");
37 fprintf(stderr, " [nick N]\n");
38 fprintf(stderr, " [rate {N|auto|fixed}]\n");
39 fprintf(stderr, " [rts {N|auto|fixed|off}]\n");
40 fprintf(stderr, " [frag {N|auto|fixed|off}]\n");
41 fprintf(stderr, " [enc NNNN-NNNN]\n");
42 fprintf(stderr, " [power { period N|timeout N}]\n");
43 fprintf(stderr, " [txpower N {mW|dBm}]\n");
48 /************************* DISPLAY ROUTINES **************************/
50 /*------------------------------------------------------------------*/
52 * Read /proc/net/wireless to get the latest statistics
55 iw_getstats(char * ifname,
58 FILE * f=fopen("/proc/net/wireless","r");
64 /* Loop on all devices */
65 while(fgets(buf,255,f))
68 while(*bp&&isspace(*bp))
70 /* Is it the good device ? */
71 if(strncmp(bp,ifname,strlen(ifname))==0 && bp[strlen(ifname)]==':')
79 stats->status = (unsigned short) t;
80 /* -- link quality -- */
81 bp = strtok(NULL, " ");
82 if(strchr(bp,'.') != NULL)
83 stats->qual.updated |= 1;
85 stats->qual.qual = (unsigned char) t;
86 /* -- signal level -- */
87 bp = strtok(NULL, " ");
88 if(strchr(bp,'.') != NULL)
89 stats->qual.updated |= 2;
91 stats->qual.level = (unsigned char) t;
92 /* -- noise level -- */
93 bp = strtok(NULL, " ");
94 if(strchr(bp,'.') != NULL)
95 stats->qual.updated += 4;
97 stats->qual.noise = (unsigned char) t;
98 /* -- discarded packets -- */
99 bp = strtok(NULL, " ");
100 sscanf(bp, "%d", &stats->discard.nwid);
101 bp = strtok(NULL, " ");
102 sscanf(bp, "%d", &stats->discard.code);
103 bp = strtok(NULL, " ");
104 sscanf(bp, "%d", &stats->discard.misc);
113 /*------------------------------------------------------------------*/
115 * Get wireless informations & config from the device driver
116 * We will call all the classical wireless ioctl on the driver through
117 * the socket to know what is supported and to get the settings...
122 struct wireless_info * info)
126 memset((char *) info, 0, sizeof(struct wireless_info));
128 /* Get wireless name */
129 strcpy(wrq.ifr_name, ifname);
130 if(ioctl(skfd, SIOCGIWNAME, &wrq) < 0)
131 /* If no wireless name : no wireless extensions */
134 strcpy(info->name, wrq.u.name);
137 if(get_range_info(skfd, ifname, &(info->range)) >= 0)
141 strcpy(wrq.ifr_name, ifname);
142 if(ioctl(skfd, SIOCGIWNWID, &wrq) >= 0)
145 memcpy(&(info->nwid), &(wrq.u.nwid), sizeof(iwparam));
148 /* Get frequency / channel */
149 strcpy(wrq.ifr_name, ifname);
150 if(ioctl(skfd, SIOCGIWFREQ, &wrq) >= 0)
153 info->freq = freq2float(&(wrq.u.freq));
156 /* Get sensitivity */
157 strcpy(wrq.ifr_name, ifname);
158 if(ioctl(skfd, SIOCGIWSENS, &wrq) >= 0)
161 memcpy(&(info->sens), &(wrq.u.sens), sizeof(iwparam));
164 /* Get encryption information */
165 strcpy(wrq.ifr_name, ifname);
166 wrq.u.data.pointer = (caddr_t) info->key;
167 wrq.u.data.length = 0;
168 wrq.u.data.flags = 0;
169 if(ioctl(skfd, SIOCGIWENCODE, &wrq) >= 0)
172 info->key_size = wrq.u.data.length;
173 info->key_flags = wrq.u.data.flags;
177 strcpy(wrq.ifr_name, ifname);
178 wrq.u.essid.pointer = (caddr_t) info->essid;
179 wrq.u.essid.length = 0;
180 wrq.u.essid.flags = 0;
181 if(ioctl(skfd, SIOCGIWESSID, &wrq) >= 0)
184 info->essid_on = wrq.u.data.flags;
188 strcpy(wrq.ifr_name, ifname);
189 if(ioctl(skfd, SIOCGIWAP, &wrq) >= 0)
191 info->has_ap_addr = 1;
192 memcpy(&(info->ap_addr), &(wrq.u.ap_addr), sizeof (sockaddr));
196 strcpy(wrq.ifr_name, ifname);
197 wrq.u.essid.pointer = (caddr_t) info->nickname;
198 wrq.u.essid.length = 0;
199 wrq.u.essid.flags = 0;
200 if(ioctl(skfd, SIOCGIWNICKN, &wrq) >= 0)
201 if(wrq.u.data.length > 1)
202 info->has_nickname = 1;
205 strcpy(wrq.ifr_name, ifname);
206 if(ioctl(skfd, SIOCGIWRATE, &wrq) >= 0)
208 info->has_bitrate = 1;
209 memcpy(&(info->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
212 /* Get RTS threshold */
213 strcpy(wrq.ifr_name, ifname);
214 if(ioctl(skfd, SIOCGIWRTS, &wrq) >= 0)
217 memcpy(&(info->rts), &(wrq.u.rts), sizeof(iwparam));
220 /* Get fragmentation threshold */
221 strcpy(wrq.ifr_name, ifname);
222 if(ioctl(skfd, SIOCGIWFRAG, &wrq) >= 0)
225 memcpy(&(info->frag), &(wrq.u.frag), sizeof(iwparam));
228 /* Get operation mode */
229 strcpy(wrq.ifr_name, ifname);
230 if(ioctl(skfd, SIOCGIWMODE, &wrq) >= 0)
232 if((wrq.u.mode < 6) && (wrq.u.mode >= 0))
234 info->mode = wrq.u.mode;
237 /* Get Power Management settings */
238 strcpy(wrq.ifr_name, ifname);
239 wrq.u.power.flags = 0;
240 if(ioctl(skfd, SIOCGIWPOWER, &wrq) >= 0)
243 memcpy(&(info->power), &(wrq.u.power), sizeof(iwparam));
247 /* Get Transmit Power */
248 strcpy(wrq.ifr_name, ifname);
249 if(ioctl(skfd, SIOCGIWTXPOW, &wrq) >= 0)
251 info->has_txpower = 1;
252 memcpy(&(info->txpower), &(wrq.u.txpower), sizeof(iwparam));
256 #if WIRELESS_EXT > 10
257 /* Get retry limit/lifetime */
258 strcpy(wrq.ifr_name, ifname);
259 if(ioctl(skfd, SIOCGIWRETRY, &wrq) >= 0)
262 memcpy(&(info->retry), &(wrq.u.retry), sizeof(iwparam));
264 #endif /* WIRELESS_EXT > 10 */
267 if(iw_getstats(ifname, &(info->stats)) >= 0)
275 /*------------------------------------------------------------------*/
277 * Print on the screen in a neat fashion all the info we have collected
281 display_info(struct wireless_info * info,
284 /* One token is more of less 5 character, 14 tokens per line */
285 int tokens = 3; /* For name */
287 /* Display device name and wireless name (name of the protocol used) */
288 printf("%-8.8s %s ", ifname, info->name);
290 /* Display ESSID (extended network), if any */
295 /* Does it have an ESSID index ? */
296 if((info->essid_on & IW_ENCODE_INDEX) > 1)
297 printf("ESSID:\"%s\" [%d] ", info->essid,
298 (info->essid_on & IW_ENCODE_INDEX));
300 printf("ESSID:\"%s\" ", info->essid);
303 printf("ESSID:off ");
306 /* Display NickName (station name), if any */
307 if(info->has_nickname)
308 printf("Nickname:\"%s\"", info->nickname);
311 if(info->has_essid || info->has_nickname)
317 /* Display Network ID */
320 /* Note : should display right number of digit according to info
321 * in range structure */
322 if(info->nwid.disabled)
323 printf("NWID:off/any ");
325 printf("NWID:%X ", info->nwid.value);
329 /* Display the current mode of operation */
332 printf("Mode:%s ", operation_mode[info->mode]);
336 /* Display frequency / channel */
339 if(info->freq < KILO)
340 printf("Channel:%g ", info->freq);
343 if(info->freq >= GIGA)
344 printf("Frequency:%gGHz ", info->freq / GIGA);
347 if(info->freq >= MEGA)
348 printf("Frequency:%gMHz ", info->freq / MEGA);
350 printf("Frequency:%gkHz ", info->freq / KILO);
356 /* Display the address of the current Access Point */
357 if(info->has_ap_addr)
359 /* A bit of clever formatting */
367 /* Oups ! No Access Point in Ad-Hoc mode */
368 if((info->has_mode) && (info->mode == IW_MODE_ADHOC))
371 printf("Access Point:");
372 printf(" %s", pr_ether(info->ap_addr.sa_data));
375 /* Display the currently used/set bit-rate */
376 if(info->has_bitrate)
378 /* A bit of clever formatting */
387 if(info->bitrate.fixed)
392 if(info->bitrate.value >= GIGA)
393 printf("%gGb/s", info->bitrate.value / GIGA);
395 if(info->bitrate.value >= MEGA)
396 printf("%gMb/s", info->bitrate.value / MEGA);
398 printf("%gkb/s", info->bitrate.value / KILO);
403 /* Display the Transmit Power */
404 if(info->has_txpower)
406 /* A bit of clever formatting */
415 if(info->txpower.disabled)
416 printf("Tx-Power:off ");
422 if(info->txpower.fixed)
427 /* Convert everything to dBm */
428 if(info->txpower.flags & IW_TXPOW_MWATT)
429 dbm = mwatt2dbm(info->txpower.value);
431 dbm = info->txpower.value;
434 printf("%d dBm ", dbm);
439 /* Display sensitivity */
442 /* A bit of clever formatting */
452 printf("Sensitivity=");
454 printf("Sensitivity:");
457 /* Display in dBm ? */
458 if(info->sens.value < 0)
459 printf("%d dBm ", info->sens.value);
461 printf("%d/%d ", info->sens.value, info->range.sensitivity);
463 printf("%d ", info->sens.value);
469 #if WIRELESS_EXT > 10
470 /* Display retry limit/lifetime information */
475 if(info->retry.disabled)
479 /* Let's check the value and its type */
480 if(info->retry.flags & IW_RETRY_TYPE)
481 print_retry_value(stdout, info->retry.value, info->retry.flags);
483 /* Let's check if nothing (simply on) */
484 if(info->retry.flags == IW_RETRY_ON)
488 tokens += 5; /* Between 3 and 5, depend on flags */
490 #endif /* WIRELESS_EXT > 10 */
492 /* Display the RTS threshold */
496 if(info->rts.disabled)
497 printf("RTS thr:off ");
506 printf("%d B ", info->rts.value);
511 /* Display the fragmentation threshold */
514 /* A bit of clever formatting */
523 if(info->frag.disabled)
524 printf("Fragment thr:off");
529 printf("Fragment thr=");
531 printf("Fragment thr:");
533 printf("%d B ", info->frag.value);
541 /* Display encryption information */
542 /* Note : we display only the "current" key, use iwlist to list all keys */
545 printf("Encryption key:");
546 if((info->key_flags & IW_ENCODE_DISABLED) || (info->key_size == 0))
550 /* Display the key */
551 print_key(stdout, info->key, info->key_size, info->key_flags);
554 if((info->key_flags & IW_ENCODE_INDEX) > 1)
555 printf(" [%d]", info->key_flags & IW_ENCODE_INDEX);
556 if(info->key_flags & IW_ENCODE_RESTRICTED)
557 printf(" Encryption mode:restricted");
558 if(info->key_flags & IW_ENCODE_OPEN)
559 printf(" Encryption mode:open");
564 /* Display Power Management information */
565 /* Note : we display only one parameter, period or timeout. If a device
566 * (such as HiperLan) has both, the user need to use iwlist... */
567 if(info->has_power) /* I hope the device has power ;-) */
569 printf("Power Management");
571 if(info->power.disabled)
575 /* Let's check the value and its type */
576 if(info->power.flags & IW_POWER_TYPE)
577 print_pm_value(stdout, info->power.value, info->power.flags);
579 /* Let's check the mode */
580 print_pm_mode(stdout, info->power.flags);
582 /* Let's check if nothing (simply on) */
583 if(info->power.flags == IW_POWER_ON)
589 /* Display statistics */
592 info->stats.qual.updated = 0x0; /* Not that reliable, disable */
594 print_stats(stdout, &info->stats.qual, &info->range, info->has_range);
596 printf(" Rx invalid nwid:%d invalid crypt:%d invalid misc:%d\n",
597 info->stats.discard.nwid,
598 info->stats.discard.code,
599 info->stats.discard.misc);
605 /*------------------------------------------------------------------*/
607 * Print on the screen in a neat fashion all the info we have collected
614 struct wireless_info info;
616 if(get_info(skfd, ifname, &info) < 0)
618 fprintf(stderr, "%-8.8s no wireless extensions.\n\n",
624 display_info(&info, ifname);
627 /*------------------------------------------------------------------*/
629 * Get info on all devices and print it on the screen
632 print_devices(int skfd)
639 /* Get list of active devices */
640 ifc.ifc_len = sizeof(buff);
642 if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
644 fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
650 for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
651 print_info(skfd, ifr->ifr_name);
654 /************************* SETTING ROUTINES **************************/
656 /*------------------------------------------------------------------*/
658 * Set the wireless options requested on command line
659 * This function is too long and probably should be split,
660 * because it look like the perfect definition of spaghetti code,
661 * but I'm way to lazy
664 set_info(int skfd, /* The socket */
665 char * args[], /* Command line args */
666 int count, /* Args count */
667 char * ifname) /* Dev name */
673 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
675 /* if nothing after the device name */
679 /* The other args on the line specify options to be set... */
680 for(i = 0; i < count; i++)
682 /* ---------- Set network ID ---------- */
683 if((!strcasecmp(args[i], "nwid")) ||
684 (!strcasecmp(args[i], "domain")))
689 if((!strcasecmp(args[i], "off")) ||
690 (!strcasecmp(args[i], "any")))
691 wrq.u.nwid.disabled = 1;
693 if(!strcasecmp(args[i], "on"))
696 if(ioctl(skfd, SIOCGIWNWID, &wrq) < 0)
698 fprintf(stderr, "SIOCGIWNWID: %s\n", strerror(errno));
701 strcpy(wrq.ifr_name, ifname);
702 wrq.u.nwid.disabled = 0;
705 if(sscanf(args[i], "%lX", (unsigned long *) &(wrq.u.nwid.value))
709 wrq.u.nwid.disabled = 0;
710 wrq.u.nwid.fixed = 1;
712 if(ioctl(skfd, SIOCSIWNWID, &wrq) < 0)
714 fprintf(stderr, "SIOCSIWNWID: %s\n", strerror(errno));
720 /* ---------- Set frequency / channel ---------- */
721 if((!strncmp(args[i], "freq", 4)) ||
722 (!strcmp(args[i], "channel")))
728 if(sscanf(args[i], "%lg", &(freq)) != 1)
730 if(index(args[i], 'G')) freq *= GIGA;
731 if(index(args[i], 'M')) freq *= MEGA;
732 if(index(args[i], 'k')) freq *= KILO;
734 float2freq(freq, &(wrq.u.freq));
736 if(ioctl(skfd, SIOCSIWFREQ, &wrq) < 0)
738 fprintf(stderr, "SIOCSIWFREQ: %s\n", strerror(errno));
744 /* ---------- Set sensitivity ---------- */
745 if(!strncmp(args[i], "sens", 4))
749 if(sscanf(args[i], "%d", &(wrq.u.sens.value)) != 1)
752 if(ioctl(skfd, SIOCSIWSENS, &wrq) < 0)
754 fprintf(stderr, "SIOCSIWSENS: %s\n", strerror(errno));
760 /* ---------- Set encryption stuff ---------- */
761 if((!strncmp(args[i], "enc", 3)) ||
762 (!strcmp(args[i], "key")))
764 unsigned char key[IW_ENCODING_TOKEN_MAX];
769 if(!strcasecmp(args[i], "on"))
771 /* Get old encryption information */
772 wrq.u.data.pointer = (caddr_t) key;
773 wrq.u.data.length = 0;
774 wrq.u.data.flags = 0;
775 if(ioctl(skfd, SIOCGIWENCODE, &wrq) < 0)
777 fprintf(stderr, "SIOCGIWENCODE: %s\n", strerror(errno));
780 strcpy(wrq.ifr_name, ifname);
781 wrq.u.data.flags &= ~IW_ENCODE_DISABLED; /* Enable */
791 wrq.u.data.pointer = (caddr_t) NULL;
792 wrq.u.data.flags = 0;
793 wrq.u.data.length = 0;
795 /* -- Check for the key -- */
796 if(!strncmp(args[i], "s:", 2))
798 /* First case : as an ASCII string */
799 wrq.u.data.length = strlen(args[i] + 2);
800 if(wrq.u.data.length > IW_ENCODING_TOKEN_MAX)
801 wrq.u.data.length = IW_ENCODING_TOKEN_MAX;
802 strncpy(key, args[i] + 2, wrq.u.data.length);
803 wrq.u.data.pointer = (caddr_t) key;
809 /* Second case : has hexadecimal digits */
810 p = buff = malloc(strlen(args[i]) + 1);
811 strcpy(buff, args[i]);
813 p = strtok(buff, "-:;.,");
814 while(p != (char *) NULL)
816 if(sscanf(p, "%2X", &temp) != 1)
821 key[k++] = (unsigned char) (temp & 0xFF);
822 if(strlen(p) > 2) /* Token not finished yet */
825 p = strtok((char *) NULL, "-:;.,");
832 wrq.u.data.length = k;
833 wrq.u.data.pointer = (caddr_t) key;
837 /* -- Check for token index -- */
839 (sscanf(args[i], "[%d]", &temp) == 1) &&
840 (temp > 0) && (temp < IW_ENCODE_INDEX))
842 wrq.u.encoding.flags |= temp;
847 /* -- Check the various flags -- */
850 if(!strcasecmp(args[i], "off"))
851 wrq.u.data.flags |= IW_ENCODE_DISABLED;
852 if(!strcasecmp(args[i], "open"))
853 wrq.u.data.flags |= IW_ENCODE_OPEN;
854 if(!strncasecmp(args[i], "restricted", 5))
855 wrq.u.data.flags |= IW_ENCODE_RESTRICTED;
856 if(wrq.u.data.flags & IW_ENCODE_FLAGS)
868 if(ioctl(skfd, SIOCSIWENCODE, &wrq) < 0)
870 fprintf(stderr, "SIOCSIWENCODE(%d): %s\n",
871 errno, strerror(errno));
877 /* ---------- Set ESSID ---------- */
878 if(!strcasecmp(args[i], "essid"))
880 char essid[IW_ESSID_MAX_SIZE + 1];
885 if((!strcasecmp(args[i], "off")) ||
886 (!strcasecmp(args[i], "any")))
888 wrq.u.essid.flags = 0;
892 if(!strcasecmp(args[i], "on"))
895 wrq.u.essid.pointer = (caddr_t) essid;
896 wrq.u.essid.length = 0;
897 wrq.u.essid.flags = 0;
898 if(ioctl(skfd, SIOCGIWESSID, &wrq) < 0)
900 fprintf(stderr, "SIOCGIWESSID: %s\n", strerror(errno));
903 strcpy(wrq.ifr_name, ifname);
904 wrq.u.essid.flags = 1;
907 if(strlen(args[i]) > IW_ESSID_MAX_SIZE)
909 fprintf(stderr, "ESSID too long (max %d): ``%s''\n",
910 IW_ESSID_MAX_SIZE, args[i]);
917 wrq.u.essid.flags = 1;
918 strcpy(essid, args[i]);
920 /* Check for ESSID index */
921 if(((i+1) < count) &&
922 (sscanf(args[i+1], "[%d]", &temp) == 1) &&
923 (temp > 0) && (temp < IW_ENCODE_INDEX))
925 wrq.u.essid.flags = temp;
930 wrq.u.essid.pointer = (caddr_t) essid;
931 wrq.u.essid.length = strlen(essid) + 1;
932 if(ioctl(skfd, SIOCSIWESSID, &wrq) < 0)
934 fprintf(stderr, "SIOCSIWESSID: %s\n", strerror(errno));
940 /* ---------- Set AP address ---------- */
941 if(!strcasecmp(args[i], "ap"))
946 /* Check if we have valid address types */
947 if(check_addr_type(skfd, ifname) < 0)
949 fprintf(stderr, "%-8.8s Interface doesn't support MAC & IP addresses\n", ifname);
953 /* Get the address */
954 if(in_addr(skfd, ifname, args[i++], &(wrq.u.ap_addr)) < 0)
957 if(ioctl(skfd, SIOCSIWAP, &wrq) < 0)
959 fprintf(stderr, "SIOCSIWAP: %s\n", strerror(errno));
965 /* ---------- Set NickName ---------- */
966 if(!strncmp(args[i], "nick", 4))
971 if(strlen(args[i]) > IW_ESSID_MAX_SIZE)
973 fprintf(stderr, "Name too long (max %d) : ``%s''\n",
974 IW_ESSID_MAX_SIZE, args[i]);
978 wrq.u.essid.pointer = (caddr_t) args[i];
979 wrq.u.essid.length = strlen(args[i]) + 1;
980 if(ioctl(skfd, SIOCSIWNICKN, &wrq) < 0)
982 fprintf(stderr, "SIOCSIWNICKN: %s\n", strerror(errno));
988 /* ---------- Set Bit-Rate ---------- */
989 if((!strncmp(args[i], "bit", 3)) ||
990 (!strcmp(args[i], "rate")))
994 if(!strcasecmp(args[i], "auto"))
996 wrq.u.bitrate.value = -1;
997 wrq.u.bitrate.fixed = 0;
1001 if(!strcasecmp(args[i], "fixed"))
1003 /* Get old bitrate */
1004 if(ioctl(skfd, SIOCGIWRATE, &wrq) < 0)
1006 fprintf(stderr, "SIOCGIWRATE: %s\n", strerror(errno));
1009 strcpy(wrq.ifr_name, ifname);
1010 wrq.u.bitrate.fixed = 1;
1012 else /* Should be a numeric value */
1016 if(sscanf(args[i], "%lg", &(brate)) != 1)
1018 if(index(args[i], 'G')) brate *= GIGA;
1019 if(index(args[i], 'M')) brate *= MEGA;
1020 if(index(args[i], 'k')) brate *= KILO;
1021 wrq.u.bitrate.value = (long) brate;
1022 wrq.u.bitrate.fixed = 1;
1024 /* Check for an additional argument */
1025 if(((i+1) < count) &&
1026 (!strcasecmp(args[i+1], "auto")))
1028 wrq.u.bitrate.fixed = 0;
1031 if(((i+1) < count) &&
1032 (!strcasecmp(args[i+1], "fixed")))
1034 wrq.u.bitrate.fixed = 1;
1040 if(ioctl(skfd, SIOCSIWRATE, &wrq) < 0)
1042 fprintf(stderr, "SIOCSIWRATE: %s\n", strerror(errno));
1048 /* ---------- Set RTS threshold ---------- */
1049 if(!strncasecmp(args[i], "rts", 3))
1054 wrq.u.rts.value = -1;
1055 wrq.u.rts.fixed = 1;
1056 wrq.u.rts.disabled = 0;
1057 if(!strcasecmp(args[i], "off"))
1058 wrq.u.rts.disabled = 1; /* i.e. max size */
1060 if(!strcasecmp(args[i], "auto"))
1061 wrq.u.rts.fixed = 0;
1064 if(!strcasecmp(args[i], "fixed"))
1066 /* Get old RTS threshold */
1067 if(ioctl(skfd, SIOCGIWRTS, &wrq) < 0)
1069 fprintf(stderr, "SIOCGIWRTS: %s\n", strerror(errno));
1072 strcpy(wrq.ifr_name, ifname);
1073 wrq.u.rts.fixed = 1;
1075 else /* Should be a numeric value */
1076 if(sscanf(args[i], "%ld", (unsigned long *) &(wrq.u.rts.value))
1081 if(ioctl(skfd, SIOCSIWRTS, &wrq) < 0)
1083 fprintf(stderr, "SIOCSIWRTS: %s\n", strerror(errno));
1089 /* ---------- Set fragmentation threshold ---------- */
1090 if(!strncmp(args[i], "frag", 4))
1095 wrq.u.frag.value = -1;
1096 wrq.u.frag.fixed = 1;
1097 wrq.u.frag.disabled = 0;
1098 if(!strcasecmp(args[i], "off"))
1099 wrq.u.frag.disabled = 1; /* i.e. max size */
1101 if(!strcasecmp(args[i], "auto"))
1102 wrq.u.frag.fixed = 0;
1105 if(!strcasecmp(args[i], "fixed"))
1107 /* Get old fragmentation threshold */
1108 if(ioctl(skfd, SIOCGIWFRAG, &wrq) < 0)
1110 fprintf(stderr, "SIOCGIWFRAG: %s\n", strerror(errno));
1113 strcpy(wrq.ifr_name, ifname);
1114 wrq.u.frag.fixed = 1;
1116 else /* Should be a numeric value */
1117 if(sscanf(args[i], "%ld", (unsigned long *) &(wrq.u.frag.value))
1122 if(ioctl(skfd, SIOCSIWFRAG, &wrq) < 0)
1124 fprintf(stderr, "SIOCSIWFRAG: %s\n", strerror(errno));
1130 /* ---------- Set operation mode ---------- */
1131 if(!strcmp(args[i], "mode"))
1139 if(sscanf(args[i], "%d", &k) != 1)
1142 while(k < 6 && strncasecmp(args[i], operation_mode[k], 3))
1145 if((k > 5) || (k < 0))
1149 if(ioctl(skfd, SIOCSIWMODE, &wrq) < 0)
1151 fprintf(stderr, "SIOCSIWMODE: %s\n", strerror(errno));
1157 /* ---------- Set Power Management ---------- */
1158 if(!strncmp(args[i], "power", 3))
1163 if(!strcasecmp(args[i], "off"))
1164 wrq.u.power.disabled = 1; /* i.e. max size */
1166 if(!strcasecmp(args[i], "on"))
1168 /* Get old Power info */
1169 if(ioctl(skfd, SIOCGIWPOWER, &wrq) < 0)
1171 fprintf(stderr, "SIOCGIWPOWER: %s\n", strerror(errno));
1174 strcpy(wrq.ifr_name, ifname);
1175 wrq.u.power.disabled = 0;
1181 /* Default - nope */
1182 wrq.u.power.flags = IW_POWER_ON;
1183 wrq.u.power.disabled = 0;
1185 /* Check value modifier */
1186 if(!strcasecmp(args[i], "min"))
1188 wrq.u.power.flags |= IW_POWER_MIN;
1193 if(!strcasecmp(args[i], "max"))
1195 wrq.u.power.flags |= IW_POWER_MAX;
1200 /* Check value type */
1201 if(!strcasecmp(args[i], "period"))
1203 wrq.u.power.flags |= IW_POWER_PERIOD;
1208 if(!strcasecmp(args[i], "timeout"))
1210 wrq.u.power.flags |= IW_POWER_TIMEOUT;
1215 /* Is there any value to grab ? */
1216 if(sscanf(args[i], "%lg", &(temp)) == 1)
1218 temp *= MEGA; /* default = s */
1219 if(index(args[i], 'u')) temp /= MEGA;
1220 if(index(args[i], 'm')) temp /= KILO;
1221 wrq.u.power.value = (long) temp;
1222 if((wrq.u.power.flags & IW_POWER_TYPE) == 0)
1223 wrq.u.power.flags |= IW_POWER_PERIOD;
1228 /* Now, check the mode */
1231 if(!strcasecmp(args[i], "all"))
1232 wrq.u.power.flags |= IW_POWER_ALL_R;
1233 if(!strncasecmp(args[i], "unicast", 4))
1234 wrq.u.power.flags |= IW_POWER_UNICAST_R;
1235 if(!strncasecmp(args[i], "multicast", 5))
1236 wrq.u.power.flags |= IW_POWER_MULTICAST_R;
1237 if(!strncasecmp(args[i], "force", 5))
1238 wrq.u.power.flags |= IW_POWER_FORCE_S;
1239 if(!strcasecmp(args[i], "repeat"))
1240 wrq.u.power.flags |= IW_POWER_REPEATER;
1241 if(wrq.u.power.flags & IW_POWER_MODE)
1252 if(ioctl(skfd, SIOCSIWPOWER, &wrq) < 0)
1254 fprintf(stderr, "SIOCSIWPOWER(%d): %s\n",
1255 errno, strerror(errno));
1261 #if WIRELESS_EXT > 9
1262 /* ---------- Set Transmit-Power ---------- */
1263 if(!strncmp(args[i], "txpower", 3))
1265 struct iw_range range;
1270 /* Extract range info */
1271 if(get_range_info(skfd, ifname, &range) < 0)
1272 memset(&range, 0, sizeof(range));
1274 /* Prepare the request */
1275 strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
1276 wrq.u.txpower.value = -1;
1277 wrq.u.txpower.fixed = 1;
1278 wrq.u.txpower.disabled = 0;
1279 wrq.u.data.flags = IW_TXPOW_DBM;
1280 if(!strcasecmp(args[i], "off"))
1281 wrq.u.txpower.disabled = 1; /* i.e. turn radio off */
1283 if(!strcasecmp(args[i], "auto"))
1284 wrq.u.txpower.fixed = 0; /* i.e. use power control */
1287 if(!strcasecmp(args[i], "fixed"))
1289 /* Get old tx-power */
1290 if(ioctl(skfd, SIOCGIWTXPOW, &wrq) < 0)
1292 fprintf(stderr, "SIOCGIWTXPOW: %s\n", strerror(errno));
1295 strcpy(wrq.ifr_name, ifname);
1296 wrq.u.txpower.fixed = 1;
1298 else /* Should be a numeric value */
1304 if(sscanf(args[i], "%ld",
1305 (unsigned long *) &(power)) != 1)
1308 /* Check if milliwatt */
1309 ismwatt = (index(args[i], 'm') != NULL);
1312 if(!ismwatt && (range.txpower_capa & IW_TXPOW_MWATT))
1314 power = dbm2mwatt(power);
1315 wrq.u.data.flags = IW_TXPOW_MWATT;
1317 if(ismwatt && !(range.txpower_capa & IW_TXPOW_MWATT))
1318 power = mwatt2dbm(power);
1319 wrq.u.bitrate.value = power;
1321 /* Check for an additional argument */
1322 if(((i+1) < count) &&
1323 (!strcasecmp(args[i+1], "auto")))
1325 wrq.u.txpower.fixed = 0;
1328 if(((i+1) < count) &&
1329 (!strcasecmp(args[i+1], "fixed")))
1331 wrq.u.txpower.fixed = 1;
1337 if(ioctl(skfd, SIOCSIWTXPOW, &wrq) < 0)
1339 fprintf(stderr, "SIOCSIWTXPOW: %s\n", strerror(errno));
1346 #if WIRELESS_EXT > 10
1347 /* ---------- Set Power Management ---------- */
1348 if(!strncmp(args[i], "retry", 3))
1356 /* Default - nope */
1357 wrq.u.retry.flags = IW_RETRY_LIMIT;
1358 wrq.u.retry.disabled = 0;
1360 /* Check value modifier */
1361 if(!strcasecmp(args[i], "min"))
1363 wrq.u.retry.flags |= IW_RETRY_MIN;
1368 if(!strcasecmp(args[i], "max"))
1370 wrq.u.retry.flags |= IW_RETRY_MAX;
1375 /* Check value type */
1376 if(!strcasecmp(args[i], "limit"))
1378 wrq.u.retry.flags |= IW_RETRY_LIMIT;
1383 if(!strncasecmp(args[i], "lifetime", 4))
1385 wrq.u.retry.flags |= IW_RETRY_LIFETIME;
1390 /* Is there any value to grab ? */
1391 if(sscanf(args[i], "%lg", &(temp)) == 1)
1393 /* Limit is absolute, on the other hand lifetime is seconds */
1394 if(!(wrq.u.retry.flags & IW_RETRY_LIMIT))
1396 /* Normalise lifetime */
1397 temp *= MEGA; /* default = s */
1398 if(index(args[i], 'u')) temp /= MEGA;
1399 if(index(args[i], 'm')) temp /= KILO;
1401 wrq.u.retry.value = (long) temp;
1410 if(ioctl(skfd, SIOCSIWRETRY, &wrq) < 0)
1412 fprintf(stderr, "SIOCSIWRETRY(%d): %s\n",
1413 errno, strerror(errno));
1419 #endif /* WIRELESS_EXT > 10 */
1421 /* ---------- Other ---------- */
1422 /* Here we have an unrecognised arg... */
1423 fprintf(stderr, "Invalid argument : %s\n", args[i]);
1426 } /* for(index ... */
1430 /******************************* MAIN ********************************/
1432 /*------------------------------------------------------------------*/
1440 int skfd = -1; /* generic raw socket desc. */
1443 /* Create a channel to the NET kernel. */
1444 if((skfd = sockets_open()) < 0)
1450 /* No argument : show the list of all device + info */
1453 print_devices(skfd);
1458 /* Special case for help... */
1459 if((!strncmp(argv[1], "-h", 9)) ||
1460 (!strcmp(argv[1], "--help")))
1467 /* The device name must be the first argument */
1470 print_info(skfd, argv[1]);
1475 /* The other args on the line specify options to be set... */
1476 goterr = set_info(skfd, argv + 2, argc - 2, argv[1]);
1478 /* Close the socket. */