Exemple #1
0
int OsSocket::read(char* buffer, int bufferLength,
                UtlString* fromAddress, int* fromPort)
{

#ifdef FORCE_SOCKET_ERRORS

   static int numForFailure = 30;
   static int count = 0;
   count++;

   if (count > numForFailure)
   {
      count = 0;
      numForFailure = 10;
#ifdef _WIN32
                closesocket(socketDescriptor);
#else
                ::close(socketDescriptor);
                Os::Logger::instance().log(FAC_KERNEL, PRI_DEBUG, "OsSocket::read[4UtlString] close socket %d",
                              socketDescriptor);
#endif
      return 0;
   }
#endif //FORCE_SOCKET_ERRORS

   int bytesRead;
   struct in_addr fromSockAddress;

   if (fromAddress)
   {
      fromAddress->remove(0);
   }

   bytesRead = read(buffer, bufferLength, &fromSockAddress, fromPort);
   if(bytesRead != -1)
   {
      if (NULL != fromAddress)
      {
         inet_ntoa_pt(fromSockAddress, *fromAddress);
      }
   }

   return(bytesRead);
}
Exemple #2
0
UtlBoolean OsSocket::getHostIpByName(const char* hostName, UtlString* hostAddress)
{
    UtlBoolean bSuccess = FALSE;

    struct hostent* server;
    socketInit();

   // If this is aleady an IP address
   if (isIp4Address(hostName))
   {
      *hostAddress = hostName;
      bSuccess = TRUE ;
   }
   else if (strcmp(hostName, "localhost") == 0)
   {
      *hostAddress = "127.0.0.1";
      bSuccess = TRUE ;
   }
   // if no default domain name and host name not fully qualified
   else if (!hasDefaultDnsDomain() && (strchr(hostName, '.') == NULL))
   {
      *hostAddress = "0.0.0.0";
   }
   else
   {
#if defined(_WIN32) || defined(__pingtel_on_posix__)
        server = gethostbyname(hostName);
#else
#error Unsupported target platform.
#endif
        if(server)
        {
            inet_ntoa_pt(*((in_addr*) (server->h_addr)), *hostAddress);
            bSuccess = TRUE ;
        }
        else
        {
            *hostAddress = "0.0.0.0";
        }
    }

   return bSuccess ;
}
void OsDatagramSocket::getRemoteHostIp(struct in_addr* remoteHostAddress,
                               int* remotePort)
{
#ifdef __pingtel_on_posix__
    socklen_t len;
#else
    int len;
#endif
    struct sockaddr_in remoteAddr;
    const struct sockaddr_in* pAddr;

    if (mSimulatedConnect) {
        getToSockaddr();
        pAddr = mpToSockaddr;
    } else {
        pAddr = &remoteAddr;

        len = sizeof(struct sockaddr_in);

        if (getpeername(socketDescriptor, (struct sockaddr *)pAddr, &len) != 0)
        {
            memset(&remoteAddr, 0, sizeof(struct sockaddr_in));
        }
    }
    memcpy(remoteHostAddress, &(pAddr->sin_addr), sizeof(struct in_addr));

#ifdef TEST_PRINT
    {
        int p = ntohs(pAddr->sin_port);
        UtlString o;
        inet_ntoa_pt(*remoteHostAddress, o);
        osPrintf("getRemoteHostIP: Remote name: %s:%d\n"
           "   (pAddr->sin_addr) = 0x%X, sizeof(struct in_addr) = %d\n",
           o.data(), p, (pAddr->sin_addr), sizeof(struct in_addr));
    }
#endif

    if (NULL != remotePort)
    {
        *remotePort = ntohs(pAddr->sin_port);
    }
}
Exemple #4
0
void OsSocket::getRemoteHostIp(UtlString* remoteHostAddress, int* remotePort)
{
    struct in_addr remoteAddr;

    getRemoteHostIp(&remoteAddr, remotePort);
    remoteHostAddress->remove(0);
    inet_ntoa_pt(remoteAddr, *remoteHostAddress);

    // If querying the network layer did not obtain an address (probably
    // because the socket is still in the process of connecting), return
    // the values stored from the connect attempt.
    if (remoteHostAddress->compareTo("0.0.0.0") == 0)
    {
       remoteHostAddress->remove(0);
       remoteHostAddress->append(mRemoteIpAddress);
       if (remotePort)
       {
          *remotePort = remoteHostPort;
       }
    }
}
Exemple #5
0
void OsSocket::getRemoteHostIp(struct in_addr* remoteHostAddress,
                               int* remotePort)
{
    struct sockaddr_in remoteAddr;
#ifdef __pingtel_on_posix__
    socklen_t len;
#else
    int len;
#endif

    len = sizeof(struct sockaddr_in);

    int ret = getpeername(socketDescriptor, (struct sockaddr *)&remoteAddr, &len);
    if (ret != 0)
    {
        memset(&remoteAddr, 0, len);
    }

    // *remoteHostAddress = remoteAddr.sin_addr;
    memcpy(remoteHostAddress, &(remoteAddr.sin_addr), sizeof(struct in_addr));

#ifdef TEST_PRINT
    {
        UtlString output_address;
        inet_ntoa_pt(remoteAddr.sin_addr, output_address);
        Os::Logger::instance().log(FAC_SIP, PRI_DEBUG,
                      "OsSocket::getRemoteHostIp Remote name '%s'",
                      output_address.data());
    }
#endif

    if (NULL != remotePort)
    {
        *remotePort = ntohs(remoteAddr.sin_port);
    }
}