Compare commits

...

22 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire d5a92a315c Re-named `TUNException` -> `AdapterException` 2022-11-28 18:43:15 +02:00
Tristan B. Velloza Kildaire 1357f7cb25 Added TODO list 2022-11-28 18:42:41 +02:00
Tristan B. Velloza Kildaire 87bfb3f1e6 Cleaned up uneeded imports and comments 2022-11-28 18:42:19 +02:00
Tristan B. Velloza Kildaire 2a8ab6f285 Removed last few extern declarations 2022-11-28 18:41:26 +02:00
Tristan B. Velloza Kildaire 76991d7082 Renamed `TUNAdapter` -> `Adapter` 2022-11-28 18:39:50 +02:00
Tristan B. Velloza Kildaire a234f52908 Updated documentation for methods already implemented 2022-11-28 18:38:26 +02:00
Tristan B. Velloza Kildaire bd21d7f7a3 Added some todo notes 2022-11-28 18:33:22 +02:00
Tristan B. Velloza Kildaire 2fbfef56f8 Switched to using ImportC 2022-11-28 18:27:38 +02:00
Tristan B. Velloza Kildaire 8ad431a76b Added branding 2022-01-12 18:17:45 +02:00
Tristan B. Velloza Kildaire e3bc5e422f Updated types 2021-11-15 13:33:14 +02:00
Tristan B. Velloza Kildaire b3631ab3e0 Updated to use stdint.h 2021-11-15 13:30:18 +02:00
Tristan B. Velloza Kildaire 8fab9b2217 Added some API notes 2021-10-25 15:09:28 +02:00
Tristan B. Velloza Kildaire 002fef91f9 Set adapter type 2021-10-24 19:41:21 +02:00
Tristan B. Velloza Kildaire b936afb809 Changed license name to fit dub registry semantics 2021-10-24 19:11:28 +02:00
Tristan B. Velloza Kildaire 9fcb3b89e7 Use cc rather 2021-10-24 19:11:01 +02:00
Tristan B. Velloza Kildaire 82c5add9f0 Updated license 2021-10-24 19:09:27 +02:00
Tristan B. Velloza Kildaire 26b29f1b2f Possibly fixed 2021-10-24 19:06:00 +02:00
Tristan B. Velloza Kildaire db29f0eeff Maybe fixed 2021-10-24 18:55:49 +02:00
Tristan B. Velloza Kildaire fc33029514 Fixed building for real 2021-10-24 18:52:12 +02:00
Tristan B. Velloza Kildaire 3976c738ca Possibly fixed build system 2021-10-24 18:50:59 +02:00
Tristan B. Velloza Kildaire 150f47bc27 Fixed build stuff 2021-10-19 12:20:28 +02:00
Tristan B. Velloza Kildaire 0bd07ea543 Fixed build system 2021-10-19 12:18:12 +02:00
8 changed files with 140 additions and 46 deletions

View File

@ -1,4 +1,61 @@
libtun
======
![](branding/logo.png)
TUN/TAP adapter for D-based applications
## Usage
First add it to your dub-based project via:
```
dub add libtun
```
### `Adapter`
The `Adapter` 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

BIN
branding/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

BIN
branding/logo.xcf Normal file

Binary file not shown.

View File

@ -4,9 +4,7 @@
],
"copyright": "Copyright © 2021, Tristan B. Kildaire",
"description": "TUN adapter for D",
"license": "GPLv3",
"license": "LGPL-3.0",
"name": "libtun",
"targetType": "library",
"preBuildCommands": ["gcc source/libtun/tunctl.c -o tunctl.o -c"],
"lflags": ["tunctl.o"]
"targetType": "library"
}

View File

@ -10,4 +10,13 @@ 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.
and also PEEK read.
## TODO
- [ ] Adapter settings
- [ ] `up`/`down` interface
- [ ] Set address on interface
- [ ] For source address selection
- [ ] For possible automatic route addition

View File

@ -1,20 +1,8 @@
module libtun.adapter;
extern (C) int ioctl(int fd, ulong request, void* data);
extern (C) int open(char* path, int flags);
import libtun.tunctl;
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
public class Adapter
{
/* Tunnel device descriptor */
private int tunFD;
@ -24,17 +12,26 @@ public class TUNAdapter
private byte[] scratch;
/**
* Instantiates a new Adapter with the given interface name
* and optionally you can specifiy the adapter type (default
* is TAP)
*
* Params:
* interfaceName = the name of the interface to create
* adapterType = The AdapterType to use
*/
this(string interfaceName, AdapterType adapterType = AdapterType.TAP)
{
init(interfaceName);
init(interfaceName, adapterType);
}
private void init(string interfaceName)
private void init(string interfaceName, AdapterType adapterType)
{
tunFD = createTun(cast(char*)interfaceName, 4096|2);
tunFD = createTun(cast(char*)interfaceName, 4096|adapterType);
if(tunFD < 0)
{
throw new TUNException("Error creating tun device");
throw new AdapterException("Error creating tun device");
}
/* TODO: Get device MTU and add functions for setting it */
@ -46,10 +43,21 @@ public class TUNAdapter
{
if(isClosed)
{
throw new TUNException("Cannot operate on closed tunnel device");
throw new AdapterException("Cannot operate on closed tunnel device");
}
}
public void setAddress()
{
}
/**
* Closes the adapter
*
* Throws:
* TUNException if the operation failed
*/
public void close()
{
sanityCheck();
@ -58,7 +66,15 @@ public class TUNAdapter
destroyTun(tunFD);
}
/**
* Blocks to receive into the buffer
*
* Params:
* buffer = The buffer variable to write the received
* data into
* Throws:
* TUNException if the read failed
*/
public void receive(ref byte[] buffer)
{
sanityCheck();
@ -79,7 +95,7 @@ public class TUNAdapter
if(status < 0)
{
throw new TUNException("Read failed");
throw new AdapterException("Read failed");
}
else if(status == 0)
{
@ -90,17 +106,16 @@ public class TUNAdapter
/* Copy the data into their buffer (and of correct length) */
buffer = scratch[0..status].dup;
}
}
/**
* Sends the provided data
*
* Params:
* buffer = The data to send
* Throws:
* TUNException if an error occurs
*/
public void send(byte[] buffer)
{
sanityCheck();
@ -120,7 +135,7 @@ public class TUNAdapter
}
public final class TUNException : Exception
public final class AdapterException : Exception
{
this(string msg)
{
@ -128,8 +143,8 @@ public final class TUNException : Exception
}
}
public enum AdapterType : ushort
public enum AdapterType : byte
{
TUN = 1,
TAP = 0
TAP = 2
}

View File

@ -8,7 +8,7 @@ import core.thread;
void main()
{
writeln("Edit source/app.d to start your project.");
TUNAdapter adapter = new TUNAdapter("testInterface0");
Adapter adapter = new Adapter("testInterface0");
while(true)
{

View File

@ -20,11 +20,20 @@
#include<string.h>
#include<sys/ioctl.h>
#include<unistd.h>
#include<stdint.h>
int createTun(char* interfaceName, int iffFlags)
/* TODO: Update types here using stdint */
//TODO: We could possibly use directly
uint32_t createTun(char* interfaceName, int32_t iffFlags)
{
/* TODO: Add all required error checking */
int tunFD = open("/dev/net/tun", O_RDWR);
int32_t tunFD = open("/dev/net/tun", O_RDWR);
/* If error */
if(tunFD < 0)
{
return tunFD;
}
/* TUN properties */
struct ifreq interfaceReqData;
@ -38,22 +47,24 @@ int createTun(char* interfaceName, int iffFlags)
strcpy(interfaceReqData.ifr_name, interfaceName);
/* Attempt to bring up the tun device node */
int tunStatus = ioctl(tunFD, TUNSETIFF, &interfaceReqData);
int32_t tunStatus = ioctl(tunFD, TUNSETIFF, &interfaceReqData);
if(tunStatus < 0)
{
tunFD = tunStatus;
tunFD = tunStatus;
}
return tunFD;
}
int destroyTun(int fd)
//TOOD: Maybe use directly
uint32_t destroyTun(uint32_t fd)
{
return close(fd);
}
int tunWrite(int fd, char* data, int length)
//TODO: Maybe use directly
uint32_t tunWrite(uint32_t fd, char* data, int length)
{
write(fd, data, length);
}
@ -64,7 +75,11 @@ int tunWrite(int fd, char* data, int length)
*
* (FIXME: For now we just read 20 bytes)
*/
int tunRead(int fd, char* data, int amount)
//TODO: Maybe use directly
uint32_t tunRead(uint32_t fd, char* data, int amount)
{
return read(fd, data, amount);
}
// uint32_t tunSet()