Пример #1
0
void OsSocket::getHostIp(UtlString* hostAddress)
{
      socketInit();
      UtlString thisHost;

#ifdef _VXWORKS /* [ */

      char ipAddr[100];
      ipAddr[0] = '\0';
      hostAddress->remove(0);
      if(!ifAddrGet("csp0", ipAddr))
         hostAddress->append(ipAddr);

#elif defined(__pingtel_on_posix__) /* ] [ */

#ifdef __linux__
      char ipAddr[100];
      unsigned int ip_int = ntohl(getExternalHostAddressLinux());
      sprintf(ipAddr, "%d.%d.%d.%d", ip_int >> 24, (ip_int >> 16) & 255, (ip_int >> 8) & 255, ip_int & 255);
      hostAddress->remove(0);
      hostAddress->append(ipAddr);
#else
      getHostName(&thisHost);
      getHostIpByName(thisHost.data(), hostAddress);
#endif /* __linux__ */

#elif defined(_WIN32) /* ] [ */

      unsigned long address_val = OsSocket::getDefaultBindAddress();

      if (address_val == htonl(INADDR_ANY))
      {
         getHostName(&thisHost);
         getHostIpByName(thisHost.data(), hostAddress);
      }
      else
      {
         struct in_addr in;
         char tmp[50];
         in.S_un.S_addr = address_val;
         strcpy(tmp,inet_ntoa(in));
         *hostAddress = tmp;
      }

#else /* ] [ */
#error Unsupported target platform.
#endif /* ] */

      thisHost.remove(0);
}
Пример #2
0
bool getHostIp(std::string &str_ip) {
    char hname[256] = {0};

    if( -1 == gethostname(hname, sizeof(hname))) {
        RPC_LOG(RPC_LOG_LEV::ERROR, "gethostname error %s", strerror(errno));
        return false;
    }

    return  getHostIpByName(str_ip, hname);
}
Пример #3
0
UtlBoolean OsSocket::isSameHost(const char* host1, const char* host2)
{
        UtlBoolean same;
        UtlBoolean isSubDomain = FALSE;

        //osPrintf("OsSocket::isSameHost host1: %s host2: %s\n", host1, host2);
        if (!isIp4Address(host1) && !isIp4Address(host2))
        {
                if( (strstr(host1, host2) == host1 || // && need to compare w/  local domain name
                        strstr(host2, host1) == host2 ))
                {
                        // && need to compare w/  local domain name
                        isSubDomain = TRUE;
                }
        }

        if(strcmp(host1, host2) == 0 ||
                (isLocalHost(host1) && isLocalHost(host2)) ||
                isSubDomain )
        {
                same = TRUE;
        }
        else
        {
                // Avoid using gethostbyname if possible
                UtlString host1Addr;
                UtlString host2Addr;
                getHostIpByName(host1, &host1Addr);
                getHostIpByName(host2, &host2Addr);
                if(host1Addr.compareTo(host2Addr) == 0)
                {
                        same = TRUE;
                }
                else
                {
                        same = FALSE;
                }
                host1Addr.remove(0);
                host2Addr.remove(0);
        }
        return(same);
}
Пример #4
0
void OsDatagramSocket::doConnect(int remoteHostPortNum, const char* remoteHost,
                                 UtlBoolean simulateConnect)
{
    struct hostent* server;    

    mToSockaddrValid = FALSE;
    memset(mpToSockaddr, 0, sizeof(struct sockaddr_in));
    remoteHostPort = remoteHostPortNum;

    // Store host name
    if(remoteHost)
    {
        remoteHostName = remoteHost ;
        getHostIpByName(remoteHostName, &mRemoteIpAddress);
    }
    else
    {
        remoteHostName.remove(0) ;
    }

    // Connect to a remote host if given
    if(portIsValid(remoteHostPort) && remoteHost && !simulateConnect)
    {
        server = gethostbyname(remoteHost);
        if (server)
        {
            struct in_addr* serverAddr = (in_addr*) (server->h_addr);
            struct sockaddr_in serverSockAddr;
            serverSockAddr.sin_family = server->h_addrtype;
            serverSockAddr.sin_port = htons(remoteHostPort);
            serverSockAddr.sin_addr.s_addr = (serverAddr->s_addr);

            // Set the default destination address for the socket
            if(connect(socketDescriptor, (const struct sockaddr*) 
                    &serverSockAddr, sizeof(serverSockAddr)))
            {
                int error = OsSocketGetERRNO();
                close();
                OsSysLog::add(FAC_KERNEL, PRI_DEBUG,
                        "OsDatagramSocket::doConnect( %s:%d ) failed w/ errno %d)",
                        remoteHost, remoteHostPortNum, error);
            }
            else
            {
                mIsConnected = TRUE;
            }
        }
        else
        {
            close();
            OsSysLog::add(FAC_KERNEL, PRI_DEBUG,
                    "OsDatagramSocket::doConnect( %s:%d ) failed host lookup)",
                    remoteHost, remoteHostPortNum);           

            goto EXIT;
        }
    }
    else if(portIsValid(remoteHostPort) && remoteHost && simulateConnect)
    {
        mIsConnected = TRUE;
        mSimulatedConnect = TRUE;
    }
EXIT:
   ;
}
Пример #5
0
void OsDatagramSocket::doConnect(int remoteHostPortNum, const char* remoteHost,
                                 UtlBoolean simulateConnect)
{
    struct hostent* server = NULL;

    mToSockaddrValid = FALSE;
    memset(mpToSockaddr, 0, sizeof(struct sockaddr_in));
    remoteHostPort = remoteHostPortNum;

    // Store host name
    if(remoteHost)
    {
        remoteHostName = remoteHost ;
        getHostIpByName(remoteHostName, &mRemoteIpAddress);
    }
    else
    {
        remoteHostName.remove(0) ;
    }

    // Connect to a remote host if given
    if(portIsValid(remoteHostPort) && remoteHost && !simulateConnect)
    {
#if defined(_WIN32) || defined(__pingtel_on_posix__)
        unsigned long ipAddr;

        ipAddr = inet_addr(remoteHost);

        if (ipAddr != INADDR_NONE) 
        {
           server = gethostbyaddr((char * )&ipAddr,sizeof(ipAddr),AF_INET);
        }

        if ( server == NULL ) 
        { 
           server = gethostbyname(remoteHost);
        }

#elif defined(_VXWORKS)
        char hostentBuf[512];
        server = resolvGetHostByName((char*) remoteHost,
                                      hostentBuf, sizeof(hostentBuf));
#else
#  error Unsupported target platform.
#endif //_VXWORKS

        if (server)
        {
            struct in_addr* serverAddr = (in_addr*) (server->h_addr);
            struct sockaddr_in serverSockAddr;
            serverSockAddr.sin_family = server->h_addrtype;
            serverSockAddr.sin_port = htons(remoteHostPort);
            serverSockAddr.sin_addr.s_addr = (serverAddr->s_addr);

            // Set the default destination address for the socket
#if defined(_WIN32) || defined(__pingtel_on_posix__)
            if(connect(socketDescriptor, (const struct sockaddr*) 
                    &serverSockAddr, sizeof(serverSockAddr)))
#elif defined(_VXWORKS)
            if(connect(socketDescriptor, (struct sockaddr*) &serverSockAddr,
                       sizeof(serverSockAddr)))
#else
#  error Unsupported target platform.
#endif



            {
                int error = OsSocketGetERRNO();
                close();
                OsSysLog::add(FAC_KERNEL, PRI_DEBUG,
                        "OsDatagramSocket::doConnect( %s:%d ) failed w/ errno %d)",
                        remoteHost, remoteHostPortNum, error);
            }
            else
            {
                mIsConnected = TRUE;
            }
        }
        else
        {
            close();
            OsSysLog::add(FAC_KERNEL, PRI_DEBUG,
                    "OsDatagramSocket::doConnect( %s:%d ) failed host lookup)",
                    remoteHost, remoteHostPortNum);           

            goto EXIT;
        }
    }
    else if(portIsValid(remoteHostPort) && remoteHost && simulateConnect)
    {
        mIsConnected = TRUE;
        mSimulatedConnect = TRUE;
    }
EXIT:
   ;
}