Recent

Author Topic: How to detect internet connection!  (Read 15522 times)

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: How to detect internet connection!
« Reply #15 on: May 19, 2019, 04:51:04 pm »
No... external IP, and not even my direct IP. You are being silly. (or have not sufficient understanding how routing works)
Look up my original postings. Both links are still up after all those years. Also demonstrates the difference.  :-X :-X.... Plz take note... You deserve my mild insults...
And YES testing an internet connection can only be done through a full connection. Happens to be my job.. just in case...Anything else can be spoofed from a local network.
(Hence that site is fully static and doesn't need https)
Your posts indicate that your job most likely consists in arguing about things you know nothing, it is likely due to your extensive experience in that area that it might very well be the only thing you do well.

Very impressive!

I enjoy the amusement (another thing you provide for free)... keep going :)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to detect internet connection!
« Reply #16 on: May 19, 2019, 09:48:15 pm »
FWIW, I always test against IANA reserved names: example.com, example.org, etc. Lessens the chance of a failed connection because a  host is down: something has to be very wrong for those to fail.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to detect internet connection!
« Reply #17 on: May 19, 2019, 11:24:01 pm »
To test against example.com, or any other domain, this involves communicating with a DNS server to find the IP address (yes, UDP). I wonder if relying on testing against a specific DNS server, like google's 8.8.8.8, would be equal.

Edit:
To check for connectivity, I noticed Android phones use:
http://connectivitycheck.android.com/generate_204
« Last Edit: May 19, 2019, 11:36:24 pm by engkin »

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: How to detect internet connection!
« Reply #18 on: May 19, 2019, 11:44:48 pm »
No it is not equal, in my opinion.

I think, if the machine have an misconfigured dns then you only know that only ip is working.
With test against dns you can be sure that both works.

Do you enter an IP address in the address field of the browser?
« Last Edit: May 19, 2019, 11:52:11 pm by sstvmaster »
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to detect internet connection!
« Reply #19 on: May 19, 2019, 11:57:58 pm »
No it is not equal, in my opinion.
That is why I posted the question, I want to hear members' opinions.

I think, if the machine have an misconfigured dns then you only know that only ip is working.
With test against dns you can be sure that both works.
I see what you mean. What I propose does not rely on the configuration of the machine. The procedure will send a DNS query packet to a DNS server, 8.8.8.8 for instance, and wait for a valid reply.

I think the signature of the procedure is something like:
Code: Pascal  [Select][+][-]
  1. function CheckConnictivityUsingDNS(ADomain:String='www.google.com'; ADNSServer:String='8.8.8.8'): boolean;

Do you enter an IP address in the address field of the browser?
Sometimes I do!



metallaro1980

  • New Member
  • *
  • Posts: 36
Re: How to detect internet connection!
« Reply #20 on: May 20, 2019, 03:49:16 pm »
i am using the dns of google for my lan...for my router.
i am bypassing the dns server of my ISP.
my program can find the external ip of my router (on my website there is a php script that shows your internet ip)

with linux if i want to check the internet connection i use ping -c 5 8.8.8.8
and with  wget -nv --spider url ->  i can check if a url is valid (reachable)

« Last Edit: May 20, 2019, 03:56:46 pm by metallaro1980 »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to detect internet connection!
« Reply #21 on: May 22, 2019, 07:01:34 pm »
Here is a way that doesn't require YOU to establish an internet connection but, that doesn't mean the system didn't establish one for you.

This is fantastic. Thank you for pointing this one out.

