static int send_getnodes(const Group_Chat *chat, IP_Port ip_port, int peernum) { if ((uint32_t)peernum >= chat->numpeers) return -1; if (!is_timeout(chat->group[peernum].last_pinged, GROUP_PING_TIMEOUT)) return -1; getnodes_data contents; contents.pingid = random_64b(); chat->group[peernum].last_pinged = unix_time(); chat->group[peernum].pingid = contents.pingid; chat->group[peernum].ping_via = ip_port; if (chat->assoc) { IPPTs ippts; ippts.timestamp = unix_time(); ippts.ip_port = ip_port; Assoc_add_entry(chat->assoc, chat->group[peernum].client_id, &ippts, NULL, 1); } return send_groupchatpacket(chat, ip_port, chat->group[peernum].client_id, (uint8_t *)&contents, sizeof(contents), CRYPTO_PACKET_GROUP_CHAT_GET_NODES); }
static void do_TCP_confirmed(TCP_Server *TCP_server) { #ifdef TCP_SERVER_USE_EPOLL if (TCP_server->last_run_pinged == unix_time()) { return; } TCP_server->last_run_pinged = unix_time(); #endif 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), 1); if (ret == 1) { conn->last_pinged = unix_time(); conn->ping_id = ping_id; } else { if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) { kill_accepted(TCP_server, i); continue; } } } if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) { kill_accepted(TCP_server, i); continue; } send_pending_data(conn); #ifndef TCP_SERVER_USE_EPOLL do_confirmed_recv(TCP_server, i); #endif } }
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 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; }
static size_t add_ping(PING *ping, IP_Port ipp) // O(n) { size_t p; remove_timeouts(ping); /* Remove oldest ping if full buffer. */ if (ping->num_pings == PING_NUM_MAX) { ping->num_pings--; ping->pos_pings = (ping->pos_pings + 1) % PING_NUM_MAX; } /* Insert new ping at end of list. */ p = (ping->pos_pings + ping->num_pings) % PING_NUM_MAX; ping->pings[p].ip_port = ipp; ping->pings[p].timestamp = unix_time(); ping->pings[p].id = random_64b(); ping->num_pings++; return ping->pings[p].id; }
uint64_t add_ping(IP_Port ipp) // O(n) { size_t p; remove_timeouts(); // Remove oldest ping if full buffer if (num_pings == PING_NUM_MAX) { num_pings--; pos_pings = (pos_pings + 1) % PING_NUM_MAX; } // Insert new ping at end of list p = (pos_pings + num_pings) % PING_NUM_MAX; pings[p].ipp = ipp; pings[p].timestamp = now(); pings[p].id = random_64b(); num_pings++; return pings[p].id; }
TEST(random, ui64) { uint64_t a = random_64b(); uint64_t b = random_64b(); ASSERT_FALSE(a == b) << "You are very lucky this test has failed for you. The probability to fail equals 2.9*10^-39"; }