Пример #1
0
void Submit::handshake()
{
    //emit add_log(LOG_INFO, "Submit::handshake");
    QString time_str = QString::number(QDateTime::currentDateTime().toTime_t());

    QCryptographicHash auth_hash(QCryptographicHash::Md5);
    auth_hash.addData(QString(context.password_hash + time_str).toLocal8Bit());
    QString auth = QString(auth_hash.result().toHex());

    QUrl url_handshake = QString( "http://%1/?hs=true&p=%2&c=%3&v=%4&u=%5&t=%6&a=%7" )
                .arg(context.host, PROTOCOL_VERSION, CLIENT_ID, CLIENT_VERSION )
                .arg( QString(QUrl::toPercentEncoding(context.username) ))
                .arg( time_str, auth );

    emit add_log(LOG_DEBUG, QString("%1: Time: %2")
                 .arg(SITE_NAME[index], time_str));
    emit add_log(LOG_DEBUG, QString("%1: Auth: %2")
                 .arg(SITE_NAME[index], auth));
    emit add_log(LOG_DEBUG, QString("%1: URL: %2")
                 .arg(SITE_NAME[index], url_handshake.toString(QUrl::None)));

    QNetworkRequest request_handshake;
    request_handshake.setUrl(url_handshake);
    request_handshake.setRawHeader("User-Agent", CLIENT_USERAGENT);

    nr_handshake = nam_handshake->get(request_handshake);
}
Пример #2
0
void* server_conn(void* t_data)
{
    int connfd = ((thread_t*)t_data)->fd;
    char* salt = ((thread_t*)t_data)->salt;
    char* ip = ((thread_t*)t_data)->ip;
    uint16_t port = ((thread_t*)t_data)->port;

    user_t *u;
    fd_set input;
    char buffer[100];
    int pos = 0, auth = 0;

    free(t_data);

    snprintf(buffer, sizeof(buffer), "%s\n", salt);
    send(connfd, buffer, strlen(buffer), MSG_NOSIGNAL);
    if(recv(connfd, buffer, 41, MSG_NOSIGNAL) == 41)
    {
        buffer[40] = 0;
        auth = auth_hash(salt, server.password, buffer);
    }

    free(salt);

    if(!auth && !server.guest)
    {
        snprintf(buffer, sizeof(buffer), "a0\n");
        send(connfd, buffer, strlen(buffer), MSG_NOSIGNAL);

#ifdef __WIN32__
        Sleep(2000);
#endif
        socket_close(connfd);
        free(ip);
        return NULL;
    }

    if(!auth && server.guest)
    {
        snprintf(buffer, sizeof(buffer), "a1\n");
        send(connfd, buffer, strlen(buffer), MSG_NOSIGNAL);
    }

#ifdef __WIN32__
    unsigned long on = 1;
    if (ioctlsocket(connfd, FIONBIO, &on) != NO_ERROR)
    {
        server_log(LOG_ERR, "server_conn: ioctlsocket");
        free(ip);
        exit(EXIT_FAILURE);
    }
#else
    fcntl(connfd, F_SETFL, O_NONBLOCK);
#endif

    server_log(LOG_INFO, "user connected: %s:%u%s", ip, port, (auth ? "" : " (guest)"));

    if(server.online_auth)
    {
        snprintf(buffer, sizeof(buffer),
                 "M%d\nY%d\nT%d\nD%d\nA%d\nF%d\nZ%d\nG%02d\nV%d\nQ%d\nC%d\nI%d,%d\n",
                 server.mode,
                 server.volume,
                 server.freq,
                 server.deemphasis,
                 server.agc,
                 server.filter,
                 server.ant,
                 server.gain,
                 server.daa,
                 server.squelch,
                 server.rotator,
                 server.sampling,
                 server.detector);
        send(connfd, buffer, strlen(buffer), MSG_NOSIGNAL);
    }

    u = user_add(&server, connfd, auth);

    snprintf(buffer, sizeof(buffer), "o%d,%d\n",
             server.online_auth,
             server.online - server.online_auth);
    msg_send(buffer, strlen(buffer));

    FD_ZERO(&input);
    FD_SET(u->fd, &input);
    while(select(u->fd+1, &input, NULL, NULL, NULL) > 0)
    {
        if(recv(u->fd, &buffer[pos], 1, MSG_NOSIGNAL) <= 0)
            break;

        /* If this command is too long to
         * fit into a buffer, clip it */
        if(buffer[pos] != '\n')
        {
            if(pos != sizeof(buffer)-1)
                pos++;
            continue;
        }

        if(buffer[0] == XDR_P_SHUTDOWN)
            break;

        if(u->auth)
            serial_write(buffer, pos+1);

        pos = 0;
    }

    user_remove(&server, u);
    server_log(LOG_INFO, "user disconnected: %s:%u", ip, port);
    free(ip);

    if(server.online)
    {
        snprintf(buffer, sizeof(buffer), "o%d,%d\n",
                 server.online_auth,
                 server.online - server.online_auth);
        msg_send(buffer, strlen(buffer));
    }

    if(!server.online_auth && server.poweroff)
    {
        if(server.online)
        {
            /* tell unauthenticated users that XDR has been powered off */
            sprintf(buffer, "X\n");
            msg_send(buffer, strlen(buffer));
        }
        server_log(LOG_INFO, "tuner shutdown");
        tuner_reset();
    }

    return NULL;
}