Here is the source code discussed in this video episode
/* The Linux Channel
* Code for Video Episode: 0x1b0 Linux TUN/TAP interfaces creation via C code - Ep5
* Author: Kiran Kankipati
* Updated: 14-Apr-2019
* tun_tap_create.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
/* create via CLI: (if not via this code)
# ip tuntap help
Usage: ip tuntap { add | del } [ dev PHYS_DEV ]
[ mode { tun | tap } ] [ user USER ] [ group GROUP ]
[ one_queue ] [ pi ] [ vnet_hdr ]
Where: USER := { STRING | NUMBER }
GROUP := { STRING | NUMBER }
*/
/*
sudo ip addr add 192.168.3.1/24 dev mytuntap1
sudo ip link set dev mytuntap1 up (or) sudo ifconfig mytuntap1 up
ping 192.168.3.1
*/
#define TUN_TAP_INT "mytuntap1"
int tun_tap_create(char *name, int type)
{ //type: IFF_TUN or IFF_TAP
struct ifreq ifr;
int fd, ret;
if((fd = open("/dev/net/tun", O_RDWR)) < 0) { printf("error: open()\n"); return fd; }
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = type;
strncpy(ifr.ifr_name, name, IFNAMSIZ);
if((ret = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 ) {
close(fd);
return ret;
}
return fd;
}
void main()
{ int fd1, len; char buf[2000];
fd1=tun_tap_create(TUN_TAP_INT, IFF_TAP);
if(fd1>0) { printf("interface %s created!\n", TUN_TAP_INT); } else { printf("interface %s creation failed!\n", TUN_TAP_INT); return; }
while(1)
{
len = read(fd1, buf, sizeof(buf));
printf("Rx %d bytes \n", len);
len=0;
}
close(fd1);
}
Refer:
Linux Kernel Source – Documentation/networking/tuntap.txt – Universal TUN/TAP device driver ↗
Wiki TUN/TAP ↗