- finished coarse refactoring
[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 /************************** Configuration parameters **************************/
76
77
78
79 /*************************** Static global variables **************************/
80
81 /**
82  * Handle to the statistics service.
83  */
84 static struct GNUNET_STATISTICS_Handle *stats;
85
86 /**
87  * Local peer own ID (memory efficient handle).
88  */
89 static GNUNET_PEER_Id myid;
90
91 /**
92  * Local peer own ID (full value).
93  */
94 static struct GNUNET_PeerIdentity my_full_id;
95
96 /**
97  * Own private key.
98  */
99 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
100
101
102 /******************************************************************************/
103 /***********************         DECLARATIONS        **************************/
104 /******************************************************************************/
105
106
107 /******************************************************************************/
108 /******************      GENERAL HELPER FUNCTIONS      ************************/
109 /******************************************************************************/
110
111
112 /**
113  * Get the static string for a peer ID.
114  *
115  * @param peer Peer.
116  *
117  * @return Static string for it's ID.
118  */
119 static const char *
120 peer2s (const struct MeshPeer *peer)
121 {
122   if (NULL == peer)
123     return "(NULL)";
124   return GNUNET_i2s (GNUNET_PEER_resolve2 (peer->id));
125 }
126
127
128
129 /**
130  * Send an ACK on the appropriate connection/channel, depending on
131  * the direction and the position of the peer.
132  *
133  * @param c Which connection to send the hop-by-hop ACK.
134  * @param ch Channel, if any.
135  * @param fwd Is this a fwd ACK? (will go dest->root)
136  */
137 static void
138 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
139 {
140   unsigned int buffer;
141
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143               "send ack %s on %p %p\n",
144               fwd ? "FWD" : "BCK", c, ch);
145   if (NULL == c || GMC_is_terminal (c, fwd))
146   {
147     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  getting from all connections\n");
148     buffer = tunnel_get_buffer (NULL == c ? ch->t : c->t, fwd);
149   }
150   else
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  getting from one connection\n");
153     buffer = connection_get_buffer (c, fwd);
154   }
155   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  buffer available: %u\n", buffer);
156
157   if ( (NULL != ch && channel_is_origin (ch, fwd)) ||
158        (NULL != c && connection_is_origin (c, fwd)) )
159   {
160     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  sending on channel...\n");
161     if (0 < buffer)
162     {
163       GNUNET_assert (NULL != ch);
164       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  really sending!\n");
165       send_local_ack (ch, fwd);
166     }
167   }
168   else if (NULL == c)
169   {
170     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  sending on all connections\n");
171     GNUNET_assert (NULL != ch);
172     channel_send_connections_ack (ch, buffer, fwd);
173   }
174   else
175   {
176     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  sending on connection\n");
177     connection_send_ack (c, buffer, fwd);
178   }
179 }
180
181
182
183
184
185
186
187
188
189
190 /******************************************************************************/
191 /****************      MESH NETWORK HANDLER HELPERS     ***********************/
192 /******************************************************************************/
193
194
195
196
197
198
199 /******************************************************************************/
200 /********************      MESH NETWORK HANDLERS     **************************/
201 /******************************************************************************/
202
203
204
205
206 /******************************************************************************/
207 /************************      MAIN FUNCTIONS      ****************************/
208 /******************************************************************************/
209
210
211 /**
212  * Task run during shutdown.
213  *
214  * @param cls unused
215  * @param tc unused
216  */
217 static void
218 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
219 {
220   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
221
222   GML_shutdown ();
223   GMD_shutdown ();
224   GMP_shutdown ();
225   GMC_shutdown ();
226   GMT_shutdown ();
227
228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
229 }
230
231
232 /**
233  * Process mesh requests.
234  *
235  * @param cls closure
236  * @param server the initialized server
237  * @param c configuration to use
238  */
239 static void
240 run (void *cls, struct GNUNET_SERVER_Handle *server,
241      const struct GNUNET_CONFIGURATION_Handle *c)
242 {
243   struct GNUNET_CRYPTO_EccPrivateKey *pk;
244
245   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
246
247   stats = GNUNET_STATISTICS_create ("mesh", c);
248
249   /* Scheduled the task to clean up when shutdown is called */
250   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
251                                 NULL);
252   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "reading key\n");
253   pk = GNUNET_CRYPTO_ecc_key_create_from_configuration (c);
254   GNUNET_assert (NULL != pk);
255   my_private_key = pk;
256   GNUNET_CRYPTO_ecc_key_get_public_for_signature (my_private_key,
257                                                   &my_full_id.public_key);
258   myid = GNUNET_PEER_intern (&my_full_id);
259   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
260               "Mesh for peer [%s] starting\n",
261               GNUNET_i2s (&my_full_id));
262
263   GML_init (server);    /* Local clients */
264   GMC_init (c);         /* Connections */
265   GMP_init (c);         /* Peers */
266   GMD_init (c, &my_full_id);         /* DHT */
267   GMT_init (c, &my_full_id, &my_private_key);
268
269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
270 }
271
272
273 /**
274  * The main function for the mesh service.
275  *
276  * @param argc number of arguments from the command line
277  * @param argv command line arguments
278  * @return 0 ok, 1 on error
279  */
280 int
281 main (int argc, char *const *argv)
282 {
283   int ret;
284   int r;
285
286   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
287   r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
288                           NULL);
289   ret = (GNUNET_OK == r) ? 0 : 1;
290   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
291
292   return ret;
293 }