fix
[oweals/gnunet.git] / src / transport / gnunet-transport.c
1 /*
2      This file is part of GNUnet.
3      (C) 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 /**
22  * @file src/transport/gnunet-transport.c
23  * @brief Tool to help configure the transports.
24  * @author Christian Grothoff
25  *
26  * This utility can be used to test if a transport mechanism for
27  * GNUnet is properly configured.
28  */
29
30 #include "platform.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_transport_service.h"
34
35 static char *cpid;
36
37 static struct GNUNET_TRANSPORT_Handle *handle;
38
39 static void
40 do_disconnect (void *cls,
41                const struct GNUNET_SCHEDULER_TaskContext *tc)
42 {
43   GNUNET_TRANSPORT_disconnect (handle);  
44 }
45
46
47 /**
48  * Main function that will be run by the scheduler.
49  *
50  * @param cls closure
51  * @param args remaining command-line arguments
52  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
53  * @param cfg configuration
54  */
55 static void
56 run (void *cls, char *const *args, const char *cfgfile,
57      const struct GNUNET_CONFIGURATION_Handle *cfg)
58 {
59   struct GNUNET_PeerIdentity pid;
60   if (NULL != cpid)
61   {
62     handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
63                                        NULL, NULL, NULL);
64     if (GNUNET_OK !=
65         GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
66     {
67       fprintf (stderr,
68                _("Failed to parse peer identity `%s'\n"),
69                cpid);
70       GNUNET_TRANSPORT_disconnect (handle);
71       return;
72     }
73     GNUNET_TRANSPORT_try_connect (handle, &pid);
74     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
75                                   &do_disconnect,
76                                   NULL);
77   }
78 }
79
80
81 int
82 main (int argc, char *const *argv)
83 {
84   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
85     {'C', "connect", "PEER",
86      gettext_noop ("try to connect to the given peer"),
87      1, &GNUNET_GETOPT_set_string, &cpid},
88     GNUNET_GETOPT_OPTION_END
89   };
90   return (GNUNET_OK ==
91           GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
92                               gettext_noop ("Direct access to transport service."),
93                               options, &run, NULL)) ? 0 : 1;
94 }
95
96
97 /* end of gnunet-transport.c */