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,
270 (ntohs (ctx->ack->header.type) == GNUNET_MESSAGE_TYPE_DV_SEND_ACK)
279 * Handles a message sent from the DV service to us.
280 * Parse it out and give it to the plugin.
282 * @param cls the handle to the DV API
283 * @param msg the message that was received
286 handle_message_receipt (void *cls,
287 const struct GNUNET_MessageHeader *msg)
289 struct GNUNET_DV_ServiceHandle *sh = cls;
290 const struct GNUNET_DV_ConnectMessage *cm;
291 const struct GNUNET_DV_DisconnectMessage *dm;
292 const struct GNUNET_DV_ReceivedMessage *rm;
293 const struct GNUNET_MessageHeader *payload;
294 const struct GNUNET_DV_AckMessage *ack;
295 struct AckContext ctx;
299 /* Connection closed */
303 switch (ntohs (msg->type))
305 case GNUNET_MESSAGE_TYPE_DV_CONNECT:
306 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ConnectMessage))
312 cm = (const struct GNUNET_DV_ConnectMessage *) msg;
313 sh->connect_cb (sh->cls,
315 ntohl (cm->distance));
317 case GNUNET_MESSAGE_TYPE_DV_DISCONNECT:
318 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DisconnectMessage))
324 dm = (const struct GNUNET_DV_DisconnectMessage *) msg;
325 sh->disconnect_cb (sh->cls,
328 case GNUNET_MESSAGE_TYPE_DV_RECV:
329 if (ntohs (msg->size) < sizeof (struct GNUNET_DV_ReceivedMessage) + sizeof (struct GNUNET_MessageHeader))
335 rm = (const struct GNUNET_DV_ReceivedMessage *) msg;
336 payload = (const struct GNUNET_MessageHeader *) &rm[1];
337 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ReceivedMessage) + ntohs (payload->size))
343 sh->message_cb (sh->cls,
345 ntohl (rm->distance),
348 case GNUNET_MESSAGE_TYPE_DV_SEND_ACK:
349 case GNUNET_MESSAGE_TYPE_DV_SEND_NACK:
350 if (ntohs (msg->size) != sizeof (struct GNUNET_DV_AckMessage))
356 ack = (const struct GNUNET_DV_AckMessage *) msg;
359 GNUNET_CONTAINER_multihashmap_get_multiple (sh->send_callbacks,
360 &ack->target.hashPubKey,
364 case GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED:
371 GNUNET_CLIENT_receive (sh->client,
372 &handle_message_receipt, sh,
373 GNUNET_TIME_UNIT_FOREVER_REL);
378 * Transmit the start message to the DV service.
380 * @param cls the 'struct GNUNET_DV_ServiceHandle'
381 * @param size number of bytes available in buf
382 * @param buf where to copy the message
383 * @return number of bytes written to buf
386 transmit_start (void *cls,
390 struct GNUNET_DV_ServiceHandle *sh = cls;
391 struct GNUNET_MessageHeader start_message;
400 GNUNET_assert (size >= sizeof (start_message));
401 start_message.size = htons (sizeof (struct GNUNET_MessageHeader));
402 start_message.type = htons (GNUNET_MESSAGE_TYPE_DV_START);
403 memcpy (buf, &start_message, sizeof (start_message));
404 GNUNET_CLIENT_receive (sh->client,
405 &handle_message_receipt, sh,
406 GNUNET_TIME_UNIT_FOREVER_REL);
408 return sizeof (start_message);
413 * We got disconnected from the service and thus all of the
414 * pending send callbacks will never be confirmed. Clean up.
416 * @param cls the 'struct GNUNET_DV_ServiceHandle'
417 * @param key a peer identity
418 * @param value a 'struct GNUNET_DV_TransmitHandle' to clean up
419 * @return GNUNET_OK (continue to iterate)
422 cleanup_send_cb (void *cls,
423 const struct GNUNET_HashCode *key,
426 struct GNUNET_DV_ServiceHandle *sh = cls;
427 struct GNUNET_DV_TransmitHandle *th = value;
429 GNUNET_assert (GNUNET_YES ==
430 GNUNET_CONTAINER_multihashmap_remove (sh->send_callbacks,
433 th->cb (th->cb_cls, GNUNET_SYSERR);
440 * Disconnect and then reconnect to the DV service.
442 * @param sh service handle
445 reconnect (struct GNUNET_DV_ServiceHandle *sh)
449 GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
452 if (NULL != sh->client)
454 GNUNET_CLIENT_disconnect (sh->client);
457 GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
460 sh->client = GNUNET_CLIENT_connect ("dv", sh->cfg);
461 if (NULL == sh->client)
466 sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
467 sizeof (struct GNUNET_MessageHeader),
468 GNUNET_TIME_UNIT_FOREVER_REL,
476 * Connect to the DV service.
478 * @param cfg configuration
479 * @param cls closure for callbacks
480 * @param connect_cb function to call on connects
481 * @param disconnect_cb function to call on disconnects
482 * @param message_cb function to call if we receive messages
483 * @return handle to access the service
485 struct GNUNET_DV_ServiceHandle *
486 GNUNET_DV_service_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
488 GNUNET_DV_ConnectCallback connect_cb,
489 GNUNET_DV_DisconnectCallback disconnect_cb,
490 GNUNET_DV_MessageReceivedCallback message_cb)
492 struct GNUNET_DV_ServiceHandle *sh;
494 sh = GNUNET_malloc (sizeof (struct GNUNET_DV_ServiceHandle));
497 sh->connect_cb = connect_cb;
498 sh->disconnect_cb = disconnect_cb;
499 sh->message_cb = message_cb;
500 sh->send_callbacks = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
507 * Disconnect from DV service.
509 * @param sh service handle
512 GNUNET_DV_service_disconnect (struct GNUNET_DV_ServiceHandle *sh)
514 struct GNUNET_DV_TransmitHandle *pos;
520 GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
523 while (NULL != (pos = sh->th_head))
525 GNUNET_CONTAINER_DLL_remove (sh->th_head,
530 if (NULL != sh->client)
532 GNUNET_CLIENT_disconnect (sh->client);
535 GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
538 GNUNET_CONTAINER_multihashmap_destroy (sh->send_callbacks);
544 * Send a message via DV service.
546 * @param sh service handle
547 * @param target intended recpient
548 * @param msg message payload
549 * @param cb function to invoke when done
550 * @param cb_cls closure for 'cb'
551 * @return handle to cancel the operation
553 struct GNUNET_DV_TransmitHandle *
554 GNUNET_DV_send (struct GNUNET_DV_ServiceHandle *sh,
555 const struct GNUNET_PeerIdentity *target,
556 const struct GNUNET_MessageHeader *msg,
557 GNUNET_DV_MessageSentCallback cb,
560 struct GNUNET_DV_TransmitHandle *th;
561 struct GNUNET_DV_SendMessage *sm;
563 if (ntohs (msg->size) + sizeof (struct GNUNET_DV_SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
568 th = GNUNET_malloc (sizeof (struct GNUNET_DV_TransmitHandle) +
569 sizeof (struct GNUNET_DV_SendMessage) +
572 th->target = *target;
575 th->msg = (const struct GNUNET_MessageHeader *) &th[1];
576 sm = (struct GNUNET_DV_SendMessage *) &th[1];
577 sm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_SEND);
578 sm->header.size = htons (sizeof (struct GNUNET_DV_SendMessage) +
580 if (0 == sh->uid_gen)
582 th->uid = sh->uid_gen;
583 sm->uid = htonl (sh->uid_gen++);
584 /* use memcpy here as 'target' may not be sufficiently aligned */
585 memcpy (&sm->target, target, sizeof (struct GNUNET_PeerIdentity));
586 memcpy (&sm[1], msg, ntohs (msg->size));
587 GNUNET_CONTAINER_DLL_insert (sh->th_head,
596 * Abort send operation (naturally, the message may have
597 * already been transmitted; this only stops the 'cb'
598 * from being called again).
600 * @param th send operation to cancel
603 GNUNET_DV_send_cancel (struct GNUNET_DV_TransmitHandle *th)
605 struct GNUNET_DV_ServiceHandle *sh = th->sh;
608 ret = GNUNET_CONTAINER_multihashmap_remove (sh->send_callbacks,
609 &th->target.hashPubKey,
611 if (GNUNET_YES != ret)
612 GNUNET_CONTAINER_DLL_remove (sh->th_head,
619 /* end of dv_api.c */