Make mesh service compile
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001 - 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 mesh/gnunet-service-mesh.c
23  * @brief GNUnet MESH service
24  * @author Bartlomiej Polot
25  *
26  * TODO:
27  * - soft stateing (keep-alive (CHANGE?) / timeout / disconnect) -- not a message issue
28  * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
29  * - partial disconnect reporting -- same as error reporting?
30  * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
31  * - speed requirement specification (change?) in mesh API -- API call
32  */
33
34 #include "platform.h"
35 #include "gnunet_common.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_core_service.h"
38 #include "gnunet_protocols.h"
39 #include "mesh.h"
40
41
42 /******************************************************************************/
43 /********************      MESH NETWORK MESSAGES     **************************/
44 /******************************************************************************/
45
46 /**
47  * Message for mesh path management
48  */
49 struct GNUNET_MESH_ManipulatePath
50 {
51     /**
52      * Type: GNUNET_MESSAGE_TYPE_MESH_PATH_[CREATE|CHANGE|ADD|DEL]
53      *
54      * Size: sizeof(struct GNUNET_MESH_ManipulatePath) + path_length * sizeof (struct GNUNET_PeerIdentity)
55      */
56     struct GNUNET_MessageHeader header;
57
58     /**
59      * Id of the tunnel this path belongs to, unique in conjunction with the origin.
60      */
61     uint32_t tid GNUNET_PACKED;
62
63     /**
64      * Information about speed requirements.  If the tunnel cannot sustain the 
65      * minimum bandwidth, packets are to be dropped.
66      */
67     uint32_t speed_min GNUNET_PACKED;
68
69     /**
70      * path_length structs defining the *whole* path from the origin [0] to the
71      * final destination [path_length-1].
72      */
73   // struct GNUNET_PeerIdentity peers[path_length];
74 };
75
76 /**
77  * Message for mesh data traffic to all tunnel targets.
78  */
79 struct GNUNET_MESH_OriginMulticast
80 {
81     /**
82      * Type: GNUNET_MESSAGE_TYPE_DATA_MULTICAST
83      */
84     struct GNUNET_MessageHeader header;
85
86     /**
87      * TID of the tunnel
88      */
89     uint32_t tid GNUNET_PACKED;
90
91     /**
92      * OID of the tunnel
93      */
94     struct GNUNET_PeerIdentity oid;
95
96     /**
97      * FIXME: Some form of authentication
98      */
99     // uint32_t token;
100
101     /**
102      * Payload follows
103      */
104 };
105
106
107 /**
108  * Message for mesh data traffic to a particular destination from origin.
109  */
110 struct GNUNET_MESH_DataMessageFromOrigin
111 {
112     /**
113      * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_FROM_ORIGIN
114      */
115     struct GNUNET_MessageHeader header;
116
117     /**
118      * TID of the tunnel
119      */
120     uint32_t tid GNUNET_PACKED;
121
122     /**
123      * OID of the tunnel
124      */
125     struct GNUNET_PeerIdentity oid;
126
127     /**
128      * Destination.
129      */
130     struct GNUNET_PeerIdentity destination;
131
132     /**
133      * FIXME: Some form of authentication
134      */
135     // uint32_t token;
136
137     /**
138      * Payload follows
139      */
140 };
141
142
143 /**
144  * Message for mesh data traffic from a tunnel participant to origin.
145  */
146 struct GNUNET_MESH_DataMessageToOrigin
147 {
148     /**
149      * Type: GNUNET_MESSAGE_TYPE_DATA_MESSAGE_TO_ORIGIN
150      */
151     struct GNUNET_MessageHeader header;
152
153     /**
154      * TID of the tunnel
155      */
156     uint32_t tid GNUNET_PACKED;
157
158     /**
159      * OID of the tunnel
160      */
161     struct GNUNET_PeerIdentity oid;
162
163     /**
164      * Sender of the message.
165      */
166     struct GNUNET_PeerIdentity sender;
167
168     /**
169      * FIXME: Some form of authentication
170      */
171     // uint32_t token;
172
173     /**
174      * Payload follows
175      */
176 };
177
178 /**
179  * Message for mesh flow control
180  */
181 struct GNUNET_MESH_SpeedNotify
182 {
183     /**
184      * Type: GNUNET_MESSAGE_TYPE_DATA_SPEED_NOTIFY
185      */
186     struct GNUNET_MessageHeader header;
187
188     /**
189      * TID of the tunnel
190      */
191     uint32_t tid GNUNET_PACKED;
192
193     /**
194      * OID of the tunnel
195      */
196     struct GNUNET_PeerIdentity oid;
197
198     /**
199      * Slowest link down the path (above minimum speed requirement).
200      */
201     uint32_t speed_min;
202
203 };
204
205 /******************************************************************************/
206 /************************      DATA STRUCTURES     ****************************/
207 /******************************************************************************/
208
209 /**
210  * All the states a peer participating in a tunnel can be in.
211  */
212 enum PeerState
213 {
214     /**
215      * Request sent, not yet answered.
216      */
217     MESH_PEER_WAITING,
218
219     /**
220      * Peer connected and ready to accept data
221      */
222     MESH_PEER_READY,
223
224     /**
225      * Peer connected previosly but not responding
226      */
227     MESH_PEER_UNAVAILABLE,
228
229     /**
230      * Peer requested but not ever connected
231      */
232     MESH_PEER_UNREACHABLE
233 };
234
235 /**
236  * Struct containing all information regarding a given peer
237  */
238 struct PeerInfo
239 {
240     /**
241      * ID of the peer
242      */
243     struct GNUNET_PeerIdentity id;
244
245     /**
246      * Is the peer reachable? Is the peer even connected?
247      */
248     enum PeerState state;
249
250     /**
251      * Who to send the data to
252      */
253     uint32_t first_hop;
254
255     /**
256      * Max data rate to this peer
257      */
258     uint32_t max_speed;
259 };
260
261 /**
262  * Information regarding a path
263  */
264 struct Path
265 {
266     /**
267      * Id of the path, in case it's needed
268      */
269     uint32_t id;
270
271     /**
272      * Whether the path is serving traffic in a tunnel or is a backup
273      */
274     int in_use;
275
276     /**
277      * List of all the peers that form the path from origin to target
278      */
279     struct PeerInfo *peers;
280 };
281
282 /**
283  * Struct containing all information regarding a tunnel
284  * For an intermediate node the improtant info used will be:
285  * - OID        \ To identify
286  * - TID        / the tunnel
287  * - paths[0]   | To know where to send it next
288  * - metainfo: ready, speeds, accounting
289  * For an end node more fields will be needed (client-handling)
290  */
291 struct MESH_tunnel
292 {
293     /**
294      * Origin ID: Node that created the tunnel
295      */
296     struct GNUNET_PeerIdentity oid;
297
298     /**
299      * Tunnel number (unique for a given oid)
300      */
301     uint32_t tid;
302
303     /**
304      * Whether the tunnel is in state to transmit data
305      */
306     int ready;
307
308     /**
309      * Minimal speed for this tunnel in kb/s
310      */
311     uint32_t speed_min;
312
313     /**
314      * Maximal speed for this tunnel in kb/s
315      */
316     uint32_t speed_max;
317
318     /**
319      * Last time the tunnel was used
320      */
321     struct GNUNET_TIME_Absolute timestamp;
322
323     /**
324      * Peers in the tunnel, for future optimizations
325      */
326     struct PeerInfo *peers;
327
328     /**
329      * Paths (used and backup)
330      */
331     struct Path *paths;
332
333     /**
334      * Messages ready to transmit
335      */
336     struct GNUNET_MessageHeader *msg_out;
337
338     /**
339      * Messages received and not processed
340      */
341     struct GNUNET_MessageHeader *msg_in;
342
343     /**
344      * FIXME Clients. Is anyone to be notified for traffic here?
345      */
346 };
347
348 /**
349  * So, I'm an endpoint. Why am I receiveing traffic?
350  * Who is interested in this? How to communicate with them?
351  */
352 struct Clients
353 {
354     /**
355      * FIXME add structures needed to handle client connections
356      */
357     int fixme;
358 };
359
360
361
362 /******************************************************************************/
363 /********************      MESH NETWORK HANDLERS     **************************/
364 /******************************************************************************/
365
366 /**
367  * Core handler for path creation
368  * struct GNUNET_CORE_MessageHandler
369  *
370  * @param cls closure
371  * @param message message
372  * @param peer peer identity this notification is about
373  * @param atsi performance data
374  * @return GNUNET_OK to keep the connection open,
375  *         GNUNET_SYSERR to close it (signal serious error)
376  *
377  */
378 static int
379 handle_mesh_path_create (void *cls,
380                               const struct GNUNET_PeerIdentity *peer,
381                               const struct GNUNET_MessageHeader *message,
382                               const struct GNUNET_TRANSPORT_ATS_Information
383                               *atsi)
384 {
385     /* Extract path */
386     /* Find origin & self */
387     /* Search for origin in local tunnels */
388     /* Create tunnel / add path */
389     /* Retransmit to next link in chain, if any (core_notify + callback) */
390     return GNUNET_OK;
391 }
392
393 /**
394  * Core handler for mesh network traffic
395  *
396  * @param cls closure
397  * @param message message
398  * @param peer peer identity this notification is about
399  * @param atsi performance data
400  * @return GNUNET_OK to keep the connection open,
401  *         GNUNET_SYSERR to close it (signal serious error)
402  */
403 static int
404 handle_mesh_network_traffic (void *cls,
405                              const struct GNUNET_PeerIdentity *peer,
406                              const struct GNUNET_MessageHeader *message,
407                              const struct GNUNET_TRANSPORT_ATS_Information
408                              *atsi)
409 {
410     if(GNUNET_MESSAGE_TYPE_MESH_DATA_GO == ntohs(message->type)) {
411         /* Retransmit to next in path of tunnel identified by message */
412         return GNUNET_OK;
413     } else { /* GNUNET_MESSAGE_TYPE_MESH_DATA_BACK */
414         /* Retransmit to previous in path of tunnel identified by message */
415         return GNUNET_OK;
416     }
417 }
418
419 /**
420  * Functions to handle messages from core
421  */
422 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
423   {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
424   {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_GO, 0},
425   {&handle_mesh_network_traffic, GNUNET_MESSAGE_TYPE_MESH_DATA_BACK, 0},
426   {NULL, 0, 0}
427 };
428
429
430
431 /******************************************************************************/
432 /*********************       MESH LOCAL HANDLES      **************************/
433 /******************************************************************************/
434
435 /**
436  * Handler for client disconnection
437  *
438  * @param cls closure
439  * @param client identification of the client; NULL
440  *        for the last call when the server is destroyed
441  */
442 static void
443 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
444 {
445     /* Remove client from list, delete all timers and queues associated */
446     return;
447 }
448
449 /**
450  * Handler for new clients
451  * 
452  * @param cls closure
453  * @param client identification of the client
454  * @param message the actual message, which includes messages the client wants
455  */
456 static void
457 handle_local_new_client (void *cls,
458                          struct GNUNET_SERVER_Client *client,
459                          const struct GNUNET_MessageHeader *message)
460 {
461     return;
462 }
463
464 /**
465  * Handler for connection requests
466  * 
467  * @param cls closure
468  * @param client identification of the client
469  * @param message the actual message
470  */
471 static void
472 handle_local_connect (void *cls,
473                          struct GNUNET_SERVER_Client *client,
474                          const struct GNUNET_MessageHeader *message)
475 {
476     return;
477 }
478
479 /**
480  * Handler for client traffic
481  * 
482  * @param cls closure
483  * @param client identification of the client
484  * @param message the actual message
485  */
486 static void
487 handle_local_network_traffic (void *cls,
488                          struct GNUNET_SERVER_Client *client,
489                          const struct GNUNET_MessageHeader *message)
490 {
491     return;
492 }
493
494 /**
495  * Functions to handle messages from clients
496  */
497 /* MESSAGES DEFINED:
498 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT              272
499 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY     273
500 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL     274
501 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD     275
502 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL     276
503 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE 277
504 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL  278
505 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY       279
506 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATED       280
507 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROYED     281
508 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA                 282
509 #define GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST       283
510  */
511 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
512   {&handle_local_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
513   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ANY, 0},
514   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ALL, 0},
515   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD, 0},
516   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_DEL, 0},
517   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_BY_TYPE, sizeof(struct GNUNET_MESH_ConnectPeerByType)},
518   {&handle_local_connect, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_CANCEL, sizeof(struct GNUNET_MESH_Control)},
519   {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_TRANSMIT_READY, sizeof(struct GNUNET_MESH_Control)},
520   {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, /* FIXME needed? */
521   {&handle_local_network_traffic, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA_BROADCAST, 0}, /* FIXME needed? */
522   {NULL, NULL, 0, 0}
523 };
524
525
526 /**
527  * To be called on core init/fail.
528  *
529  * @param cls service closure
530  * @param server handle to the server for this service
531  * @param identity the public identity of this peer
532  * @param publicKey the public key of this peer
533  */
534 static void
535 core_init (void *cls,
536            struct GNUNET_CORE_Handle *server,
537            const struct GNUNET_PeerIdentity *identity,
538            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
539 {
540     return;
541 }
542
543 /**
544  * Method called whenever a given peer connects.
545  *
546  * @param cls closure
547  * @param peer peer identity this notification is about
548  * @param atsi performance data for the connection
549  */
550 static void
551 core_connect (void *cls,
552               const struct GNUNET_PeerIdentity *peer,
553               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
554 {
555     return;
556 }
557
558 /**
559  * Method called whenever a peer disconnects.
560  *
561  * @param cls closure
562  * @param peer peer identity this notification is about
563  */
564 static void
565 core_disconnect (void *cls,
566                 const struct
567                 GNUNET_PeerIdentity *peer)
568 {
569     return;
570 }
571
572 /**
573  * Process mesh requests. FIXME NON FUNCTIONAL, SKELETON
574  *
575  * @param cls closure
576  * @param server the initialized server
577  * @param c configuration to use
578  */
579 static void
580 run (void *cls,
581      struct GNUNET_SERVER_Handle *server,
582      const struct GNUNET_CONFIGURATION_Handle *c)
583 {
584   struct GNUNET_CORE_Handle *core;
585
586   GNUNET_SERVER_add_handlers (server, plugin_handlers);
587   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
588   core = GNUNET_CORE_connect (c,   /* Main configuration */
589                             32,       /* queue size */
590                             NULL,  /* Closure passed to MESH functions */
591                             &core_init,    /* Call core_init once connected */
592                             &core_connect,  /* Handle connects */
593                             &core_disconnect,       /* remove peers on disconnects */
594                             NULL,  /* Do we care about "status" updates? */
595                             NULL,  /* Don't want notified about all incoming messages */
596                             GNUNET_NO,     /* For header only inbound notification */
597                             NULL,  /* Don't want notified about all outbound messages */
598                             GNUNET_NO,     /* For header only outbound notification */
599                             core_handlers);        /* Register these handlers */
600
601   if (core == NULL)
602     return;
603 }
604
605 /**
606  * The main function for the mesh service.
607  *
608  * @param argc number of arguments from the command line
609  * @param argv command line arguments
610  * @return 0 ok, 1 on error
611  */
612 int
613 main (int argc, char *const *argv)
614 {
615   int ret;
616
617   ret = (GNUNET_OK ==
618          GNUNET_SERVICE_run (argc,
619                              argv,
620                              "mesh",
621                              GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
622   return ret;
623 }