Code: Pascal  [Select][+][-]
  1. unit useRoutingTable;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. {
  8. Pointed out by @440bx:
  9. https://forum.lazarus.freepascal.org/index.php/topic,23943.msg321556.html#msg321556
  10. }
  11. function IsInternetAvailable(): boolean;
  12.  
  13. implementation
  14. uses
  15.   Windows;
  16.  
  17. //constant and types from JwaIpHlpApi and JwaIpRtrMib
  18. const
  19.   ANY_SIZE = 1;
  20.  
  21. type
  22. {$PUSH}
  23. {$PACKENUM 4} // jediapilib.inc
  24.  
  25.   MIB_IPFORWARDROW = record
  26.     dwForwardDest: DWORD;
  27.     dwForwardMask: DWORD;
  28.     dwForwardPolicy: DWORD;
  29.     dwForwardNextHop: DWORD;
  30.     dwForwardIfIndex: DWORD;
  31.     dwForwardType: DWORD;
  32.     dwForwardProto: DWORD;
  33.     dwForwardAge: DWORD;
  34.     dwForwardNextHopAS: DWORD;
  35.     dwForwardMetric1: DWORD;
  36.     dwForwardMetric2: DWORD;
  37.     dwForwardMetric3: DWORD;
  38.     dwForwardMetric4: DWORD;
  39.     dwForwardMetric5: DWORD;
  40.   end;
  41.   //PMIB_IPFORWARDROW = ^MIB_IPFORWARDROW;
  42.  
  43.   MIB_IPFORWARDTABLE = record
  44.     dwNumEntries: DWORD;
  45.     table: array [0..ANY_SIZE - 1] of MIB_IPFORWARDROW;
  46.   end;
  47.   PMIB_IPFORWARDTABLE = ^MIB_IPFORWARDTABLE;
  48. {$POP}
  49.  
  50. function GetIpForwardTable(pIpForwardTable: PMIB_IPFORWARDTABLE; var pdwSize: ULONG;
  51.   bOrder: BOOL): DWORD; stdcall; external 'iphlpapi.dll';
  52.  
  53. function IsInternetAvailable(): boolean;
  54. var
  55.   bIsInternetAvailable: boolean = false;
  56.   dwBufferSize: DWORD = 0;
  57.   dwIndex: DWORD;
  58.   pRoutingTable: PMIB_IPFORWARDTABLE;
  59.   pByte: Windows.PBYTE;
  60.   dwRowCount: DWORD;
  61. begin
  62.   // Get the required buffer size
  63.   if (ERROR_INSUFFICIENT_BUFFER = GetIpForwardTable(nil, &dwBufferSize, false)) then
  64.   begin
  65.     pByte := GetMem(dwBufferSize);
  66.     if (pByte<>nil) then
  67.     begin
  68.       pRoutingTable := PMIB_IPFORWARDTABLE(pByte);
  69.       // Attempt to fill buffer with routing table information
  70.       if (NO_ERROR = GetIpForwardTable(pRoutingTable, dwBufferSize, false)) then
  71.       begin
  72.         dwRowCount := pRoutingTable^.dwNumEntries; // Get row count
  73.         // Look for default route to gateway
  74.         for dwIndex := 0 to  dwRowCount-1 do
  75.         begin
  76.           if (pRoutingTable^.table[dwIndex].dwForwardDest = 0) then
  77.           begin // Default route designated by 0.0.0.0 in table
  78.             bIsInternetAvailable := true; // Found it
  79.             break; // Short circuit loop
  80.           end;
  81.         end;
  82.       end;
  83.       FreeMem(pByte); // Clean up. Just say "No" to memory leaks
  84.     end;
  85.   end;
  86.   Result := bIsInternetAvailable;
  87. end;
  88.  
  89. end.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: How to detect internet connection!
« Reply #22 on: May 22, 2019, 09:54:32 pm »
This is fantastic. Thank you for pointing this one out.
I'm very pleased you liked it. 

I never gave the problem of determining whether an internet connection is available or not much thought but, I found the OP's question and @RAW's request interesting.  A little googling found that method in CodeProject and, it looked good.   

Thank you for posting a Pascal version.  It will come in handy.



« Last Edit: May 23, 2019, 02:00:08 am by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: How to detect internet connection!
« Reply #23 on: May 23, 2019, 04:47:46 am »
This is fantastic. Thank you for pointing this one out.
This feature only means that your gateway is alive. If it has nothing to do with the Internet, then the result of the function too.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: How to detect internet connection!
« Reply #24 on: May 23, 2019, 06:00:37 am »
Here is a way that doesn't require YOU to establish an internet connection but, that doesn't mean the system didn't establish one for you.

This is fantastic. Thank you for pointing this one out.

