linux下c语言获取本机ip地址

linux下c语言获取本机ip地址

struct ifconf这个结构是在哪个头文件定义
#include <net/if.h>
其实你可以

find /usr/include/ -name "*.h" -exec grep -l "ifconf" {} \; 
(  grep  -l或--file-with-matches   列出文件内容符合指定的范本样式的文件名称。 )

linux下c语言获取本机ip地址的函数,解除了只能获取127.0.0.1的bug.获得的是大端的4字节的ip地址,不是17字节的字符串地址.本人项目中已经在使用了

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>

#include <sys/ioctl.h>
#include < linux/if.h>

long getlocalhostip(){
  int  MAXINTERFACES=16;
long ip;
int fd, intrface, retn = 0;
  struct ifreq buf[MAXINTERFACES]; ///if.h
struct ifconf ifc; ///if.h
ip = -1;
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0) //socket.h
{
  ifc.ifc_len = sizeof buf;
  ifc.ifc_buf = (caddr_t) buf;
  if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) //ioctl.h
{
  intrface = ifc.ifc_len / sizeof (struct ifreq); 
while (intrface-- > 0)
  {
  if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
  {
  ip=inet_addr( inet_ntoa( ((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr) );//types
  break;
}
           
}
}
  close (fd);
  }
return ip;
}
union ipu{
long ip;
unsigned char ipchar[4];
};

int main(int argc, char **argv){
union ipu iptest;
iptest.ip = getlocalhostip();
printf("local ip:%x :%3u.%3u.%3u.%3u \n",iptest.ip, iptest.ipchar[0],iptest.ipchar[1],iptest.ipchar[2],iptest.ipchar[3]);
return 0;
}

用ioctl获得本地ip地址时要用到两个结构体ifconf和ifreq,它们对于大多数人
来说都是比较陌生的,这里给大家一种比较简单的理解方法,当然只一种帮助
理解的方法,在描述中可能会有一些地方与真实定义有所出入,仅供参考.

首先先认识一下ifconf和ifreq:

//ifconf通常是用来保存所有接口信息的
//if.h

struct ifconf 
{
    int ifc_len;                       /* size of buffer    */
    union {
        char *ifcu_buf;                /* input from user->kernel*/
        struct ifreq *ifcu_req;        /* return from kernel->user*/
    } ifc_ifcu;
};
#define ifc_buf ifc_ifcu.ifcu_buf        /* buffer address    */
#define ifc_req ifc_ifcu.ifcu_req        /* array of structures    */

//ifreq用来保存某个接口的信息
//if.h
struct
ifreq
{
   char ifr_name[IFNAMSIZ];
   union {
      struct sockaddr ifru_addr;
      struct sockaddr ifru_dstaddr;
      struct sockaddr ifru_broadaddr;
      short ifru_flags;
      int ifru_metric;
      caddr_t ifru_data;
      }ifr_ifru;
};
#define ifr_addr ifr_ifru.ifru_addr
#define ifr_dstaddr ifr_ifru.ifru_dstaddr
#define ifr_broadaddr ifr_ifru.ifru_broadaddr
上边这两个结构看起来比较复杂,我们现在把它们简单化一些:比如说现在我们向实现获得本地IP的功能。

我们的做法是:
1. 先通过ioctl获得本地所有接口的信息,并保存在ifconf中
2. 再从ifconf中取出每一个ifreq中表示ip地址的信息

具体使用时我们可以认为ifconf就有两个成员:
ifc_len 和 ifc_buf, 如图一所示:    

ifc_len:表示用来存放所有接口信息的缓冲区长度
ifc_buf:表示存放接口信息的缓冲区

所以我们需要在程序开始时对ifconf的ifc_len和ifc_buf进行初始化 
接下来使用ioctl获取所有接口信息,完成后ifc_len内存放实际获得的借口信息总长度
并且信息被存放在ifc_buf中。 
如下图示:(假设读到两个接口信息)

接下来我们只需要从一个一个的接口信息获取ip地址信息即可。

