2 This file is part of GNUnet
3 (C) 2004, 2009 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.
21 * @file fragmentation/test_fragmentation.c
22 * @brief test for fragmentation.c
23 * @author Christian Grothoff
26 #include "gnunet_fragmentation_lib.h"
28 #define VERBOSE GNUNET_NO
30 #define DETAILS GNUNET_NO
33 * Number of messages to transmit (note: each uses ~32k memory!)
38 * MTU to force on fragmentation (must be > 1k + 12)
43 * Simulate dropping of 1 out of how many messages? (must be > 1)
49 static unsigned int dups;
51 static unsigned int fragc;
53 static unsigned int frag_drops;
55 static unsigned int acks;
57 static unsigned int ack_drops;
59 static struct GNUNET_DEFRAGMENT_Context *defrag;
61 static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
63 static struct GNUNET_FRAGMENT_Context *frags[NUM_MSGS];
65 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
68 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
74 GNUNET_DEFRAGMENT_context_destroy (defrag);
76 for (i = 0; i < NUM_MSGS; i++)
80 GNUNET_FRAGMENT_context_destroy (frags[i]);
87 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
89 static unsigned int total;
94 FPRINTF (stderr, "%s", "!"); /* message complete, good! */
96 buf = (const char *) hdr;
97 for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
98 GNUNET_assert (buf[i] == (char) i);
101 if (0 == (total % (NUM_MSGS / 100)))
102 FPRINTF (stderr, "%s", ".");
104 /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
105 if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
107 if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
108 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
114 * Process ACK (by passing to fragmenter)
117 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
122 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
125 return; /* random drop */
127 for (i = 0; i < NUM_MSGS; i++)
129 if (frags[i] == NULL)
131 ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
132 if (ret == GNUNET_OK)
135 FPRINTF (stderr, "%s", "@"); /* good ACK */
137 GNUNET_FRAGMENT_context_destroy (frags[i]);
142 if (ret == GNUNET_NO)
145 FPRINTF (stderr, "%s", "@"); /* good ACK */
152 FPRINTF (stderr, "%s", "_"); /* BAD: ack that nobody feels responsible for... */
158 * Process fragment (by passing to defrag).
161 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
163 struct GNUNET_FRAGMENT_Context **fc = cls;
166 GNUNET_FRAGMENT_context_transmission_done (*fc);
167 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
170 return; /* random drop */
174 FPRINTF (stderr, "%s", "E"); /* Error: frag after shutdown!? */
177 ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
178 if (ret == GNUNET_NO)
181 FPRINTF (stderr, "%s", "?"); /* duplicate fragment */
185 else if (ret == GNUNET_OK)
188 FPRINTF (stderr, "%s", "."); /* good fragment */
196 * Main function run with scheduler.
199 run (void *cls, char *const *args, const char *cfgfile,
200 const struct GNUNET_CONFIGURATION_Handle *cfg)
203 struct GNUNET_MessageHeader *msg;
204 char buf[MTU + 32 * 1024];
206 defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS /* enough space for all */
207 , NULL, &proc_msgs, &proc_acks);
208 for (i = 0; i < sizeof (buf); i++)
210 msg = (struct GNUNET_MessageHeader *) buf;
211 for (i = 0; i < NUM_MSGS; i++)
213 msg->type = htons ((uint16_t) i);
215 htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
216 frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
218 GNUNET_TIME_UNIT_SECONDS, msg,
219 &proc_frac, &frags[i]);
225 main (int argc, char *argv[])
227 struct GNUNET_GETOPT_CommandLineOption options[] = {
228 GNUNET_GETOPT_OPTION_END
230 char *const argv_prog[] = {
231 "test-fragmentation",
233 "test_fragmentation_data.conf",
244 GNUNET_log_setup ("test-fragmentation",
251 for (i = 0; i < NUM_MSGS; i++)
252 GNUNET_BANDWIDTH_tracker_init (&trackers[i],
253 GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
255 GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
258 "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
259 fragc, dups, acks, ack_drops);