Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3023979
  • 博文数量: 272
  • 博客积分: 5544
  • 博客等级: 大校
  • 技术积分: 5496
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 00:48
个人简介

  每个人都要有一个骨灰级的爱好,不为金钱,而纯粹是为了在这个领域享受追寻真理的快乐。

文章分类

全部博文(272)

文章存档

2015年(2)

2014年(5)

2013年(25)

2012年(58)

2011年(182)

分类: LINUX

2012-06-22 12:44:13

获得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:\t%s\n", g_eth_name);

  31.     //获取并打印网卡地址
  32.     if(ioctl(sock, SIOCGIFHWADDR, &ifr) <0 )
  33.         perror("ioctl error\n");
  34.     memcpy(g_macaddr, ifr.ifr_hwaddr.sa_data, 6);
  35.     printf("local mac:\t");
  36.     for(i=0;i<5;i++)
  37.         printf("%.2x:", g_macaddr[i]);
  38.     printf("%.2x\n", g_macaddr[i]);
  39.     
  40.     //获取并打印IP地址
  41.     if(ioctl(sock, SIOCGIFADDR, &ifr)<0)
  42.         perror("ioctl error\n");
  43.     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
  44.     g_ipaddr = sin.sin_addr.s_addr;
  45.     printf("local eth0:\t%s\n", inet_ntoa(sin.sin_addr));
  46.     
  47.     //获取并打印广播地址
  48.     if(ioctl(sock, SIOCGIFBRDADDR, &ifr)<0)
  49.         perror("ioctl error\n");
  50.     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
  51.     g_broadcast_ipaddr = sin.sin_addr.s_addr;
  52.     printf("broadcast:\t%s\n", inet_ntoa(sin.sin_addr));
  53.     
  54.     //获取并打印子网掩码
  55.     if(ioctl(sock,SIOCGIFNETMASK,&ifr)<0)
  56.         perror("ioctl error\n");
  57.     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
  58.     g_subnetmask = sin.sin_addr.s_addr;
  59.     printf("subnetmask:\t%s\n", 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!\n" );
  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\n\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\n\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\n\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. }

阅读(7675) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~