static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length) { if (length == 0) return -1; TCP_Client_Connection *TCP_client_con = object; TCP_Connections *tcp_c = TCP_client_con->custom_object; unsigned int tcp_connections_number = TCP_client_con->custom_uint; TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) return -1; /* TODO: optimize */ int connections_number = find_tcp_connection_to(tcp_c, public_key); if (connections_number == -1) { if (tcp_c->tcp_oob_callback) tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length); } else { return tcp_data_callback(object, connections_number, 0, data, length); } return 0; }
static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key) { TCP_Client_Connection *TCP_client_con = object; TCP_Connections *tcp_c = TCP_client_con->custom_object; unsigned int tcp_connections_number = TCP_client_con->custom_uint; TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) return -1; int connections_number = find_tcp_connection_to(tcp_c, public_key); if (connections_number == -1) return -1; set_tcp_connection_number(tcp_con->connection, connection_id, connections_number); TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); if (con_to == NULL) return -1; if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) return -1; return 0; }
static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key) { TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object; TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con); unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con); TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) { return -1; } int connections_number = find_tcp_connection_to(tcp_c, public_key); if (connections_number == -1) { return -1; } TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); if (con_to == nullptr) { return -1; } if (set_tcp_connection_status(con_to, tcp_connections_number, TCP_CONNECTIONS_STATUS_REGISTERED, connection_id) == -1) { return -1; } set_tcp_connection_number(tcp_con->connection, connection_id, connections_number); return 0; }
static int tcp_conn_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) { if (length == 0) { return -1; } TCP_Client_Connection *TCP_client_con = (TCP_Client_Connection *)object; TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(TCP_client_con); unsigned int tcp_connections_number = tcp_con_custom_uint(TCP_client_con); TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); if (!tcp_con) { return -1; } /* TODO(irungentoo): optimize */ int connections_number = find_tcp_connection_to(tcp_c, public_key); TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); if (con_to && tcp_connection_in_conn(con_to, tcp_connections_number)) { return tcp_conn_data_callback(object, connections_number, 0, data, length, userdata); } if (tcp_c->tcp_oob_callback) { tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length, userdata); } return 0; }
/* Create a new TCP connection to public_key. * * id is the id in the callbacks for that connection. * * return connections_number on success. * return -1 on failure. */ int new_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key, int id) { if (find_tcp_connection_to(tcp_c, public_key) != -1) return -1; int connections_number = create_connection(tcp_c); if (connections_number == -1) return -1; TCP_Connection_to *con_to = &tcp_c->connections[connections_number]; con_to->status = TCP_CONN_VALID; memcpy(con_to->public_key, public_key, crypto_box_PUBLICKEYBYTES); con_to->id = id; return connections_number; }
/* Create a new TCP connection to public_key. * * public_key must be the counterpart to the secret key that the other peer used with new_tcp_connections(). * * id is the id in the callbacks for that connection. * * return connections_number on success. * return -1 on failure. */ int new_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key, int id) { if (find_tcp_connection_to(tcp_c, public_key) != -1) { return -1; } int connections_number = create_connection(tcp_c); if (connections_number == -1) { return -1; } TCP_Connection_to *con_to = &tcp_c->connections[connections_number]; con_to->status = TCP_CONN_VALID; memcpy(con_to->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); con_to->id = id; return connections_number; }