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