2021-07-18 17:46:08 +01:00
|
|
|
/**
|
|
|
|
* This module is used just to interface between the Linux
|
|
|
|
* kernel (via GLIBC) such that the tun adpater can be setup
|
|
|
|
* and destroyed
|
|
|
|
*
|
|
|
|
* `int createTun(char* interfaceName)`
|
|
|
|
* - This creates a tun interface with the provided name
|
|
|
|
* - and returns the fd
|
|
|
|
* `int destroyTun(int fd)`
|
|
|
|
* - This destroys the tun interface given
|
|
|
|
*
|
|
|
|
* Once we have the fd everything else can be done in D
|
|
|
|
* as we just read()/write() on the returned fd we got
|
|
|
|
* using `createTun`
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include<linux/if.h>
|
|
|
|
#include<linux/if_tun.h>
|
|
|
|
#include<fcntl.h>
|
2021-07-18 18:23:02 +01:00
|
|
|
#include<string.h>
|
|
|
|
#include<sys/ioctl.h>
|
2021-07-18 17:46:08 +01:00
|
|
|
|
2021-07-18 18:23:02 +01:00
|
|
|
int createTun(char* interfaceName, int iffFlags)
|
2021-07-18 17:46:08 +01:00
|
|
|
{
|
|
|
|
/* TODO: Add all required error checking */
|
|
|
|
int tunFD = open("/dev/net/tun", O_RDWR);
|
2021-07-18 17:53:32 +01:00
|
|
|
|
2021-07-18 18:23:02 +01:00
|
|
|
/* TUN properties */
|
2021-07-18 17:53:32 +01:00
|
|
|
struct ifreq interfaceReqData;
|
|
|
|
|
2021-07-18 22:23:56 +01:00
|
|
|
/* TODO: Do tuntype */
|
|
|
|
|
2021-07-18 18:23:02 +01:00
|
|
|
/* Set the flags for the tun adapter */
|
2021-07-18 17:53:32 +01:00
|
|
|
interfaceReqData.ifr_flags = iffFlags;
|
|
|
|
|
2021-07-18 18:23:02 +01:00
|
|
|
/* Set the requested interface's name */
|
|
|
|
strcpy(interfaceReqData.ifr_name, interfaceName);
|
|
|
|
|
|
|
|
/* Attempt to bring up the tun device node */
|
2021-07-18 18:26:08 +01:00
|
|
|
int tunStatus = ioctl(tunFD, TUNSETIFF, &interfaceReqData);
|
2021-07-18 17:53:32 +01:00
|
|
|
|
2021-07-18 18:26:08 +01:00
|
|
|
if(tunStatus < 0)
|
|
|
|
{
|
|
|
|
tunFD = tunStatus;
|
|
|
|
}
|
2021-07-18 18:45:17 +01:00
|
|
|
|
2021-07-18 17:54:32 +01:00
|
|
|
return tunFD;
|
2021-07-18 17:46:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int destroyTun(int fd)
|
|
|
|
{
|
|
|
|
return 68;
|
|
|
|
}
|
|
|
|
|
2021-07-18 18:45:17 +01:00
|
|
|
int tunWrite(int fd, char* data, int length)
|
|
|
|
{
|
|
|
|
write(fd, data, length);
|
|
|
|
}
|
2021-07-18 22:10:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Depending on mode we need to read a certain amount
|
|
|
|
* to get the length and then from there move onwards
|
|
|
|
*
|
|
|
|
* (FIXME: For now we just read 20 bytes)
|
|
|
|
*/
|
2021-07-21 17:50:46 +01:00
|
|
|
int tunRead(int fd, char* data, int amount)
|
2021-07-18 22:10:11 +01:00
|
|
|
{
|
2021-07-21 17:50:46 +01:00
|
|
|
return read(fd, data, amount);
|
2021-07-18 22:10:11 +01:00
|
|
|
}
|