如何根据接口(interface)取得ipv6地址

wildbeast123 2007-11-13 10:09:18
在网上找了一段代码,使用getifaddrs()这个函数可以取。
但是我试了一下还是只能取到ipv4的地址,这个函数的资料也很少,man手册也没有。
各位老大有没有用过的,给指点一下。
我的系统是RedHat EL4.


//代码根据UNP和man手册编写
//适用于LINUX/BSD(FreeBSD, MacOS X)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>

int main(void)
{
struct ifaddrs *ifc, *ifc1;
char ip[64];
char nm[64];

if (0 != getifaddrs(&ifc)) return(-1);
ifc1 = ifc;

printf("Iface\tIP address\tNetmask\n");
for(; NULL != ifc; ifc = (*ifc).ifa_next) {
printf("%s", (*ifc).ifa_name);
if (NULL != (*ifc).ifa_addr) {
inet_ntop(AF_INET, &(((struct sockaddr_in*)((*ifc).ifa_addr))->sin_addr), ip, 64);
printf("\t%s", ip);
} else {
printf("\t\t");
}
if (NULL != (*ifc).ifa_netmask) {
inet_ntop(AF_INET, &(((struct sockaddr_in*)((*ifc).ifa_netmask))->sin_addr), nm, 64);
printf("\t%s", nm);
} else {
printf("\t\t");
}
printf("\n");
}

freeifaddrs(ifc1);
return(0);
}
...全文
699 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cceczjxy 2007-11-13
  • 打赏
  • 举报
回复
/* tcp4_10localip.c */
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <signal.h>
#include <netinet/ip.h>

struct in_addr myself, mymask;
int fd_arp; /* socket fd for receive packets */
struct ifreq ifr; /* ifr structure */
main (int argc, char* argv[]) {
char device[32]; /* ethernet device name */
struct sockaddr from, to;
int fromlen;
struct sockaddr_in *sin_ptr;
u_char *ptr;
int n;
strcpy(device, "eth0");
if ((fd_arp = socket(AF_INET, SOCK_PACKET, htons(0x0806))) < 0) {
perror( "arp socket error");
exit(-1);
}

strcpy(ifr.ifr_name, device);
/* ifr.ifr_addr.sa_family = AF_INET; */
/* get ip address of my interface */
if (ioctl(fd_arp, SIOCGIFADDR, &ifr) < 0) {
perror("ioctl SIOCGIFADDR error");
exit(-1);
}
sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
myself = sin_ptr->sin_addr;
/* get network mask of my interface */
if (ioctl(fd_arp, SIOCGIFNETMASK, &ifr) < 0) {
perror("ioctl SIOCGIFNETMASK error");
exit(-1);
}
sin_ptr = (struct sockaddr_in *)&ifr.ifr_addr;
mymask = sin_ptr->sin_addr;
/* get mac address of the interface */
if (ioctl(fd_arp, SIOCGIFHWADDR, &ifr) < 0) {
perror("ioctl SIOCGIFHWADDR error");
exit(-1);
}
ptr = (u_char *)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
printf( "request mac %02x:%02x:%02x:%02x:%02x:%02x, ",
*ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3),
*(ptr + 4), *(ptr + 5) );
printf( "request netmask %s ", inet_ntoa(mymask));
printf( "request IP %s\n", inet_ntoa(myself));
} /* end of main */


你看看这样行不
LovelyCboy 2007-11-13
  • 打赏
  • 举报
回复
http://www.ipv6style.jp/en/apps/20030829/index.shtml
wildbeast123 2007-11-13
  • 打赏
  • 举报
回复
问题解决了,之前的程序是可以的,IPv6的时候没有判断。

ioctl()取不到IPv6的地址,2楼的链接和我之前的方法一样的,
还是感谢两位的回答。

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