// Sasa Spasic // (c) 2003. #include #include #include #include #include #include #include #include #include int list_ifaces(int sock_fd) { struct ifconf ifc; struct ifreq *ifr = NULL; int if_len = 0; int num_ifreq_items = 10; // Pretpostavka da ce 10 adresa da bude dovoljno for (;;) { ifc.ifc_len = sizeof(struct ifreq) * num_ifreq_items; ifc.ifc_buf = malloc(ifc.ifc_len); if (NULL == ifc.ifc_buf) { perror("malloc"); free(ifc.ifc_buf); return -1; } if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0) { perror("ioctl(SIOCGIFCONF)"); free(ifc.ifc_buf); return -1; } if (ifc.ifc_len == sizeof(struct ifreq) * num_ifreq_items) { /* Mozda ipak nije bilo dovoljno mesta? Uvecaj buffer za 10 mesta i probaj ponovo */ num_ifreq_items += 10; free(ifc.ifc_buf); } else { break; } } ifr = ifc.ifc_req; for (if_len = 0; if_len < ifc.ifc_len; if_len += sizeof(struct ifreq), ++ifr) { struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr; printf("%s %s\n", ifr->ifr_name, inet_ntoa(sin->sin_addr)); } free(ifc.ifc_buf); return 0; } int main(int argc, char *argv[]) { int sock_fd = 0; int ret = 0; sock_fd = socket(PF_INET, SOCK_DGRAM, 0); if (sock_fd < 0) { perror("socket"); exit(-1); } ret = list_ifaces(sock_fd); close(sock_fd); exit(ret); }