Initialize city-level IP lookup tables as client. The HadronZoo Delta Server is assumed to have previously created the necessary shared memory segments. Arguments: None
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | InitIpCity | (void) |
Declared in file: hzIpaddr.h
Defined in file : hzIpaddr.cpp
Function Logic:
Function body:
hzEcode InitIpCity (void)
{
// Category: Internet
//
// Initialize city-level IP lookup tables as client. The HadronZoo Delta Server is assumed to have previously created the necessary shared memory segments.
//
// Arguments: None
//
// Returns: E_NODATA If there are no IP ranges
// E_OPENFAIL If the IP range file cannot be opened
_hzfunc(__func__) ;
ifstream is ; // Input stream
FSTAT fs ; // Shared memory 'file' stat
uint32_t* pIpr ; // Pointer to IP Ranges block
uint32_t m_fd ; // Loop control
uint32_t n ; // Loop control
// Check we have country codes. This will ensure _hzGlobal_HadronZooBase has a value and from this, _hzGlobal_IpRanges can be derived.
m_fd = shm_open("deltaIpCityText", O_RDONLY, 0);
if (m_fd < 0)
return hzerr(E_OPENFAIL, "Could not open deltaIpCityText") ;
threadLog("Set fd to %d\n", m_fd) ;
fstat(m_fd, &fs);
s_IpCity_text = (char*) mmap(0,fs.st_size, PROT_READ, MAP_SHARED, m_fd, 0);
if (!s_IpCity_text)
return hzerr(E_INITFAIL, "Shared memory address at %p errno is %d\n", s_IpCity_text, errno) ;
threadLog("text mem at %p\n", s_IpCity_text) ;
m_fd = shm_open("deltaIpCityZone", O_RDONLY, 0);
if (m_fd < 0)
return hzerr(E_OPENFAIL, "Could not open deltaIpCityZone") ;
threadLog("Set fd to %d\n", m_fd) ;
fstat(m_fd, &fs);
pIpr = (uint32_t*) mmap(0,fs.st_size, PROT_READ, MAP_SHARED, m_fd, 0);
if (!pIpr)
return hzerr(E_INITFAIL, "Shared memory address at %p errno is %d\n", pIpr, errno) ;
threadLog("zone mem at %p\n", pIpr) ;
s_IpCity_max = pIpr[0];
s_IpCity_zones = pIpr + 1;
m_fd = shm_open("deltaIpCityLocn", O_RDONLY, 0);
fstat(m_fd, &fs);
threadLog("Set fd to %d\n", m_fd) ;
s_IpCity_osets = (uint32_t*) mmap(0,fs.st_size, PROT_READ, MAP_SHARED, m_fd, 0);
if (!s_IpCity_osets)
return hzerr(E_INITFAIL, "Shared memory address at %p errno is %d\n", s_IpCity_osets, errno) ;
threadLog("locn mem at %p\n", s_IpCity_osets) ;
// Calculate start and divider
for (n = 2; n < s_IpCity_max ; n *= 2);
s_IpCity_div = n / 2;
s_IpCity_start = n - 1;
threadLog("Have %d zones, div %d start %d\n", s_IpCity_max, s_IpCity_div, s_IpCity_start) ;
return E_OK ;
}