-no peerinfo IO on tests
[oweals/gnunet.git] / src / core / gnunet-service-core.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 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 core/gnunet-service-core.c
23  * @brief high-level P2P messaging
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include <gcrypt.h>
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-core.h"
30 #include "gnunet-service-core_clients.h"
31 #include "gnunet-service-core_kx.h"
32 #include "gnunet-service-core_neighbours.h"
33 #include "gnunet-service-core_sessions.h"
34 #include "gnunet-service-core_typemap.h"
35
36
37 /**
38  * Our identity.
39  */
40 struct GNUNET_PeerIdentity GSC_my_identity;
41
42 /**
43  * Our configuration.
44  */
45 const struct GNUNET_CONFIGURATION_Handle *GSC_cfg;
46
47 /**
48  * For creating statistics.
49  */
50 struct GNUNET_STATISTICS_Handle *GSC_stats;
51
52 /**
53  * Handle to the server of the core service.
54  */
55 static struct GNUNET_SERVER_Handle *GSC_server;
56
57 /**
58  * Hostkey generation context
59  */
60 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
61
62
63 /**
64  * Last task run during shutdown.  Disconnects us from
65  * the transport.
66  * 
67  * @param cls NULL, unused
68  * @param tc scheduler context, unused
69  */
70 static void
71 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
72 {
73   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core service shutting down.\n");
74   if (NULL != keygen)
75   {
76     GNUNET_CRYPTO_ecc_key_create_stop (keygen);
77     keygen = NULL;
78   }
79   GSC_CLIENTS_done ();
80   GSC_NEIGHBOURS_done ();
81   GSC_SESSIONS_done ();
82   GSC_KX_done ();
83   GSC_TYPEMAP_done ();
84   if (NULL != GSC_stats)
85   {
86     GNUNET_STATISTICS_destroy (GSC_stats, GNUNET_NO);
87     GSC_stats = NULL;
88   }
89   GSC_cfg = NULL;
90 }
91
92
93
94 /**
95  * Callback for hostkey read/generation
96  *
97  * @param cls NULL
98  * @param pk the private key
99  * @param emsg error message
100  */
101 static void
102 key_generation_cb (void *cls,
103                    struct GNUNET_CRYPTO_EccPrivateKey *pk,
104                    const char *emsg)
105 {
106   keygen = NULL;
107   if (NULL == pk)
108   {
109     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110                 _("Failed to read or generate private key: %s\n"),
111                 emsg);
112     GNUNET_SCHEDULER_shutdown ();
113     return;
114   }
115   if ((GNUNET_OK != GSC_KX_init (pk)) || 
116       (GNUNET_OK != GSC_NEIGHBOURS_init ()))
117   {
118     GNUNET_SCHEDULER_shutdown ();
119     return;
120   }
121   GSC_SESSIONS_init ();
122   GSC_CLIENTS_init (GSC_server);
123   GNUNET_SERVER_resume (GSC_server);
124   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Core service of `%4s' ready.\n"),
125               GNUNET_i2s (&GSC_my_identity));
126 }
127
128
129 /**
130  * Initiate core service.
131  *
132  * @param cls closure
133  * @param server the initialized server
134  * @param c configuration to use
135  */
136 static void
137 run (void *cls, struct GNUNET_SERVER_Handle *server,
138      const struct GNUNET_CONFIGURATION_Handle *c)
139 {
140   char *keyfile;
141
142   GSC_cfg = c;
143   GSC_server = server;
144   if (GNUNET_OK !=
145       GNUNET_CONFIGURATION_get_value_filename (GSC_cfg, "PEER", "PRIVATE_KEY",
146                                                &keyfile))
147   {
148     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149                 _
150                 ("Core service is lacking HOSTKEY configuration setting.  Exiting.\n"));
151     GNUNET_SCHEDULER_shutdown ();
152     return;
153   }
154   GSC_stats = GNUNET_STATISTICS_create ("core", GSC_cfg);
155   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
156                                 NULL);
157   GNUNET_SERVER_suspend (server);
158   GSC_TYPEMAP_init ();
159   keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile, &key_generation_cb, NULL);
160   GNUNET_free (keyfile);
161   if (NULL == keygen)
162   {
163     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164                 _("Transport service is unable to access hostkey. Exiting.\n"));
165     GNUNET_SCHEDULER_shutdown ();
166   }
167 }
168
169
170 /**
171  * The main function for the transport service.
172  *
173  * @param argc number of arguments from the command line
174  * @param argv command line arguments
175  * @return 0 ok, 1 on error
176  */
177 int
178 main (int argc, char *const *argv)
179 {
180   return (GNUNET_OK ==
181           GNUNET_SERVICE_run (argc, argv, "core", GNUNET_SERVICE_OPTION_NONE,
182                               &run, NULL)) ? 0 : 1;
183 }
184
185 /* end of gnunet-service-core.c */