const
IFF_TUN = $0001;
TUNSETIFF = $400454ca;
IFF_NO_PI = $1000;
type
__caddr_t = byte;
{$IFDEF CPU64}
Ifmap = record
mem_start : int64;
mem_end : int64;
base_addr : word;
irq : byte;
dma : byte;
port : byte;
spare : array[0..2] of byte;
end;
{$ELSE}
Ifmap = record
mem_start : dword;
mem_end : dword;
base_addr : word;
irq : byte;
dma : byte;
port : byte;
spare : array[0..2] of byte;
end;
{$ENDIF}
Ifreq = record
ifr_ifrn : record
case longint of
0 : ( ifrn_name : array[0..15] of char );
end;
ifr_ifru : record
case longint of
0 : ( ifru_addr : sockaddr );
1 : ( ifru_dstaddr : sockaddr );
2 : ( ifru_broadaddr : sockaddr );
3 : ( ifru_netmask : sockaddr );
4 : ( ifru_hwaddr : sockaddr );
5 : ( ifru_flags : smallint );
6 : ( ifru_ifindex : longint );
7 : ( ifru_metric : longint );
8 : ( ifru_mtu : longint );
9 : ( ifru_map : ifmap );
10 : ( ifru_slave : array[0..15] of char );
11 : ( ifru_newname : array[0..15] of char );
12 : ( ifru_data : __caddr_t );
end;
end;
function createTunWithName(name, address, subnetmask : String; mtu : Integer) : Integer;
var
fileDescriptor : Integer;
ifr: Ifreq;
begin
fileDescriptor := fpOpen('/dev/net/tun', O_RDWR);
if fileDescriptor < 0 then
exit(ERROR);
ifr.ifr_ifru.ifru_flags := IFF_TUN or IFF_NO_PI;
if length(name) > 0 then
ifr.ifr_ifrn.ifrn_name := name;
if fpIOCtl(fileDescriptor, TUNSETIFF, @ifr) < 0 then
begin
fpClose(fileDescriptor);
exit(ERROR);
end;