예제 #1
0
static int do_unconfirmed(TCP_Server *TCP_server, uint32_t i)
{
    TCP_Secure_Connection *conn = &TCP_server->unconfirmed_connection_queue[i];

    if (conn->status != TCP_STATUS_UNCONFIRMED)
        return -1;

    uint8_t packet[MAX_PACKET_SIZE];
    int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, conn->recv_nonce,
              packet, sizeof(packet));

    if (len == 0) {
        return -1;
    } else if (len == -1) {
        kill_TCP_connection(conn);
        return -1;
    } else {
        int index_new;

        if ((index_new = confirm_TCP_connection(TCP_server, conn, packet, len)) == -1) {
            kill_TCP_connection(conn);
        } else {
            memset(conn, 0, sizeof(TCP_Secure_Connection));
        }

        return index_new;
    }
}
예제 #2
0
static void do_TCP_unconfirmed(TCP_Server *TCP_server)
{
    uint32_t i;

    for (i = 0; i < MAX_INCOMMING_CONNECTIONS; ++i) {
        TCP_Secure_Connection *conn = &TCP_server->unconfirmed_connection_queue[i];

        if (conn->status != TCP_STATUS_UNCONFIRMED)
            continue;

        uint8_t packet[MAX_PACKET_SIZE];
        int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, conn->recv_nonce,
                  packet, sizeof(packet));

        if (len == 0) {
            continue;
        } else if (len == -1) {
            kill_TCP_connection(conn);
            continue;
        } else {
            if (confirm_TCP_connection(TCP_server, conn, packet, len) == -1) {
                kill_TCP_connection(conn);
            } else {
                memset(conn, 0, sizeof(TCP_Secure_Connection));
            }
        }
    }
}
예제 #3
0
static void do_TCP_unconfirmed(TCP_Server *TCP_server)
{
    uint32_t i;

    for (i = 0; i < MAX_INCOMMING_CONNECTIONS; ++i) {
        TCP_Secure_Connection *conn = &TCP_server->unconfirmed_connection_queue[i];

        if (conn->status != TCP_STATUS_UNCONFIRMED)
            continue;

        uint8_t packet[MAX_PACKET_SIZE];
        int len = read_packet_TCP_secure_connection(conn, packet, sizeof(packet));

        if (len == 0) {
            continue;
        } else if (len == -1) {
            kill_TCP_connection(conn);
            continue;
        } else {
            //TODO
            confirm_TCP_connection(conn, packet, len);
            kill_TCP_connection(conn);
        }
    }
}
예제 #4
0
static void do_TCP_confirmed(TCP_Server *TCP_server)
{
    uint32_t i;

    for (i = 0; i < TCP_server->size_accepted_connections; ++i) {
        TCP_Secure_Connection *conn = &TCP_server->accepted_connection_array[i];

        if (conn->status != TCP_STATUS_CONFIRMED)
            continue;

        if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
            uint8_t ping[1 + sizeof(uint64_t)];
            ping[0] = TCP_PACKET_PING;
            uint64_t ping_id = random_64b();

            if (!ping_id)
                ++ping_id;

            memcpy(ping + 1, &ping_id, sizeof(uint64_t));
            int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping));

            if (ret == 1) {
                conn->last_pinged = unix_time();
                conn->ping_id = ping_id;
            }
        }

        if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) {
            kill_TCP_connection(conn);
            del_accepted(TCP_server, i);
            continue;
        }

        send_pending_data(conn);
        uint8_t packet[MAX_PACKET_SIZE];
        int len;

        while ((len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key,
                      conn->recv_nonce, packet, sizeof(packet)))) {
            if (len == -1) {
                kill_TCP_connection(conn);
                del_accepted(TCP_server, i);
                break;
            }

            if (handle_TCP_packet(TCP_server, i, packet, len) == -1) {
                kill_TCP_connection(conn);
                del_accepted(TCP_server, i);
                break;
            }
        }
    }
}
예제 #5
0
static void do_confirmed_recv(TCP_Server *TCP_server, uint32_t i)
{
    TCP_Secure_Connection *conn = &TCP_server->accepted_connection_array[i];

    uint8_t packet[MAX_PACKET_SIZE];
    int len;

    while ((len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key,
                  conn->recv_nonce, packet, sizeof(packet)))) {
        if (len == -1) {
            kill_accepted(TCP_server, i);
            break;
        }

        if (handle_TCP_packet(TCP_server, i, packet, len) == -1) {
            kill_accepted(TCP_server, i);
            break;
        }
    }
}
예제 #6
0
static int do_confirmed_TCP(TCP_Client_Connection *conn, void *userdata)
{
    send_pending_data(conn);
    send_ping_response(conn);
    send_ping_request(conn);

    uint8_t packet[MAX_PACKET_SIZE];
    int len;

    if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
        uint64_t ping_id = random_64b();

        if (!ping_id) {
            ++ping_id;
        }

        conn->ping_request_id = conn->ping_id = ping_id;
        send_ping_request(conn);
        conn->last_pinged = unix_time();
    }

    if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) {
        conn->status = TCP_CLIENT_DISCONNECTED;
        return 0;
    }

    while ((len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key,
                  conn->recv_nonce, packet, sizeof(packet)))) {
        if (len == -1) {
            conn->status = TCP_CLIENT_DISCONNECTED;
            break;
        }

        if (handle_TCP_packet(conn, packet, len, userdata) == -1) {
            conn->status = TCP_CLIENT_DISCONNECTED;
            break;
        }
    }

    return 0;
}