Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

networking - Assign ipv6 address using ioctl

I'm trying to assign an IPv6 address to an interface using ioctl, but in vain. Here's the code I used:

#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
");
    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
");
    return -1;
  }

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

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

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

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

The ioctl calls fail saying ENODEV. When the family of the socket is changed to sockfd = socket(AF_INET, SOCK_DGRAM, 0);, the calls fail again saying EINVAL.

I was able to assign an IPv4 address to the interface with the above code by using sockaddr_in in lieu of sockaddr_in6.

Is it not possible to assign IPv6 address using ioctl?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Drawing inspiration from the linux implementation of 'ifconfig' command, I was able to set the IPv6 address on an interface. Here's the code for it:

#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
");
          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
");
        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: %derrno: %d
", ret, errno);

    close(sockfd);
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...