Working tun interface

This commit is contained in:
Tristan B. Velloza Kildaire 2021-07-18 19:45:17 +02:00
parent ca5634679c
commit ed9f5c138e
3 changed files with 23 additions and 7 deletions

View File

@ -1,9 +1,17 @@
import std.stdio; import std.stdio;
import libtun.adapter; import libtun.adapter;
import core.thread;
void main() void main()
{ {
writeln("Edit source/app.d to start your project."); 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));
}
} }

View File

@ -3,7 +3,7 @@ module libtun.adapter;
extern (C) int ioctl(int fd, ulong request, void* data); extern (C) int ioctl(int fd, ulong request, void* data);
extern (C) int open(char* path, int flags); 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 createTun(char* interfaceName, int iffFlags);
extern (C) int destroyTun(int fd); extern (C) int destroyTun(int fd);
extern (C) int tunWrite(int fd, char* data, int length);
public class TUNAdapter public class TUNAdapter
{ {
/* Tunnel device descriptor */
private int tunFD;
this(string interfaceName) 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) if(tunFD < 0)
{ {
throw new TUNException("Error creating tun device"); throw new TUNException("Error creating tun device");
@ -35,7 +39,7 @@ public class TUNAdapter
public void send(byte[] buffer) public void send(byte[] buffer)
{ {
tunWrite(tunFD, cast(char*)buffer.ptr, cast(int)buffer.length);
} }
} }

View File

@ -41,7 +41,7 @@ int createTun(char* interfaceName, int iffFlags)
{ {
tunFD = tunStatus; tunFD = tunStatus;
} }
return tunFD; return tunFD;
} }
@ -50,3 +50,7 @@ int destroyTun(int fd)
return 68; return 68;
} }
int tunWrite(int fd, char* data, int length)
{
write(fd, data, length);
}