Throw exception on read failure (one of them atleast when status < 0)

Added close method to close tun interface
This commit is contained in:
Tristan B. Velloza Kildaire 2021-07-21 22:07:24 +02:00
parent d98e10ba22
commit 00fd2bba7e
2 changed files with 13 additions and 2 deletions

View File

@ -55,6 +55,7 @@ public class TUNAdapter
{ {
sanityCheck(); sanityCheck();
/* TODO: Get device MTU and add functions for setting it */
ushort mtu = cast(ushort)-1; ushort mtu = cast(ushort)-1;
/* Temporary scratchpad buffer */ /* Temporary scratchpad buffer */
@ -75,7 +76,7 @@ public class TUNAdapter
if(status < 0) if(status < 0)
{ {
throw new TUNException("Read failed");
} }
else if(status == 0) else if(status == 0)
{ {
@ -103,6 +104,16 @@ public class TUNAdapter
tunWrite(tunFD, cast(char*)buffer.ptr, cast(int)buffer.length); tunWrite(tunFD, cast(char*)buffer.ptr, cast(int)buffer.length);
} }
public void close()
{
int status = destroyTun(tunFD);
if(status)
{
throw new TUNException("Closing tun interface failed");
}
}
} }
public final class TUNException : Exception public final class TUNException : Exception

View File

@ -50,7 +50,7 @@ int createTun(char* interfaceName, int iffFlags)
int destroyTun(int fd) int destroyTun(int fd)
{ {
return 68; return close(fd);
} }
int tunWrite(int fd, char* data, int length) int tunWrite(int fd, char* data, int length)