mirror of https://github.com/deavmi/libtun.git
Added method to close tun device
Throw error when trying to operate on closed tun device
This commit is contained in:
parent
ed9f5c138e
commit
1eb37178c1
|
@ -11,6 +11,9 @@ void main()
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
adapter.send([1,1,2,2,2,2]);
|
adapter.send([1,1,2,2,2,2]);
|
||||||
|
byte[] buffer;
|
||||||
|
adapter.receive(buffer);
|
||||||
|
writeln(buffer);
|
||||||
Thread.sleep(dur!("msecs")(500));
|
Thread.sleep(dur!("msecs")(500));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,18 +7,20 @@ import core.stdc.stdio;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TUN maintenance routines in `test.c`
|
* TUN maintenance routines in `tunctl.c`
|
||||||
*/
|
*/
|
||||||
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);
|
extern (C) int tunWrite(int fd, char* data, int length);
|
||||||
|
extern (C) int tunRead(int fd, char* data);
|
||||||
|
|
||||||
public class TUNAdapter
|
public class TUNAdapter
|
||||||
{
|
{
|
||||||
/* Tunnel device descriptor */
|
/* Tunnel device descriptor */
|
||||||
private int tunFD;
|
private int tunFD;
|
||||||
|
private bool isClosed;
|
||||||
|
|
||||||
this(string interfaceName)
|
this(string interfaceName, AdapterType adapterType = AdapterType.TAP)
|
||||||
{
|
{
|
||||||
init(interfaceName);
|
init(interfaceName);
|
||||||
}
|
}
|
||||||
|
@ -32,13 +34,36 @@ public class TUNAdapter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receive(byte[] buffer)
|
private void sanityCheck()
|
||||||
{
|
{
|
||||||
|
if(isClosed)
|
||||||
|
{
|
||||||
|
throw new TUNException("Cannot operate on closed tunnel device");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
sanityCheck();
|
||||||
|
|
||||||
|
isClosed = true;
|
||||||
|
destroyTun(tunFD);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void receive(ref byte[] buffer)
|
||||||
|
{
|
||||||
|
sanityCheck();
|
||||||
|
|
||||||
|
/* TODO: Get amount read */
|
||||||
|
/* FIXME: For now set it length to 20 */
|
||||||
|
buffer.length = 400;
|
||||||
|
tunRead(tunFD, cast(char*)buffer.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(byte[] buffer)
|
public void send(byte[] buffer)
|
||||||
{
|
{
|
||||||
|
sanityCheck();
|
||||||
|
|
||||||
tunWrite(tunFD, cast(char*)buffer.ptr, cast(int)buffer.length);
|
tunWrite(tunFD, cast(char*)buffer.ptr, cast(int)buffer.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,4 +74,10 @@ public final class TUNException : Exception
|
||||||
{
|
{
|
||||||
super(msg);
|
super(msg);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum AdapterType
|
||||||
|
{
|
||||||
|
TUN,
|
||||||
|
TAP
|
||||||
}
|
}
|
|
@ -54,3 +54,14 @@ int tunWrite(int fd, char* data, int length)
|
||||||
{
|
{
|
||||||
write(fd, data, length);
|
write(fd, data, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: Depending on mode we need to read a certain amount
|
||||||
|
* to get the length and then from there move onwards
|
||||||
|
*
|
||||||
|
* (FIXME: For now we just read 20 bytes)
|
||||||
|
*/
|
||||||
|
int tunRead(int fd, char* data)
|
||||||
|
{
|
||||||
|
read(fd, data, 400);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue