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 disconnect events.
119 GNUNET_DV_DisconnectCallback disconnect_cb;
122 * Function to call on receiving messages events.
124 GNUNET_DV_MessageReceivedCallback message_cb;
127 * Head of messages to transmit.
129 struct GNUNET_DV_TransmitHandle *th_head;
132 * Tail of messages to transmit.
134 struct GNUNET_DV_TransmitHandle *th_tail;
137 * Mapping of peer identities to TransmitHandles to invoke
138 * upon successful transmission. The respective
139 * transmissions have already been done.
141 struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
152 * Disconnect and then reconnect to the DV service.
154 * @param sh service handle
157 reconnect (struct GNUNET_DV_ServiceHandle *sh);
161 * Gives a message from our queue to the DV service.
163 * @param cls handle to the dv service (struct GNUNET_DV_ServiceHandle)
164 * @param size how many bytes can we send
165 * @param buf where to copy the message to send
166 * @return how many bytes we copied to buf
169 transmit_pending (void *cls, size_t size, void *buf)
171 struct GNUNET_DV_ServiceHandle *sh = cls;
173 struct GNUNET_DV_TransmitHandle *th;
184 while ( (NULL != (th = sh->th_head)) &&
185 (size - ret >= (tsize = ntohs (th->msg->size)) ))
187 GNUNET_CONTAINER_DLL_remove (sh->th_head,
190 memcpy (&cbuf[ret], th->msg, tsize);
194 (void) GNUNET_CONTAINER_multihashmap_put (sh->send_callbacks,
195 &th->target.hashPubKey,
197 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
209 * Start sending messages from our queue to the service.
211 * @param sh service handle
214 start_transmit (struct GNUNET_DV_ServiceHandle *sh)
218 if (NULL == sh->th_head)
221 GNUNET_CLIENT_notify_transmit_ready (sh->client,
222 ntohs (sh->th_head->msg->size),
223 GNUNET_TIME_UNIT_FOREVER_REL,
225 &transmit_pending, sh);
230 * Closure for 'process_ack'.
237 const struct GNUNET_DV_AckMessage *ack;
240 * Our service handle.
242 struct GNUNET_DV_ServiceHandle *sh;
247 * We got an ACK. Check if it matches the given transmit handle, and if
248 * so call the continuation.
250 * @param cls the 'struct AckContext'
251 * @param key peer identity
252 * @param value the 'struct GNUNET_DV_TransmitHandle'
253 * @return GNUNET_OK if the ACK did not match (continue to iterate)
256 process_ack (void *cls,
257 const struct GNUNET_HashCode *key,
260 struct AckContext *ctx = cls;
261 struct GNUNET_DV_TransmitHandle *th = value;
263 if (th->uid != ntohl (ctx->ack->uid))
265 GNUNET_assert (GNUNET_YES ==
266 GNUNET_CONTAINER_multihashmap_remove (ctx->sh->send_callbacks,
269 /* FIXME: should distinguish between success and failure here... */
278 * Handles a message sent from the DV service to us.
279 * Parse it out and give it to the plugin.
281 * @param cls the handle to the DV API
282 * @param msg the message that was received
285 handle_message_receipt (void *cls,
286 const struct GNUNET_MessageHeader *msg)
288 struct GNUNET_DV_ServiceHandle *sh = cls;
289 const struct GNUNET_DV_ConnectMessage *cm;
290 const struct GNUNET_DV_DisconnectMessage *dm;
291 const struct GNUNET_DV_ReceivedMessage *rm;
292 const struct GNUNET_MessageHeader *payload;
293 const struct GNUNET_DV_AckMessage *ack;
294 struct AckContext ctx;
298 /* Connection closed */
302 switch (ntohs (msg->type))
304 case GNUNET_MESSAGE_TYPE_DV_CONNECT:
305 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ConnectMessage))
311 cm = (const struct GNUNET_DV_ConnectMessage *) msg;
312 sh->connect_cb (sh->cls,
314 ntohl (cm->distance));
316 case GNUNET_MESSAGE_TYPE_DV_DISCONNECT:
317 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DisconnectMessage))
323 dm = (const struct GNUNET_DV_DisconnectMessage *) msg;
324 sh->disconnect_cb (sh->cls,
327 case GNUNET_MESSAGE_TYPE_DV_RECV:
328 if (ntohs (msg->size) < sizeof (struct GNUNET_DV_ReceivedMessage) + sizeof (struct GNUNET_MessageHeader))
334 rm = (const struct GNUNET_DV_ReceivedMessage *) msg;
335 payload = (const struct GNUNET_MessageHeader *) &rm[1];
336 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ReceivedMessage) + ntohs (payload->size))
342 sh->message_cb (sh->cls,
344 ntohl (rm->distance),
347 case GNUNET_MESSAGE_TYPE_DV_SEND_ACK:
348 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_AckMessage))
354 ack = (const struct GNUNET_DV_AckMessage *) msg;
357 GNUNET_CONTAINER_multihashmap_get_multiple (sh->send_callbacks,
358 &ack->target.hashPubKey,
366 GNUNET_CLIENT_receive (sh->client,
367 &handle_message_receipt, sh,
368 GNUNET_TIME_UNIT_FOREVER_REL);
373 * Transmit the start message to the DV service.
375 * @param cls the 'struct GNUNET_DV_ServiceHandle'
376 * @param size number of bytes available in buf
377 * @param buf where to copy the message
378 * @return number of bytes written to buf
381 transmit_start (void *cls,
385 struct GNUNET_DV_ServiceHandle *sh = cls;
386 struct GNUNET_MessageHeader start_message;
395 GNUNET_assert (size >= sizeof (start_message));
396 start_message.size = htons (sizeof (struct GNUNET_MessageHeader));
397 start_message.type = htons (GNUNET_MESSAGE_TYPE_DV_START);
398 memcpy (buf, &start_message, sizeof (start_message));
399 GNUNET_CLIENT_receive (sh->client,
400 &handle_message_receipt, sh,
401 GNUNET_TIME_UNIT_FOREVER_REL);
403 return sizeof (start_message);
408 * We got disconnected from the service and thus all of the
409 * pending send callbacks will never be confirmed. Clean up.
411 * @param cls the 'struct GNUNET_DV_ServiceHandle'
412 * @param key a peer identity
413 * @param value a 'struct GNUNET_DV_TransmitHandle' to clean up
414 * @return GNUNET_OK (continue to iterate)
417 cleanup_send_cb (void *cls,
418 const struct GNUNET_HashCode *key,
421 struct GNUNET_DV_ServiceHandle *sh = cls;
422 struct GNUNET_DV_TransmitHandle *th = value;
424 GNUNET_assert (GNUNET_YES ==
425 GNUNET_CONTAINER_multihashmap_remove (sh->send_callbacks,
428 th->cb (th->cb_cls, GNUNET_SYSERR);
435 * Disconnect and then reconnect to the DV service.
437 * @param sh service handle
440 reconnect (struct GNUNET_DV_ServiceHandle *sh)
444 GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
447 if (NULL != sh->client)
449 GNUNET_CLIENT_disconnect (sh->client);
452 GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
455 sh->client = GNUNET_CLIENT_connect ("dv", sh->cfg);
456 if (NULL == sh->client)
461 sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
462 sizeof (struct GNUNET_MessageHeader),
463 GNUNET_TIME_UNIT_FOREVER_REL,
471 * Connect to the DV service.
473 * @param cfg configuration
474 * @param cls closure for callbacks
475 * @param connect_cb function to call on connects
476 * @param disconnect_cb function to call on disconnects
477 * @param message_cb function to call if we receive messages
478 * @return handle to access the service
480 struct GNUNET_DV_ServiceHandle *
481 GNUNET_DV_service_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
483 GNUNET_DV_ConnectCallback connect_cb,
484 GNUNET_DV_DisconnectCallback disconnect_cb,
485 GNUNET_DV_MessageReceivedCallback message_cb)
487 struct GNUNET_DV_ServiceHandle *sh;
489 sh = GNUNET_malloc (sizeof (struct GNUNET_DV_ServiceHandle));
492 sh->connect_cb = connect_cb;
493 sh->disconnect_cb = disconnect_cb;
494 sh->message_cb = message_cb;
495 sh->send_callbacks = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
502 * Disconnect from DV service.
504 * @param sh service handle
507 GNUNET_DV_service_disconnect (struct GNUNET_DV_ServiceHandle *sh)
509 struct GNUNET_DV_TransmitHandle *pos;
515 GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
518 while (NULL != (pos = sh->th_head))
520 GNUNET_CONTAINER_DLL_remove (sh->th_head,
525 if (NULL != sh->client)
527 GNUNET_CLIENT_disconnect (sh->client);
530 GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
533 GNUNET_CONTAINER_multihashmap_destroy (sh->send_callbacks);
539 * Send a message via DV service.
541 * @param sh service handle
542 * @param target intended recpient
543 * @param msg message payload
544 * @param cb function to invoke when done
545 * @param cb_cls closure for 'cb'
546 * @return handle to cancel the operation
548 struct GNUNET_DV_TransmitHandle *
549 GNUNET_DV_send (struct GNUNET_DV_ServiceHandle *sh,
550 const struct GNUNET_PeerIdentity *target,
551 const struct GNUNET_MessageHeader *msg,
552 GNUNET_DV_MessageSentCallback cb,
555 struct GNUNET_DV_TransmitHandle *th;
556 struct GNUNET_DV_SendMessage *sm;
558 if (ntohs (msg->size) + sizeof (struct GNUNET_DV_SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
563 th = GNUNET_malloc (sizeof (struct GNUNET_DV_TransmitHandle) +
564 sizeof (struct GNUNET_DV_SendMessage) +
567 th->target = *target;
570 th->msg = (const struct GNUNET_MessageHeader *) &th[1];
571 sm = (struct GNUNET_DV_SendMessage *) &th[1];
572 sm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_SEND);
573 sm->header.size = htons (sizeof (struct GNUNET_DV_SendMessage) +
575 if (0 == sh->uid_gen)
577 th->uid = sh->uid_gen;
578 sm->uid = htonl (sh->uid_gen++);
579 /* use memcpy here as 'target' may not be sufficiently aligned */
580 memcpy (&sm->target, target, sizeof (struct GNUNET_PeerIdentity));
581 memcpy (&sm[1], msg, ntohs (msg->size));
582 GNUNET_CONTAINER_DLL_insert (sh->th_head,
591 * Abort send operation (naturally, the message may have
592 * already been transmitted; this only stops the 'cb'
593 * from being called again).
595 * @param th send operation to cancel
598 GNUNET_DV_send_cancel (struct GNUNET_DV_TransmitHandle *th)
600 struct GNUNET_DV_ServiceHandle *sh = th->sh;
603 ret = GNUNET_CONTAINER_multihashmap_remove (sh->send_callbacks,
604 &th->target.hashPubKey,
606 if (GNUNET_YES != ret)
607 GNUNET_CONTAINER_DLL_remove (sh->th_head,
614 /* end of dv_api.c */