Code:
#include "structurs.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <locale.h>
#include <stdlib.h>
#include <libintl.h>
#include <locale.h>
#include <stdbool.h>
#include "iwlib.h"
int encryption = 0;
// It errors
#define ERR_NOSUPSCANNING 21
#define ERR_NOSUPSCANNING2 22
#define ERR_ALLOCATION 23
//===========
void startScan(int skfd, char *ifname, int we_ver);
static inline void print_result(struct stream_descr * stream, struct iw_event * event,
struct iwscan_state * state, struct iw_range * iw_range, int has_range);
#define _(text) gettext(text)
//typedef enum { false, true } bool; //boolean types
void pUsage();
int main(int argc, char *argv[])
{
/* Setting the i18n environment */
setlocale(LC_ALL, "");
char *path = malloc(100);
sprintf(path, "%s/po", getenv("PWD"));
bindtextdomain ("main", path);
textdomain ("main");
//#############################
// vars
globalArgs.interface = "wlan0";
globalArgs.outFile = "";
//#############################
int opt, longIndex;
do {
if( (opt = getopt_long(argc, argv, optString, longOpts, &longIndex)) == -1) break;
switch(opt)
{
case 'i':
globalArgs.interface = optarg;
break;
case 'o':
globalArgs.outFile = optarg;
break;
case 'v':
puts("1.0");
return 1;
case 'h':
pUsage();
return 1;
}
} while(true);
int sock;
if( (sock=iw_sockets_open()) == -1)
{
perror("socket");
return 1;
}
int we = iw_get_kernel_we_version();
startScan(sock, globalArgs.interface, we);
iw_sockets_close(sock);
return 0;
}
void pUsage()
{
char color_green[] = "\033[1;32m";
char color_blue[] = "\033[1;34m";
char color_yellow[] = "\033[1;33m";
char off[] = "\033[m";
printf("%proga 1.0%s\n", color_green, off);
printf(_("\t%s-i [name] or --interface=[name]\t%s- Scan through this interface%s\n"), color_yellow, color_blue, off);
printf(_("\t%s-v or --version\t%s- Software version%s\n"), color_yellow, color_blue, off);
printf(_("\t%s-h or --help\t%s- Print this help%s\n"), color_yellow, color_blue, off);
}
void startScan(int skfd, char *ifname, int we_ver)
{
unsigned char *buffer = NULL; /* Results */
int buflen = IW_SCAN_MAX_DATA; /* Min for compat WE<17 */
struct iw_range range;
struct iwreq wrq;
int has_range;
has_range = (iw_get_range_info(skfd, ifname, &range) >= 0); //Get range stuff
if((!has_range) || (range.we_version_compiled < 14))
{
fprintf(stderr, "%s - interface doesn't support scanning.\n", ifname);
iw_sockets_close(skfd); //bug fix
_exit(ERR_NOSUPSCANNING);
}
while(1)
{
wrq.u.data.pointer = NULL;
wrq.u.data.flags = 0;
wrq.u.data.length = 0;
if(iw_set_ext(skfd, ifname, SIOCSIWSCAN, &wrq) < 0) /* Initiate Scanning */
{
if((errno != EPERM))
{
fprintf(stderr, "%s - interface doesn't support scanning: %s\n", ifname, strerror(errno));
iw_sockets_close(skfd);
_exit(ERR_NOSUPSCANNING2);
}
}
unsigned char *newbuf;
realloc:
newbuf = realloc(buffer, buflen); /* (Re)allocate the buffer - realloc(NULL, len) == malloc(len) */
if(newbuf == NULL)
{
if(buffer) free(buffer);
fprintf(stderr, "%s: Allocation failed\n", __FUNCTION__);
iw_sockets_close(skfd);
_exit(ERR_ALLOCATION);
}
buffer = newbuf;
/* Try to read the results */
wrq.u.data.pointer = buffer;
wrq.u.data.flags = 0;
wrq.u.data.length = buflen;
if(iw_get_ext(skfd, ifname, SIOCGIWSCAN, &wrq) < 0)
{
if((errno == E2BIG) && (range.we_version_compiled > 16))
{
if(wrq.u.data.length > buflen) //if too large
buflen = wrq.u.data.length;
else
buflen *= 2;
goto realloc;
}
if(errno == EAGAIN)
{
usleep(100000);
continue;
}
/* Bad error */
free(buffer);
fprintf(stderr, "%s - Failed to read scan data: %s\n", ifname, strerror(errno));
break;
}
else
break; // We have the results, go to process them
}
if(wrq.u.data.length)
{
struct iw_event iwe;
struct stream_descr stream;
struct iwscan_state state = { .ap_num = 1, .val_index = 0 };
int ret;
iw_init_event_stream(&stream, (char *)buffer, wrq.u.data.length);
do /* Extract an event and print it */
{
ret = iw_extract_event_stream(&stream, &iwe, range.we_version_compiled);
if(ret > 0)
{
print_result(&stream, &iwe, &state, &range, has_range); //read one element
}
} while(ret > 0);
}
else
fprintf(stderr, "%s - No scan results\n", ifname);
free(buffer);
}
static inline void print_result(struct stream_descr * stream, struct iw_event * event,
struct iwscan_state * state, struct iw_range * iw_range, int has_range)
{
char buffer[128]; /* Temporary buffer */
/* Now, let's decode the event */
switch(event->cmd)
{
case SIOCGIWAP:
{
iw_ether_ntop((const struct ether_addr *) event->u.ap_addr.sa_data, buffer); //get mac
printf("Mac: %s\n", buffer);
state->ap_num++;
encryption=0;
break;
}
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)
printf("ESSID: %s\n", essid);
else
printf("off/any/hidden\n");
break;
}
case SIOCGIWFREQ:
{
double freq;
int channel = -1;
freq = iw_freq2float(&(event->u.freq));
if(has_range) /* Convert to channel if possible */
{
channel = iw_freq_to_channel(freq, iw_range);
if(channel != -1) printf("Channel: %i\n", channel);
}
break;
}
case SIOCGIWENCODE:
{
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;
if(event->u.data.flags & IW_ENCODE_DISABLED)
encryption = 0;
else
{
iw_print_key(buffer, sizeof(buffer), key, event->u.data.length,
event->u.data.flags);
encryption = 1;
}
break;
}
case IWEVGENIE:
{
}
}
}
Why can not the program make Wi-Fi scan the networks?