/* Send an oob packet via the TCP relay corresponding to tcp_connections_number.
 *
 * return 0 on success.
 * return -1 on failure.
 */
int tcp_send_oob_packet(TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *public_key,
                        const uint8_t *packet, uint16_t length)
{
    TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

    if (!tcp_con)
        return -1;

    int ret = send_oob_packet(tcp_con->connection, public_key, packet, length);

    if (ret == 1)
        return 0;

    return -1;
}
Esempio n. 2
0
/* Send an oob packet via the TCP relay corresponding to tcp_connections_number.
 *
 * return 0 on success.
 * return -1 on failure.
 */
int tcp_send_oob_packet(TCP_Connections *tcp_c, unsigned int tcp_connections_number, const uint8_t *public_key,
                        const uint8_t *packet, uint16_t length)
{
    TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

    if (!tcp_con) {
        return -1;
    }

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

    int ret = send_oob_packet(tcp_con->connection, public_key, packet, length);

    if (ret == 1) {
        return 0;
    }

    return -1;
}
/* Send a packet to the TCP connection.
 *
 * return -1 on failure.
 * return 0 on success.
 */
int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, const uint8_t *packet, uint16_t length)
{
    TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);

    if (!con_to) {
        return -1;
    }

    //TODO: detect and kill bad relays.
    //TODO: thread safety?
    unsigned int i;
    int ret = -1;

    for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
        uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
        uint8_t status = con_to->connections[i].status;
        uint8_t connection_id = con_to->connections[i].connection_id;

        if (tcp_con_num && status == TCP_CONNECTIONS_STATUS_ONLINE) {
            tcp_con_num -= 1;
            TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_con_num);

            if (!tcp_con) {
                continue;
            }

            ret = send_data(tcp_con->connection, connection_id, packet, length);

            if (ret == 1) {
                break;
            }
        }
    }

    if (ret == 1) {
        return 0;
    } else {
        for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
            uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
            uint8_t status = con_to->connections[i].status;
            uint8_t connection_id = con_to->connections[i].connection_id;

            if (tcp_con_num && status == TCP_CONNECTIONS_STATUS_REGISTERED) {
                tcp_con_num -= 1;
                TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_con_num);

                if (!tcp_con) {
                    continue;
                }

                ret = send_oob_packet(tcp_con->connection, con_to->public_key, packet, length);

                if (ret == 1) {
                    break;
                }
            }
        }

        if (ret == 1) {
            return 0;
        } else {
            return -1;
        }
    }
}
Esempio n. 4
0
/* Send a packet to the TCP connection.
 *
 * return -1 on failure.
 * return 0 on success.
 */
int send_packet_tcp_connection(TCP_Connections *tcp_c, int connections_number, const uint8_t *packet, uint16_t length)
{
    TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);

    if (!con_to) {
        return -1;
    }

    // TODO(irungentoo): detect and kill bad relays.
    // TODO(irungentoo): thread safety?
    unsigned int i;
    int ret = -1;

    bool limit_reached = 0;

    for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
        uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
        uint8_t status = con_to->connections[i].status;
        uint8_t connection_id = con_to->connections[i].connection_id;

        if (tcp_con_num && status == TCP_CONNECTIONS_STATUS_ONLINE) {
            tcp_con_num -= 1;
            TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_con_num);

            if (!tcp_con) {
                continue;
            }

            ret = send_data(tcp_con->connection, connection_id, packet, length);

            if (ret == 0) {
                limit_reached = 1;
            }

            if (ret == 1) {
                break;
            }
        }
    }

    if (ret == 1) {
        return 0;
    }

    if (!limit_reached) {
        ret = 0;

        /* Send oob packets to all relays tied to the connection. */
        for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
            uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
            uint8_t status = con_to->connections[i].status;

            if (tcp_con_num && status == TCP_CONNECTIONS_STATUS_REGISTERED) {
                tcp_con_num -= 1;
                TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_con_num);

                if (!tcp_con) {
                    continue;
                }

                if (send_oob_packet(tcp_con->connection, con_to->public_key, packet, length) == 1) {
                    ret += 1;
                }
            }
        }

        if (ret >= 1) {
            return 0;
        }

        return -1;
    }

    return -1;
}