C_RESULT vp_com_wf_disconnect(vp_com_wifi_config_t* config, vp_com_wifi_connection_t* connection) { C_RESULT res = VP_COM_OK; #ifdef USE_IWLIB struct iwreq wrq; char essid[IW_ESSID_MAX_SIZE + 1]; int wlsock = iw_sockets_open(); vp_os_memset(&wrq, 0, sizeof(struct iwreq)); strncpy(essid, connection->networkName, strlen(connection->networkName)); wrq.u.essid.flags = 0; wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = strlen(essid); if(iw_get_kernel_we_version() < 21) wrq.u.essid.length++; // Get version from kernel, device may not have range... res = ( iw_set_ext( wlsock, config->itfName, SIOCSIWESSID, &wrq) < 0 ) ? VP_COM_ERROR : VP_COM_OK; iw_sockets_close(wlsock); #endif return res; }
/* * Respond to a single RTM_NEWLINK event from the rtnetlink socket. */ static inline int index2name(int index, char *name) { int skfd = -1; /* generic raw socket desc. */ struct ifreq irq; int ret = 0; memset(name, 0, IFNAMSIZ + 1); /* Create a channel to the NET kernel. */ if((skfd = iw_sockets_open()) < 0) { perror("socket"); exit(-1); } /* Get interface name */ irq.ifr_ifindex = index; if(ioctl(skfd, SIOCGIFNAME, &irq) < 0) ret = -1; else strncpy(name, irq.ifr_name, IFNAMSIZ); close(skfd); return(ret); }
void open_sockets (void) { #ifdef WIRELESS wfd = iw_sockets_open(); #endif skfd = socket (AF_INET, SOCK_DGRAM, 0); }
int change_channel(int channel) { int skfd = 0, ret_val = 0; struct iwreq wrq; memset((void *) &wrq, 0, sizeof(struct iwreq)); /* Open NET socket */ if((skfd = iw_sockets_open()) < 0) { perror("iw_sockets_open"); } else if(get_iface()) { /* Convert channel to a frequency */ iw_float2freq((double) channel, &(wrq.u.freq)); /* Fixed frequency */ wrq.u.freq.flags = IW_FREQ_FIXED; cprintf(VERBOSE, "[+] Switching %s to channel %d\n", get_iface(), channel); /* Set frequency */ if(iw_set_ext(skfd, get_iface(), SIOCSIWFREQ, &wrq) >= 0) { set_channel(channel); ret_val = 1; } iw_sockets_close(skfd); } return ret_val; }
int main(int argc, char *argv[]) { int skfd; if ((skfd = iw_sockets_open()) < 0) { printf("(%s:%d) iw_sockets_open() fail.\n", __FUNCTION__, __LINE__); return -1; } printf("====== skfd = %d ======\n", skfd); // if(is_wifi_direct) { // ret = set_private(skfd, (char *)wlan->wlan_p2p_name, "stat", (void *)&data, arg_count, NULL); // } else { // ret = set_private(skfd, (char *)wlan->wlan_name, "stat", (void *)&data, arg_count, NULL); // } // if (ret == -1) { // fprintf(stderr, "wrong stat ioctl command\n"); // close(skfd); // return -1; // } // close(skfd); return 0; }
/* * The main ! */ int main(int argc, char ** argv) { int skfd; /* generic raw socket desc. */ int goterr = 0; /* Create a channel to the NET kernel. */ if((skfd = iw_sockets_open()) < 0) { perror("socket"); return(-1); } /* No argument : show the list of all device + info */ if(argc == 1) iw_enum_devices(skfd, &print_priv_info, NULL, 0); else /* Special cases take one... */ /* All */ if((!strncmp(argv[1], "-a", 2)) || (!strcmp(argv[1], "--all"))) iw_enum_devices(skfd, &print_priv_all, NULL, 0); else /* Help */ if((!strncmp(argv[1], "-h", 2)) || (!strcmp(argv[1], "--help"))) iw_usage(); else /* Version */ if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) goterr = iw_print_version_info("iwpriv"); else /* The device name must be the first argument */ /* Name only : show for that device only */ if(argc == 2) print_priv_info(skfd, argv[1], NULL, 0); else /* Special cases take two... */ /* All */ if((!strncmp(argv[2], "-a", 2)) || (!strcmp(argv[2], "--all"))) print_priv_all(skfd, argv[1], NULL, 0); else /* Roaming */ if(!strncmp(argv[2], "roam", 4)) goterr = set_roaming(skfd, argv + 3, argc - 3, argv[1]); else /* Port type */ if(!strncmp(argv[2], "port", 4)) goterr = port_type(skfd, argv + 3, argc - 3, argv[1]); else /*-------------*/ /* Otherwise, it's a private ioctl */ goterr = set_private(skfd, argv + 2, argc - 2, argv[1]); /* Close the socket. */ close(skfd); return(goterr); }
/* * Get interface data from cache or live interface */ static struct wireless_iface * iw_get_interface_data(int ifindex) { struct wireless_iface * curr; int skfd = -1; /* ioctl socket */ /* Search for it in the database */ curr = interface_cache; while(curr != NULL) { /* Match ? */ if(curr->ifindex == ifindex) { //printf("Cache : found %d-%s\n", curr->ifindex, curr->ifname); /* Return */ return(curr); } /* Next entry */ curr = curr->next; } /* Create a channel to the NET kernel. Doesn't happen too often, so * socket creation overhead is minimal... */ if((skfd = iw_sockets_open()) < 0) { perror("iw_sockets_open"); return(NULL); } /* Create new entry, zero, init */ curr = calloc(1, sizeof(struct wireless_iface)); if(!curr) { fprintf(stderr, "Malloc failed\n"); return(NULL); } curr->ifindex = ifindex; /* Extract static data */ if(index2name(skfd, ifindex, curr->ifname) < 0) { perror("index2name"); free(curr); return(NULL); } curr->has_range = (iw_get_range_info(skfd, curr->ifname, &curr->range) >= 0); //printf("Cache : create %d-%s\n", curr->ifindex, curr->ifname); /* Done */ iw_sockets_close(skfd); /* Link it */ curr->next = interface_cache; interface_cache = curr; return(curr); }
static GtkWidget * wireless_applet_new (WirelessApplet *applet) { panel_applet_set_flags (PANEL_APPLET (applet), PANEL_APPLET_EXPAND_MINOR); applet->skfd = iw_sockets_open (); /* this ensures that properties are loaded */ wireless_applet_load_properties (applet); wireless_applet_load_theme (applet); setup_widgets (applet); applet->tips = gtk_tooltips_new (); g_object_ref (applet->tips); gtk_object_sink (GTK_OBJECT (applet->tips)); applet->prefs = NULL; g_signal_connect (applet,"destroy", G_CALLBACK (wireless_applet_destroy),NULL); g_signal_connect (applet->button, "button_press_event", G_CALLBACK (do_not_eat_button_press), NULL); /* Setup the menus */ panel_applet_setup_menu_from_file (PANEL_APPLET (applet), NULL, "GNOME_WirelessApplet.xml", NULL, wireless_menu_verbs, applet); if (panel_applet_get_locked_down (PANEL_APPLET (applet))) { BonoboUIComponent *popup_component; popup_component = panel_applet_get_popup_component (PANEL_APPLET (applet)); bonobo_ui_component_set_prop (popup_component, "/commands/WirelessProperties", "hidden", "1", NULL); } check_wireless (applet); wireless_applet_timeout_handler (applet); wireless_applet_start_timeout (applet); g_signal_connect (G_OBJECT (applet), "change_size", G_CALLBACK (change_size_cb), applet); g_signal_connect (G_OBJECT (applet), "change_orient", G_CALLBACK (change_orient_cb), applet); return GTK_WIDGET (applet); }
C_RESULT vp_com_wf_get_rssi(vp_com_wifi_config_t* cfg, int32_t* rssi) { #ifdef USE_IWLIB struct iwreq wrq; iwstats stats; iwrange range; int wlsock = iw_sockets_open(); vp_os_memset(&wrq, 0, sizeof(struct iwreq)); iw_get_stats(wlsock, cfg->itfName, &stats, &range, 1); iw_sockets_close(wlsock); // struct iw_statistics // { // __u16 status; // Status * - device dependent for now // struct iw_quality qual; // Quality of the link * (instant/mean/max) // struct iw_discarded discard; // Packet discarded counts // struct iw_missed miss; // Packet missed counts // }; // // struct iw_range // { // ... // // Quality of link & SNR stuff */ // // Quality range (link, level, noise) // // If the quality is absolute, it will be in the range [0 ; max_qual], // // if the quality is dBm, it will be in the range [max_qual ; 0]. // // Don't forget that we use 8 bit arithmetics... // struct iw_quality max_qual; // Quality of the link // // This should contain the average/typical values of the quality // // indicator. This should be the threshold between a "good" and // // a "bad" link (example : monitor going from green to orange). // // Currently, user space apps like quality monitors don't have any // // way to calibrate the measurement. With this, they can split // // the range between 0 and max_qual in different quality level // // (using a geometric subdivision centered on the average). // // I expect that people doing the user space apps will feedback // // us on which value we need to put in each driver... // struct iw_quality avg_qual; // Quality of the link // ... // }; // struct iw_quality // { // __u8 qual; // link quality (%retries, SNR, %missed beacons or better...) // __u8 level; // signal level (dBm) // __u8 noise; // noise level (dBm) // __u8 updated; // Flags to know if updated // }; *rssi = stats.qual.qual; #endif return VP_COM_OK; }
int connect_wifi() { struct iwreq wrq; char essid[IW_ESSID_MAX_SIZE + 1]; unsigned char key[IW_ENCODING_TOKEN_MAX]; int32_t keylen = 0; int wlsock = iw_sockets_open(); const char *itfName = "wlan0"; const char *networkName = "linksys"; const char *com_key = "9F1C3EE11CBA230B27BF1C1B6F"; if(wlsock < 0) return -1; memset(&wrq,0,sizeof(struct iwreq)); keylen = iw_in_key_full(wlsock, itfName, com_key, key, &wrq.u.data.flags); if(keylen <= 0) return -1; wrq.u.data.length = keylen; wrq.u.data.pointer = (caddr_t) key; wrq.u.data.flags |= IW_ENCODE_RESTRICTED; if(iw_set_ext(wlsock, itfName, SIOCSIWENCODE, &wrq) < 0) return -1; memset(&wrq,0,sizeof(struct iwreq)); wrq.u.mode = IW_MODE_INFRA; if(iw_set_ext( wlsock, itfName, SIOCSIWMODE, &wrq) < 0) return -1; if(strlen(networkName) > IW_ESSID_MAX_SIZE) return -1; memset(essid,0,IW_ESSID_MAX_SIZE + 1); memset(&wrq,0,sizeof(struct iwreq)); strncpy(essid,networkName,strlen(networkName)); wrq.u.essid.flags = 1; wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = strlen(essid) + 1; if(iw_set_ext( wlsock, itfName, SIOCSIWESSID, &wrq) < 0) return -1; iw_sockets_close(wlsock); return 0; }
int main(int argc, char *argv[]) { std::vector<std::string> results = jw_enumate_interfaces(PROC_NET_DEV); for(unsigned int i=0; i < results.size(); i++) { std::string name = results[i]; printf("%s\n",name.c_str()); } int skfd = iw_sockets_open(); float freq = jw_get_frequency(skfd,"ra0"); printf("%f",freq); return 0; }
bool WifiStumbler::initialize(std::string wlan_if) { wlan_if_ = wlan_if; wlan_sock_ = iw_sockets_open(); if(wlan_sock_<0) { ROS_ERROR("Failed to open wlan socket on %s", wlan_if_.c_str()); return false; } return true; }
static void init_iw(void) { if (skfd < 0) { skfd = iw_sockets_open(); } if (skfd < 0) { xsg_warning("iw_socket_open() failed"); } else { xsg_set_cloexec_flag(skfd, TRUE); } }
int get_essid(char *name, int *channel) { int sockfd; char essid[16]; struct iwreq wrq; int ioc; sockfd = iw_sockets_open(); double freq; struct iw_range *range; char buffer[sizeof(iwrange) * 2]; memset(essid, '\0', sizeof(essid)); /* After IOCTL call the result will be stored in iwreq * structure. Hence prepare the structure to hold the * results. */ wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = IW_ESSID_MAX_SIZE + 2; wrq.u.essid.flags = 0; ioc = get_param(sockfd, SIOCGIWESSID, ifname_config, &wrq); strcpy(name, wrq.u.essid.pointer); /* Obtain the essid pointed by the pointer */ /* Prepare the structure to get wireless range info */ bzero(buffer, sizeof(buffer)); wrq.u.data.pointer = (caddr_t) buffer; wrq.u.data.length = sizeof(buffer); wrq.u.data.flags = 0; if (get_param(sockfd, SIOCGIWRANGE, ifname_config, &wrq) < 0) { syslog(LOG_ERR, "ERROR:%s: %s\n", ifname_config, strerror(errno)); return -1; } /* Obtain the frequency the current network is operating in */ range = (struct iw_range *)buffer; ioc = get_param(sockfd, SIOCGIWFREQ, ifname_config, &wrq); if (ioc == -1) { syslog(LOG_ERR, "ERROR:%s: %s\n", ifname_config, strerror(errno)); } /* Convert the frequency into channel no */ freq = iw_freq2float(&(wrq.u.freq)); *channel = iw_freq_to_channel(freq, range); iw_sockets_close(sockfd); return 1; }
static int iwscan_ifname_hasiwname(char* ifname) { int ret = 0; int skfd; iwreq wrq; if ((skfd = iw_sockets_open()) >= 0) { if (iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) >= 0) ret++; iw_sockets_close(skfd); } return ret; }
bool CWextHW::open() { if (m_wextFd > 0 ) return true; //Get file Descriptor to NET kernel if ( (m_wextFd = iw_sockets_open()) < 0) { m_wextFd = -1; qWarning("%s", qPrintable(tr("ERROR: Could not open socket to net kernel"))); return false; } else { //Socket is set up, now set SocketNotifier qDebug("File Descriptor for Wext is: %i", m_wextFd); //Start timer for reading wireless info (like in /proc/net/wireless) if (m_wextFd != -1) readSignalQuality(); m_sqTimer.start(m_sqPollrate, this); return true; } }
/* * The main ! */ int main(int argc, char ** argv) { int skfd; /* generic raw socket desc. */ int goterr = 0; /* Create a channel to the NET kernel. */ if((skfd = iw_sockets_open()) < 0) { perror("socket"); exit(-1); } /* No argument : show the list of all device + info */ if(argc == 1) iw_enum_devices(skfd, &print_info, NULL, 0); else /* Special case for help... */ if((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help"))) iw_usage(); else /* Special case for version... */ if(!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) goterr = iw_print_version_info("iwconfig"); else { /* '--' escape device name */ if((argc > 2) && !strcmp(argv[1], "--")) { argv++; argc--; } /* The device name must be the first argument */ if(argc == 2) print_info(skfd, argv[1], NULL, 0); else /* The other args on the line specify options to be set... */ goterr = set_info(skfd, argv + 2, argc - 2, argv[1]); } /* Close the socket. */ iw_sockets_close(skfd); return(goterr); }
/* * The main ! */ int main(int argc, char ** argv) { int skfd; /* generic raw socket desc. */ int goterr = 0; /* Create a channel to the NET kernel. */ if((skfd = iw_sockets_open()) < 0) { perror("socket"); return(-1); } /* No argument : show the list of all device + info */ if(argc == 1) iw_enum_devices(skfd, &print_spy_info, NULL, 0); else /* Special cases take one... */ /* Help */ if((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help"))) fprintf(stderr, "Usage: iwspy interface [+] [MAC address] [IP address]\n"); else /* Version */ if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) goterr = iw_print_version_info("iwspy"); else /* The device name must be the first argument */ /* Name only : show spy list for that device only */ if(argc == 2) goterr = print_spy_info(skfd, argv[1], NULL, 0); else /* Special commands */ if(!strcmp(argv[2], "setthr")) goterr = set_spy_threshold(skfd, argv[1], argv + 3, argc - 3); else if(!strcmp(argv[2], "getthr")) goterr = get_spy_threshold(skfd, argv[1], argv + 3, argc - 3); else /* Otherwise, it's a list of address to set in the spy list */ goterr = set_spy_info(skfd, argv[1], argv + 2, argc - 2); /* Close the socket. */ iw_sockets_close(skfd); return(goterr); }
void get_wireless_info (DevInfo *devinfo) { int fd; int newqual; wireless_info info = {0}; fd = iw_sockets_open (); if (fd < 0) return; if (iw_get_basic_config (fd, devinfo->name, &info.b) < 0) goto out; if (info.b.has_essid) { if ((!devinfo->essid) || (strcmp (devinfo->essid, info.b.essid) != 0)) { devinfo->essid = g_strdup (info.b.essid); } } else { devinfo->essid = NULL; } if (iw_get_stats (fd, devinfo->name, &info.stats, &info.range, info.has_range) >= 0) info.has_stats = 1; if (info.has_stats) { if ((iw_get_range_info(fd, devinfo->name, &info.range) >= 0) && (info.range.max_qual.qual > 0)) { newqual = 0.5f + (100.0f * info.stats.qual.qual) / (1.0f * info.range.max_qual.qual); } else { newqual = info.stats.qual.qual; } newqual = CLAMP(newqual, 0, 100); if (devinfo->qual != newqual) devinfo->qual = newqual; } else { devinfo->qual = 0; } goto out; out: if (fd != -1) close (fd); }
/* Accepts one parameter and check whether there is a wireless interface having * corresponding ESSID */ bool essid(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "essid: requires exactly one parameter\n"); exit(EXIT_FAILURE); } bool result = 0; char *args[2] = { argv[1], (char*) &result }; int skfd = iw_sockets_open(); int check_iface(int skfd, char *ifname, char *args[], int count) { (void) count; wireless_config info; iw_get_basic_config(skfd, ifname, &info); *args[1] = *args[1] || !strcmp(args[0], info.essid); return 0; }
int main(void) { Display *dpy; Window root; char status[201], net[30], vol[14], cpu[14], mem[14], bat[48]; int netloops = 60; dpy = XOpenDisplay(NULL); if(dpy == NULL) { fprintf(stderr, "ERROR: could not open display\n"); return 1; } root = XRootWindow(dpy, DefaultScreen(dpy)); winfo = malloc(sizeof(struct wireless_info)); memset(winfo, 0, sizeof(struct wireless_info)); skfd = iw_sockets_open(); infile = fopen(CPU_F, "r"); fscanf(infile, "cpu %ld %ld %ld %ld", &jif1, &jif2, &jif3, &jif4); fclose(infile); while(1) { if(++netloops > 60) { netloops = 0; get_wifi(net); } get_vol(vol); get_cpu(cpu); get_mem(mem); get_bat(bat); sprintf(status, "%s %s %s %s %s", net, vol, cpu, mem, bat); XStoreName(dpy, root, status); XFlush(dpy); sleep(INTERVAL); } /* NEXT LINES SHOULD NEVER EXECUTE! */ XCloseDisplay(dpy); iw_sockets_close(skfd); return 0; }
/* ---------------------------------------------------------------------- WirelessInterface_init */ static int WirelessInterface_init(wiface* self, PyObject* args, PyObject* kargs) { const char* ifname; const size_t ifnamesize; memset(&(self->info), 0, sizeof(wireless_info)); self->ifname = NULL; self->sock = 0; if (PyArg_ParseTuple(args, "s#", &ifname, &ifnamesize)) { self->sock = iw_sockets_open(); if (self->sock != -1) { ifreq frq; self->ifname = malloc(ifnamesize + 1); strncpy(self->ifname, ifname, ifnamesize + 1); strncpy(frq.ifr_name, self->ifname, IFNAMSIZ); if (!ioctl(self->sock, SIOCGIFFLAGS, &frq)) { frq.ifr_flags |= IFF_UP | IFF_RUNNING; ioctl(self->sock, SIOCSIFFLAGS, &frq); WirelessInterface_refresh(self); return 0; } else PyErr_SetString(IwScanError, "Failed to find device"); } else PyErr_SetString(IwScanError, "Failed to connect to libiw"); } return -1; }
C_RESULT vp_com_wf_connect(vp_com_t* vp_com, vp_com_wifi_connection_t* connection, int32_t numAttempts) { C_RESULT res = VP_COM_OK; #ifdef USE_IWLIB int32_t wlsock; vp_com_wifi_config_t* config = (vp_com_wifi_config_t*)vp_com->config; wireless_config iwconf; wlsock = iw_sockets_open(); res = ( wlsock < 0 ) ? VP_COM_ERROR : VP_COM_OK; VP_COM_CHECK( res ); iw_get_basic_config( wlsock, config->itfName, &iwconf ); iwconf.has_nwid = 0; iwconf.has_freq = 0; iwconf.has_key = 0; iwconf.has_mode = 1; iwconf.mode = config->infrastructure ? IW_MODE_INFRA : IW_MODE_ADHOC; iwconf.has_essid = 1; iwconf.essid_on = 1; strncpy( &iwconf.essid[0], connection->networkName, IW_ESSID_MAX_SIZE+1 ); res = iw_set_basic_config( wlsock, config->itfName, &iwconf ) < 0 ? C_FAIL : C_OK; if( SUCCEED(res) ) PRINT(" OK!\n"); else PRINT(" FAILED!\n"); iw_sockets_close(wlsock); #endif return res; }
/** * @brief Fills in basic information like the essid and the maximum signal quality */ bool openDevice() { if ((wifi_skfd = iw_sockets_open()) < 0) { ROS_ERROR("socket error"); return false; } if (iw_get_basic_config(wifi_skfd, wifi_dev, &(wifi_info->b)) < 0) { /* If no wireless name : no wireless extensions */ /* But let's check if the interface exists at all */ struct ifreq ifr; strncpy(ifr.ifr_name, wifi_dev, IFNAMSIZ); int ret = ioctl(wifi_skfd, SIOCGIFFLAGS, &ifr); if (ret < 0) { ROS_ERROR("device doesn't exist"); return false; } else { ROS_ERROR("operation not supported"); return false; } } wifi_sig.essid = wifi_info->b.essid; if (iw_get_range_info(wifi_skfd, wifi_dev, &(wifi_info->range)) >= 0) wifi_info->has_range = 1; wifi_sig.link_quality_max = wifi_info->range.max_qual.qual; return true; }
void open_sockets (void) { wfd = iw_sockets_open(); skfd = socket (AF_INET, SOCK_DGRAM, 0); }
int main(int argc, const char **argv) { /* Check uid */ if (getuid() != 0) { fprintf(stderr,"Swifer must be run as root.\n"); return 1; } /* Check config file for interface and dhcp */ FILE *cfg; if ( (cfg=fopen(config,"r")) ) { char *line = calloc(MAX_LINE+1,sizeof(char)); char *val = calloc(MAX_LINE+1,sizeof(char)); while (fgets(line,MAX_LINE,cfg) != NULL) { if (line[0] == '#') continue; if (sscanf(line,"INTERFACE = %s",val)) strncpy(ifname,val,IFNAMSIZ); else if (sscanf(line,"DHCP = %s",val)) strncpy(dhcp,val,DHCPLEN); else if (sscanf(line,"PRE_UP = %s",val)) hook_preup = strdup(val); else if (sscanf(line,"POST_UP = %s",val)) hook_postup = strdup(val); else if (strncmp(line,"[NETWORKS]",10)==0) break; } free(line); free(val); fclose(cfg); } /* Get basic wifi info */ we_ver = iw_get_kernel_we_version(); skfd = iw_sockets_open(); iw_get_basic_config(skfd,ifname,&cur); /* Bring up interface (eg "ip link set IFACE up") */ struct ifreq req; int err; strncpy(req.ifr_name,ifname,IFNAMSIZ); if ( (err=ioctl(skfd,SIOCGIFFLAGS,&req)) ){ int loop = 0; while ( (err=ioctl(skfd,SIOCGIFFLAGS,&req)) ) { usleep(100000); if (loop++ > 50) break; } if (err) { close(skfd); return 2; } } req.ifr_flags |= IFF_UP; if (ioctl(skfd,SIOCSIFFLAGS,&req)) { close(skfd); return 3; } /* Processes command line arguments */ int i; for (i = 1; i < argc; i++) { if (strncmp(argv[i],"ad",2)==0) mode |= MODE_ADD; else if (strncmp(argv[i],"au",2)==0) mode |= MODE_AUTO; else if (strncmp(argv[i],"hi",2)==0) mode |= MODE_HIDDEN; else if (strncmp(argv[i],"an",2)==0) mode |= (MODE_ANY | MODE_AUTO); else if (strncmp(argv[i],"re",2)==0) mode |= (MODE_RECONNECT | MODE_AUTO); else if (strncmp(argv[i],"ve",2)==0) mode |= MODE_VERBOSE; else if (strncmp(argv[i],"wa",2)==0) mode |= MODE_WAIT; else if (strncmp(argv[i],"de",2)==0) { if (argc > i+1) remove_network(argv[i+1]); } else fprintf(stderr,"[%s] Ignoring unknown parameter: %s\n", argv[0],argv[i]); } if ( (mode & MODE_VERBOSE) && (mode & MODE_AUTO) ) mode &= ~MODE_VERBOSE; /* Scan and select network */ iw_scan(skfd,ifname,we_ver,&context); wireless_scan *ws; if (mode & MODE_AUTO) { if (mode && MODE_WAIT) { int loops; /* keep trying for up to 5 minutes */ for (loops = 0; loops < 30 && !(ws=get_best()); ++loops) sleep(TIMEOUT); } else ws = get_best(); } else ws = show_menu(); const char *arg[4]; if (ws) { /* Stop any current processes then connect to "ws" */ arg[0] = killall; arg[1] = dhcp; arg[2] = NULL; if (fork()==0) { fclose(stdout); fclose(stderr); execvp(arg[0],(char * const *) arg); } arg[1] = wpa_sup; if (fork()==0) { fclose(stdout); fclose(stderr); execvp(arg[0],(char * const *) arg); } sleep(1); if ( (mode & MODE_ADD) && is_known(ws) ) mode &= ~MODE_ADD; if (ws->b.key_flags == 2048) mode |= MODE_SECURE; mode_t pre = umask(S_IWGRP|S_IWOTH|S_IRGRP|S_IROTH); ws_connect(ws); umask(pre); } else if ( !(mode & MODE_RECONNECT) ) { fprintf(stderr,"[swifer] no suitable networks found.\n"); return 5; } /* Keep alive to reconnect? */ iw_sockets_close(skfd); if (mode & MODE_RECONNECT) { if (fork() == 0) { setsid(); int level = THRESHOLD + 1, ret; char scanline[256]; snprintf(scanline,255,"%%*[^\n]\n%%*[^\n]\n%s: %%*d %%d.",ifname); FILE *procw; while (level > THRESHOLD) { sleep(TIMEOUT); procw = fopen(PROC_NET_WIRELESS,"r"); ret = fscanf(procw,scanline,&level); fclose(procw); if (ret != 1) level = 0; } arg[0] = argv[0]; arg[1] = re; arg[2] = an; arg[3] = NULL; if ( !(mode & MODE_ANY)) arg[2] = NULL; execvp(arg[0],(char * const *) arg); } } if (hook_preup) free(hook_preup); if (hook_postup) free(hook_postup); return 0; }
static int get_wireless_info(const char *interface, wireless_info_t *info) { memset(info, 0, sizeof(wireless_info_t)); #ifdef LINUX int skfd = iw_sockets_open(); if (skfd < 0) { perror("iw_sockets_open"); return 0; } wireless_config wcfg; if (iw_get_basic_config(skfd, interface, &wcfg) < 0) { close(skfd); return 0; } if (wcfg.has_essid && wcfg.essid_on) { info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID; strncpy(&info->essid[0], wcfg.essid, IW_ESSID_MAX_SIZE); info->essid[IW_ESSID_MAX_SIZE] = '\0'; } /* If the function iw_get_stats does not return proper stats, the wifi is considered as down. Since ad-hoc network does not have theses stats, we need to return here for this mode. */ if (wcfg.mode == 1) { close(skfd); return 1; } /* Wireless quality is a relative value in a driver-specific range. Signal and noise level can be either relative or absolute values in dBm. Furthermore, noise and quality can be expressed directly in dBm or in RCPI (802.11k), which we convert to dBm. When those values are expressed directly in dBm, they range from -192 to 63, and since the values are packed into 8 bits, we need to perform 8-bit arithmetic on them. Assume absolute values if everything else fails (driver bug). */ iwrange range; if (iw_get_range_info(skfd, interface, &range) < 0) { close(skfd); return 0; } iwstats stats; if (iw_get_stats(skfd, interface, &stats, &range, 1) < 0) { close(skfd); return 0; } if (stats.qual.level != 0 || (stats.qual.updated & (IW_QUAL_DBM | IW_QUAL_RCPI))) { if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) { info->quality = stats.qual.qual; info->quality_max = range.max_qual.qual; info->quality_average = range.avg_qual.qual; info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY; } if (stats.qual.updated & IW_QUAL_RCPI) { if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) { info->signal_level = stats.qual.level / 2.0 - 110 + 0.5; info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; } if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) { info->noise_level = stats.qual.noise / 2.0 - 110 + 0.5; info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE; } } else { if ((stats.qual.updated & IW_QUAL_DBM) || stats.qual.level > range.max_qual.level) { if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) { info->signal_level = stats.qual.level; if (info->signal_level > 63) info->signal_level -= 256; info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; } if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) { info->noise_level = stats.qual.noise; if (info->noise_level > 63) info->noise_level -= 256; info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE; } } else { if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) { info->signal_level = stats.qual.level; info->signal_level_max = range.max_qual.level; info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; } if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) { info->noise_level = stats.qual.noise; info->noise_level_max = range.max_qual.noise; info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE; } } } } else { if (!(stats.qual.updated & IW_QUAL_QUAL_INVALID)) { info->quality = stats.qual.qual; info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY; } if (!(stats.qual.updated & IW_QUAL_LEVEL_INVALID)) { info->quality = stats.qual.level; info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; } if (!(stats.qual.updated & IW_QUAL_NOISE_INVALID)) { info->quality = stats.qual.noise; info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE; } } struct iwreq wrq; if (iw_get_ext(skfd, interface, SIOCGIWRATE, &wrq) >= 0) info->bitrate = wrq.u.bitrate.value; close(skfd); return 1; #endif #if defined(__FreeBSD__) || defined(__DragonFly__) int s, len, inwid; uint8_t buf[24 * 1024], *cp; struct ieee80211req na; char network_id[IEEE80211_NWID_LEN + 1]; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return (0); memset(&na, 0, sizeof(na)); strlcpy(na.i_name, interface, sizeof(na.i_name)); na.i_type = IEEE80211_IOC_SSID; na.i_data = &info->essid[0]; na.i_len = IEEE80211_NWID_LEN + 1; if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) { close(s); return (0); } if (inwid == 0) { if (na.i_len <= IEEE80211_NWID_LEN) len = na.i_len + 1; else len = IEEE80211_NWID_LEN + 1; info->essid[len -1] = '\0'; } else { close(s); return (0); } info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID; memset(&na, 0, sizeof(na)); strlcpy(na.i_name, interface, sizeof(na.i_name)); na.i_type = IEEE80211_IOC_SCAN_RESULTS; na.i_data = buf; na.i_len = sizeof(buf); if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) { printf("fail\n"); close(s); return (0); } close(s); len = na.i_len; cp = buf; struct ieee80211req_scan_result *sr; uint8_t *vp; sr = (struct ieee80211req_scan_result *)cp; vp = (u_int8_t *)(sr + 1); strlcpy(network_id, (const char *)vp, sr->isr_ssid_len + 1); if (!strcmp(network_id, &info->essid[0])) { info->signal_level = sr->isr_rssi; info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; info->noise_level = sr->isr_noise; info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE; info->quality = sr->isr_intval; info->flags |= WIRELESS_INFO_FLAG_HAS_QUALITY; } return 1; #endif #ifdef __OpenBSD__ struct ifreq ifr; struct ieee80211_bssid bssid; struct ieee80211_nwid nwid; struct ieee80211_nodereq nr; struct ether_addr ea; int s, len, ibssid, inwid; u_int8_t zero_bssid[IEEE80211_ADDR_LEN]; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return (0); memset(&ifr, 0, sizeof(ifr)); ifr.ifr_data = (caddr_t)&nwid; (void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr); memset(&bssid, 0, sizeof(bssid)); strlcpy(bssid.i_name, interface, sizeof(bssid.i_name)); ibssid = ioctl(s, SIOCG80211BSSID, &bssid); if (ibssid != 0 || inwid != 0) { close(s); return 0; } /* NWID */ { if (nwid.i_len <= IEEE80211_NWID_LEN) len = nwid.i_len + 1; else len = IEEE80211_NWID_LEN + 1; strncpy(&info->essid[0], nwid.i_nwid, len); info->essid[IW_ESSID_MAX_SIZE] = '\0'; info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID; } /* Signal strength */ { memset(&zero_bssid, 0, sizeof(zero_bssid)); if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) { memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet)); bzero(&nr, sizeof(nr)); bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr)); strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname)); if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) { if (nr.nr_max_rssi) info->signal_level_max = IEEE80211_NODEREQ_RSSI(&nr); else info->signal_level = nr.nr_rssi; info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL; } } } close(s); return 1; #endif return 0; }
/* * The main ! */ int main(int argc, char ** argv) { int skfd; /* generic raw socket desc. */ char *dev; /* device name */ char *cmd; /* command */ char **args; /* Command arguments */ int count; /* Number of arguments */ const iwlist_cmd *iwcmd; if(argc == 1 || argc > 3) iw_usage(1); /* Those don't apply to all interfaces */ if((argc == 2) && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) iw_usage(0); if((argc == 2) && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) return(iw_print_version_info("iwlist")); if(argc == 2) { cmd = argv[1]; dev = NULL; args = NULL; count = 0; } else { cmd = argv[2]; dev = argv[1]; args = argv + 3; count = argc - 3; } /* find a command */ iwcmd = find_command(cmd); if(iwcmd == NULL) return 1; /* Check arg numbers */ if(count < iwcmd->min_count) { fprintf(stderr, "iwlist: command `%s' needs more arguments\n", cmd); return 1; } if(count > iwcmd->max_count) { fprintf(stderr, "iwlist: command `%s' needs fewer arguments\n", cmd); return 1; } /* Create a channel to the NET kernel. */ if((skfd = iw_sockets_open()) < 0) { perror("socket"); return -1; } /* do the actual work */ if (dev) (*iwcmd->fn)(skfd, dev, args, count); else iw_enum_devices(skfd, iwcmd->fn, args, count); /* Close the socket. */ iw_sockets_close(skfd); return 0; }
void hd_scan_wlan(hd_data_t *hd_data) { hd_t *hd; hd_res_t *res; struct iw_range range; int k; int skfd; struct wpa_driver_ops *wpa_drv=NULL; if(!hd_probe_feature(hd_data, pr_wlan)) return; hd_data->module = mod_wlan; PROGRESS(1, 0, "detecting wlan features"); if ((skfd = iw_sockets_open()) < 0) { ADD2LOG( "could not open socket, wlan feature query failed\n" ); return; } for(hd = hd_data->hd; hd; hd = hd->next) { if( hd->base_class.id == bc_network && hd->unix_dev_name ) { /* Get list of frequencies / channels */ if(iw_get_range_info(skfd, hd->unix_dev_name, &range) < 0) { /* this failed, maybe device does not support wireless extensions */ continue; } ADD2LOG("*** device %s is wireless ***\n", hd->unix_dev_name); hd->is.wlan = 1; hd->sub_class.id = 0x82; /* wlan */ res = new_mem(sizeof *res); res->any.type = res_wlan; if(range.num_frequency > 0) { char buff[20]; for(k = 0; k < range.num_frequency; k++) { snprintf(buff, 19, "%i", range.freq[k].i); add_str_list(&res->wlan.channels, buff); snprintf(buff, 19, "%g", (float)iw_freq2float(&(range.freq[k]))/1000000000); add_str_list(&res->wlan.frequencies, buff); } for(k = 0; k < range.num_bitrates; k++) { snprintf(buff, 19, "%g", (float)range.bitrate[k]/1000000); add_str_list(&res->wlan.bitrates, buff); } for(k = 0; k < range.num_encoding_sizes; k++) { snprintf(buff, 19, "WEP%i", range.encoding_size[k]*8); add_str_list(&res->wlan.enc_modes, buff); } /* open mode is always supported */ add_str_list(&res->wlan.auth_modes, "open"); /* if WEP is supported, be assume shared key auth support */ if(range.num_encoding_sizes) { add_str_list(&res->wlan.auth_modes, "sharedkey"); } /* detect WPA capabilities */ if (!hd_data->flags.nowpa && hd->drivers) { if (search_str_list(hd->drivers, "ath_pci")) wpa_drv = &wpa_driver_madwifi_ops; else if (strncmp(hd->drivers->str, "at76", 4)==0) wpa_drv = &wpa_driver_atmel_ops; else wpa_drv = &wpa_driver_wext_ops; } if (wpa_drv) { if (wpa_drv->set_wpa(hd->unix_dev_name, 1) == 0) { add_str_list(&res->wlan.auth_modes, "wpa-psk"); add_str_list(&res->wlan.auth_modes, "wpa-eap"); if (wpa_drv->set_auth_alg && wpa_drv->set_auth_alg(hd->unix_dev_name, AUTH_ALG_LEAP)==0) add_str_list(&res->wlan.auth_modes, "wpa-leap"); if (wpa_drv->set_key(hd->unix_dev_name, WPA_ALG_TKIP, "ff:ff:ff:ff:ff:ff", 0, 0, 0, 0, "00000000000000000000000000000000", 32) ==0) add_str_list(&res->wlan.enc_modes, "TKIP"); if (wpa_drv->set_key(hd->unix_dev_name, WPA_ALG_CCMP, "ff:ff:ff:ff:ff:ff", 0, 0, 0, 0, "0000000000000000", 16) ==0) add_str_list(&res->wlan.enc_modes, "CCMP"); wpa_drv->set_wpa(hd->unix_dev_name, 0); } } } add_res_entry(&hd->res, res); } } }
/* * The main ! */ int main(int argc, char ** argv) { int skfd; /* generic raw socket desc. */ int format = FORMAT_DEFAULT; int wtype = WTYPE_ESSID; int opt; int ret = -1; /* Check command line arguments */ while((opt = getopt_long(argc, argv, "acfhmprs", long_opts, NULL)) > 0) { switch(opt) { case 'a': /* User wants AP/Cell Address */ wtype = WTYPE_AP; break; case 'c': /* User wants channel only */ wtype = WTYPE_CHANNEL; break; case 'f': /* User wants frequency/channel */ wtype = WTYPE_FREQ; break; case 'm': /* User wants the mode */ wtype = WTYPE_MODE; break; case 'p': /* User wants the protocol */ wtype = WTYPE_PROTO; break; case 'h': iw_usage(0); break; case 'r': /* User wants a Raw format */ format = FORMAT_RAW; break; case 's': /* User wants a Scheme format */ format = FORMAT_SCHEME; break; default: iw_usage(1); break; } } if(optind + 1 < argc) { fputs("Too many arguments.\n", stderr); iw_usage(1); } /* Create a channel to the NET kernel. */ if((skfd = iw_sockets_open()) < 0) { perror("socket"); return(-1); } /* Check if first argument is a device name */ if(optind < argc) { /* Yes : query only this device */ ret = print_one_device(skfd, format, wtype, argv[optind]); } else { /* No : query all devices and print first found */ ret = scan_devices(skfd, format, wtype); } fflush(stdout); iw_sockets_close(skfd); return(ret); }