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>
35 uint32_t id GNUNET_PACKED;
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 */
52 GNUNET_CONNECTION_TransmitReadyNotify notify;
53 struct GNUNET_MESH_Tunnel *tunnel;
56 struct GNUNET_MESH_Tunnel
58 /* The other peer this tunnel leads to; just unicast for the moment! */
59 struct GNUNET_PeerIdentity peer;
63 /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
64 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
65 GNUNET_MESH_TunnelConnectHandler connect_handler;
68 struct GNUNET_MESH_Handle* handle;
70 /* The context of the receive-function. */
74 struct tunnel_list_element
76 struct GNUNET_MESH_Tunnel tunnel;
77 struct tunnel_list_element *next, *prev;
82 struct tunnel_list_element *head, *tail;
85 struct peer_list_element
87 struct GNUNET_PeerIdentity peer;
88 struct GNUNET_TRANSPORT_ATS_Information atsi;
89 struct peer_list_element *next, *prev;
94 struct peer_list_element *head, *tail;
97 struct GNUNET_MESH_Handle
99 struct GNUNET_CORE_Handle *core;
100 struct GNUNET_MESH_MessageHandler *handlers;
101 struct GNUNET_PeerIdentity myself;
102 unsigned int connected_to_core;
103 struct peer_list connected_peers;
104 struct tunnel_list established_tunnels;
105 struct tunnel_list pending_tunnels;
107 GNUNET_MESH_TunnelEndHandler *cleaner;
111 send_end_connect(void* cls,
112 const struct GNUNET_SCHEDULER_TaskContext* tc)
114 struct GNUNET_MESH_Tunnel* tunnel = cls;
116 tunnel->connect_handler(tunnel->handler_cls, NULL, NULL);
120 send_self_connect(void* cls,
121 const struct GNUNET_SCHEDULER_TaskContext* tc)
123 struct GNUNET_MESH_Tunnel* tunnel = cls;
125 tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL);
126 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
130 core_startup (void *cls,
131 struct GNUNET_CORE_Handle *core,
132 const struct GNUNET_PeerIdentity *my_identity,
133 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
135 struct GNUNET_MESH_Handle *handle = cls;
136 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
137 handle->connected_to_core = GNUNET_YES;
141 * Core calls this if we are connected to a new peer.
143 * If core tells us that we are connected to ourself, we ignore it. Otherwise, the
144 * peer is added to the connected_peers-list.
148 core_connect (void *cls,
149 const struct GNUNET_PeerIdentity *peer,
150 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
152 struct GNUNET_MESH_Handle *handle = cls;
153 /* Check for connect-to-self-message, which we ignore */
155 memcmp (peer, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
159 /* put the new peer into the list of connected peers */
160 struct peer_list_element *element =
161 GNUNET_malloc (sizeof (struct peer_list_element));
162 memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
163 memcpy (&element->atsi, atsi,
164 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
166 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
167 handle->connected_peers.tail,
168 handle->connected_peers.tail, element);
170 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
171 while (tunnel != NULL)
174 memcmp (&tunnel->tunnel.peer, peer,
175 sizeof (struct GNUNET_PeerIdentity)))
177 struct tunnel_list_element *next = tunnel->next;
178 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
179 handle->pending_tunnels.tail, tunnel);
180 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
181 handle->established_tunnels.tail,
182 handle->established_tunnels.tail,
184 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
186 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
190 tunnel = tunnel->next;
195 * Core calls this if we disconnect a peer
197 * Remove this peer from the list of connected peers
198 * Close all tunnels this peer belongs to
201 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
203 struct GNUNET_MESH_Handle *handle = cls;
205 struct peer_list_element *element = handle->connected_peers.head;
206 while (element != NULL)
209 memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
211 element = element->next;
215 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
216 handle->connected_peers.tail, element);
217 GNUNET_free (element);
220 struct tunnel_list_element *telement = handle->established_tunnels.head;
221 while (telement != NULL)
224 memcmp (&telement->tunnel.peer, peer,
225 sizeof (struct GNUNET_PeerIdentity)))
227 /* disconnect tunnels */
228 /* outbound tunnels */
229 if (telement->tunnel.connect_handler != NULL && NULL != telement->tunnel.disconnect_handler)
230 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
232 /* inbound tunnels */
233 else if (NULL != handle->cleaner)
234 handle->cleaner (handle->cls, &telement->tunnel,
235 &telement->tunnel.ctx);
237 struct tunnel_list_element *next = telement->next;
238 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
239 handle->established_tunnels.tail,
241 GNUNET_free (telement);
246 telement = telement->next;
252 * Receive a message from core.
255 core_receive (void *cls,
256 const struct GNUNET_PeerIdentity *other,
257 const struct GNUNET_MessageHeader *message,
258 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
260 struct GNUNET_MESH_Handle *handle = cls;
261 struct tunnel_message *tmessage = (struct tunnel_message *) message;
262 struct GNUNET_MessageHeader *rmessage =
263 (struct GNUNET_MessageHeader *) (tmessage + 1);
265 struct GNUNET_MESH_MessageHandler *handler;
267 for (handler = handle->handlers; handler->callback != NULL; handler++)
269 if ( (ntohs (rmessage->type) == handler->type)
270 && ( (handler->expected_size == 0)
271 || (handler->expected_size == ntohs (rmessage->size))) )
277 /* handler->callback handles this message */
279 /* If no handler was found, drop the message but keep the channel open */
280 if (handler->callback == NULL)
283 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
285 while (tunnel != NULL)
287 if (tunnel->tunnel.id.id == tmessage->id.id &&
289 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
290 sizeof (struct GNUNET_PeerIdentity)))
292 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
293 sizeof (struct GNUNET_PeerIdentity))))
295 tunnel = tunnel->next;
298 /* if no tunnel was found: create a new inbound tunnel */
301 tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
302 tunnel->tunnel.connect_handler = NULL;
303 tunnel->tunnel.disconnect_handler = NULL;
304 tunnel->tunnel.handler_cls = NULL;
305 tunnel->tunnel.ctx = NULL;
306 tunnel->tunnel.handle = handle;
307 memcpy (&tunnel->tunnel.peer, other,
308 sizeof (struct GNUNET_PeerIdentity));
309 memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
311 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
312 handle->established_tunnels.tail,
313 handle->established_tunnels.tail,
317 return handler->callback (handle->cls, &tunnel->tunnel,
318 &tunnel->tunnel.ctx, rmessage, atsi);
322 struct GNUNET_MESH_Tunnel *
323 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
324 struct GNUNET_TIME_Relative timeout,
325 unsigned int num_peers,
326 const struct GNUNET_PeerIdentity *peers,
327 GNUNET_MESH_TunnelConnectHandler
329 GNUNET_MESH_TunnelDisconnectHandler
330 disconnect_handler, void *handler_cls)
335 struct tunnel_list_element *tunnel =
336 GNUNET_malloc (sizeof (struct tunnel_list_element));
338 tunnel->tunnel.connect_handler = connect_handler;
339 tunnel->tunnel.disconnect_handler = disconnect_handler;
340 tunnel->tunnel.handler_cls = handler_cls;
341 tunnel->tunnel.ctx = NULL;
342 tunnel->tunnel.handle = handle;
343 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
344 sizeof (struct GNUNET_PeerIdentity));
345 memcpy (&tunnel->tunnel.id.target, peers,
346 sizeof (struct GNUNET_PeerIdentity));
347 tunnel->tunnel.id.id = current_id++;
348 memcpy (&tunnel->tunnel.peer, peers, sizeof(struct GNUNET_PeerIdentity));
350 struct peer_list_element *element = handle->connected_peers.head;
351 while (element != NULL)
354 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
356 element = element->next;
361 /* we are connected to this peer */
362 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
363 handle->established_tunnels.tail,
364 handle->established_tunnels.tail,
366 connect_handler (handler_cls, &element->peer, &element->atsi);
369 memcmp (peers, &handle->myself,
370 sizeof (struct GNUNET_PeerIdentity)))
372 /* we are the peer */
373 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
374 handle->established_tunnels.tail,
375 handle->established_tunnels.tail,
377 GNUNET_SCHEDULER_add_now(send_self_connect, tunnel);
381 /* we are not connected to this peer */
382 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
383 handle->pending_tunnels.tail,
384 handle->pending_tunnels.tail,
386 (void) GNUNET_CORE_peer_request_connect (handle->core,
392 return &tunnel->tunnel;
395 const struct GNUNET_PeerIdentity*
396 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
398 return &tunnel->peer;
402 core_notify(void* cls, size_t size, void* buf)
404 struct notify_cls *ncls = cls;
405 struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
406 struct tunnel_message* message = buf;
407 void* cbuf = (void*) &message[1];
408 GNUNET_assert(NULL != ncls->notify);
410 size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf);
414 if (0 == sent) return 0;
416 sent += sizeof(struct tunnel_message);
418 message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
419 message->hdr.size = htons(sent);
420 memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
424 struct GNUNET_MESH_TransmitHandle *
425 GNUNET_MESH_notify_transmit_ready (struct
435 GNUNET_CONNECTION_TransmitReadyNotify
436 notify, void *notify_cls)
438 struct notify_cls *cls = GNUNET_malloc(sizeof(struct notify_cls));
439 cls->notify_cls = notify_cls;
440 GNUNET_assert(NULL != notify);
441 cls->notify = notify;
442 cls->tunnel = tunnel;
443 GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
448 notify_size + sizeof(struct tunnel_message),
452 /* aborting is not implemented yet */
453 return (struct GNUNET_MESH_TransmitHandle*) 1;
457 struct GNUNET_MESH_Handle *
458 GNUNET_MESH_connect (const struct
459 GNUNET_CONFIGURATION_Handle
461 GNUNET_MESH_TunnelEndHandler
463 const struct GNUNET_MESH_MessageHandler *handlers)
465 struct GNUNET_MESH_Handle *ret =
466 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
468 ret->connected_to_core = GNUNET_NO;
469 ret->connected_peers.head = NULL;
470 ret->connected_peers.tail = NULL;
471 ret->cleaner = cleaner;
474 const struct GNUNET_MESH_MessageHandler *it;
475 unsigned int len = 1;
476 for (it = handlers; it->callback != NULL; it++)
482 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
483 memset(ret->handlers, 0, len * sizeof(struct GNUNET_MESH_MessageHandler));
484 memcpy (ret->handlers, handlers,
485 len * sizeof (struct GNUNET_MESH_MessageHandler));
487 const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
488 {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
492 ret->core = GNUNET_CORE_connect (cfg,
500 GNUNET_NO, NULL, GNUNET_NO, core_handlers);
505 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
507 GNUNET_free (handle->handlers);
508 GNUNET_CORE_disconnect (handle->core);
510 struct peer_list_element *element = handle->connected_peers.head;
511 while (element != NULL)
513 struct peer_list_element *next = element->next;
514 GNUNET_free (element);
518 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
519 while (tunnel != NULL)
521 struct tunnel_list_element *next = tunnel->next;
522 GNUNET_free (tunnel);
525 tunnel = handle->established_tunnels.head;;
526 while (tunnel != NULL)
528 struct tunnel_list_element *next = tunnel->next;
529 GNUNET_free (tunnel);
533 GNUNET_free (handle);
536 /* end of mesh_api.c */