Menu Home

linux c获取系统网卡信息

OS:ubuntu 12.04

int getNetCardCount()
{
    int nCount = 0;
    FILE *file = fopen("/proc/net/dev", "r");
    if(!file)
    {   
        debug("Open /proc/net/dev failed by %s !\n", strerror(errno));
        return nCount;
    }   

    char szLine[512];
    fgets(szLine, sizeof(szLine), file);
    fgets(szLine, sizeof(szLine), file);

    while(fgets(szLine, sizeof(szLine), file))
    {   
        char szName[128] = {0};
        sscanf(szLine, "%s", szName);
        int nLen = strlen(szName);
        if(nLen <= 0)
            continue;
        if(szName[nLen - 1] == ':')
            szName[nLen - 1] = 0;
        if(strcmp(szName, "lo") == 0)
            continue;
        nCount++;
    }   

    fclose(file);
    file = NULL;
    return nCount;
}

void dispNetInfo(const char *szDevName, struct netCardInfo *netCardInfo)
{
    int s = socket(AF_INET, SOCK_DGRAM, 0);
    if(s < 0)
    {
        debug("Create socket failed by %s!\n", strerror(errno));
        return;
    }
    struct ifreq ifr;
    unsigned char mac[6];
    unsigned long nIP, nNetmask, nBroadIP;

    strcpy(netCardInfo->devName, szDevName);
    printf("%s\n", szDevName);

    strcpy(ifr.ifr_name, szDevName);
    if(ioctl(s, SIOCGIFHWADDR, &ifr) < 0)
    {
        return;
    }
    memcpy(netCardInfo->mac, ifr.ifr_hwaddr.sa_data, sizeof(mac));
    //fprintf(stderr, "\tMAC:%02x-%02x-%02x-%02x-%02x-%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

    strcpy(ifr.ifr_name, szDevName);
    if(ioctl(s, SIOCGIFADDR, &ifr) < 0)
        nIP = 0;
    else
        nIP = *(unsigned long*)&ifr.ifr_broadaddr.sa_data[2];

    strcpy(netCardInfo->IP,  inet_ntoa(*(struct in_addr*)&nIP));

    //fprintf(stderr, "\tIP:%s\n", inet_ntoa(*(struct in_addr*)&nIP));

    strcpy(ifr.ifr_name, szDevName);
    if(ioctl(s, SIOCGIFBRDADDR, &ifr) < 0)
        nBroadIP = 0;
    else
        nBroadIP = *(unsigned long*)&ifr.ifr_broadaddr.sa_data[2];
    strcpy(netCardInfo->broadIp, inet_ntoa(*(struct in_addr*)&nBroadIP));
    //fprintf(stderr, "\tBroadIP:%s\n", inet_ntoa(*(struct in_addr*)&nBroadIP));

   strcpy(ifr.ifr_name, szDevName);
    if(ioctl(s, SIOCGIFNETMASK, &ifr) < 0)
        nNetmask = 0;
    else
        nNetmask = *(unsigned long*)&ifr.ifr_netmask.sa_data[2];
    strcpy(netCardInfo->netMask, inet_ntoa(*(struct in_addr*)&nNetmask));
    //fprintf(stderr, "\tNetmask:%s\n", inet_ntoa(*(struct in_addr*)&nNetmask));

    strcpy(ifr.ifr_name, szDevName);
    if(ioctl(s, SIOCGIFMTU, &ifr) < 0)
        netCardInfo->mtu = 0;
    else
        netCardInfo->mtu = ifr.ifr_mtu;
        //fprintf(stderr, "\tMTU:%d", ifr.ifr_mtu);

    close(s);
}

void dispActiveAdap(struct netInfo *netInfo)
{
    int fd;
    int count;
    struct ifconf ifc;
    struct ifreq ifr[512];

    if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        debug("socket error by %s\n", strerror(errno));
        return;
    }
    bzero(&ifc, sizeof(ifc));
    bzero(ifr, sizeof(ifr));
    ifc.ifc_len = sizeof(ifr);
    ifc.ifc_req = ifr;
    if(ioctl(fd, SIOCGIFCONF, &ifc) < 0)
    {
        debug("ioctl error by %s\n", strerror(errno));
        return;
    }
    count = ifc.ifc_len / sizeof(struct ifreq);
    int i;
    for(i = 0; i < count; i++)
    {
        struct ifreq *req = &ifr[i];
        if ((strcmp(req->ifr_name, "lo") != 0))
        {
            dispNetInfo(req->ifr_name, &(netInfo->cardInfo[i]));
        }
    }
    close(fd);
    return ;
}

void getDNS(struct netInfo *netInfo)
{
    int i;
    if(res_init() < 0)
    {
        debug("res_init error by %s\n", strerror(errno));
        return;
    }
    for(i = 0; i < _res.nscount; i++)
    {
        struct sockaddr *server = (struct sockaddr*)&_res.nsaddr_list[i];
        strcpy(netInfo->dns[i], inet_ntoa(*(struct in_addr*)&(server->sa_data[2])));
        //fprintf(stderr, "DNS%d:%s\n",i+1, inet_ntoa(*(struct in_addr*)&(server->sa_data[2])));
    }
}

Categories: C/C++ linux 网络

Tagged as:

lnmcc

Leave a Reply

Your email address will not be published.