src/cloud.c
bool cloud_initSession(CLOUD_SESSION_T *s, char *serverName, uint16_t serverPort)
{
   const int recvSockTimeOut = 5000;
   struct in_addr host_addr;
   struct in_addr* p_addr;
   struct hostent* p_host;
   bool success = false;
   /*
    * Start the transaction timer here, as sometimes the socket is already active,
    * and we want to restart the timer with every new transaction.
    */
   s->transStart = utils_getCurrentTime();
   s->diags->attempts++;
   /* Don't do anything if the session is already active. */
   if (CLOUD_INVALID_SOCKET != s->handle)
   {
      utils_sysLog(LOG_DEBUG, "%s>> session already active\n", s->name);
      return true;
   }
   s->handle = CLOUD_INVALID_SOCKET;
   if (0 == strlen(serverName))
   {
      utils_sysLog(LOG_ERR, "%s>> empty server name string\n", s->name);
      success = false;
   }
   else
   {
      host_addr.s_addr = inet_addr(serverName);
      if (host_addr.s_addr == INADDR_NONE)
      {
         utils_sysLog(LOG_DEBUG, "%s>> url: %s\n", s->name, serverName);
         memset(&serverIpAddr, 0, sizeof(CLOUD_SOCKADDR_T));
         p_host = gethostbyname(serverName);
         if (NULL != p_host)
         {
            char *addr = inet_ntoa(*((struct in_addr *)(p_host->h_addr)));
            if (NULL != addr)
            {
               serverIpAddr.sin_family = AF_INET;
               serverIpAddr.sin_addr = *((struct in_addr *)(p_host->h_addr));
               serverIpAddr.sin_port = htons(serverPort);
            }
         }
      }
      else
      {
         serverIpAddr.sin_family = AF_INET;
         serverIpAddr.sin_addr = host_addr;
         serverIpAddr.sin_port = htons(serverPort);
      }
      if (0 != serverIpAddr.sin_addr.s_addr)
      {
         p_addr = (struct in_addr *)&serverIpAddr.sin_addr;
         utils_sysLog(LOG_DEBUG, "%s>> ip: %s\n", s->name, inet_ntoa(*p_addr));
         utils_sysLog(LOG_DEBUG, "%s>> port: %d\n", s->name, ntohs(serverIpAddr.sin_port));
         s->handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
         setsockopt(s->handle, SOL_SOCKET, SO_RCVTIMEO, (char *)&recvSockTimeOut, sizeof(int));
         if (CLOUD_INVALID_SOCKET != s->handle)
         {
            cloud_setSessionStatus(s, CLOUD_SESSION_CREATE_SUCCESS);
            success = true;
         }
         else
         {
            utils_sysLog(LOG_ERR, "%s>> init session errno: %s\n", s->name, strerror(errno));
            cloud_handleSocketError(s, errno);
            success = false;
         }
      }
      else
      {
         utils_sysLog(LOG_ERR, "%s>> URL resolve failed\n", s->name);
         success = false;
      }
   }

   return success;
}
//!
//! Resolve DNS and open a socket to a server.
//!
//! This routine will only initialize the session if not already active.
//!
//! @param[in] s  Pointer to session structure
//! @param[in] serverName  Pointer to server name string
//! @param[in] serverPort  Server port number
//!
//! @return  true if session successfully created, otherwise false
//!

