/* * Try the various devices until one return something we can use */ static int scan_devices(int skfd, int format) { char buff[1024]; struct ifconf ifc; struct ifreq *ifr; int i; int ret; /* Get list of active devices */ ifc.ifc_len = sizeof(buff); ifc.ifc_buf = buff; if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0) { fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno)); return(-1); } ifr = ifc.ifc_req; /* Print the first match */ for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) { /* Try to print an ESSID */ ret = print_essid(skfd, ifr->ifr_name, format); if(ret == 0) return(0); /* Success */ /* Try to print a nwid */ ret = print_nwid(skfd, ifr->ifr_name, format); if(ret == 0) return(0); /* Success */ } return(-1); }
/* * Check options and call the proper handler */ static int print_one_device(int skfd, int format, int wtype, const char* ifname) { int ret; /* Check wtype */ switch(wtype) { case WTYPE_AP: /* Try to print an AP */ ret = print_ap(skfd, ifname, format); break; case WTYPE_CHANNEL: /* Try to print channel */ ret = print_channel(skfd, ifname, format); break; case WTYPE_FREQ: /* Try to print frequency */ ret = print_freq(skfd, ifname, format); break; case WTYPE_MODE: /* Try to print the mode */ ret = print_mode(skfd, ifname, format); break; case WTYPE_PROTO: /* Try to print the protocol */ ret = print_protocol(skfd, ifname, format); break; default: /* Try to print an ESSID */ ret = print_essid(skfd, ifname, format); if(ret < 0) { /* Try to print a nwid */ ret = print_nwid(skfd, ifname, format); } } return(ret); }
/* * The main ! */ int main(int argc, char ** argv) { int skfd = -1; /* generic raw socket desc. */ int format = FORMAT_DEFAULT; int ret = -1; /* Create a channel to the NET kernel. */ if((skfd = sockets_open()) < 0) { perror("socket"); return(-1); } /* No argument */ if(argc == 1) { /* Look on all devices */ ret = scan_devices(skfd, format); close(skfd); return(ret); } /* Only ask for first AP address */ if((!strcmp(argv[1], "--ap")) || (!strcmp(argv[1], "-a"))) { /* Look on all devices */ ret = scan_ap(skfd, format); close(skfd); return(ret); } /* Only the format, no interface name */ if((!strncmp(argv[1], "--scheme", 4)) || (!strcmp(argv[1], "-s"))) { /* Look on all devices */ format = FORMAT_SCHEME; ret = scan_devices(skfd, format); close(skfd); return(ret); } /* Help */ if((argc > 3) || (!strncmp(argv[1], "-h", 9)) || (!strcmp(argv[1], "--help"))) { fprintf(stderr, "Usage: iwgetid [interface]\n"); fprintf(stderr, " [interface] --scheme\n"); return(-1); } /* If at least a device name */ if(argc > 1) { /* Check extra format argument */ if(argc > 2) { /* Only ask for first AP address */ if((!strcmp(argv[2], "--ap")) || (!strcmp(argv[2], "-a"))) { ret = print_ap(skfd, argv[1], format); close(skfd); return(ret); } /* Want scheme format */ if((!strncmp(argv[2], "--scheme", 4)) || (!strcmp(argv[2], "-s"))) format = FORMAT_SCHEME; } /* Try to print an ESSID */ ret = print_essid(skfd, argv[1], format); if(ret == -1) { /* Try to print a nwid */ ret = print_nwid(skfd, argv[1], format); } } /* Close the socket. */ close(skfd); return(ret); }