cb wrapper for connecting peers
[oweals/gnunet.git] / src / transport / gnunet-service-transport-new.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 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 transport/gnunet-service-transport-new.c
23  * @brief 
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet-service-transport.h"
32 #include "gnunet-service-transport_ats-new.h"
33 #include "gnunet-service-transport_blacklist.h"
34 #include "gnunet-service-transport_clients.h"
35 #include "gnunet-service-transport_hello.h"
36 #include "gnunet-service-transport_neighbours.h"
37 #include "gnunet-service-transport_plugins.h"
38 #include "gnunet-service-transport_validation.h"
39
40 /* globals */
41
42 /**
43  * Statistics handle.
44  */
45 struct GNUNET_STATISTICS_Handle *GST_stats;
46
47 /**
48  * Configuration handle.
49  */
50 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
51
52 /**
53  * Configuration handle.
54  */
55 struct GNUNET_PeerIdentity GST_my_identity;
56
57 /**
58  * Handle to peerinfo service.
59  */
60 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
61
62 /**
63  * Our public key.
64  */
65 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded GST_my_public_key;
66
67 /**
68  * Our private key.
69  */
70 struct GNUNET_CRYPTO_RsaPrivateKey *GST_my_private_key;
71
72 /**
73  * ATS handle.
74  */
75 struct GST_AtsHandle *GST_ats;
76
77
78 /**
79  * My HELLO has changed. Tell everyone who should know.
80  *
81  * @param cls unused
82  * @param hello new HELLO
83  */
84 static void
85 process_hello_update (void *cls,
86                       const struct GNUNET_MessageHeader *hello)
87 {
88   GST_clients_broadcast (hello, GNUNET_NO);
89 #if 0
90   GNUNET_CONTAINER_multihashmap_iterate (neighbours,
91                                          &transmit_our_hello_if_pong,
92                                          NULL);
93 #endif
94 }
95
96
97 /**
98  * Function that will be called for each address the transport
99  * is aware that it might be reachable under.  Update our HELLO.
100  *
101  * @param cls name of the plugin (const char*)
102  * @param add_remove should the address added (YES) or removed (NO) from the
103  *                   set of valid addresses?
104  * @param addr one of the addresses of the host
105  *        the specific address format depends on the transport
106  * @param addrlen length of the address
107  */
108 static void 
109 plugin_env_address_change_notification (void *cls,
110                                         int add_remove,
111                                         const void *addr,
112                                         size_t addrlen)
113 {
114   const char *plugin_name = cls;
115
116   GST_hello_modify_addresses (add_remove,
117                               plugin_name,
118                               addr,
119                               addrlen);
120 }
121
122
123 /**
124  * Function called when the service shuts down.  Unloads our plugins
125  * and cancels pending validations.
126  *
127  * @param cls closure, unused
128  * @param tc task context (unused)
129  */
130 static void
131 shutdown_task (void *cls, 
132                const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {  
134   GST_validation_stop ();
135   GST_neighbours_stop ();
136   GST_ats_shutdown (GST_ats); GST_ats = NULL;
137   GST_clients_stop ();
138   GST_blacklist_stop ();
139   GST_plugins_unload ();
140   GST_hello_stop ();
141
142   if (GST_peerinfo != NULL)
143     {
144       GNUNET_PEERINFO_disconnect (GST_peerinfo);
145       GST_peerinfo = NULL;
146     }
147   if (GST_stats != NULL)
148     {
149       GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
150       GST_stats = NULL;
151     }  
152   if (GST_my_private_key != NULL)
153     {
154       GNUNET_CRYPTO_rsa_key_free (GST_my_private_key);
155       GST_my_private_key = NULL;
156     }
157 }
158
159
160 /**
161  * Initiate transport service.
162  *
163  * @param cls closure
164  * @param server the initialized server
165  * @param c configuration to use
166  */
167 static void
168 run (void *cls,
169      struct GNUNET_SERVER_Handle *server,
170      const struct GNUNET_CONFIGURATION_Handle *c)
171 {
172 #if 0
173   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
174     {NULL, NULL, 0, 0}
175   };
176 #endif
177   char *keyfile;
178
179   /* setup globals */
180   GST_cfg = c;
181   if (GNUNET_OK !=
182       GNUNET_CONFIGURATION_get_value_filename (c,
183                                                "GNUNETD",
184                                                "HOSTKEY", &keyfile))
185     {
186       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187                   _
188                   ("Transport service is lacking key configuration settings.  Exiting.\n"));
189       GNUNET_SCHEDULER_shutdown ();
190       return;
191     }
192   GST_my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
193   GNUNET_free (keyfile);
194   if (GST_my_private_key == NULL)
195     {
196       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197                   _("Transport service could not access hostkey.  Exiting.\n"));
198       GNUNET_SCHEDULER_shutdown ();
199       return;
200     }
201   GST_stats = GNUNET_STATISTICS_create ("transport", c);
202   GST_peerinfo = GNUNET_PEERINFO_connect (c);
203   GNUNET_CRYPTO_rsa_key_get_public (GST_my_private_key, &GST_my_public_key);
204   GNUNET_CRYPTO_hash (&GST_my_public_key,
205                       sizeof (GST_my_public_key), &GST_my_identity.hashPubKey);
206   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
207                                 &shutdown_task, NULL);
208   if (GST_peerinfo == NULL)
209     {
210       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
211                   _("Could not access PEERINFO service.  Exiting.\n"));
212       GNUNET_SCHEDULER_shutdown ();
213       return;
214     }
215   
216   /* start subsystems */
217   GST_hello_start (&process_hello_update, NULL);
218   GST_blacklist_start (server);
219   GST_plugins_load (NULL,  // FIXME...
220                     &plugin_env_address_change_notification, 
221                     NULL, // FIXME...
222                     NULL, // FIXME...
223                     NULL); // FIXME...
224   GST_ats = GST_ats_init (GST_cfg,
225                           NULL, // FIXME...
226                           NULL); // FIXME...
227   GST_neighbours_start (NULL, // FIXME...
228                         NULL, // FIXME...
229                         NULL); // FIXME...
230   GST_clients_start (server);
231   GST_validation_start ();
232 }
233
234
235 /**
236  * The main function for the transport service.
237  *
238  * @param argc number of arguments from the command line
239  * @param argv command line arguments
240  * @return 0 ok, 1 on error
241  */
242 int
243 main (int argc, char *const *argv)
244 {
245   return (GNUNET_OK ==
246           GNUNET_SERVICE_run (argc,
247                               argv,
248                               "transport",
249                               GNUNET_SERVICE_OPTION_NONE,
250                               &run, NULL)) ? 0 : 1;
251 }
252
253 /* end of file gnunet-service-transport-new.c */