-fix use after free
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2013 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-service-mesh.c
23  * @brief GNUnet MESH service with encryption
24  * @author Bartlomiej Polot
25  *
26  *  FIXME in progress:
27  * - rekey - reliability interaction
28  * - channel retransmit timing
29  *
30  * TODO:
31  * - relay corking down to core
32  * - set ttl relative to path length
33  * TODO END
34  *
35  * Dictionary:
36  * - peer: other mesh instance. If there is direct connection it's a neighbor.
37  * - tunnel: encrypted connection to a peer, neighbor or not.
38  * - channel: connection between two clients, on the same or different peers.
39  *            have properties like reliability.
40  * - path: series of directly connected peer from one peer to another.
41  * - connection: path which is being used in a tunnel.
42  */
43
44 #include "platform.h"
45 #include "gnunet_util_lib.h"
46 #include "mesh.h"
47 #include "gnunet_statistics_service.h"
48
49 #include "gnunet-service-mesh_local.h"
50 #include "gnunet-service-mesh_channel.h"
51 #include "gnunet-service-mesh_connection.h"
52 #include "gnunet-service-mesh_tunnel.h"
53 #include "gnunet-service-mesh_dht.h"
54 #include "gnunet-service-mesh_peer.h"
55
56
57 /******************************************************************************/
58 /***********************      GLOBAL VARIABLES     ****************************/
59 /******************************************************************************/
60
61 /****************************** Global variables ******************************/
62
63 /**
64  * Handle to the statistics service.
65  */
66 struct GNUNET_STATISTICS_Handle *stats;
67
68 /**
69  * Local peer own ID (memory efficient handle).
70  */
71 GNUNET_PEER_Id myid;
72
73 /**
74  * Local peer own ID (full value).
75  */
76 struct GNUNET_PeerIdentity my_full_id;
77
78 /*************************** Static global variables **************************/
79
80 /**
81  * Own private key.
82  */
83 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
84
85
86 /******************************************************************************/
87 /************************      MAIN FUNCTIONS      ****************************/
88 /******************************************************************************/
89
90 /**
91  * Task run during shutdown.
92  *
93  * @param cls unused
94  * @param tc unused
95  */
96 static void
97 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 {
99   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
100
101   GML_shutdown ();
102   GMD_shutdown ();
103   GMC_shutdown ();
104   GMT_shutdown ();
105   GMP_shutdown ();
106
107   GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
108   stats = NULL;
109   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
110 }
111
112
113 /**
114  * Process mesh requests.
115  *
116  * @param cls closure
117  * @param server the initialized server
118  * @param c configuration to use
119  */
120 static void
121 run (void *cls, struct GNUNET_SERVER_Handle *server,
122      const struct GNUNET_CONFIGURATION_Handle *c)
123 {
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
125
126   stats = GNUNET_STATISTICS_create ("mesh", c);
127
128   /* Scheduled the task to clean up when shutdown is called */
129   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
130                                 NULL);
131   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "reading key\n");
132   my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
133   GNUNET_assert (NULL != my_private_key);
134   GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_full_id.public_key);
135   myid = GNUNET_PEER_intern (&my_full_id);
136   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
137               "Mesh for peer [%s] starting\n",
138               GNUNET_i2s (&my_full_id));
139
140   GML_init (server);    /* Local clients */
141   GMC_init (c);         /* Connections */
142   GMP_init (c);         /* Peers */
143   GMD_init (c);         /* DHT */
144   GMT_init (c, my_private_key); /* Tunnels */
145
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
147 }
148
149
150 /**
151  * The main function for the mesh service.
152  *
153  * @param argc number of arguments from the command line
154  * @param argv command line arguments
155  * @return 0 ok, 1 on error
156  */
157 int
158 main (int argc, char *const *argv)
159 {
160   int ret;
161   int r;
162
163   r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
164                           NULL);
165   GNUNET_free (my_private_key);
166   ret = (GNUNET_OK == r) ? 0 : 1;
167
168   return ret;
169 }