分类: LINUX
2013-12-25 17:38:16
原文地址: http://blog.csdn.net/xiaoweibeibei/article/details/6070936
记得看过一篇日志说ioctl无法实现ipv6地址的设定,今天没事研究了下,其实并非那位仁兄所说,算了,多的不说了,上代码(代码在ubuntu10.04上调试通过),当然这是个例子程序,欢迎大家拍砖来完善!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef struct in6_ifreq
{
struct in6_addr ifr6_addr;
unsigned int ifr6_prefixlen;
unsigned int ifr6_ifindex;
}IPV6_REQ;
int main()
{
IPV6_REQ ifreq6;
ifreq6.ifr6_prefixlen = 64;
inet_pton(AF_INET6, "2002:12::223:45", &ifreq6.ifr6_addr);
set_ipv6("eth1",ifreq6);
printf("set ipv6 success!");
}
int set_ipv6(char *name,IPV6_REQ ifreq6)
{
int sockfd6;
struct ifreq ifr;
if((sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
{
perror("Socket");
return -1;
}
strcpy(ifr.ifr_name, name);
ioctl(sockfd6, SIOCGIFINDEX, &ifr);
ifreq6.ifr6_ifindex = ifr.ifr_ifindex;
if(ioctl(sockfd6,SIOCSIFADDR, &ifreq6)< 0)
{
printf("error in get_ipaddr11/n");
perror("ioctl");
return -1;
}
return 0;
}