Exemple en C pour avoir un aperçu des peripheriques reseaux


#1

petite source simple qui affiche le reseau…gcc src.c -o src -W

#include <stdio.h> /* printf, stderr… /
#include <stdlib.h> /
malloc /
#include <string.h> /
memcpy, strcpy…/
#include <net/if.h> /
if_name, ifa_addr /
#include <linux/ioctl.h> /
ioctl /
#include <netinet/in.h> /
sockaddr /
#include <arpa/inet.h> /
inet_ntoa /
#include <sys/ioctl.h> /
ioctl */

int main(int argc, char *argv[])
{
struct ifconf ifc;
struct ifreq *ifr;
struct sockaddr_in *addr;
int s, len, i;
char *buf;

    s = socket(AF_INET, SOCK_DGRAM, 0);

    len = 10 * sizeof(struct ifreq);
    buf = malloc(len);

    ifc.ifc_len = len;
    ifc.ifc_buf = buf;

    ioctl(s, SIOCGIFCONF, &ifc);

    ifr = (struct ifreq*) buf;

    for(i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++)
    {
            addr = (struct sockaddr_in *) &ifr->ifr_addr;             printf("Name:%s\t\tAdresse:%s\n",ifr->ifr_name,inet_ntoa(addr->sin_addr));
            ifr++;
    }

    return 0;

}


#2

Merci pour cette source et je te conseil d’utiliser les balises codes pour poster tes sources c’est plus lisibles

[code]#include <stdio.h> /* printf, stderr… /
#include <stdlib.h> /
malloc /
#include <string.h> /
memcpy, strcpy…/
#include <net/if.h> /
if_name, ifa_addr /
#include <linux/ioctl.h> /
ioctl /
#include <netinet/in.h> /
sockaddr /
#include <arpa/inet.h> /
inet_ntoa /
#include <sys/ioctl.h> /
ioctl */

int main(int argc, char *argv[])
{
struct ifconf ifc;
struct ifreq *ifr;
struct sockaddr_in *addr;
int s, len, i;
char *buf;

s = socket(AF_INET, SOCK_DGRAM, 0);

len = 10 * sizeof(struct ifreq);
buf = malloc(len);

ifc.ifc_len = len;
ifc.ifc_buf = buf;

ioctl(s, SIOCGIFCONF, &ifc);

ifr = (struct ifreq*) buf;

for(i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++)
{
addr = (struct sockaddr_in *) &ifr->ifr_addr; printf(“Name:%s\t\tAdresse:%s\n”,ifr->ifr_name,inet_ntoa(addr->sin_addr));
ifr++;
}

return 0;
}[/code]