📄 iwconfig.c
字号:
/* Display it ! */ display_info(&info, ifname); break; case -ENOTSUP: fprintf(stderr, "%-8.16s no wireless extensions.\n\n", ifname); break; default: fprintf(stderr, "%-8.16s %s\n\n", ifname, strerror(-rc)); } return(rc);}/****************** COMMAND LINE MODIFIERS PARSING ******************//* * Factor out the parsing of command line modifiers. *//*------------------------------------------------------------------*//* * Map command line modifiers to the proper flags... */typedef struct iwconfig_modifier { const char * cmd; /* Command line shorthand */ __u16 flag; /* Flags to add */ __u16 exclude; /* Modifiers to exclude */} iwconfig_modifier;/*------------------------------------------------------------------*//* * Modifiers for Power */static const struct iwconfig_modifier iwmod_power[] = { { "min", IW_POWER_MIN, IW_POWER_MAX }, { "max", IW_POWER_MAX, IW_POWER_MIN }, { "period", IW_POWER_PERIOD, IW_POWER_TIMEOUT | IW_POWER_SAVING }, { "timeout", IW_POWER_TIMEOUT, IW_POWER_PERIOD | IW_POWER_SAVING }, { "saving", IW_POWER_SAVING, IW_POWER_TIMEOUT | IW_POWER_PERIOD },};#define IWMOD_POWER_NUM (sizeof(iwmod_power)/sizeof(iwmod_power[0]))/*------------------------------------------------------------------*//* * Modifiers for Retry */#ifndef WE_ESSENTIALstatic const struct iwconfig_modifier iwmod_retry[] = { { "min", IW_RETRY_MIN, IW_RETRY_MAX }, { "max", IW_RETRY_MAX, IW_RETRY_MIN }, { "short", IW_RETRY_SHORT, IW_RETRY_LONG }, { "long", IW_RETRY_LONG, IW_RETRY_SHORT }, { "limit", IW_RETRY_LIMIT, IW_RETRY_LIFETIME }, { "lifetime", IW_RETRY_LIFETIME, IW_RETRY_LIMIT },};#define IWMOD_RETRY_NUM (sizeof(iwmod_retry)/sizeof(iwmod_retry[0]))#endif /* WE_ESSENTIAL *//*------------------------------------------------------------------*//* * Parse command line modifiers. * Return error or number arg parsed. * Modifiers must be at the beggining of command line. */static intparse_modifiers(char * args[], /* Command line args */ int count, /* Args count */ __u16 * pout, /* Flags to write */ const struct iwconfig_modifier modifier[], int modnum){ int i = 0; int k = 0; __u16 result = 0; /* Default : no flag set */ /* Get all modifiers and value types on the command line */ do { for(k = 0; k < modnum; k++) { /* Check if matches */ if(!strcasecmp(args[i], modifier[k].cmd)) { /* Check for conflicting flags */ if(result & modifier[k].exclude) { errarg = i; return(IWERR_ARG_CONFLICT); } /* Just add it */ result |= modifier[k].flag; ++i; break; } } } /* For as long as current arg matched and not out of args */ while((i < count) && (k < modnum)); /* Check there remains one arg for value */ if(i >= count) return(IWERR_ARG_NUM); /* Return result */ *pout = result; return(i);}/*********************** SETTING SUB-ROUTINES ***********************//* * The following functions are use to set some wireless parameters and * are called by the set dispatcher set_info(). * They take as arguments the remaining of the command line, with * arguments processed already removed. * An error is indicated by a negative return value. * 0 and positive return values indicate the number of args consumed. *//*------------------------------------------------------------------*//* * Set ESSID */static intset_essid_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */{ struct iwreq wrq; int i = 1; char essid[IW_ESSID_MAX_SIZE + 1]; int we_kernel_version; if((!strcasecmp(args[0], "off")) || (!strcasecmp(args[0], "any"))) { wrq.u.essid.flags = 0; essid[0] = '\0'; } else if(!strcasecmp(args[0], "on")) { /* Get old essid */ memset(essid, '\0', sizeof(essid)); wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1; wrq.u.essid.flags = 0; if(iw_get_ext(skfd, ifname, SIOCGIWESSID, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.essid.flags = 1; } else { i = 0; /* '-' or '--' allow to escape the ESSID string, allowing * to set it to the string "any" or "off". * This is a big ugly, but it will do for now */ if((!strcmp(args[0], "-")) || (!strcmp(args[0], "--"))) { if(++i >= count) return(IWERR_ARG_NUM); } /* Check the size of what the user passed us to avoid * buffer overflows */ if(strlen(args[i]) > IW_ESSID_MAX_SIZE) { errmax = IW_ESSID_MAX_SIZE; return(IWERR_ARG_SIZE); } else { int temp; wrq.u.essid.flags = 1; strcpy(essid, args[i]); /* Size checked, all clear */ i++; /* Check for ESSID index */ if((i < count) && (sscanf(args[i], "[%i]", &temp) == 1) && (temp > 0) && (temp < IW_ENCODE_INDEX)) { wrq.u.essid.flags = temp; ++i; } } } /* Get version from kernel, device may not have range... */ we_kernel_version = iw_get_kernel_we_version(); /* Finally set the ESSID value */ wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = strlen(essid); if(we_kernel_version < 21) wrq.u.essid.length++; if(iw_set_ext(skfd, ifname, SIOCSIWESSID, &wrq) < 0) return(IWERR_SET_EXT); /* Var args */ return(i);}/*------------------------------------------------------------------*//* * Set Mode */static intset_mode_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */{ struct iwreq wrq; unsigned int k; /* Must be unsigned */ /* Avoid "Unused parameter" warning */ count = count; /* Check if it is a uint, otherwise get is as a string */ if(sscanf(args[0], "%i", &k) != 1) { k = 0; while((k < IW_NUM_OPER_MODE) && strncasecmp(args[0], iw_operation_mode[k], 3)) k++; } if(k >= IW_NUM_OPER_MODE) { errarg = 0; return(IWERR_ARG_TYPE); } wrq.u.mode = k; if(iw_set_ext(skfd, ifname, SIOCSIWMODE, &wrq) < 0) return(IWERR_SET_EXT); /* 1 arg */ return(1);}/*------------------------------------------------------------------*//* * Set frequency/channel */static intset_freq_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */{ struct iwreq wrq; int i = 1; if(!strcasecmp(args[0], "auto")) { wrq.u.freq.m = -1; wrq.u.freq.e = 0; wrq.u.freq.flags = 0; } else { if(!strcasecmp(args[0], "fixed")) { /* Get old frequency */ if(iw_get_ext(skfd, ifname, SIOCGIWFREQ, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.freq.flags = IW_FREQ_FIXED; } else /* Should be a numeric value */ { double freq; char * unit; freq = strtod(args[0], &unit); if(unit == args[0]) { errarg = 0; return(IWERR_ARG_TYPE); } if(unit != NULL) { if(unit[0] == 'G') freq *= GIGA; if(unit[0] == 'M') freq *= MEGA; if(unit[0] == 'k') freq *= KILO; } iw_float2freq(freq, &(wrq.u.freq)); wrq.u.freq.flags = IW_FREQ_FIXED; /* Check for an additional argument */ if((i < count) && (!strcasecmp(args[i], "auto"))) { wrq.u.freq.flags = 0; ++i; } if((i < count) && (!strcasecmp(args[i], "fixed"))) { wrq.u.freq.flags = IW_FREQ_FIXED; ++i; } } } if(iw_set_ext(skfd, ifname, SIOCSIWFREQ, &wrq) < 0) return(IWERR_SET_EXT); /* Var args */ return(i);}/*------------------------------------------------------------------*//* * Set Bit Rate */static intset_bitrate_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */{ struct iwreq wrq; int i = 1; wrq.u.bitrate.flags = 0; if(!strcasecmp(args[0], "auto")) { wrq.u.bitrate.value = -1; wrq.u.bitrate.fixed = 0; } else { if(!strcasecmp(args[0], "fixed")) { /* Get old bitrate */ if(iw_get_ext(skfd, ifname, SIOCGIWRATE, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.bitrate.fixed = 1; } else /* Should be a numeric value */ { double brate; char * unit; brate = strtod(args[0], &unit); if(unit == args[0]) { errarg = 0; return(IWERR_ARG_TYPE); } if(unit != NULL) { if(unit[0] == 'G') brate *= GIGA; if(unit[0] == 'M') brate *= MEGA; if(unit[0] == 'k') brate *= KILO; } wrq.u.bitrate.value = (long) brate; wrq.u.bitrate.fixed = 1; /* Check for an additional argument */ if((i < count) && (!strcasecmp(args[i], "auto"))) { wrq.u.bitrate.fixed = 0; ++i; } if((i < count) && (!strcasecmp(args[i], "fixed"))) { wrq.u.bitrate.fixed = 1; ++i; } if((i < count) && (!strcasecmp(args[i], "unicast"))) { wrq.u.bitrate.flags |= IW_BITRATE_UNICAST; ++i; } if((i < count) && (!strcasecmp(args[i], "broadcast"))) { wrq.u.bitrate.flags |= IW_BITRATE_BROADCAST; ++i; } } } if(iw_set_ext(skfd, ifname, SIOCSIWRATE, &wrq) < 0) return(IWERR_SET_EXT); /* Var args */ return(i);}/*------------------------------------------------------------------*//* * Set encryption */static intset_enc_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */{ struct iwreq wrq; int i = 1; unsigned char key[IW_ENCODING_TOKEN_MAX]; if(!strcasecmp(args[0], "on")) { /* Get old encryption information */ wrq.u.data.pointer = (caddr_t) key; wrq.u.data.length = IW_ENCODING_TOKEN_MAX; wrq.u.data.flags = 0; if(iw_get_ext(skfd, ifname, SIOCGIWENCODE, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.data.flags &= ~IW_ENCODE_DISABLED; /* Enable */ } else { int gotone = 0; int oldone; int keylen; int temp; wrq.u.data.pointer = (caddr_t) NULL; wrq.u.data.flags = 0; wrq.u.data.length = 0; i = 0; /* Allow arguments in any order (it's safe) */ do { oldone = gotone; /* -- Check for the key -- */ if(i < count) { keylen = iw_in_key_full(skfd, ifname, args[i], key, &wrq.u.data.flags); if(keylen > 0) { wrq.u.data.length = keylen; wrq.u.data.pointer = (caddr_t) key; ++i; gotone++; } } /* -- Check for token index -- */ if((i < count) && (sscanf(args[i], "[%i]", &temp) == 1) && (temp > 0) && (temp < IW_ENCODE_INDEX)) { wrq.u.encoding.flags |= temp; ++i; gotone++; } /* -- Check the various flags -- */ if((i < count) && (!strcasecmp(args[i], "off"))) { wrq.u.data.flags |= IW_ENCODE_DISABLED; ++i; gotone++; } if((i < count) && (!strcasecmp(args[i], "open"))) { wrq.u.data.flags |= IW_ENCODE_OPEN; ++i; gotone++; } if((i < count) && (!strncasecmp(args[i], "restricted", 5))) { wrq.u.data.flags |= IW_ENCODE_RESTRICTED; ++i; gotone++; } if((i < count) && (!strncasecmp(args[i], "temporary", 4))) { wrq.u.data.flags |= IW_ENCODE_TEMP; ++i; gotone++; } } while(gotone != oldone);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -