diff options
author | Christian Grothoff <christian@grothoff.org> | 2014-11-30 21:00:06 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2014-11-30 21:00:06 +0000 |
commit | 21309bc4672041b0ae505dfed2e5665d16d89599 (patch) | |
tree | e22668a6d9d2679386afb25f07a781cfc60611aa /src/scalarproduct | |
parent | b55fbe08e368c3b77897615c577ff9b537485e26 (diff) | |
download | gnunet-21309bc4672041b0ae505dfed2e5665d16d89599.tar.gz gnunet-21309bc4672041b0ae505dfed2e5665d16d89599.zip |
-code cleanups
Diffstat (limited to 'src/scalarproduct')
-rw-r--r-- | src/scalarproduct/gnunet-service-scalarproduct.c | 112 |
1 files changed, 57 insertions, 55 deletions
diff --git a/src/scalarproduct/gnunet-service-scalarproduct.c b/src/scalarproduct/gnunet-service-scalarproduct.c index b5fc1471f..39274e4d4 100644 --- a/src/scalarproduct/gnunet-service-scalarproduct.c +++ b/src/scalarproduct/gnunet-service-scalarproduct.c | |||
@@ -462,7 +462,7 @@ prepare_bobs_cryptodata_message_multipart (void *cls); | |||
462 | 462 | ||
463 | 463 | ||
464 | /** | 464 | /** |
465 | * computes the square sum over a vector of a given length. | 465 | * Computes the square sum over a vector of a given length. |
466 | * | 466 | * |
467 | * @param vector the vector to encrypt | 467 | * @param vector the vector to encrypt |
468 | * @param length the length of the vector | 468 | * @param length the length of the vector |
@@ -474,18 +474,16 @@ compute_square_sum (gcry_mpi_t *vector, | |||
474 | { | 474 | { |
475 | gcry_mpi_t elem; | 475 | gcry_mpi_t elem; |
476 | gcry_mpi_t sum; | 476 | gcry_mpi_t sum; |
477 | int32_t i; | 477 | uint32_t i; |
478 | |||
479 | GNUNET_assert (sum = gcry_mpi_new (0)); | ||
480 | GNUNET_assert (elem = gcry_mpi_new (0)); | ||
481 | 478 | ||
482 | // calculare E(sum (ai ^ 2), publickey) | 479 | GNUNET_assert (NULL != (sum = gcry_mpi_new (0))); |
483 | for (i = 0; i < length; i++) { | 480 | GNUNET_assert (NULL != (elem = gcry_mpi_new (0))); |
481 | for (i = 0; i < length; i++) | ||
482 | { | ||
484 | gcry_mpi_mul (elem, vector[i], vector[i]); | 483 | gcry_mpi_mul (elem, vector[i], vector[i]); |
485 | gcry_mpi_add (sum, sum, elem); | 484 | gcry_mpi_add (sum, sum, elem); |
486 | } | 485 | } |
487 | gcry_mpi_release (elem); | 486 | gcry_mpi_release (elem); |
488 | |||
489 | return sum; | 487 | return sum; |
490 | } | 488 | } |
491 | 489 | ||
@@ -498,62 +496,63 @@ compute_square_sum (gcry_mpi_t *vector, | |||
498 | static void | 496 | static void |
499 | free_session_variables (struct ServiceSession *s) | 497 | free_session_variables (struct ServiceSession *s) |
500 | { | 498 | { |
501 | while (NULL != s->a_head) | 499 | struct SortedValue *e; |
500 | while (NULL != (e = s->a_head)) | ||
502 | { | 501 | { |
503 | struct SortedValue * e = s->a_head; | ||
504 | |||
505 | GNUNET_free (e->elem); | 502 | GNUNET_free (e->elem); |
506 | gcry_mpi_release (e->val); | 503 | gcry_mpi_release (e->val); |
507 | GNUNET_CONTAINER_DLL_remove (s->a_head, s->a_tail, e); | 504 | GNUNET_CONTAINER_DLL_remove (s->a_head, |
505 | s->a_tail, | ||
506 | e); | ||
508 | GNUNET_free (e); | 507 | GNUNET_free (e); |
509 | } | 508 | } |
510 | if (s->e_a) | 509 | if (NULL != s->intersected_elements) |
511 | { | ||
512 | GNUNET_free (s->e_a); | ||
513 | s->e_a = NULL; | ||
514 | } | ||
515 | if (s->sorted_elements) | ||
516 | { | ||
517 | GNUNET_free (s->sorted_elements); | ||
518 | s->sorted_elements = NULL; | ||
519 | } | ||
520 | if (s->intersected_elements) | ||
521 | { | 510 | { |
522 | GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements); | 511 | GNUNET_CONTAINER_multihashmap_destroy (s->intersected_elements); |
523 | //elements are freed independently in session->a_head/tail | 512 | /* elements are freed independently in above loop over a_head */ |
524 | s->intersected_elements = NULL; | 513 | s->intersected_elements = NULL; |
525 | } | 514 | } |
526 | if (s->intersection_listen) | 515 | if (NULL != s->intersection_listen) |
527 | { | 516 | { |
528 | GNUNET_SET_listen_cancel (s->intersection_listen); | 517 | GNUNET_SET_listen_cancel (s->intersection_listen); |
529 | s->intersection_listen = NULL; | 518 | s->intersection_listen = NULL; |
530 | } | 519 | } |
531 | if (s->intersection_op) | 520 | if (NULL != s->intersection_op) |
532 | { | 521 | { |
533 | GNUNET_SET_operation_cancel (s->intersection_op); | 522 | GNUNET_SET_operation_cancel (s->intersection_op); |
534 | s->intersection_op = NULL; | 523 | s->intersection_op = NULL; |
535 | } | 524 | } |
536 | if (s->intersection_set) | 525 | if (NULL != s->intersection_set) |
537 | { | 526 | { |
538 | GNUNET_SET_destroy (s->intersection_set); | 527 | GNUNET_SET_destroy (s->intersection_set); |
539 | s->intersection_set = NULL; | 528 | s->intersection_set = NULL; |
540 | } | 529 | } |
541 | if (s->msg) | 530 | if (NULL != s->e_a) |
531 | { | ||
532 | GNUNET_free (s->e_a); | ||
533 | s->e_a = NULL; | ||
534 | } | ||
535 | if (NULL != s->sorted_elements) | ||
536 | { | ||
537 | GNUNET_free (s->sorted_elements); | ||
538 | s->sorted_elements = NULL; | ||
539 | } | ||
540 | if (NULL != s->msg) | ||
542 | { | 541 | { |
543 | GNUNET_free (s->msg); | 542 | GNUNET_free (s->msg); |
544 | s->msg = NULL; | 543 | s->msg = NULL; |
545 | } | 544 | } |
546 | if (s->r) | 545 | if (NULL != s->r) |
547 | { | 546 | { |
548 | GNUNET_free (s->r); | 547 | GNUNET_free (s->r); |
549 | s->r = NULL; | 548 | s->r = NULL; |
550 | } | 549 | } |
551 | if (s->r_prime) | 550 | if (NULL != s->r_prime) |
552 | { | 551 | { |
553 | GNUNET_free (s->r_prime); | 552 | GNUNET_free (s->r_prime); |
554 | s->r_prime = NULL; | 553 | s->r_prime = NULL; |
555 | } | 554 | } |
556 | if (s->product) | 555 | if (NULL != s->product) |
557 | { | 556 | { |
558 | gcry_mpi_release (s->product); | 557 | gcry_mpi_release (s->product); |
559 | s->product = NULL; | 558 | s->product = NULL; |
@@ -562,12 +561,13 @@ free_session_variables (struct ServiceSession *s) | |||
562 | 561 | ||
563 | 562 | ||
564 | /** | 563 | /** |
565 | * Primitive callback for copying over a message, as they | 564 | * Primitive callback for copying over a message, as they usually are |
566 | * usually are too complex to be handled in the callback itself. | 565 | * too complex to be handled in the callback itself. Clears a |
567 | * clears a session-callback, if a session was handed over and the transmit handle was stored | 566 | * session-callback, if a session was handed over and the transmit |
567 | * handle was stored. | ||
568 | * | 568 | * |
569 | * @param cls the session containing the message object | 569 | * @param cls the session containing the message object |
570 | * @param size the size of the buffer we got | 570 | * @param size the size of the @a buf we got |
571 | * @param buf the buffer to copy the message to | 571 | * @param buf the buffer to copy the message to |
572 | * @return 0 if we couldn't copy, else the size copied over | 572 | * @return 0 if we couldn't copy, else the size copied over |
573 | */ | 573 | */ |
@@ -576,47 +576,44 @@ cb_transfer_message (void *cls, | |||
576 | size_t size, | 576 | size_t size, |
577 | void *buf) | 577 | void *buf) |
578 | { | 578 | { |
579 | struct ServiceSession * s = cls; | 579 | struct ServiceSession *s = cls; |
580 | uint16_t type; | 580 | uint16_t type; |
581 | 581 | ||
582 | s->client_transmit_handle = NULL; | ||
582 | GNUNET_assert (buf); | 583 | GNUNET_assert (buf); |
583 | if (ntohs (s->msg->size) != size) | 584 | if (ntohs (s->msg->size) > size) |
584 | { | 585 | { |
585 | GNUNET_break (0); | 586 | GNUNET_break (0); |
586 | return 0; | 587 | return 0; |
587 | } | 588 | } |
588 | 589 | size = ntohs (s->msg->size); | |
589 | type = ntohs (s->msg->type); | 590 | type = ntohs (s->msg->type); |
590 | memcpy (buf, s->msg, size); | 591 | memcpy (buf, |
592 | s->msg, | ||
593 | size); | ||
591 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 594 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
592 | "Sent a message of type %hu.\n", | 595 | "Sending a message of type %u.\n", |
593 | type); | 596 | (unsigned int) type); |
594 | GNUNET_free (s->msg); | 597 | GNUNET_free (s->msg); |
595 | s->msg = NULL; | 598 | s->msg = NULL; |
596 | 599 | ||
597 | switch (type) | 600 | switch (type) |
598 | { | 601 | { |
599 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT: | 602 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT: |
600 | s->client_transmit_handle = NULL; | ||
601 | free_session_variables (s); | 603 | free_session_variables (s); |
604 | // FIXME: that does not fully clean up 's' | ||
602 | break; | 605 | break; |
603 | |||
604 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION: | 606 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SESSION_INITIALIZATION: |
605 | s->service_transmit_handle = NULL; | ||
606 | break; | 607 | break; |
607 | |||
608 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA: | 608 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA: |
609 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA_MULTIPART: | 609 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_CRYPTODATA_MULTIPART: |
610 | s->service_transmit_handle = NULL; | ||
611 | if (s->used_element_count != s->transferred_element_count) | 610 | if (s->used_element_count != s->transferred_element_count) |
612 | prepare_alices_cyrptodata_message_multipart (s); | 611 | prepare_alices_cyrptodata_message_multipart (s); |
613 | else | 612 | else |
614 | s->channel = NULL; | 613 | s->channel = NULL; |
615 | break; | 614 | break; |
616 | |||
617 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA: | 615 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA: |
618 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART: | 616 | case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_CRYPTODATA_MULTIPART: |
619 | s->service_transmit_handle = NULL; | ||
620 | if (s->used_element_count != s->transferred_element_count) | 617 | if (s->used_element_count != s->transferred_element_count) |
621 | prepare_bobs_cryptodata_message_multipart (s); | 618 | prepare_bobs_cryptodata_message_multipart (s); |
622 | else | 619 | else |
@@ -633,15 +630,17 @@ cb_transfer_message (void *cls, | |||
633 | * Finds a not terminated client/service session in the | 630 | * Finds a not terminated client/service session in the |
634 | * given DLL based on session key, element count and state. | 631 | * given DLL based on session key, element count and state. |
635 | * | 632 | * |
633 | * FIXME: Use hashmap based on key instead of linear search. | ||
634 | * | ||
636 | * @param tail - the tail of the DLL | 635 | * @param tail - the tail of the DLL |
637 | * @param key - the key we want to search for | 636 | * @param key - the key we want to search for |
638 | * @param peerid - a pointer to the peer ID of the associated peer, NULL to ignore | 637 | * @param peerid - a pointer to the peer ID of the associated peer, NULL to ignore |
639 | * @return a pointer to a matching session, or NULL | 638 | * @return a pointer to a matching session, or NULL |
640 | */ | 639 | */ |
641 | static struct ServiceSession * | 640 | static struct ServiceSession * |
642 | find_matching_session (struct ServiceSession * tail, | 641 | find_matching_session (struct ServiceSession *tail, |
643 | const struct GNUNET_HashCode * key, | 642 | const struct GNUNET_HashCode *key, |
644 | const struct GNUNET_PeerIdentity * peerid) | 643 | const struct GNUNET_PeerIdentity *peerid) |
645 | { | 644 | { |
646 | struct ServiceSession * s; | 645 | struct ServiceSession * s; |
647 | 646 | ||
@@ -683,7 +682,8 @@ cb_client_disconnect (void *cls, | |||
683 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 682 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
684 | "Client %p disconnected from us.\n", | 683 | "Client %p disconnected from us.\n", |
685 | client); | 684 | client); |
686 | s = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession); | 685 | s = GNUNET_SERVER_client_get_user_context (client, |
686 | struct ServiceSession); | ||
687 | if (NULL == s) | 687 | if (NULL == s) |
688 | return; | 688 | return; |
689 | GNUNET_CONTAINER_DLL_remove (from_client_head, | 689 | GNUNET_CONTAINER_DLL_remove (from_client_head, |
@@ -717,8 +717,8 @@ cb_client_disconnect (void *cls, | |||
717 | /** | 717 | /** |
718 | * Notify the client that the session has succeeded or failed completely. | 718 | * Notify the client that the session has succeeded or failed completely. |
719 | * This message gets sent to | 719 | * This message gets sent to |
720 | * * alice's client if bob disconnected or to | 720 | * - Alice's client if Bob disconnected or to |
721 | * * bob's client if the operation completed or alice disconnected | 721 | * - Bob's client if the operation completed or Alice disconnected |
722 | * | 722 | * |
723 | * @param cls the associated client session | 723 | * @param cls the associated client session |
724 | * @param tc the task context handed to us by the scheduler, unused | 724 | * @param tc the task context handed to us by the scheduler, unused |
@@ -737,7 +737,7 @@ prepare_client_end_notification (void *cls, | |||
737 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT); | 737 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_RESULT); |
738 | // signal error if not signalized, positive result-range field but zero length. | 738 | // signal error if not signalized, positive result-range field but zero length. |
739 | msg->product_length = htonl (0); | 739 | msg->product_length = htonl (0); |
740 | msg->status = htonl(session->active); | 740 | msg->status = htonl (session->active); |
741 | session->msg = &msg->header; | 741 | session->msg = &msg->header; |
742 | 742 | ||
743 | //transmit this message to our client | 743 | //transmit this message to our client |
@@ -2674,6 +2674,8 @@ shutdown_task (void *cls, | |||
2674 | do_shutdown = GNUNET_YES; | 2674 | do_shutdown = GNUNET_YES; |
2675 | 2675 | ||
2676 | // terminate all owned open channels. | 2676 | // terminate all owned open channels. |
2677 | // FIXME: this should be unnecessary, as we should | ||
2678 | // get client disconnect events that create the same effect. | ||
2677 | for (s = from_client_head; NULL != s; s = s->next) | 2679 | for (s = from_client_head; NULL != s; s = s->next) |
2678 | { | 2680 | { |
2679 | if ((GNUNET_NO != s->active) && (NULL != s->channel)) | 2681 | if ((GNUNET_NO != s->active) && (NULL != s->channel)) |