aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-service-transport_manipulation.c
blob: 831d8fd5d52d13f0ad55f49f505fe8397ab04aeb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
     This file is part of GNUnet.
     (C) 2010,2011 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file transport/gnunet-service-transport_manipulation.c
 * @brief transport component manipulation traffic for simulation
 * @author Christian Grothoff
 * @author Matthias Wachs
 */
#include "platform.h"
#include "gnunet-service-transport_blacklist.h"
#include "gnunet-service-transport_clients.h"
#include "gnunet-service-transport_hello.h"
#include "gnunet-service-transport_neighbours.h"
#include "gnunet-service-transport_plugins.h"
#include "gnunet-service-transport_validation.h"
#include "gnunet-service-transport.h"
#include "transport.h"

#define DELAY 0
#define DISTANCE 1


enum TRAFFIC_METRIC_DIRECTION
{
	TM_SEND = 0,
	TM_RECEIVE = 1,
	TM_BOTH = 2
};

struct GST_ManipulationHandle man_handle;


struct GST_ManipulationHandle
{
	struct GNUNET_CONTAINER_MultiHashMap *peers;

	GNUNET_TRANSPORT_UpdateAddressMetrics metric_update_cb;

	/**
	 * General inbound delay
	 */
	struct GNUNET_TIME_Relative delay_recv;

	/**
	 * General outbound delay
	 */
	struct GNUNET_TIME_Relative delay_send;

	/**
	 * General inbound distance
	 */
	 unsigned long long  distance_recv;

	/**
	 * General outbound distance
	 */
	 unsigned long long distance_send;

};


struct TM_Peer;



struct DelayQueueEntry
{
	struct DelayQueueEntry *prev;
	struct DelayQueueEntry *next;
	struct TM_Peer *tmp;
	struct GNUNET_TIME_Absolute sent_at;
	void *msg;
	size_t msg_size;
	struct GNUNET_TIME_Relative timeout;
	GST_NeighbourSendContinuation cont;
	void *cont_cls;
};

struct TM_Peer
{
	struct GNUNET_PeerIdentity peer;
	uint32_t metrics [TM_BOTH][GNUNET_ATS_QualityPropertiesCount];
	GNUNET_SCHEDULER_TaskIdentifier send_delay_task;
	struct DelayQueueEntry *send_head;
	struct DelayQueueEntry *send_tail;
};



static void
set_delay(struct TM_Peer *tmp, struct GNUNET_PeerIdentity *peer, int direction, uint32_t value)
{
	uint32_t val;
	GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Set traffic metrics %s for peer `%s' in direction %s to %u\n",
			"DELAY", GNUNET_i2s(peer),
			(TM_BOTH == direction) ? "BOTH" : (TM_SEND == direction) ? "SEND": "RECEIVE", value);

	if (UINT32_MAX == value)
		val = UINT32_MAX - 1; /* prevent overflow */
	else if (0 == value)
		val = UINT32_MAX; /* disable */
	else
		val = value;

	switch (direction) {
		case TM_BOTH:
			tmp->metrics[TM_SEND][DELAY] = val;
			tmp->metrics[TM_RECEIVE][DELAY] = val;
			break;
		case TM_SEND:
			tmp->metrics[TM_SEND][DELAY] = val;
			break;
		case TM_RECEIVE:
			tmp->metrics[TM_RECEIVE][DELAY] = val;
			break;
		default:
			break;
	}

}

static void
set_distance (struct TM_Peer *tmp, struct GNUNET_PeerIdentity *peer, int direction, uint32_t value)
{
	uint32_t val;
	GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Set traffic metrics %s for peer `%s' in direction %s to %u\n",
			"DISTANCE", GNUNET_i2s(peer),
			(TM_BOTH == direction) ? "BOTH" : (TM_SEND == direction) ? "SEND": "RECEIVE", value);

	if (UINT32_MAX == value)
		val = UINT32_MAX - 1; /* prevent overflow */
	else if (0 == value)
		val = UINT32_MAX; /* disable */
	else
		val = value;

	switch (direction) {
	case TM_BOTH:
		tmp->metrics[TM_SEND][DISTANCE] = val;
		tmp->metrics[TM_RECEIVE][DISTANCE] = val;
		break;
	case TM_SEND:
		tmp->metrics[TM_SEND][DISTANCE] = val;
		break;
	case TM_RECEIVE:
		tmp->metrics[TM_RECEIVE][DISTANCE] = val;
		break;
	default:
		break;
	}
}

