indentation
[oweals/gnunet.git] / src / transport / test_plugin_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 2, 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 transport/test_transport_api.c
22  * @brief testcase for transport_api.c
23  * @author Sailor Siraj
24  */
25
26 #include "platform.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_plugin_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_signatures.h"
36 #include "plugin_transport.h"
37 #include "transport.h"
38
39 /**
40  * Our public key.
41  */
42 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *my_public_key;
43
44 /**
45  * Our identity.
46  */
47 static struct GNUNET_PeerIdentity my_identity;
48
49 /**
50  * Our private key.
51  */
52 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
53
54 /**
55  * Our scheduler.
56  */
57 struct GNUNET_SCHEDULER_Handle *sched;
58
59 /**
60  * Our configuration.
61  */
62 struct GNUNET_CONFIGURATION_Handle *cfg;
63
64
65
66 /**
67  * All loaded plugins.
68  */
69 static struct TransportPlugin *plugins;
70
71 /**
72  * Our server.
73  */
74 static struct GNUNET_SERVER_Handle *server;
75
76
77
78 /**
79  * Number of neighbours we'd like to have.
80  */
81 static uint32_t max_connect_per_transport;
82
83 /**
84  * Environment for this plugin.
85  */
86 struct GNUNET_TRANSPORT_PluginEnvironment env;
87
88 /**
89  *handle for the api provided by this plugin
90  */
91 struct GNUNET_TRANSPORT_PluginFunctions *api;
92
93 /**
94  * Initialize Environment for this plugin
95  */
96 struct ReadyList * 
97 receive(void *cls,void *plugin_context,
98         struct ReadyList *
99         service_context,
100         struct GNUNET_TIME_Relative
101         latency,
102         const struct GNUNET_PeerIdentity
103         * peer,
104         const struct GNUNET_MessageHeader
105         * message)
106 {
107   return NULL;
108 }
109
110 void notify_address(void *cls,
111                     const char *name,
112                     const void *addr,
113                     size_t addrlen,
114                     struct
115                     GNUNET_TIME_Relative
116                     expires)
117 {
118 }
119
120 void lookup (void *cls,
121              struct GNUNET_TIME_Relative
122              timeout,
123              const struct
124              GNUNET_PeerIdentity * target,
125              GNUNET_TRANSPORT_AddressCallback
126              iter, void *iter_cls)
127 {       
128 }
129
130
131 static void setup_plugin_environment()
132 {
133   env.cfg  = cfg;
134   env.sched = sched;
135   env.my_public_key = my_public_key;
136   env.cls=&env;
137   env.receive=&receive;
138   env.lookup=&lookup;
139   env.notify_address=&notify_address;
140   env.max_connections = max_connect_per_transport;       
141 }       
142
143
144 /**
145  * Initiate transport service.
146  *
147  * @param cls closure
148  * @param s scheduler to use
149  * @param serv the initialized server
150  * @param c configuration to use
151  */
152 static void
153 run (void *cls,
154      struct GNUNET_SCHEDULER_Handle *s,
155      struct GNUNET_SERVER_Handle *serv, struct GNUNET_CONFIGURATION_Handle *c)
156
157   unsigned long long tneigh;
158   char *keyfile;
159   char *libname;
160
161   sched = s;
162   cfg = c;
163   server = serv;
164   /* parse configuration */
165   if ((GNUNET_OK !=
166        GNUNET_CONFIGURATION_get_value_number (c,
167                                               "TRANSPORT",
168                                               "NEIGHBOUR_LIMIT",
169                                               &tneigh)) ||
170       (GNUNET_OK !=
171        GNUNET_CONFIGURATION_get_value_filename (c,
172                                                 "GNUNETD",
173                                                 "HOSTKEY", &keyfile)))
174     {
175       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176                   _("Transport service is lacking key configuration settings.  Exiting.\n"));
177       GNUNET_SCHEDULER_shutdown (s);
178       return;
179     }
180   max_connect_per_transport = (uint32_t) tneigh;
181   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
182   GNUNET_free (keyfile);
183   if (my_private_key == NULL)
184     {
185       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186                   _("Transport service could not access hostkey.  Exiting.\n"));
187       GNUNET_SCHEDULER_shutdown (s);
188       return;
189     }
190   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
191   GNUNET_CRYPTO_hash (&my_public_key,
192                       sizeof (my_public_key), &my_identity.hashPubKey);
193   
194
195   
196   /* load plugins... */  
197   setup_plugin_environment();
198   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
199               _("Loading tcp transport plugin\n"));
200   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_tcp");
201
202   api = GNUNET_PLUGIN_load(libname, &env);
203   if (api == NULL)
204     {
205       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206                   _("Failed to load transport plugin for tcp\n"));
207     } 
208   
209 }
210
211
212 /**
213  * Function called when the service shuts
214  * down.  Unloads our plugins.
215  *
216  * @param cls closure
217  * @param cfg configuration to use
218  */
219 static void
220 unload_plugins (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
221 {  
222   GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_tcp",api));
223   if (my_private_key != NULL)
224     GNUNET_CRYPTO_rsa_key_free (my_private_key);
225   
226 }
227
228
229 /**
230  * The main function for the transport service.
231  *
232  * @param argc number of arguments from the command line
233  * @param argv command line arguments
234  * @return 0 ok, 1 on error
235  */
236 int
237 main (int argc, char *const *argv)
238 {
239   GNUNET_log_setup ("test-puglin-transport",
240 #if VERBOSE
241                     "DEBUG",
242 #else
243                     "WARNING",
244 #endif
245                     NULL);       
246   return (GNUNET_OK ==
247           GNUNET_SERVICE_run (argc,
248                               argv,
249                               "transport",
250                               &run, NULL, &unload_plugins, NULL)) ? 0 : 1;
251 }
252
253 /* end of test_plugin_transport.c */