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 TypeFunction nameArguments
hzEcodeInitIpBasic(void)

Declared in file: hzIpaddr.h
Defined in file : hzIpaddr.cpp

Function Logic:

0:START 1:m_fd 2:unknown 3:Return hzerr(E_OPENFAIL,Could not open deltaIpBaseZone) 4:items items pRanges 5:unknown 6:Return hzerr(E_INITFAIL,Could not map shared memory segment for deltaIpBaseZone. Errno %d\n,errno) 7:items m_fd 8:unknown 9:Return hzerr(E_OPENFAIL,Could not open deltaIpBaseCode) 10:items items s_IpBasic_max pLocations 11:unknown 12:Return hzerr(E_INITFAIL,Shared memory address at %p errno is %d\n,pLocations,errno) 13:items s_IpBasic_zones s_IpBasic_codes 14:unknown 15:n 16:s_IpBasic_div n s_IpBasic_start items 17:Return E_OK

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 ;
}