下面有一个简单的参考:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/if.h>
#Include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
main()
{
    int i=0;
    int sockfd;
    struct ifconf ifconf;
    unsigned char buf[512];
    struct ifreq *ifreq;    //初始化ifconf
    ifconf.ifc_len = 512;
    ifconf.ifc_buf = buf;
    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)
    {
        perror("socket");
        exit(1);
     }
     ioctl(sockfd, SIOCGIFCONF, &ifconf);    //获取所有接口信息
     
    //接下来一个一个的获取IP地址
    ifreq = (struct ifreq*)buf;
    for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--)
    {
        if(ifreq->ifr_flags == AF_INET){
        //for ipv4
        printf("name = [%s]\n", ifreq->ifr_name);
        printf("local addr = [%s]\n",inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
        ifreq++;
     }
    return 0;
}


---------------------------------------------------

Linux C 获取本机IP地址的方法,排除127.0.0.1
发表于 2011年06月30日 由 温泉

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

//获取地址
//返回IP地址字符串
//返回:0=成功,-1=失败
int getlocalip(char* outip)
{
int i=0;
int sockfd;
struct ifconf ifconf;
char buf[512];
struct ifreq *ifreq;
char* ip;
//初始化ifconf
ifconf.ifc_len = 512;
ifconf.ifc_buf = buf;

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)
{
return -1;
}
ioctl(sockfd, SIOCGIFCONF, &ifconf);    //获取所有接口信息
close(sockfd);
//接下来一个一个的获取IP地址
ifreq = (struct ifreq*)buf;
for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i–)
{
ip = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr);

if(strcmp(ip,”127.0.0.1″)==0)  //排除127.0.0.1,继续下一个
{
ifreq++;
continue;
}
strcpy(outip,ip);
return 0;
}

return -1;
}
//——————————-函数的调用方式————————————-

char ip[20];

if ( getlocalip( ip ) == 0 )
{
printf( “ 本机IP地址是: %s\n”, ip );
}
else
{
printf( ” 无法获取本机IP地址  ” );
}


----------------------------

ifconf和ifreq 
 

用ioctl获得本地ip地址时要用到两个结构体ifconf和ifreq,它们对于大多数人来说都是比较陌生的,这里给一种比较简单的理解方法, 仅供参考.

首先先认识一下ifconf和ifreq:

//ifconf通常是用来保存所有接口信息的 //ifreq用来保存某个接口的信息//if.hstruct ifreq {char ifr_name[IFNAMSIZ];union {struct sockaddr ifru_addr;struct sockaddr ifru_dstaddr;struct sockaddr ifru_broadaddr;short ifru_flags;int ifru_metric;caddr_t ifru_data;} ifr_ifru;};#define ifr_addr ifr_ifru.ifru_addr#define ifr_dstaddr ifr_ifru.ifru_dstaddr#define ifr_broadaddr ifr_ifru.ifru_broadaddr

 

上边这两个结构看起来比较复杂,我们现在把它们简单化一些:比如说现在我们向实现获得本地IP的功能。

我们的做法是:
1. 先通过ioctl获得本地所有接口的信息,并保存在ifconf中
2. 再从ifconf中取出每一个ifreq中表示ip地址的信息

具体使用时我们可以认为ifconf就有两个成员:ifc_len 和 ifc_buf,
如图一所示:    


 

ifc_len:表示用来存放所有接口信息的缓冲区长度
ifc_buf:表示存放接口信息的缓冲区

所以我们需要在程序开始时对ifconf的ifc_len和ifc_buf进行初始化
接下来使用ioctl获取所有接口信息,完成后ifc_len内存放实际获得的接口信息总长度,并且信息被存放在ifc_buf中。
如下图示:(假设读到两个接口信息)


 

接下来我们只需要从一个一个的接口信息获取ip地址信息即可。

下面有一个简单的参考:

#include#include#include#include#include#include#include#includeint main(void){int i=0;int sockfd;struct ifconf ifconf;unsigned char buf[512];struct ifreq *ifreq; //初始化ifconfifconf.ifc_len = 512;ifconf.ifc_buf = buf;if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0){perror("socket");exit(1);}ioctl(sockfd, SIOCGIFCONF, &ifconf); //获取所有接口信息//逐个获取IP地址ifreq = (struct ifreq*)buf;for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--){//if(ifreq->ifr_flags == AF_INET){//for ipv4printf("name = [%s]n", ifreq->ifr_name);printf("local addr = [%s]n",inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));ifreq++;//}}return 0;}
---------------------------------------------------------------------------

