예제 #1
0
static int add_tcp_relay(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk)
{
    if (ip_port.ip.family == TCP_INET) {
        ip_port.ip.family = AF_INET;
    } else if (ip_port.ip.family == TCP_INET6) {
        ip_port.ip.family = AF_INET6;
    }

    if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6)
        return -1;

    int tcp_connections_number = create_tcp_connection(tcp_c);

    if (tcp_connections_number == -1)
        return -1;

    TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number];


    tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->dht->self_public_key, tcp_c->dht->self_secret_key,
                          &tcp_c->proxy_info);

    if (!tcp_con->connection)
        return -1;

    tcp_con->status = TCP_CONN_VALID;

    return tcp_connections_number;
}
예제 #2
0
static int unsleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number)
{
    TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

    if (!tcp_con) {
        return -1;
    }

    if (tcp_con->status != TCP_CONN_SLEEPING) {
        return -1;
    }

    tcp_con->connection = new_TCP_connection(tcp_con->ip_port, tcp_con->relay_pk, tcp_c->self_public_key,
                          tcp_c->self_secret_key, &tcp_c->proxy_info);

    if (!tcp_con->connection) {
        kill_tcp_relay_connection(tcp_c, tcp_connections_number);
        return -1;
    }

    tcp_con->lock_count = 0;
    tcp_con->sleep_count = 0;
    tcp_con->connected_time = 0;
    tcp_con->status = TCP_CONN_VALID;
    tcp_con->unsleep = 0;
    return 0;
}
예제 #3
0
static int add_tcp_relay_instance(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk)
{
    if (net_family_is_tcp_ipv4(ip_port.ip.family)) {
        ip_port.ip.family = net_family_ipv4;
    } else if (net_family_is_tcp_ipv6(ip_port.ip.family)) {
        ip_port.ip.family = net_family_ipv6;
    }

    if (!net_family_is_ipv4(ip_port.ip.family) && !net_family_is_ipv6(ip_port.ip.family)) {
        return -1;
    }

    int tcp_connections_number = create_tcp_connection(tcp_c);

    if (tcp_connections_number == -1) {
        return -1;
    }

    TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number];

    tcp_con->connection = new_TCP_connection(tcp_c->mono_time, ip_port, relay_pk, tcp_c->self_public_key,
                          tcp_c->self_secret_key, &tcp_c->proxy_info);

    if (!tcp_con->connection) {
        return -1;
    }

    tcp_con->status = TCP_CONN_VALID;

    return tcp_connections_number;
}
예제 #4
0
static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number)
{
    TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

    if (!tcp_con) {
        return -1;
    }

    if (tcp_con->status == TCP_CONN_SLEEPING) {
        return -1;
    }

    IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
    uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE];
    memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
    kill_TCP_connection(tcp_con->connection);
    tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key,
                          &tcp_c->proxy_info);

    if (!tcp_con->connection) {
        kill_tcp_relay_connection(tcp_c, tcp_connections_number);
        return -1;
    }

    unsigned int i;

    for (i = 0; i < tcp_c->connections_length; ++i) {
        TCP_Connection_to *con_to = get_connection(tcp_c, i);

        if (con_to) {
            set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_NONE, 0);
        }
    }

    if (tcp_con->onion) {
        --tcp_c->onion_num_conns;
        tcp_con->onion = 0;
    }

    tcp_con->lock_count = 0;
    tcp_con->sleep_count = 0;
    tcp_con->connected_time = 0;
    tcp_con->status = TCP_CONN_VALID;
    tcp_con->unsleep = 0;

    return 0;
}