関数 | |
static int | _get_interfaces (struct iface_struct *ifaces, int max_interfaces) |
static int | iface_comp (struct iface_struct *i1, struct iface_struct *i2) |
int | get_interfaces (struct iface_struct *ifaces, int max_interfaces) |
int | main () |
static int _get_interfaces | ( | struct iface_struct * | ifaces, | |
int | max_interfaces | |||
) | [static] |
interfaces.c の 95 行で定義されています。
参照先 fd・iface_struct::ip・iface_struct::name・name・iface_struct::netmask・total.
参照元 get_interfaces().
00096 { 00097 struct ifconf ifc; 00098 char buff[8192]; 00099 int fd, i, n; 00100 struct ifreq *ifr=NULL; 00101 int total = 0; 00102 struct in_addr ipaddr; 00103 struct in_addr nmask; 00104 char *iname; 00105 00106 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 00107 return -1; 00108 } 00109 00110 ifc.ifc_len = sizeof(buff); 00111 ifc.ifc_buf = buff; 00112 00113 if (ioctl(fd, SIOCGIFCONF, &ifc) != 0) { 00114 close(fd); 00115 return -1; 00116 } 00117 00118 ifr = ifc.ifc_req; 00119 00120 n = ifc.ifc_len / sizeof(struct ifreq); 00121 00122 /* Loop through interfaces, looking for given IP address */ 00123 for (i=n-1;i>=0 && total < max_interfaces;i--) { 00124 if (ioctl(fd, SIOCGIFADDR, &ifr[i]) != 0) { 00125 continue; 00126 } 00127 00128 iname = ifr[i].ifr_name; 00129 ipaddr = (*(struct sockaddr_in *)&ifr[i].ifr_addr).sin_addr; 00130 00131 if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) != 0) { 00132 continue; 00133 } 00134 00135 if (!(ifr[i].ifr_flags & IFF_UP)) { 00136 continue; 00137 } 00138 00139 if (ioctl(fd, SIOCGIFNETMASK, &ifr[i]) != 0) { 00140 continue; 00141 } 00142 00143 nmask = ((struct sockaddr_in *)&ifr[i].ifr_addr)->sin_addr; 00144 00145 strncpy(ifaces[total].name, iname, sizeof(ifaces[total].name)-1); 00146 ifaces[total].name[sizeof(ifaces[total].name)-1] = 0; 00147 ifaces[total].ip = ipaddr; 00148 ifaces[total].netmask = nmask; 00149 total++; 00150 } 00151 00152 close(fd); 00153 00154 return total; 00155 }
static int iface_comp | ( | struct iface_struct * | i1, | |
struct iface_struct * | i2 | |||
) | [static] |
interfaces.c の 354 行で定義されています。
参照先 iface_struct::ip・iface_struct::name・iface_struct::netmask.
参照元 get_interfaces().
00355 { 00356 int r; 00357 r = strcmp(i1->name, i2->name); 00358 if (r) return r; 00359 r = ntohl(i1->ip.s_addr) - ntohl(i2->ip.s_addr); 00360 if (r) return r; 00361 r = ntohl(i1->netmask.s_addr) - ntohl(i2->netmask.s_addr); 00362 return r; 00363 }
int get_interfaces | ( | struct iface_struct * | ifaces, | |
int | max_interfaces | |||
) |
interfaces.c の 368 行で定義されています。
参照先 _get_interfaces()・iface_comp().
参照元 get_my_ip_address()・interfaces_changed()・is_myname_or_ipaddr()・load_interfaces()・main().
00369 { 00370 int total, i, j; 00371 00372 total = _get_interfaces(ifaces, max_interfaces); 00373 if (total <= 0) return total; 00374 00375 /* now we need to remove duplicates */ 00376 qsort(ifaces, total, sizeof(ifaces[0]), QSORT_CAST iface_comp); 00377 00378 for (i=1;i<total;) { 00379 if (iface_comp(&ifaces[i-1], &ifaces[i]) == 0) { 00380 for (j=i-1;j<total-1;j++) { 00381 ifaces[j] = ifaces[j+1]; 00382 } 00383 total--; 00384 } else { 00385 i++; 00386 } 00387 } 00388 00389 return total; 00390 }
int main | ( | void | ) |
interfaces.c の 396 行で定義されています。
参照先 get_interfaces()・iface_struct::ip・iface_struct::netmask・printf().
00397 { 00398 struct iface_struct ifaces[MAX_INTERFACES]; 00399 int total = get_interfaces(ifaces, MAX_INTERFACES); 00400 int i; 00401 00402 printf("got %d interfaces:\n", total); 00403 if (total <= 0) exit(1); 00404 00405 for (i=0;i<total;i++) { 00406 printf("%-10s ", ifaces[i].name); 00407 printf("IP=%s ", inet_ntoa(ifaces[i].ip)); 00408 printf("NETMASK=%s\n", inet_ntoa(ifaces[i].netmask)); 00409 } 00410 return 0; 00411 }