-remove trailing whitespace
[oweals/gnunet.git] / src / fragmentation / test_fragmentation.c
1 /*
2      This file is part of GNUnet
3      (C) 2004, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file fragmentation/test_fragmentation.c
22  * @brief test for fragmentation.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_fragmentation_lib.h"
27
28 #define DETAILS GNUNET_NO
29
30 /**
31  * Number of messages to transmit (note: each uses ~32k memory!)
32  */
33 #define NUM_MSGS 500
34
35 /**
36  * MTU to force on fragmentation (must be > 1k + 12)
37  */
38 #define MTU 1111
39
40 /**
41  * Simulate dropping of 1 out of how many messages? (must be > 1)
42  */
43 #define DROPRATE 5
44
45 static int ret = 1;
46
47 static unsigned int dups;
48
49 static unsigned int fragc;
50
51 static unsigned int frag_drops;
52
53 static unsigned int acks;
54
55 static unsigned int ack_drops;
56
57 static struct GNUNET_DEFRAGMENT_Context *defrag;
58
59 static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
60
61 static struct GNUNET_FRAGMENT_Context *frags[NUM_MSGS];
62
63 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
64
65 static void
66 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
67 {
68   unsigned int i;
69
70   ret = 0;
71   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
72   GNUNET_DEFRAGMENT_context_destroy (defrag);
73   defrag = NULL;
74   for (i = 0; i < NUM_MSGS; i++)
75   {
76     if (frags[i] == NULL)
77       continue;
78     GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
79     frags[i] = NULL;
80   }
81 }
82
83
84 static void
85 proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
86 {
87   static unsigned int total;
88   unsigned int i;
89   const char *buf;
90
91 #if DETAILS
92   FPRINTF (stderr, "%s",  "!");        /* message complete, good! */
93 #endif
94   buf = (const char *) hdr;
95   for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
96     GNUNET_assert (buf[i] == (char) i);
97   total++;
98 #if ! DETAILS
99   if (0 == (total % (NUM_MSGS / 100)))
100     FPRINTF (stderr, "%s",  ".");
101 #endif
102   /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
103   if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
104   {
105     if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
106       shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
107   }
108 }
109
110
111 /**
112  * Process ACK (by passing to fragmenter)
113  */
114 static void
115 proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
116 {
117   unsigned int i;
118   int ret;
119
120   if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
121   {
122     ack_drops++;
123     return;                     /* random drop */
124   }
125   for (i = 0; i < NUM_MSGS; i++)
126   {
127     if (frags[i] == NULL)
128       continue;
129     ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
130     if (ret == GNUNET_OK)
131     {
132 #if DETAILS
133       FPRINTF (stderr, "%s",  "@");    /* good ACK */
134 #endif
135       GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
136       frags[i] = NULL;
137       acks++;
138       return;
139     }
140     if (ret == GNUNET_NO)
141     {
142 #if DETAILS
143       FPRINTF (stderr, "%s",  "@");    /* good ACK */
144 #endif
145       acks++;
146       return;
147     }
148   }
149 #if DETAILS
150   FPRINTF (stderr, "%s",  "_");        /* BAD: ack that nobody feels responsible for... */
151 #endif
152 }
153
154
155 /**
156  * Process fragment (by passing to defrag).
157  */
158 static void
159 proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
160 {
161   struct GNUNET_FRAGMENT_Context **fc = cls;
162   int ret;
163
164   GNUNET_FRAGMENT_context_transmission_done (*fc);
165   if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
166   {
167     frag_drops++;
168     return;                     /* random drop */
169   }
170   if (NULL == defrag)
171   {
172     FPRINTF (stderr, "%s",  "E");      /* Error: frag after shutdown!? */
173     return;
174   }
175   ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
176   if (ret == GNUNET_NO)
177   {
178 #if DETAILS
179     FPRINTF (stderr, "%s",  "?");      /* duplicate fragment */
180 #endif
181     dups++;
182   }
183   else if (ret == GNUNET_OK)
184   {
185 #if DETAILS
186     FPRINTF (stderr, "%s",  ".");      /* good fragment */
187 #endif
188     fragc++;
189   }
190 }
191
192
193 /**
194  * Main function run with scheduler.
195  */
196 static void
197 run (void *cls, char *const *args, const char *cfgfile,
198      const struct GNUNET_CONFIGURATION_Handle *cfg)
199 {
200   unsigned int i;
201   struct GNUNET_MessageHeader *msg;
202   char buf[MTU + 32 * 1024];
203
204   defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS        /* enough space for all */
205                                              , NULL, &proc_msgs, &proc_acks);
206   for (i = 0; i < sizeof (buf); i++)
207     buf[i] = (char) i;
208   msg = (struct GNUNET_MessageHeader *) buf;
209   for (i = 0; i < NUM_MSGS; i++)
210   {
211     msg->type = htons ((uint16_t) i);
212     msg->size =
213         htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
214     frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
215                                                MTU, &trackers[i],
216                                                GNUNET_TIME_UNIT_MILLISECONDS,
217                                                GNUNET_TIME_UNIT_SECONDS,
218                                                msg,
219                                                &proc_frac, &frags[i]);
220   }
221 }
222
223
224 int
225 main (int argc, char *argv[])
226 {
227   struct GNUNET_GETOPT_CommandLineOption options[] = {
228     GNUNET_GETOPT_OPTION_END
229   };
230   char *const argv_prog[] = {
231     "test-fragmentation",
232     "-c",
233     "test_fragmentation_data.conf",
234     "-L",
235     "WARNING",
236     NULL
237   };
238   unsigned int i;
239
240   GNUNET_log_setup ("test-fragmentation",
241                     "WARNING",
242                     NULL);
243   for (i = 0; i < NUM_MSGS; i++)
244     GNUNET_BANDWIDTH_tracker_init (&trackers[i],
245                                    GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
246                                    100);
247   GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
248                       &run, NULL);
249   FPRINTF (stderr,
250            "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
251            fragc, dups, acks, ack_drops);
252   return ret;
253 }