Problems retrieving complete ifreq structs from ifconf

Hey,

I'm trying to programatically retrieve certain info on my network interfaces and pack it into a string array. I do an ioctl call with the SIOCGIFCONF flag no problem, and when I loop through the result ifreq's I have the correct amount, and I can succesfully retrieve their names. The problem is that I can't seem to retrieve anything else, I'm trying to also get IP address and HW address but they just seem to be blank no matter what I try.

Furthermore, this doesn't seem to return interfaces that have not been designated an Address even though i've turned them on (ifconfig eth1 up).

My code is attached below if anyone could advise I'd be thankful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
string* NeighbourAddressFinder:: defineSelf()
{  
  struct ifconf ifc;
  struct ifreq *ifr;
  int    fd;
  char buff[300];
  struct sockaddr sa;
  try
  {
  
    //prepare socket used to channel ifreq struct
    if((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0)
    {
      perror("socket:");
      return NULL;
    }
  
    //get ifconf object via ioctl, then iterate through until we get what we want
    ifc.ifc_len=sizeof(buff);
    ifc.ifc_buf = buff;
    if(ioctl(fd, SIOCGIFCONF, &ifc) < 0)
    {
      perror("ioctl:");
      return NULL;
    }

    interfaces[0] = "eth1";  
    
    for(int c=0; c<ifc.ifc_len/sizeof(struct ifreq); c++)
    {
      ifr = &ifc.ifc_req[c];
      
      if(ifr->ifr_name == interfaces[0])
      {
        sa = ifr->ifr_hwaddr;
        interfaces[1] = ifr->ifr_hwaddr.sa_data;        
        interfaces[2] = ifr->ifr_addr.sa_data;
        cout << interfaces[2] << endl; //for test
      }
      else if(ifr->ifr_name == "eth0")
        interfaces[3] = ifr->ifr_addr.sa_data;
    }
    cout << "|| -- - " << interfaces[3] << endl;
    if(interfaces[1].empty() || interfaces[3].empty())
    {
      cout << "|| -- - ERROR retrieving Mesh interfaces" << endl;
      return NULL;
    }
    else
    return interfaces;
  }
  catch(exception& e)
  {
    cout << "|E| -- -" << NERROR_MESSAGE << "| - | @defineSelf " << e.what() << endl;
    return NULL;
  }
}
Last edited on
Topic archived. No new replies allowed.