The following function determines whether a given string is a valid network device name.
int isValidNDevice(char *name) { char data[4096]; struct ifconf ifc; struct ifreq *ifr; int sk; int nIntfcs; sk = socket(AF_INET, SOCK_DGRAM, 0); if(sk < 0) { perror("socket"); return 0; } ifc.ifc_len = sizeof(data); ifc.ifc_buf = (caddr_t)data; if(ioctl(sk, SIOCGIFCONF, &ifc) < 0) { perror("ioctl(SIOCGIFCONF)"); return 0; } ifr = (struct ifreq*)data; nIntfcs = ifc.ifc_len / sizeof(struct ifreq); for(int i=0; i < nIntfcs; i++) { safe_printf("%s\n", (&ifr[i])->ifr_name); if (!strcmp((&ifr[i])->ifr_name, name)) { return 1; } } return 0; }
When I run this function, I get the following output.
lo0
stf0
2> S
en1j
0
This code worked fine a few months ago. What changed? Am I doing something wrong?
OS: OSX El Capitan
c sockets macos
Roecrew
source share