English 中文(简体)
Assign ipv6 Address using ioctl
原标题:Assign ipv6 address using ioctl

I m试图将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>

#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: %d	errno: %d
", ret, errno);
  ioctl(sockfd, SIOCGIFFLAGS, &ifr);
  printf("ret: %d	errno: %d
", ret, errno);

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

  ioctl(sockfd, SIOCSIFFLAGS, &ifr);
  printf("ret: %d	errno: %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.

是否不可能用电线分配IPv6地址?

最佳回答

我从三军三军三军三军三军三军导师的感受,得以将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
");
          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: %d	errno: %d
", ret, errno);

    close(sockfd);
    return 0;
}
问题回答

我在“@maddy”的答复基础上,做了一个更加精细的版本,很难适应。 The trick is the struct in6_ifreq 须通过<代码>octl的结构。

#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;
}

我从所有可读性的错误核对中删除,但应当对指挥部的错误进行核对。





相关问题
C# Networking API s [closed]

Lately I ve been looking for a good networking API i could possibly use and/or reference some of the code within, but i have mere luck searching for some on Google/Bing. Hopefully somebody here has ...

Listen to a port that is in use [duplicate]

Possible Duplicate: Get connecting IP from specified ports that using by other program. If a port is used by a program, is there any way I can listen that port and get the connected IP on that ...

Twisted Spread suitable for multiplayer racing sim?

Do you think that Twisted Spread may be suitable (in terms of performance) for a multiplayer racing simulator? The rest of the application is based on Python-Ogre. Can Perspective Broker run upon (...

Optimizing a LAN server for a game

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. ...

multicast ip address - blocked in call to recvfrom

i am writing a simple multicast application. i intend to run it on localhost. i have done the following: char *maddr; . . . sendfd = socket(...); struct sockaddr_in sasend; sasend.sin_family = ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

热门标签