获得Unix/Linux系统中的IP、MAC地址等信息

实 际环境和特殊需求往往会将简单问题复杂化,比如计算机IP地址,对于一个连接中socket,可以直接获得本端和对端的IP、端口信息。但在一些特殊场合 我们可能需要更多的信息,比如系统中有几块网卡,他们的Mac地址是多少,每块网卡分配了几个IP(一个网卡对应多个IP)等等。

这些信息往往需要通过ifconfig指令来获得,对于程序员来说,在代码中调用外部的shell指令可不是个最佳方案,因为没人能保障不同平台、不同版本的ifconfig指令输出的格式是一致的。本篇文章中将介绍通过ioctl函数实现上述需求。

 

#include <sys/ioctl.h>
int ioctl(int fd, int request, … /* void *arg */);
返回:成功返回0,失败返回-1

ioctl函数的参数只有3个,但却是Unix中少有的几个“家族类”复杂函数,这里摘录一段《Unix网络编程》一书中对ioctl函数的描述:

在传统上ioctl函数是用于那些普遍使用、但不适合归入其他类别的任何特殊的系统接口……网络程序(一般是服务器程序)中ioctl常用于在程序启动时获得主机上所有接口的信息:接口的地址、接口是否支持广播、是否支持多播,等等。

ioctl 函数的第一个参数fd,可以表示一个打开的文件(文件句柄)或网络套接字,第二个和第三个参数体现了函数的家族特色,参数二request根据函数功能分 类定义了多组宏,而参数三总是一个指针,指针的类型依赖于参数二request。因为ioctl的种类实在太多,这里只列出和本文相关的几个参数定义:

分类参数二(宏)参数三描述
接口SIOCGIFCONFstruct ifconf获得所有接口列表
 SIOCGIFADDRstruct ifreq获得接口地址
 SIOCGIFFLAGSstruct ifreq获得接口标志
 SIOCGIFBRDADDRstruct ifreq获得广播地址
 SIOCGIFNETMASKstruct ifreq获得子网掩码

 

上表中列出了两个相关的结构体:struct ifconf 和 struct ifreq,要了解ioctl函数的具体运用,首先要了解这两个结构:

  1. /* net/if.h */
  2. struct ifconf
  3. {
  4.     int ifc_len;            /* Size of buffer.  */
  5.     union
  6.     {
  7.         __caddr_t ifcu_buf;
  8.         struct ifreq *ifcu_req;
  9.     } ifc_ifcu;
  10. };
  11.  
  12. struct ifreq
  13. {
  14. # define IFHWADDRLEN    6
  15. # define IFNAMSIZ   IF_NAMESIZE
  16.    
  17.     union
  18.     {
  19.         char ifrn_name[IFNAMSIZ];   /* Interface name, e.g. "en0".  */
  20.     } ifr_ifrn;
  21.    
  22.     union
  23.     {
  24.         struct sockaddr ifru_addr;
  25.         struct sockaddr ifru_dstaddr;
  26.         struct sockaddr ifru_broadaddr;
  27.         struct sockaddr ifru_netmask;
  28.         struct sockaddr ifru_hwaddr;
  29.         short int ifru_flags;
  30.         int ifru_ivalue;
  31.         int ifru_mtu;
  32.         struct ifmap ifru_map;
  33.         char ifru_slave[IFNAMSIZ];  /* Just fits the size */
  34.         char ifru_newname[IFNAMSIZ];
  35.         __caddr_t ifru_data;
  36.     } ifr_ifru;
  37. };

struct ifconf的第二个元素ifc_ifcu是一个联合,是指向struct ifreq结构的地址,通常是一组struct ifreq结构空间(每一个描述一个接口),struct ifconf的第一个元素ifc_len描述了struct ifreq结构空间的大小;结构struct ifreq也有两个元素,第一个元素ifr_ifrn内含一个字符串,用来描述接口的名称,比如“eth0″、”wlan0”等,第二个元素是联合,比较 复杂,用来描述套接口的地址结构。

struct ifconf 和 struct ifreq的关系可以参考下图:


 

ioctl函数中的struct ifconf 和 struct ifreq结构关系

