static PyObject * wifi_get_ap(PyObject *self, PyObject *args) { const char *iface; int max_quality; int fd, err; struct iwreq wrq; char buffer[32]; if (!PyArg_ParseTuple(args, "s", &iface)) return NULL; fd = socket (PF_INET, SOCK_DGRAM, 0); if (fd < 0) { fprintf (stderr, "couldn't open socket\n"); return NULL; } /* Get AP address */ err = iw_get_ext(fd, iface, SIOCGIWAP, &wrq); close (fd); if (err < 0) { PyErr_SetFromErrno(PyExc_IOError); return NULL; } return Py_BuildValue("s", iw_sawap_ntop(&wrq.u.ap_addr, buffer)); }
static const char * get_ap(void *arg) { if (iw_get_ext(skfd, arg, SIOCGIWAP, &wrq) < 0) { xsg_debug("get_ap: UNKNOWN"); return NULL; } iw_sawap_ntop(&(wrq.u.ap_addr), buffer); xsg_debug("get_ap: \"%s\"", buffer); return buffer; }
/* * Print on the screen in a neat fashion all the info we have collected * on a device. */ static void display_info(struct wireless_info * info, char * ifname) { char buffer[128]; /* Temporary buffer */ /* One token is more of less 5 characters, 14 tokens per line */ int tokens = 3; /* For name */ /* Display device name and wireless name (name of the protocol used) */ printf("%-8.16s %s ", ifname, info->b.name); /* Display ESSID (extended network), if any */ if(info->b.has_essid) { if(info->b.essid_on) { /* Does it have an ESSID index ? */ if((info->b.essid_on & IW_ENCODE_INDEX) > 1) printf("ESSID:\"%s\" [%d] ", info->b.essid, (info->b.essid_on & IW_ENCODE_INDEX)); else printf("ESSID:\"%s\" ", info->b.essid); } else printf("ESSID:off/any "); } #ifndef WE_ESSENTIAL /* Display NickName (station name), if any */ if(info->has_nickname) printf("Nickname:\"%s\"", info->nickname); #endif /* WE_ESSENTIAL */ /* Formatting */ if(info->b.has_essid || info->has_nickname) { printf("\n "); tokens = 0; } #ifndef WE_ESSENTIAL /* Display Network ID */ if(info->b.has_nwid) { /* Note : should display proper number of digits according to info * in range structure */ if(info->b.nwid.disabled) printf("NWID:off/any "); else printf("NWID:%X ", info->b.nwid.value); tokens +=2; } #endif /* WE_ESSENTIAL */ /* Display the current mode of operation */ if(info->b.has_mode) { printf("Mode:%s ", iw_operation_mode[info->b.mode]); tokens +=3; } /* Display frequency / channel */ if(info->b.has_freq) { double freq = info->b.freq; /* Frequency/channel */ int channel = -1; /* Converted to channel */ /* Some drivers insist of returning channel instead of frequency. * This fixes them up. Note that, driver should still return * frequency, because other tools depend on it. */ if(info->has_range && (freq < KILO)) channel = iw_channel_to_freq((int) freq, &freq, &info->range); /* Display */ iw_print_freq(buffer, sizeof(buffer), freq, -1, info->b.freq_flags); printf("%s ", buffer); 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->b.has_mode) && (info->b.mode == IW_MODE_ADHOC)) printf("Cell:"); else printf("Access Point:"); printf(" %s ", iw_sawap_ntop(&info->ap_addr, buffer)); } /* 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; /* Display it */ iw_print_bitrate(buffer, sizeof(buffer), info->bitrate.value); printf("Bit Rate%c%s ", (info->bitrate.fixed ? '=' : ':'), buffer); } #ifndef WE_ESSENTIAL /* Display the Transmit Power */ if(info->has_txpower) { /* A bit of clever formatting */ if(tokens > 11) { printf("\n "); tokens = 0; } tokens +=3; /* Display it */ iw_print_txpower(buffer, sizeof(buffer), &info->txpower); printf("Tx-Power%c%s ", (info->txpower.fixed ? '=' : ':'), buffer); } /* Display sensitivity */ if(info->has_sens) { /* A bit of clever formatting */ if(tokens > 10) { printf("\n "); tokens = 0; } tokens +=4; /* Fixed ? */ printf("Sensitivity%c", info->sens.fixed ? '=' : ':'); 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); } #endif /* WE_ESSENTIAL */ printf("\n "); tokens = 0; #ifndef WE_ESSENTIAL /* 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) { iw_print_retry_value(buffer, sizeof(buffer), info->retry.value, info->retry.flags, info->range.we_version_compiled); printf("%s", buffer); } /* 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 */ } /* Display the RTS threshold */ if(info->has_rts) { /* Disabled ? */ if(info->rts.disabled) printf("RTS thr:off "); else { /* Fixed ? */ printf("RTS thr%c%d B ", info->rts.fixed ? '=' : ':', info->rts.value); } tokens += 3; } /* Display the fragmentation threshold */ if(info->has_frag) { /* A bit of clever formatting */ if(tokens > 10) { printf("\n "); tokens = 0; } tokens +=4; /* Disabled ? */ if(info->frag.disabled) printf("Fragment thr:off"); else { /* Fixed ? */ printf("Fragment thr%c%d B ", info->frag.fixed ? '=' : ':', info->frag.value); } } /* Formating */ if(tokens > 0) printf("\n "); #endif /* WE_ESSENTIAL */ /* Display encryption information */ /* Note : we display only the "current" key, use iwlist to list all keys */ if(info->b.has_key) { printf("Encryption key:"); if((info->b.key_flags & IW_ENCODE_DISABLED) || (info->b.key_size == 0)) printf("off"); else { /* Display the key */ iw_print_key(buffer, sizeof(buffer), info->b.key, info->b.key_size, info->b.key_flags); printf("%s", buffer); /* Other info... */ if((info->b.key_flags & IW_ENCODE_INDEX) > 1) printf(" [%d]", info->b.key_flags & IW_ENCODE_INDEX); if(info->b.key_flags & IW_ENCODE_RESTRICTED) printf(" Security mode:restricted"); if(info->b.key_flags & IW_ENCODE_OPEN) printf(" Security mode:open"); } printf("\n "); } /* Display Power Management information */ /* Note : we display only one parameter, period or timeout. If a device * (such as HiperLan) has both, the user need to use iwlist... */ if(info->has_power) /* I hope the device has power ;-) */ { printf("Power Management"); /* Disabled ? */ if(info->power.disabled) printf(":off"); else { /* Let's check the value and its type */ if(info->power.flags & IW_POWER_TYPE) { iw_print_pm_value(buffer, sizeof(buffer), info->power.value, info->power.flags, info->range.we_version_compiled); printf("%s ", buffer); } /* Let's check the mode */ iw_print_pm_mode(buffer, sizeof(buffer), info->power.flags); printf("%s", buffer); /* Let's check if nothing (simply on) */ if(info->power.flags == IW_POWER_ON) printf(":on"); } printf("\n "); } /* Display statistics */ if(info->has_stats) { iw_print_stats(buffer, sizeof(buffer), &info->stats.qual, &info->range, info->has_range); printf("Link %s\n", buffer); if(info->range.we_version_compiled > 11) printf(" Rx invalid nwid:%d Rx invalid crypt:%d Rx invalid frag:%d\n Tx excessive retries:%d Invalid misc:%d Missed beacon:%d\n", info->stats.discard.nwid, info->stats.discard.code, info->stats.discard.fragment, info->stats.discard.retries, info->stats.discard.misc, info->stats.miss.beacon); else printf(" Rx invalid nwid:%d invalid crypt:%d invalid misc:%d\n", info->stats.discard.nwid, info->stats.discard.code, info->stats.discard.misc); } printf("\n"); }
ap_info * wireless_parse_scanning_event(struct iw_event *event, ap_info *oldinfo) { ap_info *info; /* found a new AP */ if (event->cmd==SIOCGIWAP) { char buf[128]; info = g_new0(ap_info, 1); info->apaddr = g_strdup(iw_sawap_ntop(&event->u.ap_addr, buf)); info->en_method = NS_WIRELESS_AUTH_OFF; info->haskey = FALSE; info->key_mgmt = NS_IW_IE_KEY_MGMT_NONE; info->group = NS_IW_IE_CIPHER_TKIP; info->pairwise = NS_IW_IE_CIPHER_TKIP; } else { info = oldinfo; } switch (event->cmd) { case SIOCGIWESSID: /* ESSID */ if (!event->u.essid.flags || event->u.essid.length==0 || strlen(event->u.essid.pointer)==0) { info->essid = NULL; } else { info->essid = g_strndup(event->u.essid.pointer, event->u.essid.length); } break; case IWEVQUAL: /* Signal Quality */ info->quality = (int)rint((log (event->u.qual.qual) / log (92)) * 100.0); break; case SIOCGIWENCODE: /* Encryption */ if (!event->u.data.pointer) event->u.data.flags |= IW_ENCODE_NOKEY; if (!(event->u.data.flags & IW_ENCODE_DISABLED)) { info->haskey = TRUE; /* assume WEP */ info->en_method = NS_WIRELESS_AUTH_WEP; } else { info->haskey = FALSE; info->en_method = NS_WIRELESS_AUTH_OFF; } break; case IWEVGENIE: /* Extra information */ { int offset = 0; int ielen = event->u.data.length; unsigned char *iebuf = event->u.data.pointer; while(offset <= (ielen - 2)) { /* check IE type */ switch(iebuf[offset]) { case 0xdd: /* WPA or else */ case 0x30: /* IEEE 802.11i/WPA2 */ wireless_gen_ie(info, iebuf, ielen); break; } offset += iebuf[offset+1] + 2; } } break; } return info; }
/* * Print one element from the scanning results */ static inline int print_event_token(struct iw_event * event, /* Extracted token */ struct iw_range * iw_range, /* Range info */ int has_range) { char buffer[128]; /* Temporary buffer */ char buffer2[30]; /* Temporary buffer */ char * prefix = (IW_IS_GET(event->cmd) ? "New" : "Set"); /* Now, let's decode the event */ switch(event->cmd) { /* ----- set events ----- */ /* Events that result from a "SET XXX" operation by the user */ case SIOCSIWNWID: if(event->u.nwid.disabled) printf("Set NWID:off/any\n"); else printf("Set NWID:%X\n", event->u.nwid.value); break; case SIOCSIWFREQ: case SIOCGIWFREQ: { double freq; /* Frequency/channel */ int channel = -1; /* Converted to channel */ freq = iw_freq2float(&(event->u.freq)); if(has_range) { if(freq < KILO) /* Convert channel to frequency if possible */ channel = iw_channel_to_freq((int) freq, &freq, iw_range); else /* Convert frequency to channel if possible */ channel = iw_freq_to_channel(freq, iw_range); } iw_print_freq(buffer, sizeof(buffer), freq, channel, event->u.freq.flags); printf("%s %s\n", prefix, buffer); } break; case SIOCSIWMODE: printf("Set Mode:%s\n", iw_operation_mode[event->u.mode]); break; case SIOCSIWESSID: case SIOCGIWESSID: { char essid[IW_ESSID_MAX_SIZE+1]; memset(essid, '\0', sizeof(essid)); if((event->u.essid.pointer) && (event->u.essid.length)) memcpy(essid, event->u.essid.pointer, event->u.essid.length); if(event->u.essid.flags) { /* Does it have an ESSID index ? */ if((event->u.essid.flags & IW_ENCODE_INDEX) > 1) printf("%s ESSID:\"%s\" [%d]\n", prefix, essid, (event->u.essid.flags & IW_ENCODE_INDEX)); else printf("%s ESSID:\"%s\"\n", prefix, essid); } else printf("%s ESSID:off/any\n", prefix); } break; case SIOCSIWENCODE: { unsigned char key[IW_ENCODING_TOKEN_MAX]; if(event->u.data.pointer) memcpy(key, event->u.data.pointer, event->u.data.length); else event->u.data.flags |= IW_ENCODE_NOKEY; printf("Set Encryption key:"); if(event->u.data.flags & IW_ENCODE_DISABLED) printf("off\n"); else { /* Display the key */ iw_print_key(buffer, sizeof(buffer), key, event->u.data.length, event->u.data.flags); printf("%s", buffer); /* Other info... */ if((event->u.data.flags & IW_ENCODE_INDEX) > 1) printf(" [%d]", event->u.data.flags & IW_ENCODE_INDEX); if(event->u.data.flags & IW_ENCODE_RESTRICTED) printf(" Security mode:restricted"); if(event->u.data.flags & IW_ENCODE_OPEN) printf(" Security mode:open"); printf("\n"); } } break; /* ----- driver events ----- */ /* Events generated by the driver when something important happens */ case SIOCGIWAP: printf("New Access Point/Cell address:%s\n", iw_sawap_ntop(&event->u.ap_addr, buffer)); break; case SIOCGIWSCAN: printf("Scan request completed\n"); break; case IWEVTXDROP: printf("Tx packet dropped:%s\n", iw_saether_ntop(&event->u.addr, buffer)); break; case IWEVCUSTOM: { char custom[IW_CUSTOM_MAX+1]; memset(custom, '\0', sizeof(custom)); if((event->u.data.pointer) && (event->u.data.length)) memcpy(custom, event->u.data.pointer, event->u.data.length); printf("Custom driver event:%s\n", custom); } break; case IWEVREGISTERED: printf("Registered node:%s\n", iw_saether_ntop(&event->u.addr, buffer)); break; case IWEVEXPIRED: printf("Expired node:%s\n", iw_saether_ntop(&event->u.addr, buffer)); break; case SIOCGIWTHRSPY: { struct iw_thrspy threshold; if((event->u.data.pointer) && (event->u.data.length)) { memcpy(&threshold, event->u.data.pointer, sizeof(struct iw_thrspy)); printf("Spy threshold crossed on address:%s\n", iw_saether_ntop(&threshold.addr, buffer)); iw_print_stats(buffer, sizeof(buffer), &threshold.qual, iw_range, has_range); printf(" Link %s\n", buffer); } else printf("Invalid Spy Threshold event\n"); } break; /* ----- driver WPA events ----- */ /* Events generated by the driver, used for WPA operation */ case IWEVMICHAELMICFAILURE: if(event->u.data.length >= sizeof(struct iw_michaelmicfailure)) { struct iw_michaelmicfailure mf; memcpy(&mf, event->u.data.pointer, sizeof(mf)); printf("Michael MIC failure flags:0x%X src_addr:%s tsc:%s\n", mf.flags, iw_saether_ntop(&mf.src_addr, buffer2), iw_hexdump(buffer, sizeof(buffer), mf.tsc, IW_ENCODE_SEQ_MAX_SIZE)); } break; case IWEVASSOCREQIE: printf("Association Request IEs:%s\n", iw_hexdump(buffer, sizeof(buffer), event->u.data.pointer, event->u.data.length)); break; case IWEVASSOCRESPIE: printf("Association Response IEs:%s\n", iw_hexdump(buffer, sizeof(buffer), event->u.data.pointer, event->u.data.length)); break; case IWEVPMKIDCAND: if(event->u.data.length >= sizeof(struct iw_pmkid_cand)) { struct iw_pmkid_cand cand; memcpy(&cand, event->u.data.pointer, sizeof(cand)); printf("PMKID candidate flags:0x%X index:%d bssid:%s\n", cand.flags, cand.index, iw_saether_ntop(&cand.bssid, buffer)); } break; /* ----- junk ----- */ /* other junk not currently in use */ case SIOCGIWRATE: iw_print_bitrate(buffer, sizeof(buffer), event->u.bitrate.value); printf("New Bit Rate:%s\n", buffer); break; case SIOCGIWNAME: printf("Protocol:%-1.16s\n", event->u.name); break; case IWEVQUAL: { event->u.qual.updated = 0x0; /* Not that reliable, disable */ iw_print_stats(buffer, sizeof(buffer), &event->u.qual, iw_range, has_range); printf("Link %s\n", buffer); break; } default: printf("(Unknown Wireless event 0x%04X)\n", event->cmd); } /* switch(event->cmd) */ return(0); }