sslsockets, TSSLSocketHandler, TInetSocket - example? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- prof7bit · 2022-11-15 15:23 There is a TInetSocket in ssockets (in fcl-net), It wraps a TCP socket into a TStream, I am using it to implement a TCP client like so: 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     try    FSocket := TInetSocket.Create(Host, Port);      FSocket.WriteByte(42);      ...  There is also a unit sslsockets that implements a TSSLSocketHandler that seems to be meant to be plugged into the TInetSocket constructor as the 3rd argument, probably to make it SSL capable. But TSSLSocketHandler is not meant to be used directly because it has abstract methods. Is there any example of how this code is supposed to look like, how it is meant to be used? What do I need to implement to make the above connection with TInetSocket talk to an SSL server? ---------------------------------------------------------------------- Warfley · 2022-11-15 16:20 Use "TSSLSocketHandler.GetDefaultHandler" to create an SSL handler with the default handler class. To register the default handler class to be OpenSSL simply import the "opensslsockets" unit in your uses clause and you should be fine 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 uses  ssockets, sslsockets, opensslsockets; ...FSocket := TInetSocket.Create(Host, Port, TSSLSocketHandler.GetDefaultHandler); ---------------------------------------------------------------------- prof7bit · 2022-11-16 10:52 Quote from: Warfley on November 15, 2022, 05:20:14 pmUse "TSSLSocketHandler.GetDefaultHandler" to create an SSL handler with the default handler class. To register the default handler class to be OpenSSL simply import the "opensslsockets" unit in your uses clause and you should be fine 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 uses  ssockets, sslsockets, opensslsockets; ...FSocket := TInetSocket.Create(Host, Port, TSSLSocketHandler.GetDefaultHandler); Thank you. Unfortunatley this does not seem to work. If I run that code (with FPC latest main) I get this exception: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i An unhandled exception occurred at $00000000004AE9E1:EAccessViolation: Access violation  $00000000004AE9E1  Write,  line 624 of openssl/src/fpopenssl.pp  $0000000000460A18  WriteByte,  line 1221 of ../objpas/classes/streams.inc  If I single step into it I notice that in opensslsockets.pp, line 304, in method TOpenSSLSocketHandler.Send in that line: 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     Result:=FSsl.Write(@Buffer,Count);  FSsl is nil! So there seems to be some initialization missing or broken. I am using Ubuntu 22.04.1 LTS and I have libssl.so.3 (which is why I need FPC trunk) Does the following minimal test program run on our system? 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 program test1; uses  ssockets, sslsockets, opensslsockets; var  FSocket: TInetSocket; begin  FSocket := TInetSocket.Create('google.com', 443, TSSLSocketHandler.GetDefaultHandler);  FSocket.WriteByte(0);  FSocket.Free;end. ---------------------------------------------------------------------- prof7bit · 2022-11-16 11:18 Found it. I need to explicitly call Connect after Create. When a sockethandler is passed then the behavior of the constructor changes, without passing a handler the connect happens immediately. ---------------------------------------------------------------------- prof7bit · 2022-11-16 13:22 Ok, for the sake of completeness, I think I have figured out how to use this in a real world application, because there was another minor inconvenience when trying to shut down and close the socket while a thread is in a blocking read on that socket. Also there is another minor inconvenience when using FPC stable or fixes release: the supported libssl versions are outdated (already fixed in main), it would not work on current Linux distributions anymore without patching the versions array. This minimal demo example is what I came up with: 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 program test1; uses  cthreads, Classes, sysutils, sockets, ssockets, sslsockets, opensslsockets, openssl; type   { TReadThread }   TReadThread = class(TThread)    procedure Execute; override;  end; const  SSL = True; var  Thread: TReadThread;  Socket: TInetSocket;  SSLHandler: TOpenSSLSocketHandler; { TReadThread } procedure TReadThread.Execute;var  B: Byte;begin  writeln('thread: starting');  try    repeat      B := Socket.ReadByte;      writeln(B);    until Terminated;   except    on E: EReadError do begin      WriteLn('thread: read error means socket was shut down');  end;    on E: Exception do begin      writeln('thread: some other exception');      Writeln(E.ClassName);    end;  end;  writeln('thread: ending');end; begin   // ############## connect   if SSL then begin    if not InitSSLInterface then begin      writeln('did not find libssl version 1, trying version 3');      // this is already fixed in fpc trunk      openssl.DLLVersions[1] := '.3'; // patch the versions array    end;    if not InitSSLInterface then begin      writeln('coul not find any usable ssl support');      halt(1);    end;    SSLHandler := TOpenSSLSocketHandler.create;    Socket := TInetSocket.Create('google.com', 443, SSLHandler);    Socket.Connect;  end  else begin    Socket := TInetSocket.Create('google.com', 80);  end;  Thread := TReadThread.Create(False);    // wait some time while other thread is in blocking read  Sleep(1000);    // ############## close connection   Thread.Terminate;   writeln('force it to exit blocking read in a defined manner');  if SSL then begin    // we need to shutdown the ssl handler, not the socket,    // otherwise the entire program would crash    SSLHandler.Shutdown(True);  end  else begin    fpshutdown(Socket.Handle, SHUT_RDWR);  end;   Socket.Free;  Sleep(1000);  writeln('done');end.  This should output something like this: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i did not find libssl version 1, trying version 3thread: startingforce it to exit blocking read in a defined mannerthread: read error means socket was shut downthread: endingdone ---------------------------------------------------------------------- r.lukasiak · 2023-09-29 05:55 @prof7bit great example! I've applied it to my code and it seems to be working ok but how could I apply it on the server side? I'm playing with the demos isocksvr.pp and isockcli.pp