2 This file is part of GNUnet.
3 (C) 2009--2013 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.
23 * @brief library to access the DV service
24 * @author Christian Grothoff
25 * @author Nathan Evans
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dv_service.h"
30 #include "gnunet_protocols.h"
32 #include "gnunet_transport_plugin.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "dv-api",__VA_ARGS__)
38 * Information we track for each peer.
44 * Handle for a send operation.
46 struct GNUNET_DV_TransmitHandle
51 struct GNUNET_DV_TransmitHandle *next;
56 struct GNUNET_DV_TransmitHandle *prev;
59 * Handle to the service.
61 struct GNUNET_DV_ServiceHandle *sh;
64 * Function to call upon completion.
66 GNUNET_DV_MessageSentCallback cb;
74 * The actual message (allocated at the end of this struct).
76 const struct GNUNET_MessageHeader *msg;
79 * Destination for the message.
81 struct ConnectedPeer *target;
84 * UID of our message, if any.
92 * Information we track for each peer.
98 * Identity of the peer.
100 struct GNUNET_PeerIdentity pid;
103 * Head of DLL of transmission handles where we need
104 * to invoke a continuation when we are informed about
105 * successful transmission. The respective request
106 * has already been sent to the DV service.
108 struct GNUNET_DV_TransmitHandle *head;
111 * Tail of DLL of transmission handles where we need
112 * to invoke a continuation when we are informed about
113 * successful transmission. The respective request
114 * has already been sent to the DV service.
116 struct GNUNET_DV_TransmitHandle *tail;
122 * Handle to the DV service.
124 struct GNUNET_DV_ServiceHandle
128 * Connection to DV service.
130 struct GNUNET_CLIENT_Connection *client;
133 * Active request for transmission to DV service.
135 struct GNUNET_CLIENT_TransmitHandle *th;
140 const struct GNUNET_CONFIGURATION_Handle *cfg;
143 * Closure for the callbacks.
148 * Function to call on connect events.
150 GNUNET_DV_ConnectCallback connect_cb;
153 * Function to call on distance change events.
155 GNUNET_DV_DistanceChangedCallback distance_cb;
158 * Function to call on disconnect events.
160 GNUNET_DV_DisconnectCallback disconnect_cb;
163 * Function to call on receiving messages events.
165 GNUNET_DV_MessageReceivedCallback message_cb;
168 * Head of messages to transmit.
170 struct GNUNET_DV_TransmitHandle *th_head;
173 * Tail of messages to transmit.
175 struct GNUNET_DV_TransmitHandle *th_tail;
178 * Information tracked per connected peer. Maps peer
179 * identities to `struct ConnectedPeer` entries.
181 struct GNUNET_CONTAINER_MultiPeerMap *peers;
192 * Disconnect and then reconnect to the DV service.
194 * @param sh service handle
197 reconnect (struct GNUNET_DV_ServiceHandle *sh);
201 * Gives a message from our queue to the DV service.
203 * @param cls handle to the dv service (`struct GNUNET_DV_ServiceHandle`)
204 * @param size how many bytes can we send
205 * @param buf where to copy the message to send
206 * @return how many bytes we copied to @a buf
209 transmit_pending (void *cls, size_t size, void *buf)
211 struct GNUNET_DV_ServiceHandle *sh = cls;
213 struct GNUNET_DV_TransmitHandle *th;
224 while ( (NULL != (th = sh->th_head)) &&
225 (size - ret >= (tsize = ntohs (th->msg->size)) ))
227 GNUNET_CONTAINER_DLL_remove (sh->th_head,
230 memcpy (&cbuf[ret], th->msg, tsize);
231 LOG (GNUNET_ERROR_TYPE_DEBUG,
232 "Passing %u bytes of type %u to DV service\n",
234 ntohs (th->msg->type));
239 GNUNET_CONTAINER_DLL_insert (th->target->head,
253 * Start sending messages from our queue to the service.
255 * @param sh service handle
258 start_transmit (struct GNUNET_DV_ServiceHandle *sh)
262 if (NULL == sh->th_head)
265 GNUNET_CLIENT_notify_transmit_ready (sh->client,
266 ntohs (sh->th_head->msg->size),
267 GNUNET_TIME_UNIT_FOREVER_REL,
269 &transmit_pending, sh);
274 * We got disconnected from the service and thus all of the
275 * pending send callbacks will never be confirmed. Clean up.
277 * @param cls the 'struct GNUNET_DV_ServiceHandle'
278 * @param key a peer identity
279 * @param value a `struct ConnectedPeer` to clean up
280 * @return #GNUNET_OK (continue to iterate)
283 cleanup_send_cb (void *cls,
284 const struct GNUNET_PeerIdentity *key,
287 struct GNUNET_DV_ServiceHandle *sh = cls;
288 struct ConnectedPeer *peer = value;
289 struct GNUNET_DV_TransmitHandle *th;
291 GNUNET_assert (GNUNET_YES ==
292 GNUNET_CONTAINER_multipeermap_remove (sh->peers,
295 sh->disconnect_cb (sh->cls,
297 while (NULL != (th = peer->head))
299 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, th);
300 th->cb (th->cb_cls, GNUNET_SYSERR);
309 * Handles a message sent from the DV service to us.
310 * Parse it out and give it to the plugin.
312 * @param cls the handle to the DV API
313 * @param msg the message that was received
316 handle_message_receipt (void *cls,
317 const struct GNUNET_MessageHeader *msg)
319 struct GNUNET_DV_ServiceHandle *sh = cls;
320 const struct GNUNET_DV_ConnectMessage *cm;
321 const struct GNUNET_DV_DistanceUpdateMessage *dum;
322 const struct GNUNET_DV_DisconnectMessage *dm;
323 const struct GNUNET_DV_ReceivedMessage *rm;
324 const struct GNUNET_MessageHeader *payload;
325 const struct GNUNET_DV_AckMessage *ack;
326 struct GNUNET_DV_TransmitHandle *th;
327 struct GNUNET_DV_TransmitHandle *tn;
328 struct ConnectedPeer *peer;
332 /* Connection closed */
336 LOG (GNUNET_ERROR_TYPE_DEBUG,
337 "Received message of type %u with %u bytes from DV service\n",
338 (unsigned int) ntohs (msg->type),
339 (unsigned int) ntohs (msg->size));
340 switch (ntohs (msg->type))
342 case GNUNET_MESSAGE_TYPE_DV_CONNECT:
343 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ConnectMessage))
349 cm = (const struct GNUNET_DV_ConnectMessage *) msg;
350 peer = GNUNET_CONTAINER_multipeermap_get (sh->peers,
358 peer = GNUNET_new (struct ConnectedPeer);
359 peer->pid = cm->peer;
360 GNUNET_assert (GNUNET_OK ==
361 GNUNET_CONTAINER_multipeermap_put (sh->peers,
364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
365 sh->connect_cb (sh->cls,
367 ntohl (cm->distance),
368 (enum GNUNET_ATS_Network_Type) ntohl (cm->network));
370 case GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED:
371 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DistanceUpdateMessage))
377 dum = (const struct GNUNET_DV_DistanceUpdateMessage *) msg;
378 sh->distance_cb (sh->cls,
380 ntohl (dum->distance),
381 (enum GNUNET_ATS_Network_Type) ntohl (dum->network));
383 case GNUNET_MESSAGE_TYPE_DV_DISCONNECT:
384 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DisconnectMessage))
390 dm = (const struct GNUNET_DV_DisconnectMessage *) msg;
391 peer = GNUNET_CONTAINER_multipeermap_get (sh->peers,
400 while (NULL != (th = tn))
403 if (peer == th->target)
405 GNUNET_CONTAINER_DLL_remove (sh->th_head,
408 th->cb (th->cb_cls, GNUNET_SYSERR);
412 cleanup_send_cb (sh, &dm->peer, peer);
414 case GNUNET_MESSAGE_TYPE_DV_RECV:
415 if (ntohs (msg->size) < sizeof (struct GNUNET_DV_ReceivedMessage) + sizeof (struct GNUNET_MessageHeader))
421 rm = (const struct GNUNET_DV_ReceivedMessage *) msg;
422 payload = (const struct GNUNET_MessageHeader *) &rm[1];
423 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ReceivedMessage) + ntohs (payload->size))
430 GNUNET_CONTAINER_multipeermap_get (sh->peers,
437 sh->message_cb (sh->cls,
439 ntohl (rm->distance),
442 case GNUNET_MESSAGE_TYPE_DV_SEND_ACK:
443 case GNUNET_MESSAGE_TYPE_DV_SEND_NACK:
444 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_AckMessage))
450 ack = (const struct GNUNET_DV_AckMessage *) msg;
451 peer = GNUNET_CONTAINER_multipeermap_get (sh->peers,
454 break; /* this happens, just ignore */
455 for (th = peer->head; NULL != th; th = th->next)
457 if (th->uid != ntohl (ack->uid))
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Matched ACK for message to peer %s\n",
461 GNUNET_i2s (&ack->target));
462 GNUNET_CONTAINER_DLL_remove (peer->head,
466 (ntohs (ack->header.type) == GNUNET_MESSAGE_TYPE_DV_SEND_ACK)
477 LOG (GNUNET_ERROR_TYPE_DEBUG,
478 "Received message, continuing receive loop for %p\n",
480 GNUNET_CLIENT_receive (sh->client,
481 &handle_message_receipt, sh,
482 GNUNET_TIME_UNIT_FOREVER_REL);
487 * Transmit the start message to the DV service.
489 * @param cls the `struct GNUNET_DV_ServiceHandle *`
490 * @param size number of bytes available in buf
491 * @param buf where to copy the message
492 * @return number of bytes written to buf
495 transmit_start (void *cls,
499 struct GNUNET_DV_ServiceHandle *sh = cls;
500 struct GNUNET_MessageHeader start_message;
509 GNUNET_assert (size >= sizeof (start_message));
510 start_message.size = htons (sizeof (struct GNUNET_MessageHeader));
511 start_message.type = htons (GNUNET_MESSAGE_TYPE_DV_START);
512 memcpy (buf, &start_message, sizeof (start_message));
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "Transmitting START request, starting receive loop for %p\n",
516 GNUNET_CLIENT_receive (sh->client,
517 &handle_message_receipt, sh,
518 GNUNET_TIME_UNIT_FOREVER_REL);
520 return sizeof (start_message);
525 * Disconnect and then reconnect to the DV service.
527 * @param sh service handle
530 reconnect (struct GNUNET_DV_ServiceHandle *sh)
534 GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
537 LOG (GNUNET_ERROR_TYPE_DEBUG,
538 "Disconnecting from DV service at %p\n",
540 if (NULL != sh->client)
542 GNUNET_CLIENT_disconnect (sh->client);
545 GNUNET_CONTAINER_multipeermap_iterate (sh->peers,
548 LOG (GNUNET_ERROR_TYPE_DEBUG,
549 "Connecting to DV service\n");
550 sh->client = GNUNET_CLIENT_connect ("dv", sh->cfg);
551 if (NULL == sh->client)
556 sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
557 sizeof (struct GNUNET_MessageHeader),
558 GNUNET_TIME_UNIT_FOREVER_REL,
566 * Connect to the DV service.
568 * @param cfg configuration
569 * @param cls closure for callbacks
570 * @param connect_cb function to call on connects
571 * @param distance_cb function to call if distances change
572 * @param disconnect_cb function to call on disconnects
573 * @param message_cb function to call if we receive messages
574 * @return handle to access the service
576 struct GNUNET_DV_ServiceHandle *
577 GNUNET_DV_service_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
579 GNUNET_DV_ConnectCallback connect_cb,
580 GNUNET_DV_DistanceChangedCallback distance_cb,
581 GNUNET_DV_DisconnectCallback disconnect_cb,
582 GNUNET_DV_MessageReceivedCallback message_cb)
584 struct GNUNET_DV_ServiceHandle *sh;
586 sh = GNUNET_new (struct GNUNET_DV_ServiceHandle);
589 sh->connect_cb = connect_cb;
590 sh->distance_cb = distance_cb;
591 sh->disconnect_cb = disconnect_cb;
592 sh->message_cb = message_cb;
593 sh->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
600 * Disconnect from DV service.
602 * @param sh service handle
605 GNUNET_DV_service_disconnect (struct GNUNET_DV_ServiceHandle *sh)
607 struct GNUNET_DV_TransmitHandle *pos;
613 GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
616 while (NULL != (pos = sh->th_head))
618 GNUNET_CONTAINER_DLL_remove (sh->th_head,
623 if (NULL != sh->client)
625 GNUNET_CLIENT_disconnect (sh->client);
628 GNUNET_CONTAINER_multipeermap_iterate (sh->peers,
631 GNUNET_CONTAINER_multipeermap_destroy (sh->peers);
637 * Send a message via DV service.
639 * @param sh service handle
640 * @param target intended recpient
641 * @param msg message payload
642 * @param cb function to invoke when done
643 * @param cb_cls closure for @a cb
644 * @return handle to cancel the operation
646 struct GNUNET_DV_TransmitHandle *
647 GNUNET_DV_send (struct GNUNET_DV_ServiceHandle *sh,
648 const struct GNUNET_PeerIdentity *target,
649 const struct GNUNET_MessageHeader *msg,
650 GNUNET_DV_MessageSentCallback cb,
653 struct GNUNET_DV_TransmitHandle *th;
654 struct GNUNET_DV_SendMessage *sm;
655 struct ConnectedPeer *peer;
657 if (ntohs (msg->size) + sizeof (struct GNUNET_DV_SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
662 LOG (GNUNET_ERROR_TYPE_DEBUG,
663 "Asked to send %u bytes of type %u to %s via %p\n",
664 (unsigned int) ntohs (msg->size),
665 (unsigned int) ntohs (msg->type),
668 peer = GNUNET_CONTAINER_multipeermap_get (sh->peers,
675 th = GNUNET_malloc (sizeof (struct GNUNET_DV_TransmitHandle) +
676 sizeof (struct GNUNET_DV_SendMessage) +
682 th->msg = (const struct GNUNET_MessageHeader *) &th[1];
683 sm = (struct GNUNET_DV_SendMessage *) &th[1];
684 sm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_SEND);
685 sm->header.size = htons (sizeof (struct GNUNET_DV_SendMessage) +
687 if (0 == sh->uid_gen)
689 th->uid = sh->uid_gen;
690 sm->uid = htonl (sh->uid_gen++);
691 /* use memcpy here as 'target' may not be sufficiently aligned */
692 memcpy (&sm->target, target, sizeof (struct GNUNET_PeerIdentity));
693 memcpy (&sm[1], msg, ntohs (msg->size));
694 GNUNET_CONTAINER_DLL_insert (sh->th_head,
703 * Abort send operation (naturally, the message may have
704 * already been transmitted; this only stops the 'cb'
705 * from being called again).
707 * @param th send operation to cancel
710 GNUNET_DV_send_cancel (struct GNUNET_DV_TransmitHandle *th)
712 struct GNUNET_DV_ServiceHandle *sh = th->sh;
715 GNUNET_CONTAINER_DLL_remove (th->target->head,
719 GNUNET_CONTAINER_DLL_remove (sh->th_head,
726 /* end of dv_api.c */