2 This file is part of GNUnet.
3 Copyright (C) 2010-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file transport/gnunet-service-transport_manipulation.c
23 * @brief transport component manipulation traffic for simulation
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "gnunet-service-transport_blacklist.h"
29 #include "gnunet-service-transport_clients.h"
30 #include "gnunet-service-transport_hello.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport_plugins.h"
33 #include "gnunet-service-transport_validation.h"
34 #include "gnunet-service-transport.h"
35 #include "transport.h"
39 * Struct containing information about manipulations to a specific peer
46 struct GNUNET_PeerIdentity peer;
49 * How long to delay incoming messages for this peer.
51 struct GNUNET_TIME_Relative delay_in;
54 * How long to delay outgoing messages for this peer.
56 struct GNUNET_TIME_Relative delay_out;
59 * Manipulated properties to use for this peer.
61 struct GNUNET_ATS_Properties properties;
64 * Task to schedule delayed sendding
66 struct GNUNET_SCHEDULER_Task *send_delay_task;
71 struct DelayQueueEntry *send_head;
76 struct DelayQueueEntry *send_tail;
81 * Entry in the delay queue for an outbound delayed message
83 struct DelayQueueEntry
88 struct DelayQueueEntry *prev;
93 struct DelayQueueEntry *next;
96 * Peer this entry is belonging to if (NULL == tmp): enqueued in
97 * generic DLL and scheduled by generic_send_delay_task else:
98 * enqueued in tmp->send_head and tmp->send_tail and scheduled by
99 * tmp->send_delay_task
106 struct GNUNET_PeerIdentity id;
109 * Absolute time when to send
111 struct GNUNET_TIME_Absolute sent_at;
126 struct GNUNET_TIME_Relative timeout;
129 * Transports send continuation
131 GST_NeighbourSendContinuation cont;
134 * Transports send continuation cls
140 * Hashmap contain all peers currently manipulated
142 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
145 * Inbound delay to apply to all peers.
147 static struct GNUNET_TIME_Relative delay_in;
150 * Outbound delay to apply to all peers.
152 static struct GNUNET_TIME_Relative delay_out;
155 * DLL head for delayed messages based on general delay
157 static struct DelayQueueEntry *generic_dqe_head;
160 * DLL tail for delayed messages based on general delay
162 static struct DelayQueueEntry *generic_dqe_tail;
165 * Task to schedule delayed sending based on general delay
167 static struct GNUNET_SCHEDULER_Task *generic_send_delay_task;
171 * Set traffic metric to manipulate
174 * @param client client sending message
175 * @param message containing information
178 GST_manipulation_set_metric (void *cls,
179 struct GNUNET_SERVER_Client *client,
180 const struct GNUNET_MessageHeader *message)
182 const struct TrafficMetricMessage *tm;
183 static struct GNUNET_PeerIdentity zero;
186 tm = (const struct TrafficMetricMessage *) message;
187 if (0 == memcmp (&tm->peer,
189 sizeof(struct GNUNET_PeerIdentity)))
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Received traffic metrics for all peers\n");
193 delay_in = GNUNET_TIME_relative_ntoh (tm->delay_in);
194 delay_out = GNUNET_TIME_relative_ntoh (tm->delay_out);
195 GNUNET_SERVER_receive_done (client,
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Received traffic metrics for peer `%s'\n",
201 GNUNET_i2s(&tm->peer));
203 (tmp = GNUNET_CONTAINER_multipeermap_get (peers,
206 tmp = GNUNET_new (struct TM_Peer);
207 tmp->peer = tm->peer;
208 GNUNET_CONTAINER_multipeermap_put (peers,
211 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
213 GNUNET_ATS_properties_ntoh (&tmp->properties,
215 tmp->delay_in = GNUNET_TIME_relative_ntoh (tm->delay_in);
216 tmp->delay_out = GNUNET_TIME_relative_ntoh (tm->delay_out);
217 GNUNET_SERVER_receive_done (client,
223 * We have delayed transmission, now it is time to send the
226 * @param cls the `struct DelayQueueEntry` to transmit
229 send_delayed (void *cls)
231 struct DelayQueueEntry *dqe = cls;
232 struct DelayQueueEntry *next;
233 struct TM_Peer *tmp = dqe->tmp;
234 struct GNUNET_TIME_Relative delay;
236 GNUNET_break (GNUNET_YES ==
237 GST_neighbours_test_connected (&dqe->id));
240 tmp->send_delay_task = NULL;
241 GNUNET_CONTAINER_DLL_remove (tmp->send_head,
244 next = tmp->send_head;
247 /* More delayed messages */
248 delay = GNUNET_TIME_absolute_get_remaining(next->sent_at);
249 tmp->send_delay_task = GNUNET_SCHEDULER_add_delayed(delay,
250 &send_delayed, next);
255 /* Remove from generic queue */
256 generic_send_delay_task = NULL;
257 GNUNET_CONTAINER_DLL_remove (generic_dqe_head,
260 next = generic_dqe_head;
263 /* More delayed messages */
264 delay = GNUNET_TIME_absolute_get_remaining(next->sent_at);
265 generic_send_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
270 GST_neighbours_send (&dqe->id,
281 * Adapter function between transport's send function and transport plugins.
282 * Delays message transmission if an artificial delay is configured.
284 * @param target the peer the message to send to
285 * @param msg the message received
286 * @param msg_size message size
287 * @param timeout timeout
288 * @param cont the continuation to call after sending
289 * @param cont_cls cls for @a cont
292 GST_manipulation_send (const struct GNUNET_PeerIdentity *target,
295 struct GNUNET_TIME_Relative timeout,
296 GST_NeighbourSendContinuation cont,
300 struct DelayQueueEntry *dqe;
301 struct GNUNET_TIME_Relative delay;
304 GNUNET_CONTAINER_multipeermap_get (peers,
306 delay = tmp->delay_out;
309 if (0 == delay.rel_value_us)
312 GST_neighbours_send (target,
319 dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
322 dqe->sent_at = GNUNET_TIME_relative_to_absolute (delay);
324 dqe->cont_cls = cont_cls;
326 dqe->msg_size = msg_size;
327 dqe->timeout = timeout;
333 GNUNET_CONTAINER_DLL_insert_tail (generic_dqe_head,
336 if (NULL == generic_send_delay_task)
337 generic_send_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
343 GNUNET_CONTAINER_DLL_insert_tail (tmp->send_head,
346 if (NULL == tmp->send_delay_task)
347 tmp->send_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Delaying %u byte message to peer `%s' with peer specific delay for %s\n",
355 GNUNET_STRINGS_relative_time_to_string (delay,
361 * Function that will be called to manipulate ATS information according to
362 * current manipulation settings
364 * @param address binary address
365 * @param session the session
366 * @param prop[IN|OUT] metrics to modify
369 GST_manipulation_manipulate_metrics (const struct GNUNET_HELLO_Address *address,
370 struct GNUNET_ATS_Session *session,
371 struct GNUNET_ATS_Properties *prop)
373 const struct GNUNET_PeerIdentity *peer = &address->peer;
376 tmp = GNUNET_CONTAINER_multipeermap_get (peers,
379 *prop = tmp->properties;
384 * Adapter function between transport plugins and transport receive function
385 * manipulation delays for next send.
387 * @param cls the closure for transport
388 * @param address the address and the peer the message was received from
389 * @param message the message received
390 * @param session the session the message was received on
391 * @return manipulated delay for next receive
393 struct GNUNET_TIME_Relative
394 GST_manipulation_recv (void *cls,
395 const struct GNUNET_HELLO_Address *address,
396 struct GNUNET_ATS_Session *session,
397 const struct GNUNET_MessageHeader *message)
400 struct GNUNET_TIME_Relative quota_delay;
401 struct GNUNET_TIME_Relative m_delay;
404 (tmp = GNUNET_CONTAINER_multipeermap_get (peers,
406 m_delay = tmp->delay_in;
410 quota_delay = GST_receive_callback (cls,
414 m_delay = GNUNET_TIME_relative_max (m_delay,
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Delaying next receive for peer `%s' for %s\n",
418 GNUNET_i2s (&address->peer),
419 GNUNET_STRINGS_relative_time_to_string (m_delay,
426 * Initialize traffic manipulation
429 GST_manipulation_init ()
431 struct GNUNET_TIME_Relative delay;
434 GNUNET_CONFIGURATION_get_value_time (GST_cfg,
436 "MANIPULATE_DELAY_IN",
438 (delay.rel_value_us > 0) )
440 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
441 "Delaying inbound traffic for %s\n",
442 GNUNET_STRINGS_relative_time_to_string (delay,
447 GNUNET_CONFIGURATION_get_value_time (GST_cfg,
449 "MANIPULATE_DELAY_OUT",
451 (delay.rel_value_us > 0) )
453 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
454 "Delaying outbound traffic for %s\n",
455 GNUNET_STRINGS_relative_time_to_string (delay,
459 peers = GNUNET_CONTAINER_multipeermap_create (4,
465 * Notify manipulation about disconnect so it can discard queued messages
467 * @param peer the disconnecting peer
470 GST_manipulation_peer_disconnect (const struct GNUNET_PeerIdentity *peer)
473 struct DelayQueueEntry *dqe;
474 struct DelayQueueEntry *next;
476 tmp = GNUNET_CONTAINER_multipeermap_get (peers,
480 while (NULL != (dqe = tmp->send_head))
482 GNUNET_CONTAINER_DLL_remove (tmp->send_head,
485 if (NULL != dqe->cont)
486 dqe->cont (dqe->cont_cls,
493 next = generic_dqe_head;
494 while (NULL != (dqe = next))
497 if (0 == memcmp(peer, &dqe->id, sizeof(dqe->id)))
499 GNUNET_CONTAINER_DLL_remove (generic_dqe_head,
502 if (NULL != dqe->cont)
503 dqe->cont (dqe->cont_cls,
510 if (NULL != generic_send_delay_task)
512 GNUNET_SCHEDULER_cancel (generic_send_delay_task);
513 generic_send_delay_task = NULL;
514 if (NULL != generic_dqe_head)
515 generic_send_delay_task
516 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining(generic_dqe_head->sent_at),
524 * Free manipulation information about a peer.
527 * @param key peer the info is about
528 * @param value a `struct TM_Peer` to free
529 * @return #GNUNET_OK (continue to iterate)
532 free_tmps (void *cls,
533 const struct GNUNET_PeerIdentity *key,
536 struct TM_Peer *tmp = value;
537 struct DelayQueueEntry *dqe;
539 GNUNET_break (GNUNET_YES ==
540 GNUNET_CONTAINER_multipeermap_remove (peers,
543 while (NULL != (dqe = tmp->send_head))
545 GNUNET_CONTAINER_DLL_remove (tmp->send_head,
548 if (NULL != dqe->cont)
549 dqe->cont (dqe->cont_cls,
555 if (NULL != tmp->send_delay_task)
557 GNUNET_SCHEDULER_cancel (tmp->send_delay_task);
558 tmp->send_delay_task = NULL;
566 * Stop traffic manipulation
569 GST_manipulation_stop ()
571 struct DelayQueueEntry *cur;
573 GNUNET_CONTAINER_multipeermap_iterate (peers,
576 GNUNET_CONTAINER_multipeermap_destroy (peers);
578 while (NULL != (cur = generic_dqe_head))
580 GNUNET_CONTAINER_DLL_remove (generic_dqe_head,
583 if (NULL != cur->cont)
584 cur->cont (cur->cont_cls,
590 if (NULL != generic_send_delay_task)
592 GNUNET_SCHEDULER_cancel (generic_send_delay_task);
593 generic_send_delay_task = NULL;
597 /* end of file gnunet-service-transport_manipulation.c */