Function to Verify if Connected to the Internet
Posted
by Dmitry Utenkov
on May 15th, 2000
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?
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;
}

Comments
Seems doesn't work on Win98
Posted by Legacy on 01/08/2003 12:00amOriginally posted by: max
On Win98 OS, in case of network card function gethostbyname continues to return the machine IP while disconnected(unplugged) Anybody knows solution for Win98?
ReplyBehind firewall/router
Posted by Legacy on 01/03/2003 12:00amOriginally posted by: nizchka
shouldn't 'char szHostName[128];' (actually 127 chars) be
'char szHostName[128 + 1];' because the functioncall which fills szHostName ('if (gethostname(szHostName, 128) == 0 )') gets a maxbufsize of 128 chars. Anyway I think that the error situation never happens because I don't know of such long hostnames.
But that is not my main question. A lot of Inernet users connect to the internet through a firewall/router. My router gives the local network users a ip addr in the range 192.168.0.100 - 192.168.0.115. This IP range 192.168.x.x is dedicated for your private internal net. I routed through my firewall/router, and am 24 hours a day on the net. This situation happens a lot. In your code this is given as not connected.
Anyway thanks for your work on this. Maybe there is a better way to check if youre connected to the internet.
Nizchka
nizchka@hotmail.com
Replyinput an ip address
Posted by Legacy on 09/28/2002 12:00amOriginally posted by: Mark Wall
is there any way that i can type an ip address, then it takes the first part of the ip address and converts it to bin then does the check and then out puts what class it is in. i tried to compile on unix using g++ but it does not recongize winsock2.h, even when i copy the file to the same dir. and ideas???
ReplybProxyConnection explained
Posted by Legacy on 08/02/2001 12:00amOriginally posted by: jb
bProxyConnection is meant to be an external variable that the user can change if they are on connected to a proxy network. so in your UI somewhere, let the user set bProxyConnection to true if they are on a proxy. otherwise this function will return false even though the user may be connected through a proxy. note if bProxyConnection is true, it still doesn't mean they are connected to the internet, just that they MAY be connnected...
Reply
Regarding bProxyConnection
Posted by Legacy on 04/20/2001 12:00amOriginally posted by: RCFox
ReplyBytes! To and From Internet?
Posted by Legacy on 04/11/2001 12:00amOriginally posted by: Onkar Singh
Replyundefine variable ??bProxyConnection
Posted by Legacy on 03/24/2001 12:00amOriginally posted by: kafka
hi
anyone know what will bProxyConnection become ? seem like an undefine variable ?
Replyif (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;
}