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