summaryrefslogtreecommitdiff
path: root/src/core/core_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/core_api.c')
-rw-r--r--src/core/core_api.c556
1 files changed, 279 insertions, 277 deletions
diff --git a/src/core/core_api.c b/src/core/core_api.c
index 010d0eb69..d24378654 100644
--- a/src/core/core_api.c
+++ b/src/core/core_api.c
@@ -29,13 +29,14 @@
29#include "gnunet_core_service.h" 29#include "gnunet_core_service.h"
30#include "core.h" 30#include "core.h"
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "core-api", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "core-api", __VA_ARGS__)
33 33
34 34
35/** 35/**
36 * Information we track for each peer. 36 * Information we track for each peer.
37 */ 37 */
38struct PeerRecord { 38struct PeerRecord
39{
39 /** 40 /**
40 * Corresponding CORE handle. 41 * Corresponding CORE handle.
41 */ 42 */
@@ -73,7 +74,8 @@ struct PeerRecord {
73/** 74/**
74 * Context for the core service connection. 75 * Context for the core service connection.
75 */ 76 */
76struct GNUNET_CORE_Handle { 77struct GNUNET_CORE_Handle
78{
77 /** 79 /**
78 * Configuration we're using. 80 * Configuration we're using.
79 */ 81 */
@@ -149,7 +151,7 @@ struct GNUNET_CORE_Handle {
149 * @param h our handle to the core service 151 * @param h our handle to the core service
150 */ 152 */
151static void 153static void
152reconnect(struct GNUNET_CORE_Handle *h); 154reconnect (struct GNUNET_CORE_Handle *h);
153 155
154 156
155/** 157/**
@@ -159,13 +161,13 @@ reconnect(struct GNUNET_CORE_Handle *h);
159 * @param tc task context 161 * @param tc task context
160 */ 162 */
161static void 163static void
162reconnect_task(void *cls) 164reconnect_task (void *cls)
163{ 165{
164 struct GNUNET_CORE_Handle *h = cls; 166 struct GNUNET_CORE_Handle *h = cls;
165 167
166 h->reconnect_task = NULL; 168 h->reconnect_task = NULL;
167 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service after delay\n"); 169 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service after delay\n");
168 reconnect(h); 170 reconnect (h);
169} 171}
170 172
171 173
@@ -179,26 +181,26 @@ reconnect_task(void *cls)
179 * @return #GNUNET_YES (continue) 181 * @return #GNUNET_YES (continue)
180 */ 182 */
181static int 183static int
182disconnect_and_free_peer_entry(void *cls, 184disconnect_and_free_peer_entry (void *cls,
183 const struct GNUNET_PeerIdentity *key, 185 const struct GNUNET_PeerIdentity *key,
184 void *value) 186 void *value)
185{ 187{
186 struct GNUNET_CORE_Handle *h = cls; 188 struct GNUNET_CORE_Handle *h = cls;
187 struct PeerRecord *pr = value; 189 struct PeerRecord *pr = value;
188 190
189 GNUNET_assert(pr->h == h); 191 GNUNET_assert (pr->h == h);
190 if (NULL != h->disconnects) 192 if (NULL != h->disconnects)
191 h->disconnects(h->cls, &pr->peer, pr->client_cls); 193 h->disconnects (h->cls, &pr->peer, pr->client_cls);
192 GNUNET_assert(GNUNET_YES == 194 GNUNET_assert (GNUNET_YES ==
193 GNUNET_CONTAINER_multipeermap_remove(h->peers, key, pr)); 195 GNUNET_CONTAINER_multipeermap_remove (h->peers, key, pr));
194 GNUNET_MQ_destroy(pr->mq); 196 GNUNET_MQ_destroy (pr->mq);
195 GNUNET_assert(NULL == pr->mq); 197 GNUNET_assert (NULL == pr->mq);
196 if (NULL != pr->env) 198 if (NULL != pr->env)
197 { 199 {
198 GNUNET_MQ_discard(pr->env); 200 GNUNET_MQ_discard (pr->env);
199 pr->env = NULL; 201 pr->env = NULL;
200 } 202 }
201 GNUNET_free(pr); 203 GNUNET_free (pr);
202 return GNUNET_YES; 204 return GNUNET_YES;
203} 205}
204 206
@@ -210,21 +212,21 @@ disconnect_and_free_peer_entry(void *cls,
210 * @param h our handle 212 * @param h our handle
211 */ 213 */
212static void 214static void
213reconnect_later(struct GNUNET_CORE_Handle *h) 215reconnect_later (struct GNUNET_CORE_Handle *h)
214{ 216{
215 GNUNET_assert(NULL == h->reconnect_task); 217 GNUNET_assert (NULL == h->reconnect_task);
216 if (NULL != h->mq) 218 if (NULL != h->mq)
217 { 219 {
218 GNUNET_MQ_destroy(h->mq); 220 GNUNET_MQ_destroy (h->mq);
219 h->mq = NULL; 221 h->mq = NULL;
220 } 222 }
221 GNUNET_assert(NULL == h->reconnect_task); 223 GNUNET_assert (NULL == h->reconnect_task);
222 h->reconnect_task = 224 h->reconnect_task =
223 GNUNET_SCHEDULER_add_delayed(h->retry_backoff, &reconnect_task, h); 225 GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_task, h);
224 GNUNET_CONTAINER_multipeermap_iterate(h->peers, 226 GNUNET_CONTAINER_multipeermap_iterate (h->peers,
225 &disconnect_and_free_peer_entry, 227 &disconnect_and_free_peer_entry,
226 h); 228 h);
227 h->retry_backoff = GNUNET_TIME_STD_BACKOFF(h->retry_backoff); 229 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
228} 230}
229 231
230 232
@@ -236,12 +238,12 @@ reconnect_later(struct GNUNET_CORE_Handle *h)
236 * @param error error code 238 * @param error error code
237 */ 239 */
238static void 240static void
239handle_mq_error(void *cls, enum GNUNET_MQ_Error error) 241handle_mq_error (void *cls, enum GNUNET_MQ_Error error)
240{ 242{
241 struct GNUNET_CORE_Handle *h = cls; 243 struct GNUNET_CORE_Handle *h = cls;
242 244
243 LOG(GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %d\n", error); 245 LOG (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %d\n", error);
244 reconnect_later(h); 246 reconnect_later (h);
245} 247}
246 248
247 249
@@ -254,9 +256,9 @@ handle_mq_error(void *cls, enum GNUNET_MQ_Error error)
254 * @param impl_state state of the implementation 256 * @param impl_state state of the implementation
255 */ 257 */
256static void 258static void
257core_mq_send_impl(struct GNUNET_MQ_Handle *mq, 259core_mq_send_impl (struct GNUNET_MQ_Handle *mq,
258 const struct GNUNET_MessageHeader *msg, 260 const struct GNUNET_MessageHeader *msg,
259 void *impl_state) 261 void *impl_state)
260{ 262{
261 struct PeerRecord *pr = impl_state; 263 struct PeerRecord *pr = impl_state;
262 struct GNUNET_CORE_Handle *h = pr->h; 264 struct GNUNET_CORE_Handle *h = pr->h;
@@ -267,44 +269,44 @@ core_mq_send_impl(struct GNUNET_MQ_Handle *mq,
267 enum GNUNET_MQ_PriorityPreferences flags; 269 enum GNUNET_MQ_PriorityPreferences flags;
268 270
269 if (NULL == h->mq) 271 if (NULL == h->mq)
270 { 272 {
271 /* We're currently reconnecting, pretend this worked */ 273 /* We're currently reconnecting, pretend this worked */
272 GNUNET_MQ_impl_send_continue(mq); 274 GNUNET_MQ_impl_send_continue (mq);
273 return; 275 return;
274 } 276 }
275 GNUNET_assert(NULL == pr->env); 277 GNUNET_assert (NULL == pr->env);
276 /* extract options from envelope */ 278 /* extract options from envelope */
277 env = GNUNET_MQ_get_current_envelope(mq); 279 env = GNUNET_MQ_get_current_envelope (mq);
278 flags = GNUNET_MQ_env_get_options(env); 280 flags = GNUNET_MQ_env_get_options (env);
279 281
280 /* check message size for sanity */ 282 /* check message size for sanity */
281 msize = ntohs(msg->size); 283 msize = ntohs (msg->size);
282 if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof(struct SendMessage)) 284 if (msize >= GNUNET_MAX_MESSAGE_SIZE - sizeof(struct SendMessage))
283 { 285 {
284 GNUNET_break(0); 286 GNUNET_break (0);
285 GNUNET_MQ_impl_send_continue(mq); 287 GNUNET_MQ_impl_send_continue (mq);
286 return; 288 return;
287 } 289 }
288 290
289 /* ask core for transmission */ 291 /* ask core for transmission */
290 LOG(GNUNET_ERROR_TYPE_DEBUG, 292 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "Asking core for transmission of %u bytes to `%s'\n", 293 "Asking core for transmission of %u bytes to `%s'\n",
292 (unsigned int)msize, 294 (unsigned int) msize,
293 GNUNET_i2s(&pr->peer)); 295 GNUNET_i2s (&pr->peer));
294 env = GNUNET_MQ_msg(smr, GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST); 296 env = GNUNET_MQ_msg (smr, GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
295 smr->priority = htonl((uint32_t)flags); 297 smr->priority = htonl ((uint32_t) flags);
296 smr->peer = pr->peer; 298 smr->peer = pr->peer;
297 smr->size = htons(msize); 299 smr->size = htons (msize);
298 smr->smr_id = htons(++pr->smr_id_gen); 300 smr->smr_id = htons (++pr->smr_id_gen);
299 GNUNET_MQ_send(h->mq, env); 301 GNUNET_MQ_send (h->mq, env);
300 302
301 /* prepare message with actual transmission data */ 303 /* prepare message with actual transmission data */
302 pr->env = GNUNET_MQ_msg_nested_mh(sm, GNUNET_MESSAGE_TYPE_CORE_SEND, msg); 304 pr->env = GNUNET_MQ_msg_nested_mh (sm, GNUNET_MESSAGE_TYPE_CORE_SEND, msg);
303 sm->priority = htonl((uint32_t)flags); 305 sm->priority = htonl ((uint32_t) flags);
304 sm->peer = pr->peer; 306 sm->peer = pr->peer;
305 LOG(GNUNET_ERROR_TYPE_DEBUG, 307 LOG (GNUNET_ERROR_TYPE_DEBUG,
306 "Calling get_message with buffer of %u bytes\n", 308 "Calling get_message with buffer of %u bytes\n",
307 (unsigned int)msize); 309 (unsigned int) msize);
308} 310}
309 311
310 312
@@ -316,11 +318,11 @@ core_mq_send_impl(struct GNUNET_MQ_Handle *mq,
316 * @param impl_state state of the implementation 318 * @param impl_state state of the implementation
317 */ 319 */
318static void 320static void
319core_mq_destroy_impl(struct GNUNET_MQ_Handle *mq, void *impl_state) 321core_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
320{ 322{
321 struct PeerRecord *pr = impl_state; 323 struct PeerRecord *pr = impl_state;
322 324
323 GNUNET_assert(mq == pr->mq); 325 GNUNET_assert (mq == pr->mq);
324 pr->mq = NULL; 326 pr->mq = NULL;
325} 327}
326 328
@@ -333,13 +335,13 @@ core_mq_destroy_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
333 * @param impl_state state specific to the implementation 335 * @param impl_state state specific to the implementation
334 */ 336 */
335static void 337static void
336core_mq_cancel_impl(struct GNUNET_MQ_Handle *mq, void *impl_state) 338core_mq_cancel_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
337{ 339{
338 struct PeerRecord *pr = impl_state; 340 struct PeerRecord *pr = impl_state;
339 341
340 (void)mq; 342 (void) mq;
341 GNUNET_assert(NULL != pr->env); 343 GNUNET_assert (NULL != pr->env);
342 GNUNET_MQ_discard(pr->env); 344 GNUNET_MQ_discard (pr->env);
343 pr->env = NULL; 345 pr->env = NULL;
344} 346}
345 347
@@ -353,12 +355,12 @@ core_mq_cancel_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
353 * @param error error code 355 * @param error error code
354 */ 356 */
355static void 357static void
356core_mq_error_handler(void *cls, enum GNUNET_MQ_Error error) 358core_mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
357{ 359{
358 /* struct PeerRecord *pr = cls; */ 360 /* struct PeerRecord *pr = cls; */
359 (void)cls; 361 (void) cls;
360 (void)error; 362 (void) error;
361 GNUNET_break_op(0); 363 GNUNET_break_op (0);
362} 364}
363 365
364 366
@@ -371,32 +373,32 @@ core_mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
371 * @param peer the peer that is connecting to us 373 * @param peer the peer that is connecting to us
372 */ 374 */
373static void 375static void
374connect_peer(struct GNUNET_CORE_Handle *h, 376connect_peer (struct GNUNET_CORE_Handle *h,
375 const struct GNUNET_PeerIdentity *peer) 377 const struct GNUNET_PeerIdentity *peer)
376{ 378{
377 struct PeerRecord *pr; 379 struct PeerRecord *pr;
378 380
379 pr = GNUNET_new(struct PeerRecord); 381 pr = GNUNET_new (struct PeerRecord);
380 pr->peer = *peer; 382 pr->peer = *peer;
381 pr->h = h; 383 pr->h = h;
382 GNUNET_assert(GNUNET_YES == 384 GNUNET_assert (GNUNET_YES ==
383 GNUNET_CONTAINER_multipeermap_put( 385 GNUNET_CONTAINER_multipeermap_put (
384 h->peers, 386 h->peers,
385 &pr->peer, 387 &pr->peer,
386 pr, 388 pr,
387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 389 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
388 pr->mq = GNUNET_MQ_queue_for_callbacks(&core_mq_send_impl, 390 pr->mq = GNUNET_MQ_queue_for_callbacks (&core_mq_send_impl,
389 &core_mq_destroy_impl, 391 &core_mq_destroy_impl,
390 &core_mq_cancel_impl, 392 &core_mq_cancel_impl,
391 pr, 393 pr,
392 h->handlers, 394 h->handlers,
393 &core_mq_error_handler, 395 &core_mq_error_handler,
394 pr); 396 pr);
395 if (NULL != h->connects) 397 if (NULL != h->connects)
396 { 398 {
397 pr->client_cls = h->connects(h->cls, &pr->peer, pr->mq); 399 pr->client_cls = h->connects (h->cls, &pr->peer, pr->mq);
398 GNUNET_MQ_set_handlers_closure(pr->mq, pr->client_cls); 400 GNUNET_MQ_set_handlers_closure (pr->mq, pr->client_cls);
399 } 401 }
400} 402}
401 403
402 404
@@ -409,42 +411,42 @@ connect_peer(struct GNUNET_CORE_Handle *h,
409 * @param m the init reply 411 * @param m the init reply
410 */ 412 */
411static void 413static void
412handle_init_reply(void *cls, const struct InitReplyMessage *m) 414handle_init_reply (void *cls, const struct InitReplyMessage *m)
413{ 415{
414 struct GNUNET_CORE_Handle *h = cls; 416 struct GNUNET_CORE_Handle *h = cls;
415 GNUNET_CORE_StartupCallback init; 417 GNUNET_CORE_StartupCallback init;
416 418
417 GNUNET_break(0 == ntohl(m->reserved)); 419 GNUNET_break (0 == ntohl (m->reserved));
418 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS; 420 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
419 if (NULL != (init = h->init)) 421 if (NULL != (init = h->init))
422 {
423 /* mark so we don't call init on reconnect */
424 h->init = NULL;
425 h->me = m->my_identity;
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "Connected to core service of peer `%s'.\n",
428 GNUNET_i2s (&h->me));
429 h->have_init = GNUNET_YES;
430 init (h->cls, &h->me);
431 }
432 else
433 {
434 LOG (GNUNET_ERROR_TYPE_DEBUG,
435 "Successfully reconnected to core service.\n");
436 if (GNUNET_NO == h->have_init)
420 { 437 {
421 /* mark so we don't call init on reconnect */
422 h->init = NULL;
423 h->me = m->my_identity; 438 h->me = m->my_identity;
424 LOG(GNUNET_ERROR_TYPE_DEBUG,
425 "Connected to core service of peer `%s'.\n",
426 GNUNET_i2s(&h->me));
427 h->have_init = GNUNET_YES; 439 h->have_init = GNUNET_YES;
428 init(h->cls, &h->me);
429 } 440 }
430 else 441 else
431 { 442 {
432 LOG(GNUNET_ERROR_TYPE_DEBUG, 443 GNUNET_break (0 == memcmp (&h->me,
433 "Successfully reconnected to core service.\n"); 444 &m->my_identity,
434 if (GNUNET_NO == h->have_init) 445 sizeof(struct GNUNET_PeerIdentity)));
435 {
436 h->me = m->my_identity;
437 h->have_init = GNUNET_YES;
438 }
439 else
440 {
441 GNUNET_break(0 == memcmp(&h->me,
442 &m->my_identity,
443 sizeof(struct GNUNET_PeerIdentity)));
444 }
445 } 446 }
447 }
446 /* fake 'connect to self' */ 448 /* fake 'connect to self' */
447 connect_peer(h, &h->me); 449 connect_peer (h, &h->me);
448} 450}
449 451
450 452
@@ -456,28 +458,28 @@ handle_init_reply(void *cls, const struct InitReplyMessage *m)
456 * @param cnm the connect message 458 * @param cnm the connect message
457 */ 459 */
458static void 460static void
459handle_connect_notify(void *cls, const struct ConnectNotifyMessage *cnm) 461handle_connect_notify (void *cls, const struct ConnectNotifyMessage *cnm)
460{ 462{
461 struct GNUNET_CORE_Handle *h = cls; 463 struct GNUNET_CORE_Handle *h = cls;
462 struct PeerRecord *pr; 464 struct PeerRecord *pr;
463 465
464 LOG(GNUNET_ERROR_TYPE_DEBUG, 466 LOG (GNUNET_ERROR_TYPE_DEBUG,
465 "Received notification about connection from `%s'.\n", 467 "Received notification about connection from `%s'.\n",
466 GNUNET_i2s(&cnm->peer)); 468 GNUNET_i2s (&cnm->peer));
467 if (0 == memcmp(&h->me, &cnm->peer, sizeof(struct GNUNET_PeerIdentity))) 469 if (0 == memcmp (&h->me, &cnm->peer, sizeof(struct GNUNET_PeerIdentity)))
468 { 470 {
469 /* connect to self!? */ 471 /* connect to self!? */
470 GNUNET_break(0); 472 GNUNET_break (0);
471 return; 473 return;
472 } 474 }
473 pr = GNUNET_CONTAINER_multipeermap_get(h->peers, &cnm->peer); 475 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &cnm->peer);
474 if (NULL != pr) 476 if (NULL != pr)
475 { 477 {
476 GNUNET_break(0); 478 GNUNET_break (0);
477 reconnect_later(h); 479 reconnect_later (h);
478 return; 480 return;
479 } 481 }
480 connect_peer(h, &cnm->peer); 482 connect_peer (h, &cnm->peer);
481} 483}
482 484
483 485
@@ -489,29 +491,29 @@ handle_connect_notify(void *cls, const struct ConnectNotifyMessage *cnm)
489 * @param dnm message about the disconnect event 491 * @param dnm message about the disconnect event
490 */ 492 */
491static void 493static void
492handle_disconnect_notify(void *cls, const struct DisconnectNotifyMessage *dnm) 494handle_disconnect_notify (void *cls, const struct DisconnectNotifyMessage *dnm)
493{ 495{
494 struct GNUNET_CORE_Handle *h = cls; 496 struct GNUNET_CORE_Handle *h = cls;
495 struct PeerRecord *pr; 497 struct PeerRecord *pr;
496 498
497 if (0 == memcmp(&h->me, &dnm->peer, sizeof(struct GNUNET_PeerIdentity))) 499 if (0 == memcmp (&h->me, &dnm->peer, sizeof(struct GNUNET_PeerIdentity)))
498 { 500 {
499 /* disconnect from self!? */ 501 /* disconnect from self!? */
500 GNUNET_break(0); 502 GNUNET_break (0);
501 return; 503 return;
502 } 504 }
503 GNUNET_break(0 == ntohl(dnm->reserved)); 505 GNUNET_break (0 == ntohl (dnm->reserved));
504 LOG(GNUNET_ERROR_TYPE_DEBUG, 506 LOG (GNUNET_ERROR_TYPE_DEBUG,
505 "Received notification about disconnect from `%s'.\n", 507 "Received notification about disconnect from `%s'.\n",
506 GNUNET_i2s(&dnm->peer)); 508 GNUNET_i2s (&dnm->peer));
507 pr = GNUNET_CONTAINER_multipeermap_get(h->peers, &dnm->peer); 509 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &dnm->peer);
508 if (NULL == pr) 510 if (NULL == pr)
509 { 511 {
510 GNUNET_break(0); 512 GNUNET_break (0);
511 reconnect_later(h); 513 reconnect_later (h);
512 return; 514 return;
513 } 515 }
514 disconnect_and_free_peer_entry(h, &pr->peer, pr); 516 disconnect_and_free_peer_entry (h, &pr->peer, pr);
515} 517}
516 518
517 519
@@ -523,24 +525,24 @@ handle_disconnect_notify(void *cls, const struct DisconnectNotifyMessage *dnm)
523 * @return #GNUNET_OK if the message is well-formed 525 * @return #GNUNET_OK if the message is well-formed
524 */ 526 */
525static int 527static int
526check_notify_inbound(void *cls, const struct NotifyTrafficMessage *ntm) 528check_notify_inbound (void *cls, const struct NotifyTrafficMessage *ntm)
527{ 529{
528 uint16_t msize; 530 uint16_t msize;
529 const struct GNUNET_MessageHeader *em; 531 const struct GNUNET_MessageHeader *em;
530 532
531 (void)cls; 533 (void) cls;
532 msize = ntohs(ntm->header.size) - sizeof(struct NotifyTrafficMessage); 534 msize = ntohs (ntm->header.size) - sizeof(struct NotifyTrafficMessage);
533 if (msize < sizeof(struct GNUNET_MessageHeader)) 535 if (msize < sizeof(struct GNUNET_MessageHeader))
534 { 536 {
535 GNUNET_break(0); 537 GNUNET_break (0);
536 return GNUNET_SYSERR; 538 return GNUNET_SYSERR;
537 } 539 }
538 em = (const struct GNUNET_MessageHeader *)&ntm[1]; 540 em = (const struct GNUNET_MessageHeader *) &ntm[1];
539 if (msize != ntohs(em->size)) 541 if (msize != ntohs (em->size))
540 { 542 {
541 GNUNET_break(0); 543 GNUNET_break (0);
542 return GNUNET_SYSERR; 544 return GNUNET_SYSERR;
543 } 545 }
544 return GNUNET_OK; 546 return GNUNET_OK;
545} 547}
546 548
@@ -553,24 +555,24 @@ check_notify_inbound(void *cls, const struct NotifyTrafficMessage *ntm)
553 * @param ntm the message we got from CORE. 555 * @param ntm the message we got from CORE.
554 */ 556 */
555static void 557static void
556handle_notify_inbound(void *cls, const struct NotifyTrafficMessage *ntm) 558handle_notify_inbound (void *cls, const struct NotifyTrafficMessage *ntm)
557{ 559{
558 struct GNUNET_CORE_Handle *h = cls; 560 struct GNUNET_CORE_Handle *h = cls;
559 const struct GNUNET_MessageHeader *em; 561 const struct GNUNET_MessageHeader *em;
560 struct PeerRecord *pr; 562 struct PeerRecord *pr;
561 563
562 LOG(GNUNET_ERROR_TYPE_DEBUG, 564 LOG (GNUNET_ERROR_TYPE_DEBUG,
563 "Received inbound message from `%s'.\n", 565 "Received inbound message from `%s'.\n",
564 GNUNET_i2s(&ntm->peer)); 566 GNUNET_i2s (&ntm->peer));
565 em = (const struct GNUNET_MessageHeader *)&ntm[1]; 567 em = (const struct GNUNET_MessageHeader *) &ntm[1];
566 pr = GNUNET_CONTAINER_multipeermap_get(h->peers, &ntm->peer); 568 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &ntm->peer);
567 if (NULL == pr) 569 if (NULL == pr)
568 { 570 {
569 GNUNET_break(0); 571 GNUNET_break (0);
570 reconnect_later(h); 572 reconnect_later (h);
571 return; 573 return;
572 } 574 }
573 GNUNET_MQ_inject_message(pr->mq, em); 575 GNUNET_MQ_inject_message (pr->mq, em);
574} 576}
575 577
576 578
@@ -583,38 +585,38 @@ handle_notify_inbound(void *cls, const struct NotifyTrafficMessage *ntm)
583 * @param smr the message we got 585 * @param smr the message we got
584 */ 586 */
585static void 587static void
586handle_send_ready(void *cls, const struct SendMessageReady *smr) 588handle_send_ready (void *cls, const struct SendMessageReady *smr)
587{ 589{
588 struct GNUNET_CORE_Handle *h = cls; 590 struct GNUNET_CORE_Handle *h = cls;
589 struct PeerRecord *pr; 591 struct PeerRecord *pr;
590 592
591 pr = GNUNET_CONTAINER_multipeermap_get(h->peers, &smr->peer); 593 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &smr->peer);
592 if (NULL == pr) 594 if (NULL == pr)
593 { 595 {
594 GNUNET_break(0); 596 GNUNET_break (0);
595 reconnect_later(h); 597 reconnect_later (h);
596 return; 598 return;
597 } 599 }
598 LOG(GNUNET_ERROR_TYPE_DEBUG, 600 LOG (GNUNET_ERROR_TYPE_DEBUG,
599 "Received notification about transmission readiness to `%s'.\n", 601 "Received notification about transmission readiness to `%s'.\n",
600 GNUNET_i2s(&smr->peer)); 602 GNUNET_i2s (&smr->peer));
601 if (NULL == pr->env) 603 if (NULL == pr->env)
602 { 604 {
603 /* request must have been cancelled between the original request 605 /* request must have been cancelled between the original request
604 * and the response from CORE, ignore CORE's readiness */ 606 * and the response from CORE, ignore CORE's readiness */
605 return; 607 return;
606 } 608 }
607 if (ntohs(smr->smr_id) != pr->smr_id_gen) 609 if (ntohs (smr->smr_id) != pr->smr_id_gen)
608 { 610 {
609 /* READY message is for expired or cancelled message, 611 /* READY message is for expired or cancelled message,
610 * ignore! (we should have already sent another request) */ 612 * ignore! (we should have already sent another request) */
611 return; 613 return;
612 } 614 }
613 615
614 /* ok, all good, send message out! */ 616 /* ok, all good, send message out! */
615 GNUNET_MQ_send(h->mq, pr->env); 617 GNUNET_MQ_send (h->mq, pr->env);
616 pr->env = NULL; 618 pr->env = NULL;
617 GNUNET_MQ_impl_send_continue(pr->mq); 619 GNUNET_MQ_impl_send_continue (pr->mq);
618} 620}
619 621
620 622
@@ -625,50 +627,50 @@ handle_send_ready(void *cls, const struct SendMessageReady *smr)
625 * @param h our handle to the core service 627 * @param h our handle to the core service
626 */ 628 */
627static void 629static void
628reconnect(struct GNUNET_CORE_Handle *h) 630reconnect (struct GNUNET_CORE_Handle *h)
629{ 631{
630 struct GNUNET_MQ_MessageHandler handlers[] = 632 struct GNUNET_MQ_MessageHandler handlers[] =
631 { GNUNET_MQ_hd_fixed_size(init_reply, 633 { GNUNET_MQ_hd_fixed_size (init_reply,
632 GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY, 634 GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY,
633 struct InitReplyMessage, 635 struct InitReplyMessage,
634 h), 636 h),
635 GNUNET_MQ_hd_fixed_size(connect_notify, 637 GNUNET_MQ_hd_fixed_size (connect_notify,
636 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT, 638 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT,
637 struct ConnectNotifyMessage, 639 struct ConnectNotifyMessage,
638 h), 640 h),
639 GNUNET_MQ_hd_fixed_size(disconnect_notify, 641 GNUNET_MQ_hd_fixed_size (disconnect_notify,
640 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT, 642 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT,
641 struct DisconnectNotifyMessage, 643 struct DisconnectNotifyMessage,
642 h), 644 h),
643 GNUNET_MQ_hd_var_size(notify_inbound, 645 GNUNET_MQ_hd_var_size (notify_inbound,
644 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND, 646 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND,
645 struct NotifyTrafficMessage, 647 struct NotifyTrafficMessage,
646 h), 648 h),
647 GNUNET_MQ_hd_fixed_size(send_ready, 649 GNUNET_MQ_hd_fixed_size (send_ready,
648 GNUNET_MESSAGE_TYPE_CORE_SEND_READY, 650 GNUNET_MESSAGE_TYPE_CORE_SEND_READY,
649 struct SendMessageReady, 651 struct SendMessageReady,
650 h), 652 h),
651 GNUNET_MQ_handler_end() }; 653 GNUNET_MQ_handler_end () };
652 struct InitMessage *init; 654 struct InitMessage *init;
653 struct GNUNET_MQ_Envelope *env; 655 struct GNUNET_MQ_Envelope *env;
654 uint16_t *ts; 656 uint16_t *ts;
655 657
656 GNUNET_assert(NULL == h->mq); 658 GNUNET_assert (NULL == h->mq);
657 h->mq = GNUNET_CLIENT_connect(h->cfg, "core", handlers, &handle_mq_error, h); 659 h->mq = GNUNET_CLIENT_connect (h->cfg, "core", handlers, &handle_mq_error, h);
658 if (NULL == h->mq) 660 if (NULL == h->mq)
659 { 661 {
660 reconnect_later(h); 662 reconnect_later (h);
661 return; 663 return;
662 } 664 }
663 env = GNUNET_MQ_msg_extra(init, 665 env = GNUNET_MQ_msg_extra (init,
664 sizeof(uint16_t) * h->hcnt, 666 sizeof(uint16_t) * h->hcnt,
665 GNUNET_MESSAGE_TYPE_CORE_INIT); 667 GNUNET_MESSAGE_TYPE_CORE_INIT);
666 LOG(GNUNET_ERROR_TYPE_INFO, "(Re)connecting to CORE service\n"); 668 LOG (GNUNET_ERROR_TYPE_INFO, "(Re)connecting to CORE service\n");
667 init->options = htonl(0); 669 init->options = htonl (0);
668 ts = (uint16_t *)&init[1]; 670 ts = (uint16_t *) &init[1];
669 for (unsigned int hpos = 0; hpos < h->hcnt; hpos++) 671 for (unsigned int hpos = 0; hpos < h->hcnt; hpos++)
670 ts[hpos] = htons(h->handlers[hpos].type); 672 ts[hpos] = htons (h->handlers[hpos].type);
671 GNUNET_MQ_send(h->mq, env); 673 GNUNET_MQ_send (h->mq, env);
672} 674}
673 675
674 676
@@ -687,34 +689,34 @@ reconnect(struct GNUNET_CORE_Handle *h)
687 * NULL on error (in this case, init is never called) 689 * NULL on error (in this case, init is never called)
688 */ 690 */
689struct GNUNET_CORE_Handle * 691struct GNUNET_CORE_Handle *
690GNUNET_CORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, 692GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
691 void *cls, 693 void *cls,
692 GNUNET_CORE_StartupCallback init, 694 GNUNET_CORE_StartupCallback init,
693 GNUNET_CORE_ConnectEventHandler connects, 695 GNUNET_CORE_ConnectEventHandler connects,
694 GNUNET_CORE_DisconnectEventHandler disconnects, 696 GNUNET_CORE_DisconnectEventHandler disconnects,
695 const struct GNUNET_MQ_MessageHandler *handlers) 697 const struct GNUNET_MQ_MessageHandler *handlers)
696{ 698{
697 struct GNUNET_CORE_Handle *h; 699 struct GNUNET_CORE_Handle *h;
698 700
699 h = GNUNET_new(struct GNUNET_CORE_Handle); 701 h = GNUNET_new (struct GNUNET_CORE_Handle);
700 h->cfg = cfg; 702 h->cfg = cfg;
701 h->cls = cls; 703 h->cls = cls;
702 h->init = init; 704 h->init = init;
703 h->connects = connects; 705 h->connects = connects;
704 h->disconnects = disconnects; 706 h->disconnects = disconnects;
705 h->peers = GNUNET_CONTAINER_multipeermap_create(128, GNUNET_NO); 707 h->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
706 h->handlers = GNUNET_MQ_copy_handlers(handlers); 708 h->handlers = GNUNET_MQ_copy_handlers (handlers);
707 h->hcnt = GNUNET_MQ_count_handlers(handlers); 709 h->hcnt = GNUNET_MQ_count_handlers (handlers);
708 GNUNET_assert(h->hcnt < 710 GNUNET_assert (h->hcnt <
709 (GNUNET_MAX_MESSAGE_SIZE - sizeof(struct InitMessage)) / 711 (GNUNET_MAX_MESSAGE_SIZE - sizeof(struct InitMessage))
710 sizeof(uint16_t)); 712 / sizeof(uint16_t));
711 LOG(GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n"); 713 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n");
712 reconnect(h); 714 reconnect (h);
713 if (NULL == h->mq) 715 if (NULL == h->mq)
714 { 716 {
715 GNUNET_CORE_disconnect(h); 717 GNUNET_CORE_disconnect (h);
716 return NULL; 718 return NULL;
717 } 719 }
718 return h; 720 return h;
719} 721}
720 722
@@ -725,26 +727,26 @@ GNUNET_CORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg,
725 * @param handle connection to core to disconnect 727 * @param handle connection to core to disconnect
726 */ 728 */
727void 729void
728GNUNET_CORE_disconnect(struct GNUNET_CORE_Handle *handle) 730GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
729{ 731{
730 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n"); 732 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
731 GNUNET_CONTAINER_multipeermap_iterate(handle->peers, 733 GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
732 &disconnect_and_free_peer_entry, 734 &disconnect_and_free_peer_entry,
733 handle); 735 handle);
734 GNUNET_CONTAINER_multipeermap_destroy(handle->peers); 736 GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
735 handle->peers = NULL; 737 handle->peers = NULL;
736 if (NULL != handle->reconnect_task) 738 if (NULL != handle->reconnect_task)
737 { 739 {
738 GNUNET_SCHEDULER_cancel(handle->reconnect_task); 740 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
739 handle->reconnect_task = NULL; 741 handle->reconnect_task = NULL;
740 } 742 }
741 if (NULL != handle->mq) 743 if (NULL != handle->mq)
742 { 744 {
743 GNUNET_MQ_destroy(handle->mq); 745 GNUNET_MQ_destroy (handle->mq);
744 handle->mq = NULL; 746 handle->mq = NULL;
745 } 747 }
746 GNUNET_free_non_null(handle->handlers); 748 GNUNET_free_non_null (handle->handlers);
747 GNUNET_free(handle); 749 GNUNET_free (handle);
748} 750}
749 751
750 752
@@ -756,12 +758,12 @@ GNUNET_CORE_disconnect(struct GNUNET_CORE_Handle *handle)
756 * @return NULL if peer is not connected 758 * @return NULL if peer is not connected
757 */ 759 */
758struct GNUNET_MQ_Handle * 760struct GNUNET_MQ_Handle *
759GNUNET_CORE_get_mq(const struct GNUNET_CORE_Handle *h, 761GNUNET_CORE_get_mq (const struct GNUNET_CORE_Handle *h,
760 const struct GNUNET_PeerIdentity *pid) 762 const struct GNUNET_PeerIdentity *pid)
761{ 763{
762 struct PeerRecord *pr; 764 struct PeerRecord *pr;
763 765
764 pr = GNUNET_CONTAINER_multipeermap_get(h->peers, pid); 766 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, pid);
765 if (NULL == pr) 767 if (NULL == pr)
766 return NULL; 768 return NULL;
767 return pr->mq; 769 return pr->mq;