Code: Pascal  [Select][+][-]
  1. unit useRoutingTable;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. {
  8. Pointed out by @440bx:
  9. https://forum.lazarus.freepascal.org/index.php/topic,23943.msg321556.html#msg321556
  10. }
  11. function IsInternetAvailable(): boolean;
  12.  
  13. implementation
  14. uses
  15.   Windows;
  16.  
  17. //constant and types from JwaIpHlpApi and JwaIpRtrMib
  18. const
  19.   ANY_SIZE = 1;
  20.  
  21. type
  22. {$PUSH}
  23. {$PACKENUM 4} // jediapilib.inc
  24.  
  25.   MIB_IPFORWARDROW = record
  26.     dwForwardDest: DWORD;
  27.     dwForwardMask: DWORD;
  28.     dwForwardPolicy: DWORD;
  29.     dwForwardNextHop: DWORD;
  30.     dwForwardIfIndex: DWORD;
  31.     dwForwardType: DWORD;
  32.     dwForwardProto: DWORD;
  33.     dwForwardAge: DWORD;
  34.     dwForwardNextHopAS: DWORD;
  35.     dwForwardMetric1: DWORD;
  36.     dwForwardMetric2: DWORD;
  37.     dwForwardMetric3: DWORD;
  38.     dwForwardMetric4: DWORD;
  39.     dwForwardMetric5: DWORD;
  40.   end;
  41.   //PMIB_IPFORWARDROW = ^MIB_IPFORWARDROW;
  42.  
  43.   MIB_IPFORWARDTABLE = record
  44.     dwNumEntries: DWORD;
  45.     table: array [0..ANY_SIZE - 1] of MIB_IPFORWARDROW;
  46.   end;
  47.   PMIB_IPFORWARDTABLE = ^MIB_IPFORWARDTABLE;
  48. {$POP}
  49.  
  50. function GetIpForwardTable(pIpForwardTable: PMIB_IPFORWARDTABLE; var pdwSize: ULONG;
  51.   bOrder: BOOL): DWORD; stdcall; external 'iphlpapi.dll';
  52.  
  53. function IsInternetAvailable(): boolean;
  54. var
  55.   bIsInternetAvailable: boolean = false;
  56.   dwBufferSize: DWORD = 0;
  57.   dwIndex: DWORD;
  58.   pRoutingTable: PMIB_IPFORWARDTABLE;
  59.   pByte: Windows.PBYTE;
  60.   dwRowCount: DWORD;
  61. begin
  62.   // Get the required buffer size
  63.   if (ERROR_INSUFFICIENT_BUFFER = GetIpForwardTable(nil, &dwBufferSize, false)) then
  64.   begin
  65.     pByte := GetMem(dwBufferSize);
  66.     if (pByte<>nil) then
  67.     begin
  68.       pRoutingTable := PMIB_IPFORWARDTABLE(pByte);
  69.       // Attempt to fill buffer with routing table information
  70.       if (NO_ERROR = GetIpForwardTable(pRoutingTable, dwBufferSize, false)) then
  71.       begin
  72.         dwRowCount := pRoutingTable^.dwNumEntries; // Get row count
  73.         // Look for default route to gateway
  74.         for dwIndex := 0 to  dwRowCount-1 do
  75.         begin
  76.           if (pRoutingTable^.table[dwIndex].dwForwardDest = 0) then
  77.           begin // Default route designated by 0.0.0.0 in table
  78.             bIsInternetAvailable := true; // Found it
  79.             break; // Short circuit loop
  80.           end;
  81.         end;
  82.       end;
  83.       FreeMem(pByte); // Clean up. Just say "No" to memory leaks
  84.     end;
  85.   end;
  86.   Result := bIsInternetAvailable;
  87. end;
  88.  
  89. end.
This doesn't work on a Mac... to bad  :'(
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to detect internet connection!
« Reply #25 on: May 23, 2019, 08:55:44 am »
This is fantastic. Thank you for pointing this one out.
This feature only means that your gateway is alive. If it has nothing to do with the Internet, then the result of the function too.
Did you use "If"?

It means the computer, to its best knowledge, has an interface to be used with any IP -like 8.8.8.8- that does not fit in the other entries of the routing table (not 127.0.0.*? nope. not 192.168.0.*? nope. what about 192.168.44.*? nope ...etc).

It is the "active route" with network destination value "0.0.0.0" when you type:
netstat -r

Can you give one example where this entry could exist without internet connection?



This doesn't work on a Mac... to bad  :'(
There must be a similar code for Mac and Linux. Look for retrieving the routing table.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: How to detect internet connection!
« Reply #26 on: May 23, 2019, 08:57:23 am »
Quote
This doesn't work on a Mac... to bad  :'(
The WINDOWS API isn't working on a MAC?
Sue Apple !!!  :D
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: How to detect internet connection!
« Reply #27 on: May 23, 2019, 09:12:13 am »
Can you give one example where this entry could exist without internet connection?
I. e. you apparently consider that the network without the Internet does not exist?

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: How to detect internet connection!
« Reply #28 on: May 23, 2019, 09:27:48 am »
Simple Example:

1. The code works great
2. If a patch cable is down it works great too ... so far ...
3. BUT the code cannot "see" if I cannot connect with my browser !!!

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   If IsInternetAvailable
  4.   Then Color:= clBlue
  5.   Else Color:= clBlack;
  6. end;

In most cases this can give a good result if someone connects to the internet with a router that is always connected and has no security software running. But in my case I simply opened a program that has no right to connect and then FIREFOX cannot reach a website anymore. As long as the program is in the background.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to detect internet connection!
« Reply #29 on: May 23, 2019, 09:32:37 am »
Can you give one example where this entry could exist without internet connection?
I. e. you apparently consider that the network without the Internet does not exist?
The network with the router has a separate entry. If you control the settings of your router using 192.168.44.1, for instance, you'll have an entry in the routing table with network destination value of 192.168.44.0.

If your computer is part of a local network that holds 60 computers with IP numbers from 192.168.0.2-->61, you will have an entry with 192.168.0.0.

When your system discovers that your computer can reach 8.8.8.8 through your router on 192.168.44.1, it adds the 0.0.0.0 entry for you. It will delete this entry when it fails, or when 192.168.44.1 disconnects (hardware) or disappears (Software: ARP packets receive no response).

 

TinyPortal © 2005-2018