通常运用ioctl函数的第一步是从内核获取系统的所有接口,然后再针对每个接口获取其地址信息。获取所有接口通过SIOCGIFCONF请求来实现:

  1. struct ifconf ifc;  /* ifconf结构 */
  2. struct ifreq ifrs[16];  /* ifreq结构数组(这里估计了接口的最大数量16) */
  3.  
  4. /* 初始化ifconf结构 */
  5. ifc.ifc_len = sizeof(ifrs);
  6. ifc.ifc_buf = (caddr_t) ifrs;
  7.  
  8. /* 获得接口列表 */
  9. ioctl(fd, SIOCGIFCONF, (char *) &ifc);

获得了接口列表,就可以通过struct ifconf结构中*ifcu_req的指针得到struct ifreq结构数组的地址,通过遍历获得每隔接口的详细地址信息:

  1. printf("接口名称:%s /n", ifrs[n].ifr_name); /* 接口名称 */
  2.  
  3. /* 获得IP地址 */
  4. ioctl(fd, SIOCGIFADDR, (char *) &ifrs[n]);
  5. printf("IP地址:%s /n",
  6.     (char*)inet_ntoa(((struct sockaddr_in*) (&ifrs[n].ifr_addr))->sin_addr));
  7.  
  8. /* 获得子网掩码 */
  9. ioctl(fd, SIOCGIFNETMASK, (char *) &ifrs[n]);
  10. printf("子网掩码:%s /n",
  11.     (char*)inet_ntoa(((struct sockaddr_in*) (&ifrs[n].ifr_addr))->sin_addr));
  12.  
  13. /* 获得广播地址 */
  14. ioctl(fd, SIOCGIFBRDADDR, (char *) &ifrs[n]);
  15. printf("广播地址:%s /n",
  16.     (char*)inet_ntoa(((struct sockaddr_in*) (&ifrs[n].ifr_addr))->sin_addr));
  17.  
  18. /* 获得MAC地址 */
  19. ioctl(fd, SIOCGIFHWADDR, (char *) &ifrs[n]);
  20. printf("MAC地址:%02x:%02x:%02x:%02x:%02x:%02x /n",
  21.     (unsigned char) ifrs[n].ifr_hwaddr.sa_data[0],
  22.     (unsigned char) ifrs[n].ifr_hwaddr.sa_data[1],
  23.     (unsigned char) ifrs[n].ifr_hwaddr.sa_data[2],
  24.     (unsigned char) ifrs[n].ifr_hwaddr.sa_data[3],
  25.     (unsigned char) ifrs[n].ifr_hwaddr.sa_data[4],
  26.     (unsigned char) ifrs[n].ifr_hwaddr.sa_data[5]);

最后,给出一个参考程序代码。

