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; } } } }
static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t *data, uint16_t length) { int index = add_accepted(TCP_server, con); if (index == -1) return -1; TCP_Secure_Connection *conn = &TCP_server->accepted_connection_array[index]; if (handle_TCP_packet(TCP_server, index, data, length) == -1) { kill_TCP_connection(conn); del_accepted(TCP_server, index); } return 0; }
/* Kill an accepted TCP_Secure_Connection * * return -1 on failure. * return 0 on success. */ static int kill_accepted(TCP_Server *TCP_server, int index) { if ((uint32_t)index >= TCP_server->size_accepted_connections) return -1; uint32_t i; for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { rm_connection_index(TCP_server, &TCP_server->accepted_connection_array[index], i); } sock_t sock = TCP_server->accepted_connection_array[index].sock; if (del_accepted(TCP_server, index) != 0) return -1; kill_sock(sock); return 0; }