Want to know the ESSID of wireless network via C++ in UBUNTU
Asked Answered
W

4

7

I have written the following program to get the ESSID of the wireless network to which my Desktop is currently connected, but it is giving me errors. Can anyone help me correct the bugs? Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h> 
#include <fcntl.h>
#include <errno.h>
#include <linux/wireless.h>

#define IW_INTERFACE "wlan0"

extern int errno;
struct iwreq wreq;

int main (void)
{
    int sockfd;
    char * id;

    memset(&wreq, 0, sizeof(struct iwreq));
    sprintf(wreq.ifr_name, IW_INTERFACE);

    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        fprintf(stderr, "Cannot open socket \n");
        fprintf(stderr, "errno = %d \n", errno);
        fprintf(stderr, "Error description is : %s\n",strerror(errno));
        exit(1);
    }
    printf("Socket opened successfully \n");


    id = new char(IW_ESSID_MAX_SIZE+1);
    wreq.u.essid.pointer = id;
    if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
        fprintf(stderr, "Get ESSID ioctl failed \n");
        fprintf(stderr, "errno = %d \n", errno);
        fprintf(stderr, "Error description : %s\n",strerror(errno));
        exit(2);
    }
    printf("IOCTL Successfull\n");
    printf("ESSID is %s\n", wreq.u.essid.pointer);
    exit(0);
}

I am getting following error:

  > 1. Get ESSID ioctl failed 
  > 2. errno = 7 
  > 3. Error description : Argument list too long
Warmblooded answered 9/5, 2011 at 12:30 Comment(0)
W
5

You should set length properly first before using werq, check this,

int sockfd;
    char * id;
   id = new char[IW_ESSID_MAX_SIZE+1];

    struct iwreq wreq;
    memset(&wreq, 0, sizeof(struct iwreq));
    wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;

    sprintf(wreq.ifr_name, IW_INTERFACE);

    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        fprintf(stderr, "Cannot open socket \n");
        fprintf(stderr, "errno = %d \n", errno);
        fprintf(stderr, "Error description is : %s\n",strerror(errno));
        exit(1);
    }
    printf("\nSocket opened successfully \n");

    wreq.u.essid.pointer = id;
    if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
        fprintf(stderr, "Get ESSID ioctl failed \n");
        fprintf(stderr, "errno = %d \n", errno);
        fprintf(stderr, "Error description : %s\n",strerror(errno));
        exit(2);
    }

    printf("IOCTL Successfull\n");

    printf("ESSID is %s\n", (char *)wreq.u.essid.pointer);
Woolery answered 1/11, 2013 at 19:5 Comment(0)
G
3

You are testing the return code of ioctl(2) incorrectly. ioctl(2) returns -1 on error, not true (non-zero). Since an error is not being returned, the value in errno is undefined and is misleading.

It should read:

if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
    fprintf(stderr, "Get ESSID ioctl failed \n");
    fprintf(stderr, "errno = %d \n", errno);
    fprintf(stderr, "Error description : %s\n",strerror(errno));
    exit(2);
}

Edit: With that incorrect code out of the way, there's this:

id = new char(IW_ESSID_MAX_SIZE+1);

Perhaps you meant:

id = new char[IW_ESSID_MAX_SIZE+1];

The former allocated only a single character and sets its value to IW_ESSID_MAX_SIZE+1. The latter allocates an array of size IW_ESSID_MAX_SIZE+1.

With that change, the code works for me and does not give an error for the ioctl.

Gongorism answered 9/5, 2011 at 13:10 Comment(2)
Though you are right and I even tried to what you said but the value returned is -1 by ioctl e.g.if ( ioctl(sockfd, SIOCGIWESSID, &wreq) == -1) { fprintf(stderr, "Get ESSID ioctl failed \n"); fprintf(stderr, "errno = %d \n", errno); fprintf(stderr, "Error description : %s\n",strerror(errno)); printf("ESSID is %s\n", wreq.u.essid.pointer);Warmblooded
@Alan: I've edited your question. Please tell me if it is still wrong.Gongorism
P
2

Try setting wreq.u.essid.length to the right value.

Perfective answered 9/5, 2011 at 18:7 Comment(1)
To clarify, wreq.u. memset to zero. essid.length is thus 0. The errno 7 is a bit misleading, but basically "your indicated reserved space is not long enough to hold the value".Cristycriswell
V
1

printf("Socket opened successfully \n");

//id = new char(IW_ESSID_MAX_SIZE+1);
char buffer[32];
memset(buffer, 0, 32);
wreq.u.essid.pointer = buffer;
wreq.u.essid.length = 32;

//wreq.u.essid.pointer = id;
if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
    fprintf(stderr, "Get ESSID ioctl failed \n");
    fprintf(stderr, "errno = %d \n", errno);
    fprintf(stderr, "Error description : %s\n",strerror(errno));
    exit(2);
}
printf("IOCTL Successfull\n");
Voorhees answered 13/2, 2012 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.