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