Initialize basic 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 | InitIpBasic | (void) |
Declared in file: hzIpaddr.h
Defined in file : hzIpaddr.cpp
Function Logic:
Function body:
hzEcode InitIpBasic (void)
{
// Category: Internet
//
// Initialize basic 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* pRanges ; // Pointer to IP Ranges block
uint32_t* pLocations ; // Pointer to IP Locations block
uint32_t n ; // Loop control
int32_t m_fd ; // Sheared memory file descriptor
// Check we have country codes. This will ensure _hzGlobal_HadronZooBase has a value and from this, _hzGlobal_IpRanges can be derived.
// Check we have deltaIpBaseZone
m_fd = shm_open("deltaIpBaseZone", O_RDONLY, 0);
if (m_fd < 0)
return hzerr(E_OPENFAIL, "Could not open deltaIpBaseZone") ;
threadLog("Opened deltaIpBaseZone\n") ;
fstat(m_fd, &fs);
pRanges = (uint32_t*) mmap(0,fs.st_size, PROT_READ, MAP_SHARED, m_fd, 0);
if (!pRanges)
return hzerr(E_INITFAIL, "Could not map shared memory segment for deltaIpBaseZone. Errno %d\n", errno) ;
threadLog("IP ranges size %u mem at %p\n", fs.st_size, pRanges) ;
// Check we have deltaIpBaseCode
m_fd = shm_open("deltaIpBaseCode", O_RDONLY, 0);
if (m_fd < 0)
return hzerr(E_OPENFAIL, "Could not open deltaIpBaseCode") ;
threadLog("Opened deltaIpBaseCode\n") ;
fstat(m_fd, &fs);
s_IpBasic_max = fs.st_size ;
pLocations = (uint32_t*) mmap(0,fs.st_size, PROT_READ, MAP_SHARED, m_fd, 0);
if (!pLocations)
return hzerr(E_INITFAIL, "Shared memory address at %p errno is %d\n", pLocations, errno) ;
threadLog("locations size %u mem at %p\n", fs.st_size, pLocations) ;
s_IpBasic_zones = pRanges ;
s_IpBasic_codes = (uchar*) pLocations ;
// Calculate start and divider
for (n = 2; n < s_IpBasic_max ; n *= 2);
s_IpBasic_div = n / 2;
s_IpBasic_start = n - 1;
threadLog("Total ranges %d (div %d start %d)\n\n", s_IpBasic_max, s_IpBasic_div, s_IpBasic_start) ;
return E_OK ;
}