Function to Verify if Connected to the Internet

In addition, I’d like to add that this function is based on the comments made on
this site by
Jarek Gibek
as well as the article

‘Get hostname and ip address of local computer(2)’
by
Jaroslav Pisk.

How it Works

The way this function works is that it attempts to define which are the IP addresses
which can automatically be excluded from consideration. Then it defines which IP addresses
would signify that the computer is connected to the Internet. Here’s a sampling of IP
addresses and what they mean to the function that should help to illustrate my point.

  • 127.0.0.1 – localhost;
  • Private Adress area(RFC 1597)

    • 10.*.*.* – Class A;
    • 172.16-31.*.* – Class B;
    • 192.168.*.*– Class C;

  • 169.254.*.* Microsoft AutoNet address area(See: http://lancanyon.com/autonet.htm for detail);
  • Have you reason for other ranges exclusion?

Please note that I do not exclude other specific IP adresses such as
0.0.0.0 or 255.2555.255.255 because the local host can’t have either of these
IP addresses.

The Function

While the function is stated (and commented) here, you can also
download it below.

#include "winsock2.h"

BOOL IsInternetConnection()
{
 char szHostName[128];

 BOOL bPrivateAdr = false; // Private Address area
 BOOL bClassA = false;     // Class A definition
 BOOL bClassB = false;     // Class B definition
 BOOL bClassC = false;     // Class C definition
 BOOL bAutoNet = false;    // AutoNet definition
 CString str;

 if (gethostname(szHostName, 128) == 0 )
 {
  // Get host adresses
  struct hostent * pHost;
  int i;
  UINT ipb;
  pHost = gethostbyname(szHostName);

  for (i = 0;
       pHost!= NULL && pHost->h_addr_list[i]!= NULL;
       i++ )
  {
   int j;
   str="";
   bClassA = bClassB = bClassC = false;
   for( j = 0; j < pHost->h_length; j++ )
   {
    CString addr;

    if( j > 0 )	str += ".";
    ipb = (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j];

    // Define the IP range for exclusion
    if(j==0)
    {
     if(bClassA = (ipb == 10)) break; // Class A defined
     bClassB = (ipb == 172);
     bClassC = (ipb == 192);
     bAutoNet = (ipb == 169);
    }
    else if (j==1)
    {
     // Class B defined
     if(bClassB = (bClassB && ipb >= 16 && ipb <= 31)) break;

     // Class C defined
     if(bClassC = (bClassC && ipb == 168)) break;

     //AutoNet pasibility defined
     if(bAutoNet = (bAutoNet && ipb == 254)) break;
    }

    addr.Format("%u", ipb );
    str += addr;
   }
   // If any address of Private Address 
   // area has been found bPrivateAdr = true
   bPrivateAdr = bPrivateAdr || bClassA || bClassB || bClassC;

   // If any address of Internet Address area 
   // has been found returns TRUE
   if (!bClassA
   && !bClassB
   && !bClassC
   && !bAutoNet
   && str != "127.0.0.1"
   && !str.IsEmpty())
    return TRUE;
  }
 }

 if (bPrivateAdr)
 {
  // The system has IP address from Private Address
  // area,only. Internet from the computer can be accessable
  // via Proxy. If user turn on proxy connection flag, the
  // function believe Internet accessable.
  return bProxyConnection;
 }

 return false;
}

Downloads

Download source (same function as listed above) – 1 Kb

Date Posted: May 15

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read