X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fmesh%2Fgnunet-service-mesh.c;h=f0a42f118f32942b94172e7eb783c8c1f999ed99;hb=60b22d16687c7cf4e9af0641448e1c182ad262e2;hp=f1eded3e6dae0b7241660c9d3f6cfe393e5f87b4;hpb=c15fdff413ddca047175351e8ce45c5be94f52e1;p=oweals%2Fgnunet.git diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c index f1eded3e6..f0a42f118 100644 --- a/src/mesh/gnunet-service-mesh.c +++ b/src/mesh/gnunet-service-mesh.c @@ -61,7 +61,9 @@ #define MESH_DEBUG_CONNECTION GNUNET_NO #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO -#define MESH_POLL_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) +#define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\ + GNUNET_TIME_UNIT_MINUTES,\ + 10) #if MESH_DEBUG_CONNECTION #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) @@ -182,6 +184,11 @@ struct MeshRegexDescriptor */ char *regex; + /** + * Cached DFA for regular expression 'regex'. + */ + struct GNUNET_REGEX_Automaton *dfa; + /** * How many characters per edge can we squeeze? */ @@ -516,6 +523,11 @@ struct MeshTunnel * Total messages pending for this tunnels, payload or not. */ unsigned int pending_messages; + + /** + * If the tunnel is empty, destoy it. + */ + GNUNET_SCHEDULER_TaskIdentifier delayed_destroy; }; @@ -579,7 +591,12 @@ struct MeshTunnelChildInfo /** * Task to poll peer in case of a stall. */ - GNUNET_SCHEDULER_TaskIdentifier fc_poll; + GNUNET_SCHEDULER_TaskIdentifier fc_poll; + + /** + * Time to use for next polling call. + */ + struct GNUNET_TIME_Relative fc_poll_time; }; @@ -731,6 +748,11 @@ struct MeshClient */ GNUNET_SCHEDULER_TaskIdentifier regex_announce_task; + /** + * Tmp store for partially retrieved regex. + */ + char *partial_regex; + }; @@ -911,6 +933,11 @@ static unsigned long long max_tunnels; */ static unsigned long long max_msgs_queue; +/** + * How many peers do we want to remember? + */ +static unsigned long long max_peers; + /*************************** Static global variables **************************/ @@ -1174,7 +1201,7 @@ tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer); * @return tunnel handler, NULL if doesn't exist. */ static struct MeshTunnel * -tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid); +tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid); /** @@ -1385,7 +1412,7 @@ regex_edge_iterator (void *cls, current_len = strlen (info->description) - ctx->position; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* token : %.*s\n", len, token); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key)); if (len > current_len) { @@ -1439,7 +1466,7 @@ regex_next_edge (const struct MeshRegexBlock *block, ctx->longest_match = 0; result = GNUNET_MESH_regex_block_iterate (block, size, ®ex_edge_iterator, ctx); - GNUNET_break (GNUNET_OK == result || SIZE_MAX == size); + GNUNET_break (GNUNET_OK == result); /* Did anything match? */ if (0 == ctx->longest_match) @@ -1533,138 +1560,6 @@ regex_free_result (void *cls, } -/** - * Regex callback iterator to store own service description in the DHT. - * - * @param cls closure. - * @param key hash for current state. - * @param proof proof for current state. - * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not. - * @param num_edges number of edges leaving current state. - * @param edges edges leaving current state. - */ -void -regex_iterator (void *cls, - const struct GNUNET_HashCode *key, - const char *proof, - int accepting, - unsigned int num_edges, - const struct GNUNET_REGEX_Edge *edges) -{ - struct MeshRegexBlock *block; - struct MeshRegexEdge *block_edge; - enum GNUNET_DHT_RouteOption opt; - size_t size; - size_t len; - unsigned int i; - unsigned int offset; - char *aux; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " regex dht put for state %s\n", - GNUNET_h2s(key)); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " proof: %s\n", - proof); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " num edges: %u\n", - num_edges); - - opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE; - if (GNUNET_YES == accepting) - { - struct MeshRegexAccept block; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " state %s is accepting, putting own id\n", - GNUNET_h2s(key)); - size = sizeof (block); - block.key = *key; - block.id = my_full_id; - (void) - GNUNET_DHT_put(dht_handle, key, - dht_replication_level, - opt | GNUNET_DHT_RO_RECORD_ROUTE, - GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, - size, - (char *) &block, - GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), - app_announce_time), - app_announce_time, - NULL, NULL); - } - len = strlen(proof); - size = sizeof (struct MeshRegexBlock) + len; - block = GNUNET_malloc (size); - - block->key = *key; - block->n_proof = htonl (len); - block->n_edges = htonl (num_edges); - block->accepting = htonl (accepting); - - /* Store the proof at the end of the block. */ - aux = (char *) &block[1]; - memcpy (aux, proof, len); - aux = &aux[len]; - - /* Store each edge in a variable length MeshEdge struct at the - * very end of the MeshRegexBlock structure. - */ - for (i = 0; i < num_edges; i++) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - " edge %s towards %s\n", - edges[i].label, - GNUNET_h2s(&edges[i].destination)); - - /* aux points at the end of the last block */ - len = strlen (edges[i].label); - size += sizeof (struct MeshRegexEdge) + len; - // Calculate offset FIXME is this ok? use size instead? - offset = aux - (char *) block; - block = GNUNET_realloc (block, size); - aux = &((char *) block)[offset]; - block_edge = (struct MeshRegexEdge *) aux; - block_edge->key = edges[i].destination; - block_edge->n_token = htonl (len); - aux = (char *) &block_edge[1]; - memcpy (aux, edges[i].label, len); - aux = &aux[len]; - } - (void) - GNUNET_DHT_put(dht_handle, key, - dht_replication_level, - opt, - GNUNET_BLOCK_TYPE_MESH_REGEX, size, - (char *) block, - GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), - app_announce_time), - app_announce_time, - NULL, NULL); - GNUNET_free (block); -} - - -/** - * Store the regular expression describing a local service into the DHT. - * - * @param regex The regular expresion. - */ -static void -regex_put (const struct MeshRegexDescriptor *regex) -{ - struct GNUNET_REGEX_Automaton *dfa; - - DEBUG_DHT (" regex_put (%s) start\n", regex->regex); - dfa = GNUNET_REGEX_construct_dfa (regex->regex, - strlen(regex->regex), - regex->compression); - GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL); - GNUNET_REGEX_automaton_destroy (dfa); - DEBUG_DHT (" regex_put (%s) end\n", regex); - -} - /** * Find a path to a peer that offers a regex servcie compatible * with a given string. @@ -1683,8 +1578,8 @@ regex_find_path (const struct GNUNET_HashCode *key, GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */ key, /* key to search */ dht_replication_level, /* replication level */ - GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE | - GNUNET_DHT_RO_RECORD_ROUTE, + GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE/* | + GNUNET_DHT_RO_RECORD_ROUTE*/, NULL, /* xquery */ // FIXME add BLOOMFILTER to exclude filtered peers 0, /* xquery bits */ @@ -1818,19 +1713,179 @@ announce_application (void *cls, const struct GNUNET_HashCode * key, void *value GNUNET_break (NULL != GNUNET_DHT_put (dht_handle, key, - dht_replication_level, - GNUNET_DHT_RO_RECORD_ROUTE | - GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, - GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE, - sizeof (block), - (const char *) &block, - GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), - app_announce_time), - app_announce_time, NULL, NULL)); + dht_replication_level, + GNUNET_DHT_RO_RECORD_ROUTE | + GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, + GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE, + sizeof (block), + (const char *) &block, + GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */ + app_announce_time, NULL, NULL)); return GNUNET_OK; } +#if 0 +/** + * Function called when the DHT regex put is complete. + * + * @param the 'struct MeshClient' for which we were PUTting + * @param success GNUNET_OK if the PUT was transmitted, + * GNUNET_NO on timeout, + * GNUNET_SYSERR on disconnect from service + * after the PUT message was transmitted + * (so we don't know if it was received or not) + */ +static void +announce_regex_done (void *cls, + int success) +{ + struct MeshClient *c = cls; + +} +#endif + + +/** + * Regex callback iterator to store own service description in the DHT. + * + * @param cls closure. + * @param key hash for current state. + * @param proof proof for current state. + * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not. + * @param num_edges number of edges leaving current state. + * @param edges edges leaving current state. + */ +static void +regex_iterator (void *cls, + const struct GNUNET_HashCode *key, + const char *proof, + int accepting, + unsigned int num_edges, + const struct GNUNET_REGEX_Edge *edges) +{ + struct MeshRegexBlock *block; + struct MeshRegexEdge *block_edge; + enum GNUNET_DHT_RouteOption opt; + size_t size; + size_t len; + unsigned int i; + unsigned int offset; + char *aux; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " regex dht put for state %s\n", + GNUNET_h2s(key)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " proof: %s\n", + proof); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " num edges: %u\n", + num_edges); + + opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE; + if (GNUNET_YES == accepting) + { + struct MeshRegexAccept block; + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " state %s is accepting, putting own id\n", + GNUNET_h2s(key)); + size = sizeof (block); + block.key = *key; + block.id = my_full_id; + GNUNET_STATISTICS_update (stats, "# regex accepting blocks stored", + 1, GNUNET_NO); + GNUNET_STATISTICS_update (stats, "# regex accepting block bytes stored", + sizeof (block), GNUNET_NO); + (void) + GNUNET_DHT_put(dht_handle, key, + dht_replication_level, + opt/* | GNUNET_DHT_RO_RECORD_ROUTE*/, + GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, + size, + (char *) &block, + GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: expiration time should be option */ + app_announce_time, + NULL, NULL); + } + len = strlen(proof); + size = sizeof (struct MeshRegexBlock) + len; + block = GNUNET_malloc (size); + + block->key = *key; + block->n_proof = htonl (len); + block->n_edges = htonl (num_edges); + block->accepting = htonl (accepting); + + /* Store the proof at the end of the block. */ + aux = (char *) &block[1]; + memcpy (aux, proof, len); + aux = &aux[len]; + + /* Store each edge in a variable length MeshEdge struct at the + * very end of the MeshRegexBlock structure. + */ + for (i = 0; i < num_edges; i++) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " edge %s towards %s\n", + edges[i].label, + GNUNET_h2s(&edges[i].destination)); + + /* aux points at the end of the last block */ + len = strlen (edges[i].label); + size += sizeof (struct MeshRegexEdge) + len; + // Calculate offset FIXME is this ok? use size instead? + offset = aux - (char *) block; + block = GNUNET_realloc (block, size); + aux = &((char *) block)[offset]; + block_edge = (struct MeshRegexEdge *) aux; + block_edge->key = edges[i].destination; + block_edge->n_token = htonl (len); + aux = (char *) &block_edge[1]; + memcpy (aux, edges[i].label, len); + aux = &aux[len]; + } + (void) + GNUNET_DHT_put(dht_handle, key, + dht_replication_level, + opt, + GNUNET_BLOCK_TYPE_MESH_REGEX, size, + (char *) block, + GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */ + app_announce_time, + NULL, NULL); + GNUNET_STATISTICS_update (stats, "# regex blocks stored", + 1, GNUNET_NO); + GNUNET_STATISTICS_update (stats, "# regex block bytes stored", + size, GNUNET_NO); + + GNUNET_free (block); +} + + +/** + * Store the regular expression describing a local service into the DHT. + * + * @param regex The regular expresion. + */ +static void +regex_put (struct MeshRegexDescriptor *regex) +{ + if (NULL == regex->dfa) + { + regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex, + strlen (regex->regex), + regex->compression); + } + + DEBUG_DHT (" regex_put (%s) start\n", regex->regex); + GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL); + DEBUG_DHT (" regex_put (%s) end\n", regex->regex); +} + + /** * Periodically announce what applications are provided by local clients * (by regex) @@ -1846,22 +1901,14 @@ announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK; if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) - { return; - } - DEBUG_DHT ("Starting PUT for regex\n"); - for (i = 0; i < c->n_regex; i++) - { regex_put (&c->regexes[i]); - } c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls); DEBUG_DHT ("Finished PUT for regex\n"); - - return; } @@ -1889,8 +1936,6 @@ announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications, cls); DEBUG_DHT ("Finished PUT for apps\n"); - - return; } @@ -2228,6 +2273,9 @@ send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id) { struct GNUNET_MESH_PeerControl pc; + if (NULL == t->owner || GNUNET_YES == t->destroy) + return; + pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD); pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl)); pc.tunnel_id = htonl (t->local_tid); @@ -2297,6 +2345,23 @@ send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c) } +/** + * Iterator over all the peers to remove the oldest not-used entry. + * + * @param cls Closure (unsued). + * @param key ID of the peer. + * @param value Peer_Info of the peer. + * + * FIXME implement + */ +static int +peer_info_timeout (void *cls, + const struct GNUNET_HashCode *key, + void *value) +{ + return GNUNET_YES; +} + /** * Retrieve the MeshPeerInfo stucture associated with the peer, create one * and insert it in the appropiate structures if the peer is not known yet. @@ -2315,10 +2380,17 @@ peer_info_get (const struct GNUNET_PeerIdentity *peer) { peer_info = (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo)); + if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers) + { + GNUNET_CONTAINER_multihashmap_iterate (peers, + &peer_info_timeout, + NULL); + } GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); peer_info->id = GNUNET_PEER_intern (peer); } + peer_info->last_contact = GNUNET_TIME_absolute_get(); return peer_info; } @@ -2486,6 +2558,9 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message, } #endif GNUNET_break (0); // FIXME sometimes fails (testing disconnect?) + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + " no direct connection to %s\n", + GNUNET_i2s (peer)); GNUNET_free (info->mesh_data->data); GNUNET_free (info->mesh_data); GNUNET_free (info); @@ -2971,7 +3046,7 @@ peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info, * Function called if the connection to the peer has been stalled for a while, * possibly due to a missed ACK. Poll the peer about its ACK status. * - * @param cls Closure (info about regex search). + * @param cls Closure (cinfo). * @param tc TaskContext. */ static void @@ -2997,8 +3072,11 @@ tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_PEER_resolve (cinfo->id, &id); send_prebuilt_message (&msg.header, &id, cinfo->t); - cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, - &tunnel_poll, cinfo); + cinfo->fc_poll_time = GNUNET_TIME_relative_min ( + MESH_MAX_POLL_TIME, + GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2)); + cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time, + &tunnel_poll, cinfo); } @@ -3201,7 +3279,7 @@ tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid) * @return tunnel handler, NULL if doesn't exist */ static struct MeshTunnel * -tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid) +tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid) { return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid); } @@ -3306,6 +3384,11 @@ tunnel_destroy_child (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n); } GNUNET_free_non_null (cinfo->send_buffer); + if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) + { + GNUNET_SCHEDULER_cancel (cinfo->fc_poll); + cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK; + } GNUNET_free (cinfo); return GNUNET_YES; } @@ -3669,6 +3752,9 @@ tunnel_get_neighbor_fc (struct MeshTunnel *t, cinfo->bck_ack = delta; cinfo->bck_pid = -1; + cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK; + cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS; + cinfo->send_buffer = GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max); @@ -4292,17 +4378,30 @@ tunnel_unlock_bck_queue (struct MeshTunnel *t) * valid. * * @param t The tunnel whose peers to notify. + * @param send_back Do we need to notify our parent node? */ static void -tunnel_send_destroy (struct MeshTunnel *t) +tunnel_send_destroy (struct MeshTunnel *t, int send_back) { struct GNUNET_MESH_TunnelDestroy msg; + struct GNUNET_PeerIdentity id; + GNUNET_PEER_Id parent; + + if (tree_count_children(t->tree) > 0) + { + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY); + GNUNET_PEER_resolve (t->id.oid, &msg.oid); + msg.tid = htonl (t->id.tid); + tunnel_send_multicast (t, &msg.header); + } + parent = tree_get_predecessor(t->tree); + if (GNUNET_NO == send_back || 0 == parent) + return; msg.header.size = htons (sizeof (msg)); msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY); - GNUNET_PEER_resolve (t->id.oid, &msg.oid); - msg.tid = htonl (t->id.tid); - tunnel_send_multicast (t, &msg.header); + send_prebuilt_message(&msg.header, &id, t); } @@ -4415,17 +4514,9 @@ tunnel_destroy (struct MeshTunnel *t) } } - if (t->nclients > 0) - { - if (GNUNET_YES != - GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t)) - { - GNUNET_break (0); - r = GNUNET_SYSERR; - } - GNUNET_free (t->clients); - GNUNET_free (t->clients_fc); - } + (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t); + GNUNET_free_non_null (t->clients); + GNUNET_free_non_null (t->clients_fc); if (NULL != t->peers) { @@ -4440,22 +4531,94 @@ tunnel_destroy (struct MeshTunnel *t) GNUNET_CONTAINER_multihashmap_destroy (t->children_fc); t->children_fc = NULL; - tree_iterate_children (t->tree, &tunnel_cancel_queues, t); - tree_destroy (t->tree); + tree_iterate_children (t->tree, &tunnel_cancel_queues, t); + tree_destroy (t->tree); + + if (NULL != t->regex_ctx) + regex_cancel_search (t->regex_ctx); + if (NULL != t->dht_get_type) + GNUNET_DHT_get_stop (t->dht_get_type); + if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task) + GNUNET_SCHEDULER_cancel (t->timeout_task); + if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task) + GNUNET_SCHEDULER_cancel (t->path_refresh_task); + + n_tunnels--; + GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO); + GNUNET_free (t); + return r; +} + +#define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS + +/** + * Tunnel is empty: destroy it. + * + * @param cls Closure (Tunnel). + * @param tc TaskContext. + */ +static void +tunnel_destroy_empty_delayed (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct MeshTunnel *t = cls; + + t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK; + if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) + return; + + if (0 != t->nclients || + 0 != tree_count_children (t->tree)) + return; + + #if MESH_DEBUG + { + struct GNUNET_PeerIdentity id; + + GNUNET_PEER_resolve (t->id.oid, &id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "executing destruction of empty tunnel %s [%X]\n", + GNUNET_i2s (&id), t->id.tid); + } + #endif + + tunnel_destroy (t); +} + + +/** + * Schedule tunnel destruction if is empty and no new traffic comes in a time. + * + * @param t Tunnel to destroy if empty. + */ +static void +tunnel_destroy_empty (struct MeshTunnel *t) +{ + if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy || + 0 != t->nclients || + 0 != tree_count_children (t->tree)) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "%u %u %u\n", + t->delayed_destroy, t->nclients, tree_count_children(t->tree)); + return; + } + + #if MESH_DEBUG + { + struct GNUNET_PeerIdentity id; - if (NULL != t->regex_ctx) - regex_cancel_search (t->regex_ctx); - if (NULL != t->dht_get_type) - GNUNET_DHT_get_stop (t->dht_get_type); - if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task) - GNUNET_SCHEDULER_cancel (t->timeout_task); - if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task) - GNUNET_SCHEDULER_cancel (t->path_refresh_task); + GNUNET_PEER_resolve (t->id.oid, &id); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "scheduling destruction of empty tunnel %s [%X]\n", + GNUNET_i2s (&id), t->id.tid); + } + #endif - n_tunnels--; - GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO); - GNUNET_free (t); - return r; + t->delayed_destroy = + GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME, + &tunnel_destroy_empty_delayed, + t); } @@ -4566,21 +4729,23 @@ tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer) * @return GNUNET_OK, keep iterating. */ static int -tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value) +tunnel_destroy_iterator (void *cls, + const struct GNUNET_HashCode * key, + void *value) { struct MeshTunnel *t = value; struct MeshClient *c = cls; - send_client_tunnel_disconnect(t, c); + send_client_tunnel_disconnect (t, c); if (c != t->owner) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Client %u is destination, keeping the tunnel alive.\n", c->id); - tunnel_delete_client(t, c); - client_delete_tunnel(c, t); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id); + tunnel_delete_client (t, c); + client_delete_tunnel (c, t); + tunnel_destroy_empty (t); return GNUNET_OK; } - tunnel_send_destroy(t); + tunnel_send_destroy (t, GNUNET_YES); t->owner = NULL; t->destroy = GNUNET_YES; @@ -5173,8 +5338,12 @@ queue_send (void *cls, size_t size, void *buf) GNUNET_i2s(&my_full_id)); if (NULL == cinfo) cinfo = tunnel_get_neighbor_fc (t, &dst_id); - cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(MESH_POLL_TIME, - &tunnel_poll, cinfo); + // FIXME unify bck/fwd structures, bck does not have cinfo right now + if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll) + { + cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time, + &tunnel_poll, cinfo); + } } } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size); @@ -5618,10 +5787,12 @@ handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MESH_TunnelDestroy *msg; struct MeshTunnel *t; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer)); msg = (struct GNUNET_MESH_TunnelDestroy *) message; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n", + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Got a TUNNEL DESTROY packet from %s\n", + GNUNET_i2s (peer)); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " for tunnel %s [%u]\n", GNUNET_i2s (&msg->oid), ntohl (msg->tid)); t = tunnel_get (&msg->oid, ntohl (msg->tid)); if (NULL == t) @@ -5630,7 +5801,8 @@ handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer, * destroyed the tunnel and retransmitted to children. * Safe to ignore. */ - GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO); + GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", + 1, GNUNET_NO); return GNUNET_OK; } if (t->id.oid == myid) @@ -5645,7 +5817,7 @@ handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer, t->local_tid, t->local_tid_dest); send_clients_tunnel_destroy (t); } - tunnel_send_destroy (t); + tunnel_send_destroy (t, GNUNET_YES); t->destroy = GNUNET_YES; // TODO: add timeout to destroy the tunnel anyway return GNUNET_OK; @@ -5878,6 +6050,7 @@ handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer, struct MeshPeerInfo *peer_info; struct MeshTunnel *t; struct MeshTunnelChildInfo *cinfo; + GNUNET_PEER_Id predecessor; size_t size; uint32_t pid; @@ -5900,10 +6073,9 @@ handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer, { /* TODO notify that we dont know this tunnel (whom)? */ GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO); - GNUNET_break_op (0); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Received to_origin with PID %u on unknown tunnel\n", - pid); + "Received to_origin with PID %u on unknown tunnel %s [%u]\n", + pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid)); return GNUNET_OK; } @@ -5957,7 +6129,31 @@ handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer, GNUNET_break (0); return GNUNET_OK; } - GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id); + predecessor = tree_get_predecessor (t->tree); + if (0 == predecessor) + { + if (GNUNET_YES == t->destroy) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "to orig received on a dying tunnel %s [%X]\n", + GNUNET_i2s (&msg->oid), ntohl(msg->tid)); + return GNUNET_OK; + } + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "unknown to origin at %s\n", + GNUNET_i2s (&my_full_id)); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "from peer %s\n", + GNUNET_i2s (peer)); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "for tunnel %s [%X]\n", + GNUNET_i2s (&msg->oid), ntohl(msg->tid)); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "current tree:\n"); + tree_debug (t->tree); + return GNUNET_OK; + } + GNUNET_PEER_resolve (predecessor, &id); send_prebuilt_message (message, &id, t); GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO); @@ -6013,6 +6209,12 @@ handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer, cinfo->fwd_ack = ack; tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK); tunnel_unlock_fwd_queues (t); + if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll) + { + GNUNET_SCHEDULER_cancel (cinfo->fc_poll); + cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK; + cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS; + } } else { @@ -6487,17 +6689,21 @@ dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp, const struct MeshRegexAccept *block = data; struct MeshRegexSearchContext *ctx = cls; struct MeshRegexSearchInfo *info = ctx->info; - struct MeshPeerPath *p; +// struct MeshPeerPath *p; struct MeshPeerInfo *peer_info; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n"); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description); + GNUNET_STATISTICS_update (stats, "# regex accepting blocks found", + 1, GNUNET_NO); + GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found", + size, GNUNET_NO); peer_info = peer_info_get(&block->id); - p = path_build_from_dht (get_path, get_path_length, put_path, - put_path_length); - path_add_to_peers (p, GNUNET_NO); - path_destroy(p); +// p = path_build_from_dht (get_path, get_path_length, put_path, +// put_path_length); +// path_add_to_peers (p, GNUNET_NO); +// path_destroy(p); tunnel_add_peer (info->t, peer_info); peer_info_connect (peer_info, info->t); @@ -6556,6 +6762,11 @@ dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " key: %s\n", GNUNET_h2s (key)); + GNUNET_STATISTICS_update (stats, "# regex blocks found", + 1, GNUNET_NO); + GNUNET_STATISTICS_update (stats, "# regex block bytes found", + size, GNUNET_NO); + copy = GNUNET_malloc (size); memcpy (copy, data, size); GNUNET_break (GNUNET_OK == @@ -6619,7 +6830,6 @@ handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) return; } -// return; uncomment for regex_profiler c = clients; while (NULL != c) { @@ -6662,6 +6872,8 @@ handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) for (i = 0; i < c->n_regex; i++) { GNUNET_free (c->regexes[i].regex); + if (NULL != c->regexes[i].dfa) + GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa); } GNUNET_free_non_null (c->regexes); if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task) @@ -6789,11 +7001,12 @@ static void handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { - struct GNUNET_MESH_RegexAnnounce *msg; + const struct GNUNET_MESH_RegexAnnounce *msg; struct MeshRegexDescriptor rd; struct MeshClient *c; char *regex; size_t len; + size_t offset; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n"); @@ -6806,17 +7019,43 @@ handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client, } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); - msg = (struct GNUNET_MESH_RegexAnnounce *) message; + msg = (const struct GNUNET_MESH_RegexAnnounce *) message; + len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce); - regex = GNUNET_malloc (len + 1); - memcpy (regex, &msg[1], len); - regex[len] = '\0'; + if (NULL != c->partial_regex) + { + regex = c->partial_regex; + offset = strlen (c->partial_regex); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " continuation, already have %u bytes\n", + offset); + } + else + { + regex = NULL; + offset = 0; + } + + regex = GNUNET_realloc (regex, offset + len + 1); + memcpy (®ex[offset], &msg[1], len); + regex[offset + len] = '\0'; + if (0 == ntohs (msg->last)) + { + c->partial_regex = regex; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + " not ended, stored %u bytes for later\n", + len); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } rd.regex = regex; rd.compression = ntohs (msg->compression_characters); + rd.dfa = NULL; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression)); GNUNET_array_append (c->regexes, c->n_regex, rd); + c->partial_regex = NULL; if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task) { c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c); @@ -6954,19 +7193,7 @@ handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client, if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) { client_ignore_tunnel (c, t); -#if 0 - // TODO: when to destroy incoming tunnel? - if (t->nclients == 0) - { - GNUNET_assert (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, - &hash, t)); - GNUNET_assert (GNUNET_YES == - GNUNET_CONTAINER_multihashmap_remove (t->peers, - &my_full_id.hashPubKey, - t)); - } -#endif + tunnel_destroy_empty (t); GNUNET_SERVER_receive_done (client, GNUNET_OK); return; } @@ -6975,7 +7202,7 @@ handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client, /* Don't try to ACK the client about the tunnel_destroy multicast packet */ t->owner = NULL; - tunnel_send_destroy (t); + tunnel_send_destroy (t, GNUNET_YES); t->destroy = GNUNET_YES; // The tunnel will be destroyed when the last message is transmitted. GNUNET_SERVER_receive_done (client, GNUNET_OK); @@ -7950,7 +8177,7 @@ handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client, ack = ntohl (msg->max_pid); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack); - /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */ + /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */ if (NULL != t->owner && t->owner->handle == client) { /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */ @@ -7964,12 +8191,299 @@ handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client, tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK); } - GNUNET_SERVER_receive_done (client, GNUNET_OK); + GNUNET_SERVER_receive_done (client, GNUNET_OK); return; } +/** + * Iterator over all peers to send a monitoring client info about a tunnel. + * + * @param cls Closure (message being built). + * @param key Key (hashed tunnel ID, unused). + * @param value Peer info. + * + * @return GNUNET_YES, to keep iterating. + */ +static int +monitor_peers_iterator (void *cls, + const struct GNUNET_HashCode * key, + void *value) +{ + struct GNUNET_MESH_LocalMonitor *msg = cls; + struct GNUNET_PeerIdentity *id; + struct MeshPeerInfo *info = value; + + id = (struct GNUNET_PeerIdentity *) &msg[1]; + GNUNET_PEER_resolve (info->id, &id[msg->npeers]); + msg->npeers++; + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "* sending info about peer %s [%u]\n", + GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers); + + return GNUNET_YES; +} + + + +/** + * Iterator over all tunnels to send a monitoring client info about each tunnel. + * + * @param cls Closure (client handle). + * @param key Key (hashed tunnel ID, unused). + * @param value Tunnel info. + * + * @return GNUNET_YES, to keep iterating. + */ +static int +monitor_all_tunnels_iterator (void *cls, + const struct GNUNET_HashCode * key, + void *value) +{ + struct GNUNET_SERVER_Client *client = cls; + struct MeshTunnel *t = value; + struct GNUNET_MESH_LocalMonitor *msg; + uint32_t npeers; + + npeers = GNUNET_CONTAINER_multihashmap_size (t->peers); + msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) + + npeers * sizeof (struct GNUNET_PeerIdentity)); + GNUNET_PEER_resolve(t->id.oid, &msg->owner); + msg->tunnel_id = htonl (t->id.tid); + msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) + + npeers * sizeof (struct GNUNET_PeerIdentity)); + msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS); + msg->npeers = 0; + (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers, + monitor_peers_iterator, + msg); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "* sending info about tunnel %s [%u] (%u peers)\n", + GNUNET_i2s (&msg->owner), t->id.tid, npeers); + + if (msg->npeers != npeers) + { + GNUNET_break (0); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Get tunnels fail: size %u - iter %u\n", + npeers, msg->npeers); + } + + msg->npeers = htonl (npeers); + GNUNET_SERVER_notification_context_unicast (nc, client, + &msg->header, GNUNET_NO); + return GNUNET_YES; +} + + +/** + * Handler for client's MONITOR request. + * + * @param cls Closure (unused). + * @param client Identification of the client. + * @param message The actual message. + */ +static void +handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + struct MeshClient *c; + + /* Sanity check for client registration */ + if (NULL == (c = client_get (client))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Received get tunnels request from client %u\n", + c->id); + GNUNET_CONTAINER_multihashmap_iterate (tunnels, + monitor_all_tunnels_iterator, + client); + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Get tunnels request from client %u completed\n", + c->id); + GNUNET_SERVER_receive_done (client, GNUNET_OK); +} + + +/** + * Data needed to build a Monitor_Tunnel message. + */ +struct MeshMonitorTunnelContext +{ + /** + * Partial message, including peer count. + */ + struct GNUNET_MESH_LocalMonitor *msg; + + /** + * Hashmap with positions: peer->position. + */ + struct GNUNET_CONTAINER_MultiHashMap *lookup; + + /** + * Index of the parent of each peer in the message, realtive to the absolute + * order in the array (can be in a previous message). + */ + uint32_t parents[1024]; + + /** + * Peers visited so far in the tree, aka position of the current peer. + */ + unsigned int npeers; + + /** + * Client requesting the info. + */ + struct MeshClient *c; +}; + + +/** + * Send a client a message about the structure of a tunnel. + * + * @param ctx Context of the tunnel iteration, with info regarding the state + * of the execution and the number of peers visited for this message. + */ +static void +send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx) +{ + struct GNUNET_MESH_LocalMonitor *resp = ctx->msg; + struct GNUNET_PeerIdentity *pid; + unsigned int *parent; + size_t size; + + size = sizeof (struct GNUNET_MESH_LocalMonitor); + size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers; + resp->header.size = htons (size); + pid = (struct GNUNET_PeerIdentity *) &resp[1]; + parent = (unsigned int *) &pid[resp->npeers]; + memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers); + GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle, + &resp->header, GNUNET_NO); +} + +/** + * Iterator over a tunnel tree to build a message containing all peers + * the in the tunnel, including relay nodes. + * + * @param cls Closure (pointer to pointer of message being built). + * @param peer Short ID of a peer. + * @param parent Short ID of the @c peer 's parent. + */ +static void +tunnel_tree_iterator (void *cls, + GNUNET_PEER_Id peer, + GNUNET_PEER_Id parent) +{ + struct MeshMonitorTunnelContext *ctx = cls; + struct GNUNET_MESH_LocalMonitor *msg; + struct GNUNET_PeerIdentity *pid; + struct GNUNET_PeerIdentity ppid; + + msg = ctx->msg; + pid = (struct GNUNET_PeerIdentity *) &msg[1]; + GNUNET_PEER_resolve (peer, &pid[msg->npeers]); + GNUNET_CONTAINER_multihashmap_put (ctx->lookup, + &pid[msg->npeers].hashPubKey, + (void *) (long) ctx->npeers, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); + GNUNET_PEER_resolve (parent, &ppid); + ctx->parents[msg->npeers] = + htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup, + &ppid.hashPubKey)); + + ctx->npeers++; + msg->npeers++; + + if (sizeof (struct GNUNET_MESH_LocalMonitor) + + (msg->npeers + 1) * + (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t)) + > USHRT_MAX) + { + send_client_tunnel_info (ctx); + msg->npeers = 0; + } +} + + +/** + * Handler for client's MONITOR_TUNNEL request. + * + * @param cls Closure (unused). + * @param client Identification of the client. + * @param message The actual message. + */ +static void +handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ + const struct GNUNET_MESH_LocalMonitor *msg; + struct GNUNET_MESH_LocalMonitor *resp; + struct MeshMonitorTunnelContext ctx; + struct MeshClient *c; + struct MeshTunnel *t; + + /* Sanity check for client registration */ + if (NULL == (c = client_get (client))) + { + GNUNET_break (0); + GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); + return; + } + + msg = (struct GNUNET_MESH_LocalMonitor *) message; + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Received tunnel info request from client %u for tunnel %s[%X]\n", + c->id, + &msg->owner, + ntohl (msg->tunnel_id)); + t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id)); + if (NULL == t) + { + /* We don't know the tunnel */ + struct GNUNET_MESH_LocalMonitor warn; + + warn = *msg; + warn.npeers = htonl (UINT_MAX); + GNUNET_SERVER_notification_context_unicast (nc, client, + &warn.header, + GNUNET_NO); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + /* Initialize context */ + resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */ + *resp = *msg; + resp->npeers = 0; + ctx.msg = resp; + ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total, + GNUNET_YES); + ctx.c = c; + + /* Collect and send information */ + tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx); + send_client_tunnel_info (&ctx); + + /* Free context */ + GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup); + GNUNET_free (resp); + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Monitor tunnel request from client %u completed\n", + c->id); + GNUNET_SERVER_receive_done (client, GNUNET_OK); +} + + /** * Functions to handle messages from clients */ @@ -8022,6 +8536,12 @@ static struct GNUNET_SERVER_MessageHandler client_handlers[] = { {&handle_local_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, sizeof (struct GNUNET_MESH_LocalAck)}, + {&handle_local_get_tunnels, NULL, + GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS, + sizeof (struct GNUNET_MessageHeader)}, + {&handle_local_show_tunnel, NULL, + GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL, + sizeof (struct GNUNET_MESH_LocalMonitor)}, {NULL, NULL, 0, 0} }; @@ -8044,6 +8564,12 @@ core_init (void *cls, struct GNUNET_CORE_Handle *server, NULL == server) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n")); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + " core id %s\n", + GNUNET_i2s (identity)); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + " my id %s\n", + GNUNET_i2s (&my_full_id)); GNUNET_SCHEDULER_shutdown (); // Try gracefully if (10 < i++) GNUNET_abort(); // Try harder @@ -8203,7 +8729,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_CORE_disconnect (core_handle); core_handle = NULL; } - if (NULL != keygen) + if (NULL != keygen) { GNUNET_CRYPTO_rsa_key_create_stop (keygen); keygen = NULL; @@ -8225,6 +8751,11 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_SCHEDULER_cancel (announce_id_task); announce_id_task = GNUNET_SCHEDULER_NO_TASK; } + if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task) + { + GNUNET_SCHEDULER_cancel (announce_applications_task); + announce_applications_task = GNUNET_SCHEDULER_NO_TASK; + } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n"); } @@ -8232,7 +8763,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) /** * Callback for hostkey read/generation * - * @param cls NULL + * @param cls Closure (Configuration handle). * @param pk the private key * @param emsg error message */ @@ -8241,6 +8772,7 @@ key_generation_cb (void *cls, struct GNUNET_CRYPTO_RsaPrivateKey *pk, const char *emsg) { + const struct GNUNET_CONFIGURATION_Handle *c = cls; struct MeshPeerInfo *peer; struct MeshPeerPath *p; @@ -8248,7 +8780,8 @@ key_generation_cb (void *cls, if (NULL == pk) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _("Mesh service could not access hostkey. Exiting.\n")); + _("Mesh service could not access hostkey: %s. Exiting.\n"), + emsg); GNUNET_SCHEDULER_shutdown (); return; } @@ -8268,7 +8801,23 @@ key_generation_cb (void *cls, // NULL, // NULL); - + core_handle = GNUNET_CORE_connect (c, /* Main configuration */ + NULL, /* Closure passed to MESH functions */ + &core_init, /* Call core_init once connected */ + &core_connect, /* Handle connects */ + &core_disconnect, /* remove peers on disconnects */ + NULL, /* Don't notify about all incoming messages */ + GNUNET_NO, /* For header only in notification */ + NULL, /* Don't notify about all outbound messages */ + GNUNET_NO, /* For header-only out notification */ + core_handlers); /* Register these handlers */ + + if (core_handle == NULL) + { + GNUNET_break (0); + GNUNET_SCHEDULER_shutdown (); + return; + } next_tid = 0; next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV; @@ -8313,23 +8862,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n"); server_handle = server; - core_handle = GNUNET_CORE_connect (c, /* Main configuration */ - NULL, /* Closure passed to MESH functions */ - &core_init, /* Call core_init once connected */ - &core_connect, /* Handle connects */ - &core_disconnect, /* remove peers on disconnects */ - NULL, /* Don't notify about all incoming messages */ - GNUNET_NO, /* For header only in notification */ - NULL, /* Don't notify about all outbound messages */ - GNUNET_NO, /* For header-only out notification */ - core_handlers); /* Register these handlers */ - - if (core_handle == NULL) - { - GNUNET_break (0); - GNUNET_SCHEDULER_shutdown (); - return; - } if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY", @@ -8366,8 +8898,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, GNUNET_SCHEDULER_shutdown (); return; } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value); - + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "APP_ANNOUNCE_TIME %llu ms\n", + app_announce_time.rel_value); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME", &id_announce_time)) @@ -8379,9 +8912,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, GNUNET_SCHEDULER_shutdown (); return; } - else - { - } if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT", @@ -8430,6 +8960,16 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, default_ttl = 64; } + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS", + &max_peers)) + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + _("%s service is lacking key configuration settings (%s). Using default (%u).\n"), + "mesh", "max peers", 1000); + max_peers = 1000; + } + if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL", &dht_replication_level)) @@ -8437,8 +8977,8 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("%s service is lacking key configuration settings (%s). Using default (%u).\n"), - "mesh", "dht replication level", 10); - dht_replication_level = 10; + "mesh", "dht replication level", 3); + dht_replication_level = 3; } tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO); @@ -8458,7 +8998,9 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, /* Scheduled the task to clean up when shutdown is called */ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL); - keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL); + keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, + &key_generation_cb, + (void *) c); GNUNET_free (keyfile); }