I want to get all network interfaces names present on the system. For system portability (Android included) I decided to write a method that performs this task using ioctl
because I think (probably wrongly) that ioctl
is one of the best for this task. So I wrote this code
struct ifconf contr_req; contr_req.ifc_ifcu.ifcu_req = NULL; if(ioctl(sock,SIOCGIFCONF,&contr_req) < 0) return NULL; contr_req.ifc_ifcu.ifcu_buf = malloc(contr_req.ifc_len); register int len = contr_req.ifc_len/sizeof(struct ifreq); char **names = malloc(sizeof(char*)*len); if(ioctl(sock,SIOCGIFCONF,&contr_req) < 0) return NULL; struct ifreq *ini; for(int i = 0;i < len; i++){ ini = contr_req.ifc_ifcu.ifcu_req + sizeof(struct ifreq)*i; register name_len = strlen(ini->ifr_ifrn.ifrn_name)+1; names[i] = malloc(name_len); memset(names[i],0,name_len); memcpy(names[i],ini->ifr_ifrn.ifrn_name,name_len-1); } return names;
But the problem is, that it returns only the loopback interface. Can anyone explain where is the mistake or if there is another ioctl
entry to return all interfaces names on the system?
(I don’t want to use getifaddr
or if_nameindex
but ioctl
.)
Advertisement
Answer
Finally i discovered that SIOCGIFCONF returns only the interfaces that are connected.