Determine the hostname of this server. This is called as part of the initialization sequence of a server application, if not also a client application. It should generally not be called mid way though. The hostname could change during runtime, but in a live server environment proving an online service, performing such a change is absurd. The function calls the standard functions gethostname() and gethostbyname(). The former to establish the hostname and set _hzGlobal_Hostname. The latter to establish the physical server IP address and set _hzGlobal_HostIP. Although it is intended that this function be called as part of program initialization, there is still a need to take account of the condition in which the DNS advises a retry (h_errno set to DNS_TRY_AGAIN). This is because it should not be assumed the program is to be started manually. This function will repeat the call to gethostbyname() for a maximum of 10 times before failing. The application should check that the hostname is not set to localhost if it is to provide an online service. e hzString, namely _hzGlobal_Hostname and _hzGlobal_HostIP

Return TypeFunction nameArguments
hzEcodeSetupHost(void)

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

Function Logic:

0:START 1:unknown 2:items 3:Return E_NODATA 4:_hzGlobal_Hostname 5:unknown 6:pHost 7:unknown 8:unknown 9:items 10:Return E_NOTFOUND 11:unknown 12:items 13:Return E_DNS_RETRY 14:items SvrAddr items _hzGlobal_HostIP _hzGlobal_livehost 15:Return E_OK

Function body:

hzEcode SetupHost (void)
{
   //  Category: Internet Server
   //  
   //  Determine the hostname of this server. This is called as part of the initialization sequence of a server application, if not also a client application.
   //  It should generally not be called mid way though. The hostname could change during runtime, but in a live server environment proving an online service,
   //  performing such a change is absurd.
   //  
   //  The function calls the standard functions gethostname() and gethostbyname(). The former to establish the hostname and set _hzGlobal_Hostname. The latter
   //  to establish the physical server IP address and set _hzGlobal_HostIP.
   //  
   //  Although it is intended that this function be called as part of program initialization, there is still a need to take account of the condition in which
   //  the DNS advises a retry (h_errno set to DNS_TRY_AGAIN). This is because it should not be assumed the program is to be started manually. This function
   //  will repeat the call to gethostbyname() for a maximum of 10 times before failing.
   //  
   //  The application should check that the hostname is not set to localhost if it is to provide an online service.
   //  
   //  Arguments: None. The function sets two global parameters, both of type
   //     hzString, namely _hzGlobal_Hostname and _hzGlobal_HostIP
   //  
   //  Returns: E_NODATA If the call to gethostname fails
   //     E_NOTFOUND If the call to gethostbyname does not return a host
   //     E_DNS_RETRY If the number of retries reaches 10
   //     E_OK  If the hostname established
   _hzfunc("SetupHost") ;
   struct hostent*     pHost ;     //  Host entry
   struct sockaddr_in  SvrAddr ;   //  Server address
   uint32_t    nTries ;            //  Number of DNS tries
   char        buf [256];          //  Buffer for gethostname function
   if (gethostname(buf, 256))
   {
       hzerr(E_NODATA, "System call 'gethostname' failed. Cannot establish hostname") ;
       return E_NODATA ;
   }
   _hzGlobal_Hostname = buf ;
   for (nTries = 0; nTries < 10;nTries++)
   {
       pHost = gethostbyname(*_hzGlobal_Hostname) ;
       if (pHost)
           break ;
       if (h_errno == TRY_AGAIN)
           continue ;
       hzerr(E_NOTFOUND, "Could not lookup the hostname") ;
       return E_NOTFOUND ;
   }
   if (nTries == 10)
   {
       hzerr(E_DNS_RETRY, "Could not lookup the hostname") ;
       return E_DNS_RETRY ;
   }
   memset(&SvrAddr, 0,sizeof(SvrAddr)) ;
   SvrAddr.sin_family = AF_INET ;
   memcpy(&SvrAddr.sin_addr, pHost->h_addr, pHost->h_length) ;
   _hzGlobal_HostIP = inet_ntoa(SvrAddr.sin_addr) ;
   _hzGlobal_livehost = _hzGlobal_HostIP ;
   return E_OK ;
}