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;
157 finish(handle, GNUNET_SYSERR);
166 * Try to send messages from list of messages to send
168 static void process_pending_message(struct GNUNET_DV_Handle *handle)
170 struct GNUNET_TIME_Relative timeout;
172 if (handle->current != NULL)
173 return; /* action already pending */
174 if (GNUNET_YES != try_connect (handle))
176 finish (handle, GNUNET_SYSERR);
180 /* schedule next action */
181 handle->current = handle->pending_list;
182 if (NULL == handle->current)
184 if (handle->do_destroy)
186 handle->do_destroy = GNUNET_NO;
187 //GNUNET_DV_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
191 handle->pending_list = handle->pending_list->next;
192 handle->current->next = NULL;
194 timeout = GNUNET_TIME_absolute_get_remaining (handle->current->timeout);
196 (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
197 ntohs(handle->current->msg->msgbuf_size),
200 &transmit_pending, handle)))
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Failed to transmit request to dv service.\n");
206 finish (handle, GNUNET_SYSERR);
211 * Add a pending message to the linked list
213 * @param handle handle to the specified DV api
214 * @param msg the message to add to the list
216 static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
218 struct PendingMessages *new_message;
219 struct PendingMessages *pos;
220 struct PendingMessages *last;
222 new_message = GNUNET_malloc(sizeof(struct PendingMessages));
223 new_message->msg = msg;
225 if (handle->pending_list != NULL)
227 pos = handle->pending_list;
233 new_message->next = last->next; /* Should always be null */
234 last->next = new_message;
238 new_message->next = handle->pending_list; /* Will always be null */
239 handle->pending_list = new_message;
242 process_pending_message(handle);
248 void handle_message_receipt (void *cls,
249 const struct GNUNET_MessageHeader * msg)
251 struct GNUNET_DV_Handle *handle = cls;
252 struct GNUNET_DV_MessageReceived *received_msg;
253 char *sender_address;
255 GNUNET_assert(ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE);
257 if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
260 received_msg = (struct GNUNET_DV_MessageReceived *)msg;
261 GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + ntohs(received_msg->msg->size) + ntohs(received_msg->sender_address_len)));
263 sender_address = GNUNET_malloc(ntohs(received_msg->sender_address_len));
264 sender_address = memcpy(sender_address, &received_msg[1], ntohs(received_msg->sender_address_len));
266 handle->receive_handler(handle->receive_cls,
267 &received_msg->sender,
269 ntohl(received_msg->distance),
271 ntohs(received_msg->sender_address_len));
273 GNUNET_free(sender_address);
275 GNUNET_CLIENT_receive (handle->client,
276 &handle_message_receipt,
277 handle, GNUNET_TIME_UNIT_FOREVER_REL);
281 * Send a message from the plugin to the DV service indicating that
282 * a message should be sent via DV to some peer.
284 * @param target the final target of the message
285 * @param msgbuf the msg(s) to send
286 * @param msgbuf_size the size of msgbuf
287 * @param priority priority to pass on to core when sending the message
288 * @param timeout how long can this message be delayed (pass through to core)
289 * @param addr the address of this peer (internally known to DV)
290 * @param addrlen the length of the peer address
293 int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
294 const struct GNUNET_PeerIdentity *target,
297 unsigned int priority,
298 struct GNUNET_TIME_Relative timeout,
302 struct GNUNET_DV_SendMessage *msg;
304 msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendMessage) + msgbuf_size + addrlen);
305 msg->header.size = htons(sizeof(struct GNUNET_DV_SendMessage) + msgbuf_size + addrlen);
306 msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
307 memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
308 msg->msgbuf = GNUNET_malloc(msgbuf_size);
309 memcpy(msg->msgbuf, msgbuf, msgbuf_size);
310 msg->msgbuf_size = htons(msgbuf_size);
311 msg->priority = htonl(priority);
312 msg->timeout = timeout;
313 msg->addrlen = htons(addrlen);
314 memcpy(&msg[1], addr, addrlen);
316 add_pending(dv_handle, msg);
317 process_pending_message(dv_handle);
323 * Connect to the DV service
325 * @param sched the scheduler to use
326 * @param cfg the configuration to use
327 * @param receive_handler method call when on receipt from the service
328 * @param receive_handler_cls closure for receive_handler
330 * @return handle to the DV service
332 struct GNUNET_DV_Handle *
333 GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
334 const struct GNUNET_CONFIGURATION_Handle *cfg,
335 GNUNET_DV_MessageReceivedHandler receive_handler,
336 void *receive_handler_cls)
338 struct GNUNET_DV_Handle *handle;
340 handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
343 handle->sched = sched;
344 handle->pending_list = NULL;
345 handle->current = NULL;
346 handle->do_destroy = GNUNET_NO;
348 handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
349 handle->receive_handler = receive_handler;
350 handle->receive_cls = receive_handler_cls;
352 if (handle->client == NULL)
355 GNUNET_CLIENT_receive (handle->client,
356 &handle_message_receipt,
357 handle, GNUNET_TIME_UNIT_FOREVER_REL);
363 * Disconnect from the DV service
365 * @param handle the current handle to the service to disconnect
367 void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
369 struct PendingMessages *pos;
371 GNUNET_assert(handle != NULL);
373 if (handle->th != NULL) /* We have a live transmit request in the Aether */
375 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
378 if (handle->current != NULL) /* We are trying to send something now, clean it up */
379 GNUNET_free(handle->current);
380 while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
382 handle->pending_list = pos->next;
385 if (handle->client != NULL) /* Finally, disconnect from the service */
387 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
388 handle->client = NULL;
391 GNUNET_free (handle);
394 /* end of dv_api.c */