如何在C中设置TUN接口的IP地址和对等IP地址

如何在C中设置TUN接口的IP地址和对等IP地址

我是一名为 CMU 开发月球车的学生,需要在月球车上的两个设备之间使用 TUN 接口通信。我可以创建 TUN 接口,但无法在 C 中分配其 IP 地址。

以下是制作 TUN 设备后的当前状态:

41: tun11: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 500
link/none

这是所需的状态:

14: tun71: <POINTOPOINT,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 500
link/none 
inet 192.168.0.29 peer 192.168.0.23/32 scope global tun71
   valid_lft forever preferred_lft forever

我尝试为 ifreq 设置 dstaddr 但无济于事。

int fd;
int dst_fd;
struct ifreq ifr;
struct sockaddr_in* addr;
struct sockaddr_in* dst_addr;

fd = socket(AF_INET, SOCK_DGRAM, 0);
dst_fd = socket(AF_INET, SOCK_DGRAM, 0);

ifr.ifr_addr.sa_family = AF_INET;

memcpy(ifr.ifr_name, "tun11", IFNAMSIZ - 1);
addr = (struct sockaddr_in*)&ifr.ifr_addr;
dst_addr = (struct sockaddr_in*)&ifr.ifr_dstaddr;

inet_pton(AF_INET, ip_address, &addr->sin_addr);
inet_pton(AF_INET, ip_address, &dst_addr->sin_addr);
int err;
if ( (err = ioctl(fd, SIOCSIFADDR, &ifr)) == -1 ) {
    perror("ioctl SIOCSIFFLAGS");close(fd);exit(1);
}

我不知道还能做什么。我在给它一个命令后尝试调试 IP,但我无法弄清楚。

答案1

如果你还没有,创建一个测试tun

# ip tuntap add mode tun dev tun0
# ip address show dev tun0
5: tun0: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 500
    link/none

这看起来与问题中的内容非常相似。现在到看看我们需要什么代码...

// uptun.c - set an IP address and UP a tun
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>             /* offsetof */
#include <net/if.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <netinet/in.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

// TWEAK
#define IFNAME "tun0"
#define HOST "192.168.0.29"

#define ifreq_offsetof(x) offsetof(struct ifreq, x)

int main(int argc, char **argv) {
    struct ifreq ifr;
    struct sockaddr_in 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_INET, SOCK_DGRAM, 0);

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

    memset(&sai, 0, sizeof(struct sockaddr));
    sai.sin_family = AF_INET;
    sai.sin_port   = 0;

    sai.sin_addr.s_addr = inet_addr(HOST);

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

    ioctl(sockfd, SIOCSIFADDR, &ifr);
    ioctl(sockfd, SIOCGIFFLAGS, &ifr);

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

    ioctl(sockfd, SIOCSIFFLAGS, &ifr);
    close(sockfd);
    return 0;
}

这给了我们:

# make uptun
cc     uptun.c   -o uptun
# ./uptun
# ip address show dev tun0
5: tun0: <NO-CARRIER,POINTOPOINT,MULTICAST,NOARP,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 500
    link/none
    inet 192.168.0.29/32 scope global tun0
       valid_lft forever preferred_lft forever

只缺少对等地址。一些 altagoobingleduckgoing 表明 tunSIOCSIFDSTADDR除了 之外还有一个 (set destination, duh) SIOCSIFADDR。让我们将其塞入脚本中该行之后 SIOCGIFFLAGS...

        // KLUGE copypasta in the destination address
        sai.sin_addr.s_addr = inet_addr("192.168.0.23");
        p = (char *) &sai;
        memcpy((((char *) &ifr + ifreq_offsetof(ifr_addr))), p,
               sizeof(struct sockaddr));
        ioctl(sockfd, SIOCSIFDSTADDR, &ifr);

编译并重新运行后uptun我们有:

# ip address show dev tun0
5: tun0: <NO-CARRIER,POINTOPOINT,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 500
    link/none
    inet 192.168.0.29 peer 192.168.0.23/32 scope global tun0
       valid_lft forever preferred_lft forever

相关内容