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 * Handle for a send operation.
40 struct GNUNET_DV_TransmitHandle
45 struct GNUNET_DV_TransmitHandle *next;
50 struct GNUNET_DV_TransmitHandle *prev;
53 * Handle to the service.
55 struct GNUNET_DV_ServiceHandle *sh;
58 * Function to call upon completion.
60 GNUNET_DV_MessageSentCallback cb;
68 * The actual message (allocated at the end of this struct).
70 const struct GNUNET_MessageHeader *msg;
73 * Destination for the message.
75 struct GNUNET_PeerIdentity target;
78 * UID of our message, if any.
86 * Handle to the DV service.
88 struct GNUNET_DV_ServiceHandle
92 * Connection to DV service.
94 struct GNUNET_CLIENT_Connection *client;
97 * Active request for transmission to DV service.
99 struct GNUNET_CLIENT_TransmitHandle *th;
104 const struct GNUNET_CONFIGURATION_Handle *cfg;
107 * Closure for the callbacks.
112 * Function to call on connect events.
114 GNUNET_DV_ConnectCallback connect_cb;
117 * Function to call on distance change events.
119 GNUNET_DV_DistanceChangedCallback distance_cb;
122 * Function to call on disconnect events.
124 GNUNET_DV_DisconnectCallback disconnect_cb;
127 * Function to call on receiving messages events.
129 GNUNET_DV_MessageReceivedCallback message_cb;
132 * Head of messages to transmit.
134 struct GNUNET_DV_TransmitHandle *th_head;
137 * Tail of messages to transmit.
139 struct GNUNET_DV_TransmitHandle *th_tail;
142 * Mapping of peer identities to TransmitHandles to invoke
143 * upon successful transmission. The respective
144 * transmissions have already been done.
146 struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
157 * Disconnect and then reconnect to the DV service.
159 * @param sh service handle
162 reconnect (struct GNUNET_DV_ServiceHandle *sh);
166 * Gives a message from our queue to the DV service.
168 * @param cls handle to the dv service (struct GNUNET_DV_ServiceHandle)
169 * @param size how many bytes can we send
170 * @param buf where to copy the message to send
171 * @return how many bytes we copied to buf
174 transmit_pending (void *cls, size_t size, void *buf)
176 struct GNUNET_DV_ServiceHandle *sh = cls;
178 struct GNUNET_DV_TransmitHandle *th;
189 while ( (NULL != (th = sh->th_head)) &&
190 (size - ret >= (tsize = ntohs (th->msg->size)) ))
192 GNUNET_CONTAINER_DLL_remove (sh->th_head,
195 memcpy (&cbuf[ret], th->msg, tsize);
199 (void) GNUNET_CONTAINER_multihashmap_put (sh->send_callbacks,
200 &th->target.hashPubKey,
202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
214 * Start sending messages from our queue to the service.
216 * @param sh service handle
219 start_transmit (struct GNUNET_DV_ServiceHandle *sh)
223 if (NULL == sh->th_head)
226 GNUNET_CLIENT_notify_transmit_ready (sh->client,
227 ntohs (sh->th_head->msg->size),
228 GNUNET_TIME_UNIT_FOREVER_REL,
230 &transmit_pending, sh);
235 * Closure for 'process_ack'.
242 const struct GNUNET_DV_AckMessage *ack;
245 * Our service handle.
247 struct GNUNET_DV_ServiceHandle *sh;
252 * We got an ACK. Check if it matches the given transmit handle, and if
253 * so call the continuation.
255 * @param cls the 'struct AckContext'
256 * @param key peer identity
257 * @param value the 'struct GNUNET_DV_TransmitHandle'
258 * @return GNUNET_OK if the ACK did not match (continue to iterate)
261 process_ack (void *cls,
262 const struct GNUNET_HashCode *key,
265 struct AckContext *ctx = cls;
266 struct GNUNET_DV_TransmitHandle *th = value;
268 if (th->uid != ntohl (ctx->ack->uid))
270 GNUNET_assert (GNUNET_YES ==
271 GNUNET_CONTAINER_multihashmap_remove (ctx->sh->send_callbacks,
275 (ntohs (ctx->ack->header.type) == GNUNET_MESSAGE_TYPE_DV_SEND_ACK)
284 * Handles a message sent from the DV service to us.
285 * Parse it out and give it to the plugin.
287 * @param cls the handle to the DV API
288 * @param msg the message that was received
291 handle_message_receipt (void *cls,
292 const struct GNUNET_MessageHeader *msg)
294 struct GNUNET_DV_ServiceHandle *sh = cls;
295 const struct GNUNET_DV_ConnectMessage *cm;
296 const struct GNUNET_DV_DistanceUpdateMessage *dum;
297 const struct GNUNET_DV_DisconnectMessage *dm;
298 const struct GNUNET_DV_ReceivedMessage *rm;
299 const struct GNUNET_MessageHeader *payload;
300 const struct GNUNET_DV_AckMessage *ack;
301 struct AckContext ctx;
305 /* Connection closed */
309 switch (ntohs (msg->type))
311 case GNUNET_MESSAGE_TYPE_DV_CONNECT:
312 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ConnectMessage))
318 cm = (const struct GNUNET_DV_ConnectMessage *) msg;
319 sh->connect_cb (sh->cls,
321 ntohl (cm->distance));
323 case GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED:
324 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DistanceUpdateMessage))
330 dum = (const struct GNUNET_DV_DistanceUpdateMessage *) msg;
331 sh->distance_cb (sh->cls,
333 ntohl (dum->distance));
335 case GNUNET_MESSAGE_TYPE_DV_DISCONNECT:
336 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DisconnectMessage))
342 dm = (const struct GNUNET_DV_DisconnectMessage *) msg;
343 sh->disconnect_cb (sh->cls,
346 case GNUNET_MESSAGE_TYPE_DV_RECV:
347 if (ntohs (msg->size) < sizeof (struct GNUNET_DV_ReceivedMessage) + sizeof (struct GNUNET_MessageHeader))
353 rm = (const struct GNUNET_DV_ReceivedMessage *) msg;
354 payload = (const struct GNUNET_MessageHeader *) &rm[1];
355 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ReceivedMessage) + ntohs (payload->size))
361 sh->message_cb (sh->cls,
363 ntohl (rm->distance),
366 case GNUNET_MESSAGE_TYPE_DV_SEND_ACK:
367 case GNUNET_MESSAGE_TYPE_DV_SEND_NACK:
368 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_AckMessage))
374 ack = (const struct GNUNET_DV_AckMessage *) msg;
377 GNUNET_CONTAINER_multihashmap_get_multiple (sh->send_callbacks,
378 &ack->target.hashPubKey,
386 GNUNET_CLIENT_receive (sh->client,
387 &handle_message_receipt, sh,
388 GNUNET_TIME_UNIT_FOREVER_REL);
393 * Transmit the start message to the DV service.
395 * @param cls the 'struct GNUNET_DV_ServiceHandle'
396 * @param size number of bytes available in buf
397 * @param buf where to copy the message
398 * @return number of bytes written to buf
401 transmit_start (void *cls,
405 struct GNUNET_DV_ServiceHandle *sh = cls;
406 struct GNUNET_MessageHeader start_message;
415 GNUNET_assert (size >= sizeof (start_message));
416 start_message.size = htons (sizeof (struct GNUNET_MessageHeader));
417 start_message.type = htons (GNUNET_MESSAGE_TYPE_DV_START);
418 memcpy (buf, &start_message, sizeof (start_message));
419 GNUNET_CLIENT_receive (sh->client,
420 &handle_message_receipt, sh,
421 GNUNET_TIME_UNIT_FOREVER_REL);
423 return sizeof (start_message);
428 * We got disconnected from the service and thus all of the
429 * pending send callbacks will never be confirmed. Clean up.
431 * @param cls the 'struct GNUNET_DV_ServiceHandle'
432 * @param key a peer identity
433 * @param value a 'struct GNUNET_DV_TransmitHandle' to clean up
434 * @return GNUNET_OK (continue to iterate)
437 cleanup_send_cb (void *cls,
438 const struct GNUNET_HashCode *key,
441 struct GNUNET_DV_ServiceHandle *sh = cls;
442 struct GNUNET_DV_TransmitHandle *th = value;
444 GNUNET_assert (GNUNET_YES ==
445 GNUNET_CONTAINER_multihashmap_remove (sh->send_callbacks,
448 th->cb (th->cb_cls, GNUNET_SYSERR);
455 * Disconnect and then reconnect to the DV service.
457 * @param sh service handle
460 reconnect (struct GNUNET_DV_ServiceHandle *sh)
464 GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
467 if (NULL != sh->client)
469 GNUNET_CLIENT_disconnect (sh->client);
472 GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
475 sh->client = GNUNET_CLIENT_connect ("dv", sh->cfg);
476 if (NULL == sh->client)
481 sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
482 sizeof (struct GNUNET_MessageHeader),
483 GNUNET_TIME_UNIT_FOREVER_REL,
491 * Connect to the DV service.
493 * @param cfg configuration
494 * @param cls closure for callbacks
495 * @param connect_cb function to call on connects
496 * @param distance_cb function to call if distances change
497 * @param disconnect_cb function to call on disconnects
498 * @param message_cb function to call if we receive messages
499 * @return handle to access the service
501 struct GNUNET_DV_ServiceHandle *
502 GNUNET_DV_service_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
504 GNUNET_DV_ConnectCallback connect_cb,
505 GNUNET_DV_DistanceChangedCallback distance_cb,
506 GNUNET_DV_DisconnectCallback disconnect_cb,
507 GNUNET_DV_MessageReceivedCallback message_cb)
509 struct GNUNET_DV_ServiceHandle *sh;
511 sh = GNUNET_malloc (sizeof (struct GNUNET_DV_ServiceHandle));
514 sh->connect_cb = connect_cb;
515 sh->distance_cb = distance_cb;
516 sh->disconnect_cb = disconnect_cb;
517 sh->message_cb = message_cb;
518 sh->send_callbacks = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
525 * Disconnect from DV service.
527 * @param sh service handle
530 GNUNET_DV_service_disconnect (struct GNUNET_DV_ServiceHandle *sh)
532 struct GNUNET_DV_TransmitHandle *pos;
538 GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
541 while (NULL != (pos = sh->th_head))
543 GNUNET_CONTAINER_DLL_remove (sh->th_head,
548 if (NULL != sh->client)
550 GNUNET_CLIENT_disconnect (sh->client);
553 GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
556 GNUNET_CONTAINER_multihashmap_destroy (sh->send_callbacks);
562 * Send a message via DV service.
564 * @param sh service handle
565 * @param target intended recpient
566 * @param msg message payload
567 * @param cb function to invoke when done
568 * @param cb_cls closure for 'cb'
569 * @return handle to cancel the operation
571 struct GNUNET_DV_TransmitHandle *
572 GNUNET_DV_send (struct GNUNET_DV_ServiceHandle *sh,
573 const struct GNUNET_PeerIdentity *target,
574 const struct GNUNET_MessageHeader *msg,
575 GNUNET_DV_MessageSentCallback cb,
578 struct GNUNET_DV_TransmitHandle *th;
579 struct GNUNET_DV_SendMessage *sm;
581 if (ntohs (msg->size) + sizeof (struct GNUNET_DV_SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
586 th = GNUNET_malloc (sizeof (struct GNUNET_DV_TransmitHandle) +
587 sizeof (struct GNUNET_DV_SendMessage) +
590 th->target = *target;
593 th->msg = (const struct GNUNET_MessageHeader *) &th[1];
594 sm = (struct GNUNET_DV_SendMessage *) &th[1];
595 sm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_SEND);
596 sm->header.size = htons (sizeof (struct GNUNET_DV_SendMessage) +
598 if (0 == sh->uid_gen)
600 th->uid = sh->uid_gen;
601 sm->uid = htonl (sh->uid_gen++);
602 /* use memcpy here as 'target' may not be sufficiently aligned */
603 memcpy (&sm->target, target, sizeof (struct GNUNET_PeerIdentity));
604 memcpy (&sm[1], msg, ntohs (msg->size));
605 GNUNET_CONTAINER_DLL_insert (sh->th_head,
614 * Abort send operation (naturally, the message may have
615 * already been transmitted; this only stops the 'cb'
616 * from being called again).
618 * @param th send operation to cancel
621 GNUNET_DV_send_cancel (struct GNUNET_DV_TransmitHandle *th)
623 struct GNUNET_DV_ServiceHandle *sh = th->sh;
626 ret = GNUNET_CONTAINER_multihashmap_remove (sh->send_callbacks,
627 &th->target.hashPubKey,
629 if (GNUNET_YES != ret)
630 GNUNET_CONTAINER_DLL_remove (sh->th_head,
637 /* end of dv_api.c */