Resolve DNS and open a socket to a server.
This routine will only initialize the session if not already active.
Resolve DNS and open a socket to a server . This routine will only initialize the session if not already active .
bool cloud_initSession(CLOUD_SESSION_T *s, char *serverName, uint16_t serverPort)
{
   const int recvSockTimeOut = 5000;
   struct in_addr host_addr;
   struct in_addr* p_addr;
   struct hostent* p_host;
   bool success = false;
   s->transStart = utils_getCurrentTime();
   s->diags->attempts++;
   if (CLOUD_INVALID_SOCKET != s->handle)
   {
      utils_sysLog(LOG_DEBUG, "%s>> session already active\n", s->name);
      return true;
   }
   s->handle = CLOUD_INVALID_SOCKET;
   if (0 == strlen(serverName))
   {
      utils_sysLog(LOG_ERR, "%s>> empty server name string\n", s->name);
      success = false;
   }
   else
   {
      host_addr.s_addr = inet_addr(serverName);
      if (host_addr.s_addr == INADDR_NONE)
      {
         utils_sysLog(LOG_DEBUG, "%s>> url: %s\n", s->name, serverName);
         memset(&serverIpAddr, 0, sizeof(CLOUD_SOCKADDR_T));
         p_host = gethostbyname(serverName);
         if (NULL != p_host)
         {
            char *addr = inet_ntoa(*((struct in_addr *)(p_host->h_addr)));
            if (NULL != addr)
            {
               serverIpAddr.sin_family = AF_INET;
               serverIpAddr.sin_addr = *((struct in_addr *)(p_host->h_addr));
               serverIpAddr.sin_port = htons(serverPort);
            }
         }
      }
      else
      {
         serverIpAddr.sin_family = AF_INET;
         serverIpAddr.sin_addr = host_addr;
         serverIpAddr.sin_port = htons(serverPort);
      }
      if (0 != serverIpAddr.sin_addr.s_addr)
      {
         p_addr = (struct in_addr *)&serverIpAddr.sin_addr;
         utils_sysLog(LOG_DEBUG, "%s>> ip: %s\n", s->name, inet_ntoa(*p_addr));
         utils_sysLog(LOG_DEBUG, "%s>> port: %d\n", s->name, ntohs(serverIpAddr.sin_port));
         s->handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
         setsockopt(s->handle, SOL_SOCKET, SO_RCVTIMEO, (char *)&recvSockTimeOut, sizeof(int));
         if (CLOUD_INVALID_SOCKET != s->handle)
         {
            cloud_setSessionStatus(s, CLOUD_SESSION_CREATE_SUCCESS);
            success = true;
         }
         else
         {
            utils_sysLog(LOG_ERR, "%s>> init session errno: %s\n", s->name, strerror(errno));
            cloud_handleSocketError(s, errno);
            success = false;
         }
      }
      else
      {
         utils_sysLog(LOG_ERR, "%s>> URL resolve failed\n", s->name);
         success = false;
      }
   }
   return success;
}
bool cloud_initSession ( CLOUD_SESSION_T * s , char * serverName , uint16_t serverPort ) { const int recvSockTimeOut = 5000 ; struct in_addr host_addr ; struct in_addr * p_addr ; struct hostent * p_host ; bool success = false ; s -> transStart = utils_getCurrentTime ( ) ; s -> diags -> attempts ++ ; if ( CLOUD_INVALID_SOCKET != s -> handle ) { utils_sysLog ( LOG_DEBUG , " \n " , s -> name ) ; return true ; } s -> handle = CLOUD_INVALID_SOCKET ; if ( 0 == strlen ( serverName ) ) { utils_sysLog ( LOG_ERR , " \n " , s -> name ) ; success = false ; } else { host_addr . s_addr = inet_addr ( serverName ) ; if ( host_addr . s_addr == INADDR_NONE ) { utils_sysLog ( LOG_DEBUG , " \n " , s -> name , serverName ) ; memset ( & serverIpAddr , 0 , sizeof ( CLOUD_SOCKADDR_T ) ) ; p_host = gethostbyname ( serverName ) ; if ( NULL != p_host ) { char * addr = inet_ntoa ( * ( ( struct in_addr * ) ( p_host -> h_addr ) ) ) ; if ( NULL != addr ) { serverIpAddr . sin_family = AF_INET ; serverIpAddr . sin_addr = * ( ( struct in_addr * ) ( p_host -> h_addr ) ) ; serverIpAddr . sin_port = htons ( serverPort ) ; } } } else { serverIpAddr . sin_family = AF_INET ; serverIpAddr . sin_addr = host_addr ; serverIpAddr . sin_port = htons ( serverPort ) ; } if ( 0 != serverIpAddr . sin_addr . s_addr ) { p_addr = ( struct in_addr * ) & serverIpAddr . sin_addr ; utils_sysLog ( LOG_DEBUG , " \n " , s -> name , inet_ntoa ( * p_addr ) ) ; utils_sysLog ( LOG_DEBUG , " \n " , s -> name , ntohs ( serverIpAddr . sin_port ) ) ; s -> handle = socket ( AF_INET , SOCK_STREAM , IPPROTO_TCP ) ; setsockopt ( s -> handle , SOL_SOCKET , SO_RCVTIMEO , ( char * ) & recvSockTimeOut , sizeof ( int ) ) ; if ( CLOUD_INVALID_SOCKET != s -> handle ) { cloud_setSessionStatus ( s , CLOUD_SESSION_CREATE_SUCCESS ) ; success = true ; } else { utils_sysLog ( LOG_ERR , " \n " , s -> name , strerror ( errno ) ) ; cloud_handleSocketError ( s , errno ) ; success = false ; } } else { utils_sysLog ( LOG_ERR , " \n " , s -> name ) ; success = false ; } } return success ; }
Resolve DNS and open a socket to a server.
Resolve DNS and open a socket to a server .
/*
    * Start the transaction timer here, as sometimes the socket is already active,
    * and we want to restart the timer with every new transaction.
    */ /* Don't do anything if the session is already active. */