zoukankan      html  css  js  c++  java
  • ioctl()获取本地网卡设备信息

    获得eth0接口所有信息:

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <sys/types.h>
    4. #include <sys/stat.h>
    5. #include <unistd.h>
    6. #include <sys/ioctl.h>
    7. #include <sys/socket.h>
    8. #include <netinet/in.h>
    9. #include <arpa/inet.h>
    10. #include <net/if.h>
    11. #include <string.h>
    12. unsigned char g_eth_name[16];
    13. unsigned char g_macaddr[16];
    14. unsigned int g_subnetmask;
    15. unsigned int g_ipaddr;
    16. unsigned int g_broadcast_ipaddr;
    17. //初始化网络,获取当前网络设备信息
    18. void init_net(void)
    19. {
    20.     int i ;
    21.     int sock;
    22.     struct sockaddr_in sin;
    23.     struct ifreq ifr;
    24.     
    25.     sock=socket(AF_INET, SOCK_DGRAM, 0);
    26.     if(sock==-1)
    27.         perror("socket");
    28.     strcpy(g_eth_name, "eth0");
    29.     strcpy(ifr.ifr_name, g_eth_name);
    30.     printf("eth name: %s ", g_eth_name);
    31.     //获取并打印网卡地址
    32.     if(ioctl(sock, SIOCGIFHWADDR, &ifr) <0 )
    33.         perror("ioctl error ");
    34.     memcpy(g_macaddr, ifr.ifr_hwaddr.sa_data, 6);
    35.     printf("local mac: ");
    36.     for(i=0;i<5;i++)
    37.         printf("%.2x:", g_macaddr[i]);
    38.     printf("%.2x ", g_macaddr[i]);
    39.     
    40.     //获取并打印IP地址
    41.     if(ioctl(sock, SIOCGIFADDR, &ifr)<0)
    42.         perror("ioctl error ");
    43.     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    44.     g_ipaddr = sin.sin_addr.s_addr;
    45.     printf("local eth0: %s ", inet_ntoa(sin.sin_addr));
    46.     
    47.     //获取并打印广播地址
    48.     if(ioctl(sock, SIOCGIFBRDADDR, &ifr)<0)
    49.         perror("ioctl error ");
    50.     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    51.     g_broadcast_ipaddr = sin.sin_addr.s_addr;
    52.     printf("broadcast: %s ", inet_ntoa(sin.sin_addr));
    53.     
    54.     //获取并打印子网掩码
    55.     if(ioctl(sock,SIOCGIFNETMASK,&ifr)<0)
    56.         perror("ioctl error ");
    57.     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    58.     g_subnetmask = sin.sin_addr.s_addr;
    59.     printf("subnetmask: %s ", inet_ntoa(sin.sin_addr));
    60.     
    61.     close(sock); 
    62. }
    63. int main()
    64. {
    65.     //initialize
    66.     init_net();
    67.     //do something
    68.     //....
    69.     return 0;
    70. }

    获取所有IP:

    1. #include <string.h>
    2. #include <sys/ioctl.h>
    3. #include <sys/socket.h>
    4. #include <stdlib.h>
    5. #include <stdio.h>
    6. #include <linux/if.h>
    7. #include <arpa/inet.h>
    8. int main()
    9. {
    10.     int i=0;
    11.     int sockfd;
    12.     struct ifconf ifconf;
    13.     unsigned char buf[512];
    14.     struct ifreq *ifreq;
    15.     //初始化ifconf
    16.     ifconf.ifc_len = 512;
    17.     ifconf.ifc_buf = buf;
    18.     
    19.     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    20.     {
    21.         perror("socket open failure! " );
    22.         exit(1);
    23.     }
    24.     
    25.     ioctl(sockfd, SIOCGIFCONF, &ifconf); //获取所有接口信息
    26.     //接下来一个一个的获取IP地址
    27.     ifreq = (struct ifreq*)buf;
    28.     for (i=(ifconf.ifc_len/sizeof (struct ifreq)); i>0; i--)
    29.     {
    30.         if(ifreq->ifr_flags == AF_INET)
    31.         {
    32.             printf("name = [%s] " , ifreq->ifr_name);
    33.             printf("local addr = [%s]",
    34.                 inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
    35.             printf();
    36.         ifreq++;
    37.         }
    38.     }
    39.     return 0;
    40. }
    获取本机的IP和MAC地址:
    1. /* 得到本机的mac地址和ip地址 */
    2. int GetLocalMac ( const char *device,char *mac,char *ip )
    3. {
    4.     int sockfd;
    5.     struct ifreq req;
    6.     struct sockaddr_in * sin;
    7.     if ( ( sockfd = socket ( PF_INET,SOCK_DGRAM,0 ) ) ==-1 )
    8.     {
    9.         fprintf ( stderr,"Sock Error:%s a",strerror ( errno ) );
    10.         return ( -1 );
    11.     }
    12.     memset ( &req,0,sizeof ( req ) );
    13.     strcpy ( req.ifr_name,device );
    14.     if ( ioctl ( sockfd,SIOCGIFHWADDR, ( char * ) &req ) ==-1 )
    15.     {
    16.         fprintf ( stderr,"ioctl SIOCGIFHWADDR:%s a",strerror ( errno ) );
    17.         close ( sockfd );
    18.         return ( -1 );
    19.     }
    20.     memcpy ( mac,req.ifr_hwaddr.sa_data,6 );
    21.     req.ifr_addr.sa_family = PF_INET;
    22.     if ( ioctl ( sockfd,SIOCGIFADDR, ( char * ) &req ) ==-1 )
    23.     {
    24.         fprintf ( stderr,"ioctl SIOCGIFADDR:%s a",strerror ( errno ) );
    25.         close ( sockfd );
    26.         return ( -1 );
    27.     }
    28.     sin = ( struct sockaddr_in * ) &req.ifr_addr;
    29.     memcpy ( ip, ( char * ) &sin->sin_addr,4 );
    30.     return ( 0 );
    31. }
  • 相关阅读:
    jq判断鼠标滚轴向上滚动还是向下滚动
    Directory 中user Var 如何添加到通道变量中?
    Centos 6.5 freeswitch 编译mod_shout
    golang esl api
    opensips redis配置记录
    luasocket 安装记录 (FS1.4)
    luasocket 安装记录 (FS1.6)
    Callcenter 模块解析
    OpenSIPS 1.11.1安装记录
    阿里服务器挂载数据盘
  • 原文地址:https://www.cnblogs.com/lidabo/p/5344830.html
Copyright © 2011-2022 走看看