2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/mesh_api.c
23 * @brief mesh service; API for the Mesh. This is used to talk to arbitrary peers
24 * as of 2011-01Jan-06 this is a mockup.
25 * @author Philipp Tölke
28 #include <gnunet_constants.h>
29 #include <gnunet_mesh_service.h>
30 #include <gnunet_core_service.h>
31 #include <gnunet_container_lib.h>
36 struct GNUNET_PeerIdentity initiator;
37 struct GNUNET_PeerIdentity target;
40 static uint32_t current_id = 0;
44 struct GNUNET_MessageHeader hdr;
46 /* followed by another GNUNET_MessageHeader */
49 struct GNUNET_MESH_Tunnel
51 /* The other peer this tunnel leads to; just unicast for the moment! */
52 struct GNUNET_PeerIdentity peer;
57 GNUNET_CONNECTION_TransmitReadyNotify notify;
59 /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
60 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
61 GNUNET_MESH_TunnelConnectHandler connect_handler;
64 struct GNUNET_MESH_Handle* handle;
66 /* The context of the receive-function. */
70 struct tunnel_list_element
72 struct GNUNET_MESH_Tunnel tunnel;
73 struct tunnel_list_element *next, *prev;
78 struct tunnel_list_element *head, *tail;
81 struct peer_list_element
83 struct GNUNET_PeerIdentity peer;
84 struct GNUNET_TRANSPORT_ATS_Information atsi;
85 struct peer_list_element *next, *prev;
90 struct peer_list_element *head, *tail;
93 struct GNUNET_MESH_Handle
95 struct GNUNET_CORE_Handle *core;
96 struct GNUNET_MESH_MessageHandler *handlers;
97 struct GNUNET_PeerIdentity myself;
98 unsigned int connected_to_core;
99 struct peer_list connected_peers;
100 struct tunnel_list established_tunnels;
101 struct tunnel_list pending_tunnels;
103 GNUNET_MESH_TunnelEndHandler *cleaner;
107 core_startup (void *cls,
108 struct GNUNET_CORE_Handle *core,
109 const struct GNUNET_PeerIdentity *my_identity,
110 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
112 struct GNUNET_MESH_Handle *handle = cls;
113 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
114 handle->connected_to_core = GNUNET_YES;
118 * Core calls this if we are connected to a new peer.
120 * If core tells us that we are connected to ourself, we ignore it. Otherwise, the
121 * peer is added to the connected_peers-list.
125 core_connect (void *cls,
126 const struct GNUNET_PeerIdentity *peer,
127 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
129 struct GNUNET_MESH_Handle *handle = cls;
130 /* Check for connect-to-self-message, which we ignore */
132 memcmp (peer, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
136 /* put the new peer into the list of connected peers */
137 struct peer_list_element *element =
138 GNUNET_malloc (sizeof (struct peer_list_element));
139 memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
140 memcpy (&element->atsi, atsi,
141 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
143 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
144 handle->connected_peers.tail,
145 handle->connected_peers.tail, element);
147 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
148 while (tunnel != NULL)
151 memcmp (&tunnel->tunnel.peer, peer,
152 sizeof (struct GNUNET_PeerIdentity)))
154 struct tunnel_list_element *next = tunnel->next;
155 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
156 handle->pending_tunnels.tail, tunnel);
157 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
158 handle->established_tunnels.tail,
159 handle->established_tunnels.tail,
161 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
166 tunnel = tunnel->next;
171 * Core calls this if we disconnect a peer
173 * Remove this peer from the list of connected peers
174 * Close all tunnels this peer belongs to
177 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
179 struct GNUNET_MESH_Handle *handle = cls;
181 struct peer_list_element *element = handle->connected_peers.head;
182 while (element != NULL)
185 memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
187 element = element->next;
191 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
192 handle->connected_peers.tail, element);
193 GNUNET_free (element);
196 struct tunnel_list_element *telement = handle->established_tunnels.head;
197 while (telement != NULL)
200 memcmp (&telement->tunnel.peer, peer,
201 sizeof (struct GNUNET_PeerIdentity)))
203 /* disconnect tunnels */
204 /* outbound tunnels */
205 if (telement->tunnel.connect_handler != NULL)
206 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
208 /* inbound tunnels */
210 handle->cleaner (handle->cls, &telement->tunnel,
211 &telement->tunnel.ctx);
213 struct tunnel_list_element *next = telement->next;
214 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
215 handle->established_tunnels.tail,
217 GNUNET_free (telement);
222 telement = telement->next;
228 * Receive a message from core.
231 core_receive (void *cls,
232 const struct GNUNET_PeerIdentity *other,
233 const struct GNUNET_MessageHeader *message,
234 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
236 struct GNUNET_MESH_Handle *handle = cls;
237 struct tunnel_message *tmessage = (struct tunnel_message *) message;
238 struct GNUNET_MessageHeader *rmessage =
239 (struct GNUNET_MessageHeader *) (tmessage + 1);
241 struct GNUNET_MESH_MessageHandler *handler;
243 for (handler = handle->handlers; handler != NULL; handler++)
245 if (ntohs (rmessage->type) == handler->type
246 && (handler->expected_size == 0
247 || handler->expected_size == ntohs (rmessage->size)))
253 /* handler->callback handles this message */
255 /* If no handler was found, drop the message but keep the channel open */
259 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
261 while (tunnel != NULL)
263 if (tunnel->tunnel.id.id == tmessage->id.id &&
265 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
266 sizeof (struct GNUNET_PeerIdentity)))
268 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
269 sizeof (struct GNUNET_PeerIdentity))))
271 tunnel = tunnel->next;
274 /* if no tunnel was found: create a new inbound tunnel */
277 tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
278 tunnel->tunnel.connect_handler = NULL;
279 tunnel->tunnel.disconnect_handler = NULL;
280 tunnel->tunnel.handler_cls = NULL;
281 tunnel->tunnel.ctx = NULL;
282 tunnel->tunnel.handle = handle;
283 memcpy (&tunnel->tunnel.peer, other,
284 sizeof (struct GNUNET_PeerIdentity));
285 memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
287 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
288 handle->established_tunnels.tail,
289 handle->established_tunnels.tail,
293 return handler->callback (handle->cls, &tunnel->tunnel,
294 &tunnel->tunnel.ctx, rmessage, atsi);
298 struct GNUNET_MESH_Tunnel *
299 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
300 struct GNUNET_TIME_Relative timeout,
301 unsigned int num_peers,
302 const struct GNUNET_PeerIdentity *peers,
303 GNUNET_MESH_TunnelConnectHandler
305 GNUNET_MESH_TunnelDisconnectHandler
306 disconnect_handler, void *handler_cls)
311 struct tunnel_list_element *tunnel =
312 GNUNET_malloc (sizeof (struct tunnel_list_element));
314 tunnel->tunnel.connect_handler = connect_handler;
315 tunnel->tunnel.disconnect_handler = disconnect_handler;
316 tunnel->tunnel.handler_cls = handler_cls;
317 tunnel->tunnel.ctx = NULL;
318 tunnel->tunnel.handle = handle;
319 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
320 sizeof (struct GNUNET_PeerIdentity));
321 memcpy (&tunnel->tunnel.id.target, peers,
322 sizeof (struct GNUNET_PeerIdentity));
323 tunnel->tunnel.id.id = current_id++;
325 struct peer_list_element *element = handle->connected_peers.head;
326 while (element != NULL)
329 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
331 element = element->next;
336 /* we are connected to this peer */
337 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
338 handle->established_tunnels.tail,
339 handle->established_tunnels.tail,
341 connect_handler (handler_cls, &element->peer, &element->atsi);
344 memcmp (peers, &handle->myself,
345 sizeof (struct GNUNET_PeerIdentity)))
347 /* we are the peer */
348 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
349 handle->established_tunnels.tail,
350 handle->established_tunnels.tail,
352 connect_handler (handler_cls, &handle->myself, NULL);
356 /* we are not connected to this peer */
357 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
358 handle->pending_tunnels.tail,
359 handle->pending_tunnels.tail,
363 return &tunnel->tunnel;
367 core_notify(void* cls, size_t size, void* buf)
369 struct GNUNET_MESH_Tunnel *tunnel = cls;
370 struct tunnel_message* message = buf;
371 void* cbuf = (void*)(message + 1);
373 size_t sent = tunnel->notify(tunnel->notify_cls, size - sizeof(struct tunnel_message), cbuf);
375 tunnel->notify = NULL;
376 tunnel->notify_cls = NULL;
378 sent += sizeof(struct tunnel_message);
380 message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
381 message->hdr.size = htons(sent);
382 memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
386 struct GNUNET_MESH_TransmitHandle *
387 GNUNET_MESH_notify_transmit_ready (struct
397 GNUNET_CONNECTION_TransmitReadyNotify
398 notify, void *notify_cls)
400 tunnel->notify_cls = notify_cls;
401 tunnel->notify = notify;
402 GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
406 notify_size + sizeof(struct tunnel_message),
410 /* aborting is not implemented yet */
411 return (struct GNUNET_MESH_TransmitHandle*)1;
415 struct GNUNET_MESH_Handle *
416 GNUNET_MESH_connect (const struct
417 GNUNET_CONFIGURATION_Handle
419 GNUNET_MESH_TunnelEndHandler
421 const struct GNUNET_MESH_MessageHandler *handlers)
423 struct GNUNET_MESH_Handle *ret =
424 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
426 ret->connected_to_core = GNUNET_NO;
427 ret->connected_peers.head = NULL;
428 ret->connected_peers.tail = NULL;
429 ret->cleaner = cleaner;
432 const struct GNUNET_MESH_MessageHandler *it;
433 unsigned int len = 1;
434 for (it = handlers; it->callback != NULL; it++)
440 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
441 memcpy (ret->handlers, handlers,
442 len * sizeof (struct GNUNET_MESH_MessageHandler));
444 const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
445 {core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
449 ret->core = GNUNET_CORE_connect (cfg,
457 GNUNET_NO, NULL, GNUNET_NO, core_handlers);
462 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
464 GNUNET_free (handle->handlers);
465 GNUNET_CORE_disconnect (handle->core);
467 struct peer_list_element *element = handle->connected_peers.head;
468 while (element != NULL)
470 struct peer_list_element *next = element->next;
471 GNUNET_free (element);
475 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
476 while (tunnel != NULL)
478 struct tunnel_list_element *next = tunnel->next;
479 GNUNET_free (tunnel);
482 tunnel = handle->established_tunnels.head;;
483 while (tunnel != NULL)
485 struct tunnel_list_element *next = tunnel->next;
486 GNUNET_free (tunnel);
490 GNUNET_free (handle);
493 /* end of core_api.c */