首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ioctl分配ipv6地址

问使用ioctl分配ipv6地址
EN

Stack Overflow用户
提问于 2011-11-23 18:36:47
回答 2查看 7.2K关注 0票数 5

我正在尝试使用ioctl为接口分配IPv6地址,但徒劳无功。下面是我使用的代码:

代码语言:javascript
复制
#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。当套接字的系列更改为sockfd = socket(AF_INET, SOCK_DGRAM, 0);时,调用再次失败,并显示EINVAL。

通过使用sockaddr_in代替sockaddr_in6,我可以使用上面的代码为接口分配一个IPv4地址。

是否不能使用ioctl分配IPv6地址?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-01 16:29:36

从'ifconfig‘命令的linux实现中获得灵感,我能够在一个接口上设置IPv6地址。下面是它的代码:

代码语言:javascript
复制
#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;
}
票数 6
EN

Stack Overflow用户

发布于 2014-02-27 04:35:20

基于@maddy的回答,我做了一个更紧凑的版本,更容易适应。诀窍在于必须传递给ioctl的struct in6_ifreq结构。

代码语言:javascript
复制
#include <stdint.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>

#define IFNAME "eth0"
#define HOST   "2001::22"

struct in6_ifreq {
    struct in6_addr addr;
    uint32_t        prefixlen;
    unsigned int    ifindex;
};

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

    struct ifreq ifr;
    struct in6_ifreq ifr6;
    int sockfd;
    int err;

    // Create IPv6 socket to perform the ioctl operations on
    sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IP);

    // Copy the interface name to the ifreq struct
    strncpy(ifr.ifr_name, IFNAME, IFNAMSIZ);
    // Get the ifrindex of the interface
    err = ioctl(sockfd, SIOGIFINDEX, &ifr);

    // Prepare the in6_ifreq struct and set the address to the interface
    inet_pton(AF_INET6, HOST, &ifr6.addr);
    ifr6.ifindex = ifr.ifr_ifindex;
    ifr6.prefixlen = 64;
    err = ioctl(sockfd, SIOCSIFADDR, &ifr6);

    close(sockfd);
    return 0;
}

为了可读性,我停止了所有的错误检查,但是应该检查命令中的错误。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8240724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档