LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2011, 07:05 AM   #1
rajashacks
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
IOCTL error: to get the ssid through c program


dear all,

I was trying to get the ssid of WLAN through the following C program

/* Program to print the SSID of the current network */
/* getssid.c */


#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 = malloc(IW_ESSID_MAX_SIZE+1);
wreq.u.essid.pointer = id;
if (ioctl(sockfd, SIOCGIWESSID, &wreq)) {
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);
}



While trying to run I got the following error


Socket opened successfully
Get ESSID ioctl failed
errno = 7
Error description : Argument list too long

What is the cause of this error? any suggestions
 
Old 03-13-2011, 12:43 PM   #2
timetraveler
Member
 
Registered: Apr 2010
Posts: 243
Blog Entries: 2

Rep: Reputation: 31
Have a look at some wifi code that does this and is working.
 
Old 04-29-2020, 07:57 AM   #3
mas94
LQ Newbie
 
Registered: Apr 2020
Posts: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by rajashacks View Post
What is the cause of this error? any suggestions
I imagine, nine years on, you've either got this working or no longer care, but since Google brought me here looking for how to do this, perhaps this will be useful for someone in the future.

You need to set another field in the
Code:
wreq.u.essid
to the length of the buffer you've set
Code:
wreq.u.essid.pointer
to.

Complete working code:

Code:
/* Program to print the SSID of the current network */
/* getssid.c */

#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;

int main (void)
{
    struct iwreq wreq;
	memset(&wreq, 0, sizeof(struct iwreq));
	sprintf(wreq.ifr_name, IW_INTERFACE);

	int sockfd;
	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");

	char *id = malloc(IW_ESSID_MAX_SIZE+1);
	wreq.u.essid.pointer = id;
	wreq.u.essid.length = IW_ESSID_MAX_SIZE;
	if (ioctl(sockfd, SIOCGIWESSID, &wreq)) {
		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));
	exit(0);
}
Hope that helps someone in the future, as the original post did me :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need a ioctl sample program supernaturalyasir Programming 1 09-09-2009 08:12 AM
Error Message: Program eject is using a deprecated SCSI ioctl, please convert to SG Jogie Linux - Newbie 4 08-24-2009 06:35 PM
ioctl call returns error. HarryBoy Programming 6 09-10-2008 10:08 AM
How to pass IOCTL arguments from usespace ioctl call devkpict Linux - Kernel 1 12-07-2007 06:45 PM
set SSID error chalichalupa Linux - Networking 1 01-26-2004 08:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration