diff --git a/source/app.d b/source/app.d index 741a370..aed9cd6 100644 --- a/source/app.d +++ b/source/app.d @@ -1,9 +1,17 @@ import std.stdio; import libtun.adapter; +import core.thread; void main() { writeln("Edit source/app.d to start your project."); - new TUNAdapter(""); + TUNAdapter adapter = new TUNAdapter("testInterface0"); + + while(true) + { + adapter.send([1,1,2,2,2,2]); + Thread.sleep(dur!("msecs")(500)); + } + } diff --git a/source/libtun/adapter.d b/source/libtun/adapter.d index 16d3498..7347294 100644 --- a/source/libtun/adapter.d +++ b/source/libtun/adapter.d @@ -3,7 +3,7 @@ module libtun.adapter; extern (C) int ioctl(int fd, ulong request, void* data); extern (C) int open(char* path, int flags); -import std.stdio; +import core.stdc.stdio; /** @@ -11,17 +11,21 @@ import std.stdio; */ extern (C) int createTun(char* interfaceName, int iffFlags); extern (C) int destroyTun(int fd); +extern (C) int tunWrite(int fd, char* data, int length); public class TUNAdapter { + /* Tunnel device descriptor */ + private int tunFD; + this(string interfaceName) { - init(); + init(interfaceName); } - private void init() + private void init(string interfaceName) { - int tunFD = createTun(cast(char*)"dd", 1); + tunFD = createTun(cast(char*)interfaceName, 1); if(tunFD < 0) { throw new TUNException("Error creating tun device"); @@ -35,7 +39,7 @@ public class TUNAdapter public void send(byte[] buffer) { - + tunWrite(tunFD, cast(char*)buffer.ptr, cast(int)buffer.length); } } diff --git a/source/libtun/tunctl.c b/source/libtun/tunctl.c index 7311ebd..68fad00 100644 --- a/source/libtun/tunctl.c +++ b/source/libtun/tunctl.c @@ -41,7 +41,7 @@ int createTun(char* interfaceName, int iffFlags) { tunFD = tunStatus; } - + return tunFD; } @@ -50,3 +50,7 @@ int destroyTun(int fd) return 68; } +int tunWrite(int fd, char* data, int length) +{ + write(fd, data, length); +}