/** * Retrieve cached security token for a given KUID. * * @param id the KUID for which we'd like the security token * @param len_ptr where the length of the security token is written * @param tok_ptr where the address of the security token is written * @param time_ptr where the last update time of token is writen * * @return TRUE if we found a token, with len_ptr and tok_ptr filled with * the information about the length and the token pointer. Information is * returned from a static memory buffer so it must be perused immediately. */ bool tcache_get(const kuid_t *id, uint8 *len_ptr, const void **tok_ptr, time_t *time_ptr) { struct tokdata *td; g_assert(id != NULL); td = get_tokdata(id); if (NULL == td) return FALSE; if (delta_time(tm_time(), td->last_update) > token_life) { delete_tokdata(id); return FALSE; } if (len_ptr != NULL) *len_ptr = td->length; if (tok_ptr != NULL) *tok_ptr = td->token; if (time_ptr != NULL) *time_ptr = td->last_update; if (GNET_PROPERTY(dht_tcache_debug) > 4) { char buf[80]; bin_to_hex_buf(td->token, td->length, buf, sizeof buf); g_debug("DHT TCACHE security token for %s is %u-byte \"%s\" (%s)", kuid_to_hex_string(id), td->length, buf, compact_time(delta_time(tm_time(), td->last_update))); } gnet_stats_inc_general(GNR_DHT_CACHED_TOKENS_HITS); return TRUE; }
/** * Pretty-printing of node information for logs into the supplied buffers. * * IP address is followed by '*' if the contact's address/port was patched. * IP address is followed by '?' if the UDP message came from another IP * * A "zombie" node is a node retrieved from the persisted routing table that * is not alive. Normally, only alive hosts from which we get traffic are * added, but here we have an instance that is not alive -- a zombie. * * A "cached" node is a node coming from the k-closest root cache. * * A firewalled node is indicated by a trailing "fw" indication. * * @return the buffer where printing was done. */ const char * knode_to_string_buf(const knode_t *kn, char buf[], size_t len) { char host_buf[HOST_ADDR_PORT_BUFLEN]; char vc_buf[VENDOR_CODE_BUFLEN]; char kuid_buf[KUID_HEX_BUFLEN]; knode_check(kn); bin_to_hex_buf(kn->id, KUID_RAW_SIZE, kuid_buf, sizeof kuid_buf); host_addr_port_to_string_buf(kn->addr, kn->port, host_buf, sizeof host_buf); vendor_code_to_string_buf(kn->vcode.u32, vc_buf, sizeof vc_buf); str_bprintf(buf, len, "%s%s%s (%s v%u.%u) [%s] \"%s\", ref=%d%s%s%s%s [%s]", host_buf, (kn->flags & KNODE_F_PCONTACT) ? "*" : "", (kn->flags & KNODE_F_FOREIGN_IP) ? "?" : "", vc_buf, kn->major, kn->minor, kuid_buf, knode_status_to_string(kn->status), kn->refcnt, (kn->status != KNODE_UNKNOWN && !(kn->flags & KNODE_F_ALIVE)) ? " zombie" : "", (kn->flags & KNODE_F_CACHED) ? " cached" : "", (kn->flags & KNODE_F_RPC) ? " RPC" : "", (kn->flags & KNODE_F_FIREWALLED) ? " fw" : "", compact_time(delta_time(tm_time(), kn->first_seen))); return buf; }
/** * Convert a KUID to an hex string. * * @return pointer to static data. */ const char * kuid_to_hex_string2(const kuid_t *kuid) { static char buf[KUID_HEX_BUFLEN]; bin_to_hex_buf(kuid, KUID_RAW_SIZE, buf, sizeof buf); return buf; }
/** * Map iterator to record security tokens in the database. */ static void record_token(void *key, void *value, void *unused_u) { kuid_t *id = key; lookup_token_t *ltok = value; struct tokdata td; (void) unused_u; td.last_update = ltok->retrieved; td.length = ltok->token->length; td.token = td.length ? wcopy(ltok->token->v, td.length) : NULL; if (GNET_PROPERTY(dht_tcache_debug) > 4) { char buf[80]; bin_to_hex_buf(td.token, td.length, buf, sizeof buf); g_debug("DHT TCACHE adding security token for %s: %u-byte \"%s\"", kuid_to_hex_string(id), td.length, buf); } /* * Data is put in the DBMW cache and the dynamically allocated token * will be freed via free_tokdata() when the cached entry is released. */ if (!dbmw_exists(db_tokdata, id->v)) gnet_stats_inc_general(GNR_DHT_CACHED_TOKENS_HELD); dbmw_write(db_tokdata, id->v, &td, sizeof td); if (GNET_PROPERTY(dht_tcache_debug_flags) & DBG_DSF_USR1) { g_debug("DHT TCACHE %s: stats=%s, count=%zu, id=%s", G_STRFUNC, uint64_to_string( gnet_stats_get_general(GNR_DHT_CACHED_TOKENS_HELD)), dbmw_count(db_tokdata), kuid_to_hex_string(id)); } }