LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3629|回复: 8

怎样通过接口名得到IP地址?

[复制链接]
发表于 2005-4-28 21:57:41 | 显示全部楼层 |阅读模式
:ask   不知Linux有没有通过网卡接口名得到ip地址的系统接口,不会是通过读/etc/hosts文件得到吧,系望高手帮忙!!!!!!!!!! :thank
发表于 2005-4-29 09:25:38 | 显示全部楼层
什么是"ip地址的系统接口"?!!
能不能把这个概念说具体一点。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-29 09:55:07 | 显示全部楼层
Post by kj501
什么是"ip地址的系统接口"?!!
能不能把这个概念说具体一点。



就是通过网卡接口名得到IP地址的函数接口!
回复 支持 反对

使用道具 举报

发表于 2005-4-29 10:50:40 | 显示全部楼层
你的意思是想知道eth0绑定的是那个ip地址?
可以分析ifconfig的执行结果。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-29 11:01:58 | 显示全部楼层
就是怎么通过编程实现通过接口名,比如eth0,得到它的IP地址,不通过Shell命令。
回复 支持 反对

使用道具 举报

发表于 2005-4-29 14:17:39 | 显示全部楼层
ioctl
回复 支持 反对

使用道具 举报

发表于 2005-4-29 21:50:21 | 显示全部楼层
怎么这么多人问这个问题?贴我的代码:


  1. #include <errno.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <sys/ioctl.h>
  5. #include <net/if.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <stdio.h>
  9. #include <string.h>

  10. inline static int __getifaddr_ipv4(int ifindex, const char *ifname,
  11.                                    struct in_addr *ifaddr)
  12. {
  13.     struct ifreq ifreq;
  14.     int sockfd;
  15.     int ret = -1;
  16.     char ifn[IFNAMSIZ];
  17.                                                                                                                                                
  18.     if (ifindex > 0)
  19.     {
  20.         if (!(ifname = if_indextoname(ifindex, ifn)))
  21.         {
  22.             errno = ENXIO;
  23.             return -1;
  24.         }
  25.     }
  26.                                                                                                                                                
  27.     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
  28.     {
  29.         strncpy(ifreq.ifr_name, ifname, IFNAMSIZ);
  30.         if ((ret = ioctl(sockfd, SIOCGIFADDR, &ifreq)) >= 0)
  31.             *ifaddr = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
  32.                                                                                                                                                
  33.         close(sockfd);
  34.     }
  35.                                                                                                                                                
  36.     return ret;
  37. }
  38.                                                                                                                                                
  39. #ifdef IPV6
  40.                                                                                                                                                
  41. #define PROC_IFINET6_PATH   "/proc/net/if_inet6"
  42.                                                                                                                                                
  43. inline static int __getifaddr_ipv6(unsigned int ifindex, const char *ifname,
  44.                                    struct in6_addr *ifaddr)
  45. {
  46.     FILE *fp = fopen(PROC_IFINET6_PATH, "r");
  47.     char addrstr[INET6_ADDRSTRLEN];
  48.     char seg[8][5];
  49.     int index, plen, scope, flags;
  50.     char ifn[IFNAMSIZ];
  51.     int ret = -1;
  52.                                                                                                                                                
  53.     if (fp)
  54.     {
  55.         while (fscanf(fp, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %s\n"
  56.                       seg[0], seg[1], seg[2], seg[3], seg[4], seg[5], seg[6],
  57.                       seg[7], &index, &plen, &scope, &flags, ifn) != EOF)
  58.         {
  59.             if (ifindex == index || ifindex == 0 && strcmp(ifn, ifname) == 0)
  60.             {
  61.                 sprintf(addrstr, "%s:%s:%s:%s:%s:%s:%s:%s", seg[0], seg[1],
  62.                         seg[2], seg[3], seg[4], seg[5], seg[6], seg[7]);
  63.                 ret = inet_pton(AF_INET6, addrstr, ifaddr);
  64.                 goto out;
  65.             }
  66.         }
  67.                                                                                                                                                
  68.         errno = ENXIO;
  69. out:
  70.         fclose(fp);
  71.     }
  72.                                                                                                                                                
  73.     return ret;
  74. }
  75.                                                                                                                                                
  76. #endif
  77.                                                                                                                                                
  78. int getifaddr(int family, unsigned int ifindex, const char *ifname,
  79.               void *ifaddr)
  80. {
  81.     switch (family)
  82.     {
  83.     case AF_INET:
  84.         return __getifaddr_ipv4(ifindex, ifname, (struct in_addr *)ifaddr);
  85. #ifdef IPV6
  86.     case AF_INET6:
  87.         return __getifaddr_ipv6(ifindex, ifname, (struct in6_addr *)ifaddr);
  88. #endif
  89.     default:
  90.         errno = EAFNOSUPPORT;
  91.         return -1;
  92.     }
  93. }
  94.                                                                                                                                        


复制代码


例如:
struct in_addr addr;
getifaddr(AF_INET, 0, "eth0", &addr);
如果ifindex不为0优先使用ifindex, 否则用ifname.
回复 支持 反对

使用道具 举报

发表于 2005-6-20 23:52:04 | 显示全部楼层
看libnet时正好也看到有这样的api
获取ip地址
u_int32_t  libnet_get_ipaddr4(libnet_t *);
获取mac地址
struct libnet_ether_addr *  libnet_get_hwaddr(libnet_t *);
回复 支持 反对

使用道具 举报

发表于 2005-6-21 14:22:01 | 显示全部楼层
Tetris就是强!以后多多帮助大家啊.:-)
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表