1

Hello I've written the following code which is a part of a project. It is used to find the ESSID of the current associated network.

But it has a flaw: it also the displays the ESSID of the network with which I am not associated i.e. if I try to associate myself with a wireless n/w and if it is unsuccessful i.e. NO DHCP OFFERS ARE RECEIVED, then also it will display the that ESSID with which I have made my attempt.

Could anyone give me an ioctl call to find the BSSID of current associated wireless n/w?. In my opinion it is the only way with which I a can mark b/w associated and non associated.

CODE:-

 int main (void)
 {
 int errno;
 struct iwreq wreq;

 CStdString result = "None";

int sockfd;
char * id;
char ESSID[100];
memset(&wreq, 0, sizeof(struct iwreq));


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));
return result ;
}
CLog::Log(LOGINFO,"Socket opened successfully");


FILE* fp = fopen("/proc/net/dev", "r");
if (!fp)
{
 // TBD: Error
 return result;
}

char* line = NULL;
size_t linel = 0;
int n;
char* p;
int linenum = 0;
while (getdelim(&line, &linel, '\n', fp) > 0)
{
  // skip first two lines
  if (linenum++ < 2)
     continue;


  p = line;
  while (isspace(*p))
  ++p;


  n = strcspn(p, ": \t");
  p[n] = 0;

  strcpy(wreq.ifr_name, p);



id = new char[IW_ESSID_MAX_SIZE+100];
wreq.u.essid.pointer = id;
wreq.u.essid.length = 100;
if ( ioctl(sockfd,SIOCGIWESSID, &wreq) == -1 ) {
    continue;
    }
else 
{
strcpy(ESSID,id);
return ESSID;
}
free(id);
}

free(line);
fclose(fp);
return result;
 }
J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • Do not repost your question on StackOverflow. It will be moved. – BBlake May 13 '11 at 13:38
  • 1
    Does StackOverflow have a /b/ board he could post this to? – SnobOverflow May 13 '11 at 13:41
  • possible duplicate of [Want to know the ESSID of wireless network via C++ in UBUNTU](http://stackoverflow.com/questions/5937757/want-to-know-the-essid-of-wireless-network-via-c-in-ubuntu) – johnsyweb May 13 '11 at 15:01
  • Does this answer your question? [Want to know the ESSID of wireless network via C++ in UBUNTU](https://stackoverflow.com/questions/5937757/want-to-know-the-essid-of-wireless-network-via-c-in-ubuntu) – Cœur Nov 24 '19 at 14:03

0 Answers0