void
GST_manipulation_set_metric (void *cls, struct GNUNET_SERVER_Client *client,
    const struct GNUNET_MessageHeader *message)
{
	struct TrafficMetricMessage *tm = (struct TrafficMetricMessage *) message;
	struct GNUNET_PeerIdentity dummy;
	struct GNUNET_ATS_Information *ats;
	struct TM_Peer *tmp;
	uint32_t type;
	uint32_t value;
	uint16_t direction;
	int c;
	int c2;

	if (0 == ntohs (tm->ats_count))
	  GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);

	switch (ntohs(tm->direction)) {
		case 1:
			direction = TM_SEND;
			break;
		case 2:
			direction = TM_RECEIVE;
			break;
		case 3:
			direction = TM_BOTH;
			break;
		default:
			break;
	}

	memset (&dummy, '\0', sizeof (struct GNUNET_PeerIdentity));
	if (0 == memcmp (&tm->peer, &dummy, sizeof (struct GNUNET_PeerIdentity)))
	{
			GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received traffic metrics for all peers \n");

			ats = (struct GNUNET_ATS_Information *) &tm[1];
			for (c = 0; c < ntohs (tm->ats_count); c++)
			{
					type = htonl (ats[c].type);
					value = htonl (ats[c].value);

					switch (type) {
						case GNUNET_ATS_QUALITY_NET_DELAY:
							if ((TM_RECEIVE == direction) || (TM_BOTH == direction))
									man_handle.delay_recv.rel_value = value;
							if ((TM_SEND == direction) || (TM_BOTH == direction))
									man_handle.delay_send.rel_value = value;
							break;
						case GNUNET_ATS_QUALITY_NET_DISTANCE:
							if ((TM_RECEIVE == direction) || (TM_BOTH == direction))
									man_handle.distance_recv = value;
							if ((TM_SEND == direction) || (TM_BOTH == direction))
									man_handle.distance_send = value;
							break;
						default:
							break;
					}

			}
			return;
	}

	GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received traffic metrics for peer `%s'\n",
			GNUNET_i2s(&tm->peer));

	if (NULL == (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &tm->peer.hashPubKey)))
	{
			tmp = GNUNET_malloc (sizeof (struct TM_Peer));
			tmp->peer = (tm->peer);
			for (c = 0; c < TM_BOTH; c++)
			{
					for (c2 = 0; c2 < GNUNET_ATS_QualityPropertiesCount; c2++)
					{
							tmp->metrics[c][c2] = UINT32_MAX;
					}
			}
			GNUNET_CONTAINER_multihashmap_put (man_handle.peers, &tm->peer.hashPubKey, tmp, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
	}

	ats = (struct GNUNET_ATS_Information *) &tm[1];
	for (c = 0; c < ntohs (tm->ats_count); c++)
	{
			type = htonl (ats[c].type);
			value = htonl (ats[c].value);
			switch (type) {
				case GNUNET_ATS_QUALITY_NET_DELAY:
					set_delay (tmp, &tm->peer, direction, value);
					break;
				case GNUNET_ATS_QUALITY_NET_DISTANCE:
					set_distance (tmp, &tm->peer, direction, value);
					break;
				default:
					break;
			}
	}

  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}

static void
send_delayed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
	struct DelayQueueEntry *dqe = cls;
	struct DelayQueueEntry *next;
	struct TM_Peer *tmp = dqe->tmp;
	struct GNUNET_TIME_Relative delay;
	tmp->send_delay_task = GNUNET_SCHEDULER_NO_TASK;
	GNUNET_CONTAINER_DLL_remove (tmp->send_head, tmp->send_tail, dqe);
	GST_neighbours_send (&tmp->peer, dqe->msg, dqe->msg_size, dqe->timeout, dqe->cont, dqe->cont_cls);

	next = tmp->send_head;
	if (NULL != next)
	{
			/* More delayed messages */
			delay = GNUNET_TIME_absolute_get_remaining (next->sent_at);
			tmp->send_delay_task = GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, dqe);
	}

	GNUNET_free (dqe);
}

void
GST_manipulation_send (const struct GNUNET_PeerIdentity *target, const void *msg,
    size_t msg_size, struct GNUNET_TIME_Relative timeout,
    GST_NeighbourSendContinuation cont, void *cont_cls)
{
	struct TM_Peer *tmp;
	struct DelayQueueEntry *dqe;
	struct GNUNET_TIME_Relative delay;

	if (NULL != (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &target->hashPubKey)))
	{
			/* Manipulate here */
			/* Delay */
			if (UINT32_MAX != tmp->metrics[TM_SEND][DELAY])
			{
					/* We have a delay */
					delay.rel_value = tmp->metrics[TM_SEND][DELAY];
					dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
					dqe->tmp = tmp;
					dqe->sent_at = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), delay);
					dqe->cont = cont;
					dqe->cont_cls = cont_cls;
					dqe->msg = &dqe[1];
					dqe->msg_size = msg_size;
					dqe->timeout = timeout;
					memcpy (dqe->msg, msg, msg_size);
					GNUNET_CONTAINER_DLL_insert_tail (tmp->send_head, tmp->send_tail, dqe);
					if (GNUNET_SCHEDULER_NO_TASK == tmp->send_delay_task)
						tmp->send_delay_task =GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, dqe);
					return;
			}
	}
	else if (man_handle.delay_send.rel_value != 0)
	{
			/* We have a delay */
			delay = man_handle.delay_send;
			dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
			dqe->tmp = tmp;
			dqe->sent_at = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), delay);
			dqe->cont = cont;
			dqe->cont_cls = cont_cls;
			dqe->msg = &dqe[1];
			dqe->msg_size = msg_size;
			dqe->timeout = timeout;
			memcpy (dqe->msg, msg, msg_size);
			GNUNET_CONTAINER_DLL_insert_tail (tmp->send_head, tmp->send_tail, dqe);
			if (GNUNET_SCHEDULER_NO_TASK == tmp->send_delay_task)
				tmp->send_delay_task =GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, dqe);
			return;
	}

	/* Normal sending */
	GST_neighbours_send (target, msg, msg_size, timeout, cont, cont_cls);
}


/**
 * Function that will be called to figure if an address is an loopback,
 * LAN, WAN etc. address
 *
 * @param cls closure
 * @param addr binary address
 * @param addrlen length of the address
 * @return ATS Information containing the network type
 */
void
GST_manipulation_metrics_recv (void *cls,
																 struct GNUNET_PeerIdentity *peer,
																 const char *address,
																 uint16_t address_len,
																 struct Session *session,
																 struct GNUNET_ATS_Information *ats,
																 uint32_t ats_count)
{

	struct GNUNET_ATS_Information ats_new[ats_count];
	struct TM_Peer *tmp;
	uint32_t m_distance;
	int d;


	m_distance = 0;
	if (NULL != (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &peer->hashPubKey)))
	{
			if (UINT32_MAX != tmp->metrics[TM_RECEIVE][DISTANCE])
				m_distance = tmp->metrics[TM_RECEIVE][DISTANCE];
	}

	for (d = 0; d < ats_count; d++)
	{
		ats_new[d] = ats[d];
		if (ntohl(ats[d].type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
		{
			if (m_distance > 0)
				ats_new[d].value = htonl(m_distance);
			else if  (man_handle.distance_recv > 0)
				ats_new[d].value = htonl(man_handle.distance_recv);
		}
	}

	man_handle.metric_update_cb (cls, peer, address, address_len, session, ats_new, ats_count);
}

struct GNUNET_TIME_Relative
GST_manipulation_recv (void *cls,
		const struct GNUNET_PeerIdentity *peer,
    const struct GNUNET_MessageHeader *message,
    struct Session *session,
    const char *sender_address,
    uint16_t sender_address_len)
{
	struct TM_Peer *tmp;

	struct GNUNET_TIME_Relative quota_delay;
	struct GNUNET_TIME_Relative m_delay;

	if (man_handle.delay_recv.rel_value > GNUNET_TIME_UNIT_ZERO.rel_value)
		m_delay = man_handle.delay_recv; /* Global delay */
	else
		m_delay = GNUNET_TIME_UNIT_ZERO;

	if (NULL != (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &peer->hashPubKey)))
	{
			/* Manipulate receive delay */
			if (UINT32_MAX != tmp->metrics[TM_RECEIVE][DELAY])
					m_delay.rel_value = tmp->metrics[TM_RECEIVE][DELAY]; /* Peer specific delay */
	}

	quota_delay = GST_receive_callback (cls, peer, message,
			session, sender_address, sender_address_len);
	if (quota_delay.rel_value > m_delay.rel_value)
		return quota_delay;
	else
		return m_delay;

}

void
GST_manipulation_init (const struct GNUNET_CONFIGURATION_Handle *GST_cfg,
		GNUNET_TRANSPORT_UpdateAddressMetrics metric_update_cb)
{
	man_handle.metric_update_cb = metric_update_cb;

	if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
			"transport", "MANIPULATE_DISTANCE_IN", &man_handle.distance_recv))
		GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting inbound distance_in to %u\n",
				(unsigned long long) man_handle.distance_recv);
	else
		man_handle.distance_recv = 0;

	if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
			"transport", "MANIPULATE_DISTANCE_OUT", &man_handle.distance_send))
		GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting outbound distance_in to %u\n",
				(unsigned long long) man_handle.distance_send);
	else
		man_handle.distance_send = 0;

	if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_time (GST_cfg,
			"transport", "MANIPULATE_DELAY_IN", &man_handle.delay_recv))
		GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Delaying inbound traffic for %llu ms\n",
				(unsigned long long) man_handle.delay_recv.rel_value);
	else
		man_handle.delay_recv.rel_value = 0;

	if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_time (GST_cfg,
			"transport", "MANIPULATE_DELAY_OUT", &man_handle.delay_send))
		GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Delaying outbound traffic for %llu ms\n",
			(unsigned long long) man_handle.delay_send.rel_value);
	else
		man_handle.delay_send.rel_value = 0;

	man_handle.peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
}

int free_tmps (void *cls,
							 const struct GNUNET_HashCode * key,
							 void *value)
{
	struct DelayQueueEntry *dqe;
	struct DelayQueueEntry *next;
	if (NULL != value)
	{
			struct TM_Peer *tmp = (struct TM_Peer *) value;
			GNUNET_CONTAINER_multihashmap_remove (man_handle.peers, key, value);
			next = tmp->send_head;
			while (NULL != (dqe = next))
			{
					next = dqe->next;
					GNUNET_CONTAINER_DLL_remove (tmp->send_head, tmp->send_tail, dqe);
					GNUNET_free (dqe);
			}
			if (GNUNET_SCHEDULER_NO_TASK != tmp->send_delay_task)
			{
					GNUNET_SCHEDULER_cancel (tmp->send_delay_task);
					tmp->send_delay_task = GNUNET_SCHEDULER_NO_TASK;
			}
			GNUNET_free (tmp);
	}
	return GNUNET_OK;
}

void
GST_manipulation_stop ()
{
	GNUNET_CONTAINER_multihashmap_iterate (man_handle.peers, &free_tmps,NULL);

	GNUNET_CONTAINER_multihashmap_destroy (man_handle.peers);
	man_handle.peers = NULL;
}


/* end of file gnunet-service-transport_manipulation.c */