Skip to content

Instantly share code, notes, and snippets.

@TrilbyWhite
Created December 27, 2014 03:01
Show Gist options
  • Save TrilbyWhite/9dfa1b2ec18ef3aab61c to your computer and use it in GitHub Desktop.
Save TrilbyWhite/9dfa1b2ec18ef3aab61c to your computer and use it in GitHub Desktop.
// build: gcc -o iwtest iwtest.c -liw
#include <stdio.h>
#include <iwlib.h>
int main(int argc, const char **argv) {
/* Check uid */
if (getuid() != 0) {
fprintf(stderr,"must be run as root.\n");
return 1;
}
/* Check for interface name */
char *ifname;
if (!(ifname = (char *) argv[1])) {
fprintf(stderr,"an interface name must be provided.\n");
return 2;
}
/* Get basic wifi info */
int we_ver, skfd, mode;
wireless_scan_head context;
wireless_config cur;
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);
fprintf(stderr,"unable to set \"%s\" down.\n", ifname);
return 3;
}
}
req.ifr_flags |= IFF_UP;
if (ioctl(skfd,SIOCSIFFLAGS,&req)) {
fprintf(stderr,"unable to set \"%s\" up.\n", ifname);
close(skfd); return 3;
}
/* Scan and select network */
iw_scan(skfd,ifname,we_ver,&context);
wireless_scan *ws;
/* list */
int i = 0;
for (ws = context.result; ws; ws = ws->next)
printf("%d: %s\n", ++i, ws->b.essid);
/* close down */
iw_sockets_close(skfd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment