Linux TUN lazarus / 32-Linux ---------------------------------------------------------------------- Hiko222 · 2018-04-20 18:20 Hi, I can create a TUN(( Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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;  fpIOCtl returned -1, fpgeterrno returned 77. Please help! ---------------------------------------------------------------------- Leledumbo · 2018-04-22 07:20 Quote from: Hiko222 on April 20, 2018, 08:20:45 pmfpIOCtl returned -1, fpgeterrno returned 77. Please help! 77 is EBADFD, that means your fpopen return value is actually invalid. Did you run with sudo? Normal users shouldn't have privileges to open device. ---------------------------------------------------------------------- Hiko222 · 2018-04-22 08:31 Quote from: Leledumbo on April 22, 2018, 09:20:29 amQuote from: Hiko222 on April 20, 2018, 08:20:45 pmfpIOCtl returned -1, fpgeterrno returned 77. Please help! 77 is EBADFD, that means your fpopen return value is actually invalid. Did you run with sudo? Normal users shouldn't have privileges to open device. I run code from the root. I wrote a library for c++, it creates tun normally. ---------------------------------------------------------------------- zeljko · 2018-04-22 11:53 Are you sure that your record ifmap is exact same as C struct ? ---------------------------------------------------------------------- Hiko222 · 2018-04-22 20:42 Quote from: zeljko on April 22, 2018, 01:53:13 pmAre you sure that your record ifmap is exact same as C struct ? yes struct ifmap {     unsigned long   mem_start;     unsigned long   mem_end;     unsigned short  base_addr;     unsigned char   irq;                      unsigned char   dma;     unsigned char   port; }; ---------------------------------------------------------------------- Laksen · 2018-04-22 20:49 Have you verified that the structs are the right size compared to the C versions? You might have to add a {$packrecords C} in there first ---------------------------------------------------------------------- Hiko222 · 2018-04-22 21:04 Quote from: Laksen on April 22, 2018, 10:49:58 pmHave you verified that the structs are the right size compared to the C versions? You might have to add a {$packrecords C} in there first fpgeterrno returned 77(( ---------------------------------------------------------------------- benohb · 2018-04-22 23:53 what do you want to do . Maybe there is another way shared some of your thoughts ---------------------------------------------------------------------- Hiko222 · 2018-04-23 06:25 Quote from: benohb on April 23, 2018, 01:53:12 amwhat do you want to do . Maybe there is another way shared some of your thoughts I want to create a Tun interface! But it does not work. ---------------------------------------------------------------------- Thaddy · 2018-04-23 07:00 Make sure your records are all packed C, like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IFDEF FPC}{$PACKRECORDS C}{$ENDIF}  type    ifmap = packed record  // declare ALL records as packed, also all child records.        mem_start : dword;        mem_end : dword;        base_addr : word;      irq : byte;        dma : byte;        port : byte;      end;Here your original code works (as root). I only added all the packed and changed char to byte because a C char is actually a byte. If you do not use packed, a record uses natural alignment. You *must* use packed afaik. ---------------------------------------------------------------------- benohb · 2018-04-24 00:04 Quote from: Thaddy on April 23, 2018, 09:00:07 amMake sure your records are all packed C, like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IFDEF FPC}{$PACKRECORDS C}{$ENDIF}  type    ifmap = packed record  // declare ALL records as packed, also all child records.        mem_start : dword;        mem_end : dword;        base_addr : word;      irq : byte;        dma : byte;        port : byte;      end;Here your original code works (as root). I only added all the packed and changed char to byte because a C char is actually a byte. If you do not use packed, a record uses natural alignment. You *must* use packed afaik. Does the code work in your computer ?? ---------------------------------------------------------------------- Thaddy · 2018-04-24 07:50 Only if there is an interface established (I use both OpenSSH and vtund). I forgot to tell the second const needs to be cint cast. ---------------------------------------------------------------------- Hiko222 · 2018-04-24 15:03 So everything is fine, I created an interface. I can read from it. But I can not write to it ... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   var    data : string[10] = '1234567890';  begin    writeln(fileDescriptor);  writeln(fpWrite(fileDescriptor, data[1], 1));    writeln(fpgeterrno);  fileDescriptor = 5 fpWrite= -1 fpgeterrno = 22 I updated the main code in the first message! с code write data fine! ---------------------------------------------------------------------- Hiko222 · 2018-04-25 16:23 the solution of the problem found here https://github.com/songgao/water/issues/18 Thanks for the help! ---------------------------------------------------------------------- Hiko222 · 2018-04-26 07:17 Why the use of IFF_NO_PI in c ++ goes fine and at FPC at write causes an error of 22 (EINVAL)?