aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/Makefile.am68
-rw-r--r--src/core/core.h308
-rw-r--r--src/core/core_api.c1071
-rw-r--r--src/core/gnunet-service-core.c2859
-rw-r--r--src/core/test_core_api.c368
-rw-r--r--src/core/test_core_api_data.conf27
-rw-r--r--src/core/test_core_api_peer1.conf40
-rw-r--r--src/core/test_core_api_peer2.conf39
-rw-r--r--src/core/test_core_api_start_only.c257
9 files changed, 5037 insertions, 0 deletions
diff --git a/src/core/Makefile.am b/src/core/Makefile.am
new file mode 100644
index 000000000..55ef757c2
--- /dev/null
+++ b/src/core/Makefile.am
@@ -0,0 +1,68 @@
1INCLUDES = -I$(top_srcdir)/src/include
2
3plugindir = $(libdir)/gnunet
4
5if MINGW
6 WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols
7endif
8
9if USE_COVERAGE
10 AM_CFLAGS = -fprofile-arcs -ftest-coverage
11endif
12
13
14lib_LTLIBRARIES = \
15 libgnunetcore.la
16
17libgnunetcore_la_SOURCES = \
18 core_api.c core.h
19libgnunetcore_la_LIBADD = \
20 $(top_builddir)/src/arm/libgnunetarm.la \
21 $(top_builddir)/src/util/libgnunetutil.la \
22 $(GN_LIBINTL)
23libgnunetcore_la_LDFLAGS = \
24 $(GN_LIB_LDFLAGS) $(WINFLAGS) \
25 -version-info 0:0:0
26
27
28bin_PROGRAMS = \
29 gnunet-service-core
30
31gnunet_service_core_SOURCES = \
32 gnunet-service-core.c
33gnunet_service_core_LDADD = \
34 $(top_builddir)/src/hello/libgnunethello.la \
35 $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \
36 $(top_builddir)/src/transport/libgnunettransport.la \
37 $(top_builddir)/src/util/libgnunetutil.la \
38 $(GN_LIBINTL)
39
40
41check_PROGRAMS = \
42 test_core_api_start_only \
43 test_core_api
44
45TESTS = $(check_PROGRAMS)
46
47test_core_api_SOURCES = \
48 test_core_api.c
49test_core_api_LDADD = \
50 $(top_builddir)/src/arm/libgnunetarm.la \
51 $(top_builddir)/src/core/libgnunetcore.la \
52 $(top_builddir)/src/transport/libgnunettransport.la \
53 $(top_builddir)/src/util/libgnunetutil.la
54
55
56test_core_api_start_only_SOURCES = \
57 test_core_api_start_only.c
58test_core_api_start_only_LDADD = \
59 $(top_builddir)/src/arm/libgnunetarm.la \
60 $(top_builddir)/src/core/libgnunetcore.la \
61 $(top_builddir)/src/util/libgnunetutil.la
62
63
64EXTRA_DIST = \
65 test_core_api_data.conf \
66 test_core_api_peer1.conf \
67 test_core_api_peer2.conf
68
diff --git a/src/core/core.h b/src/core/core.h
new file mode 100644
index 000000000..840c7e143
--- /dev/null
+++ b/src/core/core.h
@@ -0,0 +1,308 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 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 2, 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/core.h
23 * @brief common internal definitions for core service
24 * @author Christian Grothoff
25 */
26#include "gnunet_crypto_lib.h"
27#include "gnunet_time_lib.h"
28
29#define DEBUG_CORE GNUNET_NO
30
31/**
32 * Definition of bits in the InitMessage's options field that specify
33 * which events this client cares about. Note that inbound messages
34 * for handlers that were specifically registered are always
35 * transmitted to the client.
36 */
37#define GNUNET_CORE_OPTION_NOTHING 0
38#define GNUNET_CORE_OPTION_SEND_CONNECT 1
39#define GNUNET_CORE_OPTION_SEND_DISCONNECT 2
40#define GNUNET_CORE_OPTION_SEND_BFC 4
41#define GNUNET_CORE_OPTION_SEND_FULL_INBOUND 8
42#define GNUNET_CORE_OPTION_SEND_HDR_INBOUND 16
43#define GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND 32
44#define GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND 64
45
46
47/**
48 * Message transmitted core clients to gnunet-service-core
49 * to start the interaction. This header is followed by
50 * uint16_t type values specifying which messages this
51 * client is interested in.
52 */
53struct InitMessage
54{
55
56 /**
57 * Header with type GNUNET_MESSAGE_TYPE_CORE_INIT.
58 */
59 struct GNUNET_MessageHeader header;
60
61 /**
62 * Options, see GNUNET_CORE_OPTION_ values.
63 */
64 uint32_t options GNUNET_PACKED;
65
66};
67
68
69/**
70 * Message transmitted by the gnunet-service-core process
71 * to its clients in response to an INIT message.
72 */
73struct InitReplyMessage
74{
75
76 /**
77 * Header with type GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY
78 */
79 struct GNUNET_MessageHeader header;
80
81 /**
82 * Always zero.
83 */
84 uint32_t reserved GNUNET_PACKED;
85
86 /**
87 * Public key of the local peer.
88 */
89 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded publicKey;
90
91};
92
93
94/**
95 * Message sent by the service to clients to notify them
96 * about a peer connecting or disconnecting.
97 */
98struct ConnectNotifyMessage
99{
100 /**
101 * Header with type GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT
102 * or GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT.
103 */
104 struct GNUNET_MessageHeader header;
105
106 /**
107 * Available bandwidth to this peer; zero for disconnect.
108 * [TODO: currently set to hard-coded constant and hence
109 * not really useful, right?]
110 */
111 uint32_t bpm_available GNUNET_PACKED;
112
113 /**
114 * Identity of the connecting peer.
115 */
116 struct GNUNET_PeerIdentity peer;
117
118 /**
119 * Time of our last interaction with the peer; close
120 * to "now" for connect messages.
121 * [TODO: is this useful?]
122 */
123 struct GNUNET_TIME_AbsoluteNBO last_activity;
124
125};
126
127
128
129/**
130 * Message sent by the service to clients to notify them about
131 * messages being received or transmitted. This overall message is
132 * followed by the real message, or just the header of the real
133 * message (depending on the client's preferences). The receiver can
134 * tell if he got the full message or only a partial message by
135 * looking at the size field in the header of NotifyTrafficMessage and
136 * checking it with the size field in the message that follows.
137 */
138struct NotifyTrafficMessage
139{
140 /**
141 * Header with type GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND
142 * or GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND.
143 */
144 struct GNUNET_MessageHeader header;
145
146 /**
147 * Always zero.
148 */
149 uint32_t reserved GNUNET_PACKED;
150
151 /**
152 * Identity of the receiver or sender.
153 */
154 struct GNUNET_PeerIdentity peer;
155
156};
157
158
159/**
160 * Message sent to the core asking for configuration
161 * information and possibly preference changes.
162 */
163struct RequestConfigureMessage
164{
165 /**
166 * Header with type GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONFIGURE
167 */
168 struct GNUNET_MessageHeader header;
169
170 /**
171 * Always zero.
172 */
173 uint32_t reserved GNUNET_PACKED;
174
175 /**
176 * Limit the number of bytes of outbound traffic to this
177 * peer to at most the specified amount (naturally, the
178 * amount is also limited by the receiving peer).
179 */
180 uint32_t limit_outbound_bpm GNUNET_PACKED;
181
182 /**
183 * Number of bytes of inbound traffic to reserve, can
184 * be negative (to unreserve). NBO.
185 */
186 int32_t reserve_inbound GNUNET_PACKED;
187
188 /**
189 * Increment the current traffic preference for the given peer by
190 * the specified amont. The traffic preference is used to determine
191 * the share of bandwidth this peer will typcially be assigned.
192 */
193 double preference_change GNUNET_PACKED;
194
195 /**
196 * Identity of the peer being configured.
197 */
198 struct GNUNET_PeerIdentity peer;
199
200};
201
202
203/**
204 * Response from the core to a "RequestConfigureMessage"
205 * providing traffic status information for a peer.
206 */
207struct ConfigurationInfoMessage
208{
209 /**
210 * Header with type GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO
211 */
212 struct GNUNET_MessageHeader header;
213
214 /**
215 * Amount of traffic (inbound number of bytes) that was reserved in
216 * response to the configuration change request. Negative for
217 * "unreserved" bytes.
218 */
219 int32_t reserved_amount GNUNET_PACKED;
220
221 /**
222 * Available bandwidth in (in bytes per minute) for this peer.
223 * 0 if we have been disconnected.
224 */
225 uint32_t bpm_in GNUNET_PACKED;
226
227 /**
228 * Available bandwidth out (in bytes per minute) for this peer,
229 * 0 if we have been disconnected.
230 */
231 uint32_t bpm_out GNUNET_PACKED;
232
233 /**
234 * Latest transport latency estimate for the peer.
235 * FOREVER if we have been disconnected.
236 */
237 struct GNUNET_TIME_RelativeNBO latency;
238
239 /**
240 * Current traffic preference for the peer.
241 * 0 if we have been disconnected.
242 */
243 double preference;
244
245 /**
246 * Identity of the receiver or sender.
247 */
248 struct GNUNET_PeerIdentity peer;
249
250};
251
252
253/**
254 * Core asking a client to generate traffic for a particular
255 * target.
256 */
257struct SolicitTrafficMessage
258{
259 /**
260 * Header with type GNUNET_MESSAGE_TYPE_CORE_SOLICIT_TRAFFIC
261 * or GNUNET_MESSAGE_TYPE_CORE_RECV_OK
262 */
263 struct GNUNET_MessageHeader header;
264
265 /**
266 * Number of bytes of traffic being solicited.
267 */
268 uint32_t solicit_size GNUNET_PACKED;
269
270 /**
271 * Identity of the receiver or sender.
272 */
273 struct GNUNET_PeerIdentity peer;
274
275};
276
277
278/**
279 * Client asking core to transmit a particular message to
280 * a particular target. Does NOT have to be solicited.
281 */
282struct SendMessage
283{
284 /**
285 * Header with type GNUNET_MESSAGE_TYPE_CORE_SEND
286 */
287 struct GNUNET_MessageHeader header;
288
289 /**
290 * How important is this message?
291 */
292 uint32_t priority GNUNET_PACKED;
293
294 /**
295 * By what time would the sender really like to see this
296 * message transmitted?
297 */
298 struct GNUNET_TIME_AbsoluteNBO deadline;
299
300 /**
301 * Identity of the receiver or sender.
302 */
303 struct GNUNET_PeerIdentity peer;
304
305};
306
307
308/* end of core.h */
diff --git a/src/core/core_api.c b/src/core/core_api.c
new file mode 100644
index 000000000..10fa0ccdd
--- /dev/null
+++ b/src/core/core_api.c
@@ -0,0 +1,1071 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 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 2, 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/core_api.c
23 * @brief core service; this is the main API for encrypted P2P
24 * communications
25 * @author Christian Grothoff
26 */
27#include "platform.h"
28#include "gnunet_core_service.h"
29#include "core.h"
30
31
32/**
33 * Context for the core service connection.
34 */
35struct GNUNET_CORE_Handle
36{
37
38 /**
39 * Our scheduler.
40 */
41 struct GNUNET_SCHEDULER_Handle *sched;
42
43 /**
44 * Configuration we're using.
45 */
46 struct GNUNET_CONFIGURATION_Handle *cfg;
47
48 /**
49 * Closure for the various callbacks.
50 */
51 void *cls;
52
53 /**
54 * Function to call once we've handshaked with the core service.
55 */
56 GNUNET_CORE_StartupCallback init;
57
58 /**
59 * Function to call whenever we're notified about a peer connecting.
60 */
61 GNUNET_CORE_ClientEventHandler connects;
62
63 /**
64 * Function to call whenever we're notified about a peer disconnecting.
65 */
66 GNUNET_CORE_ClientEventHandler disconnects;
67
68 /**
69 * Function to call whenever we're asked to generate traffic
70 * (data provided to be transmitted back to the service).
71 */
72 GNUNET_CORE_BufferFillCallback bfc;
73
74 /**
75 * Function to call whenever we receive an inbound message.
76 */
77 GNUNET_CORE_MessageCallback inbound_notify;
78
79 /**
80 * Function to call whenever we receive an outbound message.
81 */
82 GNUNET_CORE_MessageCallback outbound_notify;
83
84 /**
85 * Function handlers for messages of particular type.
86 */
87 const struct GNUNET_CORE_MessageHandler *handlers;
88
89 /**
90 * Our connection to the service.
91 */
92 struct GNUNET_CLIENT_Connection *client;
93
94 /**
95 * Handle for our current transmission request.
96 */
97 struct GNUNET_NETWORK_TransmitHandle *th;
98
99 /**
100 * Head of doubly-linked list of pending requests.
101 */
102 struct GNUNET_CORE_TransmitHandle *pending_head;
103
104 /**
105 * Tail of doubly-linked list of pending requests.
106 */
107 struct GNUNET_CORE_TransmitHandle *pending_tail;
108
109 /**
110 * Currently submitted request (or NULL)
111 */
112 struct GNUNET_CORE_TransmitHandle *submitted;
113
114 /**
115 * How long to wait until we time out the connection attempt?
116 */
117 struct GNUNET_TIME_Absolute startup_timeout;
118
119 /**
120 * ID of reconnect task (if any).
121 */
122 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
123
124 /**
125 * Number of entries in the handlers array.
126 */
127 unsigned int hcnt;
128
129 /**
130 * For inbound notifications without a specific handler, do
131 * we expect to only receive headers?
132 */
133 int inbound_hdr_only;
134
135 /**
136 * For outbound notifications without a specific handler, do
137 * we expect to only receive headers?
138 */
139 int outbound_hdr_only;
140
141 /**
142 * Are we currently disconnected and hence unable to forward
143 * requests?
144 */
145 int currently_down;
146};
147
148
149/**
150 * Handle for a transmission request.
151 */
152struct GNUNET_CORE_TransmitHandle
153{
154
155 /**
156 * We keep active transmit handles in a doubly-linked list.
157 */
158 struct GNUNET_CORE_TransmitHandle *next;
159
160 /**
161 * We keep active transmit handles in a doubly-linked list.
162 */
163 struct GNUNET_CORE_TransmitHandle *prev;
164
165 /**
166 * Corresponding core handle.
167 */
168 struct GNUNET_CORE_Handle *ch;
169
170 /**
171 * Function that will be called to get the actual request
172 * (once we are ready to transmit this request to the core).
173 * The function will be called with a NULL buffer to signal
174 * timeout.
175 */
176 GNUNET_NETWORK_TransmitReadyNotify get_message;
177
178 /**
179 * Closure for get_message.
180 */
181 void *get_message_cls;
182
183 /**
184 * If this entry is for a configuration request, pointer
185 * to the information callback; otherwise NULL.
186 */
187 GNUNET_CORE_PeerConfigurationInfoCallback info;
188
189 /**
190 * Closure for info.
191 */
192 void *info_cls;
193
194 /**
195 * If this entry is for a transmission request, pointer
196 * to the notify callback; otherwise NULL.
197 */
198 GNUNET_NETWORK_TransmitReadyNotify notify;
199
200 /**
201 * Closure for notify.
202 */
203 void *notify_cls;
204
205 /**
206 * Peer the request is about.
207 */
208 struct GNUNET_PeerIdentity peer;
209
210 /**
211 * Timeout for this handle.
212 */
213 struct GNUNET_TIME_Absolute timeout;
214
215 /**
216 * ID of timeout task.
217 */
218 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
219
220 /**
221 * How important is this message?
222 */
223 uint32_t priority;
224
225 /**
226 * Size of this request.
227 */
228 uint16_t msize;
229
230
231};
232
233
234/**
235 * Function called when we are ready to transmit our
236 * "START" message (or when this operation timed out).
237 *
238 * @param cls closure
239 * @param size number of bytes available in buf
240 * @param buf where the callee should write the message
241 * @return number of bytes written to buf
242 */
243static size_t transmit_start (void *cls, size_t size, void *buf);
244
245
246/**
247 * Our current client connection went down. Clean it up
248 * and try to reconnect!
249 */
250static void
251reconnect (struct GNUNET_CORE_Handle *h)
252{
253 GNUNET_CLIENT_disconnect (h->client);
254 h->currently_down = GNUNET_YES;
255 h->client = GNUNET_CLIENT_connect (h->sched, "core", h->cfg);
256 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
257 sizeof (struct InitMessage) +
258 sizeof (uint16_t) * h->hcnt,
259 GNUNET_TIME_UNIT_SECONDS,
260 &transmit_start, h);
261}
262
263
264/**
265 * The given request hit its timeout. Remove from the
266 * doubly-linked list and call the respective continuation.
267 *
268 * @param cls the transmit handle of the request that timed out
269 * @param tc context, can be NULL (!)
270 */
271static void
272timeout_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
273{
274 struct GNUNET_CORE_TransmitHandle *th = cls;
275 struct GNUNET_CORE_Handle *h;
276
277 h = th->ch;
278 th->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
279 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
280 GNUNET_CORE_notify_transmit_ready_cancel (th);
281}
282
283
284/**
285 * Function called when we are ready to transmit a request from our
286 * request list (or when this operation timed out).
287 *
288 * @param cls closure
289 * @param size number of bytes available in buf
290 * @param buf where the callee should write the message
291 * @return number of bytes written to buf
292 */
293static size_t
294request_start (void *cls, size_t size, void *buf)
295{
296 struct GNUNET_CORE_Handle *h = cls;
297 struct GNUNET_CORE_TransmitHandle *th;
298 size_t ret;
299
300 h->th = NULL;
301 th = h->pending_head;
302 if (buf == NULL)
303 {
304 timeout_request (th, NULL);
305 return 0;
306 }
307 /* create new timeout task (in case core takes too long to respond!) */
308 th->timeout_task = GNUNET_SCHEDULER_add_delayed (h->sched,
309 GNUNET_NO,
310 GNUNET_SCHEDULER_PRIORITY_KEEP,
311 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
312 GNUNET_TIME_absolute_get_remaining
313 (th->timeout),
314 &timeout_request, th);
315 /* remove th from doubly-linked pending list, move to submitted */
316 GNUNET_assert (th->prev == NULL);
317 h->pending_head = th->next;
318 if (th->next == NULL)
319 h->pending_tail = NULL;
320 else
321 th->next->prev = NULL;
322 GNUNET_assert (h->submitted == NULL);
323 h->submitted = th;
324 GNUNET_assert (size >= th->msize);
325 ret = th->get_message (th->get_message_cls, size, buf);
326 GNUNET_assert (ret <= size);
327 return ret;
328}
329
330
331/**
332 * Check the list of pending requests, send the next
333 * one to the core.
334 */
335static void
336trigger_next_request (struct GNUNET_CORE_Handle *h)
337{
338 struct GNUNET_CORE_TransmitHandle *th;
339 if (h->currently_down)
340 return; /* connection temporarily down */
341 if (NULL == (th = h->pending_head))
342 return; /* no requests pending */
343 GNUNET_assert (NULL == h->th);
344 GNUNET_SCHEDULER_cancel (h->sched, th->timeout_task);
345 th->timeout_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
346 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
347 th->msize,
348 GNUNET_TIME_absolute_get_remaining
349 (th->timeout), &request_start,
350 h);
351}
352
353
354/**
355 * cls is a pointer to a 32 bit number followed by that
356 * amount of data. If possible, copy to buf and return
357 * number of bytes copied. Always free the buffer.
358 */
359static size_t
360copy_and_free (void *cls, size_t size, void *buf)
361{
362 char *cbuf = cls;
363 uint32_t have;
364
365 memcpy (&have, cbuf, sizeof (uint32_t));
366 if (have > size)
367 {
368 /* timeout / error case */
369 GNUNET_free (cbuf);
370 return 0;
371 }
372 memcpy (buf, cbuf + sizeof (uint32_t), have);
373 GNUNET_free (cbuf);
374 return have;
375}
376
377
378/**
379 * Call bfc callback to solicit traffic for the given peer.
380 */
381static void
382solicit_traffic (struct GNUNET_CORE_Handle *h,
383 const struct GNUNET_PeerIdentity *peer, uint32_t amount)
384{
385 char buf[amount];
386 size_t have;
387 char *cbuf;
388
389 have = h->bfc (h->cls, peer, buf, amount);
390 if (have == 0)
391 return;
392 GNUNET_assert (have >= sizeof (struct GNUNET_MessageHeader));
393 cbuf = GNUNET_malloc (have + sizeof (uint32_t));
394 memcpy (cbuf, &have, sizeof (uint32_t));
395 memcpy (cbuf + sizeof (uint32_t), buf, have);
396 GNUNET_CORE_notify_transmit_ready (h,
397 0,
398 GNUNET_TIME_UNIT_SECONDS,
399 peer, have, &copy_and_free, cbuf);
400}
401
402
403/**
404 * Handler for most messages received from the core.
405 */
406static void
407main_handler (void *cls, const struct GNUNET_MessageHeader *msg)
408{
409 struct GNUNET_CORE_Handle *h = cls;
410 unsigned int hpos;
411 const struct ConnectNotifyMessage *cnm;
412 const struct NotifyTrafficMessage *ntm;
413 const struct ConfigurationInfoMessage *cim;
414 const struct SolicitTrafficMessage *stm;
415 const struct GNUNET_MessageHeader *em;
416 uint16_t msize;
417 uint16_t et;
418 uint32_t ss;
419 const struct GNUNET_CORE_MessageHandler *mh;
420
421 if (msg == NULL)
422 {
423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
424 _
425 ("Client was disconnected from core service, trying to reconnect.\n"));
426 reconnect (h);
427 return;
428 }
429 msize = ntohs (msg->size);
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Processing message of type %u and size %u from core service\n",
432 ntohs (msg->type), msize);
433 switch (ntohs (msg->type))
434 {
435 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
436 if (NULL == h->connects)
437 {
438 GNUNET_break (0);
439 break;
440 }
441 if (msize != sizeof (struct ConnectNotifyMessage))
442 {
443 GNUNET_break (0);
444 break;
445 }
446 cnm = (const struct ConnectNotifyMessage *) msg;
447 break;
448 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
449 if (NULL == h->disconnects)
450 {
451 GNUNET_break (0);
452 break;
453 }
454 if (msize != sizeof (struct ConnectNotifyMessage))
455 {
456 GNUNET_break (0);
457 break;
458 }
459 cnm = (const struct ConnectNotifyMessage *) msg;
460 break;
461 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
462 if (msize <
463 sizeof (struct NotifyTrafficMessage) +
464 sizeof (struct GNUNET_MessageHeader))
465 {
466 GNUNET_break (0);
467 break;
468 }
469 ntm = (const struct NotifyTrafficMessage *) msg;
470 em = (const struct GNUNET_MessageHeader *) &ntm[1];
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Received message of type %u from peer `%4s'\n",
473 ntohs (em->type), GNUNET_i2s (&ntm->peer));
474 if ((GNUNET_NO == h->inbound_hdr_only) &&
475 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
476 {
477 GNUNET_break (0);
478 break;
479 }
480 et = ntohs (em->type);
481 for (hpos = 0; hpos < h->hcnt; hpos++)
482 {
483 mh = &h->handlers[hpos];
484 if (mh->type != et)
485 continue;
486 if ((mh->expected_size != ntohs (em->size)) &&
487 (mh->expected_size != 0))
488 {
489 GNUNET_break (0);
490 continue;
491 }
492 if (GNUNET_OK !=
493 h->handlers[hpos].callback (h->cls, &ntm->peer, em))
494 {
495 /* error in processing, disconnect ! */
496 reconnect (h);
497 return;
498 }
499 }
500 if (NULL != h->inbound_notify)
501 h->inbound_notify (h->cls, &ntm->peer, em);
502 break;
503 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
504 if (msize <
505 sizeof (struct NotifyTrafficMessage) +
506 sizeof (struct GNUNET_MessageHeader))
507 {
508 GNUNET_break (0);
509 break;
510 }
511 ntm = (const struct NotifyTrafficMessage *) msg;
512 em = (const struct GNUNET_MessageHeader *) &ntm[1];
513 if ((GNUNET_NO == h->outbound_hdr_only) &&
514 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
515 {
516 GNUNET_break (0);
517 break;
518 }
519 if (NULL == h->outbound_notify)
520 {
521 GNUNET_break (0);
522 break;
523 }
524 h->outbound_notify (h->cls, &ntm->peer, em);
525 break;
526 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
527 if (msize != sizeof (struct ConfigurationInfoMessage))
528 {
529 GNUNET_break (0);
530 break;
531 }
532 if (NULL == h->submitted)
533 break;
534 cim = (const struct ConfigurationInfoMessage *) msg;
535
536 /* process configuration data */
537 if (h->submitted->info != NULL)
538 h->submitted->info (h->submitted->info_cls,
539 &h->submitted->peer,
540 ntohl (cim->bpm_in),
541 ntohl (cim->bpm_out),
542 GNUNET_TIME_relative_ntoh (cim->latency),
543 (int) ntohl (cim->reserved_amount),
544 cim->preference);
545 /* done, clean up! */
546 GNUNET_CORE_notify_transmit_ready_cancel (h->submitted);
547 trigger_next_request (h);
548 break;
549 case GNUNET_MESSAGE_TYPE_CORE_SOLICIT_TRAFFIC:
550 if (msize != sizeof (struct SolicitTrafficMessage))
551 {
552 GNUNET_break (0);
553 break;
554 }
555 stm = (const struct SolicitTrafficMessage *) msg;
556 if (NULL == h->bfc)
557 {
558 GNUNET_break (0);
559 break;
560 }
561 ss = ntohl (stm->solicit_size);
562 if ((ss > GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
563 (ss + sizeof (struct SendMessage) > GNUNET_SERVER_MAX_MESSAGE_SIZE))
564 {
565 GNUNET_break (0);
566 break;
567 }
568 solicit_traffic (h, &stm->peer, ss);
569 break;
570 default:
571 GNUNET_break (0);
572 break;
573 }
574 GNUNET_CLIENT_receive (h->client,
575 &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
576}
577
578
579
580/**
581 * Function called when we are ready to transmit our
582 * "START" message (or when this operation timed out).
583 *
584 * @param cls closure
585 * @param size number of bytes available in buf
586 * @param buf where the callee should write the message
587 * @return number of bytes written to buf
588 */
589static size_t transmit_start (void *cls, size_t size, void *buf);
590
591
592/**
593 * Function called on the first message received from
594 * the service (contains our public key, etc.).
595 * Should trigger calling the init callback
596 * and then start our regular message processing.
597 *
598 * @param cls closure
599 * @param msg message received, NULL on timeout or fatal error
600 */
601static void
602init_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg)
603{
604 struct GNUNET_CORE_Handle *h = cls;
605 const struct InitReplyMessage *m;
606 GNUNET_CORE_StartupCallback init;
607 struct GNUNET_PeerIdentity my_identity;
608
609 if ((msg == NULL) ||
610 (ntohs (msg->size) != sizeof (struct InitReplyMessage)) ||
611 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY))
612 {
613 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
614 _
615 ("Error connecting to core service (failed to receive `%s' message).\n"),
616 "INIT_REPLY");
617 GNUNET_break (msg == NULL);
618 transmit_start (h, 0, NULL);
619 return;
620 }
621 m = (const struct InitReplyMessage *) msg;
622 /* start our message processing loop */
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 _
625 ("Successfully connected to core service, starting processing loop.\n"));
626 h->currently_down = GNUNET_NO;
627 trigger_next_request (h);
628 GNUNET_CLIENT_receive (h->client,
629 &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
630 if (NULL != (init = h->init))
631 {
632 /* mark so we don't call init on reconnect */
633 h->init = NULL;
634 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
635 _("Successfully connected to core service.\n"));
636 GNUNET_CRYPTO_hash (&m->publicKey,
637 sizeof (struct
638 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
639 &my_identity.hashPubKey);
640 init (h->cls, h, &my_identity, &m->publicKey);
641 }
642}
643
644
645static void
646reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
647{
648 struct GNUNET_CORE_Handle *h = cls;
649 h->reconnect_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
650 reconnect (h);
651}
652
653
654/**
655 * Function called when we are ready to transmit our
656 * "START" message (or when this operation timed out).
657 *
658 * @param cls closure
659 * @param size number of bytes available in buf
660 * @param buf where the callee should write the message
661 * @return number of bytes written to buf
662 */
663static size_t
664transmit_start (void *cls, size_t size, void *buf)
665{
666 struct GNUNET_CORE_Handle *h = cls;
667 struct InitMessage *init;
668 uint16_t *ts;
669 uint16_t msize;
670 uint32_t opt;
671 unsigned int hpos;
672 struct GNUNET_TIME_Relative delay;
673
674 h->th = NULL;
675 if (size == 0)
676 {
677 if ((h->init == NULL) ||
678 (GNUNET_TIME_absolute_get ().value < h->startup_timeout.value))
679 {
680 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
681 _("Failed to connect to core service, retrying.\n"));
682 delay = GNUNET_TIME_absolute_get_remaining (h->startup_timeout);
683 if ((h->init == NULL) || (delay.value > 1000))
684 delay = GNUNET_TIME_UNIT_SECONDS;
685 if (h->init == NULL)
686 h->startup_timeout =
687 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
688 h->reconnect_task =
689 GNUNET_SCHEDULER_add_delayed (h->sched, GNUNET_NO,
690 GNUNET_SCHEDULER_PRIORITY_IDLE,
691 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
692 delay, &reconnect_task, h);
693 return 0;
694 }
695 /* timeout on initial connect */
696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
697 _("Failed to connect to core service, giving up.\n"));
698 h->init (h->cls, NULL, NULL, NULL);
699 GNUNET_CORE_disconnect (h);
700 return 0;
701 }
702 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
703 GNUNET_assert (size >= msize);
704 init = buf;
705 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
706 init->header.size = htons (msize);
707 opt = GNUNET_CORE_OPTION_NOTHING;
708 if (h->connects != NULL)
709 opt |= GNUNET_CORE_OPTION_SEND_CONNECT;
710 if (h->disconnects != NULL)
711 opt |= GNUNET_CORE_OPTION_SEND_DISCONNECT;
712 if (h->bfc != NULL)
713 opt |= GNUNET_CORE_OPTION_SEND_BFC;
714 if (h->inbound_notify != NULL)
715 {
716 if (h->inbound_hdr_only)
717 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
718 else
719 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
720 }
721 if (h->outbound_notify != NULL)
722 {
723 if (h->outbound_hdr_only)
724 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
725 else
726 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
727 }
728 init->options = htonl (opt);
729 ts = (uint16_t *) & init[1];
730 for (hpos = 0; hpos < h->hcnt; hpos++)
731 ts[hpos] = htons (h->handlers[hpos].type);
732 GNUNET_CLIENT_receive (h->client,
733 &init_reply_handler,
734 h,
735 GNUNET_TIME_absolute_get_remaining (h->
736 startup_timeout));
737 return sizeof (struct InitMessage) + h->hcnt * sizeof (uint16_t);
738}
739
740
741/**
742 * Connect to the core service. Note that the connection may
743 * complete (or fail) asynchronously.
744 *
745 * @param sched scheduler to use
746 * @param cfg configuration to use
747 * @param timeout after how long should we give up trying to connect to the core service?
748 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
749 * @param init callback to call on timeout or once we have successfully
750 * connected to the core service
751 * @param connects function to call on peer connect, can be NULL
752 * @param disconnects function to call on peer disconnect / timeout, can be NULL
753 * @param bfc function to call to fill up spare bandwidth, can be NULL
754 * @param inbound_notify function to call for all inbound messages, can be NULL
755 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
756 * GNUNET_MessageHeader and hence we do not need to give it the full message;
757 * can be used to improve efficiency, ignored if inbound_notify is NULLL
758 * @param outbound_notify function to call for all outbound messages, can be NULL
759 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
760 * GNUNET_MessageHeader and hence we do not need to give it the full message
761 * can be used to improve efficiency, ignored if outbound_notify is NULLL
762 * @param handlers callbacks for messages we care about, NULL-terminated
763 */
764void
765GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
766 struct GNUNET_CONFIGURATION_Handle *cfg,
767 struct GNUNET_TIME_Relative timeout,
768 void *cls,
769 GNUNET_CORE_StartupCallback init,
770 GNUNET_CORE_ClientEventHandler connects,
771 GNUNET_CORE_ClientEventHandler disconnects,
772 GNUNET_CORE_BufferFillCallback bfc,
773 GNUNET_CORE_MessageCallback inbound_notify,
774 int inbound_hdr_only,
775 GNUNET_CORE_MessageCallback outbound_notify,
776 int outbound_hdr_only,
777 const struct GNUNET_CORE_MessageHandler *handlers)
778{
779 struct GNUNET_CORE_Handle *h;
780
781 GNUNET_assert (init != NULL);
782 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
783 h->sched = sched;
784 h->cfg = cfg;
785 h->cls = cls;
786 h->init = init;
787 h->connects = connects;
788 h->disconnects = disconnects;
789 h->bfc = bfc;
790 h->inbound_notify = inbound_notify;
791 h->outbound_notify = outbound_notify;
792 h->inbound_hdr_only = inbound_hdr_only;
793 h->outbound_hdr_only = outbound_hdr_only;
794 h->handlers = handlers;
795 h->client = GNUNET_CLIENT_connect (sched, "core", cfg);
796 h->startup_timeout = GNUNET_TIME_relative_to_absolute (timeout);
797 h->hcnt = 0;
798 while (handlers[h->hcnt].callback != NULL)
799 h->hcnt++;
800 GNUNET_assert (h->hcnt <
801 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
802 sizeof (struct InitMessage)) / sizeof (uint16_t));
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
804 "Trying to connect to core service in next %llu ms.\n",
805 timeout.value);
806 h->th =
807 GNUNET_CLIENT_notify_transmit_ready (h->client,
808 sizeof (struct InitMessage) +
809 sizeof (uint16_t) * h->hcnt, timeout,
810 &transmit_start, h);
811}
812
813
814/**
815 * Disconnect from the core service.
816 *
817 * @param handle connection to core to disconnect
818 */
819void
820GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
821{
822 if (handle->th != NULL)
823 GNUNET_NETWORK_notify_transmit_ready_cancel (handle->th);
824 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
825 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
826 GNUNET_CLIENT_disconnect (handle->client);
827 GNUNET_free (handle);
828}
829
830
831/**
832 * Build the configure message.
833 */
834static size_t
835produce_configure_message (void *cls, size_t size, void *buf)
836{
837 struct GNUNET_CORE_TransmitHandle *th = cls;
838 struct GNUNET_CORE_Handle *ch = th->ch;
839
840 if (buf == NULL)
841 {
842 /* communicate handle timeout/error! */
843 if (th->info != NULL)
844 th->info (th->info_cls, NULL, 0, 0, GNUNET_TIME_UNIT_ZERO, 0, 0.0);
845 if (th->timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
846 GNUNET_CORE_notify_transmit_ready_cancel (th);
847 if (ch->submitted == th)
848 ch->submitted = NULL;
849 trigger_next_request (ch);
850 return 0;
851 }
852 GNUNET_assert (size >= sizeof (struct RequestConfigureMessage));
853 memcpy (buf, &th[1], sizeof (struct RequestConfigureMessage));
854 if (th->prev == NULL)
855 ch->pending_head = th->next;
856 else
857 th->prev->next = th->next;
858 if (th->next == NULL)
859 ch->pending_tail = th->prev;
860 else
861 th->next->prev = th->prev;
862 GNUNET_assert (ch->submitted == NULL);
863 ch->submitted = th;
864 return sizeof (struct RequestConfigureMessage);
865}
866
867
868/**
869 * Obtain statistics and/or change preferences for the given peer.
870 *
871 * @param handle connection to core to use
872 * @param peer identifies the peer
873 * @param timeout after how long should we give up (and call "info" with NULL
874 * for "peer" to signal an error)?
875 * @param bpm_out set to the current bandwidth limit (sending) for this peer,
876 * caller should set "bpm_out" to "-1" to avoid changing
877 * the current value; otherwise "bpm_out" will be lowered to
878 * the specified value; passing a pointer to "0" can be used to force
879 * us to disconnect from the peer; "bpm_out" might not increase
880 * as specified since the upper bound is generally
881 * determined by the other peer!
882 * @param amount reserve N bytes for receiving, negative
883 * amounts can be used to undo a (recent) reservation;
884 * @param preference increase incoming traffic share preference by this amount;
885 * in the absence of "amount" reservations, we use this
886 * preference value to assign proportional bandwidth shares
887 * to all connected peers
888 * @param info function to call with the resulting configuration information
889 * @param info_cls closure for info
890 */
891void
892GNUNET_CORE_peer_configure (struct GNUNET_CORE_Handle *handle,
893 const struct GNUNET_PeerIdentity *peer,
894 struct GNUNET_TIME_Relative timeout,
895 unsigned int bpm_out,
896 int amount,
897 double preference,
898 GNUNET_CORE_PeerConfigurationInfoCallback info,
899 void *info_cls)
900{
901 struct RequestConfigureMessage *rcm;
902 struct GNUNET_CORE_TransmitHandle *th;
903
904 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle) +
905 sizeof (struct RequestConfigureMessage));
906 /* append to list */
907 th->prev = handle->pending_tail;
908 if (handle->pending_tail == NULL)
909 handle->pending_head = th;
910 else
911 handle->pending_tail->next = th;
912 th->ch = handle;
913 th->get_message = &produce_configure_message;
914 th->get_message_cls = th;
915 th->info = info;
916 th->info_cls = info_cls;
917 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
918 th->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched,
919 GNUNET_NO,
920 GNUNET_SCHEDULER_PRIORITY_KEEP,
921 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
922 timeout,
923 &timeout_request, th);
924 th->msize = sizeof (struct RequestConfigureMessage);
925 rcm = (struct RequestConfigureMessage *) &th[1];
926 rcm->header.size = htons (sizeof (struct RequestConfigureMessage));
927 rcm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONFIGURE);
928 rcm->reserved = htonl (0);
929 rcm->limit_outbound_bpm = htonl (bpm_out);
930 rcm->reserve_inbound = htonl (amount);
931 rcm->preference_change = preference;
932 rcm->peer = *peer;
933 if (handle->pending_head == th)
934 trigger_next_request (handle);
935}
936
937
938/**
939 * Build the message requesting data transmission.
940 */
941static size_t
942produce_send (void *cls, size_t size, void *buf)
943{
944 struct GNUNET_CORE_TransmitHandle *th = cls;
945 struct GNUNET_CORE_Handle *h;
946 struct SendMessage *sm;
947 size_t dt;
948 GNUNET_NETWORK_TransmitReadyNotify notify;
949 void *notify_cls;
950
951 h = th->ch;
952 if (buf == NULL)
953 {
954 /* timeout or error */
955 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
956 if (th->timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
957 GNUNET_CORE_notify_transmit_ready_cancel (th);
958 trigger_next_request (h);
959 return 0;
960 }
961 GNUNET_assert (th->timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
962 sm = (struct SendMessage *) buf;
963 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
964 sm->priority = htonl (th->priority);
965 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
966 sm->peer = th->peer;
967 notify = th->notify;
968 notify_cls = th->notify_cls;
969 GNUNET_CORE_notify_transmit_ready_cancel (th);
970 trigger_next_request (h);
971 GNUNET_assert (size >= sizeof (struct SendMessage));
972 dt = notify (notify_cls, size - sizeof (struct SendMessage), &sm[1]);
973 sm->header.size = htons (dt + sizeof (struct SendMessage));
974 GNUNET_assert (dt + sizeof (struct SendMessage) < size);
975 return dt + sizeof (struct SendMessage);
976}
977
978
979/**
980 * Ask the core to call "notify" once it is ready to transmit the
981 * given number of bytes to the specified "target". If we are not yet
982 * connected to the specified peer, a call to this function will cause
983 * us to try to establish a connection.
984 *
985 * @param handle connection to core service
986 * @param priority how important is the message?
987 * @param maxdelay how long can the message wait?
988 * @param target who should receive the message,
989 * use NULL for this peer (loopback)
990 * @param notify_size how many bytes of buffer space does notify want?
991 * @param notify function to call when buffer space is available
992 * @param notify_cls closure for notify
993 * @return non-NULL if the notify callback was queued,
994 * NULL if we can not even queue the request (insufficient
995 * memory); if NULL is returned, "notify" will NOT be called.
996 */
997struct GNUNET_CORE_TransmitHandle *
998GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
999 unsigned int priority,
1000 struct GNUNET_TIME_Relative maxdelay,
1001 const struct GNUNET_PeerIdentity *target,
1002 size_t notify_size,
1003 GNUNET_NETWORK_TransmitReadyNotify notify,
1004 void *notify_cls)
1005{
1006 struct GNUNET_CORE_TransmitHandle *th;
1007
1008 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1009 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1010 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1011 th->ch = handle;
1012 /* append to list */
1013 th->prev = handle->pending_tail;
1014 if (handle->pending_tail == NULL)
1015 handle->pending_head = th;
1016 else
1017 handle->pending_tail->next = th;
1018 th->get_message = &produce_send;
1019 th->get_message_cls = th;
1020 th->notify = notify;
1021 th->notify_cls = notify_cls;
1022 th->peer = *target;
1023 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1024 th->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched,
1025 GNUNET_NO,
1026 GNUNET_SCHEDULER_PRIORITY_KEEP,
1027 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1028 maxdelay,
1029 &timeout_request, th);
1030 th->priority = priority;
1031 th->msize = sizeof (struct SendMessage) + notify_size;
1032 /* was the request queue previously empty? */
1033 if (handle->pending_head == th)
1034 trigger_next_request (handle);
1035 return NULL;
1036}
1037
1038
1039/**
1040 * Cancel the specified transmission-ready notification.
1041 *
1042 * @param h handle that was returned by "notify_transmit_ready".
1043 */
1044void
1045GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1046 *h)
1047{
1048 struct GNUNET_CORE_Handle *handle = h->ch;
1049
1050 if (handle->submitted == h)
1051 {
1052 handle->submitted = NULL;
1053 }
1054 else
1055 {
1056 if (h->prev == NULL)
1057 handle->pending_head = h->next;
1058 else
1059 h->prev->next = h->next;
1060 if (h->next == NULL)
1061 handle->pending_tail = h->prev;
1062 else
1063 h->next->prev = h->prev;
1064 }
1065 if (h->timeout_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1066 GNUNET_SCHEDULER_cancel (handle->sched, h->timeout_task);
1067 GNUNET_free (h);
1068}
1069
1070
1071/* end of core_api.c */
diff --git a/src/core/gnunet-service-core.c b/src/core/gnunet-service-core.c
new file mode 100644
index 000000000..e9e076bcc
--- /dev/null
+++ b/src/core/gnunet-service-core.c
@@ -0,0 +1,2859 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 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 2, 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 * TODO:
27 * TESTING:
28 * - write test for basic core functions:
29 * + connect to peer
30 * + transmit (encrypted) message [with handshake]
31 * + receive (encrypted) message, forward plaintext to clients
32 * POST-TESTING:
33 * - revisit API (which arguments are used, needed)?
34 * - add code to bound queue size when handling client's SEND message
35 * - add code to bound message queue size when passing messages to clients
36 * - add code to discard_expired_messages
37 * - add code to re-transmit key if first attempt failed
38 * + timeout on connect / key exchange, etc.
39 * + timeout for automatic re-try, etc.
40 * - add code to give up re-transmission of key if many attempts fail
41 * - add code to send PINGs if we are about to time-out otherwise
42 * ? add heuristic to do another send_key in "handle_set_key"
43 * in case previous attempt failed / didn't work / persist
44 * (but don't do it always to avoid storm of SET_KEY's going
45 * back and forth!) --- alternatively, add "status" field
46 * of the other peer to the set key message, that way we'd
47 * know for sure!
48 * - check that hostkey used by transport (for HELLOs) is the
49 * same as the hostkey that we are using!
50 * - free list of clients on exit
51 * - topology management:
52 * + bootstrapping (transport offer hello, plugins)
53 * + internal neighbour selection
54 * + update bandwidth usage statistics
55 * + bandwidth allocation (transport set quota)
56 * - optimize lookup (many O(n) list traversals
57 * could ideally be changed to O(1) hash map lookups)
58 */
59#include "platform.h"
60#include "gnunet_util_lib.h"
61#include "gnunet_hello_lib.h"
62#include "gnunet_peerinfo_service.h"
63#include "gnunet_protocols.h"
64#include "gnunet_signatures.h"
65#include "gnunet_transport_service.h"
66#include "core.h"
67
68
69/**
70 * Receive and send buffer windows grow over time. For
71 * how long can 'unused' bandwidth accumulate before we
72 * need to cap it? (specified in ms).
73 */
74#define MAX_WINDOW_TIME (5 * 60 * 1000)
75
76
77/**
78 * Amount of bytes per minute (in/out) to assume initially
79 * (before either peer has communicated any particular
80 * preference). Should be rather low.
81 */
82#define DEFAULT_BPM_IN_OUT 2048
83
84
85/**
86 * What is the maximum delay for a SET_KEY message?
87 */
88#define MAX_SET_KEY_DELAY GNUNET_TIME_UNIT_SECONDS
89
90
91/**
92 * What how long do we wait for SET_KEY confirmation initially?
93 */
94#define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
95
96
97/**
98 * What is the maximum delay for a PING message?
99 */
100#define MAX_PING_DELAY GNUNET_TIME_UNIT_SECONDS
101
102
103/**
104 * What is the maximum delay for a PONG message?
105 */
106#define MAX_PONG_DELAY GNUNET_TIME_UNIT_SECONDS
107
108
109/**
110 * What is the priority for a SET_KEY message?
111 */
112#define SET_KEY_PRIORITY 0xFFFFFF
113
114
115/**
116 * What is the priority for a PING message?
117 */
118#define PING_PRIORITY 0xFFFFFF
119
120
121/**
122 * What is the priority for a PONG message?
123 */
124#define PONG_PRIORITY 0xFFFFFF
125
126
127/**
128 * What is the maximum age of a message for us to consider
129 * processing it? Note that this looks at the timestamp used
130 * by the other peer, so clock skew between machines does
131 * come into play here. So this should be picked high enough
132 * so that a little bit of clock skew does not prevent peers
133 * from connecting to us.
134 */
135#define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
136
137
138/**
139 * What is the maximum size for encrypted messages? Note that this
140 * number imposes a clear limit on the maximum size of any message.
141 * Set to a value close to 64k but not so close that transports will
142 * have trouble with their headers.
143 */
144#define MAX_ENCRYPTED_MESSAGE_SIZE (63 * 1024)
145
146
147/**
148 * State machine for our P2P encryption handshake. Everyone starts in
149 * "DOWN", if we receive the other peer's key (other peer initiated)
150 * we start in state RECEIVED (since we will immediately send our
151 * own); otherwise we start in SENT. If we get back a PONG from
152 * within either state, we move up to CONFIRMED (the PONG will always
153 * be sent back encrypted with the key we sent to the other peer).
154 */
155enum PeerStateMachine
156{
157 PEER_STATE_DOWN,
158 PEER_STATE_KEY_SENT,
159 PEER_STATE_KEY_RECEIVED,
160 PEER_STATE_KEY_CONFIRMED
161};
162
163
164/**
165 * Number of bytes (at the beginning) of "struct EncryptedMessage"
166 * that are NOT encrypted.
167 */
168#define ENCRYPTED_HEADER_SIZE (sizeof(struct GNUNET_MessageHeader) + sizeof(uint32_t) + sizeof(GNUNET_HashCode))
169
170/**
171 * Encapsulation for encrypted messages exchanged between
172 * peers. Followed by the actual encrypted data.
173 */
174struct EncryptedMessage
175{
176 /**
177 * Message type is either CORE_ENCRYPTED_MESSAGE.
178 */
179 struct GNUNET_MessageHeader header;
180
181 /**
182 * Always zero.
183 */
184 uint32_t reserved GNUNET_PACKED;
185
186 /**
187 * Hash of the plaintext, used to verify message integrity;
188 * ALSO used as the IV for the symmetric cipher! Everything
189 * after this hash will be encrypted. ENCRYPTED_HEADER_SIZE
190 * must be set to the offset of the next field.
191 */
192 GNUNET_HashCode plaintext_hash;
193
194 /**
195 * Sequence number, in network byte order. This field
196 * must be the first encrypted/decrypted field and the
197 * first byte that is hashed for the plaintext hash.
198 */
199 uint32_t sequence_number GNUNET_PACKED;
200
201 /**
202 * Desired bandwidth (how much we should send to this
203 * peer / how much is the sender willing to receive),
204 * in bytes per minute.
205 */
206 uint32_t inbound_bpm_limit GNUNET_PACKED;
207
208 /**
209 * Timestamp. Used to prevent reply of ancient messages
210 * (recent messages are caught with the sequence number).
211 */
212 struct GNUNET_TIME_AbsoluteNBO timestamp;
213
214};
215
216/**
217 * We're sending an (encrypted) PING to the other peer to check if he
218 * can decrypt. The other peer should respond with a PONG with the
219 * same content, except this time encrypted with the receiver's key.
220 */
221struct PingMessage
222{
223 /**
224 * Message type is either CORE_PING or CORE_PONG.
225 */
226 struct GNUNET_MessageHeader header;
227
228 /**
229 * Random number chosen to make reply harder.
230 */
231 uint32_t challenge GNUNET_PACKED;
232
233 /**
234 * Intended target of the PING, used primarily to check
235 * that decryption actually worked.
236 */
237 struct GNUNET_PeerIdentity target;
238};
239
240
241/**
242 * Message transmitted to set (or update) a session key.
243 */
244struct SetKeyMessage
245{
246
247 /**
248 * Message type is either CORE_SET_KEY.
249 */
250 struct GNUNET_MessageHeader header;
251
252 /**
253 * Status of the sender (should be in "enum PeerStateMachine"), nbo.
254 */
255 int32_t sender_status GNUNET_PACKED;
256
257 /**
258 * Purpose of the signature, will be
259 * GNUNET_SIGNATURE_PURPOSE_SET_KEY.
260 */
261 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
262
263 /**
264 * At what time was this key created?
265 */
266 struct GNUNET_TIME_AbsoluteNBO creation_time;
267
268 /**
269 * The encrypted session key.
270 */
271 struct GNUNET_CRYPTO_RsaEncryptedData encrypted_key;
272
273 /**
274 * Who is the intended recipient?
275 */
276 struct GNUNET_PeerIdentity target;
277
278 /**
279 * Signature of the stuff above (starting at purpose).
280 */
281 struct GNUNET_CRYPTO_RsaSignature signature;
282
283};
284
285
286/**
287 * Message waiting for transmission. This struct
288 * is followed by the actual content of the message.
289 */
290struct MessageEntry
291{
292
293 /**
294 * We keep messages in a linked list (for now).
295 */
296 struct MessageEntry *next;
297
298 /**
299 * By when are we supposed to transmit this message?
300 */
301 struct GNUNET_TIME_Absolute deadline;
302
303 /**
304 * How important is this message to us?
305 */
306 unsigned int priority;
307
308 /**
309 * How long is the message? (number of bytes following
310 * the "struct MessageEntry", but not including the
311 * size of "struct MessageEntry" itself!)
312 */
313 uint16_t size;
314
315 /**
316 * Was this message selected for transmission in the
317 * current round? GNUNET_YES or GNUNET_NO.
318 */
319 int16_t do_transmit;
320
321};
322
323
324struct Neighbour
325{
326 /**
327 * We keep neighbours in a linked list (for now).
328 */
329 struct Neighbour *next;
330
331 /**
332 * Unencrypted messages destined for this peer.
333 */
334 struct MessageEntry *messages;
335
336 /**
337 * Head of the batched, encrypted message queue (already ordered,
338 * transmit starting with the head).
339 */
340 struct MessageEntry *encrypted_head;
341
342 /**
343 * Tail of the batched, encrypted message queue (already ordered,
344 * append new messages to tail)
345 */
346 struct MessageEntry *encrypted_tail;
347
348 /**
349 * Handle for pending requests for transmission to this peer
350 * with the transport service. NULL if no request is pending.
351 */
352 struct GNUNET_TRANSPORT_TransmitHandle *th;
353
354 /**
355 * Public key of the neighbour, NULL if we don't have it yet.
356 */
357 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
358
359 /**
360 * We received a PING message before we got the "public_key"
361 * (or the SET_KEY). We keep it here until we have a key
362 * to decrypt it. NULL if no PING is pending.
363 */
364 struct PingMessage *pending_ping;
365
366 /**
367 * Identity of the neighbour.
368 */
369 struct GNUNET_PeerIdentity peer;
370
371 /**
372 * Key we use to encrypt our messages for the other peer
373 * (initialized by us when we do the handshake).
374 */
375 struct GNUNET_CRYPTO_AesSessionKey encrypt_key;
376
377 /**
378 * Key we use to decrypt messages from the other peer
379 * (given to us by the other peer during the handshake).
380 */
381 struct GNUNET_CRYPTO_AesSessionKey decrypt_key;
382
383 /**
384 * ID of task used for re-trying plaintext scheduling.
385 */
386 GNUNET_SCHEDULER_TaskIdentifier retry_plaintext_task;
387
388 /**
389 * ID of task used for re-trying SET_KEY and PING message.
390 */
391 GNUNET_SCHEDULER_TaskIdentifier retry_set_key_task;
392
393 /**
394 * At what time did we generate our encryption key?
395 */
396 struct GNUNET_TIME_Absolute encrypt_key_created;
397
398 /**
399 * At what time did the other peer generate the decryption key?
400 */
401 struct GNUNET_TIME_Absolute decrypt_key_created;
402
403 /**
404 * At what time did we initially establish (as in, complete session
405 * key handshake) this connection? Should be zero if status != KEY_CONFIRMED.
406 */
407 struct GNUNET_TIME_Absolute time_established;
408
409 /**
410 * At what time did we last receive an encrypted message from the
411 * other peer? Should be zero if status != KEY_CONFIRMED.
412 */
413 struct GNUNET_TIME_Absolute last_activity;
414
415 /**
416 * Last latency observed from this peer.
417 */
418 struct GNUNET_TIME_Relative last_latency;
419
420 /**
421 * At what frequency are we currently re-trying SET KEY messages?
422 */
423 struct GNUNET_TIME_Relative set_key_retry_frequency;
424
425 /**
426 * Time of our last update to the "available_send_window".
427 */
428 struct GNUNET_TIME_Absolute last_asw_update;
429
430 /**
431 * Time of our last update to the "available_recv_window".
432 */
433 struct GNUNET_TIME_Absolute last_arw_update;
434
435 /**
436 * Number of bytes that we are eligible to transmit to this
437 * peer at this point. Incremented every minute by max_out_bpm,
438 * bounded by max_bpm (no back-log larger than MAX_BUF_FACT minutes,
439 * bandwidth-hogs are sampled at a frequency of about 78s!);
440 * may get negative if we have VERY high priority content.
441 */
442 long long available_send_window;
443
444 /**
445 * How much downstream capacity of this peer has been reserved for
446 * our traffic? (Our clients can request that a certain amount of
447 * bandwidth is available for replies to them; this value is used to
448 * make sure that this reserved amount of bandwidth is actually
449 * available).
450 */
451 long long available_recv_window;
452
453 /**
454 * How valueable were the messages of this peer recently?
455 */
456 double current_preference;
457
458 /**
459 * Bit map indicating which of the 32 sequence numbers before the last
460 * were received (good for accepting out-of-order packets and
461 * estimating reliability of the connection)
462 */
463 unsigned int last_packets_bitmap;
464
465 /**
466 * Number of messages in the message queue for this peer.
467 */
468 unsigned int message_queue_size;
469
470 /**
471 * last sequence number received on this connection (highest)
472 */
473 uint32_t last_sequence_number_received;
474
475 /**
476 * last sequence number transmitted
477 */
478 uint32_t last_sequence_number_sent;
479
480 /**
481 * Available bandwidth in for this peer (current target).
482 */
483 uint32_t bpm_in;
484
485 /**
486 * Available bandwidth out for this peer (current target).
487 */
488 uint32_t bpm_out;
489
490 /**
491 * Internal bandwidth limit set for this peer (initially
492 * typcially set to "-1"). "bpm_out" is MAX of
493 * "bpm_out_internal_limit" and "bpm_out_external_limit".
494 */
495 uint32_t bpm_out_internal_limit;
496
497 /**
498 * External bandwidth limit set for this peer by the
499 * peer that we are communicating with. "bpm_out" is MAX of
500 * "bpm_out_internal_limit" and "bpm_out_external_limit".
501 */
502 uint32_t bpm_out_external_limit;
503
504 /**
505 * What was our PING challenge number?
506 */
507 uint32_t ping_challenge;
508
509 /**
510 * What is our connection status?
511 */
512 enum PeerStateMachine status;
513
514};
515
516
517/**
518 * Events are messages for clients. The struct
519 * itself is followed by the actual message.
520 */
521struct Event
522{
523 /**
524 * This is a linked list.
525 */
526 struct Event *next;
527
528 /**
529 * Size of the message.
530 */
531 size_t size;
532
533 /**
534 * Could this event be dropped if this queue
535 * is getting too large? (NOT YET USED!)
536 */
537 int can_drop;
538
539};
540
541
542/**
543 * Data structure for each client connected to the core service.
544 */
545struct Client
546{
547 /**
548 * Clients are kept in a linked list.
549 */
550 struct Client *next;
551
552 /**
553 * Handle for the client with the server API.
554 */
555 struct GNUNET_SERVER_Client *client_handle;
556
557 /**
558 * Linked list of messages we still need to deliver to
559 * this client.
560 */
561 struct Event *event_head;
562
563 /**
564 * Tail of the linked list of events.
565 */
566 struct Event *event_tail;
567
568 /**
569 * Current transmit handle, NULL if no transmission request
570 * is pending.
571 */
572 struct GNUNET_NETWORK_TransmitHandle *th;
573
574 /**
575 * Array of the types of messages this peer cares
576 * about (with "tcnt" entries). Allocated as part
577 * of this client struct, do not free!
578 */
579 uint16_t *types;
580
581 /**
582 * Options for messages this client cares about,
583 * see GNUNET_CORE_OPTION_ values.
584 */
585 uint32_t options;
586
587 /**
588 * Number of types of incoming messages this client
589 * specifically cares about. Size of the "types" array.
590 */
591 unsigned int tcnt;
592
593};
594
595
596/**
597 * Our public key.
598 */
599static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
600
601/**
602 * Our identity.
603 */
604static struct GNUNET_PeerIdentity my_identity;
605
606/**
607 * Our private key.
608 */
609static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
610
611/**
612 * Our scheduler.
613 */
614struct GNUNET_SCHEDULER_Handle *sched;
615
616/**
617 * Our configuration.
618 */
619struct GNUNET_CONFIGURATION_Handle *cfg;
620
621/**
622 * Our server.
623 */
624static struct GNUNET_SERVER_Handle *server;
625
626/**
627 * Transport service.
628 */
629static struct GNUNET_TRANSPORT_Handle *transport;
630
631/**
632 * We keep neighbours in a linked list (for now).
633 */
634static struct Neighbour *neighbours;
635
636/**
637 * Linked list of our clients.
638 */
639static struct Client *clients;
640
641
642/**
643 * Recalculate the number of bytes we expect to
644 * receive or transmit in a given window.
645 *
646 * @param window pointer to the byte counter (updated)
647 * @param ts pointer to the timestamp (updated)
648 * @param bpm number of bytes per minute that should
649 * be added to the window.
650 */
651static void
652update_window (long long *window,
653 struct GNUNET_TIME_Absolute *ts, unsigned int bpm)
654{
655 struct GNUNET_TIME_Relative since;
656
657 since = GNUNET_TIME_absolute_get_duration (*ts);
658 if (since.value < 60 * 1000)
659 return; /* not even a minute has passed */
660 *ts = GNUNET_TIME_absolute_get ();
661 *window += (bpm * since.value) / 60 / 1000;
662 if (*window > MAX_WINDOW_TIME * bpm)
663 *window = MAX_WINDOW_TIME * bpm;
664}
665
666
667/**
668 * Find the entry for the given neighbour.
669 *
670 * @param peer identity of the neighbour
671 * @return NULL if we are not connected, otherwise the
672 * neighbour's entry.
673 */
674static struct Neighbour *
675find_neighbour (const struct GNUNET_PeerIdentity *peer)
676{
677 struct Neighbour *ret;
678
679 ret = neighbours;
680 while ((ret != NULL) &&
681 (0 != memcmp (&ret->peer,
682 peer, sizeof (struct GNUNET_PeerIdentity))))
683 ret = ret->next;
684 return ret;
685}
686
687
688/**
689 * Find the entry for the given client.
690 *
691 * @param client handle for the client
692 * @return NULL if we are not connected, otherwise the
693 * client's struct.
694 */
695static struct Client *
696find_client (const struct GNUNET_SERVER_Client *client)
697{
698 struct Client *ret;
699
700 ret = clients;
701 while ((ret != NULL) && (client != ret->client_handle))
702 ret = ret->next;
703 return ret;
704}
705
706
707/**
708 * If necessary, initiate a request with the server to
709 * transmit messages from the queue of the given client.
710 * @param client who to transfer messages to
711 */
712static void request_transmit (struct Client *client);
713
714
715/**
716 * Client is ready to receive data, provide it.
717 * @param cls closure
718 * @param size number of bytes available in buf
719 * @param buf where the callee should write the message
720 * @return number of bytes written to buf
721 */
722static size_t
723do_client_transmit (void *cls, size_t size, void *buf)
724{
725 struct Client *client = cls;
726 struct Event *e;
727 char *tgt;
728 size_t ret;
729
730 client->th = NULL;
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Client ready to receive %u bytes.\n", size);
733 if (buf == NULL)
734 {
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Failed to transmit data to client (disconnect)?\n");
737 return 0; /* we'll surely get a disconnect soon... */
738 }
739 tgt = buf;
740 ret = 0;
741 while ((NULL != (e = client->event_head)) && (e->size <= size))
742 {
743 memcpy (&tgt[ret], &e[1], e->size);
744 size -= e->size;
745 ret += e->size;
746 client->event_head = e->next;
747 GNUNET_free (e);
748 }
749 GNUNET_assert (ret > 0);
750 if (client->event_head == NULL)
751 client->event_tail = NULL;
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
753 "Transmitting %u bytes to client\n", ret);
754 request_transmit (client);
755 return ret;
756}
757
758
759/**
760 * If necessary, initiate a request with the server to
761 * transmit messages from the queue of the given client.
762 * @param client who to transfer messages to
763 */
764static void
765request_transmit (struct Client *client)
766{
767
768 if (NULL != client->th)
769 return; /* already pending */
770 if (NULL == client->event_head)
771 return; /* no more events pending */
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "Asking server to transmit %u bytes to client\n",
774 client->event_head->size);
775 client->th
776 = GNUNET_SERVER_notify_transmit_ready (client->client_handle,
777 client->event_head->size,
778 GNUNET_TIME_UNIT_FOREVER_REL,
779 &do_client_transmit, client);
780}
781
782
783/**
784 * Send a message to one of our clients.
785 * @param client target for the message
786 * @param msg message to transmit
787 * @param can_drop could this message be dropped if the
788 * client's queue is getting too large?
789 */
790static void
791send_to_client (struct Client *client,
792 const struct GNUNET_MessageHeader *msg, int can_drop)
793{
794 struct Event *e;
795 uint16_t msize;
796
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Preparing to send message of type %u to client.\n",
799 ntohs (msg->type));
800 msize = ntohs (msg->size);
801 e = GNUNET_malloc (sizeof (struct Event) + msize);
802 /* append */
803 if (client->event_tail != NULL)
804 client->event_tail->next = e;
805 else
806 client->event_head = e;
807 client->event_tail = e;
808 e->can_drop = can_drop;
809 e->size = msize;
810 memcpy (&e[1], msg, msize);
811 request_transmit (client);
812}
813
814
815/**
816 * Send a message to all of our current clients.
817 */
818static void
819send_to_all_clients (const struct GNUNET_MessageHeader *msg, int can_drop)
820{
821 struct Client *c;
822
823 c = clients;
824 while (c != NULL)
825 {
826 send_to_client (c, msg, can_drop);
827 c = c->next;
828 }
829}
830
831
832/**
833 * Handle CORE_INIT request.
834 */
835static void
836handle_client_init (void *cls,
837 struct GNUNET_SERVER_Handle *server,
838 struct GNUNET_SERVER_Client *client,
839 const struct GNUNET_MessageHeader *message)
840{
841 const struct InitMessage *im;
842 struct InitReplyMessage irm;
843 struct Client *c;
844 uint16_t msize;
845 const uint16_t *types;
846 struct Neighbour *n;
847 struct ConnectNotifyMessage cnm;
848
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "Client connecting to core service with `%s' message\n",
851 "INIT");
852 /* check that we don't have an entry already */
853 c = clients;
854 while (c != NULL)
855 {
856 if (client == c->client_handle)
857 {
858 GNUNET_break (0);
859 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
860 return;
861 }
862 c = c->next;
863 }
864 msize = ntohs (message->size);
865 if (msize < sizeof (struct InitMessage))
866 {
867 GNUNET_break (0);
868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
869 return;
870 }
871 im = (const struct InitMessage *) message;
872 types = (const uint16_t *) &im[1];
873 msize -= sizeof (struct InitMessage);
874 c = GNUNET_malloc (sizeof (struct Client) + msize);
875 c->client_handle = client;
876 c->next = clients;
877 clients = c;
878 memcpy (&c[1], types, msize);
879 c->types = (uint16_t *) & c[1];
880 c->options = ntohl (im->options);
881 c->tcnt = msize / sizeof (uint16_t);
882 /* send init reply message */
883 irm.header.size = htons (sizeof (struct InitReplyMessage));
884 irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
885 irm.reserved = htonl (0);
886 memcpy (&irm.publicKey,
887 &my_public_key,
888 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Sending `%s' message to client.\n", "INIT_REPLY");
891 send_to_client (c, &irm.header, GNUNET_NO);
892 /* notify new client about existing neighbours */
893 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
894 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
895 n = neighbours;
896 while (n != NULL)
897 {
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "Sending `%s' message to client.\n", "NOTIFY_CONNECT");
900 cnm.bpm_available = htonl (n->bpm_out);
901 cnm.last_activity = GNUNET_TIME_absolute_hton (n->last_activity);
902 cnm.peer = n->peer;
903 send_to_client (c, &cnm.header, GNUNET_NO);
904 n = n->next;
905 }
906 GNUNET_SERVER_receive_done (client, GNUNET_OK);
907}
908
909
910/**
911 * A client disconnected, clean up.
912 *
913 * @param cls closure
914 * @param client identification of the client
915 */
916static void
917handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
918{
919 struct Client *pos;
920 struct Client *prev;
921
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Client has disconnected from core service.\n");
924 prev = NULL;
925 pos = clients;
926 while (pos != NULL)
927 {
928 if (client == pos->client_handle)
929 {
930 if (prev == NULL)
931 clients = pos->next;
932 else
933 prev->next = pos->next;
934 if (pos->th != NULL)
935 GNUNET_NETWORK_notify_transmit_ready_cancel (pos->th);
936 GNUNET_free (pos);
937 return;
938 }
939 prev = pos;
940 pos = pos->next;
941 }
942 /* client never sent INIT */
943}
944
945
946/**
947 * Handle REQUEST_CONFIGURE request.
948 */
949static void
950handle_client_request_configure (void *cls,
951 struct GNUNET_SERVER_Handle *server,
952 struct GNUNET_SERVER_Client *client,
953 const struct GNUNET_MessageHeader *message)
954{
955 const struct RequestConfigureMessage *rcm;
956 struct Neighbour *n;
957 struct ConfigurationInfoMessage cim;
958 struct Client *c;
959 int reserv;
960
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Core service receives `%s' request.\n", "CONFIGURE");
963 rcm = (const struct RequestConfigureMessage *) message;
964 n = find_neighbour (&rcm->peer);
965 memset (&cim, 0, sizeof (cim));
966 if ((n != NULL) && (n->status == PEER_STATE_KEY_CONFIRMED))
967 {
968 n->bpm_out_internal_limit = ntohl (rcm->limit_outbound_bpm);
969 n->bpm_out = GNUNET_MAX (n->bpm_out_internal_limit,
970 n->bpm_out_external_limit);
971 reserv = ntohl (rcm->reserve_inbound);
972 if (reserv < 0)
973 {
974 n->available_recv_window += reserv;
975 }
976 else if (reserv > 0)
977 {
978 update_window (&n->available_recv_window,
979 &n->last_arw_update, n->bpm_in);
980 if (n->available_recv_window < reserv)
981 reserv = n->available_recv_window;
982 n->available_recv_window -= reserv;
983 }
984 n->current_preference += rcm->preference_change;
985 if (n->current_preference < 0)
986 n->current_preference = 0;
987 cim.reserved_amount = htonl (reserv);
988 cim.bpm_in = htonl (n->bpm_in);
989 cim.bpm_out = htonl (n->bpm_out);
990 cim.latency = GNUNET_TIME_relative_hton (n->last_latency);
991 cim.preference = n->current_preference;
992 }
993 cim.header.size = htons (sizeof (struct ConfigurationInfoMessage));
994 cim.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO);
995 cim.peer = rcm->peer;
996 c = find_client (client);
997 if (c == NULL)
998 {
999 GNUNET_break (0);
1000 return;
1001 }
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Sending `%s' message to client.\n", "CONFIGURATION_INFO");
1004 send_to_client (c, &cim.header, GNUNET_NO);
1005}
1006
1007
1008/**
1009 * Check if we have encrypted messages for the specified neighbour
1010 * pending, and if so, check with the transport about sending them
1011 * out.
1012 *
1013 * @param n neighbour to check.
1014 */
1015static void process_encrypted_neighbour_queue (struct Neighbour *n);
1016
1017
1018/**
1019 * Function called when the transport service is ready to
1020 * receive an encrypted message for the respective peer
1021 *
1022 * @param cls neighbour to use message from
1023 * @param size number of bytes we can transmit
1024 * @param buf where to copy the message
1025 * @return number of bytes transmitted
1026 */
1027static size_t
1028notify_encrypted_transmit_ready (void *cls, size_t size, void *buf)
1029{
1030 struct Neighbour *n = cls;
1031 struct MessageEntry *m;
1032 size_t ret;
1033 char *cbuf;
1034
1035 n->th = NULL;
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Transport ready to receive %u bytes for `%4s'\n",
1038 size, GNUNET_i2s (&n->peer));
1039 GNUNET_assert (NULL != (m = n->encrypted_head));
1040 n->encrypted_head = m->next;
1041 if (m->next == NULL)
1042 n->encrypted_tail = NULL;
1043 ret = 0;
1044 cbuf = buf;
1045 if (buf != NULL)
1046 {
1047 GNUNET_assert (size >= m->size);
1048 memcpy (cbuf, &m[1], m->size);
1049 ret = m->size;
1050 process_encrypted_neighbour_queue (n);
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
1053 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
1054 ret, GNUNET_i2s (&n->peer));
1055 }
1056 else
1057 {
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "Transmission for message of type %u and size %u failed\n",
1060 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
1061 m->size);
1062 }
1063 GNUNET_free (m);
1064 return ret;
1065}
1066
1067
1068/**
1069 * Check if we have plaintext messages for the specified neighbour
1070 * pending, and if so, consider batching and encrypting them (and
1071 * then trigger processing of the encrypted queue if needed).
1072 *
1073 * @param n neighbour to check.
1074 */
1075static void process_plaintext_neighbour_queue (struct Neighbour *n);
1076
1077
1078/**
1079 * Check if we have encrypted messages for the specified neighbour
1080 * pending, and if so, check with the transport about sending them
1081 * out.
1082 *
1083 * @param n neighbour to check.
1084 */
1085static void
1086process_encrypted_neighbour_queue (struct Neighbour *n)
1087{
1088 if (n->th != NULL)
1089 {
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Asked to process encrypted queue, but request already pending.\n");
1092 return; /* already pending */
1093 }
1094 if (n->encrypted_head == NULL)
1095 {
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 "Encrypted queue empty, trying plaintext queue instead.\n");
1098 process_plaintext_neighbour_queue (n);
1099 return;
1100 }
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Asking transport for transmission of %u bytes to `%4s' in next %llu ms\n",
1103 n->encrypted_head->size,
1104 GNUNET_i2s (&n->peer),
1105 GNUNET_TIME_absolute_get_remaining (n->encrypted_head->
1106 deadline).value);
1107 n->th =
1108 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer,
1109 n->encrypted_head->size,
1110 GNUNET_TIME_absolute_get_remaining
1111 (n->encrypted_head->deadline),
1112 &notify_encrypted_transmit_ready,
1113 n);
1114 if (n->th == NULL)
1115 {
1116 /* message request too large (oops) */
1117 GNUNET_break (0);
1118 /* FIXME: handle error somehow! */
1119 }
1120}
1121
1122
1123/**
1124 * Decrypt size bytes from in and write the result to out. Use the
1125 * key for inbound traffic of the given neighbour. This function does
1126 * NOT do any integrity-checks on the result.
1127 *
1128 * @param n neighbour we are receiving from
1129 * @param iv initialization vector to use
1130 * @param in ciphertext
1131 * @param out plaintext
1132 * @param size size of in/out
1133 * @return GNUNET_OK on success
1134 */
1135static int
1136do_decrypt (struct Neighbour *n,
1137 const GNUNET_HashCode * iv,
1138 const void *in, void *out, size_t size)
1139{
1140 if (size != (uint16_t) size)
1141 {
1142 GNUNET_break (0);
1143 return GNUNET_NO;
1144 }
1145 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
1146 (n->status != PEER_STATE_KEY_CONFIRMED))
1147 {
1148 GNUNET_break_op (0);
1149 return GNUNET_SYSERR;
1150 }
1151 if (size !=
1152 GNUNET_CRYPTO_aes_decrypt (&n->decrypt_key,
1153 in,
1154 (uint16_t) size,
1155 (const struct
1156 GNUNET_CRYPTO_AesInitializationVector *) iv,
1157 out))
1158 {
1159 GNUNET_break (0);
1160 return GNUNET_SYSERR;
1161 }
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "Decrypted %u bytes from `%4s' using key %u\n",
1164 size, GNUNET_i2s (&n->peer), n->decrypt_key.crc32);
1165 return GNUNET_OK;
1166}
1167
1168
1169/**
1170 * Encrypt size bytes from in and write the result to out. Use the
1171 * key for outbound traffic of the given neighbour.
1172 *
1173 * @param n neighbour we are sending to
1174 * @param iv initialization vector to use
1175 * @param in ciphertext
1176 * @param out plaintext
1177 * @param size size of in/out
1178 * @return GNUNET_OK on success
1179 */
1180static int
1181do_encrypt (struct Neighbour *n,
1182 const GNUNET_HashCode * iv,
1183 const void *in, void *out, size_t size)
1184{
1185 if (size != (uint16_t) size)
1186 {
1187 GNUNET_break (0);
1188 return GNUNET_NO;
1189 }
1190 GNUNET_assert (size ==
1191 GNUNET_CRYPTO_aes_encrypt (in,
1192 (uint16_t) size,
1193 &n->encrypt_key,
1194 (const struct
1195 GNUNET_CRYPTO_AesInitializationVector
1196 *) iv, out));
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "Encrypted %u bytes for `%4s' using key %u\n", size,
1199 GNUNET_i2s (&n->peer), n->encrypt_key.crc32);
1200 return GNUNET_OK;
1201}
1202
1203
1204/**
1205 * Select messages for transmission. This heuristic uses a combination
1206 * of earliest deadline first (EDF) scheduling (with bounded horizon)
1207 * and priority-based discard (in case no feasible schedule exist) and
1208 * speculative optimization (defer any kind of transmission until
1209 * we either create a batch of significant size, 25% of max, or until
1210 * we are close to a deadline). Furthermore, when scheduling the
1211 * heuristic also packs as many messages into the batch as possible,
1212 * starting with those with the earliest deadline. Yes, this is fun.
1213 *
1214 * @param n neighbour to select messages from
1215 * @param size number of bytes to select for transmission
1216 * @param retry_time set to the time when we should try again
1217 * (only valid if this function returns zero)
1218 * @return number of bytes selected, or 0 if we decided to
1219 * defer scheduling overall; in that case, retry_time is set.
1220 */
1221static size_t
1222select_messages (struct Neighbour *n,
1223 size_t size, struct GNUNET_TIME_Relative *retry_time)
1224{
1225 struct MessageEntry *pos;
1226 struct MessageEntry *min;
1227 struct MessageEntry *last;
1228 unsigned int min_prio;
1229 struct GNUNET_TIME_Absolute t;
1230 struct GNUNET_TIME_Absolute now;
1231 uint64_t delta;
1232 uint64_t avail;
1233 unsigned long long slack; /* how long could we wait before missing deadlines? */
1234 size_t off;
1235 int discard_low_prio;
1236
1237 GNUNET_assert (NULL != n->messages);
1238 now = GNUNET_TIME_absolute_get ();
1239 /* last entry in linked list of messages processed */
1240 last = NULL;
1241 /* should we remove the entry with the lowest
1242 priority from consideration for scheduling at the
1243 end of the loop? */
1244 discard_low_prio = GNUNET_YES;
1245 while (GNUNET_YES == discard_low_prio)
1246 {
1247 min = NULL;
1248 min_prio = -1;
1249 discard_low_prio = GNUNET_NO;
1250 /* number of bytes available for transmission at time "t" */
1251 avail = n->available_send_window;
1252 t = n->last_asw_update;
1253 /* how many bytes have we (hyptothetically) scheduled so far */
1254 off = 0;
1255 /* maximum time we can wait before transmitting anything
1256 and still make all of our deadlines */
1257 slack = -1;
1258
1259 pos = n->messages;
1260 /* note that we use "*2" here because we want to look
1261 a bit further into the future; much more makes no
1262 sense since new message might be scheduled in the
1263 meantime... */
1264 while ((pos != NULL) && (off < size * 2))
1265 {
1266 if (pos->do_transmit == GNUNET_YES)
1267 {
1268 /* already removed from consideration */
1269 pos = pos->next;
1270 continue;
1271 }
1272 if (discard_low_prio == GNUNET_NO)
1273 {
1274 delta = pos->deadline.value;
1275 if (delta < t.value)
1276 delta = 0;
1277 else
1278 delta = t.value - delta;
1279 avail += delta * n->bpm_out / 1000 / 60;
1280 if (avail < pos->size)
1281 {
1282 discard_low_prio = GNUNET_YES; /* we could not schedule this one! */
1283 }
1284 else
1285 {
1286 avail -= pos->size;
1287 /* update slack, considering both its absolute deadline
1288 and relative deadlines caused by other messages
1289 with their respective load */
1290 slack = GNUNET_MIN (slack, avail / n->bpm_out);
1291 if (pos->deadline.value < now.value)
1292 slack = 0;
1293 else
1294 slack =
1295 GNUNET_MIN (slack, pos->deadline.value - now.value);
1296 }
1297 }
1298 off += pos->size;
1299 t.value = GNUNET_MAX (pos->deadline.value, t.value);
1300 if (pos->priority <= min_prio)
1301 {
1302 /* update min for discard */
1303 min_prio = pos->priority;
1304 min = pos;
1305 }
1306 pos = pos->next;
1307 }
1308 if (discard_low_prio)
1309 {
1310 /* remove lowest-priority entry from consideration */
1311 min->do_transmit = GNUNET_YES; /* means: discard (for now) */
1312 }
1313 last = pos;
1314 }
1315 /* guard against sending "tiny" messages with large headers without
1316 urgent deadlines */
1317 if ((slack > 1000) && (size > 4 * off))
1318 {
1319 /* less than 25% of message would be filled with
1320 deadlines still being met if we delay by one
1321 second or more; so just wait for more data */
1322 retry_time->value = slack / 2;
1323 /* reset do_transmit values for next time */
1324 while (pos != last)
1325 {
1326 pos->do_transmit = GNUNET_NO;
1327 pos = pos->next;
1328 }
1329 return 0;
1330 }
1331 /* select marked messages (up to size) for transmission */
1332 off = 0;
1333 pos = n->messages;
1334 while (pos != last)
1335 {
1336 if ((pos->size <= size) && (pos->do_transmit == GNUNET_NO))
1337 {
1338 pos->do_transmit = GNUNET_YES; /* mark for transmission */
1339 off += pos->size;
1340 size -= pos->size;
1341 }
1342 else
1343 pos->do_transmit = GNUNET_NO; /* mark for not transmitting! */
1344 pos = pos->next;
1345 }
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "Selected %u bytes of plaintext messages for transmission to `%4s'.\n",
1348 off, GNUNET_i2s (&n->peer));
1349 return off;
1350}
1351
1352
1353/**
1354 * Batch multiple messages into a larger buffer.
1355 *
1356 * @param n neighbour to take messages from
1357 * @param buf target buffer
1358 * @param size size of buf
1359 * @param deadline set to transmission deadline for the result
1360 * @param retry_time set to the time when we should try again
1361 * (only valid if this function returns zero)
1362 * @param priority set to the priority of the batch
1363 * @return number of bytes written to buf (can be zero)
1364 */
1365static size_t
1366batch_message (struct Neighbour *n,
1367 char *buf,
1368 size_t size,
1369 struct GNUNET_TIME_Absolute *deadline,
1370 struct GNUNET_TIME_Relative *retry_time,
1371 unsigned int *priority)
1372{
1373 struct MessageEntry *pos;
1374 struct MessageEntry *prev;
1375 struct MessageEntry *next;
1376 size_t ret;
1377
1378 ret = 0;
1379 *priority = 0;
1380 *deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1381 *retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
1382 if (0 == select_messages (n, size, retry_time))
1383 {
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "No messages selected, will try again in %llu ms\n",
1386 retry_time->value);
1387 return 0;
1388 }
1389 pos = n->messages;
1390 prev = NULL;
1391 while ((pos != NULL) && (size >= sizeof (struct GNUNET_MessageHeader)))
1392 {
1393 next = pos->next;
1394 if (GNUNET_YES == pos->do_transmit)
1395 {
1396 GNUNET_assert (pos->size <= size);
1397 memcpy (&buf[ret], &pos[1], pos->size);
1398 ret += pos->size;
1399 size -= pos->size;
1400 *priority += pos->priority;
1401 deadline->value = GNUNET_MIN (deadline->value, pos->deadline.value);
1402 GNUNET_free (pos);
1403 if (prev == NULL)
1404 n->messages = next;
1405 else
1406 prev->next = next;
1407 }
1408 else
1409 {
1410 prev = pos;
1411 }
1412 pos = next;
1413 }
1414 return ret;
1415}
1416
1417
1418/**
1419 * Remove messages with deadlines that have long expired from
1420 * the queue.
1421 *
1422 * @param n neighbour to inspect
1423 */
1424static void
1425discard_expired_messages (struct Neighbour *n)
1426{
1427 /* FIXME */
1428}
1429
1430
1431/**
1432 * Signature of the main function of a task.
1433 *
1434 * @param cls closure
1435 * @param tc context information (why was this task triggered now)
1436 */
1437static void
1438retry_plaintext_processing (void *cls,
1439 const struct GNUNET_SCHEDULER_TaskContext *tc)
1440{
1441 struct Neighbour *n = cls;
1442
1443 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1444 process_plaintext_neighbour_queue (n);
1445}
1446
1447
1448/**
1449 * Send our key (and encrypted PING) to the other peer.
1450 *
1451 * @param n the other peer
1452 */
1453static void send_key (struct Neighbour *n);
1454
1455
1456/**
1457 * Check if we have plaintext messages for the specified neighbour
1458 * pending, and if so, consider batching and encrypting them (and
1459 * then trigger processing of the encrypted queue if needed).
1460 *
1461 * @param n neighbour to check.
1462 */
1463static void
1464process_plaintext_neighbour_queue (struct Neighbour *n)
1465{
1466 char pbuf[MAX_ENCRYPTED_MESSAGE_SIZE]; /* plaintext */
1467 size_t used;
1468 size_t esize;
1469 struct EncryptedMessage *em; /* encrypted message */
1470 struct EncryptedMessage *ph; /* plaintext header */
1471 struct MessageEntry *me;
1472 unsigned int priority;
1473 struct GNUNET_TIME_Absolute deadline;
1474 struct GNUNET_TIME_Relative retry_time;
1475
1476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1477 "Processing plaintext message queue for `%4s', scheduling messages.\n",
1478 GNUNET_i2s (&n->peer));
1479 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
1480 {
1481 GNUNET_SCHEDULER_cancel (sched, n->retry_plaintext_task);
1482 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1483 }
1484 switch (n->status)
1485 {
1486 case PEER_STATE_DOWN:
1487 send_key (n);
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1489 "Not yet connected, deferring processing of plaintext messages.\n");
1490 return;
1491 case PEER_STATE_KEY_SENT:
1492 GNUNET_assert (n->retry_set_key_task !=
1493 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1495 "Not yet connected, deferring processing of plaintext messages.\n");
1496 return;
1497 case PEER_STATE_KEY_RECEIVED:
1498 GNUNET_assert (n->retry_set_key_task !=
1499 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
1500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1501 "Not yet connected, deferring processing of plaintext messages.\n");
1502 return;
1503 case PEER_STATE_KEY_CONFIRMED:
1504 /* ready to continue */
1505 break;
1506 }
1507 if (n->messages == NULL)
1508 {
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "Plaintext message queue is empty.\n");
1511 return; /* no pending messages */
1512 }
1513 discard_expired_messages (n);
1514 if (n->encrypted_head != NULL)
1515 {
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Encrypted message queue is still full, delaying plaintext processing.\n");
1518 return; /* wait for messages already encrypted to be
1519 processed first! */
1520 }
1521 ph = (struct EncryptedMessage *) pbuf;
1522 deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1523 priority = 0;
1524 used = sizeof (struct EncryptedMessage);
1525
1526 used += batch_message (n,
1527 &pbuf[used],
1528 MAX_ENCRYPTED_MESSAGE_SIZE - used,
1529 &deadline, &retry_time, &priority);
1530 if (used == sizeof (struct EncryptedMessage))
1531 {
1532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1533 "No messages selected for processing at this time, will try again later.\n");
1534 /* no messages selected for sending, try again later... */
1535 n->retry_plaintext_task =
1536 GNUNET_SCHEDULER_add_delayed (sched,
1537 GNUNET_NO,
1538 GNUNET_SCHEDULER_PRIORITY_IDLE,
1539 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1540 retry_time,
1541 &retry_plaintext_processing, n);
1542 return;
1543 }
1544
1545 ph->sequence_number = htonl (++n->last_sequence_number_sent);
1546 ph->inbound_bpm_limit = htonl (n->bpm_in);
1547 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1548
1549 /* setup encryption message header */
1550 me = GNUNET_malloc (sizeof (struct MessageEntry) + used);
1551 me->deadline = deadline;
1552 me->priority = priority;
1553 me->size = used;
1554 em = (struct EncryptedMessage *) &me[1];
1555 em->header.size = htons (used);
1556 em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
1557 em->reserved = htonl (0);
1558 esize = used - ENCRYPTED_HEADER_SIZE;
1559 GNUNET_CRYPTO_hash (&ph->sequence_number, esize, &em->plaintext_hash);
1560 /* encrypt */
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562 "Encrypting plaintext messages for transmission.\n");
1563 GNUNET_assert (GNUNET_OK ==
1564 do_encrypt (n,
1565 &em->plaintext_hash,
1566 &ph->sequence_number,
1567 &em->sequence_number, esize));
1568 /* append to transmission list */
1569 if (n->encrypted_tail == NULL)
1570 n->encrypted_head = me;
1571 else
1572 n->encrypted_tail->next = me;
1573 n->encrypted_tail = me;
1574 process_encrypted_neighbour_queue (n);
1575}
1576
1577
1578/**
1579 * Handle CORE_SEND request.
1580 */
1581static void
1582handle_client_send (void *cls,
1583 struct GNUNET_SERVER_Handle *server,
1584 struct GNUNET_SERVER_Client *client,
1585 const struct GNUNET_MessageHeader *message);
1586
1587
1588/**
1589 * Function called to notify us that we either succeeded
1590 * or failed to connect (at the transport level) to another
1591 * peer. We should either free the message we were asked
1592 * to transmit or re-try adding it to the queue.
1593 *
1594 * @param cls closure
1595 * @param size number of bytes available in buf
1596 * @param buf where the callee should write the message
1597 * @return number of bytes written to buf
1598 */
1599static size_t
1600send_connect_continuation (void *cls, size_t size, void *buf)
1601{
1602 struct SendMessage *sm = cls;
1603
1604 if (buf == NULL)
1605 {
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1607 "Asked to send message to disconnected peer `%4s' and connection failed. Discarding message.\n",
1608 GNUNET_i2s (&sm->peer));
1609 GNUNET_free (sm);
1610 return 0;
1611 }
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1613 "Connection to peer `%4s' succeeded, retrying original send request\n",
1614 GNUNET_i2s (&sm->peer));
1615 handle_client_send (NULL, NULL, NULL, &sm->header);
1616 GNUNET_free (sm);
1617 return 0;
1618}
1619
1620
1621/**
1622 * Handle CORE_SEND request.
1623 */
1624static void
1625handle_client_send (void *cls,
1626 struct GNUNET_SERVER_Handle *server,
1627 struct GNUNET_SERVER_Client *client,
1628 const struct GNUNET_MessageHeader *message)
1629{
1630 const struct SendMessage *sm;
1631 struct SendMessage *smc;
1632 const struct GNUNET_MessageHeader *mh;
1633 struct Neighbour *n;
1634 struct MessageEntry *pred;
1635 struct MessageEntry *pos;
1636 struct MessageEntry *e;
1637 uint16_t msize;
1638
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1640 "Core service receives `%s' request.\n", "SEND");
1641 msize = ntohs (message->size);
1642 if (msize <
1643 sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
1644 {
1645 GNUNET_break (0);
1646 if (client != NULL)
1647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1648 return;
1649 }
1650 sm = (const struct SendMessage *) message;
1651 msize -= sizeof (struct SendMessage);
1652 mh = (const struct GNUNET_MessageHeader *) &sm[1];
1653 if (msize != ntohs (mh->size))
1654 {
1655 GNUNET_break (0);
1656 if (client != NULL)
1657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1658 return;
1659 }
1660 n = find_neighbour (&sm->peer);
1661 if (n == NULL)
1662 {
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1664 "Not yet connected to `%4s', will try to establish connection\n",
1665 GNUNET_i2s (&sm->peer));
1666 msize += sizeof (struct SendMessage);
1667 /* ask transport to connect to the peer */
1668 /* FIXME: this code does not handle the
1669 case where we get multiple SendMessages before
1670 transport responds to this request;
1671 => need to track pending requests! */
1672 smc = GNUNET_malloc (msize);
1673 memcpy (smc, sm, msize);
1674 GNUNET_TRANSPORT_notify_transmit_ready (transport,
1675 &sm->peer,
1676 0,
1677 GNUNET_TIME_absolute_get_remaining
1678 (GNUNET_TIME_absolute_ntoh
1679 (sm->deadline)),
1680 &send_connect_continuation,
1681 smc);
1682 if (client != NULL)
1683 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1684 return;
1685 }
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1687 "Core queues %u bytes of plaintext data for transmission to `%4s'.\n",
1688 msize, GNUNET_i2s (&sm->peer));
1689 /* FIXME: consider bounding queue size */
1690 e = GNUNET_malloc (sizeof (struct MessageEntry) + msize);
1691 e->deadline = GNUNET_TIME_absolute_ntoh (sm->deadline);
1692 e->priority = ntohl (sm->priority);
1693 e->size = msize;
1694 memcpy (&e[1], mh, msize);
1695
1696 /* insert, keep list sorted by deadline */
1697 pred = NULL;
1698 pos = n->messages;
1699 while ((pos != NULL) && (pos->deadline.value < e->deadline.value))
1700 {
1701 pred = pos;
1702 pos = pos->next;
1703 }
1704 if (pred == NULL)
1705 n->messages = e;
1706 else
1707 pred->next = e;
1708 e->next = pos;
1709
1710 /* consider scheduling now */
1711 process_plaintext_neighbour_queue (n);
1712 if (client != NULL)
1713 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1714}
1715
1716
1717/**
1718 * List of handlers for the messages understood by this
1719 * service.
1720 */
1721static struct GNUNET_SERVER_MessageHandler handlers[] = {
1722 {&handle_client_init, NULL,
1723 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
1724 {&handle_client_request_configure, NULL,
1725 GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONFIGURE,
1726 sizeof (struct RequestConfigureMessage)},
1727 {&handle_client_send, NULL,
1728 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
1729 {NULL, NULL, 0, 0}
1730};
1731
1732
1733/**
1734 * PEERINFO is giving us a HELLO for a peer. Add the
1735 * public key to the neighbour's struct and retry
1736 * send_key. Or, if we did not get a HELLO, just do
1737 * nothing.
1738 *
1739 * @param cls NULL
1740 * @param peer the peer for which this is the HELLO
1741 * @param hello HELLO message of that peer
1742 * @param trust amount of trust we currently have in that peer
1743 */
1744static void
1745process_hello_retry_send_key (void *cls,
1746 const struct GNUNET_PeerIdentity *peer,
1747 const struct GNUNET_HELLO_Message *hello,
1748 uint32_t trust)
1749{
1750 struct Neighbour *n;
1751
1752 if (peer == NULL)
1753 return;
1754 n = find_neighbour (peer);
1755 if (n == NULL)
1756 return;
1757 if (n->public_key != NULL)
1758 return;
1759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1760 "Received new HELLO for `%4s', initiating key exchange.\n",
1761 GNUNET_i2s (peer));
1762 n->public_key =
1763 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1764 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
1765 {
1766 GNUNET_free (n->public_key);
1767 n->public_key = NULL;
1768 return;
1769 }
1770 send_key (n);
1771}
1772
1773
1774/**
1775 * Task that will retry "send_key" if our previous attempt failed
1776 * to yield a PONG.
1777 */
1778static void
1779set_key_retry_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1780{
1781 struct Neighbour *n = cls;
1782
1783 GNUNET_assert (n->status != PEER_STATE_KEY_CONFIRMED);
1784 n->retry_set_key_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
1785 n->set_key_retry_frequency =
1786 GNUNET_TIME_relative_multiply (n->set_key_retry_frequency, 2);
1787 send_key (n);
1788}
1789
1790
1791/**
1792 * Send our key (and encrypted PING) to the other peer.
1793 *
1794 * @param n the other peer
1795 */
1796static void
1797send_key (struct Neighbour *n)
1798{
1799 struct SetKeyMessage *sm;
1800 struct MessageEntry *me;
1801 struct PingMessage pp;
1802 struct PingMessage *pm;
1803
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1805 "Asked to perform key exchange with `%4s'.\n",
1806 GNUNET_i2s (&n->peer));
1807 if (n->public_key == NULL)
1808 {
1809 /* lookup n's public key, then try again */
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1811 "Lacking public key for `%4s', trying to obtain one.\n",
1812 GNUNET_i2s (&n->peer));
1813 GNUNET_PEERINFO_for_all (cfg,
1814 sched,
1815 &n->peer,
1816 0,
1817 GNUNET_TIME_UNIT_MINUTES,
1818 &process_hello_retry_send_key, NULL);
1819 return;
1820 }
1821 /* first, set key message */
1822 me = GNUNET_malloc (sizeof (struct MessageEntry) +
1823 sizeof (struct SetKeyMessage));
1824 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_SET_KEY_DELAY);
1825 me->priority = SET_KEY_PRIORITY;
1826 me->size = sizeof (struct SetKeyMessage);
1827 if (n->encrypted_head == NULL)
1828 n->encrypted_head = me;
1829 else
1830 n->encrypted_tail->next = me;
1831 n->encrypted_tail = me;
1832 sm = (struct SetKeyMessage *) &me[1];
1833 sm->header.size = htons (sizeof (struct SetKeyMessage));
1834 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SET_KEY);
1835 sm->sender_status = htonl ((int32_t) ((n->status == PEER_STATE_DOWN) ?
1836 PEER_STATE_KEY_SENT : n->status));
1837 sm->purpose.size =
1838 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1839 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
1840 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
1841 sizeof (struct GNUNET_PeerIdentity));
1842 sm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_KEY);
1843 sm->creation_time = GNUNET_TIME_absolute_hton (n->encrypt_key_created);
1844 sm->target = n->peer;
1845 GNUNET_assert (GNUNET_OK ==
1846 GNUNET_CRYPTO_rsa_encrypt (&n->encrypt_key,
1847 sizeof (struct
1848 GNUNET_CRYPTO_AesSessionKey),
1849 n->public_key,
1850 &sm->encrypted_key));
1851 GNUNET_assert (GNUNET_OK ==
1852 GNUNET_CRYPTO_rsa_sign (my_private_key, &sm->purpose,
1853 &sm->signature));
1854
1855 /* second, encrypted PING message */
1856 me = GNUNET_malloc (sizeof (struct MessageEntry) +
1857 sizeof (struct PingMessage));
1858 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PING_DELAY);
1859 me->priority = PING_PRIORITY;
1860 me->size = sizeof (struct PingMessage);
1861 n->encrypted_tail->next = me;
1862 n->encrypted_tail = me;
1863 pm = (struct PingMessage *) &me[1];
1864 pm->header.size = htons (sizeof (struct PingMessage));
1865 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
1866 pp.challenge = htonl (n->ping_challenge);
1867 pp.target = n->peer;
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Encrypting `%s' and `%s' messages for `%4s'.\n",
1870 "SET_KEY", "PING", GNUNET_i2s (&n->peer));
1871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1872 "Sending `%s' to `%4s' with challenge %u encrypted using key %u\n",
1873 "PING",
1874 GNUNET_i2s (&n->peer), n->ping_challenge, n->encrypt_key.crc32);
1875 do_encrypt (n,
1876 &n->peer.hashPubKey,
1877 &pp.challenge,
1878 &pm->challenge,
1879 sizeof (struct PingMessage) -
1880 sizeof (struct GNUNET_MessageHeader));
1881 /* update status */
1882 switch (n->status)
1883 {
1884 case PEER_STATE_DOWN:
1885 n->status = PEER_STATE_KEY_SENT;
1886 break;
1887 case PEER_STATE_KEY_SENT:
1888 break;
1889 case PEER_STATE_KEY_RECEIVED:
1890 break;
1891 case PEER_STATE_KEY_CONFIRMED:
1892 GNUNET_break (0);
1893 break;
1894 default:
1895 GNUNET_break (0);
1896 break;
1897 }
1898 /* trigger queue processing */
1899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1900 "Triggering processing of encrypted message queue.\n");
1901 process_encrypted_neighbour_queue (n);
1902 if (n->status != PEER_STATE_KEY_CONFIRMED)
1903 n->retry_set_key_task
1904 = GNUNET_SCHEDULER_add_delayed (sched,
1905 GNUNET_NO,
1906 GNUNET_SCHEDULER_PRIORITY_KEEP,
1907 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1908 n->set_key_retry_frequency,
1909 &set_key_retry_task, n);
1910}
1911
1912
1913/**
1914 * We received a SET_KEY message. Validate and update
1915 * our key material and status.
1916 *
1917 * @param n the neighbour from which we received message m
1918 * @param m the set key message we received
1919 */
1920static void
1921handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m);
1922
1923
1924/**
1925 * PEERINFO is giving us a HELLO for a peer. Add the public key to
1926 * the neighbour's struct and retry handling the set_key message. Or,
1927 * if we did not get a HELLO, just free the set key message.
1928 *
1929 * @param cls pointer to the set key message
1930 * @param peer the peer for which this is the HELLO
1931 * @param hello HELLO message of that peer
1932 * @param trust amount of trust we currently have in that peer
1933 */
1934static void
1935process_hello_retry_handle_set_key (void *cls,
1936 const struct GNUNET_PeerIdentity *peer,
1937 const struct GNUNET_HELLO_Message *hello,
1938 uint32_t trust)
1939{
1940 struct SetKeyMessage *sm = cls;
1941 struct Neighbour *n;
1942
1943 if (peer == NULL)
1944 {
1945 GNUNET_free (sm);
1946 return;
1947 }
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1949 "Looking for peer `%4s' to handle `%s' message.\n",
1950 GNUNET_i2s (peer), "SET_KEY");
1951 n = find_neighbour (peer);
1952 if (n == NULL)
1953 {
1954 GNUNET_break (0);
1955 return;
1956 }
1957 if (n->public_key != NULL)
1958 return; /* multiple HELLOs match!? */
1959 n->public_key =
1960 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1961 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
1962 {
1963 GNUNET_free (n->public_key);
1964 n->public_key = NULL;
1965 return;
1966 }
1967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1968 "Received `%s' for `%4s', continuing processing of `%s' message.\n",
1969 "HELLO", GNUNET_i2s (peer), "SET_KEY");
1970 handle_set_key (n, sm);
1971}
1972
1973
1974/**
1975 * We received a PING message. Validate and transmit
1976 * PONG.
1977 *
1978 * @param n sender of the PING
1979 * @param m the encrypted PING message itself
1980 */
1981static void
1982handle_ping (struct Neighbour *n, const struct PingMessage *m)
1983{
1984 struct PingMessage t;
1985 struct PingMessage *tp;
1986 struct MessageEntry *me;
1987
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 "Core service receives `%s' request from `%4s'.\n",
1990 "PING", GNUNET_i2s (&n->peer));
1991 if (GNUNET_OK !=
1992 do_decrypt (n,
1993 &my_identity.hashPubKey,
1994 &m->challenge,
1995 &t.challenge,
1996 sizeof (struct PingMessage) -
1997 sizeof (struct GNUNET_MessageHeader)))
1998 return;
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 "Decrypted `%s' to `%4s' with challenge %u decrypted using key %u\n",
2001 "PING",
2002 GNUNET_i2s (&t.target),
2003 ntohl (t.challenge), n->decrypt_key.crc32);
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2005 "Target of `%s' request is `%4s'.\n",
2006 "PING", GNUNET_i2s (&t.target));
2007 if (0 != memcmp (&t.target,
2008 &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2009 {
2010 GNUNET_break_op (0);
2011 return;
2012 }
2013 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2014 sizeof (struct PingMessage));
2015 if (n->encrypted_tail != NULL)
2016 n->encrypted_tail->next = me;
2017 else
2018 {
2019 n->encrypted_tail = me;
2020 n->encrypted_head = me;
2021 }
2022 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PONG_DELAY);
2023 me->priority = PONG_PRIORITY;
2024 me->size = sizeof (struct PingMessage);
2025 tp = (struct PingMessage *) &me[1];
2026 tp->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
2027 tp->header.size = htons (sizeof (struct PingMessage));
2028 do_encrypt (n,
2029 &my_identity.hashPubKey,
2030 &t.challenge,
2031 &tp->challenge,
2032 sizeof (struct PingMessage) -
2033 sizeof (struct GNUNET_MessageHeader));
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2035 "Encrypting `%s' with challenge %u using key %u\n", "PONG",
2036 ntohl (t.challenge), n->encrypt_key.crc32);
2037 /* trigger queue processing */
2038 process_encrypted_neighbour_queue (n);
2039}
2040
2041
2042/**
2043 * We received a SET_KEY message. Validate and update
2044 * our key material and status.
2045 *
2046 * @param n the neighbour from which we received message m
2047 * @param m the set key message we received
2048 */
2049static void
2050handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m)
2051{
2052 struct SetKeyMessage *m_cpy;
2053 struct GNUNET_TIME_Absolute t;
2054 struct GNUNET_CRYPTO_AesSessionKey k;
2055 struct PingMessage *ping;
2056 enum PeerStateMachine sender_status;
2057
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "Core service receives `%s' request from `%4s'.\n",
2060 "SET_KEY", GNUNET_i2s (&n->peer));
2061 if (n->public_key == NULL)
2062 {
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "Lacking public key for peer, trying to obtain one.\n");
2065 m_cpy = GNUNET_malloc (sizeof (struct SetKeyMessage));
2066 memcpy (m_cpy, m, sizeof (struct SetKeyMessage));
2067 /* lookup n's public key, then try again */
2068 GNUNET_PEERINFO_for_all (cfg,
2069 sched,
2070 &n->peer,
2071 0,
2072 GNUNET_TIME_UNIT_MINUTES,
2073 &process_hello_retry_handle_set_key, m_cpy);
2074 return;
2075 }
2076 if ((ntohl (m->purpose.size) !=
2077 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2078 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
2079 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
2080 sizeof (struct GNUNET_PeerIdentity)) ||
2081 (GNUNET_OK !=
2082 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_KEY,
2083 &m->purpose, &m->signature, n->public_key)))
2084 {
2085 /* invalid signature */
2086 GNUNET_break_op (0);
2087 return;
2088 }
2089 t = GNUNET_TIME_absolute_ntoh (m->creation_time);
2090 if (((n->status == PEER_STATE_KEY_RECEIVED) ||
2091 (n->status == PEER_STATE_KEY_CONFIRMED)) &&
2092 (t.value < n->decrypt_key_created.value))
2093 {
2094 /* this could rarely happen due to massive re-ordering of
2095 messages on the network level, but is most likely either
2096 a bug or some adversary messing with us. Report. */
2097 GNUNET_break_op (0);
2098 return;
2099 }
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decrypting key material.\n");
2101 if ((GNUNET_CRYPTO_rsa_decrypt (my_private_key,
2102 &m->encrypted_key,
2103 &k,
2104 sizeof (struct GNUNET_CRYPTO_AesSessionKey))
2105 != sizeof (struct GNUNET_CRYPTO_AesSessionKey)) ||
2106 (GNUNET_OK != GNUNET_CRYPTO_aes_check_session_key (&k)))
2107 {
2108 /* failed to decrypt !? */
2109 GNUNET_break_op (0);
2110 return;
2111 }
2112
2113 n->decrypt_key = k;
2114 if (n->decrypt_key_created.value != t.value)
2115 {
2116 /* fresh key, reset sequence numbers */
2117 n->last_sequence_number_received = 0;
2118 n->last_packets_bitmap = 0;
2119 n->decrypt_key_created = t;
2120 }
2121 sender_status = (enum PeerStateMachine) ntohl (m->sender_status);
2122 switch (n->status)
2123 {
2124 case PEER_STATE_DOWN:
2125 n->status = PEER_STATE_KEY_RECEIVED;
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127 "Responding to `%s' with my own key.\n", "SET_KEY");
2128 send_key (n);
2129 break;
2130 case PEER_STATE_KEY_SENT:
2131 case PEER_STATE_KEY_RECEIVED:
2132 n->status = PEER_STATE_KEY_RECEIVED;
2133 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
2134 (sender_status != PEER_STATE_KEY_CONFIRMED))
2135 {
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2137 "Responding to `%s' with my own key (other peer has status %u).\n",
2138 "SET_KEY", sender_status);
2139 send_key (n);
2140 }
2141 break;
2142 case PEER_STATE_KEY_CONFIRMED:
2143 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
2144 (sender_status != PEER_STATE_KEY_CONFIRMED))
2145 {
2146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2147 "Responding to `%s' with my own key (other peer has status %u), I was already fully up.\n",
2148 "SET_KEY", sender_status);
2149 send_key (n);
2150 }
2151 break;
2152 default:
2153 GNUNET_break (0);
2154 break;
2155 }
2156 if (n->pending_ping != NULL)
2157 {
2158 ping = n->pending_ping;
2159 n->pending_ping = NULL;
2160 handle_ping (n, ping);
2161 GNUNET_free (ping);
2162 }
2163}
2164
2165
2166/**
2167 * We received a PONG message. Validate and update
2168 * our status.
2169 *
2170 * @param n sender of the PONG
2171 * @param m the encrypted PONG message itself
2172 */
2173static void
2174handle_pong (struct Neighbour *n, const struct PingMessage *m)
2175{
2176 struct PingMessage t;
2177
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2179 "Core service receives `%s' request from `%4s'.\n",
2180 "PONG", GNUNET_i2s (&n->peer));
2181 if (GNUNET_OK !=
2182 do_decrypt (n,
2183 &n->peer.hashPubKey,
2184 &m->challenge,
2185 &t.challenge,
2186 sizeof (struct PingMessage) -
2187 sizeof (struct GNUNET_MessageHeader)))
2188 return;
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 "Decrypted `%s' from `%4s' with challenge %u using key %u\n",
2191 "PONG",
2192 GNUNET_i2s (&t.target),
2193 ntohl (t.challenge), n->decrypt_key.crc32);
2194 if ((0 != memcmp (&t.target,
2195 &n->peer,
2196 sizeof (struct GNUNET_PeerIdentity))) ||
2197 (n->ping_challenge != ntohl (t.challenge)))
2198 {
2199 /* PONG malformed */
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2201 "Received malfromed `%s' wanted sender `%4s' with challenge %u\n",
2202 "PONG", GNUNET_i2s (&n->peer), n->ping_challenge);
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 "Received malfromed `%s' received from `%4s' with challenge %u\n",
2205 "PONG", GNUNET_i2s (&t.target), ntohl (t.challenge));
2206 GNUNET_break_op (0);
2207 return;
2208 }
2209 switch (n->status)
2210 {
2211 case PEER_STATE_DOWN:
2212 GNUNET_break (0); /* should be impossible */
2213 return;
2214 case PEER_STATE_KEY_SENT:
2215 GNUNET_break (0); /* should be impossible, how did we decrypt? */
2216 return;
2217 case PEER_STATE_KEY_RECEIVED:
2218 n->status = PEER_STATE_KEY_CONFIRMED;
2219 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
2220 {
2221 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
2222 n->retry_set_key_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
2223 }
2224 process_encrypted_neighbour_queue (n);
2225 break;
2226 case PEER_STATE_KEY_CONFIRMED:
2227 /* duplicate PONG? */
2228 break;
2229 default:
2230 GNUNET_break (0);
2231 break;
2232 }
2233}
2234
2235
2236/**
2237 * Send a P2P message to a client.
2238 *
2239 * @param sender who sent us the message?
2240 * @param client who should we give the message to?
2241 * @param m contains the message to transmit
2242 * @param msize number of bytes in buf to transmit
2243 */
2244static void
2245send_p2p_message_to_client (struct Neighbour *sender,
2246 struct Client *client,
2247 const void *m, size_t msize)
2248{
2249 char buf[msize + sizeof (struct NotifyTrafficMessage)];
2250 struct NotifyTrafficMessage *ntm;
2251
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2253 "Core service passes P2P message of type %u to client.\n",
2254 ntohs (((const struct GNUNET_MessageHeader *) m)->type));
2255 ntm = (struct NotifyTrafficMessage *) buf;
2256 ntm->header.size = htons (msize + sizeof (struct NotifyTrafficMessage));
2257 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
2258 ntm->reserved = htonl (0);
2259 ntm->peer = sender->peer;
2260 memcpy (&ntm[1], m, msize);
2261 send_to_client (client, &ntm->header, GNUNET_YES);
2262}
2263
2264
2265/**
2266 * Deliver P2P message to interested clients.
2267 *
2268 * @param sender who sent us the message?
2269 * @param m the message
2270 * @param msize size of the message (including header)
2271 */
2272static void
2273deliver_message (struct Neighbour *sender,
2274 const struct GNUNET_MessageHeader *m, size_t msize)
2275{
2276 struct Client *cpos;
2277 uint16_t type;
2278 unsigned int tpos;
2279 int deliver_full;
2280
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 "Passing decrypted P2P message to interested clients.\n");
2283 type = ntohs (m->type);
2284 cpos = clients;
2285 while (cpos != NULL)
2286 {
2287 deliver_full = GNUNET_NO;
2288 if (cpos->options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND)
2289 deliver_full = GNUNET_YES;
2290 else
2291 {
2292 for (tpos = 0; tpos < cpos->tcnt; tpos++)
2293 {
2294 if (type != cpos->types[tpos])
2295 continue;
2296 deliver_full = GNUNET_YES;
2297 break;
2298 }
2299 }
2300 if (GNUNET_YES == deliver_full)
2301 send_p2p_message_to_client (sender, cpos, m, msize);
2302 else if (cpos->options & GNUNET_CORE_OPTION_SEND_HDR_INBOUND)
2303 send_p2p_message_to_client (sender, cpos, m,
2304 sizeof (struct GNUNET_MessageHeader));
2305 cpos = cpos->next;
2306 }
2307}
2308
2309
2310/**
2311 * Align P2P message and then deliver to interested clients.
2312 *
2313 * @param sender who sent us the message?
2314 * @param buffer unaligned (!) buffer containing message
2315 * @param msize size of the message (including header)
2316 */
2317static void
2318align_and_deliver (struct Neighbour *sender, const char *buffer, size_t msize)
2319{
2320 char abuf[msize];
2321
2322 /* TODO: call to statistics? */
2323 memcpy (abuf, buffer, msize);
2324 deliver_message (sender, (const struct GNUNET_MessageHeader *) abuf, msize);
2325}
2326
2327
2328/**
2329 * Deliver P2P messages to interested clients.
2330 *
2331 * @param sender who sent us the message?
2332 * @param buffer buffer containing messages, can be modified
2333 * @param buffer_size size of the buffer (overall)
2334 * @param offset offset where messages in the buffer start
2335 */
2336static void
2337deliver_messages (struct Neighbour *sender,
2338 const char *buffer, size_t buffer_size, size_t offset)
2339{
2340 struct GNUNET_MessageHeader *mhp;
2341 struct GNUNET_MessageHeader mh;
2342 uint16_t msize;
2343 int need_align;
2344
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 "Delivering %u bytes of plaintext to interested clients.\n",
2347 buffer_size);
2348 while (offset + sizeof (struct GNUNET_MessageHeader) <= buffer_size)
2349 {
2350 if (0 != offset % sizeof (uint16_t))
2351 {
2352 /* outch, need to copy to access header */
2353 memcpy (&mh, &buffer[offset], sizeof (struct GNUNET_MessageHeader));
2354 mhp = &mh;
2355 }
2356 else
2357 {
2358 /* can access header directly */
2359 mhp = (struct GNUNET_MessageHeader *) &buffer[offset];
2360 }
2361 msize = ntohs (mhp->size);
2362 if (msize + offset > buffer_size)
2363 {
2364 /* malformed message, header says it is larger than what
2365 would fit into the overall buffer */
2366 GNUNET_break_op (0);
2367 break;
2368 }
2369#if HAVE_UNALIGNED_64_ACCESS
2370 need_align = (0 != offset % 4) ? GNUNET_YES : GNUNET_NO;
2371#else
2372 need_align = (0 != offset % 8) ? GNUNET_YES : GNUNET_NO;
2373#endif
2374 if (GNUNET_YES == need_align)
2375 align_and_deliver (sender, &buffer[offset], msize);
2376 else
2377 deliver_message (sender,
2378 (const struct GNUNET_MessageHeader *)
2379 &buffer[offset], msize);
2380 offset += msize;
2381 }
2382}
2383
2384
2385/**
2386 * We received an encrypted message. Decrypt, validate and
2387 * pass on to the appropriate clients.
2388 */
2389static void
2390handle_encrypted_message (struct Neighbour *n,
2391 const struct EncryptedMessage *m)
2392{
2393 size_t size = ntohs (m->header.size);
2394 char buf[size];
2395 struct EncryptedMessage *pt; /* plaintext */
2396 GNUNET_HashCode ph;
2397 size_t off;
2398 uint32_t snum;
2399 struct GNUNET_TIME_Absolute t;
2400
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 "Core service receives `%s' request from `%4s'.\n",
2403 "ENCRYPTED_MESSAGE", GNUNET_i2s (&n->peer));
2404 /* decrypt */
2405 if (GNUNET_OK !=
2406 do_decrypt (n,
2407 &m->plaintext_hash,
2408 &m->sequence_number,
2409 &buf[ENCRYPTED_HEADER_SIZE], size - ENCRYPTED_HEADER_SIZE))
2410 return;
2411 pt = (struct EncryptedMessage *) buf;
2412
2413 /* validate hash */
2414 GNUNET_CRYPTO_hash (&pt->sequence_number,
2415 size - ENCRYPTED_HEADER_SIZE, &ph);
2416 if (0 != memcmp (&ph, &m->plaintext_hash, sizeof (GNUNET_HashCode)))
2417 {
2418 /* checksum failed */
2419 GNUNET_break_op (0);
2420 return;
2421 }
2422
2423 /* validate sequence number */
2424 snum = ntohl (pt->sequence_number);
2425 if (n->last_sequence_number_received == snum)
2426 {
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428 "Received duplicate message, ignoring.\n");
2429 /* duplicate, ignore */
2430 return;
2431 }
2432 if ((n->last_sequence_number_received > snum) &&
2433 (n->last_sequence_number_received - snum > 32))
2434 {
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2436 "Received ancient out of sequence message, ignoring.\n");
2437 /* ancient out of sequence, ignore */
2438 return;
2439 }
2440 if (n->last_sequence_number_received > snum)
2441 {
2442 unsigned int rotbit =
2443 1 << (n->last_sequence_number_received - snum - 1);
2444 if ((n->last_packets_bitmap & rotbit) != 0)
2445 {
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2447 "Received duplicate message, ignoring.\n");
2448 /* duplicate, ignore */
2449 return;
2450 }
2451 n->last_packets_bitmap |= rotbit;
2452 }
2453 if (n->last_sequence_number_received < snum)
2454 {
2455 n->last_packets_bitmap <<= (snum - n->last_sequence_number_received);
2456 n->last_sequence_number_received = snum;
2457 }
2458
2459 /* check timestamp */
2460 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
2461 if (GNUNET_TIME_absolute_get_duration (t).value > MAX_MESSAGE_AGE.value)
2462 {
2463 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2464 _
2465 ("Message received far too old (%llu ms). Content ignored.\n"),
2466 GNUNET_TIME_absolute_get_duration (t).value);
2467 return;
2468 }
2469
2470 /* process decrypted message(s) */
2471 n->bpm_out_external_limit = ntohl (pt->inbound_bpm_limit);
2472 n->bpm_out = GNUNET_MAX (n->bpm_out_external_limit,
2473 n->bpm_out_internal_limit);
2474 n->last_activity = GNUNET_TIME_absolute_get ();
2475 off = sizeof (struct EncryptedMessage);
2476 deliver_messages (n, buf, size, off);
2477}
2478
2479
2480/**
2481 * Function called by the transport for each received message.
2482 *
2483 * @param cls closure
2484 * @param latency estimated latency for communicating with the
2485 * given peer
2486 * @param peer (claimed) identity of the other peer
2487 * @param message the message
2488 */
2489static void
2490handle_transport_receive (void *cls,
2491 struct GNUNET_TIME_Relative latency,
2492 const struct GNUNET_PeerIdentity *peer,
2493 const struct GNUNET_MessageHeader *message)
2494{
2495 struct Neighbour *n;
2496 struct GNUNET_TIME_Absolute now;
2497 int up;
2498 uint16_t type;
2499 uint16_t size;
2500
2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2502 "Received message of type %u from `%4s', demultiplexing.\n",
2503 ntohs (message->type), GNUNET_i2s (peer));
2504 n = find_neighbour (peer);
2505 if (n == NULL)
2506 {
2507 GNUNET_break (0);
2508 return;
2509 }
2510 n->last_latency = latency;
2511 up = n->status == PEER_STATE_KEY_CONFIRMED;
2512 type = ntohs (message->type);
2513 size = ntohs (message->size);
2514 switch (type)
2515 {
2516 case GNUNET_MESSAGE_TYPE_CORE_SET_KEY:
2517 if (size != sizeof (struct SetKeyMessage))
2518 {
2519 GNUNET_break_op (0);
2520 return;
2521 }
2522 handle_set_key (n, (const struct SetKeyMessage *) message);
2523 break;
2524 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
2525 if (size < sizeof (struct EncryptedMessage) +
2526 sizeof (struct GNUNET_MessageHeader))
2527 {
2528 GNUNET_break_op (0);
2529 return;
2530 }
2531 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
2532 (n->status != PEER_STATE_KEY_CONFIRMED))
2533 {
2534 GNUNET_break_op (0);
2535 return;
2536 }
2537 handle_encrypted_message (n, (const struct EncryptedMessage *) message);
2538 break;
2539 case GNUNET_MESSAGE_TYPE_CORE_PING:
2540 if (size != sizeof (struct PingMessage))
2541 {
2542 GNUNET_break_op (0);
2543 return;
2544 }
2545 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
2546 (n->status != PEER_STATE_KEY_CONFIRMED))
2547 {
2548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2549 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
2550 "PING", GNUNET_i2s (&n->peer));
2551 GNUNET_free_non_null (n->pending_ping);
2552 n->pending_ping = GNUNET_malloc (sizeof (struct PingMessage));
2553 memcpy (n->pending_ping, message, sizeof (struct PingMessage));
2554 return;
2555 }
2556 handle_ping (n, (const struct PingMessage *) message);
2557 break;
2558 case GNUNET_MESSAGE_TYPE_CORE_PONG:
2559 if (size != sizeof (struct PingMessage))
2560 {
2561 GNUNET_break_op (0);
2562 return;
2563 }
2564 if ((n->status != PEER_STATE_KEY_SENT) &&
2565 (n->status != PEER_STATE_KEY_RECEIVED) &&
2566 (n->status != PEER_STATE_KEY_CONFIRMED))
2567 {
2568 /* could not decrypt pong, oops! */
2569 GNUNET_break_op (0);
2570 return;
2571 }
2572 handle_pong (n, (const struct PingMessage *) message);
2573 break;
2574 default:
2575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2576 _("Unsupported message of type %u received.\n"), type);
2577 return;
2578 }
2579 if (n->status == PEER_STATE_KEY_CONFIRMED)
2580 {
2581 now = GNUNET_TIME_absolute_get ();
2582 n->last_activity = now;
2583 if (!up)
2584 n->time_established = now;
2585 }
2586}
2587
2588
2589/**
2590 * Function called by transport to notify us that
2591 * a peer connected to us (on the network level).
2592 *
2593 * @param cls closure
2594 * @param peer the peer that connected
2595 * @param latency current latency of the connection
2596 */
2597static void
2598handle_transport_notify_connect (void *cls,
2599 const struct GNUNET_PeerIdentity *peer,
2600 struct GNUNET_TIME_Relative latency)
2601{
2602 struct Neighbour *n;
2603 struct GNUNET_TIME_Absolute now;
2604 struct ConnectNotifyMessage cnm;
2605
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 "Received connection from `%4s'.\n", GNUNET_i2s (peer));
2608 n = find_neighbour (peer);
2609 if (n != NULL)
2610 {
2611 /* duplicate connect notification!? */
2612 GNUNET_break (0);
2613 return;
2614 }
2615 now = GNUNET_TIME_absolute_get ();
2616 n = GNUNET_malloc (sizeof (struct Neighbour));
2617 n->next = neighbours;
2618 neighbours = n;
2619 n->peer = *peer;
2620 n->last_latency = latency;
2621 GNUNET_CRYPTO_aes_create_session_key (&n->encrypt_key);
2622 n->encrypt_key_created = now;
2623 n->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
2624 n->last_asw_update = now;
2625 n->last_arw_update = now;
2626 n->bpm_in = DEFAULT_BPM_IN_OUT;
2627 n->bpm_out = DEFAULT_BPM_IN_OUT;
2628 n->bpm_out_internal_limit = (uint32_t) - 1;
2629 n->bpm_out_external_limit = DEFAULT_BPM_IN_OUT;
2630 n->ping_challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2631 (uint32_t) - 1);
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 "Created entry for new neighbour `%4s'.\n",
2634 GNUNET_i2s (&n->peer));
2635 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
2636 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
2637 cnm.bpm_available = htonl (DEFAULT_BPM_IN_OUT);
2638 cnm.peer = *peer;
2639 cnm.last_activity = GNUNET_TIME_absolute_hton (now);
2640 send_to_all_clients (&cnm.header, GNUNET_YES);
2641}
2642
2643
2644/**
2645 * Free the given entry for the neighbour (it has
2646 * already been removed from the list at this point).
2647 * @param n neighbour to free
2648 */
2649static void
2650free_neighbour (struct Neighbour *n)
2651{
2652 struct MessageEntry *m;
2653
2654 while (NULL != (m = n->messages))
2655 {
2656 n->messages = m->next;
2657 GNUNET_free (m);
2658 }
2659 while (NULL != (m = n->encrypted_head))
2660 {
2661 n->encrypted_head = m->next;
2662 GNUNET_free (m);
2663 }
2664 if (NULL != n->th)
2665 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
2666 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
2667 GNUNET_SCHEDULER_cancel (sched, n->retry_plaintext_task);
2668 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
2669 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
2670 GNUNET_free_non_null (n->public_key);
2671 GNUNET_free_non_null (n->pending_ping);
2672 GNUNET_free (n);
2673}
2674
2675
2676/**
2677 * Function called by transport telling us that a peer
2678 * disconnected.
2679 *
2680 * @param cls closure
2681 * @param peer the peer that disconnected
2682 */
2683static void
2684handle_transport_notify_disconnect (void *cls,
2685 const struct GNUNET_PeerIdentity *peer)
2686{
2687 struct ConnectNotifyMessage cnm;
2688 struct Neighbour *n;
2689 struct Neighbour *p;
2690
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2692 "Peer `%4s' disconnected from us.\n", GNUNET_i2s (peer));
2693 p = NULL;
2694 n = neighbours;
2695 while ((n != NULL) &&
2696 (0 != memcmp (&n->peer, peer, sizeof (struct GNUNET_PeerIdentity))))
2697 {
2698 p = n;
2699 n = n->next;
2700 }
2701 if (n == NULL)
2702 {
2703 GNUNET_break (0);
2704 return;
2705 }
2706 if (p == NULL)
2707 neighbours = n->next;
2708 else
2709 p->next = n->next;
2710 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
2711 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
2712 cnm.bpm_available = htonl (0);
2713 cnm.peer = *peer;
2714 cnm.last_activity = GNUNET_TIME_absolute_hton (n->last_activity);
2715 send_to_all_clients (&cnm.header, GNUNET_YES);
2716 free_neighbour (n);
2717}
2718
2719
2720/**
2721 * Last task run during shutdown. Disconnects us from
2722 * the transport.
2723 */
2724static void
2725cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2726{
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core service shutting down.\n");
2728 GNUNET_assert (transport != NULL);
2729 GNUNET_TRANSPORT_disconnect (transport);
2730 transport = NULL;
2731}
2732
2733
2734/**
2735 * Initiate core service.
2736 *
2737 * @param cls closure
2738 * @param s scheduler to use
2739 * @param serv the initialized server
2740 * @param c configuration to use
2741 */
2742static void
2743run (void *cls,
2744 struct GNUNET_SCHEDULER_Handle *s,
2745 struct GNUNET_SERVER_Handle *serv, struct GNUNET_CONFIGURATION_Handle *c)
2746{
2747#if 0
2748 unsigned long long qin;
2749 unsigned long long qout;
2750 unsigned long long tneigh;
2751#endif
2752 char *keyfile;
2753
2754 sched = s;
2755 cfg = c;
2756 /* parse configuration */
2757 if (
2758#if 0
2759 (GNUNET_OK !=
2760 GNUNET_CONFIGURATION_get_value_number (c,
2761 "CORE",
2762 "XX",
2763 &qin)) ||
2764 (GNUNET_OK !=
2765 GNUNET_CONFIGURATION_get_value_number (c,
2766 "CORE",
2767 "YY",
2768 &qout)) ||
2769 (GNUNET_OK !=
2770 GNUNET_CONFIGURATION_get_value_number (c,
2771 "CORE",
2772 "ZZ_LIMIT", &tneigh)) ||
2773#endif
2774 (GNUNET_OK !=
2775 GNUNET_CONFIGURATION_get_value_filename (c,
2776 "GNUNETD",
2777 "HOSTKEY", &keyfile)))
2778 {
2779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2780 _
2781 ("Core service is lacking key configuration settings. Exiting.\n"));
2782 GNUNET_SCHEDULER_shutdown (s);
2783 return;
2784 }
2785 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
2786 GNUNET_free (keyfile);
2787 if (my_private_key == NULL)
2788 {
2789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2790 _("Core service could not access hostkey. Exiting.\n"));
2791 GNUNET_SCHEDULER_shutdown (s);
2792 return;
2793 }
2794 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
2795 GNUNET_CRYPTO_hash (&my_public_key,
2796 sizeof (my_public_key), &my_identity.hashPubKey);
2797 /* setup notification */
2798 server = serv;
2799 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
2800 /* setup transport connection */
2801 transport = GNUNET_TRANSPORT_connect (sched,
2802 cfg,
2803 NULL,
2804 &handle_transport_receive,
2805 &handle_transport_notify_connect,
2806 &handle_transport_notify_disconnect);
2807 GNUNET_assert (NULL != transport);
2808 GNUNET_SCHEDULER_add_delayed (sched,
2809 GNUNET_YES,
2810 GNUNET_SCHEDULER_PRIORITY_IDLE,
2811 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
2812 GNUNET_TIME_UNIT_FOREVER_REL,
2813 &cleaning_task, NULL);
2814 /* process client requests */
2815 GNUNET_SERVER_add_handlers (server, handlers);
2816 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2817 _("Core service of `%4s' ready.\n"), GNUNET_i2s (&my_identity));
2818}
2819
2820
2821/**
2822 * Function called during shutdown. Clean up our state.
2823 */
2824static void
2825cleanup (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
2826{
2827 struct Neighbour *n;
2828
2829 if (my_private_key != NULL)
2830 GNUNET_CRYPTO_rsa_key_free (my_private_key);
2831 while (NULL != (n = neighbours))
2832 {
2833 neighbours = n->next;
2834 free_neighbour (n);
2835 }
2836 /*
2837 FIXME:
2838 - free clients
2839 */
2840}
2841
2842
2843/**
2844 * The main function for the transport service.
2845 *
2846 * @param argc number of arguments from the command line
2847 * @param argv command line arguments
2848 * @return 0 ok, 1 on error
2849 */
2850int
2851main (int argc, char *const *argv)
2852{
2853 return (GNUNET_OK ==
2854 GNUNET_SERVICE_run (argc,
2855 argv,
2856 "core", &run, NULL, &cleanup, NULL)) ? 0 : 1;
2857}
2858
2859/* end of gnunet-service-core.c */
diff --git a/src/core/test_core_api.c b/src/core/test_core_api.c
new file mode 100644
index 000000000..e1b3dcd0d
--- /dev/null
+++ b/src/core/test_core_api.c
@@ -0,0 +1,368 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 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 2, 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 * @file transport/test_core_api.c
22 * @brief testcase for core_api.c
23 *
24 * FIXME:
25 * - make sure connect callback is invoked properly as well!
26 */
27#include "platform.h"
28#include "gnunet_common.h"
29#include "gnunet_arm_service.h"
30#include "gnunet_core_service.h"
31#include "gnunet_getopt_lib.h"
32#include "gnunet_os_lib.h"
33#include "gnunet_program_lib.h"
34#include "gnunet_scheduler_lib.h"
35#include "gnunet_transport_service.h"
36
37#define VERBOSE GNUNET_NO
38
39#define START_ARM GNUNET_YES
40
41
42/**
43 * How long until we give up on transmitting the message?
44 */
45#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
46
47#define MTYPE 12345
48
49struct PeerContext
50{
51 struct GNUNET_CONFIGURATION_Handle *cfg;
52 struct GNUNET_CORE_Handle *ch;
53 struct GNUNET_PeerIdentity id; /* FIXME: this is always all-zeros! */
54 struct GNUNET_TRANSPORT_Handle *th;
55 struct GNUNET_MessageHeader *hello;
56#if START_ARM
57 pid_t arm_pid;
58#endif
59};
60
61static struct PeerContext p1;
62
63static struct PeerContext p2;
64
65static struct GNUNET_SCHEDULER_Handle *sched;
66
67static int ok;
68
69#if VERBOSE
70#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
71#else
72#define OKPP do { ok++; } while (0)
73#endif
74
75
76
77static void
78terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79{
80 GNUNET_assert (ok == 6);
81 GNUNET_CORE_disconnect (p1.ch);
82 GNUNET_CORE_disconnect (p2.ch);
83 GNUNET_TRANSPORT_disconnect (p1.th);
84 GNUNET_TRANSPORT_disconnect (p2.th);
85 GNUNET_ARM_stop_service ("core", p1.cfg, sched, TIMEOUT, NULL, NULL);
86 GNUNET_ARM_stop_service ("core", p2.cfg, sched, TIMEOUT, NULL, NULL);
87 ok = 0;
88}
89
90
91static void
92connect_notify (void *cls,
93 const struct GNUNET_PeerIdentity *peer,
94 unsigned int bpm, struct GNUNET_TIME_Absolute last_activity)
95{
96 GNUNET_assert ((ok == 5) || (ok == 6));
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98 "Encrypted connection established to peer `%4s' (%u bpm)\n",
99 GNUNET_i2s (peer), bpm);
100}
101
102
103static void
104disconnect_notify (void *cls,
105 const struct GNUNET_PeerIdentity *peer,
106 unsigned int bpm,
107 struct GNUNET_TIME_Absolute last_activity)
108{
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
111}
112
113
114static unsigned int
115bfc_callback (void *cls,
116 const struct GNUNET_PeerIdentity *receiver,
117 void *position, unsigned int padding)
118{
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 "Core requests data for `%4s', I have none.\n",
121 GNUNET_i2s (receiver));
122 return 0;
123}
124
125
126static int
127inbound_notify (void *cls,
128 const struct GNUNET_PeerIdentity *other,
129 const struct GNUNET_MessageHeader *message)
130{
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
133 return GNUNET_OK;
134}
135
136
137static int
138outbound_notify (void *cls,
139 const struct GNUNET_PeerIdentity *other,
140 const struct GNUNET_MessageHeader *message)
141{
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Core notifies about outbound data for `%4s'.\n",
144 GNUNET_i2s (other));
145 return GNUNET_OK;
146}
147
148
149static int
150process_mtype (void *cls,
151 const struct GNUNET_PeerIdentity *peer,
152 const struct GNUNET_MessageHeader *message)
153{
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
156 GNUNET_assert (ok == 5);
157 OKPP;
158 GNUNET_SCHEDULER_add_delayed (sched,
159 GNUNET_NO,
160 GNUNET_SCHEDULER_PRIORITY_KEEP,
161 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
162 GNUNET_TIME_UNIT_ZERO, &terminate_task, NULL);
163 return GNUNET_OK;
164}
165
166
167static struct GNUNET_CORE_MessageHandler handlers[] = {
168 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
169 {NULL, 0, 0}
170};
171
172
173static size_t
174transmit_ready (void *cls, size_t size, void *buf)
175{
176 struct PeerContext *p = cls;
177 struct GNUNET_MessageHeader *m;
178
179 GNUNET_assert (ok == 4);
180 OKPP;
181 GNUNET_assert (p == &p1);
182 GNUNET_assert (buf != NULL);
183 m = (struct GNUNET_MessageHeader *) buf;
184 m->type = htons (MTYPE);
185 m->size = htons (sizeof (struct GNUNET_MessageHeader));
186 return sizeof (struct GNUNET_MessageHeader);
187}
188
189
190
191static void
192init_notify (void *cls,
193 struct GNUNET_CORE_Handle *server,
194 const struct GNUNET_PeerIdentity *my_identity,
195 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
196{
197 struct PeerContext *p = cls;
198
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Core connection to `%4s' established\n",
201 GNUNET_i2s (my_identity));
202 GNUNET_assert (server != NULL);
203 p->id = *my_identity;
204 p->ch = server;
205 if (cls == &p1)
206 {
207 GNUNET_assert (ok == 2);
208 OKPP;
209 /* connect p2 */
210 GNUNET_CORE_connect (sched,
211 p2.cfg,
212 TIMEOUT,
213 &p2,
214 &init_notify,
215 &connect_notify,
216 &disconnect_notify,
217 &bfc_callback,
218 &inbound_notify,
219 GNUNET_YES,
220 &outbound_notify, GNUNET_YES, handlers);
221 }
222 else
223 {
224 GNUNET_assert (ok == 3);
225 OKPP;
226 GNUNET_assert (cls == &p2);
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Asking core (1) for transmission to peer `%4s'\n",
229 GNUNET_i2s (&p2.id));
230 GNUNET_CORE_notify_transmit_ready (p1.ch,
231 0,
232 TIMEOUT,
233 &p2.id,
234 sizeof (struct GNUNET_MessageHeader),
235 &transmit_ready, &p1);
236
237 }
238}
239
240
241static void
242process_hello (void *cls,
243 struct GNUNET_TIME_Relative latency,
244 const struct GNUNET_PeerIdentity *peer,
245 const struct GNUNET_MessageHeader *message)
246{
247 struct PeerContext *p = cls;
248
249 GNUNET_assert (peer != NULL);
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Received (my) `%s' from transport service of `%4s'\n",
252 "HELLO", GNUNET_i2s (peer));
253 GNUNET_assert (message != NULL);
254 p->hello = GNUNET_malloc (ntohs (message->size));
255 memcpy (p->hello, message, ntohs (message->size));
256 if ((p == &p1) && (p2.th != NULL))
257 GNUNET_TRANSPORT_offer_hello (p2.th, message);
258 if ((p == &p2) && (p1.th != NULL))
259 GNUNET_TRANSPORT_offer_hello (p1.th, message);
260
261 if ((p == &p1) && (p2.hello != NULL))
262 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
263 if ((p == &p2) && (p1.hello != NULL))
264 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
265}
266
267
268
269static void
270setup_peer (struct PeerContext *p, const char *cfgname)
271{
272 p->cfg = GNUNET_CONFIGURATION_create ();
273#if START_ARM
274 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
275 "gnunet-service-arm",
276#if VERBOSE
277 "-L", "DEBUG",
278#endif
279 "-c", cfgname, NULL);
280 sleep (1); /* allow ARM to start */
281#endif
282 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
283 GNUNET_ARM_start_service ("core", p->cfg, sched, TIMEOUT, NULL, NULL);
284 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
285 GNUNET_assert (p->th != NULL);
286 GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p);
287}
288
289
290static void
291run (void *cls,
292 struct GNUNET_SCHEDULER_Handle *s,
293 char *const *args,
294 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
295{
296 GNUNET_assert (ok == 1);
297 OKPP;
298 sched = s;
299 setup_peer (&p1, "test_core_api_peer1.conf");
300 setup_peer (&p2, "test_core_api_peer2.conf");
301 GNUNET_CORE_connect (sched,
302 p1.cfg,
303 TIMEOUT,
304 &p1,
305 &init_notify,
306 &connect_notify,
307 &disconnect_notify,
308 &bfc_callback,
309 &inbound_notify,
310 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
311}
312
313
314static void
315stop_arm (struct PeerContext *p)
316{
317#if START_ARM
318 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
319 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
320 if (p->arm_pid != waitpid (p->arm_pid, NULL, 0))
321 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "ARM process %u stopped\n", p->arm_pid);
324#endif
325 GNUNET_CONFIGURATION_destroy (p->cfg);
326}
327
328static int
329check ()
330{
331 char *const argv[] = { "test-core-api",
332 "-c",
333 "test_core_api_data.conf",
334#if VERBOSE
335 "-L", "DEBUG",
336#endif
337 NULL
338 };
339 struct GNUNET_GETOPT_CommandLineOption options[] = {
340 GNUNET_GETOPT_OPTION_END
341 };
342 sleep (1);
343 ok = 1;
344 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
345 argv, "test-core-api", "nohelp", options, &run, &ok);
346 stop_arm (&p1);
347 stop_arm (&p2);
348 return ok;
349}
350
351int
352main (int argc, char *argv[])
353{
354 int ret;
355
356 GNUNET_log_setup ("test-core-api",
357#if VERBOSE
358 "DEBUG",
359#else
360 "WARNING",
361#endif
362 NULL);
363 ret = check ();
364
365 return ret;
366}
367
368/* end of test_core_api.c */
diff --git a/src/core/test_core_api_data.conf b/src/core/test_core_api_data.conf
new file mode 100644
index 000000000..7a35faaf0
--- /dev/null
+++ b/src/core/test_core_api_data.conf
@@ -0,0 +1,27 @@
1[PATHS]
2SERVICEHOME = /tmp/test-gnunetd-core-master/
3
4[resolver]
5PORT = 2464
6
7[transport]
8PORT = 2465
9PLUGINS = tcp
10
11[arm]
12PORT = 2466
13
14[statistics]
15PORT = 2467
16
17[tcp]
18PORT = 2468
19
20[peerinfo]
21PORT = 2469
22
23[core]
24PORT = 2470
25
26[testing]
27WEAKRANDOM = YES
diff --git a/src/core/test_core_api_peer1.conf b/src/core/test_core_api_peer1.conf
new file mode 100644
index 000000000..fb060e3e6
--- /dev/null
+++ b/src/core/test_core_api_peer1.conf
@@ -0,0 +1,40 @@
1[PATHS]
2SERVICEHOME = /tmp/test-gnunetd-core-peer-1/
3DEFAULTCONFIG = test_core_api_peer1.conf
4
5[resolver]
6PORT = 12464
7#PREFIX = xterm -T resolver1 -e valgrind --tool=memcheck
8
9[transport]
10PORT = 12465
11PLUGINS = tcp
12#PREFIX = xterm -T transport1 -e
13#PREFIX = xterm -T transport1 -e valgrind --tool=memcheck
14#DEBUG = YES
15
16[arm]
17PORT = 12466
18#GLOBAL_PREFIX = xterm -e valgrind --tool=memcheck
19
20[statistics]
21PORT = 12467
22#PREFIX = xterm -T statistics1 -e valgrind --tool=memcheck
23
24[tcp]
25PORT = 12468
26
27[peerinfo]
28PORT = 12469
29#PREFIX = xterm -T peerinfo1 -e valgrind --tool=memcheck
30#PREFIX = xterm -T peerinfo1 -e
31
32[core]
33PORT = 12470
34#PREFIX = xterm -T core1 -e valgrind --tool=memcheck
35#PREFIX = xterm -T core1 -e gdb -x cmd --args
36#PREFIX = xterm -T core1 -e
37#DEBUG = YES
38
39[testing]
40WEAKRANDOM = YES
diff --git a/src/core/test_core_api_peer2.conf b/src/core/test_core_api_peer2.conf
new file mode 100644
index 000000000..e3862da0f
--- /dev/null
+++ b/src/core/test_core_api_peer2.conf
@@ -0,0 +1,39 @@
1[PATHS]
2SERVICEHOME = /tmp/test-gnunetd-core-peer-2/
3DEFAULTCONFIG = test_core_api_peer2.conf
4
5[resolver]
6PORT = 22464
7#PREFIX = xterm -T resolver2 -e valgrind --tool=memcheck
8
9[transport]
10PORT = 22465
11PLUGINS = tcp
12#PREFIX = xterm -T transport2 -e
13#PREFIX = xterm -T transport2 -e valgrind --tool=memcheck
14#DEBUG = YES
15
16[arm]
17PORT = 22466
18#GLOBAL_PREFIX = xterm -e valgrind --tool=memcheck
19
20[statistics]
21PORT = 22467
22#PREFIX = xterm -T statistics2 -e valgrind --tool=memcheck
23
24[tcp]
25PORT = 22468
26
27[peerinfo]
28PORT = 22469
29#PREFIX = xterm -T peerinfo2 -e valgrind --tool=memcheck
30#PREFIX = xterm -T peerinfo2 -e
31
32[core]
33PORT = 22470
34#PREFIX = xterm -T core2 -e
35#PREFIX = xterm -T core2 -e valgrind --tool=memcheck
36#DEBUG = YES
37
38[testing]
39WEAKRANDOM = YES
diff --git a/src/core/test_core_api_start_only.c b/src/core/test_core_api_start_only.c
new file mode 100644
index 000000000..dc4bfea98
--- /dev/null
+++ b/src/core/test_core_api_start_only.c
@@ -0,0 +1,257 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 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 2, 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 * @file transport/test_core_api_start_only.c
22 * @brief testcase for core_api.c that only starts two peers,
23 * connects to the core service and shuts down again
24 */
25#include "platform.h"
26#include "gnunet_common.h"
27#include "gnunet_arm_service.h"
28#include "gnunet_core_service.h"
29#include "gnunet_getopt_lib.h"
30#include "gnunet_os_lib.h"
31#include "gnunet_program_lib.h"
32#include "gnunet_scheduler_lib.h"
33
34#define VERBOSE GNUNET_NO
35
36#define START_ARM GNUNET_YES
37
38
39/**
40 * How long until we give up on transmitting the message?
41 */
42#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43
44#define MTYPE 12345
45
46struct PeerContext
47{
48 struct GNUNET_CONFIGURATION_Handle *cfg;
49 struct GNUNET_CORE_Handle *ch;
50 struct GNUNET_PeerIdentity id;
51#if START_ARM
52 pid_t arm_pid;
53#endif
54};
55
56static struct PeerContext p1;
57
58static struct PeerContext p2;
59
60static struct GNUNET_SCHEDULER_Handle *sched;
61
62static int ok;
63
64#if VERBOSE
65#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
66#else
67#define OKPP do { ok++; } while (0)
68#endif
69
70
71
72static void
73connect_notify (void *cls,
74 const struct GNUNET_PeerIdentity *peer,
75 unsigned int bpm, struct GNUNET_TIME_Absolute last_activity)
76{
77}
78
79
80static void
81disconnect_notify (void *cls,
82 const struct GNUNET_PeerIdentity *peer,
83 unsigned int bpm,
84 struct GNUNET_TIME_Absolute last_activity)
85{
86}
87
88
89static unsigned int
90bfc_callback (void *cls,
91 const struct GNUNET_PeerIdentity *receiver,
92 void *position, unsigned int padding)
93{
94 return 0;
95}
96
97
98static int
99inbound_notify (void *cls,
100 const struct GNUNET_PeerIdentity *other,
101 const struct GNUNET_MessageHeader *message)
102{
103 return GNUNET_OK;
104}
105
106
107static int
108outbound_notify (void *cls,
109 const struct GNUNET_PeerIdentity *other,
110 const struct GNUNET_MessageHeader *message)
111{
112 return GNUNET_OK;
113}
114
115
116static struct GNUNET_CORE_MessageHandler handlers[] = {
117 {NULL, 0, 0}
118};
119
120
121
122static void
123init_notify (void *cls,
124 struct GNUNET_CORE_Handle *server,
125 const struct GNUNET_PeerIdentity *my_identity,
126 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
127{
128 struct PeerContext *p = cls;
129
130 GNUNET_assert (server != NULL);
131 p->ch = server;
132 if (cls == &p1)
133 {
134 /* connect p2 */
135 GNUNET_CORE_connect (sched,
136 p2.cfg,
137 TIMEOUT,
138 &p2,
139 &init_notify,
140 &connect_notify,
141 &disconnect_notify,
142 &bfc_callback,
143 &inbound_notify,
144 GNUNET_YES,
145 &outbound_notify, GNUNET_YES, handlers);
146 }
147 else
148 {
149 GNUNET_assert (cls == &p2);
150 GNUNET_CORE_disconnect (p1.ch);
151 GNUNET_CORE_disconnect (p2.ch);
152 GNUNET_ARM_stop_service ("core", p1.cfg, sched, TIMEOUT, NULL, NULL);
153 GNUNET_ARM_stop_service ("core", p2.cfg, sched, TIMEOUT, NULL, NULL);
154
155 ok = 0;
156 }
157}
158
159
160static void
161setup_peer (struct PeerContext *p, const char *cfgname)
162{
163 p->cfg = GNUNET_CONFIGURATION_create ();
164#if START_ARM
165 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
166 "gnunet-service-arm",
167#if VERBOSE
168 "-L", "DEBUG",
169#endif
170 "-c", cfgname, NULL);
171 sleep (1); /* allow ARM to start */
172#endif
173 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
174 GNUNET_ARM_start_service ("core", p->cfg, sched, TIMEOUT, NULL, NULL);
175}
176
177
178static void
179run (void *cls,
180 struct GNUNET_SCHEDULER_Handle *s,
181 char *const *args,
182 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
183{
184 GNUNET_assert (ok == 1);
185 OKPP;
186 sched = s;
187 setup_peer (&p1, "test_core_api_peer1.conf");
188 setup_peer (&p2, "test_core_api_peer2.conf");
189 GNUNET_CORE_connect (sched,
190 p1.cfg,
191 TIMEOUT,
192 &p1,
193 &init_notify,
194 &connect_notify,
195 &disconnect_notify,
196 &bfc_callback,
197 &inbound_notify,
198 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
199}
200
201
202static void
203stop_arm (struct PeerContext *p)
204{
205#if START_ARM
206 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
207 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
208 if (p->arm_pid != waitpid (p->arm_pid, NULL, 0))
209 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "ARM process %u stopped\n", p->arm_pid);
212#endif
213 GNUNET_CONFIGURATION_destroy (p->cfg);
214}
215
216
217static int
218check ()
219{
220 char *const argv[] = { "test-core-api",
221 "-c",
222 "test_core_api_data.conf",
223#if VERBOSE
224 "-L", "DEBUG",
225#endif
226 NULL
227 };
228 struct GNUNET_GETOPT_CommandLineOption options[] = {
229 GNUNET_GETOPT_OPTION_END
230 };
231
232 ok = 1;
233 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
234 argv, "test-core-api", "nohelp", options, &run, &ok);
235 stop_arm (&p1);
236 stop_arm (&p2);
237 return ok;
238}
239
240int
241main (int argc, char *argv[])
242{
243 int ret;
244
245 GNUNET_log_setup ("test-core-api",
246#if VERBOSE
247 "DEBUG",
248#else
249 "WARNING",
250#endif
251 NULL);
252 ret = check ();
253
254 return ret;
255}
256
257/* end of test_core_api_start_only.c */