📄 iwconfig.c
字号:
/* * Wireless Tools * * Jean II - HPLB 97->99 - HPL 99->01 * * Main code for "iwconfig". This is the generic tool for most * manipulations... * You need to link this code against "iwcommon.c" and "-lm". * * This file is released under the GPL license. */#include "iwcommon.h" /* Header *//**************************** VARIABLES ****************************/char * operation_mode[] = { "Auto", "Ad-Hoc", "Managed", "Master", "Repeater", "Secondary" };/************************* MISC SUBROUTINES **************************//*------------------------------------------------------------------*//* * Print usage string */static voidiw_usage(void){ fprintf(stderr, "Usage: iwconfig interface [essid {NN|on|off}]\n"); fprintf(stderr, " [nwid {NN|on|off}]\n"); fprintf(stderr, " [freq N.NNNN[k|M|G]]\n"); fprintf(stderr, " [channel N]\n"); fprintf(stderr, " [sens N]\n"); fprintf(stderr, " [nick N]\n"); fprintf(stderr, " [rate {N|auto|fixed}]\n"); fprintf(stderr, " [rts {N|auto|fixed|off}]\n"); fprintf(stderr, " [frag {N|auto|fixed|off}]\n"); fprintf(stderr, " [enc NNNN-NNNN]\n"); fprintf(stderr, " [power { period N|timeout N}]\n"); fprintf(stderr, " [txpower N {mW|dBm}]\n"); exit(1);}/************************* DISPLAY ROUTINES **************************//*------------------------------------------------------------------*//* * Read /proc/net/wireless to get the latest statistics */static intiw_getstats(char * ifname, iwstats * stats){ FILE * f=fopen("/proc/net/wireless","r"); char buf[256]; char * bp; int t; if(f==NULL) return -1; /* Loop on all devices */ while(fgets(buf,255,f)) { bp=buf; while(*bp&&isspace(*bp)) bp++; /* Is it the good device ? */ if(strncmp(bp,ifname,strlen(ifname))==0 && bp[strlen(ifname)]==':') { /* Skip ethX: */ bp=strchr(bp,':'); bp++; /* -- status -- */ bp = strtok(bp, " "); sscanf(bp, "%X", &t); stats->status = (unsigned short) t; /* -- link quality -- */ bp = strtok(NULL, " "); if(strchr(bp,'.') != NULL) stats->qual.updated |= 1; sscanf(bp, "%d", &t); stats->qual.qual = (unsigned char) t; /* -- signal level -- */ bp = strtok(NULL, " "); if(strchr(bp,'.') != NULL) stats->qual.updated |= 2; sscanf(bp, "%d", &t); stats->qual.level = (unsigned char) t; /* -- noise level -- */ bp = strtok(NULL, " "); if(strchr(bp,'.') != NULL) stats->qual.updated += 4; sscanf(bp, "%d", &t); stats->qual.noise = (unsigned char) t; /* -- discarded packets -- */ bp = strtok(NULL, " "); sscanf(bp, "%d", &stats->discard.nwid); bp = strtok(NULL, " "); sscanf(bp, "%d", &stats->discard.code); bp = strtok(NULL, " "); sscanf(bp, "%d", &stats->discard.misc); fclose(f); return 0; } } fclose(f); return -1;}/*------------------------------------------------------------------*//* * Get wireless informations & config from the device driver * We will call all the classical wireless ioctl on the driver through * the socket to know what is supported and to get the settings... */static intget_info(int skfd, char * ifname, struct wireless_info * info){ struct iwreq wrq; memset((char *) info, 0, sizeof(struct wireless_info)); /* Get wireless name */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWNAME, &wrq) < 0) /* If no wireless name : no wireless extensions */ return(-1); else strcpy(info->name, wrq.u.name); /* Get ranges */ if(get_range_info(skfd, ifname, &(info->range)) >= 0) info->has_range = 1; /* Get network ID */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWNWID, &wrq) >= 0) { info->has_nwid = 1; memcpy(&(info->nwid), &(wrq.u.nwid), sizeof(iwparam)); } /* Get frequency / channel */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWFREQ, &wrq) >= 0) { info->has_freq = 1; info->freq = freq2float(&(wrq.u.freq)); } /* Get sensitivity */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWSENS, &wrq) >= 0) { info->has_sens = 1; memcpy(&(info->sens), &(wrq.u.sens), sizeof(iwparam)); } /* Get encryption information */ strcpy(wrq.ifr_name, ifname); wrq.u.data.pointer = (caddr_t) info->key; wrq.u.data.length = 0; wrq.u.data.flags = 0; if(ioctl(skfd, SIOCGIWENCODE, &wrq) >= 0) { info->has_key = 1; info->key_size = wrq.u.data.length; info->key_flags = wrq.u.data.flags; } /* Get ESSID */ strcpy(wrq.ifr_name, ifname); wrq.u.essid.pointer = (caddr_t) info->essid; wrq.u.essid.length = 0; wrq.u.essid.flags = 0; if(ioctl(skfd, SIOCGIWESSID, &wrq) >= 0) { info->has_essid = 1; info->essid_on = wrq.u.data.flags; } /* Get AP address */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWAP, &wrq) >= 0) { info->has_ap_addr = 1; memcpy(&(info->ap_addr), &(wrq.u.ap_addr), sizeof (sockaddr)); } /* Get NickName */ strcpy(wrq.ifr_name, ifname); wrq.u.essid.pointer = (caddr_t) info->nickname; wrq.u.essid.length = 0; wrq.u.essid.flags = 0; if(ioctl(skfd, SIOCGIWNICKN, &wrq) >= 0) if(wrq.u.data.length > 1) info->has_nickname = 1; /* Get bit rate */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWRATE, &wrq) >= 0) { info->has_bitrate = 1; memcpy(&(info->bitrate), &(wrq.u.bitrate), sizeof(iwparam)); } /* Get RTS threshold */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWRTS, &wrq) >= 0) { info->has_rts = 1; memcpy(&(info->rts), &(wrq.u.rts), sizeof(iwparam)); } /* Get fragmentation threshold */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWFRAG, &wrq) >= 0) { info->has_frag = 1; memcpy(&(info->frag), &(wrq.u.frag), sizeof(iwparam)); } /* Get operation mode */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWMODE, &wrq) >= 0) { if((wrq.u.mode < 6) && (wrq.u.mode >= 0)) info->has_mode = 1; info->mode = wrq.u.mode; } /* Get Power Management settings */ strcpy(wrq.ifr_name, ifname); wrq.u.power.flags = 0; if(ioctl(skfd, SIOCGIWPOWER, &wrq) >= 0) { info->has_power = 1; memcpy(&(info->power), &(wrq.u.power), sizeof(iwparam)); }#if WIRELESS_EXT > 9 /* Get Transmit Power */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWTXPOW, &wrq) >= 0) { info->has_txpower = 1; memcpy(&(info->txpower), &(wrq.u.txpower), sizeof(iwparam)); }#endif#if WIRELESS_EXT > 10 /* Get retry limit/lifetime */ strcpy(wrq.ifr_name, ifname); if(ioctl(skfd, SIOCGIWRETRY, &wrq) >= 0) { info->has_retry = 1; memcpy(&(info->retry), &(wrq.u.retry), sizeof(iwparam)); }#endif /* WIRELESS_EXT > 10 */ /* Get stats */ if(iw_getstats(ifname, &(info->stats)) >= 0) { info->has_stats = 1; } return(0);}/*------------------------------------------------------------------*//* * Print on the screen in a neat fashion all the info we have collected * on a device. */static voiddisplay_info(struct wireless_info * info, char * ifname){ /* One token is more of less 5 character, 14 tokens per line */ int tokens = 3; /* For name */ /* Display device name and wireless name (name of the protocol used) */ printf("%-8.8s %s ", ifname, info->name); /* Display ESSID (extended network), if any */ if(info->has_essid) { if(info->essid_on) { /* Does it have an ESSID index ? */ if((info->essid_on & IW_ENCODE_INDEX) > 1) printf("ESSID:\"%s\" [%d] ", info->essid, (info->essid_on & IW_ENCODE_INDEX)); else printf("ESSID:\"%s\" ", info->essid); } else printf("ESSID:off "); } /* Display NickName (station name), if any */ if(info->has_nickname) printf("Nickname:\"%s\"", info->nickname); /* Formatting */ if(info->has_essid || info->has_nickname) { printf("\n "); tokens = 0; } /* Display Network ID */ if(info->has_nwid) { /* Note : should display right number of digit according to info * in range structure */ if(info->nwid.disabled) printf("NWID:off/any "); else printf("NWID:%X ", info->nwid.value); tokens +=2; } /* Display the current mode of operation */ if(info->has_mode) { printf("Mode:%s ", operation_mode[info->mode]); tokens +=3; } /* Display frequency / channel */ if(info->has_freq) { if(info->freq < KILO) printf("Channel:%g ", info->freq); else { if(info->freq >= GIGA) printf("Frequency:%gGHz ", info->freq / GIGA); else { if(info->freq >= MEGA) printf("Frequency:%gMHz ", info->freq / MEGA); else printf("Frequency:%gkHz ", info->freq / KILO); } } tokens +=4; } /* Display the address of the current Access Point */ if(info->has_ap_addr) { /* A bit of clever formatting */ if(tokens > 8) { printf("\n "); tokens = 0; } tokens +=6; /* Oups ! No Access Point in Ad-Hoc mode */ if((info->has_mode) && (info->mode == IW_MODE_ADHOC)) printf("Cell:"); else printf("Access Point:"); printf(" %s", pr_ether(info->ap_addr.sa_data)); } /* Display the currently used/set bit-rate */ if(info->has_bitrate) { /* A bit of clever formatting */ if(tokens > 11) { printf("\n "); tokens = 0; } tokens +=3; /* Fixed ? */ if(info->bitrate.fixed) printf("Bit Rate="); else printf("Bit Rate:"); if(info->bitrate.value >= GIGA) printf("%gGb/s", info->bitrate.value / GIGA); else if(info->bitrate.value >= MEGA) printf("%gMb/s", info->bitrate.value / MEGA); else printf("%gkb/s", info->bitrate.value / KILO); printf(" "); }#if WIRELESS_EXT > 9 /* Display the Transmit Power */ if(info->has_txpower) { /* A bit of clever formatting */ if(tokens > 11) { printf("\n "); tokens = 0; } tokens +=3; /* Disabled ? */ if(info->txpower.disabled) printf("Tx-Power:off "); else { int dbm; /* Fixed ? */ if(info->txpower.fixed) printf("Tx-Power="); else printf("Tx-Power:"); /* Convert everything to dBm */ if(info->txpower.flags & IW_TXPOW_MWATT) dbm = mwatt2dbm(info->txpower.value); else dbm = info->txpower.value; /* Display */ printf("%d dBm ", dbm); } }#endif /* Display sensitivity */ if(info->has_sens) { /* A bit of clever formatting */ if(tokens > 10) { printf("\n "); tokens = 0; } tokens +=4; /* Fixed ? */ if(info->sens.fixed) printf("Sensitivity="); else printf("Sensitivity:"); if(info->has_range) /* Display in dBm ? */ if(info->sens.value < 0) printf("%d dBm ", info->sens.value); else printf("%d/%d ", info->sens.value, info->range.sensitivity); else printf("%d ", info->sens.value); } printf("\n "); tokens = 0;#if WIRELESS_EXT > 10 /* Display retry limit/lifetime information */ if(info->has_retry) { printf("Retry"); /* Disabled ? */ if(info->retry.disabled) printf(":off"); else { /* Let's check the value and its type */ if(info->retry.flags & IW_RETRY_TYPE) print_retry_value(stdout, info->retry.value, info->retry.flags); /* Let's check if nothing (simply on) */ if(info->retry.flags == IW_RETRY_ON) printf(":on"); } printf(" "); tokens += 5; /* Between 3 and 5, depend on flags */ }#endif /* WIRELESS_EXT > 10 */ /* Display the RTS threshold */ if(info->has_rts) { /* Disabled ? */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -