2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
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.
22 * @file dv/gnunet-service-dv.c
23 * @brief the distance vector service, primarily handles gossip of nearby
24 * peers and sending/receiving DV messages from core and decapsulating
27 * @author Christian Grothoff
28 * @author Nathan Evans
32 #include "gnunet_client_lib.h"
33 #include "gnunet_getopt_lib.h"
34 #include "gnunet_os_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_service_lib.h"
37 #include "gnunet_core_service.h"
38 #include "gnunet_signal_lib.h"
42 * DV Service Context stuff goes here...
44 static struct GNUNET_CORE_Handle *coreAPI;
46 static struct GNUNET_PeerIdentity *my_identity;
48 const struct GNUNET_CONFIGURATION_Handle *cfg;
50 struct GNUNET_SCHEDULER_Handle *sched;
52 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
55 * Core handler for dv data messages. Whatever this message
56 * contains all we really have to do is rip it out of its
57 * DV layering and give it to our pal the DV plugin to report
61 * @param client identification of the client
62 * @param message the actual message
64 void handle_dv_data_message (void *cls,
65 struct GNUNET_SERVER_Client *
68 GNUNET_MessageHeader *
72 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
73 "%s: Receives %s message!\n", "dv", "DV DATA");
78 * Core handler for dv gossip messages. These will be used
79 * by us to create a HELLO message for the newly peer containing
80 * which direct peer we can connect through, and what the cost
81 * is. This HELLO will then be scheduled for validation by the
82 * transport service so that it can be used by all others.
85 * @param client identification of the client
86 * @param message the actual message
88 void handle_dv_gossip_message (void *cls,
89 struct GNUNET_SERVER_Client *
91 const struct GNUNET_MessageHeader *
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "%s: Receives %s message!\n", "dv", "DV GOSSIP");
102 * Service server's handler for message send requests (which come
103 * bubbling up to us through the DV plugin).
106 * @param client identification of the client
107 * @param message the actual message
109 void send_dv_message (void *cls,
110 struct GNUNET_SERVER_Client * client,
111 const struct GNUNET_MessageHeader * message)
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115 "%s: Receives %s message!\n", "dv", "SEND");
120 * List of handlers for the messages understood by this
123 * Hmm... will we need to register some handlers with core and
124 * some handlers with our server here? Because core should be
125 * getting the incoming DV messages (from whichever lower level
126 * transport) and then our server should be getting messages
127 * from the dv_plugin, right?
129 static struct GNUNET_SERVER_MessageHandler core_handlers[] = {
130 {&handle_dv_data_message, NULL, GNUNET_MESSAGE_TYPE_DV_DATA, 0},
131 {&handle_dv_gossip_message, NULL, GNUNET_MESSAGE_TYPE_DV_GOSSIP, 0},
135 static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
136 {&send_dv_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
142 * Task run during shutdown.
148 shutdown_task (void *cls,
149 const struct GNUNET_SCHEDULER_TaskContext *tc)
152 GNUNET_CORE_disconnect (coreAPI);
156 * To be called on core init/fail.
158 void core_init (void *cls,
159 struct GNUNET_CORE_Handle * server,
160 const struct GNUNET_PeerIdentity *my_identity,
161 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded * publicKey)
166 GNUNET_SCHEDULER_cancel(sched, cleanup_task);
167 GNUNET_SCHEDULER_add_now(sched, &shutdown_task, NULL);
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "%s: Core initialized, I am peer: %s\n", "dv", GNUNET_i2s(my_identity));
177 * Method called whenever a given peer either connects.
180 * @param peer peer identity this notification is about
181 * @param latency reported latency of the connection with 'other'
182 * @param distance reported distance (DV) to 'other'
184 void handle_core_connect (void *cls,
185 const struct GNUNET_PeerIdentity * peer,
186 struct GNUNET_TIME_Relative latency,
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "%s: Receives core connect message!\n", "dv");
197 * Method called whenever a given peer either connects.
200 * @param peer peer identity this notification is about
201 * @param latency reported latency of the connection with 'other'
202 * @param distance reported distance (DV) to 'other'
204 void handle_core_disconnect (void *cls,
205 const struct GNUNET_PeerIdentity * peer,
206 struct GNUNET_TIME_Relative latency,
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "%s: Receives core disconnect message!\n", "dv");
217 * Process dv requests.
220 * @param sched scheduler to use
221 * @param server the initialized server
222 * @param c configuration to use
226 struct GNUNET_SCHEDULER_Handle *scheduler,
227 struct GNUNET_SERVER_Handle *server,
228 const struct GNUNET_CONFIGURATION_Handle *c)
230 struct GNUNET_TIME_Relative timeout;
233 GNUNET_SERVER_add_handlers (server, plugin_handlers);
235 GNUNET_CORE_connect (sched,
238 NULL, /* FIXME: anything we want to pass around? */
240 NULL, /* Don't care about pre-connects */
241 &handle_core_connect,
242 &handle_core_disconnect,
251 /* load (server); Huh? */
253 /* Scheduled the task to clean up when shutdown is called */
255 cleanup_task = GNUNET_SCHEDULER_add_delayed (sched,
256 GNUNET_TIME_UNIT_FOREVER_REL,
263 * The main function for the dv service.
265 * @param argc number of arguments from the command line
266 * @param argv command line arguments
267 * @return 0 ok, 1 on error
270 main (int argc, char *const *argv)
273 GNUNET_SERVICE_run (argc,
276 GNUNET_SERVICE_OPTION_NONE,
277 &run, NULL)) ? 0 : 1;