Thread: The program does not want to scan networks

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    1

    Red face The program does not want to scan networks

    I made a program that requests the wifi network and displays their information on the screen. Here is the source code:
    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?
    The program is similar to iwlist.
    I run it and gave it to me: No scan results.
    I run sudo ./scan -i wifi ans see: wifi - interface doesn't support scanningevice or resource is busy.
    I run: iwlist wifi scan and run: ./scan -i wifi and saw a list of networks.
    From the root user, the program starts the scan but gives an error !! I can not understand what the problem is and ask for help, on the official site of the library, they do not answer me for a month

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I have not study the code ,(yet) but I know on Linux the user has to have root privileges to scan, if your code is good it maybe your privileges then holding you back. that is a quick check to eliminate that possibility.

    edit: I do not have #include "structurs.h", so it's error out 4 me.

    Is that just your personal functions you're using in main that you put in that file?
    Last edited by userxbw; 11-10-2017 at 08:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scan for vowels program..
    By Sean Bradley in forum C Programming
    Replies: 20
    Last Post: 11-20-2013, 12:14 PM
  2. Replies: 3
    Last Post: 06-16-2013, 01:28 AM
  3. scan f seems to crash my program
    By jakeonator in forum C Programming
    Replies: 25
    Last Post: 06-12-2011, 12:08 PM
  4. Neural Networks VS. Spike Neural Networks
    By magis in forum General AI Programming
    Replies: 1
    Last Post: 04-12-2005, 06:37 AM
  5. P2P networks.
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-05-2003, 03:43 AM

Tags for this Thread