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 2, 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 Not Nathan Evans
28 #include "gnunet_bandwidth_lib.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_container_lib.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "gnunet_dv_service.h"
41 struct PendingMessages
44 * Linked list of pending messages
46 struct PendingMessages *next;
49 * Message that is pending
51 struct GNUNET_DV_SendMessage *msg;
54 * Timeout for this message
56 struct GNUNET_TIME_Absolute timeout;
61 * Handle for the service.
63 struct GNUNET_DV_Handle
68 struct GNUNET_SCHEDULER_Handle *sched;
71 * Configuration to use.
73 const struct GNUNET_CONFIGURATION_Handle *cfg;
76 * Socket (if available).
78 struct GNUNET_CLIENT_Connection *client;
81 * Currently pending transmission request.
83 struct GNUNET_CLIENT_TransmitHandle *th;
86 * List of the currently pending messages for the DV service.
88 struct PendingMessages *pending_list;
91 * Message we are currently sending.
93 struct PendingMessages *current;
96 * Kill off the connection and any pending messages.
101 * Handler for messages we receive from the DV service
103 GNUNET_DV_MessageReceivedHandler receive_handler;
106 * Closure for the receive handler
114 * Try to (re)connect to the dv service.
116 * @return GNUNET_YES on success, GNUNET_NO on failure.
119 try_connect (struct GNUNET_DV_Handle *ret)
121 if (ret->client != NULL)
123 ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
124 if (ret->client != NULL)
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 _("Failed to connect to the dv service!\n"));
133 static void process_pending_message(struct GNUNET_DV_Handle *handle);
136 * Send complete, schedule next
139 finish (struct GNUNET_DV_Handle *handle, int code)
141 struct PendingMessages *pos = handle->current;
142 handle->current = NULL;
143 process_pending_message (handle);
150 transmit_pending (void *cls, size_t size, void *buf)
152 struct GNUNET_DV_Handle *handle = cls;
158 finish(handle, GNUNET_SYSERR);
165 if (handle->current != NULL)
167 tsize = ntohs(handle->current->msg->header.size);
170 memcpy(buf, handle->current->msg, tsize);
182 * Try to send messages from list of messages to send
184 static void process_pending_message(struct GNUNET_DV_Handle *handle)
186 struct GNUNET_TIME_Relative timeout;
188 if (handle->current != NULL)
189 return; /* action already pending */
190 if (GNUNET_YES != try_connect (handle))
192 finish (handle, GNUNET_SYSERR);
196 /* schedule next action */
197 handle->current = handle->pending_list;
198 if (NULL == handle->current)
200 if (handle->do_destroy)
202 handle->do_destroy = GNUNET_NO;
203 //GNUNET_DV_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
207 handle->pending_list = handle->pending_list->next;
208 handle->current->next = NULL;
210 timeout = GNUNET_TIME_absolute_get_remaining (handle->current->timeout);
212 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
213 ntohs(handle->current->msg->msgbuf_size),
216 &transmit_pending, handle)))
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Failed to transmit request to dv service.\n");
222 finish (handle, GNUNET_SYSERR);
227 * Add a pending message to the linked list
229 * @param handle handle to the specified DV api
230 * @param msg the message to add to the list
232 static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
234 struct PendingMessages *new_message;
235 struct PendingMessages *pos;
236 struct PendingMessages *last;
238 new_message = GNUNET_malloc(sizeof(struct PendingMessages));
239 new_message->msg = msg;
241 if (handle->pending_list != NULL)
243 pos = handle->pending_list;
249 new_message->next = last->next; /* Should always be null */
250 last->next = new_message;
254 new_message->next = handle->pending_list; /* Will always be null */
255 handle->pending_list = new_message;
258 process_pending_message(handle);
264 void handle_message_receipt (void *cls,
265 const struct GNUNET_MessageHeader * msg)
267 struct GNUNET_DV_Handle *handle = cls;
268 struct GNUNET_DV_MessageReceived *received_msg;
269 size_t packed_msg_len;
270 size_t sender_address_len;
271 char *sender_address;
276 return; /* Connection closed? */
279 GNUNET_assert(ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
281 if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
284 received_msg = (struct GNUNET_DV_MessageReceived *)msg;
285 packed_msg_len = ntohs(received_msg->msg_len);
286 sender_address_len = ntohs(received_msg->sender_address_len);
287 GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + packed_msg_len + sender_address_len));
289 sender_address = GNUNET_malloc(sender_address_len);
290 memcpy(sender_address, &received_msg[1], sender_address_len);
291 packed_msg = GNUNET_malloc(packed_msg_len);
292 memcpy(packed_msg, &received_msg[1 + sender_address_len], packed_msg_len);
294 handle->receive_handler(handle->receive_cls,
295 &received_msg->sender,
298 ntohl(received_msg->distance),
302 GNUNET_free(sender_address);
304 GNUNET_CLIENT_receive (handle->client,
305 &handle_message_receipt,
306 handle, GNUNET_TIME_UNIT_FOREVER_REL);
310 * Send a message from the plugin to the DV service indicating that
311 * a message should be sent via DV to some peer.
313 * @param dv_handle the handle to the DV api
314 * @param target the final target of the message
315 * @param msgbuf the msg(s) to send
316 * @param msgbuf_size the size of msgbuf
317 * @param priority priority to pass on to core when sending the message
318 * @param timeout how long can this message be delayed (pass through to core)
319 * @param addr the address of this peer (internally known to DV)
320 * @param addrlen the length of the peer address
323 int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
324 const struct GNUNET_PeerIdentity *target,
327 unsigned int priority,
328 struct GNUNET_TIME_Relative timeout,
332 struct GNUNET_DV_SendMessage *msg;
334 msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendMessage) + msgbuf_size + addrlen);
335 msg->header.size = htons(sizeof(struct GNUNET_DV_SendMessage) + msgbuf_size + addrlen);
336 msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
337 memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
338 msg->msgbuf = GNUNET_malloc(msgbuf_size);
339 memcpy(msg->msgbuf, msgbuf, msgbuf_size);
340 msg->msgbuf_size = htons(msgbuf_size);
341 msg->priority = htonl(priority);
342 msg->timeout = timeout;
343 msg->addrlen = htons(addrlen);
344 memcpy(&msg[1], addr, addrlen);
346 add_pending(dv_handle, msg);
352 * Connect to the DV service
354 * @param sched the scheduler to use
355 * @param cfg the configuration to use
356 * @param receive_handler method call when on receipt from the service
357 * @param receive_handler_cls closure for receive_handler
359 * @return handle to the DV service
361 struct GNUNET_DV_Handle *
362 GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
363 const struct GNUNET_CONFIGURATION_Handle *cfg,
364 GNUNET_DV_MessageReceivedHandler receive_handler,
365 void *receive_handler_cls)
367 struct GNUNET_DV_Handle *handle;
369 handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
372 handle->sched = sched;
373 handle->pending_list = NULL;
374 handle->current = NULL;
375 handle->do_destroy = GNUNET_NO;
377 handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
378 handle->receive_handler = receive_handler;
379 handle->receive_cls = receive_handler_cls;
381 if (handle->client == NULL)
384 GNUNET_CLIENT_receive (handle->client,
385 &handle_message_receipt,
386 handle, GNUNET_TIME_UNIT_FOREVER_REL);
392 * Disconnect from the DV service
394 * @param handle the current handle to the service to disconnect
396 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
398 struct PendingMessages *pos;
400 GNUNET_assert(handle != NULL);
402 if (handle->th != NULL) /* We have a live transmit request in the Aether */
404 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
407 if (handle->current != NULL) /* We are trying to send something now, clean it up */
408 GNUNET_free(handle->current);
409 while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
411 handle->pending_list = pos->next;
414 if (handle->client != NULL) /* Finally, disconnect from the service */
416 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
417 handle->client = NULL;
420 GNUNET_free (handle);
423 /* end of dv_api.c */