This returns a location description for the supplied IP address if the IP location table has been loaded. Otherwise this function calls LocateCountry(), which will return the country code for the IP address - if the country codes table has been set up. This function never returns NULL. A pointer to a static string is returned in the event of lookup failure. If the supplied IP address is invalid but the IP location table has been loaded, this will read "-- Invalid IP location". If the IP location table is not loaded and LocateCountry() is used instead, this will read "-- Invalid Country Code". The lookup is by binary chop array of known IP blocks to see if the IP address falls within any of them. Pointer to either the country code or the location code
| Return Type | Function name | Arguments |
|---|---|---|
| const char* | GetIpLocation | (hzIpaddr&,) |
Declared in file: hzIpaddr.h
Defined in file : hzIpaddr.cpp
Function Logic:
Function body:
const char* GetIpLocation (hzIpaddr& ipa)
{
// Category: Internet
//
// This returns a location description for the supplied IP address if the IP location table has been loaded. Otherwise this function calls LocateCountry(),
// which will return the country code for the IP address - if the country codes table has been set up.
//
// This function never returns NULL. A pointer to a static string is returned in the event of lookup failure. If the supplied IP address is invalid but the
// IP location table has been loaded, this will read "-- Invalid IP location". If the IP location table is not loaded and LocateCountry() is used instead,
// this will read "-- Invalid Country Code".
//
// The lookup is by binary chop array of known IP blocks to see if the IP address falls within any of them.
//
// Arguments: 1) The client IP address
//
// Returns: Pointer to either the country code or the location code
_hzfunc(__func__) ;
static hzString ip_not_found = "-- IP Location Not-found" ;
static hzString ip_error = "-- IP Location Error" ;
const uint32_t* pZones ; // IP ranges
uint32_t nDiv ; // Binary chop divider
uint32_t nPos ; // Position found within IP range table
uint32_t nMax ; // Total IP zones in operation
uint32_t ipval ; // IP as 32 bit uint
bool bFound ; // Position if redult
if (s_IpCity_max)
{
// There is a city level IP location table
pZones = s_IpCity_zones ;
nDiv = s_IpCity_div ;
nPos = s_IpCity_start ;
nMax = s_IpCity_max ;
}
else
{
if (!s_IpBasic_max)
return *ip_not_found ;
pZones = s_IpBasic_zones ;
nDiv = s_IpBasic_div ;
nPos = s_IpBasic_start ;
nMax = s_IpBasic_max ;
}
bFound = false ;
ipval = (uint32_t) ipa ;
for (;;)
{
if (nPos >&eq; nMax)
{
if (!nDiv)
break ;
nPos -= nDiv ;
nDiv /= 2;
continue ;
}
if (ipval >&eq; pZones[nPos+1])
{
if (!nDiv)
break ;
nPos += nDiv ;
nDiv /= 2;
continue ;
}
if (ipval < pZones[nPos])
{
if (!nDiv)
break ;
nPos -= nDiv ;
nDiv /= 2;
continue ;
}
bFound = true ;
break ;
}
if (!bFound)
{
threadLog("Not found\n") ;
return *ip_not_found ;
}
if (s_IpCity_max)
{
return s_IpCity_text + s_IpCity_osets[nPos] ;
}
return GetCountryCode(s_IpBasic_codes[nPos]) ;
}