void CSocket::GetPeerAddress(unsigned int*   host,
                             unsigned short* port,
                             ENH_ByteOrder   byte_order) const
{
    if ( !m_Socket ) {
        if ( host )
            *host = 0;
        if ( port )
            *port = 0;
    } else
        SOCK_GetPeerAddress(m_Socket, host, port, byte_order);
}
Exemplo n.º 2
0
static CONNECTOR s_Init
(SOCK           sock,
 const char*    host,
 unsigned short port,
 unsigned short try_own,
 const void*    init_data,
 size_t         init_size,
 TSOCK_Flags    flags)
{
    CONNECTOR       ccc;
    SSockConnector* xxx;

    /* some sanity checks */
    assert(!sock  ||  !(init_size || init_data || flags));
    assert(!init_size  ||  init_data);

    if (!(ccc = (SConnector*) malloc(sizeof(SConnector))))
        return 0;
    if (!(xxx = (SSockConnector*) malloc(sizeof(*xxx)
                                         + (init_data ? init_size : 0)
                                         + (host
                                            ? strlen(host) + 1
                                            : MAX_IP_ADDR_LEN)))) {
        free(ccc);
        return 0;
    }

    /* initialize internal data structures */
    if (sock  ||  !host  ||  !port) {
        xxx->sock      = sock;
        xxx->init_size = 0;
        xxx->init_data = 0;
        if (host) {
            xxx->host  = strcpy((char*) xxx + sizeof(*xxx), host);
            xxx->port  = 0;
        } else if (sock) {
            unsigned int x_host;
            char* addr = (char*) xxx + sizeof(*xxx);
            SOCK_GetPeerAddress(sock, &x_host, &xxx->port, eNH_HostByteOrder);
            SOCK_ntoa(SOCK_HostToNetLong(x_host), addr, MAX_IP_ADDR_LEN);
            xxx->host  = addr;
            assert(xxx->port);
        } else {
            /* this signifies invalid state */
            xxx->host  = 0;
            xxx->port  = 0;
        }
        xxx->try_own   = try_own   ? 1                                  : 0;
    } else {
        void* data     = (char*) xxx + sizeof(*xxx);
        xxx->sock      = 0;
        xxx->init_size = init_data ? init_size                          : 0;
        xxx->init_data = memcpy(data, init_data, xxx->init_size);
        xxx->host      = strcpy((char*) data + xxx->init_size, host);
        xxx->port      = port;
        xxx->try_own   = try_own   ? try_own                            : 1;
        xxx->flags     = flags;
    }

    /* initialize connector data */
    ccc->handle  = xxx;
    ccc->next    = 0;
    ccc->meta    = 0;
    ccc->setup   = s_Setup;
    ccc->destroy = s_Destroy;

    return ccc;
}