gpt4 book ai didi

networking - 使用 ioctl 分配 ipv6 地址

转载 作者:行者123 更新时间:2023-12-04 17:26:29 25 4
gpt4 key购买 nike

我正在尝试使用 ioctl 将 IPv6 地址分配给接口(interface),但徒劳无功。这是我使用的代码:

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

#define IFNAME "eth1"
#define HOST "fec2::22"
#define ifreq_offsetof(x) offsetof(struct ifreq, x)

int main(int argc, char **argv) {
struct ifreq ifr;
struct sockaddr_in6 sai;
int sockfd; /* socket fd we use to manipulate stuff with */
int selector;
unsigned char mask;

char *p;

/* Create a channel to the NET kernel. */
sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
if (sockfd == -1) {
printf("Bad fd\n");
return -1;
}

/* get interface name */
strncpy(ifr.ifr_name, IFNAME, IFNAMSIZ);

memset(&sai, 0, sizeof(struct sockaddr));
sai.sin6_family = AF_INET6;
sai.sin6_port = 0;

//if(inet_aton(HOST, &sai.sin_addr.s_addr) == 0) {
if(inet_pton(AF_INET6, HOST, (void *)&sai.sin6_addr) <= 0) {
//&((struct sockaddr_in*)&sa)->sin_addr
printf("Bad address\n");
return -1;
}

p = (char *) &sai;
memcpy( (((char *)&ifr + ifreq_offsetof(ifr_addr) )),
p, sizeof(struct sockaddr));

int ret = ioctl(sockfd, SIOCSIFADDR, &ifr);
printf("ret: %d\terrno: %d\n", ret, errno);
ioctl(sockfd, SIOCGIFFLAGS, &ifr);
printf("ret: %d\terrno: %d\n", ret, errno);

ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
// ifr.ifr_flags &= ~selector; // unset something

ioctl(sockfd, SIOCSIFFLAGS, &ifr);
printf("ret: %d\terrno: %d\n", ret, errno);
close(sockfd);
return 0;
}

ioctl 调用失败说 ENODEV。当socket的family改为 sockfd = socket(AF_INET, SOCK_DGRAM, 0);时,调用再次失败,说 EINVAL。

我能够使用上述代码为接口(interface)分配 IPv4 地址 sockaddr_in代替 sockaddr_in6 .

不能使用 ioctl 分配 IPv6 地址吗?

最佳答案

从 'ifconfig' 命令的 linux 实现中汲取灵感,我能够在接口(interface)上设置 IPv6 地址。这是它的代码:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <net/if.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_ether.h>
#endif

#define IFNAME "eth0"
#define HOST "fec2::22"
#define ifreq_offsetof(x) offsetof(struct ifreq, x)

struct in6_ifreq {
struct in6_addr ifr6_addr;
__u32 ifr6_prefixlen;
unsigned int ifr6_ifindex;
};

int main(int argc, char **argv) {

struct ifreq ifr;
struct sockaddr_in6 sai;
int sockfd;
struct in6_ifreq ifr6;

sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IP);
if (sockfd == -1) {
printf("Bad fd\n");
return -1;
}

/* get interface name */
strncpy(ifr.ifr_name, IFNAME, IFNAMSIZ);

memset(&sai, 0, sizeof(struct sockaddr));
sai.sin6_family = AF_INET6;
sai.sin6_port = 0;

if(inet_pton(AF_INET6, HOST, (void *)&sai.sin6_addr) <= 0) {
printf("Bad address\n");
return -1;
}

memcpy((char *) &ifr6.ifr6_addr, (char *) &sai.sin6_addr,
sizeof(struct in6_addr));

if (ioctl(sockfd, SIOGIFINDEX, &ifr) < 0) {
perror("SIOGIFINDEX");
}
ifr6.ifr6_ifindex = ifr.ifr_ifindex;
ifr6.ifr6_prefixlen = 64;
if (ioctl(sockfd, SIOCSIFADDR, &ifr6) < 0) {
perror("SIOCSIFADDR");
}

ifr.ifr_flags |= IFF_UP | IFF_RUNNING;

int ret = ioctl(sockfd, SIOCSIFFLAGS, &ifr);
printf("ret: %d\terrno: %d\n", ret, errno);

close(sockfd);
return 0;
}

关于networking - 使用 ioctl 分配 ipv6 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8240724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com