Beispiel #1
0
Sock::status_t
Sock::connectTo(int sock, const char* hostname, unsigned int port)
{
    struct sockaddr_in s_addr;
    struct hostent sent_struct, *sent = NULL;
    int rc, err;
    char buff[HOST_BUF_SIZE];

    // using reentrant version of gethostbyname()
    rc = gethostbyname_r(hostname, &sent_struct, 
            buff, HOST_BUF_SIZE, &sent, &err);
    if((rc) || (sent == NULL)) {
        return ERR_SOCK_RESOLVE;
    }
    memset((char *)&s_addr, 0, sizeof(s_addr));
    s_addr.sin_family = AF_INET;
    memcpy(&s_addr.sin_addr.s_addr, sent->h_addr, sent->h_length);
    s_addr.sin_port = htons(port);

    // Enable keepalive so we will be informed if the peer
    // node goes away before it can inform us.
    setKeepalive(sock);

    rc = connect(sock, (struct sockaddr *)&s_addr, sizeof(s_addr));
    if(rc < 0) {
        if (errno != EINPROGRESS)
            return ERR_SOCK_CONNECT;

        return SOCK_CONNECTING;
    }

    return SUCCESS;
}
Beispiel #2
0
Sock::status_t
Sock::setupListener(unsigned short port, int& listen_sock,
                    int sndBufSz, int rcvBufSz)
{
    int rc;
    struct sockaddr_in s_addr;

    listen_sock = socket(PF_INET, SOCK_STREAM, 0);
    if(listen_sock < 0) {
        LOG_ERROR("Sock::setupListener: socket: %s\n", strerror(errno));
        return ERR_SOCK_CREATE;
    }


    setCloExec(listen_sock);

    memset((char *)&s_addr, 0, sizeof(s_addr));
    s_addr.sin_family = AF_INET;
    s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    s_addr.sin_port = htons(port);

    // Reuse addr so the server can be restarted immediately. Eliminates
    // CLOSE_WAIT state of the server side of the socket
    setReuseAddr(listen_sock);

    // Enable keepalive so we will be informed if the peer
    // node goes away before it can inform us.
    setKeepalive(listen_sock); 

    // Set send and receive buffer sizes before listen (see man 7 tcp)
    setBufSize(listen_sock, sndBufSz, rcvBufSz);

    rc = bind(listen_sock, (struct sockaddr *)&s_addr, sizeof(s_addr));
    if(rc < 0) {
        LOG_ERROR("Sock::setupListener: bind: %s\n", strerror(errno));
        return ERR_SOCK_BIND;
    }

    // We could just use SOMAXCONN
    // rc = listen(listen_sock, SOMAXCONN);
    rc = listen(listen_sock, Sock::LISTEN_BACKLOG);
    if(rc < 0) {
        LOG_ERROR("Sock::setupListener: listen: %s\n", strerror(errno));
        return ERR_SOCK_LISTEN;
    }

    return SUCCESS;
}
Beispiel #3
0
Sock::status_t
Sock::connectToUdServer(const char* path, int& sock)
{
    struct sockaddr_un s_addr;
    int rc;
    memset((char *)&s_addr, 0, sizeof(s_addr));
    s_addr.sun_family = AF_LOCAL;
    strcpy(s_addr.sun_path, path);

    // Enable keepalive so we will be informed if the peer
    // node goes away before it can inform us.
    setKeepalive(sock);
    rc = connect(sock, (struct sockaddr *)&s_addr, sizeof(s_addr));
    if(rc < 0) {
        return ERR_SOCK_CONNECT;
    }
    return SUCCESS;
}
Beispiel #4
0
void MythSocket::setSocket(int socket, Type type)
{
    VERBOSE(VB_SOCKET, LOC + QString("setSocket: %1").arg(socket));
    if (socket < 0)
    {
        VERBOSE(VB_SOCKET, LOC + "setSocket called with invalid socket");
        return;
    }

    if (state() == Connected)
    {
        VERBOSE(VB_SOCKET, LOC +
                "setSocket called while in Connected state, closing");
        close();
    }

    MSocketDevice::setSocket(socket, type);
    setBlocking(false);
    setState(Connected);
    setKeepalive(true);
}
Beispiel #5
0
/**
 *  \brief connect to host
 *  \return true on success
 */
bool MythSocket::connect(const QHostAddress &addr, quint16 port)
{
    if (state() == Connected)
    {
        VERBOSE(VB_SOCKET, LOC +
                "connect() called with already open socket, closing");
        close();
    }

    VERBOSE(VB_SOCKET, LOC + QString("attempting connect() to (%1:%2)")
            .arg(addr.toString()).arg(port));

    if (!MSocketDevice::connect(addr, port))
    {
        VERBOSE(VB_SOCKET, LOC + QString("connect() failed (%1)")
                .arg(errorToString()));
        setState(Idle);
        return false;
    }

    setReceiveBufferSize(kSocketBufferSize);
    setAddressReusable(true);
    setKeepalive(true);
    if (state() == Connecting)
    {
        setState(Connected);
        if (m_cb)
        {
            VERBOSE(VB_SOCKET, LOC + "calling m_cb->connected()");
            m_cb->connected(this);
            s_readyread_thread->WakeReadyReadThread();
        }
    }
    else
    {
        setState(Connected);
    }

    return true;
}