Recent

Author Topic: is there an internet connection?  (Read 4510 times)

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: is there an internet connection?
« Reply #45 on: February 16, 2023, 06:39:20 am »
Here is a list of servers I would prefer to contact, but I do not know how. http://4.2.2.1 does not work.
https://sebastianhetzel.net/zuverlaessige-oeffentliche-dns-server/

As I said before, those are DNS servers.
However, they seem to be either https://www.rfc-editor.org/rfc/rfc5966 or https://www.rfc-editor.org/rfc/rfc7766 compliant, as they are accepting TCP connections on port 53.

If you don't care if they are working or not, but that you are merely able to connect to at least one, something like the following should work:

Code: Pascal  [Select][+][-]
  1. program checkdns_ex;
  2.  
  3. {$mode objfpc}
  4. {$H+}
  5.  
  6. uses
  7.   baseunix, // needed for TTimeVal (what about windows?)
  8.   Sockets,
  9.   types;
  10.  
  11. function checkConnect (const hostAddress: string; portNumber: integer; timeout: integer = 3): Boolean;
  12.   var
  13.     sock:    LongInt;
  14.     addr:    TSockAddr;
  15.     timeset: TTimeVal; // is TTimeVal available on windows?
  16.   begin
  17.     sock := fpsocket(AF_INET, SOCK_STREAM, 0);
  18.     if sock = -1 then begin
  19.       result := false;
  20.       Exit;
  21.     end;
  22.  
  23.     timeset.tv_sec  := timeout;
  24.     timeset.tv_usec := 0;
  25.     fpsetsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, @timeset, SizeOf(timeset));
  26.  
  27.     addr.sin_family := AF_INET;
  28.     addr.sin_port   := htons(portNumber);
  29.     addr.sin_addr   := StrToNetAddr(hostAddress);
  30.  
  31.     result := 0 = fpconnect(sock, @addr, SizeOf(addr));
  32.  
  33.     CloseSocket(Sock);
  34.   end;
  35.  
  36. function checkAnyConnect (const hosts: TStringDynArray; const port: integer = 53): boolean;
  37.   var
  38.     host: string;
  39.   begin
  40.     for host in hosts do if checkConnect(host, port) then exit(true);
  41.     result := false;
  42.   end;
  43.  
  44. begin
  45.   if checkAnyConnect([
  46.     '4.2.2.1',
  47.     '4.2.2.2',
  48.     '4.2.2.3',
  49.     '4.2.2.4',
  50.     '4.2.2.5',
  51.     '4.2.2.6']) then writeln('Connection Success')
  52.   else
  53.     writeln('Connection Failed');
  54. end.

I don't have any Windows setup to test on, I tested on Linux. I'm not sure on Windows, I had to use baseunix for the TTimeVal type definition to set a decent connection timeout.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: is there an internet connection?
« Reply #46 on: February 16, 2023, 06:47:25 am »
I don't have any Windows setup to test on, I tested on Linux. I'm not sure on Windows, I had to use baseunix for the TTimeVal type definition to set a decent connection timeout.

On Windows it is likely winsock or winsock2 that is needed for TTimeVal.

balazsszekely

  • Guest
Re: is there an internet connection?
« Reply #47 on: February 16, 2023, 07:50:18 am »
I don't have any Windows setup to test on, I tested on Linux. I'm not sure on Windows, I had to use baseunix for the TTimeVal type definition to set a decent connection timeout.

On Windows it is likely winsock or winsock2 that is needed for TTimeVal.
With a small modification your code works on windows too. Here is a cross platform version, tested on Win10/Linux Mint 20.02, both 64 bit:
Code: Pascal  [Select][+][-]
  1. program checkdns_ex;
  2.  
  3. {$mode objfpc}
  4. {$H+}
  5.  
  6. uses
  7.   Classes, Sysutils, Sockets, Types,
  8.   {$IFDEF WINDOWS}
  9.   Windows,
  10.   Winsock;
  11.   {$ELSE}
  12.   BaseUnix,
  13.   UnixType;
  14.   {$ENDIF}
  15.  
  16. function checkConnect (const hostAddress: string; portNumber: integer; timeout: integer = 3): Boolean;
  17. var
  18.   sock:    LongInt;
  19.   addr:    TSockAddr;
  20.   timeset: TTimeVal; // is TTimeVal available on windows?
  21. begin
  22.   sock := fpsocket(AF_INET, SOCK_STREAM, 0);
  23.   if sock = -1 then begin
  24.     result := false;
  25.     Exit;
  26.   end;
  27.  
  28.   timeset.tv_sec  := timeout;
  29.   timeset.tv_usec := 0;
  30.   fpsetsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, @timeset, SizeOf(timeset));
  31.  
  32.   addr.sin_family := AF_INET;
  33.   addr.sin_port   := htons(portNumber);
  34.   addr.sin_addr   := TInAddr(StrToNetAddr(hostAddress));
  35.  
  36.   result := 0 = fpconnect(sock, @addr, SizeOf(addr));
  37.  
  38.   CloseSocket(Sock);
  39. end;
  40.  
  41. function checkAnyConnect (const hosts: TStringDynArray; const port: integer = 53): boolean;
  42. var
  43.   host: string;
  44. begin
  45.   for host in hosts do if checkConnect(host, port) then exit(true);
  46.   result := false;
  47. end;
  48.  
  49. begin
  50.   if checkAnyConnect([
  51.     '4.2.2.1',
  52.     '4.2.2.2',
  53.     '4.2.2.3',
  54.     '4.2.2.4',
  55.     '4.2.2.5',
  56.     '4.2.2.6']) then writeln('Connection Success')
  57.   else
  58.     writeln('Connection Failed');
  59. end.

 

TinyPortal © 2005-2018