Vot novaja modifikacija etogo koda, kotoraja ne tol'ko poluchaet spisok
interfeisov, no I IP adres, masku I tip dlja kazdogo interfeisa.
Poprobujte na raznih operacionkah. Esli ne budet rabotat', to bilo bi
zdorovo, esli bi kto-nibud' razobralsja, kak sdelat', chtobi etot kod
rabotal.
Viktor
-----------
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
int main(int argc, char *argv[])
{
struct if_nameindex *p;
struct ifreq ifrq;
struct sockaddr_in *sa;
int i, fd;
p = if_nameindex();
if (p != NULL)
{
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1)
{
perror("socket");
return 1;
}
for(i = 0; p[i].if_index != 0; i++)
{
printf("%d %s ", p[i].if_index, p[i].if_name);
strcpy(ifrq.ifr_name, p[i].if_name);
if (ioctl(fd, SIOCGIFADDR, &ifrq) == 0)
{
sa = (struct sockaddr_in *)&ifrq.ifr_addr;
printf("%s ", inet_ntoa(sa->sin_addr));
}
if (ioctl(fd, SIOCGIFNETMASK, &ifrq) == 0)
{
sa = (struct sockaddr_in *)&ifrq.ifr_addr;
printf("%s ", inet_ntoa(sa->sin_addr));
}
if (ioctl(fd, SIOCGIFHWADDR, &ifrq) == 0)
{
printf("%d ", ifrq.ifr_hwaddr.sa_family);
}
printf("\n");
}
close(fd);
if_freenameindex(p);
}
else
{
perror("if_nameindex");
}
return 0;
}
Received on Tue May 18 2004 - 10:17:38 EEST
This archive was generated by hypermail 2.2.0 : Mon Aug 01 2005 - 00:43:15 EEST