mirror of https://github.com/deavmi/libtun.git
Working tun interface
This commit is contained in:
parent
ca5634679c
commit
ed9f5c138e
10
source/app.d
10
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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue