mirror of https://github.com/deavmi/libtun.git
Compare commits
7 Commits
8fab9b2217
...
76991d7082
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | 76991d7082 | |
Tristan B. Velloza Kildaire | a234f52908 | |
Tristan B. Velloza Kildaire | bd21d7f7a3 | |
Tristan B. Velloza Kildaire | 2fbfef56f8 | |
Tristan B. Velloza Kildaire | 8ad431a76b | |
Tristan B. Velloza Kildaire | e3bc5e422f | |
Tristan B. Velloza Kildaire | b3631ab3e0 |
|
@ -1,6 +1,8 @@
|
|||
libtun
|
||||
======
|
||||
|
||||
![](branding/logo.png)
|
||||
|
||||
TUN/TAP adapter for D-based applications
|
||||
|
||||
## Usage
|
||||
|
@ -11,9 +13,9 @@ First add it to your dub-based project via:
|
|||
dub add libtun
|
||||
```
|
||||
|
||||
### `TUNAdapter`
|
||||
### `Adapter`
|
||||
|
||||
The `TUNAdapter` class provides you with all you need to get started. One can construct a new adapter as follows:
|
||||
The `Adapter` class provides you with all you need to get started. One can construct a new adapter as follows:
|
||||
|
||||
```d
|
||||
import libtun.adapter;
|
||||
|
@ -56,4 +58,4 @@ There are two types of adapters:
|
|||
|
||||
## License
|
||||
|
||||
LGPLv3
|
||||
LGPLv3
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 5.1 KiB |
Binary file not shown.
4
dub.json
4
dub.json
|
@ -6,7 +6,5 @@
|
|||
"description": "TUN adapter for D",
|
||||
"license": "LGPL-3.0",
|
||||
"name": "libtun",
|
||||
"targetType": "library",
|
||||
"preBuildCommands": ["cc $PACKAGE_DIR/source/libtun/tunctl.c -o $PACKAGE_DIR/source/tunctl.o -c"],
|
||||
"lflags": ["$PACKAGE_DIR/source/tunctl.o"]
|
||||
"targetType": "library"
|
||||
}
|
|
@ -8,13 +8,15 @@ import core.stdc.stdio;
|
|||
|
||||
/**
|
||||
* TUN maintenance routines in `tunctl.c`
|
||||
* TODO: Use import C here
|
||||
*/
|
||||
extern (C) int createTun(char* interfaceName, int iffFlags);
|
||||
extern (C) int destroyTun(int fd);
|
||||
extern (C) int tunWrite(int fd, char* data, int length);
|
||||
extern (C) int tunRead(int fd, char* data, int amount);
|
||||
import libtun.tunctl;
|
||||
// extern (C) int createTun(char* interfaceName, int iffFlags);
|
||||
// extern (C) int destroyTun(int fd);
|
||||
// extern (C) int tunWrite(int fd, char* data, int length);
|
||||
// extern (C) int tunRead(int fd, char* data, int amount);
|
||||
|
||||
public class TUNAdapter
|
||||
public class Adapter
|
||||
{
|
||||
/* Tunnel device descriptor */
|
||||
private int tunFD;
|
||||
|
@ -24,6 +26,15 @@ public class TUNAdapter
|
|||
private byte[] scratch;
|
||||
|
||||
|
||||
/**
|
||||
* Instantiates a new Adapter with the given interface name
|
||||
* and optionally you can specifiy the adapter type (default
|
||||
* is TAP)
|
||||
*
|
||||
* Params:
|
||||
* interfaceName = the name of the interface to create
|
||||
* adapterType = The AdapterType to use
|
||||
*/
|
||||
this(string interfaceName, AdapterType adapterType = AdapterType.TAP)
|
||||
{
|
||||
init(interfaceName, adapterType);
|
||||
|
@ -50,6 +61,17 @@ public class TUNAdapter
|
|||
}
|
||||
}
|
||||
|
||||
public void setAddress()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the adapter
|
||||
*
|
||||
* Throws:
|
||||
* TUNException if the operation failed
|
||||
*/
|
||||
public void close()
|
||||
{
|
||||
sanityCheck();
|
||||
|
@ -58,7 +80,15 @@ public class TUNAdapter
|
|||
destroyTun(tunFD);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Blocks to receive into the buffer
|
||||
*
|
||||
* Params:
|
||||
* buffer = The buffer variable to write the received
|
||||
* data into
|
||||
* Throws:
|
||||
* TUNException if the read failed
|
||||
*/
|
||||
public void receive(ref byte[] buffer)
|
||||
{
|
||||
sanityCheck();
|
||||
|
@ -90,17 +120,16 @@ public class TUNAdapter
|
|||
/* Copy the data into their buffer (and of correct length) */
|
||||
buffer = scratch[0..status].dup;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the provided data
|
||||
*
|
||||
* Params:
|
||||
* buffer = The data to send
|
||||
* Throws:
|
||||
* TUNException if an error occurs
|
||||
*/
|
||||
public void send(byte[] buffer)
|
||||
{
|
||||
sanityCheck();
|
||||
|
|
|
@ -8,7 +8,7 @@ import core.thread;
|
|||
void main()
|
||||
{
|
||||
writeln("Edit source/app.d to start your project.");
|
||||
TUNAdapter adapter = new TUNAdapter("testInterface0");
|
||||
Adapter adapter = new Adapter("testInterface0");
|
||||
|
||||
while(true)
|
||||
{
|
||||
|
|
|
@ -20,11 +20,20 @@
|
|||
#include<string.h>
|
||||
#include<sys/ioctl.h>
|
||||
#include<unistd.h>
|
||||
#include<stdint.h>
|
||||
|
||||
int createTun(char* interfaceName, int iffFlags)
|
||||
/* TODO: Update types here using stdint */
|
||||
//TODO: We could possibly use directly
|
||||
uint32_t createTun(char* interfaceName, int32_t iffFlags)
|
||||
{
|
||||
/* TODO: Add all required error checking */
|
||||
int tunFD = open("/dev/net/tun", O_RDWR);
|
||||
int32_t tunFD = open("/dev/net/tun", O_RDWR);
|
||||
|
||||
/* If error */
|
||||
if(tunFD < 0)
|
||||
{
|
||||
return tunFD;
|
||||
}
|
||||
|
||||
/* TUN properties */
|
||||
struct ifreq interfaceReqData;
|
||||
|
@ -38,22 +47,24 @@ int createTun(char* interfaceName, int iffFlags)
|
|||
strcpy(interfaceReqData.ifr_name, interfaceName);
|
||||
|
||||
/* Attempt to bring up the tun device node */
|
||||
int tunStatus = ioctl(tunFD, TUNSETIFF, &interfaceReqData);
|
||||
int32_t tunStatus = ioctl(tunFD, TUNSETIFF, &interfaceReqData);
|
||||
|
||||
if(tunStatus < 0)
|
||||
{
|
||||
tunFD = tunStatus;
|
||||
tunFD = tunStatus;
|
||||
}
|
||||
|
||||
return tunFD;
|
||||
}
|
||||
|
||||
int destroyTun(int fd)
|
||||
//TOOD: Maybe use directly
|
||||
uint32_t destroyTun(uint32_t fd)
|
||||
{
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
int tunWrite(int fd, char* data, int length)
|
||||
//TODO: Maybe use directly
|
||||
uint32_t tunWrite(uint32_t fd, char* data, int length)
|
||||
{
|
||||
write(fd, data, length);
|
||||
}
|
||||
|
@ -64,7 +75,11 @@ int tunWrite(int fd, char* data, int length)
|
|||
*
|
||||
* (FIXME: For now we just read 20 bytes)
|
||||
*/
|
||||
int tunRead(int fd, char* data, int amount)
|
||||
//TODO: Maybe use directly
|
||||
uint32_t tunRead(uint32_t fd, char* data, int amount)
|
||||
{
|
||||
return read(fd, data, amount);
|
||||
}
|
||||
|
||||
|
||||
// uint32_t tunSet()
|
Loading…
Reference in New Issue