linux 工具函数之网络工具函数



**本地IP、子网掩码、网关、MAC地址 获取方法 **

#include <stdio.h>      
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h> 
#include <string.h> 
#include <arpa/inet.h>
#include <net/if.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <linux/sockios.h>

char* getLocalIp( )  
&#123;  
    int sock_get_ip;  
    char ipaddr[50];  

    struct   sockaddr_in *sin;  
    struct   ifreq ifr_ip;     

    if ((sock_get_ip=socket(AF_INET, SOCK_STREAM, 0)) == -1)  
    &#123;  
         printf("socket create failse...GetLocalIp!\n");  
         return NULL;  
    &#125;  

    memset(&ifr_ip, 0, sizeof(ifr_ip));    
    //指定网卡为 eth0 
    strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name) - 1);     

    if( ioctl( sock_get_ip, SIOCGIFADDR, &ifr_ip) < 0 )     
    &#123;     
         return NULL;     
    &#125;       
    sin = (struct sockaddr_in *)&ifr_ip.ifr_addr;     
    strcpy(ipaddr,inet_ntoa(sin->sin_addr));         

    printf("local net adapter:[ %-5s ] ip_v4 [ %-20s ]\n", "eth0", ipaddr);    
    close( sock_get_ip );  

    return NULL;  
&#125;  
void getlocalIp2()
&#123;
    struct ifaddrs * ifAddrStruct=NULL;  
    void * tmpAddrPtr=NULL;  

    getifaddrs(&ifAddrStruct);  

   //获取所有网卡的ip
    while (ifAddrStruct!=NULL)   
    &#123;  
        if (ifAddrStruct->ifa_addr->sa_family==AF_INET)  
        &#123;   // check it is IP4  
            // is a valid IP4 Address  
            tmpAddrPtr = &((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;  
            char addressBuffer[INET_ADDRSTRLEN];  
            inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);  
            printf("local net adapter:[ %-5s ] ip_v4 [ %-20s ]\n", ifAddrStruct->ifa_name, addressBuffer);   
        &#125;  
        else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6)  
        &#123;   // check it is IP6  
            // is a valid IP6 Address  
            tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;  
            char addressBuffer[INET6_ADDRSTRLEN];  
            inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);  
            printf("local net adapter:[ %-5s ] ip_v6 [ %-20s ]\n", ifAddrStruct->ifa_name, addressBuffer);   
        &#125;   
        ifAddrStruct = ifAddrStruct->ifa_next;  
    &#125;  
    return 0; 
&#125;
int SetLocalIp( const char *ipaddr )  
&#123;  

    int sock_set_ip;  

    struct sockaddr_in sin_set_ip;  
    struct ifreq ifr_set_ip;  

    bzero( &ifr_set_ip,sizeof(ifr_set_ip));  

    if( ipaddr == NULL )  
        return -1;  

    if(sock_set_ip = socket( AF_INET, SOCK_STREAM, 0 ) == -1);  
    &#123;  
        perror("socket create failse...SetLocalIp!\n");  
        return -1;  
    &#125;  

    memset( &sin_set_ip, 0, sizeof(sin_set_ip));  
    strncpy(ifr_set_ip.ifr_name, "eth0", sizeof(ifr_set_ip.ifr_name)-1);     

    sin_set_ip.sin_family = AF_INET;  
    sin_set_ip.sin_addr.s_addr = inet_addr(ipaddr);  
    memcpy( &ifr_set_ip.ifr_addr, &sin_set_ip, sizeof(sin_set_ip));  

    if( ioctl( sock_set_ip, SIOCSIFADDR, &ifr_set_ip) < 0 )  
    &#123;  
        perror( "Not setup interface\n");  
        return -1;  
    &#125;  

    //设置激活标志  
    ifr_set_ip.ifr_flags |= IFF_UP |IFF_RUNNING;  

    //get the status of the device  
    if( ioctl( sock_set_ip, SIOCSIFFLAGS, &ifr_set_ip ) < 0 )  
    &#123;  
         perror("SIOCSIFFLAGS");  
         return -1;  
    &#125;  

    close( sock_set_ip );  
    return 0;  
&#125;  
void getLocalNetMask()  
&#123;  
    int sock_netmask;  
    char netmask_addr[50];  

    struct ifreq ifr_mask;  
    struct sockaddr_in *net_mask;  

    sock_netmask = socket( AF_INET, SOCK_STREAM, 0 );  
    if( sock_netmask == -1)  
    &#123;  
        perror("create socket failture...GetLocalNetMask\n");  
        return ;  
    &#125;  

    memset(&ifr_mask, 0, sizeof(ifr_mask));    

    /* 获取指定网卡的子网掩码 */
    strncpy(ifr_mask.ifr_name, "eth0", sizeof(ifr_mask.ifr_name )-1);     

    if( (ioctl( sock_netmask, SIOCGIFNETMASK, &ifr_mask ) ) < 0 )   
    &#123;  
        printf("mac ioctl error\n");  
        return ;  
    &#125;  

    net_mask = ( struct sockaddr_in * )&( ifr_mask.ifr_netmask );  
    strcpy( netmask_addr, inet_ntoa( net_mask -> sin_addr ) );  

    printf("local net adapter:[ %-5s ] mask  [ %-20s ]\n","eth0",netmask_addr);      

    close( sock_netmask );  
    return ;  
&#125;
void getLocalMac()  
&#123;  
    int sock_mac;  

    struct ifreq ifr_mac;  
    char mac_addr[30];     

    sock_mac = socket( AF_INET, SOCK_STREAM, 0 );  
    if( sock_mac == -1)  
    &#123;  
        perror("create socket falise...mac\n");  
        return ;  
    &#125;  

    memset(&ifr_mac,0,sizeof(ifr_mac));     
    strncpy(ifr_mac.ifr_name, "eth0", sizeof(ifr_mac.ifr_name)-1);     

    if( (ioctl( sock_mac, SIOCGIFHWADDR, &ifr_mac)) < 0)  
    &#123;  
        printf("mac ioctl error\n");  
        return ;  
    &#125;  

    sprintf(mac_addr,"%02x%02x%02x%02x%02x%02x",  
            (unsigned char)ifr_mac.ifr_hwaddr.sa_data[0],  
            (unsigned char)ifr_mac.ifr_hwaddr.sa_data[1],  
            (unsigned char)ifr_mac.ifr_hwaddr.sa_data[2],  
            (unsigned char)ifr_mac.ifr_hwaddr.sa_data[3],  
            (unsigned char)ifr_mac.ifr_hwaddr.sa_data[4],  
            (unsigned char)ifr_mac.ifr_hwaddr.sa_data[5]);  

    printf("local net adapter:[ %-5s ] mac   [ %-20s ]\n","eth0",mac_addr);      

    close( sock_mac );  
    return ;  
&#125;   
void getGateWay()  
&#123;  
    FILE *fp;  
    char buf[512];  
    char cmd[128];  
    char gateway[30];  
    char *tmp;  

    strcpy(cmd, "ip route");  
    fp = popen(cmd, "r");  
    if(NULL == fp)  
    &#123;  
        perror("popen error");  
        return ;  
    &#125;  
    while(fgets(buf, sizeof(buf), fp) != NULL)  
    &#123;  
        tmp =buf;  
        while(*tmp && isspace(*tmp))  
            ++ tmp;  
        if(strncmp(tmp, "default", strlen("default")) == 0)  
            break;  
    &#125;  
    sscanf(buf, "%*s%*s%s", gateway);         
    printf("default gateway:%s\n", gateway);  
    pclose(fp);  

    return ;  
&#125;  
void main( )
&#123;
    getLocalIp( ) ;
    getlocalIp2( );
    getLocalNetMask();
    getLocalMac();
    getGateWay();
&#125;

运行结果:
net.png


文章作者: Alex.Lin
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Alex.Lin !
 上一篇
Lua之 loadfile ,dofile, loadstring,require Lua之 loadfile ,dofile, loadstring,require
loadfile——只编译,不运行   1.功能:载入文件但不执行代码块,对于相同的文件每次都会执行。只是编译代码,然后将编译结果作为一个函数返回  2.调用:loadfile(“filename”)  3.错误处理:不引发错误
2017-03-26 Alex.Lin
下一篇 
STM8 命名规则 STM8 命名规则
STM8命名规则示列:STM8S005K6T6Cxxx代表的意义为: 超值型 STM8内核(可以理解为8位51增强型内核)MCU,LQFP-32封装,32KB FLASH容量,温度范围-40℃-85℃;(工业级)具体分解如下:
2017-03-26 Alex.Lin
  目录