towards KX
[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 "core.h"
45
46
47 #define DEBUG_HANDSHAKE GNUNET_EXTRA_LOGGING
48
49 #define DEBUG_CORE_QUOTA GNUNET_EXTRA_LOGGING
50
51 /**
52  * Receive and send buffer windows grow over time.  For
53  * how long can 'unused' bandwidth accumulate before we
54  * need to cap it?  (specified in seconds).
55  */
56 #define MAX_WINDOW_TIME_S (5 * 60)
57
58 /**
59  * How many messages do we queue up at most for optional
60  * notifications to a client?  (this can cause notifications
61  * about outgoing messages to be dropped).
62  */
63 #define MAX_NOTIFY_QUEUE 1024
64
65 /**
66  * Minimum bandwidth (out) to assign to any connected peer.
67  * Should be rather low; values larger than DEFAULT_BW_IN_OUT make no
68  * sense.
69  */
70 #define MIN_BANDWIDTH_PER_PEER GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT
71
72 /**
73  * After how much time past the "official" expiration time do
74  * we discard messages?  Should not be zero since we may
75  * intentionally defer transmission until close to the deadline
76  * and then may be slightly past the deadline due to inaccuracy
77  * in sleep and our own CPU consumption.
78  */
79 #define PAST_EXPIRATION_DISCARD_TIME GNUNET_TIME_UNIT_SECONDS
80
81 /**
82  * What is the maximum delay for a SET_KEY message?
83  */
84 #define MAX_SET_KEY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
85
86 /**
87  * How long do we wait for SET_KEY confirmation initially?
88  */
89 #define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 1)
90
91 /**
92  * What is the maximum delay for a PING message?
93  */
94 #define MAX_PING_DELAY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 2)
95
96 /**
97  * What is the maximum delay for a PONG message?
98  */
99 #define MAX_PONG_DELAY GNUNET_TIME_relative_multiply (MAX_PING_DELAY, 2)
100
101 /**
102  * What is the minimum frequency for a PING message?
103  */
104 #define MIN_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
105
106 /**
107  * How often do we recalculate bandwidth quotas?
108  */
109 #define QUOTA_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
110
111 /**
112  * What is the priority for a SET_KEY message?
113  */
114 #define SET_KEY_PRIORITY 0xFFFFFF
115
116 /**
117  * What is the priority for a PING message?
118  */
119 #define PING_PRIORITY 0xFFFFFF
120
121 /**
122  * What is the priority for a PONG message?
123  */
124 #define PONG_PRIORITY 0xFFFFFF
125
126 /**
127  * How many messages do we queue per peer at most?  Must be at
128  * least two.
129  */
130 #define MAX_PEER_QUEUE_SIZE 16
131
132 /**
133  * How many non-mandatory messages do we queue per client at most?
134  */
135 #define MAX_CLIENT_QUEUE_SIZE 32
136
137 /**
138  * What is the maximum age of a message for us to consider
139  * processing it?  Note that this looks at the timestamp used
140  * by the other peer, so clock skew between machines does
141  * come into play here.  So this should be picked high enough
142  * so that a little bit of clock skew does not prevent peers
143  * from connecting to us.
144  */
145 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
146
147
148
149 /**
150  * Number of bytes (at the beginning) of "struct EncryptedMessage"
151  * that are NOT encrypted.
152  */
153 #define ENCRYPTED_HEADER_SIZE (offsetof(struct EncryptedMessage, sequence_number))
154
155
156 /**
157  * Our identity.
158  */
159 struct GNUNET_PeerIdentity GSC_my_identity;
160
161 /**
162  * Our configuration.
163  */
164 const struct GNUNET_CONFIGURATION_Handle *GSC_cfg;
165
166 /**
167  * For creating statistics.
168  */
169 struct GNUNET_STATISTICS_Handle *GSC_stats;
170
171 /**
172  * Our message stream tokenizer (for encrypted payload).
173  */
174 struct GNUNET_SERVER_MessageStreamTokenizer *GSC_mst;
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
190   if (GSC_mst != NULL)
191   {
192     GNUNET_SERVER_mst_destroy (GSC_mst);
193     GSC_mst = NULL;
194   }
195   if (GSC_stats != NULL)
196   {
197     GNUNET_STATISTICS_destroy (GSC_stats, GNUNET_NO);
198     GSC_stats = NULL;
199   }
200   GSC_cfg = NULL;
201 }
202
203
204 /**
205  * Initiate core service.
206  *
207  * @param cls closure
208  * @param server the initialized server
209  * @param c configuration to use
210  */
211 static void
212 run (void *cls, struct GNUNET_SERVER_Handle *server,
213      const struct GNUNET_CONFIGURATION_Handle *c)
214 {
215   GSC_cfg = c;  
216   GSC_mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
217   GSC_stats = GNUNET_STATISTICS_create ("core", cfg);
218
219   GSC_CLIENTS_init (server);
220   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
221                                 NULL);
222   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Core service of `%4s' ready.\n"),
223               GNUNET_i2s (&my_identity));
224 }
225
226
227
228 /**
229  * The main function for the transport service.
230  *
231  * @param argc number of arguments from the command line
232  * @param argv command line arguments
233  * @return 0 ok, 1 on error
234  */
235 int
236 main (int argc, char *const *argv)
237 {
238   return (GNUNET_OK ==
239           GNUNET_SERVICE_run (argc, argv, "core", GNUNET_SERVICE_OPTION_NONE,
240                               &run, NULL)) ? 0 : 1;
241 }
242
243 /* end of gnunet-service-core.c */