From 88590a7fd73dde7fcede837cf631a8020fefc9de Mon Sep 17 00:00:00 2001 From: Bart Polot Date: Fri, 2 Sep 2011 12:38:18 +0000 Subject: [PATCH] Added tunnel destroy, changed message types in client <-> service protocol --- src/include/gnunet_protocols.h | 25 ++-------- src/mesh/gnunet-service-mesh.c | 10 ++-- src/mesh/mesh_api_new.c | 87 ++++++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 36 deletions(-) diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h index 5f3ab25ce..6d4f7265b 100644 --- a/src/include/gnunet_protocols.h +++ b/src/include/gnunet_protocols.h @@ -876,7 +876,6 @@ extern "C" /** * Transport data back in the mesh (end->origin) - * (not sure if this is the right way, should be some other solution) */ #define GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN 262 @@ -908,38 +907,22 @@ extern "C" /** * Ask the mesh service to add a peer to an existing tunnel */ -#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD 275 +#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD 275 /** * Ask the mesh service to remove a peer from a tunnel */ -#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL 276 +#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL 276 /** * Ask the mesh service to add a peer offering a service to an existing tunnel */ -#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE 277 - -/** - * Ask the mesh service to cancel a peer connection request - */ -#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL 278 - -/** - * Notify a mesh client that a peer has connected to a tunnel - */ -#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED 279 - -/** - * Notify a mesh client that a peer has disconnected from a tunnel - */ -#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED 280 - +#define GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE 277 /** * 640kb should be enough for everybody */ -#define GNUNET_MESSAGE_TYPE_MESH_RESERVE_END 288 +#define GNUNET_MESSAGE_TYPE_MESH_RESERVE_END 288 diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c index 0f8f2732f..c396303a8 100644 --- a/src/mesh/gnunet-service-mesh.c +++ b/src/mesh/gnunet-service-mesh.c @@ -1682,7 +1682,7 @@ handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer, return GNUNET_OK; } peer_info->state = MESH_PEER_READY; - pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED); + 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); GNUNET_PEER_resolve (peer_info->id, &pc.peer); @@ -2494,7 +2494,7 @@ handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client, get_peer_info (&pc.peer), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl)); - pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED); + pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD); pc.tunnel_id = htonl (t->local_tid); GNUNET_SERVER_notification_context_unicast (nc, client, NULL, GNUNET_NO); } @@ -2679,13 +2679,13 @@ static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = { GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY, sizeof (struct GNUNET_MESH_TunnelMessage)}, {&handle_local_connect_add, NULL, - GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD, + GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD, sizeof (struct GNUNET_MESH_PeerControl)}, {&handle_local_connect_del, NULL, - GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL, + GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL, sizeof (struct GNUNET_MESH_PeerControl)}, {&handle_local_connect_by_type, NULL, - GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE, + GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE, sizeof (struct GNUNET_MESH_ConnectPeerByType)}, {&handle_local_unicast, NULL, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0}, diff --git a/src/mesh/mesh_api_new.c b/src/mesh/mesh_api_new.c index 34e8f4747..b639df812 100644 --- a/src/mesh/mesh_api_new.c +++ b/src/mesh/mesh_api_new.c @@ -325,10 +325,10 @@ retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid) /** - * Get the tunnel handler for the tunnel specified by id from the given handle + * Create a new tunnel and insert it in the tunnel list of the mesh handle * @param h Mesh handle - * @param tid ID of the wanted tunnel - * @return handle to the required tunnel or NULL if not found + * @param tid desired tid of the tunnel, 0 to assign one automatically + * @return handle to the created tunnel */ static struct GNUNET_MESH_Tunnel * create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid) @@ -343,6 +343,8 @@ create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid) } else { + /* FIXME keep double numbering? + * client numbers from 0x8... anx service from 0xB... ? */ t->tid = tid; h->next_tid = tid + 1; } @@ -352,6 +354,42 @@ create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid) } +/** + * Get the tunnel handler for the tunnel specified by id from the given handle + * @param h Mesh handle + * @param tid ID of the wanted tunnel + * @return handle to the required tunnel or NULL if not found + */ +static void +destroy_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid) +{ + struct GNUNET_MESH_Tunnel *t; + struct GNUNET_PeerIdentity pi; + unsigned int i; + + t = retrieve_tunnel(h, tid); + if (NULL == t) + { + GNUNET_break (0); + return; + } + GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t); + for (i = 0; i < t->npeers; i++) + { + GNUNET_PEER_resolve(t->peers[i]->id, &pi); + t->disconnect_handler(t->cls, &pi); + GNUNET_PEER_change_rc(t->peers[i]->id, -1); + GNUNET_free (t->peers[i]); + } + h->cleaner(h->cls, t, NULL); /* FIXME ctx? */ + if (0 != t->owner) + GNUNET_PEER_change_rc(t->owner, -1); + GNUNET_free(t->peers); + GNUNET_free(t); + return; +} + + /** * Get the peer descriptor for the peer with id from the given tunnel * @param t Tunnel handle @@ -524,7 +562,7 @@ reconnect (struct GNUNET_MESH_Handle *h) struct GNUNET_MESH_PeerControl msg; msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD); msg.tunnel_id = htonl (t->tid); GNUNET_PEER_resolve (t->peers[i]->id, &msg.peer); send_packet (t->mesh, &msg.header); @@ -562,10 +600,35 @@ process_tunnel_create (struct GNUNET_MESH_Handle *h, t->cls = h->cls; t->mesh = h; t->tid = tid; + GNUNET_CONTAINER_DLL_insert(h->tunnels_head, h->tunnels_tail, t); return; } +/** + * Process the tunnel destroy notification and free associated resources + * + * @param h The mesh handle + * @param msg A message with the details of the tunnel being destroyed + */ +static void +process_tunnel_destroy (struct GNUNET_MESH_Handle *h, + const struct GNUNET_MESH_TunnelMessage *msg) +{ + struct GNUNET_MESH_Tunnel *t; + MESH_TunnelNumber tid; + + tid = ntohl (msg->tunnel_id); + t = retrieve_tunnel(h, tid); + + t->cls = h->cls; + t->mesh = h; + t->tid = tid; + GNUNET_CONTAINER_DLL_insert(h->tunnels_head, h->tunnels_tail, t); + return; +} + + /** * Process the new peer event and notify the upper level of it * @@ -606,7 +669,7 @@ process_peer_event (struct GNUNET_MESH_Handle *h, p = add_peer_to_tunnel (t, &msg->peer); atsi.type = 0; atsi.value = 0; - if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED == msg->header.type) + if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD == msg->header.type) { if (NULL != t->connect_handler) { @@ -726,9 +789,13 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg) case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE: process_tunnel_create (h, (struct GNUNET_MESH_TunnelMessage *) msg); break; + /* Notify of a tunnel disconnection */ + case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY: + process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg); + break; /* Notify of a new peer or a peer disconnect in the tunnel */ - case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED: - case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED: + case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD: + case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL: process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg); break; /* Notify of a new data packet in the tunnel */ @@ -1084,7 +1151,7 @@ GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel, add_peer_to_tunnel (tunnel, peer); msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD); msg.tunnel_id = htonl (tunnel->tid); msg.peer = *peer; send_packet (tunnel->mesh, &msg.header); @@ -1130,7 +1197,7 @@ GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel, GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1); msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL); msg.tunnel_id = htonl (tunnel->tid); memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity)); send_packet (tunnel->mesh, &msg.header); @@ -1155,7 +1222,7 @@ GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel, /* FIXME: add a new api call disconnect by type? */ msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType)); - msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE); msg.tunnel_id = htonl (tunnel->tid); msg.type = htonl (app_type); send_packet (tunnel->mesh, &msg.header); -- 2.25.1