aboutsummaryrefslogtreecommitdiff
path: root/src/dv/gnunet-service-dv.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dv/gnunet-service-dv.c')
-rw-r--r--src/dv/gnunet-service-dv.c627
1 files changed, 466 insertions, 161 deletions
diff --git a/src/dv/gnunet-service-dv.c b/src/dv/gnunet-service-dv.c
index b5fb9ba7d..696ccc165 100644
--- a/src/dv/gnunet-service-dv.c
+++ b/src/dv/gnunet-service-dv.c
@@ -27,6 +27,15 @@
27 * @author Christian Grothoff 27 * @author Christian Grothoff
28 * @author Nathan Evans 28 * @author Nathan Evans
29 * 29 *
30 * TODO: Currently the final hop of a DV message assigns a 0 to the receiver
31 * id field. This probably can't work(!) even if we know that the peer is
32 * a direct neighbor (unless we can trust that transport will choose that
33 * address for the peer). So the DV message will likely need to have the
34 * peer identity of the recipient.
35 *
36 * Also the gossip rates need to be worked out. Probably many other things
37 * as well.
38 *
30 */ 39 */
31#include "platform.h" 40#include "platform.h"
32#include "gnunet_client_lib.h" 41#include "gnunet_client_lib.h"
@@ -134,28 +143,60 @@ static size_t default_dv_priority = 0;
134 143
135 144
136/** 145/**
137 * Pending message struct, also a test to see if these can 146 * Linked list of messages to send to clients.
138 * safely ONLY be freed by callback.
139 */ 147 */
140struct PendingMessage 148struct PendingMessage
141{ 149{
142 /** 150 /**
143 * Copy of message to be sent 151 * Pointer to next item in the list
144 */ 152 */
145 struct GNUNET_MessageHeader *msg; 153 struct PendingMessage *next;
146 154
147 /** 155 /**
148 * Size of message to be sent 156 * Pointer to previous item in the list
149 */ 157 */
150 size_t msg_size; 158 struct PendingMessage *prev;
151 159
152 /** 160 /**
153 * Transmit handle, for cancellation if necessary. 161 * Actual message to be sent; // avoid allocation
154 */ 162 */
155 struct GNUNET_CORE_TransmitHandle *transmit_handle; 163 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
156 164
157}; 165};
158 166
167/**
168 * Transmit handle to the plugin.
169 */
170struct GNUNET_CONNECTION_TransmitHandle * plugin_transmit_handle;
171
172/**
173 * Head of DLL for client messages
174 */
175struct PendingMessage *plugin_pending_head;
176
177/**
178 * Tail of DLL for client messages
179 */
180struct PendingMessage *plugin_pending_tail;
181
182
183/**
184 * Transmit handle to core service.
185 */
186struct GNUNET_CORE_TransmitHandle * core_transmit_handle;
187
188/**
189 * Head of DLL for core messages
190 */
191struct PendingMessage *core_pending_head;
192
193/**
194 * Tail of DLL for core messages
195 */
196struct PendingMessage *core_pending_tail;
197
198
199
159 200
160/** 201/**
161 * Context created whenever a direct peer connects to us, 202 * Context created whenever a direct peer connects to us,
@@ -341,7 +382,7 @@ struct HelloContext
341 /** 382 /**
342 * Identity of distant neighbor. 383 * Identity of distant neighbor.
343 */ 384 */
344 struct GNUNET_PeerIdentity *distant_peer; 385 struct GNUNET_PeerIdentity distant_peer;
345 386
346 /** 387 /**
347 * Identity of direct neighbor, via which we send this message. 388 * Identity of direct neighbor, via which we send this message.
@@ -355,6 +396,41 @@ struct HelloContext
355 396
356}; 397};
357 398
399struct DV_SendContext
400{
401 /**
402 * The distant peer (should always match)
403 */
404 struct GNUNET_PeerIdentity *distant_peer;
405
406 /**
407 * The direct peer, we need to verify the referrer of.
408 */
409 struct GNUNET_PeerIdentity *direct_peer;
410
411 /**
412 * The message to be sent
413 */
414 struct GNUNET_MessageHeader *message;
415
416 /**
417 * The size of the message being sent, may be larger
418 * than message->header.size because it's multiple
419 * messages packed into one!
420 */
421 size_t message_size;
422
423 /**
424 * How important is this message?
425 */
426 unsigned int importance;
427
428 /**
429 * Timeout for this message
430 */
431 struct GNUNET_TIME_Relative timeout;
432};
433
358/** 434/**
359 * Global construct 435 * Global construct
360 */ 436 */
@@ -439,23 +515,42 @@ find_destination (void *cls,
439size_t transmit_to_plugin (void *cls, 515size_t transmit_to_plugin (void *cls,
440 size_t size, void *buf) 516 size_t size, void *buf)
441{ 517{
442 struct GNUNET_DV_MessageReceived *msg = cls; 518 char *cbuf = buf;
443 int msize; 519 struct PendingMessage *reply;
520 size_t off;
521 size_t msize;
444 522
445 if (buf == NULL) 523 if (buf == NULL)
446 return 0; 524 {
447 525 /* client disconnected */
448#if DEBUG_DV 526#if DEBUG_DV
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
450 "%s: Transmit to plugin sending message of type %d!\n", "dv", ntohs(msg->header.type)); 528#endif
529 return 0;
530 }
531 plugin_transmit_handle = NULL;
532 off = 0;
533 while ( (NULL != (reply = plugin_pending_head)) &&
534 (size >= off + (msize = ntohs (reply->msg->size))))
535 {
536#if DEBUG_DV
537 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (plugin) called with size %d\n", "dv service", msize);
451#endif 538#endif
539 GNUNET_CONTAINER_DLL_remove (plugin_pending_head,
540 plugin_pending_tail,
541 reply);
542 memcpy (&cbuf[off], reply->msg, msize);
543 GNUNET_free (reply);
544 off += msize;
545 }
452 546
453 msize = ntohs(msg->header.size); 547 if (plugin_pending_head != NULL)
454 GNUNET_assert(size >= msize); 548 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
549 ntohs(plugin_pending_head->msg->size),
550 GNUNET_TIME_UNIT_FOREVER_REL,
551 &transmit_to_plugin, NULL);
455 552
456 memcpy(buf, msg, msize); 553 return off;
457 /* FIXME: this causes crash? GNUNET_free(msg);*/
458 return msize;
459} 554}
460 555
461 556
@@ -466,6 +561,7 @@ void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
466 size_t cost) 561 size_t cost)
467{ 562{
468 struct GNUNET_DV_MessageReceived *received_msg; 563 struct GNUNET_DV_MessageReceived *received_msg;
564 struct PendingMessage *pending_message;
469#if DEBUG_DV 565#if DEBUG_DV
470 struct GNUNET_MessageHeader * packed_message_header; 566 struct GNUNET_MessageHeader * packed_message_header;
471 struct GNUNET_HELLO_Message *hello_msg; 567 struct GNUNET_HELLO_Message *hello_msg;
@@ -476,6 +572,10 @@ void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
476 char *packed_msg_start; 572 char *packed_msg_start;
477 int size; 573 int size;
478 574
575#if DEBUG_DV
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_to_plugin called with peer %s as sender\n", GNUNET_i2s(distant_neighbor));
577#endif
578
479 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0) 579 if (memcmp(sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity)) != 0)
480 { 580 {
481 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2; 581 sender_address_len = sizeof(struct GNUNET_PeerIdentity) * 2;
@@ -498,7 +598,7 @@ void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
498 received_msg->distance = htonl(cost); 598 received_msg->distance = htonl(cost);
499 received_msg->msg_len = htons(message_size); 599 received_msg->msg_len = htons(message_size);
500 /* Set the sender in this message to be the original sender! */ 600 /* Set the sender in this message to be the original sender! */
501 memcpy(&received_msg->sender, &distant_neighbor, sizeof(struct GNUNET_PeerIdentity)); 601 memcpy(&received_msg->sender, distant_neighbor, sizeof(struct GNUNET_PeerIdentity));
502 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */ 602 /* Copy the intermediate sender to the end of the message, this is how the transport identifies this peer */
503 memcpy(&received_msg[1], sender_address, sender_address_len); 603 memcpy(&received_msg[1], sender_address, sender_address_len);
504 GNUNET_free(sender_address); 604 GNUNET_free(sender_address);
@@ -517,13 +617,26 @@ void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packed HELLO message is about peer %s\n", GNUNET_i2s(&hello_identity)); 617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packed HELLO message is about peer %s\n", GNUNET_i2s(&hello_identity));
518 } 618 }
519#endif 619#endif
620 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + size);
621 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
622 memcpy(&pending_message[1], received_msg, size);
623 GNUNET_free(received_msg);
624
625 GNUNET_CONTAINER_DLL_insert_after(plugin_pending_head, plugin_pending_tail, plugin_pending_tail, pending_message);
626
520 if (client_handle != NULL) 627 if (client_handle != NULL)
521 { 628 {
522 GNUNET_SERVER_notify_transmit_ready (client_handle, 629 if (plugin_transmit_handle == NULL)
523 size, client_transmit_timeout, 630 {
524 &transmit_to_plugin, received_msg); 631 plugin_transmit_handle = GNUNET_SERVER_notify_transmit_ready (client_handle,
632 size, GNUNET_TIME_UNIT_FOREVER_REL,
633 &transmit_to_plugin, NULL);
634 }
635 else
636 {
637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to queue message for plugin, must be one in progress already!!\n");
638 }
525 } 639 }
526
527} 640}
528 641
529 642
@@ -541,27 +654,127 @@ void send_to_plugin(const struct GNUNET_PeerIdentity * sender,
541size_t core_transmit_notify (void *cls, 654size_t core_transmit_notify (void *cls,
542 size_t size, void *buf) 655 size_t size, void *buf)
543{ 656{
544 struct PendingMessage *pending_message = cls; 657 char *cbuf = buf;
545 size_t ssize; 658 struct PendingMessage *reply;
659 size_t off;
660 size_t msize;
546 661
547 if (buf == NULL) 662 if (buf == NULL)
548 { 663 {
549 /* FIXME: error handling: try again? free pending_message? */ 664 /* client disconnected */
665#if DEBUG_DV
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': buffer was NULL\n", "DHT");
667#endif
550 return 0; 668 return 0;
551 } 669 }
552 ssize = pending_message->msg_size; 670
553 GNUNET_assert(size >= ssize); 671 core_transmit_handle = NULL;
554 memcpy(buf, pending_message->msg, ssize); 672 off = 0;
555 GNUNET_free(pending_message->msg); 673 while ( (NULL != (reply = core_pending_head)) &&
556 GNUNET_free(pending_message); 674 (size >= off + (msize = ntohs (reply->msg->size))))
557 return ssize; 675 {
676#if DEBUG_DV
677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' : transmit_notify (core) called with size %d\n", "dv service", msize);
678#endif
679 GNUNET_CONTAINER_DLL_remove (core_pending_head,
680 core_pending_tail,
681 reply);
682 memcpy (&cbuf[off], reply->msg, msize);
683 GNUNET_free (reply);
684 off += msize;
685 }
686 return off;
687}
688
689
690/**
691 * Send a DV data message via DV.
692 *
693 * @param sender the original sender of the message
694 * @param specific_neighbor the specific DistantNeighbor to use, complete with referrer!
695 * @param message the packed message
696 * @param importance what priority to send this message with
697 * @param timeout how long to possibly delay sending this message
698 */
699static int
700send_message_via (const struct GNUNET_PeerIdentity * sender,
701 const struct DistantNeighbor * specific_neighbor,
702 struct DV_SendContext *send_context)
703{
704 p2p_dv_MESSAGE_Data *toSend;
705 unsigned int msg_size;
706 unsigned int cost;
707 unsigned int recipient_id;
708 unsigned int sender_id;
709 struct DistantNeighbor *source;
710 struct PendingMessage *pending_message;
711#if DEBUG_DV
712 char shortname[5];
713#endif
714
715 msg_size = send_context->message_size + sizeof (p2p_dv_MESSAGE_Data);
716
717 if (specific_neighbor == NULL)
718 {
719 /* target unknown to us, drop! */
720 return GNUNET_SYSERR;
721 }
722 recipient_id = specific_neighbor->referrer_id;
723
724 source = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
725 &sender->hashPubKey);
726 if (source == NULL)
727 {
728 if (0 != (memcmp (&my_identity,
729 sender, sizeof (struct GNUNET_PeerIdentity))))
730 {
731 /* sender unknown to us, drop! */
732 return GNUNET_SYSERR;
733 }
734 sender_id = 0; /* 0 == us */
735 }
736 else
737 {
738 /* find out the number that we use when we gossip about
739 the sender */
740 sender_id = source->our_id;
741 }
742
743 cost = specific_neighbor->cost;
744 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
745 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
746 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
747 toSend->header.size = htons (msg_size);
748 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
749 toSend->sender = htonl (sender_id);
750 toSend->recipient = htonl (recipient_id);
751 memcpy (&toSend[1], send_context->message, send_context->message_size);
752
753#if DEBUG_DV
754 memcpy(&shortname, GNUNET_i2s(&specific_neighbor->identity), 4);
755 shortname[4] = '\0';
756 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send to destination `%s' via `%s' size %u\n", "DV", &shortname, GNUNET_i2s(&specific_neighbor->referrer->identity), msg_size);
757#endif
758
759 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
760 core_pending_tail,
761 core_pending_tail,
762 pending_message);
763 if (core_transmit_handle == NULL)
764 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, send_context->importance, send_context->timeout, &specific_neighbor->referrer->identity, msg_size, &core_transmit_notify, NULL);
765 else
766 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`%s': Failed to schedule pending transmission (must be one in progress!)\n", "dv service");
767
768 return (int) cost;
558} 769}
559 770
771
560/** 772/**
561 * Send a DV data message via DV. 773 * Send a DV data message via DV.
562 * 774 *
563 * @param recipient the ultimate recipient of this message 775 * @param recipient the ultimate recipient of this message
564 * @param sender the original sender of the message 776 * @param sender the original sender of the message
777 * @param specific_neighbor the specific neighbor to send this message via
565 * @param message the packed message 778 * @param message the packed message
566 * @param importance what priority to send this message with 779 * @param importance what priority to send this message with
567 * @param timeout how long to possibly delay sending this message 780 * @param timeout how long to possibly delay sending this message
@@ -569,7 +782,9 @@ size_t core_transmit_notify (void *cls,
569static int 782static int
570send_message (const struct GNUNET_PeerIdentity * recipient, 783send_message (const struct GNUNET_PeerIdentity * recipient,
571 const struct GNUNET_PeerIdentity * sender, 784 const struct GNUNET_PeerIdentity * sender,
785 const struct DistantNeighbor * specific_neighbor,
572 const struct GNUNET_MessageHeader * message, 786 const struct GNUNET_MessageHeader * message,
787 size_t message_size,
573 unsigned int importance, struct GNUNET_TIME_Relative timeout) 788 unsigned int importance, struct GNUNET_TIME_Relative timeout)
574{ 789{
575 p2p_dv_MESSAGE_Data *toSend; 790 p2p_dv_MESSAGE_Data *toSend;
@@ -581,7 +796,7 @@ send_message (const struct GNUNET_PeerIdentity * recipient,
581 struct DistantNeighbor *source; 796 struct DistantNeighbor *source;
582 struct PendingMessage *pending_message; 797 struct PendingMessage *pending_message;
583 798
584 msg_size = ntohs (message->size) + sizeof (p2p_dv_MESSAGE_Data); 799 msg_size = message_size + sizeof (p2p_dv_MESSAGE_Data);
585 800
586 target = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors, 801 target = GNUNET_CONTAINER_multihashmap_get (ctx.extended_neighbors,
587 &recipient->hashPubKey); 802 &recipient->hashPubKey);
@@ -612,26 +827,25 @@ send_message (const struct GNUNET_PeerIdentity * recipient,
612 } 827 }
613 828
614 cost = target->cost; 829 cost = target->cost;
615 pending_message = GNUNET_malloc(sizeof(struct PendingMessage)); 830 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + msg_size);
616 pending_message->msg = GNUNET_malloc (msg_size); 831 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
617 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg; 832 toSend = (p2p_dv_MESSAGE_Data *)pending_message->msg;
618 toSend->header.size = htons (msg_size); 833 toSend->header.size = htons (msg_size);
619 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA); 834 toSend->header.type = htons (GNUNET_MESSAGE_TYPE_DV_DATA);
620 toSend->sender = htonl (sender_id); 835 toSend->sender = htonl (sender_id);
621 toSend->recipient = htonl (recipient_id); 836 toSend->recipient = htonl (recipient_id);
622 memcpy (&toSend[1], message, ntohs (message->size)); 837 memcpy (&toSend[1], message, message_size);
623 pending_message->msg_size = msg_size;
624 838
625 pending_message->transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, pending_message); 839 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
626 if (NULL == pending_message->transmit_handle) 840 core_pending_tail,
627 { 841 core_pending_tail,
628 GNUNET_free (pending_message->msg); 842 pending_message);
629 GNUNET_free (pending_message); 843#if DEBUG_DV
630 return GNUNET_SYSERR; 844 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s: Notifying core of send size %d to destination `%s'\n", "DV SEND MESSAGE", msg_size, GNUNET_i2s(recipient));
631 } 845#endif
846 if (core_transmit_handle == NULL)
847 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, importance, timeout, &target->referrer->identity, msg_size, &core_transmit_notify, NULL);
632 848
633 /*coreAPI->ciphertext_send (&target->referrer->identity,
634 &toSend->header, importance, maxdelay);*/
635 return (int) cost; 849 return (int) cost;
636} 850}
637 851
@@ -654,13 +868,8 @@ static int handle_dv_data_message (void *cls,
654 struct GNUNET_TIME_Relative latency, 868 struct GNUNET_TIME_Relative latency,
655 uint32_t distance) 869 uint32_t distance)
656{ 870{
657#if DEBUG_DV
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "%s: Receives %s message!\n", "dv", "DV DATA");
660#endif
661
662 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message; 871 const p2p_dv_MESSAGE_Data *incoming = (const p2p_dv_MESSAGE_Data *) message;
663 const struct GNUNET_MessageHeader *packed_message = (const struct GNUNET_MessageHeader *) &incoming[1]; 872 const struct GNUNET_MessageHeader *packed_message;
664 struct DirectNeighbor *dn; 873 struct DirectNeighbor *dn;
665 struct DistantNeighbor *pos; 874 struct DistantNeighbor *pos;
666 unsigned int sid; /* Sender id */ 875 unsigned int sid; /* Sender id */
@@ -669,12 +878,22 @@ static int handle_dv_data_message (void *cls,
669 struct GNUNET_PeerIdentity destination; 878 struct GNUNET_PeerIdentity destination;
670 struct FindDestinationContext fdc; 879 struct FindDestinationContext fdc;
671 int ret; 880 int ret;
881 size_t packed_message_size;
882 char *cbuf;
883 size_t offset;
884
885 packed_message_size = ntohs(incoming->header.size) - sizeof(p2p_dv_MESSAGE_Data);
672 886
673 if ((ntohs (incoming->header.size) < 887#if DEBUG_DV
674 sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader)) 888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 || (ntohs (incoming->header.size) != 889 "%s: Receives %s message size %d, packed message size %d!\n", "dv", "DV DATA", ntohs(incoming->header.size), packed_message_size);
676 (sizeof (p2p_dv_MESSAGE_Data) + ntohs (packed_message->size)))) 890#endif
891 if (ntohs (incoming->header.size) < sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader))
677 { 892 {
893#if DEBUG_DV
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "`%s': Message sizes don't add up, total size %u, expected at least %u!\n", "dv service", ntohs(incoming->header.size), sizeof (p2p_dv_MESSAGE_Data) + sizeof (struct GNUNET_MessageHeader));
896#endif
678 return GNUNET_SYSERR; 897 return GNUNET_SYSERR;
679 } 898 }
680 899
@@ -682,6 +901,10 @@ static int handle_dv_data_message (void *cls,
682 &peer->hashPubKey); 901 &peer->hashPubKey);
683 if (dn == NULL) 902 if (dn == NULL)
684 { 903 {
904#if DEBUG_DV
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "%s: dn NULL!\n", "dv");
907#endif
685 return GNUNET_OK; 908 return GNUNET_OK;
686 } 909 }
687 sid = ntohl (incoming->sender); 910 sid = ntohl (incoming->sender);
@@ -690,6 +913,10 @@ static int handle_dv_data_message (void *cls,
690 pos = pos->next; 913 pos = pos->next;
691 if (pos == NULL) 914 if (pos == NULL)
692 { 915 {
916#if DEBUG_DV
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "%s: unknown sender (%d), size of extended_peers is %d!\n", "dv", ntohl(incoming->sender), GNUNET_CONTAINER_multihashmap_size (ctx.extended_neighbors));
919#endif
693 /* unknown sender */ 920 /* unknown sender */
694 return GNUNET_OK; 921 return GNUNET_OK;
695 } 922 }
@@ -699,19 +926,31 @@ static int handle_dv_data_message (void *cls,
699 { 926 {
700 /* 0 == us */ 927 /* 0 == us */
701 928
702 /* FIXME: Will we support wrapped messages being these types? Probably not, they should 929 cbuf = (char *)&incoming[1];
703 * be encrypted messages that need decrypting and junk like that. 930 offset = 0;
704 */ 931 while(offset < packed_message_size)
705 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP); 932 {
706 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA); 933 packed_message = (struct GNUNET_MessageHeader *)&cbuf[offset];
707 if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) && 934#if DEBUG_DV
708 (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) ) 935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 { 936 "%s: Receives %s message for me, size %d type %d!\n", "dv", "DV DATA", ntohs(packed_message->size), ntohs(packed_message->type));
710 send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost); 937#endif
711 } 938 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP);
939 GNUNET_break_op (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA);
940 if ( (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_GOSSIP) &&
941 (ntohs (packed_message->type) != GNUNET_MESSAGE_TYPE_DV_DATA) )
942 {
943 send_to_plugin(peer, packed_message, ntohs(packed_message->size), &pos->identity, pos->cost);
944 }
945 offset += ntohs(packed_message->size);
946 }
712 947
713 return GNUNET_OK; 948 return GNUNET_OK;
714 } 949 }
950 else
951 {
952 packed_message = (struct GNUNET_MessageHeader *)&incoming[1];
953 }
715 954
716 /* FIXME: this is the *only* per-request operation we have in DV 955 /* FIXME: this is the *only* per-request operation we have in DV
717 that is O(n) in relation to the number of connected peers; a 956 that is O(n) in relation to the number of connected peers; a
@@ -721,6 +960,12 @@ static int handle_dv_data_message (void *cls,
721 fdc.dest = NULL; 960 fdc.dest = NULL;
722 GNUNET_CONTAINER_heap_iterate (ctx.neighbor_max_heap, 961 GNUNET_CONTAINER_heap_iterate (ctx.neighbor_max_heap,
723 &find_destination, &fdc); 962 &find_destination, &fdc);
963
964#if DEBUG_DV
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "%s: Receives %s message for someone else!\n", "dv", "DV DATA");
967#endif
968
724 if (fdc.dest == NULL) 969 if (fdc.dest == NULL)
725 { 970 {
726 return GNUNET_OK; 971 return GNUNET_OK;
@@ -730,6 +975,9 @@ static int handle_dv_data_message (void *cls,
730 if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity))) 975 if (0 == memcmp (&destination, peer, sizeof (struct GNUNET_PeerIdentity)))
731 { 976 {
732 /* FIXME: create stat: routing loop-discard! */ 977 /* FIXME: create stat: routing loop-discard! */
978#if DEBUG_DV
979 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\nLoopy loo message\n\n\n");
980#endif
733 return GNUNET_OK; 981 return GNUNET_OK;
734 } 982 }
735 983
@@ -742,7 +990,11 @@ static int handle_dv_data_message (void *cls,
742 /*ret = send_message (&destination, 990 /*ret = send_message (&destination,
743 &original_sender, 991 &original_sender,
744 packed_message, DV_PRIORITY, DV_DELAY);*/ 992 packed_message, DV_PRIORITY, DV_DELAY);*/
745 ret = send_message(&destination, &original_sender, packed_message, default_dv_priority, default_dv_delay); 993#if DEBUG_DV
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "%s: Sends message size %d on!\n", "dv", packed_message_size);
996#endif
997 ret = send_message(&destination, &original_sender, NULL, packed_message, packed_message_size, default_dv_priority, default_dv_delay);
746 998
747 if (ret != GNUNET_SYSERR) 999 if (ret != GNUNET_SYSERR)
748 return GNUNET_OK; 1000 return GNUNET_OK;
@@ -761,13 +1013,12 @@ neighbor_send_task (void *cls,
761 const struct GNUNET_SCHEDULER_TaskContext *tc) 1013 const struct GNUNET_SCHEDULER_TaskContext *tc)
762{ 1014{
763 struct NeighborSendContext *send_context = cls; 1015 struct NeighborSendContext *send_context = cls;
764#if DEBUG_DV 1016#if DEBUG_DV_GOSSIP
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "%s: Entering neighbor_send_task...\n", 1018 "%s: Entering neighbor_send_task...\n",
767 GNUNET_i2s(&my_identity)); 1019 GNUNET_i2s(&my_identity));
768 char * encPeerAbout; 1020 char * encPeerAbout;
769 char * encPeerTo; 1021 char * encPeerTo;
770 struct GNUNET_PeerIdentity about_peer_id;
771#endif 1022#endif
772 struct DistantNeighbor *about; 1023 struct DistantNeighbor *about;
773 struct DirectNeighbor *to; 1024 struct DirectNeighbor *to;
@@ -777,7 +1028,7 @@ neighbor_send_task (void *cls,
777 1028
778 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) 1029 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
779 { 1030 {
780#if DEBUG_DV 1031#if DEBUG_DV_GOSSIP
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "%s: Called with reason shutdown, shutting down!\n", 1033 "%s: Called with reason shutdown, shutting down!\n",
783 GNUNET_i2s(&my_identity)); 1034 GNUNET_i2s(&my_identity));
@@ -810,7 +1061,7 @@ neighbor_send_task (void *cls,
810 &to->identity, sizeof (struct GNUNET_PeerIdentity))) && 1061 &to->identity, sizeof (struct GNUNET_PeerIdentity))) &&
811 (about->pkey != NULL)) 1062 (about->pkey != NULL))
812 { 1063 {
813#if DEBUG_DV 1064#if DEBUG_DV_GOSSIP
814 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity)); 1065 encPeerAbout = GNUNET_strdup(GNUNET_i2s(&about->identity));
815 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity)); 1066 encPeerTo = GNUNET_strdup(GNUNET_i2s(&to->identity));
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -820,8 +1071,8 @@ neighbor_send_task (void *cls,
820 GNUNET_free(encPeerAbout); 1071 GNUNET_free(encPeerAbout);
821 GNUNET_free(encPeerTo); 1072 GNUNET_free(encPeerTo);
822#endif 1073#endif
823 pending_message = GNUNET_malloc(sizeof(struct PendingMessage)); 1074 pending_message = GNUNET_malloc(sizeof(struct PendingMessage) + sizeof(p2p_dv_MESSAGE_NeighborInfo));
824 pending_message->msg = GNUNET_malloc(sizeof(p2p_dv_MESSAGE_NeighborInfo)); 1075 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1];
825 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg; 1076 message = (p2p_dv_MESSAGE_NeighborInfo *)pending_message->msg;
826 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo)); 1077 message->header.size = htons (sizeof (p2p_dv_MESSAGE_NeighborInfo));
827 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP); 1078 message->header.type = htons (GNUNET_MESSAGE_TYPE_DV_GOSSIP);
@@ -832,26 +1083,14 @@ neighbor_send_task (void *cls,
832 memcpy (&message->neighbor, 1083 memcpy (&message->neighbor,
833 &about->identity, sizeof (struct GNUNET_PeerIdentity)); 1084 &about->identity, sizeof (struct GNUNET_PeerIdentity));
834 1085
835#if DEBUG_DV 1086 GNUNET_CONTAINER_DLL_insert_after (core_pending_head,
836 GNUNET_CRYPTO_hash (about->pkey, 1087 core_pending_tail,
837 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 1088 core_pending_tail,
838 &about_peer_id.hashPubKey); 1089 pending_message);
839 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer id from message %s\n", GNUNET_i2s(&about->identity));
840 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer id from pubkey %s\n", GNUNET_i2s(&about_peer_id));
841#endif
842 1090
843 pending_message->msg_size = sizeof(p2p_dv_MESSAGE_NeighborInfo); 1091 if (core_transmit_handle == NULL)
844 pending_message->transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, default_dv_priority, default_dv_delay, &to->identity, sizeof(p2p_dv_MESSAGE_NeighborInfo), &core_transmit_notify, pending_message); 1092 core_transmit_handle = GNUNET_CORE_notify_transmit_ready(coreAPI, default_dv_priority, default_dv_delay, &to->identity, sizeof(p2p_dv_MESSAGE_NeighborInfo), &core_transmit_notify, NULL);
845 1093
846 if (NULL == pending_message->transmit_handle)
847 {
848 GNUNET_free (pending_message->msg);
849 GNUNET_free (pending_message);
850 return;
851 }
852 /*coreAPI->ciphertext_send (&to->identity, &message.header,
853 GNUNET_DV_DHT_GOSSIP_PRIORITY,
854 ctx.send_interval);*/
855 } 1094 }
856 1095
857 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, send_context->timeout, &neighbor_send_task, send_context); 1096 send_context->task = GNUNET_SCHEDULER_add_delayed(sched, send_context->timeout, &neighbor_send_task, send_context);
@@ -886,6 +1125,32 @@ handle_start (void *cls,
886 1125
887 1126
888/** 1127/**
1128 * Iterate over hash map entries for a distant neighbor,
1129 * if direct neighbor matches context call send message
1130 *
1131 * @param cls closure, a DV_SendContext
1132 * @param key current key code
1133 * @param value value in the hash map
1134 * @return GNUNET_YES if we should continue to
1135 * iterate,
1136 * GNUNET_NO if not.
1137 */
1138int send_iterator (void *cls,
1139 const GNUNET_HashCode * key,
1140 void *value)
1141{
1142 struct DV_SendContext *send_context = cls;
1143 struct DistantNeighbor *distant_neighbor = value;
1144
1145 if (memcmp(distant_neighbor->referrer, send_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)) == 0) /* They match, send and free */
1146 {
1147 send_message_via(&my_identity, distant_neighbor, send_context);
1148 return GNUNET_NO;
1149 }
1150 return GNUNET_YES;
1151}
1152
1153/**
889 * Service server's handler for message send requests (which come 1154 * Service server's handler for message send requests (which come
890 * bubbling up to us through the DV plugin). 1155 * bubbling up to us through the DV plugin).
891 * 1156 *
@@ -900,10 +1165,14 @@ void handle_dv_send_message (void *cls,
900 struct GNUNET_DV_SendMessage *send_msg; 1165 struct GNUNET_DV_SendMessage *send_msg;
901 size_t address_len; 1166 size_t address_len;
902 size_t message_size; 1167 size_t message_size;
903#if DEBUG_DV 1168 struct GNUNET_PeerIdentity *destination;
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1169 struct GNUNET_PeerIdentity *direct;
905 "%s: Receives %s message!\n\n\n", "dv", "SEND"); 1170 struct GNUNET_MessageHeader *message_buf;
906#endif 1171 char *temp_pos;
1172 int offset;
1173 static struct GNUNET_CRYPTO_HashAsciiEncoded dest_hash;
1174 struct DV_SendContext *send_context;
1175
907 if (client_handle == NULL) 1176 if (client_handle == NULL)
908 { 1177 {
909 client_handle = client; 1178 client_handle = client;
@@ -920,28 +1189,63 @@ void handle_dv_send_message (void *cls,
920 1189
921 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage)); 1190 GNUNET_assert(ntohs(message->size) > sizeof(struct GNUNET_DV_SendMessage));
922 send_msg = (struct GNUNET_DV_SendMessage *)message; 1191 send_msg = (struct GNUNET_DV_SendMessage *)message;
1192
923 address_len = ntohs(send_msg->addrlen); 1193 address_len = ntohs(send_msg->addrlen);
1194 GNUNET_assert(address_len == sizeof(struct GNUNET_PeerIdentity) * 2);
924 message_size = ntohs(send_msg->msgbuf_size); 1195 message_size = ntohs(send_msg->msgbuf_size);
1196
1197#if DEBUG_DV
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "%s: Receives %s message size %u!\n\n\n", "dv", "SEND", message_size);
1200#endif
925 GNUNET_assert(ntohs(message->size) == sizeof(struct GNUNET_DV_SendMessage) + address_len + message_size); 1201 GNUNET_assert(ntohs(message->size) == sizeof(struct GNUNET_DV_SendMessage) + address_len + message_size);
1202 destination = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1203 direct = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
1204 message_buf = GNUNET_malloc(message_size);
1205
1206 temp_pos = (char *)&send_msg[1]; /* Set pointer to end of message */
1207 offset = 0; /* Offset starts at zero */
1208
1209 memcpy(destination, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1210 offset += sizeof(struct GNUNET_PeerIdentity);
1211
1212 memcpy(direct, &temp_pos[offset], sizeof(struct GNUNET_PeerIdentity));
1213 offset += sizeof(struct GNUNET_PeerIdentity);
1214
1215
1216 memcpy(message_buf, &temp_pos[offset], message_size);
1217 if (memcmp(&send_msg->target, destination, sizeof(struct GNUNET_PeerIdentity)) != 0)
1218 {
1219 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1220 dest_hash.encoding[4] = '\0';
1221 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s: asked to send message to `%s', but address is for `%s'!", "DV SERVICE", GNUNET_i2s(&send_msg->target), (const char *)&dest_hash.encoding);
1222 }
926 1223
927#if DEBUG_DV 1224#if DEBUG_DV
928 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SEND called with message of size %d, address size %d\n", message_size, address_len); 1225 GNUNET_CRYPTO_hash_to_enc (&destination->hashPubKey, &dest_hash); /* GNUNET_i2s won't properly work, need to hash one ourselves */
1226 dest_hash.encoding[4] = '\0';
1227 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SEND called with message of size %d type %d, destination `%s' via `%s'\n", message_size, ntohs(message_buf->type), (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
929#endif 1228#endif
930/* How this was created, opposite to get data */ 1229 send_context = GNUNET_malloc(sizeof(struct DV_SendContext));
931 /* 1230
932 msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size); 1231 send_context->importance = ntohs(send_msg->priority);
933 msg->header.size = htons(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size); 1232 send_context->timeout = send_msg->timeout;
934 msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND); 1233 send_context->direct_peer = direct;
935 memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity)); 1234 send_context->distant_peer = destination;
936 msg->msgbuf_size = htons(msgbuf_size); 1235 send_context->message = message_buf;
937 msg->priority = htonl(priority); 1236 send_context->message_size = message_size;
938 msg->timeout = timeout; 1237
939 msg->addrlen = htons(addrlen); 1238 /* In bizarro world GNUNET_SYSERR indicates that we succeeded */
940 memcpy(&msg[1], addr, addrlen); 1239 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors, &destination->hashPubKey, &send_iterator, send_context))
941 end_of_message = (char *)&msg[1]; 1240 {
942 end_of_message = &end_of_message[addrlen]; 1241 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "DV SEND failed to send message to destination `%s' via `%s'\n", (const char *)&dest_hash.encoding, GNUNET_i2s(direct));
943 memcpy(end_of_message, msgbuf, msgbuf_size); 1242 }
944 */ 1243
1244 GNUNET_free(message_buf);
1245 GNUNET_free(send_context);
1246 GNUNET_free(direct);
1247 GNUNET_free(destination);
1248
945 GNUNET_SERVER_receive_done(client, GNUNET_OK); 1249 GNUNET_SERVER_receive_done(client, GNUNET_OK);
946} 1250}
947 1251
@@ -967,24 +1271,9 @@ static struct GNUNET_CORE_MessageHandler core_handlers[] = {
967 {NULL, 0, 0} 1271 {NULL, 0, 0}
968}; 1272};
969 1273
970
971#if DEBUG_DV
972static void handle_any(void *cls,
973 struct GNUNET_SERVER_Client * client,
974 const struct GNUNET_MessageHeader * message)
975{
976 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "\n\n\n\n\n\nDV service receives message of type %d, size %d\n\n\n\n\n\n", ntohs(message->type), ntohs(message->size));
977 GNUNET_SERVER_receive_done (client, GNUNET_OK);
978}
979#endif
980
981
982static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = { 1274static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
983 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0}, 1275 {&handle_dv_send_message, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND, 0},
984 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0}, 1276 {&handle_start, NULL, GNUNET_MESSAGE_TYPE_DV_START, 0},
985#if DEBUG_DV
986 {&handle_any, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
987#endif
988 {NULL, NULL, 0, 0} 1277 {NULL, NULL, 0, 0}
989}; 1278};
990 1279
@@ -999,7 +1288,13 @@ static void
999shutdown_task (void *cls, 1288shutdown_task (void *cls,
1000 const struct GNUNET_SCHEDULER_TaskContext *tc) 1289 const struct GNUNET_SCHEDULER_TaskContext *tc)
1001{ 1290{
1291#if DEBUG_DV
1292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "calling CORE_DISCONNECT\n");
1293#endif
1002 GNUNET_CORE_disconnect (coreAPI); 1294 GNUNET_CORE_disconnect (coreAPI);
1295#if DEBUG_DV
1296 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CORE_DISCONNECT completed\n");
1297#endif
1003} 1298}
1004 1299
1005/** 1300/**
@@ -1041,22 +1336,11 @@ static int add_pkey_to_extended (void *cls,
1041{ 1336{
1042 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls; 1337 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pkey = cls;
1043 struct DistantNeighbor *distant_neighbor = value; 1338 struct DistantNeighbor *distant_neighbor = value;
1044#if DEBUG_DV
1045 struct GNUNET_PeerIdentity about_peer_id;
1046#endif
1047 1339
1048 if (distant_neighbor->pkey == NULL) 1340 if (distant_neighbor->pkey == NULL)
1049 { 1341 {
1050 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)); 1342 distant_neighbor->pkey = GNUNET_malloc(sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1051 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)); 1343 memcpy(distant_neighbor->pkey, pkey, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1052#if DEBUG_DV
1053 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Updating pkey for extended list for peer %s\n", GNUNET_i2s(&distant_neighbor->identity));
1054 GNUNET_CRYPTO_hash (distant_neighbor->pkey,
1055 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1056 &about_peer_id.hashPubKey);
1057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer id from setup %s\n", GNUNET_i2s(&distant_neighbor->identity));
1058 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer id from pkey %s\n", GNUNET_i2s(&about_peer_id));
1059#endif
1060 } 1344 }
1061 1345
1062 return GNUNET_YES; 1346 return GNUNET_YES;
@@ -1118,6 +1402,35 @@ distant_neighbor_free (struct DistantNeighbor *referee)
1118} 1402}
1119 1403
1120 1404
1405#if DEBUG_DV_GOSSIP
1406/**
1407 * Iterator over hash map entries.
1408 *
1409 * @param cls closure (NULL)
1410 * @param key current key code
1411 * @param value value in the hash map (DistantNeighbor)
1412 * @return GNUNET_YES if we should continue to
1413 * iterate,
1414 * GNUNET_NO if not.
1415 */
1416int print_neighbors (void *cls,
1417 const GNUNET_HashCode * key,
1418 void *value)
1419{
1420 struct DistantNeighbor *distant_neighbor = value;
1421 char my_shortname[5];
1422 char referrer_shortname[5];
1423 memcpy(&my_shortname, GNUNET_i2s(&my_identity), 4);
1424 my_shortname[4] = '\0';
1425 memcpy(&referrer_shortname, GNUNET_i2s(&distant_neighbor->referrer->identity), 4);
1426 referrer_shortname[4] = '\0';
1427
1428 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "`%s' %s: Peer `%s', distance %d, referrer `%s'\n", &my_shortname, "DV", GNUNET_i2s(&distant_neighbor->identity), distant_neighbor->cost, &referrer_shortname);
1429 return GNUNET_YES;
1430}
1431
1432#endif
1433
1121/** 1434/**
1122 * Handles when a peer is either added due to being newly connected 1435 * Handles when a peer is either added due to being newly connected
1123 * or having been gossiped about, also called when the cost for a neighbor 1436 * or having been gossiped about, also called when the cost for a neighbor
@@ -1215,18 +1528,21 @@ addUpdateNeighbor (const struct GNUNET_PeerIdentity * peer, struct GNUNET_CRYPTO
1215 } 1528 }
1216 else 1529 else
1217 { 1530 {
1218#if DEBUG_DV 1531#if DEBUG_DV_GOSSIP
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id); 1533 "%s: Already know peer %s distance %d, referrer id %d!\n", "dv", GNUNET_i2s(peer), cost, referrer_peer_id);
1221#endif 1534#endif
1222 } 1535 }
1223#if DEBUG_DV 1536#if DEBUG_DV_GOSSIP
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(ctx.extended_neighbors)); 1538 "%s: Size of extended_neighbors is %d\n", "dv", GNUNET_CONTAINER_multihashmap_size(ctx.extended_neighbors));
1539 GNUNET_CONTAINER_multihashmap_iterate(ctx.extended_neighbors, &print_neighbors, NULL);
1226#endif 1540#endif
1227 GNUNET_free(neighbor_update); 1541 GNUNET_free(neighbor_update);
1228 /* Old logic to remove entry and replace, not needed now as we only want to remove when full 1542 /* Old logic to remove entry and replace, not needed now as we only want to remove when full
1229 * or when the referring peer disconnects from us. 1543 * or when the referring peer disconnects from us.
1544 *
1545 * FIXME: add new functionality, or check if it already exists (i forget)
1230 */ 1546 */
1231 /* 1547 /*
1232 GNUNET_DLL_remove (neighbor->referrer->referee_head, 1548 GNUNET_DLL_remove (neighbor->referrer->referee_head,
@@ -1264,7 +1580,7 @@ generate_hello_address (void *cls, size_t max, void *buf)
1264 addr_buffer = GNUNET_malloc(size); 1580 addr_buffer = GNUNET_malloc(size);
1265 offset = 0; 1581 offset = 0;
1266 /* Copy the distant peer identity to buffer */ 1582 /* Copy the distant peer identity to buffer */
1267 memcpy(addr_buffer, hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity)); 1583 memcpy(addr_buffer, &hello_context->distant_peer, sizeof(struct GNUNET_PeerIdentity));
1268 offset += sizeof(struct GNUNET_PeerIdentity); 1584 offset += sizeof(struct GNUNET_PeerIdentity);
1269 /* Copy the direct peer identity to buffer */ 1585 /* Copy the direct peer identity to buffer */
1270 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity)); 1586 memcpy(&addr_buffer[offset], hello_context->direct_peer, sizeof(struct GNUNET_PeerIdentity));
@@ -1275,6 +1591,7 @@ generate_hello_address (void *cls, size_t max, void *buf)
1275 1591
1276 hello_context->addresses_to_add--; 1592 hello_context->addresses_to_add--;
1277 1593
1594 GNUNET_free(addr_buffer);
1278 return ret; 1595 return ret;
1279} 1596}
1280 1597
@@ -1309,7 +1626,7 @@ static int handle_dv_gossip_message (void *cls,
1309 return GNUNET_SYSERR; /* invalid message */ 1626 return GNUNET_SYSERR; /* invalid message */
1310 } 1627 }
1311 1628
1312#if DEBUG_DV 1629#if DEBUG_DV_GOSSIP
1313 char * encPeerAbout; 1630 char * encPeerAbout;
1314 char * encPeerFrom; 1631 char * encPeerFrom;
1315 1632
@@ -1332,17 +1649,18 @@ static int handle_dv_gossip_message (void *cls,
1332 1649
1333 hello_context = GNUNET_malloc(sizeof(struct HelloContext)); 1650 hello_context = GNUNET_malloc(sizeof(struct HelloContext));
1334 hello_context->direct_peer = peer; 1651 hello_context->direct_peer = peer;
1335 hello_context->distant_peer = &enc_message->neighbor; 1652 memcpy(&hello_context->distant_peer, &enc_message->neighbor, sizeof(struct GNUNET_PeerIdentity));
1336 hello_context->addresses_to_add = 1; 1653 hello_context->addresses_to_add = 1;
1337 hello_msg = GNUNET_HELLO_create(&enc_message->pkey, &generate_hello_address, hello_context); 1654 hello_msg = GNUNET_HELLO_create(&enc_message->pkey, &generate_hello_address, hello_context);
1338
1339 hello_hdr = GNUNET_HELLO_get_header(hello_msg); 1655 hello_hdr = GNUNET_HELLO_get_header(hello_msg);
1340#if DEBUG_DV 1656#if DEBUG_DV_GOSSIP
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "%s: Sending %s message to plugin, type is %d, size %d!\n", "dv", "HELLO", ntohs(hello_hdr->type), ntohs(hello_hdr->size)); 1658 "%s: Sending %s message to plugin, type is %d, size %d!\n", "dv", "HELLO", ntohs(hello_hdr->type), ntohs(hello_hdr->size));
1343#endif 1659#endif
1344 1660
1345 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), hello_context->distant_peer, ntohl(enc_message->cost) + 1); 1661 send_to_plugin(hello_context->direct_peer, GNUNET_HELLO_get_header(hello_msg), GNUNET_HELLO_size(hello_msg), &hello_context->distant_peer, ntohl(enc_message->cost) + 1);
1662 GNUNET_free(hello_context);
1663 GNUNET_free(hello_msg);
1346 return GNUNET_OK; 1664 return GNUNET_OK;
1347} 1665}
1348 1666
@@ -1354,15 +1672,10 @@ process_peerinfo (void *cls,
1354 struct PeerIteratorContext *peerinfo_iterator = cls; 1672 struct PeerIteratorContext *peerinfo_iterator = cls;
1355 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor; 1673 struct DirectNeighbor *neighbor = peerinfo_iterator->neighbor;
1356 1674
1357#if DEBUG_DV
1358 struct GNUNET_PeerIdentity about_peer_id;
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "%s: process_peerinfo called!\n", "dv");
1361#endif
1362
1363 if ((peer == NULL))/* && (neighbor->pkey == NULL))*/ 1675 if ((peer == NULL))/* && (neighbor->pkey == NULL))*/
1364 { 1676 {
1365 /* FIXME: Remove peer! */ 1677 /* FIXME: Remove peer! */
1678 GNUNET_free(peerinfo_iterator);
1366 return; 1679 return;
1367 } 1680 }
1368 1681
@@ -1371,14 +1684,6 @@ process_peerinfo (void *cls,
1371 1684
1372 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK)) 1685 if ((hello != NULL) && (GNUNET_HELLO_get_key (hello, &neighbor->pkey) == GNUNET_OK))
1373 { 1686 {
1374#if DEBUG_DV
1375 GNUNET_CRYPTO_hash (&neighbor->pkey,
1376 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1377 &about_peer_id.hashPubKey);
1378 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "(peerinfo) Peer id from setup %s\n", GNUNET_i2s(&neighbor->identity));
1379 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "(peerinfo) Peer id from hello %s\n", GNUNET_i2s(&about_peer_id));
1380#endif
1381
1382 GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors, 1687 GNUNET_CONTAINER_multihashmap_get_multiple(ctx.extended_neighbors,
1383 &peer->hashPubKey, 1688 &peer->hashPubKey,
1384 &add_pkey_to_extended, 1689 &add_pkey_to_extended,