bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else. { if (ipp.ip.i == 0 && ping_id == 0) return false; size_t i, id; remove_timeouts(); for (i=0; i<num_pings; i++) { id = (pos_pings + i) % PING_NUM_MAX; // ping_id = 0 means match any id if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) { return true; } } return false; }
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; }
static bool is_pinging(PING *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Replace this with something else. { /* shouldn't that be an OR ? */ if (!ip_isset(&ipp.ip) && ping_id == 0) return false; size_t i, id; remove_timeouts(ping); for (i = 0; i < ping->num_pings; i++) { id = (ping->pos_pings + i) % PING_NUM_MAX; /* ping_id = 0 means match any id. */ if ((!ip_isset(&ipp.ip) || ipport_equal(&ping->pings[id].ip_port, &ipp)) && (ping->pings[id].id == ping_id || ping_id == 0)) { return true; } } return false; }
/* checks if ip/port or ping_id are already in the list to ping * if both are set, both must match, otherwise the set must match * * returns 0 if neither is set or no match was found * returns the (index + 1) of the match if one was found */ static int is_pinging(PING *ping, IP_Port ipp, size_t ping_id) { // O(n) TODO: Replace this with something else. /* at least one MUST be set */ size_t ip_valid = ip_isset(&ipp.ip); if (!ip_valid && !ping_id) return 0; size_t i; remove_timeouts(ping); for (i = 0; i < ping->num_pings; i++) { size_t id = (ping->pos_pings + i) % PING_NUM_MAX; if (!ping_id || (ping->pings[id].id == ping_id)) if (!ip_valid || ipport_equal(&ping->pings[id].ip_port, &ipp)) return id + 1; } return 0; }