mirror of https://github.com/deavmi/libtun.git
Removed code
This commit is contained in:
parent
8fab9b2217
commit
27082c8c18
59
README.md
59
README.md
|
@ -1,59 +0,0 @@
|
|||
libtun
|
||||
======
|
||||
|
||||
TUN/TAP adapter for D-based applications
|
||||
|
||||
## Usage
|
||||
|
||||
First add it to your dub-based project via:
|
||||
|
||||
```
|
||||
dub add libtun
|
||||
```
|
||||
|
||||
### `TUNAdapter`
|
||||
|
||||
The `TUNAdapter` class provides you with all you need to get started. One can construct a new adapter as follows:
|
||||
|
||||
```d
|
||||
import libtun.adapter;
|
||||
|
||||
void main()
|
||||
{
|
||||
try
|
||||
{
|
||||
TUNAdapter tun = new TUNAdapter("interface0", AdapterType.TUN);
|
||||
}
|
||||
catch(TUNException)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reading and writing is easy:
|
||||
|
||||
```d
|
||||
byte[] data;
|
||||
|
||||
try
|
||||
{
|
||||
tun.receive(data);
|
||||
tun.write([65,66,66,65]);
|
||||
}
|
||||
catch(TUNException)
|
||||
{
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
There are two types of adapters:
|
||||
|
||||
1. `AdapterType.TUN`
|
||||
* This is for creating a TUN device
|
||||
2. `AdapterType.TAP`
|
||||
* This is for creating a TAP device
|
||||
|
||||
## License
|
||||
|
||||
LGPLv3
|
12
dub.json
12
dub.json
|
@ -1,12 +0,0 @@
|
|||
{
|
||||
"authors": [
|
||||
"Tristan B. Kildaire"
|
||||
],
|
||||
"copyright": "Copyright © 2021, Tristan B. Kildaire",
|
||||
"description": "TUN adapter for D",
|
||||
"license": "LGPL-3.0",
|
||||
"name": "libtun",
|
||||
"targetType": "library",
|
||||
"preBuildCommands": ["cc $PACKAGE_DIR/source/libtun/tunctl.c -o $PACKAGE_DIR/source/tunctl.o -c"],
|
||||
"lflags": ["$PACKAGE_DIR/source/tunctl.o"]
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
{
|
||||
"fileVersion": 1,
|
||||
"versions": {
|
||||
}
|
||||
}
|
13
notes.md
13
notes.md
|
@ -1,13 +0,0 @@
|
|||
Notes
|
||||
=====
|
||||
|
||||
Finally works
|
||||
|
||||
TUNTAP is message-oriented, now I did allocate a huge amount. I could have possibly tried using PEEK if it implements it but the thing is that then I would have to mangle that fucking horried ethernet frame format (not str8 forward like IP)
|
||||
|
||||
It appears you canot use PEEK on fd's that are not sockets.
|
||||
|
||||
Regardless of whether they implement queuoing or not, it would be nice
|
||||
if that was allowed. And if tuntap did it. THEN I could make a system
|
||||
that doesn't allocate something huge and THEN I could go ahead
|
||||
and also PEEK read.
|
|
@ -1,135 +0,0 @@
|
|||
module libtun.adapter;
|
||||
|
||||
extern (C) int ioctl(int fd, ulong request, void* data);
|
||||
extern (C) int open(char* path, int flags);
|
||||
|
||||
import core.stdc.stdio;
|
||||
|
||||
|
||||
/**
|
||||
* TUN maintenance routines in `tunctl.c`
|
||||
*/
|
||||
extern (C) int createTun(char* interfaceName, int iffFlags);
|
||||
extern (C) int destroyTun(int fd);
|
||||
extern (C) int tunWrite(int fd, char* data, int length);
|
||||
extern (C) int tunRead(int fd, char* data, int amount);
|
||||
|
||||
public class TUNAdapter
|
||||
{
|
||||
/* Tunnel device descriptor */
|
||||
private int tunFD;
|
||||
private bool isClosed;
|
||||
|
||||
/* Temporary scratchpad buffer */
|
||||
private byte[] scratch;
|
||||
|
||||
|
||||
this(string interfaceName, AdapterType adapterType = AdapterType.TAP)
|
||||
{
|
||||
init(interfaceName, adapterType);
|
||||
}
|
||||
|
||||
private void init(string interfaceName, AdapterType adapterType)
|
||||
{
|
||||
tunFD = createTun(cast(char*)interfaceName, 4096|adapterType);
|
||||
if(tunFD < 0)
|
||||
{
|
||||
throw new TUNException("Error creating tun device");
|
||||
}
|
||||
|
||||
/* TODO: Get device MTU and add functions for setting it */
|
||||
ushort mtu = cast(ushort)-1;
|
||||
scratch.length = mtu;
|
||||
}
|
||||
|
||||
private void sanityCheck()
|
||||
{
|
||||
if(isClosed)
|
||||
{
|
||||
throw new TUNException("Cannot operate on closed tunnel device");
|
||||
}
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
sanityCheck();
|
||||
|
||||
isClosed = true;
|
||||
destroyTun(tunFD);
|
||||
}
|
||||
|
||||
|
||||
public void receive(ref byte[] buffer)
|
||||
{
|
||||
sanityCheck();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* We read with a request of maximum possible
|
||||
* Ethernet frame size (-1 -> 2 bytes) 65535,
|
||||
* this ensures our buffer fills up in all cases
|
||||
* but we get returned either < 0 or > 0.
|
||||
*
|
||||
* Former, systemcall read error
|
||||
* Latter, ethernet frame size
|
||||
*/
|
||||
int status = tunRead(tunFD, cast(char*)scratch.ptr, cast(int)scratch.length);
|
||||
|
||||
|
||||
if(status < 0)
|
||||
{
|
||||
throw new TUNException("Read failed");
|
||||
}
|
||||
else if(status == 0)
|
||||
{
|
||||
assert(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Copy the data into their buffer (and of correct length) */
|
||||
buffer = scratch[0..status].dup;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void send(byte[] buffer)
|
||||
{
|
||||
sanityCheck();
|
||||
|
||||
tunWrite(tunFD, cast(char*)buffer.ptr, cast(int)buffer.length);
|
||||
}
|
||||
|
||||
|
||||
public void setDeviceMTU(ushort mtu)
|
||||
{
|
||||
/* TODO: Set the MTU on the device */
|
||||
|
||||
/* TODO: Set the scratchpad to match the MTU */
|
||||
scratch.length = mtu;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public final class TUNException : Exception
|
||||
{
|
||||
this(string msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public enum AdapterType : byte
|
||||
{
|
||||
TUN = 1,
|
||||
TAP = 2
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
module libtun.testing.app;
|
||||
|
||||
import std.stdio;
|
||||
|
||||
import libtun.adapter;
|
||||
import core.thread;
|
||||
|
||||
void main()
|
||||
{
|
||||
writeln("Edit source/app.d to start your project.");
|
||||
TUNAdapter adapter = new TUNAdapter("testInterface0");
|
||||
|
||||
while(true)
|
||||
{
|
||||
adapter.send([1,1,2,2,2,2]);
|
||||
byte[] buffer;
|
||||
adapter.receive(buffer);
|
||||
writeln(buffer);
|
||||
Thread.sleep(dur!("msecs")(50));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
/**
|
||||
* 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>
|
||||
#include<string.h>
|
||||
#include<sys/ioctl.h>
|
||||
#include<unistd.h>
|
||||
|
||||
int createTun(char* interfaceName, int iffFlags)
|
||||
{
|
||||
/* TODO: Add all required error checking */
|
||||
int tunFD = open("/dev/net/tun", O_RDWR);
|
||||
|
||||
/* TUN properties */
|
||||
struct ifreq interfaceReqData;
|
||||
|
||||
/* TODO: Do tuntype */
|
||||
|
||||
/* Set the flags for the tun adapter */
|
||||
interfaceReqData.ifr_flags = iffFlags;
|
||||
|
||||
/* Set the requested interface's name */
|
||||
strcpy(interfaceReqData.ifr_name, interfaceName);
|
||||
|
||||
/* Attempt to bring up the tun device node */
|
||||
int tunStatus = ioctl(tunFD, TUNSETIFF, &interfaceReqData);
|
||||
|
||||
if(tunStatus < 0)
|
||||
{
|
||||
tunFD = tunStatus;
|
||||
}
|
||||
|
||||
return tunFD;
|
||||
}
|
||||
|
||||
int destroyTun(int fd)
|
||||
{
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
int tunWrite(int fd, char* data, int length)
|
||||
{
|
||||
write(fd, data, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*/
|
||||
int tunRead(int fd, char* data, int amount)
|
||||
{
|
||||
return read(fd, data, amount);
|
||||
}
|
Loading…
Reference in New Issue