lib lsocket.pbi and ltls.pbi for Network Sockets and TLS Connections.

Linux specific forum
Ground0
User
User
Posts: 40
Joined: Fri Jun 09, 2006 10:20 pm
Location: Stilli, Schweiz
Contact:

lib lsocket.pbi and ltls.pbi for Network Sockets and TLS Connections.

Post by Ground0 »

Original Title: libressl tls test and network is blocked ... SOLVED :-)
Hello all ...

I hope someone can help me here ...
I was doing it wrong way :-/
Delete:I receive everytime a TLS_WANT_POLLIN ... I think the Network Library is blocking me out ...

Now i'm on the Client Socket and not anymore on Server... The New Fail is as follow :

Code: Select all

Konfiguriert
#PB_NetworkEvent_Connect New Client Socket:36695088
#PB_NetworkEvent_Data Network Client Upgraded to tls cctx = 36695088
#PB_NetworkEvent_Data TLS: [36695088] from ClienID:36691848 IP:127.0.0.1
#PB_NetworkEvent_Data Length=-1
tls_read Fehler:[handshake failed: error:02FFF009:system library:func(4095):Bad file descriptor]
Did someone have any more idea ?

Code: Select all

;tls test libressl

EnableExplicit

InitNetwork()

PrototypeC.i Pr_tls_init()
PrototypeC.i Pr_tls_config_new()
PrototypeC.i Pr_tls_load_file_pw(FileName.p-utf8, Length, Password.p-utf8)
PrototypeC.i Pr_tls_load_file(FileName.p-utf8, Length, Passord.s = #Null$)
PrototypeC.i Pr_tls_config_set_ca_mem(*tls_config , Memory.i, Length.i)
PrototypeC.i Pr_tls_config_set_cert_mem(*tls_config , Memory.i, Length.i)
PrototypeC.i Pr_tls_config_set_key_mem(*tls_config , Memory.i, Length.i)
PrototypeC.i Pr_tls_server()
PrototypeC.i Pr_tls_configure(*ctx, *cfg)
PrototypeC.i Pr_tls_error(*ctx)
PrototypeC.i Pr_tls_accept_socket(*ctx, *cctx, Socket.l)
PrototypeC.i Pr_tls_accept_cbs(*ctx, *cctx, read_cb.i, write_cb.i, arg_ch.i = #Null)
PrototypeC.i Pr_tls_read(*cctx, *buffer, Size.i)
PrototypeC.i Pr_tls_handshake(*cctx)
PrototypeC.i Pr_tls_close(*cctx)
PrototypeC.i Pr_tls_free(*ctx)
PrototypeC.i Pr_tls_config_free(*cfg)


#TLS_WANT_POLLIN = -2
#TLS_WANT_POLLOUT   = -3
   
Define libressl_tls = OpenLibrary(#PB_Any, "./libtls.so")

If IsLibrary(libressl_tls)
  Define Call_tls_init = GetFunction(libressl_tls, "tls_init")
  Define Call_tls_config_new = GetFunction(libressl_tls, "tls_config_new")
  Define Call_tls_load_file = GetFunction(libressl_tls, "tls_load_file")
  Define Call_tls_config_set_ca_mem = GetFunction(libressl_tls, "tls_config_set_ca_mem")
  Define Call_tls_config_set_cert_mem = GetFunction(libressl_tls, "tls_config_set_cert_mem")
  Define Call_tls_config_set_key_mem = GetFunction(libressl_tls, "tls_config_set_key_mem")
  Define Call_tls_server = GetFunction(libressl_tls, "tls_server")
  Define Call_tls_configure = GetFunction(libressl_tls, "tls_configure")
  Define Call_tls_error = GetFunction(libressl_tls, "tls_error")
  Define Call_tls_accept_socket = GetFunction(libressl_tls, "tls_accept_socket")
  Define Call_tls_accept_cbs = GetFunction(libressl_tls, "tls_accept_cbs")
  Define Call_tls_read = GetFunction(libressl_tls, "tls_read")
  Define Call_tls_handshake = GetFunction(libressl_tls, "tls_handshake")
  Define Call_tls_close = GetFunction(libressl_tls, "tls_close")
  Define Call_tls_free = GetFunction(libressl_tls, "tls_free")
  Define Call_tls_config_free = GetFunction(libressl_tls, "tls_config_free")
Else
  Debug "Library Not found"
  End
EndIf


Define.Pr_tls_init tls_init = Call_tls_init
Define.Pr_tls_config_new tls_config_new = Call_tls_config_new
Define.Pr_tls_load_file tls_load_file = Call_tls_load_file
Define.Pr_tls_load_file_pw tls_load_file_pw = Call_tls_load_file
Define.Pr_tls_config_set_ca_mem tls_config_set_ca_mem = Call_tls_config_set_ca_mem
Define.Pr_tls_config_set_cert_mem tls_config_set_cert_mem = Call_tls_config_set_cert_mem
Define.Pr_tls_config_set_key_mem tls_config_set_key_mem = Call_tls_config_set_key_mem
Define.Pr_tls_server tls_server = Call_tls_server
Define.Pr_tls_configure tls_configure = Call_tls_configure
Define.Pr_tls_error tls_error = Call_tls_error
Define.Pr_tls_accept_socket tls_accept_socket = Call_tls_accept_socket
Define.Pr_tls_accept_cbs tls_accept_cbs = Call_tls_accept_cbs
Define.Pr_tls_read tls_read = Call_tls_read
Define.Pr_tls_handshake tls_handshake = Call_tls_handshake
Define.Pr_tls_close tls_close = Call_tls_close
Define.Pr_tls_free tls_free = Call_tls_free
Define.Pr_tls_config_free tls_config_free = Call_tls_config_free

Define *ctx, tls_mem_ptr, *cfg, handle, tls_socket, *cctx, *Error
Define Length, ReceivedLength
Define.s Ca, Cert, Key, Password, Error, S_Buffer
Define server_read, client_read, Socket_ID, res, listen
Define SEvent,Server_ID, Client_ID, *Buffer, Quit, BufferSize
BufferSize = 65535
*Buffer = AllocateMemory(BufferSize)
S_Buffer = Space(65535)
*ctx = #Null
*cctx = #Null
*cfg = #Null

If tls_init() = 0
  Debug "Tls_Init Ok"
  *cfg = tls_config_new()
  Debug "Tls_cfg:"+Str(*cfg)
  Ca = "/home/renlin/dev/libressl/sample/test_ca.pem"
  tls_mem_ptr = tls_load_file(Ca, @Length)
  Debug "CA:"+Ca
  If tls_mem_ptr
    Debug "tls_load_file tls_mem_ptr:"+Str(tls_mem_ptr)
    Debug "tls_load_file Length:"+Str(Length)
    Debug "tls_load_file Peeks:"+PeekS(tls_mem_ptr, Length, #PB_Ascii)
    Debug tls_config_set_ca_mem(*cfg, tls_mem_ptr, Length)
  Else
    Debug "load Failed"
    End
  EndIf
 
  Cert = "/home/renlin/dev/libressl/sample/test_server_cert.pem"
  tls_mem_ptr = tls_load_file(Cert, @Length)
  Debug "Cert:"+Cert
  If tls_mem_ptr
    Debug "tls_load_file tls_mem_ptr:"+Str(tls_mem_ptr)
    Debug "tls_load_file Length:"+Str(Length)
    Debug "tls_load_file Peeks:"+PeekS(tls_mem_ptr, Length, #PB_Ascii)
    Debug tls_config_set_cert_mem(*cfg, tls_mem_ptr, Length)
  Else
    Debug "load Failed"
    End
  EndIf
 
  Key = "/home/renlin/dev/libressl/sample/test_server_key.pem" ;/home/renlin/dev/libressl/sample/
  Password = "test-server-pass"
  tls_mem_ptr = tls_load_file_pw(Key, @Length, Password)
  Debug "Key:"+Key
  If tls_mem_ptr
    Debug "tls_load_file tls_mem_ptr:"+Str(tls_mem_ptr)
    Debug "tls_load_file Length:"+Str(Length)
    Debug "tls_load_file Peeks:"+PeekS(tls_mem_ptr, Length, #PB_Ascii)
    Debug tls_config_set_key_mem(*cfg, tls_mem_ptr, Length)
  Else
    Debug "load Failed"
    End
  EndIf
  *ctx = tls_server()
  If *ctx
    Debug "tls_server = " + Str(*ctx)
  Else
    Debug "tls_server failed = " + Str(*ctx)
    End
  EndIf
  If tls_configure(*ctx, *cfg) = 0
    Debug "Konfiguriert"
  Else
    Error = PeekS(tls_error(*ctx),-1,#PB_Ascii)
    Debug "Fehler:["+Error+"]"
    End
  EndIf
  If CreateNetworkServer(0, 1234)
;   Socket_ID = socket_(#AF_INET, #SOCK_STREAM, 6)
;   Debug "Socket ID"+Str(Socket_ID)
;   sockinfo\sin_family = #AF_INET
;   sockinfo\sin_port = htons_(12345)
;   res = bind_(Socket_ID, *sock, SizeOf(sockaddr_in))
;   Debug "Bind" + Str(res)
;   If Socket_ID > 0
;     listen_(Socket_ID, 5)
    ;Warten auf Datenempfang:
     Repeat
    
       SEvent = NetworkServerEvent()
   
      If SEvent
     
        Client_ID = EventClient()
   
        Select SEvent
          Case #PB_NetworkEvent_None
            Delay(1)
          Case #PB_NetworkEvent_Connect
            tls_socket = tls_accept_socket(*ctx, @*cctx, Client_ID)
            If tls_socket = -1
              Error = PeekS(tls_error(*ctx),-1,#PB_Ascii)
              Debug "#PB_NetworkEvent_Connect Fehler:["+Error+"]"
              End
            ElseIf tls_socket = 0 
              Debug "#PB_NetworkEvent_Connect New Client Socket:"+Str(*cctx)
            Else
              Debug "Completly Failed"
              End
              
            EndIf  
                
          Case #PB_NetworkEvent_Data
;             Debug "Client "+Str(Client_ID)+" has send a packet !"
;               ;ReceiveNetworkData(Client_ID, *Buffer, 1000)
;               ;Debug "String Undecoded: "+PeekS(*Buffer, -1, #PB_UTF8)
;               ;ReceiveNetworkData(Client_ID, *Buffer, 1000)
;             
;             
;             
            Repeat
              If *cctx = 0
                Debug "#PB_NetworkEvent_Data Socket Upgrade wurde nicht beim Connect durchgeführt"
                tls_socket = tls_accept_socket(*ctx, @*cctx, Client_ID)
                If tls_socket = -1
                  Error = PeekS(tls_error(*ctx),-1,#PB_Ascii)
                  Debug "#PB_NetworkEvent_Data Fehler:["+Error+"]"
                  End
                ElseIf tls_socket = 0
                  Debug "#PB_NetworkEvent_Data Jetzt:"+Str(*cctx)
                Else
                  Debug "Completly Failed."
                  End
                EndIf  
              EndIf
              
              If *cctx > 0
                Debug "#PB_NetworkEvent_Data Network Client Upgraded to tls cctx = " + Str(*cctx)
                Debug "#PB_NetworkEvent_Data TLS: ["+Str(*cctx) + "] from ClienID:" + Str(Client_ID) + " IP:" +IPString(GetClientIP(Client_ID))
                ;Debug "FCNTL: GET:" + Hex(fcntl_(Client_ID, #F_GETFL,0))
;                 If tls_handshake(*ctx) = -1
;                   Debug "#PB_NetworkEvent_Data Handshacke Failed."
;                   End
;                 Else
;                   Debug "#PB_NetworkEvent_Data Handshake I.O."
;                 EndIf
;                  ReceiveNetworkData(Client_ID, *Buffer, 1000)
;                  Debug "String Undecoded: "+PeekS(*Buffer, -1, #PB_Ascii) ; Plain Empfang funktioniert
;                  End
                
                ReceivedLength = tls_read(*cctx, @S_Buffer, 1000)    ; Funktioniert nicht ...
                Debug "#PB_NetworkEvent_Data Length="+Str(ReceivedLength)
                
                If ReceivedLength >= 0
                  ;Debug "String: "+PeekS(*Buffer, ReceivedLength, #PB_UTF8)
                  Debug "String:"+S_Buffer
                  Quit = 1
                  End
                ElseIf ReceivedLength = -1
                  *Error = tls_error(*cctx)
                  If *error > 0
                    Error = PeekS(*Error,-1,#PB_Ascii)
                    Debug "tls_read Fehler:["+Error+"]"
                    End
                  Else
                    Debug *error
                    Debug "End error unknown"
                    End
                  EndIf
                ElseIf ReceivedLength = #TLS_WANT_POLLIN
                  ;Blocked !!!
                  ;How to free ???
                  End
                  
                  ;save_fd = fcntl( create_socket, F_GETFL );
                  ;save_fd |= O_NONBLOCK;
                  ;fcntl( create_socket, F_SETFL, save_fd );
                  
                EndIf
              Else
                Quit = 1
                Error = PeekS(tls_error(*ctx),-1,#PB_Ascii)
                Debug "Fehler:["+Error+"]"
              EndIf
              
            Until Quit = 1
            Quit = 0
          Case #PB_NetworkEvent_Disconnect
            If tls_close(*cctx) <> 0
              *Error = tls_error(*cctx)
              If *error > 0
                Error = PeekS(*Error,-1,#PB_Ascii)
                Debug "tls_read Fehler:["+Error+"]"
                End
              Else
                Debug *error
                Debug "End error unknown"
                End
              EndIf
            Else
              tls_free(*cctx)
            EndIf
            
     
        EndSelect
      EndIf
     
    Until Quit = 1
    tls_free(*ctx)
    tls_config_free(*cfg)
     
  Else
    Debug "NEttwerkfehler"
  EndIf
 
Else
  Debug "Tls Library could Not be initialized"
  End
EndIf 
I use a self compiled Version from libressl: https://ftp.openbsd.org/pub/OpenBSD/Lib ... 2.2.tar.gz
Copied all compiled .so files to the sample directory and PureBASIC Compiler set to first create Executable before run (Compile/Run F5 Function)

The Source to test everything is from Github Gist... : https://gist.github.com/kinichiro/9ac1f ... 9ffac7d098

I think i didn't anything wrong with Library because the Gist Sample works. (Also is everything Working until tls_read(.....))

Greeting Ground0
Last edited by Ground0 on Tue May 25, 2021 9:30 am, edited 1 time in total.
Sys 1 HW: Ryzen 7 3700X, 32GB RAM, RX 5700, NVME 512GiB And SATA 2TiB OS: openSUSE Tumbleweed x64
Sys 2 HW: 7 1700X, 16GB RAM, R7 360, SSD 480GiB & 1TiB OS: "
Sys 3 NB: HP envy X360, 5 2500U ,16GB RAM, NVME 256Gib OS: " zus. Win. 10 x64
Ground0
User
User
Posts: 40
Joined: Fri Jun 09, 2006 10:20 pm
Location: Stilli, Schweiz
Contact:

Re: libressl tls test and network is blocked ... SOLVED :-)

Post by Ground0 »

Here is the new Code:

Code: Select all

;tls test libressl

EnableExplicit

;InitNetwork()

PrototypeC.i Pr_tls_init()
PrototypeC.i Pr_tls_config_new()
PrototypeC.i Pr_tls_load_file_pw(FileName.p-utf8, Length, Password.p-utf8)
PrototypeC.i Pr_tls_load_file(FileName.p-utf8, Length, Passord.s = #Null$)
PrototypeC.i Pr_tls_config_set_ca_mem(*tls_config , Memory.i, Length.i)
PrototypeC.i Pr_tls_config_set_cert_mem(*tls_config , Memory.i, Length.i)
PrototypeC.i Pr_tls_config_set_key_mem(*tls_config , Memory.i, Length.i)
PrototypeC.i Pr_tls_server()
PrototypeC.i Pr_tls_configure(*ctx, *cfg)
PrototypeC.i Pr_tls_error(*ctx)
PrototypeC.i Pr_tls_accept_socket(*ctx, *cctx, Socket.l)
PrototypeC.i Pr_tls_accept_cbs(*ctx, *cctx, read_cb.i, write_cb.i, arg_ch.i = #Null)
PrototypeC.i Pr_tls_read(*cctx, *buffer, Size.i)
PrototypeC.i Pr_tls_handshake(*cctx)
PrototypeC.i Pr_tls_close(*cctx)
PrototypeC.i Pr_tls_free(*ctx)
PrototypeC.i Pr_tls_config_free(*cfg)


#TLS_WANT_POLLIN = -2
#TLS_WANT_POLLOUT   = -3
   
Define libressl_tls = OpenLibrary(#PB_Any, "./libtls.so")

If IsLibrary(libressl_tls)
  Define Call_tls_init = GetFunction(libressl_tls, "tls_init")
  Define Call_tls_config_new = GetFunction(libressl_tls, "tls_config_new")
  Define Call_tls_load_file = GetFunction(libressl_tls, "tls_load_file")
  Define Call_tls_config_set_ca_mem = GetFunction(libressl_tls, "tls_config_set_ca_mem")
  Define Call_tls_config_set_cert_mem = GetFunction(libressl_tls, "tls_config_set_cert_mem")
  Define Call_tls_config_set_key_mem = GetFunction(libressl_tls, "tls_config_set_key_mem")
  Define Call_tls_server = GetFunction(libressl_tls, "tls_server")
  Define Call_tls_configure = GetFunction(libressl_tls, "tls_configure")
  Define Call_tls_error = GetFunction(libressl_tls, "tls_error")
  Define Call_tls_accept_socket = GetFunction(libressl_tls, "tls_accept_socket")
  Define Call_tls_accept_cbs = GetFunction(libressl_tls, "tls_accept_cbs")
  Define Call_tls_read = GetFunction(libressl_tls, "tls_read")
  Define Call_tls_handshake = GetFunction(libressl_tls, "tls_handshake")
  Define Call_tls_close = GetFunction(libressl_tls, "tls_close")
  Define Call_tls_free = GetFunction(libressl_tls, "tls_free")
  Define Call_tls_config_free = GetFunction(libressl_tls, "tls_config_free")
Else
  Debug "Library Not found"
  End
EndIf


Define.Pr_tls_init tls_init = Call_tls_init
Define.Pr_tls_config_new tls_config_new = Call_tls_config_new
Define.Pr_tls_load_file tls_load_file = Call_tls_load_file
Define.Pr_tls_load_file_pw tls_load_file_pw = Call_tls_load_file
Define.Pr_tls_config_set_ca_mem tls_config_set_ca_mem = Call_tls_config_set_ca_mem
Define.Pr_tls_config_set_cert_mem tls_config_set_cert_mem = Call_tls_config_set_cert_mem
Define.Pr_tls_config_set_key_mem tls_config_set_key_mem = Call_tls_config_set_key_mem
Define.Pr_tls_server tls_server = Call_tls_server
Define.Pr_tls_configure tls_configure = Call_tls_configure
Define.Pr_tls_error tls_error = Call_tls_error
Define.Pr_tls_accept_socket tls_accept_socket = Call_tls_accept_socket
Define.Pr_tls_accept_cbs tls_accept_cbs = Call_tls_accept_cbs
Define.Pr_tls_read tls_read = Call_tls_read
Define.Pr_tls_handshake tls_handshake = Call_tls_handshake
Define.Pr_tls_close tls_close = Call_tls_close
Define.Pr_tls_free tls_free = Call_tls_free
Define.Pr_tls_config_free tls_config_free = Call_tls_config_free

Structure sockaddr 
  sa_family.w
  sa_data.s{14}
EndStructure

Structure in_addr
  s_addr.w  
EndStructure

Structure sockaddr_in
  sin_family.w
  sin_port.w
  sin_addr.in_addr
  sin_zero.s{8}
EndStructure

; socket.h
#PF_INET  = 2
#AF_INET  = #PF_INET

; Socket types:
#SOCK_STREAM	    =  1		; stream (connection) socket	
#SOCK_DGRAM	      =  2		; datagram (conn.less) socket	
#SOCK_RAW	        =  3		; raw socket			
#SOCK_RDM	        =  4		; reliably-delivered message	
#SOCK_SEQPACKET   =  5		; sequential packet socket	
#SOCK_PACKET      = 10		; linux specific way of	
					                ; getting packets at the dev
					                ; level.  For writing rarp And	
					                ; other similar things on the	
					                ; user level.

#MAX      = 16384
#PORT     = 32356 
Define SA.sockaddr 
  
Define *ctx, tls_mem_ptr, *cfg, handle, tls_socket, *cctx, *Error
Define Length, ReceivedLength
Define.s Ca, Cert, Key, Password, Error, S_Buffer
Define server_read, client_read, Socket_ID, res, listen
Define SEvent,Server_ID, Client_ID, *Buffer, Quit, BufferSize
Define.i sockfd, connfd, length
Define servaddr.sockaddr_in
Define cli.sockaddr_in
Define.i Ports
Define.s Bindadress
Define buff.s{16384}
Ports = 1235
Bindadress = "127.0.0.1"
servaddr\sin_family = #AF_INET 
servaddr\sin_addr\s_addr = Inet_addr_(Bindadress)
servaddr\sin_port = htons_(Ports)

BufferSize = 65535
*Buffer = AllocateMemory(BufferSize)
S_Buffer = Space(65535)
*ctx = #Null
*cctx = #Null
*cfg = #Null

If tls_init() = 0
  Debug "Tls_Init Ok"
  *cfg = tls_config_new()
  Debug "Tls_cfg:"+Str(*cfg)
  Ca = "/home/renlin/dev/libressl/sample/test_ca.pem"
  tls_mem_ptr = tls_load_file(Ca, @Length)
  Debug "CA:"+Ca
  If tls_mem_ptr
    Debug "tls_load_file tls_mem_ptr:"+Str(tls_mem_ptr)
    Debug "tls_load_file Length:"+Str(Length)
    Debug "tls_load_file Peeks:"+PeekS(tls_mem_ptr, Length, #PB_Ascii)
    Debug tls_config_set_ca_mem(*cfg, tls_mem_ptr, Length)
  Else
    Debug "load Failed"
    End
  EndIf
 
  Cert = "/home/renlin/dev/libressl/sample/test_server_cert.pem"
  tls_mem_ptr = tls_load_file(Cert, @Length)
  Debug "Cert:"+Cert
  If tls_mem_ptr
    Debug "tls_load_file tls_mem_ptr:"+Str(tls_mem_ptr)
    Debug "tls_load_file Length:"+Str(Length)
    Debug "tls_load_file Peeks:"+PeekS(tls_mem_ptr, Length, #PB_Ascii)
    Debug tls_config_set_cert_mem(*cfg, tls_mem_ptr, Length)
  Else
    Debug "load Failed"
    End
  EndIf
 
  Key = "/home/renlin/dev/libressl/sample/test_server_key.pem" ;/home/renlin/dev/libressl/sample/
  Password = "test-server-pass"
  tls_mem_ptr = tls_load_file_pw(Key, @Length, Password)
  Debug "Key:"+Key
  If tls_mem_ptr
    Debug "tls_load_file tls_mem_ptr:"+Str(tls_mem_ptr)
    Debug "tls_load_file Length:"+Str(Length)
    Debug "tls_load_file Peeks:"+PeekS(tls_mem_ptr, Length, #PB_Ascii)
    Debug tls_config_set_key_mem(*cfg, tls_mem_ptr, Length)
  Else
    Debug "load Failed"
    End
  EndIf
  *ctx = tls_server()
  If *ctx
    Debug "tls_server = " + Str(*ctx)
  Else
    Debug "tls_server failed = " + Str(*ctx)
    End
  EndIf
  If tls_configure(*ctx, *cfg) = 0
    Debug "Konfiguriert"
  Else
    Error = PeekS(tls_error(*ctx),-1,#PB_Ascii)
    Debug "Fehler:["+Error+"]"
    End
  EndIf
  ;If CreateNetworkServer(0, 1234)
  sockfd = socket_(#AF_INET, #SOCK_STREAM, 0)
  If sockfd = -1
    Debug "socket creation failed..."
    End 1
  Else
    Debug "Socket successfully created.."
  EndIf
  ;// Binding newly created socket To given IP And verification 
  If ((bind_(sockfd, @servaddr, SizeOf(servaddr))) <> 0) 
    ;PrintN("socket bind failed...")
    Debug "socket bind failed..."
    End 1 
  Else
    ;PrintN("Socket successfully binded..")
    Debug "Socket successfully binded.."
  EndIf
  ;// Now server is ready To listen And verification 
  If ((listen_(sockfd, 5)) <> 0) 
    ;PrintN("Listen failed...")
    Debug "Listen failed..."
    End 1
  Else
    ;PrintN("Server listening..")
    Debug "Server listening.."
  EndIf
  length = SizeOf(cli); 
                      ;PrintN("IP:"+Str(servaddr\sin_addr\s_addr))
  Debug "IP:"+Str(servaddr\sin_addr\s_addr)
  ;// Accept the Data packet from client And verification 
  connfd = accept_(sockfd, @cli, @length)
  ;PrintN("Accept")
  Debug "Accept"
  If (connfd < 0)
    Debug "server acccept failed...\n"
    End 1
  Else
    Debug "server acccept the client...\n"
    tls_socket = tls_accept_socket(*ctx, @*cctx, connfd)
    If tls_socket = -1
      Error = PeekS(tls_error(*ctx),-1,#PB_Ascii)
      Debug "#PB_NetworkEvent_Connect Fehler:["+Error+"]"
      End
    ElseIf tls_socket = 0 
      Debug "#PB_NetworkEvent_Connect New Client Socket:"+Str(*cctx)
    Else
      Debug "Completly Failed"
      End
      
    EndIf  
                
    If *cctx = 0
      Debug "#PB_NetworkEvent_Data Socket Upgrade wurde nicht beim Connect durchgeführt"
      tls_socket = tls_accept_socket(*ctx, @*cctx, Client_ID)
      If tls_socket = -1
        Error = PeekS(tls_error(*ctx),-1,#PB_Ascii)
        Debug "#PB_NetworkEvent_Data Fehler:["+Error+"]"
        End
      ElseIf tls_socket = 0
        Debug "#PB_NetworkEvent_Data Jetzt:"+Str(*cctx)
      Else
        Debug "Completly Failed."
        End
      EndIf  
    EndIf
    
    If *cctx > 0
      Debug "#PB_NetworkEvent_Data Network Client Upgraded to tls cctx = " + Str(*cctx)
      Debug "#PB_NetworkEvent_Data TLS: ["+Str(*cctx) + "] from ClienID:" + Str(connfd) + " IP:" ;+IPString(GetClientIP(connfd))
      ReceivedLength = tls_read(*cctx, @buff, 16384)    ; Funktioniert nicht ...
      Debug "#PB_NetworkEvent_Data Length="+Str(ReceivedLength)
      If ReceivedLength >= 0
        ;Debug "String: "+PeekS(*Buffer, ReceivedLength, #PB_UTF8)
        Debug "String:"+PeekS(@buff,16384, #PB_Ascii)
        Quit = 1
        End
      ElseIf ReceivedLength = -1
        *Error = tls_error(*cctx)
        If *error > 0
          Error = PeekS(*Error,-1,#PB_Ascii)
          Debug "tls_read Fehler:["+Error+"]"
          End
        Else
          Debug *error
          Debug "End error unknown"
          End
        EndIf
      ElseIf ReceivedLength = #TLS_WANT_POLLIN
        ;Blocked !!!
        ;How to free ???
        End
      EndIf
    Else
      Quit = 1
      Error = PeekS(tls_error(*ctx),-1,#PB_Ascii)
      Debug "Fehler:["+Error+"]"
    EndIf
    tls_free(*ctx)
    tls_config_free(*cfg)
    close_(sockfd) 
  EndIf
 
Else
  Debug "Tls Library could Not be initialized"
  End
EndIf 
What did i do, completly removed the PureBasic Network Code, and moved to Sockets.

But it would be nice if that socket code could be used with normal PureBASIC Code. I think there is some special parameter set, which the TLS library could not use.
If someone have any Ideas ... let me know @fred how did you open an Network Connection , and is it possible to change something this Code would work?
Sys 1 HW: Ryzen 7 3700X, 32GB RAM, RX 5700, NVME 512GiB And SATA 2TiB OS: openSUSE Tumbleweed x64
Sys 2 HW: 7 1700X, 16GB RAM, R7 360, SSD 480GiB & 1TiB OS: "
Sys 3 NB: HP envy X360, 5 2500U ,16GB RAM, NVME 256Gib OS: " zus. Win. 10 x64
Ground0
User
User
Posts: 40
Joined: Fri Jun 09, 2006 10:20 pm
Location: Stilli, Schweiz
Contact:

Re: libressl tls test and network is blocked ... SOLVED :-)

Post by Ground0 »

And the Result of all this hacking:

https://gitea.lihaso.com/PB_Includes/lweb

I'm on reworking to translate everything to english.

The Target Module: https://gitea.lihaso.com/PB_Includes/lw ... n/ltls.pbi
And it works only with https://gitea.lihaso.com/PB_Includes/lw ... socket.pbi

Currently Licensed everything to LGPL v2.1 if someone have a better license idea and or would help ... lets go 8)

Greeting Ground0
Sys 1 HW: Ryzen 7 3700X, 32GB RAM, RX 5700, NVME 512GiB And SATA 2TiB OS: openSUSE Tumbleweed x64
Sys 2 HW: 7 1700X, 16GB RAM, R7 360, SSD 480GiB & 1TiB OS: "
Sys 3 NB: HP envy X360, 5 2500U ,16GB RAM, NVME 256Gib OS: " zus. Win. 10 x64
Ground0
User
User
Posts: 40
Joined: Fri Jun 09, 2006 10:20 pm
Location: Stilli, Schweiz
Contact:

Re: libressl tls test and network is blocked ... SOLVED :-)

Post by Ground0 »

lsocket.pbi and ltls.pbi

Bugfixes:
lsocket.pbi
- Empty Strucutre (Protected Variables cannot be filled by API, now allocate memory and copy it)

New:
lsocket.pbi
- GetSocketIP(Connection.i) Get a IP Address of a connection as String

ltls.pbi
- GetSocket(Connection.i) Get Socket connection from the TLS connection for ex. GetSocketIP()
Sys 1 HW: Ryzen 7 3700X, 32GB RAM, RX 5700, NVME 512GiB And SATA 2TiB OS: openSUSE Tumbleweed x64
Sys 2 HW: 7 1700X, 16GB RAM, R7 360, SSD 480GiB & 1TiB OS: "
Sys 3 NB: HP envy X360, 5 2500U ,16GB RAM, NVME 256Gib OS: " zus. Win. 10 x64
infratec
Always Here
Always Here
Posts: 6810
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: lib lsocket.pbi and ltls.pbi for Network Sockets and TLS Connections.

Post by infratec »

Good work :!:

I overseen this topic completely.
Post Reply