📄 iwlib.c
字号:
/*------------------------------------------------------------------*//* * Output the link statistics, taking care of formating */voidiw_print_stats(char * buffer, iwqual * qual, iwrange * range, int has_range){ /* Just do it */ if(has_range && (qual->level != 0)) { /* If the statistics are in dBm */ if(qual->level > range->max_qual.level) { /* Statistics are in dBm (absolute power measurement) */ sprintf(buffer, "Quality:%d/%d Signal level:%d dBm Noise level:%d dBm%s", qual->qual, range->max_qual.qual, qual->level - 0x100, qual->noise - 0x100, (qual->updated & 0x7) ? " (updated)" : ""); } else { /* Statistics are relative values (0 -> max) */ sprintf(buffer, "Quality:%d/%d Signal level:%d/%d Noise level:%d/%d%s", qual->qual, range->max_qual.qual, qual->level, range->max_qual.level, qual->noise, range->max_qual.noise, (qual->updated & 0x7) ? " (updated)" : ""); } } else { /* We can't read the range, so we don't know... */ sprintf(buffer, "Quality:%d Signal level:%d Noise level:%d%s", qual->qual, qual->level, qual->noise, (qual->updated & 0x7) ? " (updated)" : ""); }}/*********************** ENCODING SUBROUTINES ***********************//*------------------------------------------------------------------*//* * Output the encoding key, with a nice formating */voidiw_print_key(char * buffer, unsigned char * key, int key_size, int key_flags){ int i; /* Is the key present ??? */ if(key_flags & IW_ENCODE_NOKEY) { /* Nope : print on or dummy */ if(key_size <= 0) strcpy(buffer, "on"); else { strcpy(buffer, "**"); buffer +=2; for(i = 1; i < key_size; i++) { if((i & 0x1) == 0) strcpy(buffer++, "-"); strcpy(buffer, "**"); buffer +=2; } } } else { /* Yes : print the key */ sprintf(buffer, "%.2X", key[0]); buffer +=2; for(i = 1; i < key_size; i++) { if((i & 0x1) == 0) strcpy(buffer++, "-"); sprintf(buffer, "%.2X", key[i]); buffer +=2; } }}/*------------------------------------------------------------------*//* * Convert a passphrase into a key * ### NOT IMPLEMENTED ### * Return size of the key, or 0 (no key) or -1 (error) */intiw_pass_key(char * input, unsigned char * key){ input = input; key = key; fprintf(stderr, "Error: Passphrase not implemented\n"); return(-1);}/*------------------------------------------------------------------*//* * Parse a key from the command line. * Return size of the key, or 0 (no key) or -1 (error) */intiw_in_key(char * input, unsigned char * key){ int keylen = 0; /* Check the type of key */ if(!strncmp(input, "s:", 2)) { /* First case : as an ASCII string (Lucent/Agere cards) */ keylen = strlen(input + 2); /* skip "s:" */ if(keylen > IW_ENCODING_TOKEN_MAX) keylen = IW_ENCODING_TOKEN_MAX; strncpy(key, input + 2, keylen); } else if(!strncmp(input, "p:", 2)) { /* Second case : as a passphrase (PrismII cards) */ return(iw_pass_key(input + 2, key)); /* skip "p:" */ } else { char * buff; char * hex; char * out; char * p; /* Third case : as hexadecimal digits */ buff = malloc(IW_ENCODING_TOKEN_MAX + strlen(input) + 1); if(buff == NULL) { fprintf(stderr, "Malloc failed (string too long ?)\n"); return(-1); } /* Preserve original buffers (both in & out) */ hex = buff + IW_ENCODING_TOKEN_MAX; strcpy(hex, input); out = buff; /* Parse */ p = strtok(hex, "-:;.,"); while((p != (char *) NULL) && (keylen < IW_ENCODING_TOKEN_MAX)) { int temph; int templ; int count; int len; /* Get each char separatly (and not by two) so that we don't * get confused by 'enc' (=> '0E'+'0C') and similar */ count = sscanf(p, "%1X%1X", &temph, &templ); if(count < 1) return(-1); /* Error -> non-hex char */ /* Fixup odd strings such as '123' is '01'+'23' and not '12'+'03'*/ len = strlen(p); if(len % 2) count = 1; /* Put back two chars as one byte */ if(count == 2) templ |= temph << 4; else templ = temph; out[keylen++] = (unsigned char) (templ & 0xFF); /* Check where to get next char from */ if(len > count) /* Token not finished yet */ p += count; else p = strtok((char *) NULL, "-:;.,"); } memcpy(key, out, keylen); free(buff); } return(keylen);}/*------------------------------------------------------------------*//* * Parse a key from the command line. * Return size of the key, or 0 (no key) or -1 (error) */intiw_in_key_full(int skfd, char * ifname, char * input, unsigned char * key, __u16 * flags){ int keylen = 0; char * p; if(!strncmp(input, "l:", 2)) {#if WIRELESS_EXT > 15 struct iw_range range;#endif /* Extra case : as a login (user:passwd - Cisco LEAP) */ keylen = strlen(input + 2) + 1; /* skip "l:", add '\0' */ /* Most user/password is 8 char, so 18 char total, < 32 */ if(keylen > IW_ENCODING_TOKEN_MAX) keylen = IW_ENCODING_TOKEN_MAX; memcpy(key, input + 2, keylen); /* Separate the two strings */ p = strchr(key, ':'); if(p == NULL) { fprintf(stderr, "Error: Invalid login format\n"); return(-1); } *p = '\0';#if WIRELESS_EXT > 15 printf("flags = %X, index = %X\n", *flags, range.encoding_login_index); if((*flags & IW_ENCODE_INDEX) == 0) { /* Extract range info */ if(iw_get_range_info(skfd, ifname, &range) < 0) memset(&range, 0, sizeof(range)); printf("flags = %X, index = %X\n", *flags, range.encoding_login_index); /* Set the index the driver expects */ *flags |= range.encoding_login_index & IW_ENCODE_INDEX; } printf("flags = %X, index = %X\n", *flags, range.encoding_login_index);#else /* Avoid "Unused parameter" warning */ skfd = skfd; ifname = ifname; flags = flags;#endif } else /* Simpler routine above */ keylen = iw_in_key(input, key); return(keylen);}/******************* POWER MANAGEMENT SUBROUTINES *******************//*------------------------------------------------------------------*//* * Output a power management value with all attributes... */voidiw_print_pm_value(char * buffer, int value, int flags){ /* Modifiers */ if(flags & IW_POWER_MIN) { strcpy(buffer, " min"); buffer += 4; } if(flags & IW_POWER_MAX) { strcpy(buffer, " max"); buffer += 4; } /* Type */ if(flags & IW_POWER_TIMEOUT) { strcpy(buffer, " timeout:"); buffer += 9; } else { strcpy(buffer, " period:"); buffer += 8; } /* Display value without units */ if(flags & IW_POWER_RELATIVE) sprintf(buffer, "%g", ((double) value) / MEGA); else { /* Display value with units */ if(value >= (int) MEGA) sprintf(buffer, "%gs", ((double) value) / MEGA); else if(value >= (int) KILO) sprintf(buffer, "%gms", ((double) value) / KILO); else sprintf(buffer, "%dus", value); }}/*------------------------------------------------------------------*//* * Output a power management mode */voidiw_print_pm_mode(char * buffer, int flags){ /* Print the proper mode... */ switch(flags & IW_POWER_MODE) { case IW_POWER_UNICAST_R: strcpy(buffer, "mode:Unicast only received"); break; case IW_POWER_MULTICAST_R: strcpy(buffer, "mode:Multicast only received"); break; case IW_POWER_ALL_R: strcpy(buffer, "mode:All packets received"); break; case IW_POWER_FORCE_S: strcpy(buffer, "mode:Force sending"); break; case IW_POWER_REPEATER: strcpy(buffer, "mode:Repeat multicasts"); break; default: strcpy(buffer, ""); break; }}/***************** RETRY LIMIT/LIFETIME SUBROUTINES *****************/#if WIRELESS_EXT > 10/*------------------------------------------------------------------*//* * Output a retry value with all attributes... */voidiw_print_retry_value(char * buffer, int value, int flags){ /* Modifiers */ if(flags & IW_RETRY_MIN) { strcpy(buffer, " min"); buffer += 4; } if(flags & IW_RETRY_MAX) { strcpy(buffer, " max"); buffer += 4; } /* Type lifetime of limit */ if(flags & IW_RETRY_LIFETIME) { strcpy(buffer, " lifetime:"); buffer += 10; /* Display value without units */ if(flags & IW_POWER_RELATIVE) sprintf(buffer, "%g", ((double) value) / MEGA); else { /* Display value with units */ if(value >= (int) MEGA) sprintf(buffer, "%gs", ((double) value) / MEGA); else if(value >= (int) KILO) sprintf(buffer, "%gms", ((double) value) / KILO); else sprintf(buffer, "%dus", value); } } else sprintf(buffer, " limit:%d", value);}#endif /* WIRELESS_EXT > 10 *//************************* TIME SUBROUTINES *************************//*------------------------------------------------------------------*//* * Print timestamps * Inspired from irdadump... */voidiw_print_timeval(char * buffer, const struct timeval * time){ int s; s = (time->tv_sec) % 86400; sprintf(buffer, "%02d:%02d:%02d.%06u ", s / 3600, (s % 3600) / 60, s % 60, (u_int32_t) time->tv_usec);}/*********************** ADDRESS SUBROUTINES ************************//* * This section is mostly a cut & past from net-tools-1.2.0 * manage address display and input... *//*------------------------------------------------------------------*//* * Check if interface support the right MAC address type... */intiw_check_mac_addr_type(int skfd, char * ifname){ struct ifreq ifr; /* Get the type of hardware address */ strncpy(ifr.ifr_name, ifname, IFNAMSIZ); if((ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) || (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)) { /* Deep trouble... */ fprintf(stderr, "Interface %s doesn't support MAC addresses\n", ifname); return(-1); }#ifdef DEBUG printf("Hardware : %d - %s\n", ifr.ifr_hwaddr.sa_family, iw_ether_ntoa((struct ether_addr *) ifr.ifr_hwaddr.sa_data));#endif return(0);}/*------------------------------------------------------------------*//* * Check if interface support the right interface address type... */intiw_check_if_addr_type(int skfd, char * ifname){ struct ifreq ifr; /* Get the type of interface address */ strncpy(ifr.ifr_name, ifname, IFNAMSIZ); if((ioctl(skfd, SIOCGIFADDR, &ifr) < 0) || (ifr.ifr_addr.sa_family != AF_INET)) { /* Deep trouble... */ fprintf(stderr, "Interface %s doesn't support IP addresses\n", ifname); return(-1); }#ifdef DEBUG printf("Interface : %d - 0x%lX\n", ifr.ifr_addr.sa_family, *((unsigned long *) ifr.ifr_addr.sa_data));#endif return(0);}#if 0/*------------------------------------------------------------------*//* * Check if interface support the right address types... */intiw_check_addr_type(int skfd, char * ifname)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -