Compare commits

...

8 Commits

6 changed files with 61 additions and 35 deletions

View File

@ -13,9 +13,9 @@ First add it to your dub-based project via:
dub add libtun
```
### `TUNAdapter`
### `Adapter`
The `TUNAdapter` class provides you with all you need to get started. One can construct a new adapter as follows:
The `Adapter` class provides you with all you need to get started. One can construct a new adapter as follows:
```d
import libtun.adapter;

View File

@ -6,7 +6,5 @@
"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"]
"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,6 +12,15 @@ 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, adapterType);
@ -34,7 +31,7 @@ public class TUNAdapter
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)
{

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

@ -23,6 +23,7 @@
#include<stdint.h>
/* 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 */
@ -56,11 +57,13 @@ uint32_t createTun(char* interfaceName, int32_t iffFlags)
return tunFD;
}
//TOOD: Maybe use directly
uint32_t destroyTun(uint32_t fd)
{
return close(fd);
}
//TODO: Maybe use directly
uint32_t tunWrite(uint32_t fd, char* data, int length)
{
write(fd, data, length);
@ -72,6 +75,7 @@ uint32_t tunWrite(uint32_t fd, char* data, int length)
*
* (FIXME: For now we just read 20 bytes)
*/
//TODO: Maybe use directly
uint32_t tunRead(uint32_t fd, char* data, int amount)
{
return read(fd, data, amount);