0

I am a student working on a moon rover for CMU and need to work with a TUN interface communication between two devices on the rover. I can create a TUN interface, but am unable to assign its IP addresses in C and I'd prefer not to use the ip command. Here is the current state right after making the TUN device:

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

link/none Here is the desired state:

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

I have tried setting the dstaddr for the ifreq to no avail.

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 SIOCSIFADDR");close(fd);exit(1);
}

I don't know what else to do. I have tried debugging ip after giving it a command to do this, but I cannot figure it out what exactly it's doing, largely because it doesn't use ioctls.

0 Answers0