ioctl函数没有纳入POXIS规范,各系统对ioctl的实现也不尽相同,下面的代码在我的Ubuntu10.04 linux上可执行通过,但在其他Unix系统上不一定能够通过编译,例如在Power AIX 5.3上需要将获得MAC地址的那段代码注释掉。

  1. #include <arpa/inet.h>
  2. #include <net/if.h>
  3. #include <net/if_arp.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <unistd.h>
  9.  
  10. #define MAXINTERFACES 16    /* 最大接口数 */
  11.  
  12. int fd;         /* 套接字 */
  13. int if_len;     /* 接口数量 */
  14. struct ifreq buf[MAXINTERFACES];    /* ifreq结构数组 */
  15. struct ifconf ifc;                  /* ifconf结构 */
  16.  
  17. int main(argc, argv)
  18. {
  19.     /* 建立IPv4的UDP套接字fd */
  20.     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  21.     {
  22.         perror("socket(AF_INET, SOCK_DGRAM, 0)");
  23.         return -1;
  24.     }
  25.  
  26.     /* 初始化ifconf结构 */
  27.     ifc.ifc_len = sizeof(buf);
  28.     ifc.ifc_buf = (caddr_t) buf;
  29.  
  30.     /* 获得接口列表 */
  31.     if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) == -1)
  32.     {
  33.         perror("SIOCGIFCONF ioctl");
  34.         return -1;
  35.     }
  36.  
  37.     if_len = ifc.ifc_len / sizeof(struct ifreq); /* 接口数量 */
  38.     printf("接口数量:%d /n/n", if_len);
  39.  
  40.     while (if_len– > 0) /* 遍历每个接口 */
  41.     {
  42.         printf("接口:%s /n", buf[if_len].ifr_name); /* 接口名称 */
  43.  
  44.         /* 获得接口标志 */
  45.         if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[if_len])))
  46.         {
  47.             /* 接口状态 */
  48.             if (buf[if_len].ifr_flags & IFF_UP)
  49.             {
  50.                 printf("接口状态: UP /n");
  51.             }
  52.             else
  53.             {
  54.                 printf("接口状态: DOWN /n");
  55.             }
  56.         }
  57.         else
  58.         {
  59.             char str[256];
  60.             sprintf(str, "SIOCGIFFLAGS ioctl %s", buf[if_len].ifr_name);
  61.             perror(str);
  62.         }
  63.  
  64.  
  65.         /* IP地址 */
  66.         if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[if_len])))
  67.         {
  68.             printf("IP地址:%s /n",
  69.                     (char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr));
  70.         }
  71.         else
  72.         {
  73.             char str[256];
  74.             sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
  75.             perror(str);
  76.         }
  77.  
  78.         /* 子网掩码 */
  79.         if (!(ioctl(fd, SIOCGIFNETMASK, (char *) &buf[if_len])))
  80.         {
  81.             printf("子网掩码:%s /n",
  82.                     (char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr));
  83.         }
  84.         else
  85.         {
  86.             char str[256];
  87.             sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
  88.             perror(str);
  89.         }
  90.  
  91.         /* 广播地址 */
  92.         if (!(ioctl(fd, SIOCGIFBRDADDR, (char *) &buf[if_len])))
  93.         {
  94.             printf("广播地址:%s /n",
  95.                     (char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr));
  96.         }
  97.         else
  98.         {
  99.             char str[256];
  100.             sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
  101.             perror(str);
  102.         }
  103.  
  104.         /*MAC地址 */
  105.         if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[if_len])))
  106.         {
  107.             printf("MAC地址:%02x:%02x:%02x:%02x:%02x:%02x /n/n",
  108.                     (unsigned char) buf[if_len].ifr_hwaddr.sa_data[0],
  109.                     (unsigned char) buf[if_len].ifr_hwaddr.sa_data[1],
  110.                     (unsigned char) buf[if_len].ifr_hwaddr.sa_data[2],
  111.                     (unsigned char) buf[if_len].ifr_hwaddr.sa_data[3],
  112.                     (unsigned char) buf[if_len].ifr_hwaddr.sa_data[4],
  113.                     (unsigned char) buf[if_len].ifr_hwaddr.sa_data[5]);
  114.         }
  115.         else
  116.         {
  117.             char str[256];
  118.             sprintf(str, "SIOCGIFHWADDR ioctl %s", buf[if_len].ifr_name);
  119.             perror(str);
  120.         }
  121.     }//–while end
  122.  
  123.     //关闭socket
  124.     close(fd);
  125.     return 0;
  126. }

在我的系统上,程序输出:

接口数量:4

接口:wlan0
接口状态: UP
IP地址:192.168.1.142
子网掩码:255.255.255.0
广播地址:192.168.1.255
MAC地址:00:14:a5:65:47:57

接口:eth0:0
接口状态: UP
IP地址:192.168.4.113
子网掩码:255.255.255.0
广播地址:192.168.4.255
MAC地址:00:14:c2:e5:45:57

接口:eth0
接口状态: UP
IP地址:192.168.4.111
子网掩码:255.255.255.0
广播地址:192.168.4.255
MAC地址:00:14:c2:e5:45:57

接口:lo
接口状态: UP
IP地址:127.0.0.1
子网掩码:255.0.0.0
广播地址:0.0.0.0
MAC地址:00:00:00:00:00:00

从输出可以看出,系统有4个接口,”wlan0″表示第一块无线网卡接口,”eth0″(IP地址:192.168.4.111)表示第一块连线网卡接口(我们最长用的RJ45连接口网卡),”lo”是回路地址接口(我们常用的127.0.0.1)。

注意:”eth0:0″(IP地址:192.168.4.113)是有线网卡的别名(单网卡绑定多个IP),这是为了测试这个参考程序特意在eth0上添加的一个IP地址。

参考资料:《Unix网络编程》第16章 ioctl操作

文章来自:http://blog.163.com/lijm1989@yeah/blog/static/17147890320146206624469/
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值