formatting
[oweals/gnunet.git] / src / mesh / gnunet-regex-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 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 /**
22  * @file mesh/gnunet-regex-profiler.c
23  * @brief Regex profiler for testing distributed regex use.
24  * @author Bart Polot
25  * @author Max Szengel
26  *
27  * TODO:
28  * - Connect to statistics service
29  */
30
31 #include <string.h>
32
33 #include "platform.h"
34 #include "gnunet_applications.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_mesh_service.h"
37 #include "gnunet_stream_lib.h"
38 #include "gnunet_testbed_service.h"
39
40 /**
41  * DLL of operations
42  */
43 struct DLLOperation
44 {
45   /**
46    * The testbed operation handle
47    */
48   struct GNUNET_TESTBED_Operation *op;
49
50   /**
51    * Closure
52    */
53   void *cls;
54
55   /**
56    * The next pointer for DLL
57    */
58   struct DLLOperation *next;
59
60   /**
61    * The prev pointer for DLL
62    */
63   struct DLLOperation *prev;
64 };
65
66
67 /**
68  * Available states during profiling
69  */
70 enum State
71 {
72   /**
73    * Initial state
74    */
75   STATE_INIT = 0,
76
77   /**
78    * Starting slaves
79    */
80   STATE_SLAVES_STARTING,
81
82   /**
83    * Creating peers
84    */
85   STATE_PEERS_CREATING,
86
87   /**
88    * Starting peers
89    */
90   STATE_PEERS_STARTING,
91
92   /**
93    * Linking peers
94    */
95   STATE_PEERS_LINKING,
96
97   /**
98    * Destroying peers; we can do this as the controller takes care of stopping a
99    * peer if it is running
100    */
101   STATE_PEERS_DESTROYING
102 };
103
104
105 /**
106  * An array of hosts loaded from the hostkeys file
107  */
108 static struct GNUNET_TESTBED_Host **hosts;
109
110 /**
111  * Peer handles.
112  */
113 struct RegexPeer
114 {
115   /**
116    * Peer id.
117    */
118   unsigned int id;
119
120   /**
121    * The actual testbed peer handle.
122    */
123   struct GNUNET_TESTBED_Peer *peer_handle;
124
125   /**
126    * Host on which the peer is running.
127    */
128   struct GNUNET_TESTBED_Host *host_handle;
129
130   /**
131    * Filename of the peer's policy file.
132    */
133   char *policy_file;
134
135   /**
136    * Peers search string.
137    */
138   const char *search_str;
139
140   /**
141    * Peer's mesh handle.
142    */
143   struct GNUNET_MESH_Handle *mesh_handle;
144
145   /**
146    * Peer's mesh tunnel handle.
147    */
148   struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
149
150   /**
151    * Testbed operation handle for the mesh service.
152    */
153   struct GNUNET_TESTBED_Operation *mesh_op_handle;
154
155   /**
156    * Peers's statistics handle.
157    */
158   struct GNUNET_STATISTICS_Handle *stats_handle;
159
160   /**
161    * Testbed operation handle for the statistics service.
162    */
163   struct GNUNET_TESTBED_Operation *stats_op_handle;
164 };
165
166 /**
167  * Array of peer handles used to pass to
168  * GNUNET_TESTBED_overlay_configure_topology
169  */
170 struct GNUNET_TESTBED_Peer **peer_handles;
171
172 /**
173  * The array of peers; we fill this as the peers are given to us by the testbed
174  */
175 static struct RegexPeer *peers;
176
177 /**
178  * Host registration handle
179  */
180 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
181
182 /**
183  * Handle to the master controller process
184  */
185 struct GNUNET_TESTBED_ControllerProc *mc_proc;
186
187 /**
188  * Handle to the master controller
189  */
190 struct GNUNET_TESTBED_Controller *mc;
191
192 /**
193  * Handle to global configuration
194  */
195 struct GNUNET_CONFIGURATION_Handle *cfg;
196
197 /**
198  * Head of the operations list
199  */
200 struct DLLOperation *dll_op_head;
201
202 /**
203  * Tail of the operations list
204  */
205 struct DLLOperation *dll_op_tail;
206
207 /**
208  * Peer linking - topology operation
209  */
210 struct GNUNET_TESTBED_Operation *topology_op;
211
212 /**
213  * Abort task identifier
214  */
215 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
216
217 /**
218  * Host registration task identifier
219  */
220 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
221
222 /**
223  * Global event mask for all testbed events
224  */
225 uint64_t event_mask;
226
227 /**
228  * The starting time of a profiling step
229  */
230 struct GNUNET_TIME_Absolute prof_start_time;
231
232 /**
233  * Duration profiling step has taken
234  */
235 struct GNUNET_TIME_Relative prof_time;
236
237 /**
238  * Current peer id
239  */
240 unsigned int peer_id;
241
242 /**
243  * Number of peers to be started by the profiler
244  */
245 static unsigned int num_peers;
246
247 /**
248  * Number of hosts in the hosts array
249  */
250 static unsigned int num_hosts;
251
252 /**
253  * Number of random links to be established between peers
254  */
255 static unsigned int num_links;
256
257 /**
258  * Number of timeout failures to tolerate
259  */
260 static unsigned int num_cont_fails;
261
262 /**
263  * Number of times we try overlay connect operations
264  */
265 static unsigned int retry_links;
266
267 /**
268  * Continuous failures during overlay connect operations
269  */
270 static unsigned int cont_fails;
271
272 /**
273  * Global testing status
274  */
275 static int result;
276
277 /**
278  * current state of profiling
279  */
280 enum State state;
281
282 /**
283  * Folder where policy files are stored.
284  */
285 static char * policy_dir;
286
287 /**
288  * Search strings.
289  */
290 static char **search_strings;
291
292 /**
293  * Number of search strings.
294  */
295 static int num_search_strings;
296
297 /**
298  * Number of peers found with search strings.
299  */
300 static unsigned int peers_found;
301
302 /**
303  * Search task identifier
304  */
305 static GNUNET_SCHEDULER_TaskIdentifier search_task;
306
307 /**
308  * Search timeout task identifier.
309  */
310 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
311
312 /**
313  * Search timeout in seconds.
314  */
315 static struct GNUNET_TIME_Relative search_timeout = { 60000 };
316
317 /**
318  * How long do we wait before starting the search?
319  * Default: 1 m.
320  */
321 static struct GNUNET_TIME_Relative search_delay = { 60000 };
322
323
324 /**
325  * Shutdown nicely
326  *
327  * @param cls NULL
328  * @param tc the task context
329  */
330 static void
331 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
332 {
333   struct DLLOperation *dll_op;
334   unsigned int nhost;
335   unsigned int peer_cnt;
336   unsigned int search_str_cnt;
337
338   for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
339   {
340     if (NULL != peers[peer_cnt].mesh_op_handle)
341       GNUNET_TESTBED_operation_cancel (peers[peer_cnt].mesh_op_handle);
342     if (NULL != peers[peer_cnt].stats_op_handle)
343       GNUNET_TESTBED_operation_cancel (peers[peer_cnt].stats_op_handle);
344   }
345   for (search_str_cnt = 0; search_str_cnt < num_search_strings; search_str_cnt++)
346   {
347     GNUNET_free (search_strings[search_str_cnt]);
348   }
349   GNUNET_free (search_strings);
350   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
351     GNUNET_SCHEDULER_cancel (abort_task);
352   if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
353     GNUNET_SCHEDULER_cancel (register_hosts_task);
354   if (NULL != reg_handle)
355     GNUNET_TESTBED_cancel_registration (reg_handle);
356   if (NULL != topology_op)
357     GNUNET_TESTBED_operation_cancel (topology_op);
358   for (nhost = 0; nhost < num_hosts; nhost++)
359     if (NULL != hosts[nhost])
360       GNUNET_TESTBED_host_destroy (hosts[nhost]);
361   GNUNET_free_non_null (hosts);
362   while (NULL != (dll_op = dll_op_head))
363   {
364     GNUNET_TESTBED_operation_cancel (dll_op->op);
365     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
366     GNUNET_free (dll_op);
367   }
368   if (NULL != mc)
369     GNUNET_TESTBED_controller_disconnect (mc);
370   if (NULL != mc_proc)
371     GNUNET_TESTBED_controller_stop (mc_proc);
372   if (NULL != cfg)
373     GNUNET_CONFIGURATION_destroy (cfg);
374
375   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
376 }
377
378
379 /**
380  * abort task to run on test timed out
381  *
382  * @param cls NULL
383  * @param tc the task context
384  */
385 static void
386 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
387 {
388   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
389   abort_task = GNUNET_SCHEDULER_NO_TASK;
390   result = GNUNET_SYSERR;
391   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
392 }
393
394
395 /******************************************************************************/
396 /*********************  STATISTICS SERVICE CONNECTIONS  ***********************/
397 /******************************************************************************/
398
399 /**
400  * Adapter function called to establish a connection to
401  * statistics service.
402  *
403  * @param cls closure
404  * @param cfg configuration of the peer to connect to; will be available until
405  *          GNUNET_TESTBED_operation_done() is called on the operation returned
406  *          from GNUNET_TESTBED_service_connect()
407  * @return service handle to return in 'op_result', NULL on error
408  */
409 static void *
410 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
411 {
412   return GNUNET_STATISTICS_create ("<driver>", cfg);
413 }
414
415
416 /**
417  * Adapter function called to destroy a connection to
418  * statistics service.
419  *
420  * @param cls closure
421  * @param op_result service handle returned from the connect adapter
422  */
423 static void
424 stats_da (void *cls, void *op_result)
425 {
426   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
427 }
428
429
430 /**
431  * Process statistic values.
432  *
433  * @param cls closure
434  * @param subsystem name of subsystem that created the statistic
435  * @param name the name of the datum
436  * @param value the current value
437  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
438  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
439  */
440 static int
441 stats_iterator (void *cls, const char *subsystem, const char *name,
442                 uint64_t value, int is_persistent)
443 {
444   struct RegexPeer *peer = cls;
445   //  char output_buffer[512];
446   //  size_t size;
447
448   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stats iterator callback for peer %u\n", peer->id);
449   /*
450   if (NULL == output_file)
451   {*/
452     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
453                 "%p -> %s [%s]: %llu\n",
454                 peer, subsystem, name, value);
455     return GNUNET_OK;
456     /*
457   }
458   size =
459     GNUNET_snprintf (output_buffer,
460                      sizeof (output_buffer),
461                      "%p [%s] %s %llu\n",
462                      peer,
463                      subsystem, name, value);
464   if (size != GNUNET_DISK_file_write (output_file, output_buffer, size))
465     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
466   */
467   return GNUNET_OK;
468 }
469
470
471 /**
472  * Continuation callback for stats.
473  *
474  * @param cls closure
475  * @param success GNUNET_OK if statistics were
476  *        successfully obtained, GNUNET_SYSERR if not.
477  */
478 static void
479 stats_cont_cb (void *cls,
480                int success)
481 {
482   struct RegexPeer *peer = cls;
483
484   if (GNUNET_OK != success)
485     return;
486
487   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488               "Stats continuation callback for peer %u.\n", peer->id);
489
490 }
491
492
493 /**
494  * Function called by testbed once we are connected to stats service.
495  *
496  * @param cls the 'struct RegexPeer' for which we connected to stats
497  * @param op connect operation handle
498  * @param ca_result handle to stats service
499  * @param emsg error message on failure
500  */
501 static void
502 stats_connect_cb (void *cls,
503                   struct GNUNET_TESTBED_Operation *op,
504                   void *ca_result,
505                   const char *emsg)
506 {
507   struct RegexPeer *peer = cls;
508
509   if (NULL == ca_result)
510   {
511     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
512                 "Failed to connect to statistics service on peer %u: %s\n",
513                 peer->id, emsg);
514     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
515     return;
516   }
517
518   peer->stats_handle = ca_result;
519 }
520
521
522 /******************************************************************************/
523 /************************  MESH SERVICE CONNECTIONS  **************************/
524 /******************************************************************************/
525
526 /**
527  * Method called whenever another peer has added us to a tunnel
528  * the other peer initiated.
529  * Only called (once) upon reception of data with a message type which was
530  * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
531  * causes te tunnel to be ignored and no further notifications are sent about
532  * the same tunnel.
533  *
534  * @param cls closure
535  * @param tunnel new handle to the tunnel
536  * @param initiator peer that started the tunnel
537  * @param atsi performance information for the tunnel
538  * @return initial tunnel context for the tunnel
539  *         (can be NULL -- that's not an error)
540  */
541 void *
542 mesh_inbound_tunnel_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
543                              const struct GNUNET_PeerIdentity *initiator,
544                              const struct GNUNET_ATS_Information *atsi)
545 {
546   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh inbound tunnel handler.\n");
547
548   return NULL;
549 }
550
551
552 /**
553  * Function called whenever an inbound tunnel is destroyed.  Should clean up
554  * any associated state.  This function is NOT called if the client has
555  * explicitly asked for the tunnel to be destroyed using
556  * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
557  * the tunnel.
558  *
559  * @param cls closure (set from GNUNET_MESH_connect)
560  * @param tunnel connection to the other end (henceforth invalid)
561  * @param tunnel_ctx place where local state associated
562  *                   with the tunnel is stored
563  */
564 void
565 mesh_tunnel_end_handler (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
566                          void *tunnel_ctx)
567 {
568   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh tunnel end handler.\n");
569 }
570
571
572 /**
573  * Method called whenever a peer has disconnected from the tunnel.
574  * Implementations of this callback must NOT call
575  * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
576  * to run in some other task later.  However, calling
577  * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
578  *
579  * @param cls closure
580  * @param peer_id peer identity the tunnel stopped working with
581  */
582 void
583 mesh_peer_disconnect_handler (void *cls,
584                               const struct GNUNET_PeerIdentity * peer_id)
585 {
586   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer disconnect handler.\n");
587 }
588
589
590 /**
591  * Method called whenever a peer has connected to the tunnel.
592  *
593  * @param cls closure
594  * @param peer_id peer identity the tunnel was created to, NULL on timeout
595  * @param atsi performance data for the connection
596  *
597  */
598 void
599 mesh_peer_connect_handler (void *cls,
600                            const struct GNUNET_PeerIdentity* peer_id,
601                            const struct GNUNET_ATS_Information * atsi)
602 {
603   struct RegexPeer *peer = cls;
604
605   peers_found++;
606
607   if (NULL == peer_id)
608   {
609     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
610                 "String matching timed out for string %s on peer %u (%i/%i)\n",
611                 peer->search_str, peer->id, peers_found, num_search_strings);
612   }
613   else
614   {
615     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616                 "String %s successfully matched on peer %u (%i/%i)\n",
617                 peer->search_str, peer->id, peers_found, num_search_strings);
618
619     GNUNET_STATISTICS_get (peer->stats_handle, "mesh", NULL,
620                            GNUNET_TIME_UNIT_FOREVER_REL,
621                            &stats_cont_cb,
622                            &stats_iterator, peer);
623   }
624
625   if (peers_found == num_search_strings)
626   {
627
628     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
629     printf ("\nAll strings successfully matched in %.2f minutes\n", ((double)prof_time.rel_value / 1000.0 / 60.0));
630
631     if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
632       GNUNET_SCHEDULER_cancel (search_timeout_task);
633     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
634   }
635 }
636
637
638 /**
639  * Connect by string timeout task
640  *
641  * @param cls NULL
642  * @param tc the task context
643  */
644 static void
645 do_connect_by_string_timeout (void *cls,
646                               const struct GNUNET_SCHEDULER_TaskContext * tc)
647 {
648   printf ("Searching for all strings did not succeed after %s.\n",
649           GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
650   printf ("Found %i of %i strings\n", peers_found, num_search_strings);
651
652   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
653 }
654
655
656 /**
657  * Connect by string task that is run to search for a string in the NFA
658  *
659  * @param cls NULL
660  * @param tc the task context
661  */
662 static void
663 do_connect_by_string (void *cls,
664                       const struct GNUNET_SCHEDULER_TaskContext * tc)
665 {
666   unsigned int search_cnt;
667   struct RegexPeer *peer;
668
669   for (search_cnt = 0; search_cnt < num_search_strings; search_cnt++)
670   {
671     peer = &peers[search_cnt % num_peers];
672     peer->search_str = search_strings[search_cnt];
673
674     printf ("Searching for string \"%s\" on peer %d with file %s\n",
675             peer->search_str, (search_cnt % num_peers), peer->policy_file);
676
677     peer->mesh_tunnel_handle = GNUNET_MESH_tunnel_create (peer->mesh_handle,
678                                                           NULL,
679                                                           &mesh_peer_connect_handler,
680                                                           &mesh_peer_disconnect_handler,
681                                                           peer);
682
683     GNUNET_MESH_peer_request_connect_by_string (peer->mesh_tunnel_handle,
684                                                 peer->search_str);
685   }
686
687   prof_start_time = GNUNET_TIME_absolute_get ();
688
689   search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout,
690                                                       &do_connect_by_string_timeout, NULL);
691 }
692
693
694 /**
695  * Mesh connect callback.
696  *
697  * @param cls internal peer id.
698  * @param op operation handle.
699  * @param ca_result connect adapter result.
700  * @param emsg error message.
701  */
702 void
703 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
704                  void *ca_result, const char *emsg)
705 {
706   static unsigned int connected_mesh_handles;
707   struct RegexPeer *peer = (struct RegexPeer *) cls;
708   char *regex;
709   char *data;
710   char *buf;
711   uint64_t filesize;
712   unsigned int offset;
713
714   if (NULL != emsg)
715   {
716     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
717     GNUNET_assert (0);
718   }
719
720   GNUNET_assert (peer->mesh_op_handle == op);
721   GNUNET_assert (peer->mesh_handle == ca_result);
722   GNUNET_assert (NULL != peer->policy_file);
723
724   printf ("Announcing regexes for peer with file %s\n", peer->policy_file);
725   fflush (stdout);
726
727   if (GNUNET_YES != GNUNET_DISK_file_test (peer->policy_file))
728   {
729     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
730                 "Could not find policy file %s\n", peer->policy_file);
731     return;
732   }
733   if (GNUNET_OK != GNUNET_DISK_file_size (peer->policy_file, &filesize, GNUNET_YES, GNUNET_YES))
734     filesize = 0;
735   if (0 == filesize)
736   {
737     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", peer->policy_file);
738     return;
739   }
740   data = GNUNET_malloc (filesize);
741   if (filesize != GNUNET_DISK_fn_read (peer->policy_file, data, filesize))
742   {
743     GNUNET_free (data);
744     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read policy file %s.\n",
745          peer->policy_file);
746     return;
747   }
748   buf = data;
749   offset = 0;
750   regex = NULL;
751   while (offset < (filesize - 1))
752   {
753     offset++;
754     if (((data[offset] == '\n')) && (buf != &data[offset]))
755     {
756       data[offset] = '\0';
757       regex = buf;
758       GNUNET_assert (NULL != regex);
759       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s on peer\n", regex);
760       GNUNET_MESH_announce_regex (peer->mesh_handle, regex);
761       buf = &data[offset + 1];
762     }
763     else if ((data[offset] == '\n') || (data[offset] == '\0'))
764       buf = &data[offset + 1];
765   }
766   GNUNET_free (data);
767
768   if (++connected_mesh_handles == num_peers)
769   {
770     printf ("\nAll mesh handles connected.\nWaiting %s before starting to search.\n",
771             GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_YES));
772
773     search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
774                                                 &do_connect_by_string, NULL);
775   }
776 }
777
778
779 /**
780  * Mesh connect adapter.
781  *
782  * @param cls not used.
783  * @param cfg configuration handle.
784  *
785  * @return
786  */
787 void *
788 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
789 {
790   GNUNET_MESH_ApplicationType app;
791   struct RegexPeer *peer = cls;
792
793   static struct GNUNET_MESH_MessageHandler handlers[] = {
794     {NULL, 0, 0}
795   };
796
797   app = (GNUNET_MESH_ApplicationType)0;
798
799   peer->mesh_handle =
800     GNUNET_MESH_connect (cfg, cls, NULL, NULL, handlers, &app);
801
802   return peer->mesh_handle;
803 }
804
805
806 /**
807  * Adapter function called to destroy a connection to
808  * the mesh service
809  *
810  * @param cls closure
811  * @param op_result service handle returned from the connect adapter
812  */
813 void
814 mesh_da (void *cls, void *op_result)
815 {
816   struct RegexPeer *peer = (struct RegexPeer *) cls;
817
818   GNUNET_assert (peer->mesh_handle == op_result);
819
820   if (NULL != peer->mesh_tunnel_handle)
821   {
822     GNUNET_MESH_tunnel_destroy (peer->mesh_tunnel_handle);
823     peer->mesh_tunnel_handle = NULL;
824   }
825
826   if (NULL != peer->mesh_handle)
827   {
828     GNUNET_MESH_disconnect (peer->mesh_handle);
829     peer->mesh_handle = NULL;
830   }
831 }
832
833
834 /******************************************************************************/
835 /***************************  TESTBED PEER SETUP  *****************************/
836 /******************************************************************************/
837
838
839 /**
840  * Functions of this signature are called when a peer has been successfully
841  * started or stopped.
842  *
843  * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
844  * @param emsg NULL on success; otherwise an error description
845  */
846 static void
847 peer_churn_cb (void *cls, const char *emsg)
848 {
849   struct DLLOperation *dll_op = cls;
850   struct GNUNET_TESTBED_Operation *op;
851   static unsigned int started_peers;
852   unsigned int peer_cnt;
853
854   op = dll_op->op;
855   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
856   GNUNET_free (dll_op);
857   if (NULL != emsg)
858   {
859     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
860          _("An operation has failed while starting peers\n"));
861     GNUNET_TESTBED_operation_done (op);
862     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
863       GNUNET_SCHEDULER_cancel (abort_task);
864     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
865     return;
866   }
867   GNUNET_TESTBED_operation_done (op);
868   if (++started_peers == num_peers)
869   {
870     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
871     printf ("All peers started successfully in %.2f seconds\n",
872             ((double) prof_time.rel_value) / 1000.00);
873     result = GNUNET_OK;
874
875     if (0 == num_links)
876       num_links = num_peers * 5;
877
878     peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
879     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
880       peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
881
882     state = STATE_PEERS_LINKING;
883     /* Do overlay connect */
884     prof_start_time = GNUNET_TIME_absolute_get ();
885     topology_op =
886         GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
887                                                    GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
888                                                    num_links,
889                                                    GNUNET_TESTBED_TOPOLOGY_OPTION_END);
890     if (NULL == topology_op)
891     {
892       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
893                   "Cannot create topology, op handle was NULL\n");
894       GNUNET_assert (0);
895     }
896   }
897 }
898
899
900 /**
901  * Functions of this signature are called when a peer has been successfully
902  * created
903  *
904  * @param cls the closure from GNUNET_TESTBED_peer_create()
905  * @param peer the handle for the created peer; NULL on any error during
906  *          creation
907  * @param emsg NULL if peer is not NULL; else MAY contain the error description
908  */
909 static void
910 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
911 {
912   struct DLLOperation *dll_op = cls;
913   struct RegexPeer *peer_ptr;
914   static unsigned int created_peers;
915   unsigned int peer_cnt;
916
917   if (NULL != emsg)
918   {
919     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
920          _("Creating a peer failed. Error: %s\n"), emsg);
921     GNUNET_TESTBED_operation_done (dll_op->op);
922     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
923     GNUNET_free (dll_op);
924     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
925       GNUNET_SCHEDULER_cancel (abort_task);
926     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
927     return;
928   }
929
930   peer_ptr = dll_op->cls;
931   GNUNET_assert (NULL == peer_ptr->peer_handle);
932   peer_ptr->peer_handle = peer;
933   GNUNET_TESTBED_operation_done (dll_op->op);
934   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
935   GNUNET_free (dll_op);
936
937   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
938               peer_ptr->id,
939               GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
940
941   if (++created_peers == num_peers)
942   {
943     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
944     printf ("All peers created successfully in %.2f seconds\n",
945             ((double) prof_time.rel_value) / 1000.00);
946     /* Now peers are to be started */
947     state = STATE_PEERS_STARTING;
948     prof_start_time = GNUNET_TIME_absolute_get ();
949     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
950     {
951       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
952       dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
953                                               &peer_churn_cb, dll_op);
954       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
955     }
956   }
957 }
958
959 /**
960  * Function called with a filename.
961  *
962  * @param cls closure
963  * @param filename complete filename (absolute path)
964  * @return GNUNET_OK to continue to iterate,
965  *  GNUNET_SYSERR to abort iteration with error!
966  */
967 int
968 policy_filename_cb (void *cls, const char *filename)
969 {
970   static unsigned int peer_cnt;
971   struct DLLOperation *dll_op;
972
973   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer %i on host %s for policy file %s\n",
974               peer_cnt,
975               GNUNET_TESTBED_host_get_hostname (hosts[peer_cnt % num_hosts]),
976               filename);
977
978   peers[peer_cnt].id = peer_cnt;
979   peers[peer_cnt].policy_file = GNUNET_strdup (filename);
980   peers[peer_cnt].host_handle = hosts[peer_cnt % num_hosts];
981   peers[peer_cnt].mesh_handle = NULL;
982   peers[peer_cnt].mesh_tunnel_handle = NULL;
983   peers[peer_cnt].stats_handle = NULL;
984   peers[peer_cnt].stats_op_handle = NULL;
985
986   dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
987   dll_op->cls = &peers[peer_cnt];
988   dll_op->op = GNUNET_TESTBED_peer_create (mc,
989                                            hosts[peer_cnt % num_hosts],
990                                            cfg,
991                                            &peer_create_cb,
992                                            dll_op);
993   GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
994   peer_cnt++;
995
996   return GNUNET_OK;
997 }
998
999
1000 /**
1001  * Controller event callback.
1002  *
1003  * @param cls NULL
1004  * @param event the controller event
1005  */
1006 static void
1007 controller_event_cb (void *cls,
1008                      const struct GNUNET_TESTBED_EventInformation *event)
1009 {
1010   struct DLLOperation *dll_op;
1011   struct GNUNET_TESTBED_Operation *op;
1012
1013   switch (state)
1014   {
1015   case STATE_SLAVES_STARTING:
1016     switch (event->type)
1017     {
1018     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1019       {
1020         static unsigned int slaves_started;
1021
1022         dll_op = event->details.operation_finished.op_cls;
1023         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1024         GNUNET_free (dll_op);
1025         op = event->details.operation_finished.operation;
1026         if (NULL != event->details.operation_finished.emsg)
1027         {
1028           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1029                _("An operation has failed while starting slaves\n"));
1030           GNUNET_TESTBED_operation_done (op);
1031           if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1032             GNUNET_SCHEDULER_cancel (abort_task);
1033           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1034           return;
1035         }
1036         GNUNET_TESTBED_operation_done (op);
1037         /* Proceed to start peers */
1038         if (++slaves_started == num_hosts - 1)
1039         {
1040           printf ("All slaves started successfully\n");
1041
1042           state = STATE_PEERS_CREATING;
1043           prof_start_time = GNUNET_TIME_absolute_get ();
1044
1045           num_peers = GNUNET_DISK_directory_scan (policy_dir,
1046                                                   NULL,
1047                                                   NULL);
1048           peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1049
1050           GNUNET_DISK_directory_scan (policy_dir,
1051                                       &policy_filename_cb,
1052                                       NULL);
1053         }
1054       }
1055       break;
1056     default:
1057       GNUNET_assert (0);
1058     }
1059     break;
1060   case STATE_PEERS_STARTING:
1061     switch (event->type)
1062     {
1063     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1064       /* Control reaches here when peer start fails */
1065     case GNUNET_TESTBED_ET_PEER_START:
1066       /* we handle peer starts in peer_churn_cb */
1067       break;
1068     default:
1069       GNUNET_assert (0);
1070     }
1071     break;
1072   case STATE_PEERS_LINKING:
1073    switch (event->type)
1074     {
1075     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1076       /* Control reaches here when a peer linking operation fails */
1077       if (NULL != event->details.operation_finished.emsg)
1078       {
1079         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1080              _("An operation has failed while linking\n"));
1081         retry_links++;
1082         if (++cont_fails > num_cont_fails)
1083         {
1084           printf ("\nAborting due to very high failure rate");
1085           if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1086             GNUNET_SCHEDULER_cancel (abort_task);
1087           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1088         }
1089       }
1090       break;
1091     case GNUNET_TESTBED_ET_CONNECT:
1092       {
1093         static unsigned int established_links;
1094         unsigned int peer_cnt;
1095
1096         if (0 == established_links)
1097           printf ("Establishing links\n .");
1098         else
1099         {
1100           printf (".");
1101           fflush (stdout);
1102         }
1103         if (++established_links == num_links)
1104         {
1105           prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1106           printf ("\n%u links established in %.2f seconds\n",
1107                   num_links, ((double) prof_time.rel_value) / 1000.00);
1108           result = GNUNET_OK;
1109           GNUNET_free (peer_handles);
1110           printf ("\nConnecting to mesh and statistics service...\n");
1111           for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1112           {
1113             peers[peer_cnt].mesh_op_handle =
1114               GNUNET_TESTBED_service_connect (NULL,
1115                                               peers[peer_cnt].peer_handle,
1116                                               "mesh",
1117                                               &mesh_connect_cb,
1118                                               &peers[peer_cnt],
1119                                               &mesh_ca,
1120                                               &mesh_da,
1121                                               &peers[peer_cnt]);
1122
1123             peers[peer_cnt].stats_op_handle =
1124               GNUNET_TESTBED_service_connect (NULL,
1125                                               peers[peer_cnt].peer_handle,
1126                                               "statistics",
1127                                               &stats_connect_cb,
1128                                               &peers[peer_cnt],
1129                                               &stats_ca,
1130                                               &stats_da,
1131                                               &peers[peer_cnt]);
1132           }
1133         }
1134       }
1135       break;
1136     default:
1137       GNUNET_assert (0);
1138     }
1139     break;
1140   default:
1141     GNUNET_assert (0);
1142   }
1143 }
1144
1145
1146 /**
1147  * Task to register all hosts available in the global host list
1148  *
1149  * @param cls NULL
1150  * @param tc the scheduler task context
1151  */
1152 static void
1153 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1154
1155
1156 /**
1157  * Callback which will be called to after a host registration succeeded or failed
1158  *
1159  * @param cls the closure
1160  * @param emsg the error message; NULL if host registration is successful
1161  */
1162 static void
1163 host_registration_completion (void *cls, const char *emsg)
1164 {
1165   reg_handle = NULL;
1166   if (NULL != emsg)
1167   {
1168     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1169                 _("Host registration failed for a host. Error: %s\n"), emsg);
1170     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1171       GNUNET_SCHEDULER_cancel (abort_task);
1172     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1173     return;
1174   }
1175   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
1176 }
1177
1178
1179 /**
1180  * Task to register all hosts available in the global host list
1181  *
1182  * @param cls NULL
1183  * @param tc the scheduler task context
1184  */
1185 static void
1186 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1187 {
1188   struct DLLOperation *dll_op;
1189   static unsigned int reg_host;
1190   unsigned int slave;
1191
1192   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1193   if (reg_host == num_hosts - 1)
1194   {
1195     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196                 "All hosts successfully registered\n");
1197     /* Start slaves */
1198     state = STATE_SLAVES_STARTING;
1199     for (slave = 1; slave < num_hosts; slave++)
1200     {
1201       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1202       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1203                                                    mc,
1204                                                    hosts[slave],
1205                                                    hosts[0],
1206                                                    cfg,
1207                                                    GNUNET_YES);
1208       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1209     }
1210     return;
1211   }
1212   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1213                                              host_registration_completion,
1214                                              NULL);
1215 }
1216
1217
1218 /**
1219  * Callback to signal successfull startup of the controller process
1220  *
1221  * @param cls the closure from GNUNET_TESTBED_controller_start()
1222  * @param config the configuration with which the controller has been started;
1223  *          NULL if status is not GNUNET_OK
1224  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1225  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1226  */
1227 static void
1228 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1229 {
1230   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1231     GNUNET_SCHEDULER_cancel (abort_task);
1232   if (GNUNET_OK != status)
1233   {
1234     mc_proc = NULL;
1235     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1236     return;
1237   }
1238   event_mask = 0;
1239   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1240   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1241   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1242   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1243   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1244   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1245                                           &controller_event_cb, NULL);
1246   if (NULL == mc)
1247   {
1248     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1249                 _("Unable to connect to master controller -- Check config\n"));
1250     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1251     return;
1252   }
1253   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
1254   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1255                                              &do_abort, NULL);
1256 }
1257
1258 /**
1259  * Load search strings from given filename. One search string per line.
1260  *
1261  * @param filename filename of the file containing the search strings.
1262  * @param strings set of strings loaded from file. Caller needs to free this
1263  *                if number returned is greater than zero.
1264  * @return number of strings found in the file. GNUNET_SYSERR on error.
1265  */
1266 static int
1267 load_search_strings (const char *filename, char ***strings)
1268 {
1269   char *data;
1270   char *buf;
1271   uint64_t filesize;
1272   unsigned int offset;
1273   int str_cnt;
1274   unsigned int i;
1275
1276   if (NULL == filename)
1277   {
1278     return GNUNET_SYSERR;
1279   }
1280
1281   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1282   {
1283     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284                 "Could not find search strings file %s\n", filename);
1285     return GNUNET_SYSERR;
1286   }
1287   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1288     filesize = 0;
1289   if (0 == filesize)
1290   {
1291     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1292     return GNUNET_SYSERR;
1293   }
1294   data = GNUNET_malloc (filesize);
1295   if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1296   {
1297     GNUNET_free (data);
1298     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1299          filename);
1300     return GNUNET_SYSERR;
1301   }
1302   buf = data;
1303   offset = 0;
1304   str_cnt = 0;
1305   while (offset < (filesize - 1))
1306   {
1307     offset++;
1308     if (((data[offset] == '\n')) && (buf != &data[offset]))
1309     {
1310       data[offset] = '\0';
1311       str_cnt++;
1312       buf = &data[offset + 1];
1313     }
1314     else if ((data[offset] == '\n') || (data[offset] == '\0'))
1315       buf = &data[offset + 1];
1316   }
1317   *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1318   offset = 0;
1319   for (i = 0; i < str_cnt; i++)
1320   {
1321     (*strings)[i] = GNUNET_strdup (&data[offset]);
1322     offset += strlen ((*strings)[i]) + 1;
1323   }
1324   free (data);
1325   return str_cnt;
1326 }
1327
1328 /**
1329  * Main function that will be run by the scheduler.
1330  *
1331  * @param cls closure
1332  * @param args remaining command-line arguments
1333  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1334  * @param config configuration
1335  */
1336 static void
1337 run (void *cls, char *const *args, const char *cfgfile,
1338      const struct GNUNET_CONFIGURATION_Handle *config)
1339 {
1340   unsigned int nhost;
1341
1342   if (NULL == args[0])
1343   {
1344     fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
1345     return;
1346   }
1347   if (NULL == args[1])
1348   {
1349     fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
1350     return;
1351   }
1352   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
1353   if (0 == num_hosts)
1354   {
1355     fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
1356     return;
1357   }
1358   for (nhost = 0; nhost < num_hosts; nhost++)
1359   {
1360     if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
1361     {
1362       fprintf (stderr, _("Host %s cannot start testbed\n"),
1363                          GNUNET_TESTBED_host_get_hostname (hosts[nhost]));
1364       break;
1365     }
1366   }
1367   if (num_hosts != nhost)
1368   {
1369     fprintf (stderr, _("Exiting\n"));
1370     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1371     return;
1372   }
1373   if (NULL == config)
1374   {
1375     fprintf (stderr, _("No configuration file given. Exiting\n"));
1376     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1377     return;
1378   }
1379   if (GNUNET_YES != GNUNET_DISK_directory_test (args[1]))
1380   {
1381     fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
1382     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1383     return;
1384   }
1385   policy_dir = args[1];
1386   if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
1387   {
1388     fprintf (stderr, _("No search strings file given. Exiting.\n"));
1389     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1390     return;
1391   }
1392   num_search_strings = load_search_strings (args[2], &search_strings);
1393   if (0 >= num_search_strings || NULL == search_strings)
1394   {
1395     fprintf (stderr, _("Error loading search strings. Exiting.\n"));
1396     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1397     return;
1398   }
1399   unsigned int i;
1400   for (i = 0; i < num_search_strings; i++)
1401     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
1402   cfg = GNUNET_CONFIGURATION_dup (config);
1403   mc_proc =
1404       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1405                                        (hosts[0]),
1406                                        hosts[0],
1407                                        cfg,
1408                                        status_cb,
1409                                        NULL);
1410   abort_task =
1411       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1412                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
1413                                     NULL);
1414 }
1415
1416
1417 /**
1418  * Main function.
1419  *
1420  * @param argc argument count
1421  * @param argv argument values
1422  * @return 0 on success
1423  */
1424 int
1425 main (int argc, char *const *argv)
1426 {
1427   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1428     { 'n', "num-links", "COUNT",
1429       gettext_noop ("create COUNT number of random links"),
1430       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
1431     { 'e', "num-errors", "COUNT",
1432       gettext_noop ("tolerate COUNT number of continious timeout failures"),
1433       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
1434     { 't', "matching-timeout", "TIMEOUT",
1435       gettext_noop ("wait TIMEOUT seconds before considering a string match as failed"),
1436       GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout },
1437     { 's', "search-delay", "DELAY",
1438       gettext_noop ("wait DELAY minutes before starting string search"),
1439       GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
1440     GNUNET_GETOPT_OPTION_END
1441   };
1442   int ret;
1443
1444   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1445     return 2;
1446
1447   result = GNUNET_SYSERR;
1448   ret =
1449       GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
1450                           _("Profiler for regex/mesh"),
1451                           options, &run, NULL);
1452   if (GNUNET_OK != ret)
1453     return ret;
1454   if (GNUNET_OK != result)
1455     return 1;
1456   return 0;
1457 }