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 */
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 send_end_connect(void* cls,
108 const struct GNUNET_SCHEDULER_TaskContext* tc)
110 struct GNUNET_MESH_Tunnel* tunnel = cls;
112 tunnel->connect_handler(tunnel->handler_cls, NULL, NULL);
116 send_self_connect(void* cls,
117 const struct GNUNET_SCHEDULER_TaskContext* tc)
119 struct GNUNET_MESH_Tunnel* tunnel = cls;
121 tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL);
122 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
126 core_startup (void *cls,
127 struct GNUNET_CORE_Handle *core,
128 const struct GNUNET_PeerIdentity *my_identity,
129 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
131 struct GNUNET_MESH_Handle *handle = cls;
132 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
133 handle->connected_to_core = GNUNET_YES;
137 * Core calls this if we are connected to a new peer.
139 * If core tells us that we are connected to ourself, we ignore it. Otherwise, the
140 * peer is added to the connected_peers-list.
144 core_connect (void *cls,
145 const struct GNUNET_PeerIdentity *peer,
146 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
148 struct GNUNET_MESH_Handle *handle = cls;
149 /* Check for connect-to-self-message, which we ignore */
151 memcmp (peer, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
155 /* put the new peer into the list of connected peers */
156 struct peer_list_element *element =
157 GNUNET_malloc (sizeof (struct peer_list_element));
158 memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
159 memcpy (&element->atsi, atsi,
160 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
162 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
163 handle->connected_peers.tail,
164 handle->connected_peers.tail, element);
166 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
167 while (tunnel != NULL)
170 memcmp (&tunnel->tunnel.peer, peer,
171 sizeof (struct GNUNET_PeerIdentity)))
173 struct tunnel_list_element *next = tunnel->next;
174 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
175 handle->pending_tunnels.tail, tunnel);
176 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
177 handle->established_tunnels.tail,
178 handle->established_tunnels.tail,
180 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
182 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
186 tunnel = tunnel->next;
191 * Core calls this if we disconnect a peer
193 * Remove this peer from the list of connected peers
194 * Close all tunnels this peer belongs to
197 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
199 struct GNUNET_MESH_Handle *handle = cls;
201 struct peer_list_element *element = handle->connected_peers.head;
202 while (element != NULL)
205 memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
207 element = element->next;
211 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
212 handle->connected_peers.tail, element);
213 GNUNET_free (element);
216 struct tunnel_list_element *telement = handle->established_tunnels.head;
217 while (telement != NULL)
220 memcmp (&telement->tunnel.peer, peer,
221 sizeof (struct GNUNET_PeerIdentity)))
223 /* disconnect tunnels */
224 /* outbound tunnels */
225 if (telement->tunnel.connect_handler != NULL)
226 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
228 /* inbound tunnels */
230 handle->cleaner (handle->cls, &telement->tunnel,
231 &telement->tunnel.ctx);
233 struct tunnel_list_element *next = telement->next;
234 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
235 handle->established_tunnels.tail,
237 GNUNET_free (telement);
242 telement = telement->next;
248 * Receive a message from core.
251 core_receive (void *cls,
252 const struct GNUNET_PeerIdentity *other,
253 const struct GNUNET_MessageHeader *message,
254 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
256 struct GNUNET_MESH_Handle *handle = cls;
257 struct tunnel_message *tmessage = (struct tunnel_message *) message;
258 struct GNUNET_MessageHeader *rmessage =
259 (struct GNUNET_MessageHeader *) (tmessage + 1);
261 struct GNUNET_MESH_MessageHandler *handler;
263 for (handler = handle->handlers; handler != NULL; handler++)
265 if ( (ntohs (rmessage->type) == handler->type)
266 && ( (handler->expected_size == 0)
267 || (handler->expected_size == ntohs (rmessage->size))) )
273 /* handler->callback handles this message */
275 /* If no handler was found, drop the message but keep the channel open */
279 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
281 while (tunnel != NULL)
283 if (tunnel->tunnel.id.id == tmessage->id.id &&
285 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
286 sizeof (struct GNUNET_PeerIdentity)))
288 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
289 sizeof (struct GNUNET_PeerIdentity))))
291 tunnel = tunnel->next;
294 /* if no tunnel was found: create a new inbound tunnel */
297 tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
298 tunnel->tunnel.connect_handler = NULL;
299 tunnel->tunnel.disconnect_handler = NULL;
300 tunnel->tunnel.handler_cls = NULL;
301 tunnel->tunnel.ctx = NULL;
302 tunnel->tunnel.handle = handle;
303 memcpy (&tunnel->tunnel.peer, other,
304 sizeof (struct GNUNET_PeerIdentity));
305 memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
307 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
308 handle->established_tunnels.tail,
309 handle->established_tunnels.tail,
313 return handler->callback (handle->cls, &tunnel->tunnel,
314 &tunnel->tunnel.ctx, rmessage, atsi);
318 struct GNUNET_MESH_Tunnel *
319 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
320 struct GNUNET_TIME_Relative timeout,
321 unsigned int num_peers,
322 const struct GNUNET_PeerIdentity *peers,
323 GNUNET_MESH_TunnelConnectHandler
325 GNUNET_MESH_TunnelDisconnectHandler
326 disconnect_handler, void *handler_cls)
331 struct tunnel_list_element *tunnel =
332 GNUNET_malloc (sizeof (struct tunnel_list_element));
334 tunnel->tunnel.connect_handler = connect_handler;
335 tunnel->tunnel.disconnect_handler = disconnect_handler;
336 tunnel->tunnel.handler_cls = handler_cls;
337 tunnel->tunnel.ctx = NULL;
338 tunnel->tunnel.handle = handle;
339 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
340 sizeof (struct GNUNET_PeerIdentity));
341 memcpy (&tunnel->tunnel.id.target, peers,
342 sizeof (struct GNUNET_PeerIdentity));
343 tunnel->tunnel.id.id = current_id++;
345 struct peer_list_element *element = handle->connected_peers.head;
346 while (element != NULL)
349 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
351 element = element->next;
356 /* we are connected to this peer */
357 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
358 handle->established_tunnels.tail,
359 handle->established_tunnels.tail,
361 connect_handler (handler_cls, &element->peer, &element->atsi);
364 memcmp (peers, &handle->myself,
365 sizeof (struct GNUNET_PeerIdentity)))
367 /* we are the peer */
368 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
369 handle->established_tunnels.tail,
370 handle->established_tunnels.tail,
372 GNUNET_SCHEDULER_add_now(send_self_connect, tunnel);
376 /* we are not connected to this peer */
377 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
378 handle->pending_tunnels.tail,
379 handle->pending_tunnels.tail,
381 (void) GNUNET_CORE_peer_request_connect (handle->core,
387 return &tunnel->tunnel;
390 const struct GNUNET_PeerIdentity*
391 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
393 return &tunnel->peer;
397 core_notify(void* cls, size_t size, void* buf)
399 struct GNUNET_MESH_Tunnel *tunnel = cls;
400 struct tunnel_message* message = buf;
401 void* cbuf = (void*) &message[1];
403 size_t sent = tunnel->notify(tunnel->notify_cls, size - sizeof(struct tunnel_message), cbuf);
405 tunnel->notify = NULL;
406 tunnel->notify_cls = NULL;
408 sent += sizeof(struct tunnel_message);
410 message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
411 message->hdr.size = htons(sent);
412 memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
416 struct GNUNET_MESH_TransmitHandle *
417 GNUNET_MESH_notify_transmit_ready (struct
427 GNUNET_CONNECTION_TransmitReadyNotify
428 notify, void *notify_cls)
430 tunnel->notify_cls = notify_cls;
431 tunnel->notify = notify;
432 GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
436 notify_size + sizeof(struct tunnel_message),
440 /* aborting is not implemented yet */
441 return (struct GNUNET_MESH_TransmitHandle*) 1;
445 struct GNUNET_MESH_Handle *
446 GNUNET_MESH_connect (const struct
447 GNUNET_CONFIGURATION_Handle
449 GNUNET_MESH_TunnelEndHandler
451 const struct GNUNET_MESH_MessageHandler *handlers)
453 struct GNUNET_MESH_Handle *ret =
454 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
456 ret->connected_to_core = GNUNET_NO;
457 ret->connected_peers.head = NULL;
458 ret->connected_peers.tail = NULL;
459 ret->cleaner = cleaner;
462 const struct GNUNET_MESH_MessageHandler *it;
463 unsigned int len = 1;
464 for (it = handlers; it->callback != NULL; it++)
470 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
471 memcpy (ret->handlers, handlers,
472 len * sizeof (struct GNUNET_MESH_MessageHandler));
474 const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
475 {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
479 ret->core = GNUNET_CORE_connect (cfg,
487 GNUNET_NO, NULL, GNUNET_NO, core_handlers);
492 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
494 GNUNET_free (handle->handlers);
495 GNUNET_CORE_disconnect (handle->core);
497 struct peer_list_element *element = handle->connected_peers.head;
498 while (element != NULL)
500 struct peer_list_element *next = element->next;
501 GNUNET_free (element);
505 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
506 while (tunnel != NULL)
508 struct tunnel_list_element *next = tunnel->next;
509 GNUNET_free (tunnel);
512 tunnel = handle->established_tunnels.head;;
513 while (tunnel != NULL)
515 struct tunnel_list_element *next = tunnel->next;
516 GNUNET_free (tunnel);
520 GNUNET_free (handle);
523 /* end of mesh_api.c */