fixes
[oweals/gnunet.git] / src / core / gnunet-service-core-new.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 core/gnunet-service-core.c
23  * @brief high-level P2P messaging
24  * @author Christian Grothoff
25  *
26  * Type map implementation:
27  * - track type maps for neighbours (can wait)
28  * - only notify clients about peers with matching type maps (can wait)
29  *
30  * Considerations for later:
31  * - check that hostkey used by transport (for HELLOs) is the
32  *   same as the hostkey that we are using!
33  */
34 #include "platform.h"
35 #include <zlib.h>
36 #include "gnunet_constants.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_peerinfo_service.h"
40 #include "gnunet_protocols.h"
41 #include "gnunet_signatures.h"
42 #include "gnunet_statistics_service.h"
43 #include "gnunet_transport_service.h"
44 #include "gnunet-service-core.h"
45 #include "gnunet-service-core_clients.h"
46 #include "gnunet-service-core_kx.h"
47 #include "gnunet-service-core_neighbours.h"
48 #include "gnunet-service-core_sessions.h"
49 #include "gnunet-service-core_typemap.h"
50
51
52 #define DEBUG_HANDSHAKE GNUNET_EXTRA_LOGGING
53
54 #define DEBUG_CORE_QUOTA GNUNET_EXTRA_LOGGING
55
56 /**
57  * Receive and send buffer windows grow over time.  For
58  * how long can 'unused' bandwidth accumulate before we
59  * need to cap it?  (specified in seconds).
60  */
61 #define MAX_WINDOW_TIME_S (5 * 60)
62
63 /**
64  * How many messages do we queue up at most for optional
65  * notifications to a client?  (this can cause notifications
66  * about outgoing messages to be dropped).
67  */
68 #define MAX_NOTIFY_QUEUE 1024
69
70 /**
71  * Minimum bandwidth (out) to assign to any connected peer.
72  * Should be rather low; values larger than DEFAULT_BW_IN_OUT make no
73  * sense.
74  */
75 #define MIN_BANDWIDTH_PER_PEER GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT
76
77 /**
78  * After how much time past the "official" expiration time do
79  * we discard messages?  Should not be zero since we may
80  * intentionally defer transmission until close to the deadline
81  * and then may be slightly past the deadline due to inaccuracy
82  * in sleep and our own CPU consumption.
83  */
84 #define PAST_EXPIRATION_DISCARD_TIME GNUNET_TIME_UNIT_SECONDS
85
86 /**
87  * What is the maximum delay for a SET_KEY message?
88  */
89 #define MAX_SET_KEY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
90
91 /**
92  * How long do we wait for SET_KEY confirmation initially?
93  */
94 #define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 1)
95
96 /**
97  * What is the maximum delay for a PING message?
98  */
99 #define MAX_PING_DELAY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 2)
100
101 /**
102  * What is the maximum delay for a PONG message?
103  */
104 #define MAX_PONG_DELAY GNUNET_TIME_relative_multiply (MAX_PING_DELAY, 2)
105
106 /**
107  * What is the minimum frequency for a PING message?
108  */
109 #define MIN_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
110
111 /**
112  * How often do we recalculate bandwidth quotas?
113  */
114 #define QUOTA_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
115
116 /**
117  * What is the priority for a SET_KEY message?
118  */
119 #define SET_KEY_PRIORITY 0xFFFFFF
120
121 /**
122  * What is the priority for a PING message?
123  */
124 #define PING_PRIORITY 0xFFFFFF
125
126 /**
127  * What is the priority for a PONG message?
128  */
129 #define PONG_PRIORITY 0xFFFFFF
130
131 /**
132  * How many messages do we queue per peer at most?  Must be at
133  * least two.
134  */
135 #define MAX_PEER_QUEUE_SIZE 16
136
137 /**
138  * How many non-mandatory messages do we queue per client at most?
139  */
140 #define MAX_CLIENT_QUEUE_SIZE 32
141
142 /**
143  * What is the maximum age of a message for us to consider
144  * processing it?  Note that this looks at the timestamp used
145  * by the other peer, so clock skew between machines does
146  * come into play here.  So this should be picked high enough
147  * so that a little bit of clock skew does not prevent peers
148  * from connecting to us.
149  */
150 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
151
152
153
154 /**
155  * Number of bytes (at the beginning) of "struct EncryptedMessage"
156  * that are NOT encrypted.
157  */
158 #define ENCRYPTED_HEADER_SIZE (offsetof(struct EncryptedMessage, sequence_number))
159
160
161 /**
162  * Our identity.
163  */
164 struct GNUNET_PeerIdentity GSC_my_identity;
165
166 /**
167  * Our configuration.
168  */
169 const struct GNUNET_CONFIGURATION_Handle *GSC_cfg;
170
171 /**
172  * For creating statistics.
173  */
174 struct GNUNET_STATISTICS_Handle *GSC_stats;
175
176
177 /**
178  * Last task run during shutdown.  Disconnects us from
179  * the transport.
180  */
181 static void
182 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 {
184 #if DEBUG_CORE
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
186               "Core service shutting down.\n");
187 #endif
188   GSC_CLIENTS_done ();
189   GSC_SESSIONS_done ();
190   GSC_NEIGHBOURS_done ();
191   GSC_KX_done ();
192   GSC_TYPEMAP_done ();
193   if (GSC_stats != NULL)
194   {
195     GNUNET_STATISTICS_destroy (GSC_stats, GNUNET_NO);
196     GSC_stats = NULL;
197   }
198   GSC_cfg = NULL;
199 }
200
201
202 /**
203  * Initiate core service.
204  *
205  * @param cls closure
206  * @param server the initialized server
207  * @param c configuration to use
208  */
209 static void
210 run (void *cls, struct GNUNET_SERVER_Handle *server,
211      const struct GNUNET_CONFIGURATION_Handle *c)
212 {
213   GSC_cfg = c;  
214   GSC_stats = GNUNET_STATISTICS_create ("core", GSC_cfg);
215   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
216                                 NULL);
217   GSC_TYPEMAP_init ();
218   if ( (GNUNET_OK != GSC_KX_init ()) ||
219        (GNUNET_OK != GSC_NEIGHBOURS_init ()) )
220   {
221     GNUNET_SCHEDULER_shutdown ();
222     return;
223   }
224   GSC_SESSIONS_init ();
225   GSC_CLIENTS_init (server);
226   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Core service of `%4s' ready.\n"),
227               GNUNET_i2s (&GSC_my_identity));
228 }
229
230
231
232 /**
233  * The main function for the transport service.
234  *
235  * @param argc number of arguments from the command line
236  * @param argv command line arguments
237  * @return 0 ok, 1 on error
238  */
239 int
240 main (int argc, char *const *argv)
241 {
242   return (GNUNET_OK ==
243           GNUNET_SERVICE_run (argc, argv, "core", GNUNET_SERVICE_OPTION_NONE,
244                               &run, NULL)) ? 0 : 1;
245 }
246
247 /* end of gnunet-service-core.c */