Example #1
0
//------------------------------------------------------------------------------
// init() -- initialize the network
//------------------------------------------------------------------------------
bool PosixHandler::init()
{
    bool ok = BaseClass::init();

    // ---
    // Set the local IP address
    // ---
    if (localIpAddr != nullptr) {
        setLocalAddr(localIpAddr);
    }

    return ok;
}
Example #2
0
int
setConnAddr( rcComm_t *conn ) {
    int status1, status2;

    status1 = setLocalAddr( conn->sock, &conn->localAddr );

    status2 = setRemoteAddr( conn->sock, &conn->remoteAddr );

    if ( status1 < 0 ) {
        return status1;
    }
    else if ( status2 < 0 ) {
        return status2;
    }
    else {
        return 0;
    }
}
Example #3
0
// Sets the local IP address using hostname or the Internet standard "." (dotted) notation
bool PosixHandler::setLocalAddr(const char* const hostname)
{
    bool ok = false;
    if (hostname != 0) {
        uint32_t addr0 = INADDR_NONE;
        if (std::isdigit(hostname[0])) {
            // If 'hostname' starts with a number then first try to use it as an IP address
            addr0 = ::inet_addr(hostname);
            ok = (addr0 != INADDR_NONE);
        }
        if (addr0 == INADDR_NONE) {
            // Didn't work, try to find the host IP address by name
            if (isMessageEnabled(MSG_DEBUG)) {
                std::cout << "PosixHandler::setLocalAddr(): Looking up host name: " << hostname;
            }
            const hostent* p = gethostbyname(hostname);
            if (p != 0 && p->h_length > 0) {
                // 'q' points to the four byte address (in network order) as a single unsigned integer
                const unsigned int* const q = reinterpret_cast<const unsigned int*>(p->h_addr_list[0]);
                if (q != 0) {
                    struct in_addr in;
                    in.s_addr = *q;
                    addr0 = in.s_addr;

                    char* ipAddr = ::inet_ntoa(in);
                    if (ipAddr != 0) {
                        if (isMessageEnabled(MSG_DEBUG)) {
                           std::cout << " -- IP Address: " << ipAddr << std::endl;
                        }
                        ok = true;
                    }
                }
            }
            if (!ok && isMessageEnabled(MSG_DEBUG)) {
                std::cout << " -- HOST NOT FOUND!" << std::endl;
            }
        }
        if (addr0 != INADDR_NONE) ok = setLocalAddr(addr0);
    }
    return ok;
}
Example #4
0
// -------------------------------------------------------------
// bindSocket() -- bind the socket to an address, and configure
// the send and receive buffers.
// -------------------------------------------------------------
bool NetHandler::bindSocket()
{
#if defined(WIN32)
    if (socketNum == INVALID_SOCKET) return false;
#else
    if (socketNum < 0) return false;
#endif

    // ---
    // Make sure we have a local host IP address
    // ---
    if (localAddr == INADDR_ANY) {
        char localhost[256];
        int result = gethostname(localhost, sizeof(localhost));
        if (result == 0) {
            setLocalAddr(localhost);
        }
    }

    // ---
    // Set the reuse socket attribute
    // ---
    {
#if defined(WIN32)
        BOOL optval = getSharedFlag();
        Len optlen = sizeof(optval);
        if( setsockopt(socketNum, SOL_SOCKET, SO_REUSEADDR, (const char*) &optval, optlen) == SOCKET_ERROR)
#else
        int optval = getSharedFlag();
        Len optlen = sizeof(optval);
        if( setsockopt(socketNum, SOL_SOCKET, SO_REUSEADDR, &optval, optlen) == SOCKET_ERROR)
#endif
        {
            perror("init(): error setsockopt(SO_REUSEADDR)\n");
            return false;
        }
    }

   return true;
}
Example #5
0
//------------------------------------------------------------------------------
// init() -- initialize the network
//------------------------------------------------------------------------------
bool NetHandler::init()
{
   bool ok = true;
#if defined(WIN32)
   // Init Winsock2
   WSADATA wsaData;
   WORD wVersionRequested = MAKEWORD( 2, 2 );
   int err = WSAStartup( wVersionRequested, &wsaData );
   if (err != 0) {
      std::cerr << "NetHandler::init() -- WSAStartup() FAILED" << std::endl;
   }
   ok = (err == 0);
#endif

   // ---
   // Set the local IP address
   // ---
   if (localIpAddr != 0) {
      setLocalAddr(localIpAddr);
   }

   return ok;
}