aboutsummaryrefslogtreecommitdiff
path: root/src/dht/gnunet-service-dht.c
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2010-12-15 11:10:12 +0000
committerNathan S. Evans <evans@in.tum.de>2010-12-15 11:10:12 +0000
commit47fb383ed46ccd2724cad225c99b692e16a0d400 (patch)
tree03752aaa8fffde94f941ac19b2403b69a7ea5939 /src/dht/gnunet-service-dht.c
parenta81781346c7ef4698fe763b35ac3ce2564294504 (diff)
downloadgnunet-47fb383ed46ccd2724cad225c99b692e16a0d400.tar.gz
gnunet-47fb383ed46ccd2724cad225c99b692e16a0d400.zip
different indentation, somewhat cleaner
Diffstat (limited to 'src/dht/gnunet-service-dht.c')
-rw-r--r--src/dht/gnunet-service-dht.c2602
1 files changed, 1472 insertions, 1130 deletions
diff --git a/src/dht/gnunet-service-dht.c b/src/dht/gnunet-service-dht.c
index df7c28024..02ce5ce14 100644
--- a/src/dht/gnunet-service-dht.c
+++ b/src/dht/gnunet-service-dht.c
@@ -149,12 +149,12 @@
149/* 149/*
150 * Default frequency for sending malicious get messages 150 * Default frequency for sending malicious get messages
151 */ 151 */
152#define DEFAULT_MALICIOUS_GET_FREQUENCY 1000 /* Number of milliseconds */ 152#define DEFAULT_MALICIOUS_GET_FREQUENCY 1000 /* Number of milliseconds */
153 153
154/* 154/*
155 * Default frequency for sending malicious put messages 155 * Default frequency for sending malicious put messages
156 */ 156 */
157#define DEFAULT_MALICIOUS_PUT_FREQUENCY 1000 /* Default is in milliseconds */ 157#define DEFAULT_MALICIOUS_PUT_FREQUENCY 1000 /* Default is in milliseconds */
158 158
159 159
160#define DHT_DEFAULT_PING_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1) 160#define DHT_DEFAULT_PING_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1)
@@ -176,32 +176,32 @@ enum ConvergenceOptions
176 /** 176 /**
177 * Use the linear method for convergence. 177 * Use the linear method for convergence.
178 */ 178 */
179 DHT_CONVERGE_LINEAR, 179 DHT_CONVERGE_LINEAR,
180 180
181 /** 181 /**
182 * Converge using a fast converging square 182 * Converge using a fast converging square
183 * function. 183 * function.
184 */ 184 */
185 DHT_CONVERGE_SQUARE, 185 DHT_CONVERGE_SQUARE,
186 186
187 /** 187 /**
188 * Converge using a slower exponential 188 * Converge using a slower exponential
189 * function. 189 * function.
190 */ 190 */
191 DHT_CONVERGE_EXPONENTIAL, 191 DHT_CONVERGE_EXPONENTIAL,
192 192
193 /** 193 /**
194 * Don't do any special convergence, allow 194 * Don't do any special convergence, allow
195 * the algorithm to hopefully route to closer 195 * the algorithm to hopefully route to closer
196 * peers more often. 196 * peers more often.
197 */ 197 */
198 DHT_CONVERGE_RANDOM, 198 DHT_CONVERGE_RANDOM,
199 199
200 /** 200 /**
201 * Binary convergence, start routing to closest 201 * Binary convergence, start routing to closest
202 * only after set number of hops. 202 * only after set number of hops.
203 */ 203 */
204 DHT_CONVERGE_BINARY 204 DHT_CONVERGE_BINARY
205}; 205};
206 206
207/** 207/**
@@ -237,7 +237,7 @@ struct P2PPendingMessage
237 /** 237 /**
238 * Actual message to be sent; // avoid allocation 238 * Actual message to be sent; // avoid allocation
239 */ 239 */
240 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len); 240 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
241 241
242}; 242};
243 243
@@ -363,7 +363,7 @@ struct PendingMessage
363 /** 363 /**
364 * Actual message to be sent; // avoid allocation 364 * Actual message to be sent; // avoid allocation
365 */ 365 */
366 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len); 366 const struct GNUNET_MessageHeader *msg; // msg = (cast) &pm[1]; // memcpy (&pm[1], data, len);
367 367
368}; 368};
369 369
@@ -798,7 +798,7 @@ static GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
798/** 798/**
799 * The lowest currently used bucket. 799 * The lowest currently used bucket.
800 */ 800 */
801static unsigned int lowest_bucket; /* Initially equal to MAX_BUCKETS - 1 */ 801static unsigned int lowest_bucket; /* Initially equal to MAX_BUCKETS - 1 */
802 802
803/** 803/**
804 * The maximum number of hops before we stop routing messages. 804 * The maximum number of hops before we stop routing messages.
@@ -819,7 +819,7 @@ static int use_max_hops;
819 * The buckets (Kademlia routing table, complete with growth). 819 * The buckets (Kademlia routing table, complete with growth).
820 * Array of size MAX_BUCKET_SIZE. 820 * Array of size MAX_BUCKET_SIZE.
821 */ 821 */
822static struct PeerBucket k_buckets[MAX_BUCKETS]; /* From 0 to MAX_BUCKETS - 1 */ 822static struct PeerBucket k_buckets[MAX_BUCKETS]; /* From 0 to MAX_BUCKETS - 1 */
823 823
824/** 824/**
825 * Hash map of all known peers, for easy removal from k_buckets on disconnect. 825 * Hash map of all known peers, for easy removal from k_buckets on disconnect.
@@ -834,7 +834,7 @@ static struct GNUNET_CONTAINER_MultiHashMap *recent_find_peer_requests;
834/** 834/**
835 * Maximum size for each bucket. 835 * Maximum size for each bucket.
836 */ 836 */
837static unsigned int bucket_size = DEFAULT_BUCKET_SIZE; /* Initially equal to DEFAULT_BUCKET_SIZE */ 837static unsigned int bucket_size = DEFAULT_BUCKET_SIZE; /* Initially equal to DEFAULT_BUCKET_SIZE */
838 838
839/** 839/**
840 * List of active clients. 840 * List of active clients.
@@ -911,14 +911,11 @@ static struct GNUNET_BLOCK_Context *block_context;
911/** 911/**
912 * Forward declaration. 912 * Forward declaration.
913 */ 913 */
914static size_t 914static size_t send_generic_reply (void *cls, size_t size, void *buf);
915send_generic_reply (void *cls, size_t size, void *buf);
916 915
917 916
918/** Declare here so retry_core_send is aware of it */ 917/** Declare here so retry_core_send is aware of it */
919static size_t 918static size_t core_transmit_notify (void *cls, size_t size, void *buf);
920core_transmit_notify (void *cls,
921 size_t size, void *buf);
922 919
923/** 920/**
924 * Convert unique ID to hash code. 921 * Convert unique ID to hash code.
@@ -927,11 +924,10 @@ core_transmit_notify (void *cls,
927 * @param hash set to uid (extended with zeros) 924 * @param hash set to uid (extended with zeros)
928 */ 925 */
929static void 926static void
930hash_from_uid (uint64_t uid, 927hash_from_uid (uint64_t uid, GNUNET_HashCode * hash)
931 GNUNET_HashCode *hash)
932{ 928{
933 memset (hash, 0, sizeof(GNUNET_HashCode)); 929 memset (hash, 0, sizeof (GNUNET_HashCode));
934 *((uint64_t*)hash) = uid; 930 *((uint64_t *) hash) = uid;
935} 931}
936 932
937#if AVG 933#if AVG
@@ -942,31 +938,31 @@ hash_from_uid (uint64_t uid,
942 * @return the average time between asking core to send a message 938 * @return the average time between asking core to send a message
943 * and when the buffer for copying it is passed 939 * and when the buffer for copying it is passed
944 */ 940 */
945static struct GNUNET_TIME_Relative get_average_send_delay() 941static struct GNUNET_TIME_Relative
942get_average_send_delay ()
946{ 943{
947 unsigned int i; 944 unsigned int i;
948 unsigned int divisor; 945 unsigned int divisor;
949 struct GNUNET_TIME_Relative average_time; 946 struct GNUNET_TIME_Relative average_time;
950 average_time = GNUNET_TIME_relative_get_zero(); 947 average_time = GNUNET_TIME_relative_get_zero ();
951 divisor = 0; 948 divisor = 0;
952 for (i = 0; i < MAX_REPLY_TIMES; i++) 949 for (i = 0; i < MAX_REPLY_TIMES; i++)
953 { 950 {
954 average_time = GNUNET_TIME_relative_add(average_time, reply_times[i]); 951 average_time = GNUNET_TIME_relative_add (average_time, reply_times[i]);
955 if (reply_times[i].abs_value == (uint64_t)0) 952 if (reply_times[i].abs_value == (uint64_t) 0)
956 continue; 953 continue;
957 else 954 else
958 divisor++; 955 divisor++;
959 } 956 }
960 if (divisor == 0) 957 if (divisor == 0)
961 { 958 {
962 return average_time; 959 return average_time;
963 } 960 }
964 961
965 average_time = GNUNET_TIME_relative_divide(average_time, divisor); 962 average_time = GNUNET_TIME_relative_divide (average_time, divisor);
966 fprintf(stderr, 963 fprintf (stderr,
967 "Avg send delay: %u sends is %llu\n", 964 "Avg send delay: %u sends is %llu\n",
968 divisor, 965 divisor, (unsigned long long) average_time.abs_value);
969 (unsigned long long) average_time.abs_value);
970 return average_time; 966 return average_time;
971} 967}
972#endif 968#endif
@@ -976,7 +972,8 @@ static struct GNUNET_TIME_Relative get_average_send_delay()
976 * so the next time around we may have a chance at sending 972 * so the next time around we may have a chance at sending
977 * again. 973 * again.
978 */ 974 */
979static void decrease_max_send_delay(struct GNUNET_TIME_Relative max_time) 975static void
976decrease_max_send_delay (struct GNUNET_TIME_Relative max_time)
980{ 977{
981 unsigned int i; 978 unsigned int i;
982 for (i = 0; i < MAX_REPLY_TIMES; i++) 979 for (i = 0; i < MAX_REPLY_TIMES; i++)
@@ -995,26 +992,27 @@ static void decrease_max_send_delay(struct GNUNET_TIME_Relative max_time)
995 * @return the average time between asking core to send a message 992 * @return the average time between asking core to send a message
996 * and when the buffer for copying it is passed 993 * and when the buffer for copying it is passed
997 */ 994 */
998static struct GNUNET_TIME_Relative get_max_send_delay() 995static struct GNUNET_TIME_Relative
996get_max_send_delay ()
999{ 997{
1000 unsigned int i; 998 unsigned int i;
1001 struct GNUNET_TIME_Relative max_time; 999 struct GNUNET_TIME_Relative max_time;
1002 max_time = GNUNET_TIME_relative_get_zero(); 1000 max_time = GNUNET_TIME_relative_get_zero ();
1003 1001
1004 for (i = 0; i < MAX_REPLY_TIMES; i++) 1002 for (i = 0; i < MAX_REPLY_TIMES; i++)
1005 { 1003 {
1006 if (reply_times[i].rel_value > max_time.rel_value) 1004 if (reply_times[i].rel_value > max_time.rel_value)
1007 max_time.rel_value = reply_times[i].rel_value; 1005 max_time.rel_value = reply_times[i].rel_value;
1008 } 1006 }
1009 1007
1010 if (max_time.rel_value > MAX_REQUEST_TIME.rel_value) 1008 if (max_time.rel_value > MAX_REQUEST_TIME.rel_value)
1011 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n", 1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Max send delay was %llu\n",
1012 (unsigned long long) max_time.rel_value); 1010 (unsigned long long) max_time.rel_value);
1013 return max_time; 1011 return max_time;
1014} 1012}
1015 1013
1016static void 1014static void
1017increment_stats(const char *value) 1015increment_stats (const char *value)
1018{ 1016{
1019 if (stats != NULL) 1017 if (stats != NULL)
1020 { 1018 {
@@ -1026,8 +1024,7 @@ increment_stats(const char *value)
1026 * Try to send another message from our core send list 1024 * Try to send another message from our core send list
1027 */ 1025 */
1028static void 1026static void
1029try_core_send (void *cls, 1027try_core_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1030 const struct GNUNET_SCHEDULER_TaskContext *tc)
1031{ 1028{
1032 struct PeerInfo *peer = cls; 1029 struct PeerInfo *peer = cls;
1033 struct P2PPendingMessage *pending; 1030 struct P2PPendingMessage *pending;
@@ -1039,26 +1036,27 @@ try_core_send (void *cls,
1039 return; 1036 return;
1040 1037
1041 if (peer->th != NULL) 1038 if (peer->th != NULL)
1042 return; /* Message send already in progress */ 1039 return; /* Message send already in progress */
1043 1040
1044 pending = peer->head; 1041 pending = peer->head;
1045 if (pending != NULL) 1042 if (pending != NULL)
1046 { 1043 {
1047 ssize = ntohs(pending->msg->size); 1044 ssize = ntohs (pending->msg->size);
1048#if DEBUG_DHT > 1 1045#if DEBUG_DHT > 1
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n", my_short_id, 1047 "`%s:%s': Calling notify_transmit_ready with size %d for peer %s\n",
1051 "DHT", ssize, GNUNET_i2s(&peer->id)); 1048 my_short_id, "DHT", ssize, GNUNET_i2s (&peer->id));
1052#endif 1049#endif
1053 pending->scheduled = GNUNET_TIME_absolute_get(); 1050 pending->scheduled = GNUNET_TIME_absolute_get ();
1054 reply_counter++; 1051 reply_counter++;
1055 if (reply_counter >= MAX_REPLY_TIMES) 1052 if (reply_counter >= MAX_REPLY_TIMES)
1056 reply_counter = 0; 1053 reply_counter = 0;
1057 peer->th = GNUNET_CORE_notify_transmit_ready (coreAPI, pending->importance, 1054 peer->th =
1058 pending->timeout, &peer->id, 1055 GNUNET_CORE_notify_transmit_ready (coreAPI, pending->importance,
1059 ssize, &core_transmit_notify, peer); 1056 pending->timeout, &peer->id, ssize,
1057 &core_transmit_notify, peer);
1060 if (peer->th == NULL) 1058 if (peer->th == NULL)
1061 increment_stats("# notify transmit ready failed"); 1059 increment_stats ("# notify transmit ready failed");
1062 } 1060 }
1063} 1061}
1064 1062
@@ -1071,43 +1069,52 @@ try_core_send (void *cls,
1071 * @param peer the peer to forward the message to 1069 * @param peer the peer to forward the message to
1072 * @param msg_ctx the context of the message (hop count, bloom, etc.) 1070 * @param msg_ctx the context of the message (hop count, bloom, etc.)
1073 */ 1071 */
1074static void 1072static void
1075forward_result_message (const struct GNUNET_MessageHeader *msg, 1073forward_result_message (const struct GNUNET_MessageHeader *msg,
1076 struct PeerInfo *peer, 1074 struct PeerInfo *peer,
1077 struct DHT_MessageContext *msg_ctx) 1075 struct DHT_MessageContext *msg_ctx)
1078{ 1076{
1079 struct GNUNET_DHT_P2PRouteResultMessage *result_message; 1077 struct GNUNET_DHT_P2PRouteResultMessage *result_message;
1080 struct P2PPendingMessage *pending; 1078 struct P2PPendingMessage *pending;
1081 size_t msize; 1079 size_t msize;
1082 size_t psize; 1080 size_t psize;
1083 1081
1084 increment_stats(STAT_RESULT_FORWARDS); 1082 increment_stats (STAT_RESULT_FORWARDS);
1085 msize = sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs(msg->size); 1083 msize =
1086 GNUNET_assert(msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE); 1084 sizeof (struct GNUNET_DHT_P2PRouteResultMessage) + ntohs (msg->size);
1087 psize = sizeof(struct P2PPendingMessage) + msize; 1085 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1088 pending = GNUNET_malloc(psize); 1086 psize = sizeof (struct P2PPendingMessage) + msize;
1089 pending->msg = (struct GNUNET_MessageHeader *)&pending[1]; 1087 pending = GNUNET_malloc (psize);
1088 pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
1090 pending->importance = DHT_SEND_PRIORITY; 1089 pending->importance = DHT_SEND_PRIORITY;
1091 pending->timeout = GNUNET_TIME_relative_get_forever(); 1090 pending->timeout = GNUNET_TIME_relative_get_forever ();
1092 result_message = (struct GNUNET_DHT_P2PRouteResultMessage *)pending->msg; 1091 result_message = (struct GNUNET_DHT_P2PRouteResultMessage *) pending->msg;
1093 result_message->header.size = htons(msize); 1092 result_message->header.size = htons (msize);
1094 result_message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT); 1093 result_message->header.type =
1095 result_message->put_path_length = htons(0); /* FIXME: implement */ 1094 htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE_RESULT);
1096 result_message->get_path_length = htons(0); /* FIXME: implement */ 1095 result_message->put_path_length = htons (0); /* FIXME: implement */
1097 result_message->options = htonl(msg_ctx->msg_options); 1096 result_message->get_path_length = htons (0); /* FIXME: implement */
1098 result_message->hop_count = htonl(msg_ctx->hop_count + 1); 1097 result_message->options = htonl (msg_ctx->msg_options);
1099 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(msg_ctx->bloom, result_message->bloomfilter, DHT_BLOOM_SIZE)); 1098 result_message->hop_count = htonl (msg_ctx->hop_count + 1);
1100 result_message->unique_id = GNUNET_htonll(msg_ctx->unique_id); 1099 GNUNET_assert (GNUNET_OK ==
1101 memcpy(&result_message->key, &msg_ctx->key, sizeof(GNUNET_HashCode)); 1100 GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
1102 memcpy(&result_message[1], msg, ntohs(msg->size)); 1101 result_message->
1102 bloomfilter,
1103 DHT_BLOOM_SIZE));
1104 result_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1105 memcpy (&result_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1106 memcpy (&result_message[1], msg, ntohs (msg->size));
1103#if DEBUG_DHT > 1 1107#if DEBUG_DHT > 1
1104 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Adding pending message size %d for peer %s\n", my_short_id, "DHT", msize, GNUNET_i2s(&peer->id)); 1108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1109 "%s:%s Adding pending message size %d for peer %s\n",
1110 my_short_id, "DHT", msize, GNUNET_i2s (&peer->id));
1105#endif 1111#endif
1106 peer->pending_count++; 1112 peer->pending_count++;
1107 increment_stats("# pending messages scheduled"); 1113 increment_stats ("# pending messages scheduled");
1108 GNUNET_CONTAINER_DLL_insert_after(peer->head, peer->tail, peer->tail, pending); 1114 GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
1115 pending);
1109 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK) 1116 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1110 peer->send_task = GNUNET_SCHEDULER_add_now(&try_core_send, peer); 1117 peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1111} 1118}
1112 1119
1113 1120
@@ -1120,9 +1127,8 @@ forward_result_message (const struct GNUNET_MessageHeader *msg,
1120 * @param buf where the callee should write the message 1127 * @param buf where the callee should write the message
1121 * @return number of bytes written to buf 1128 * @return number of bytes written to buf
1122 */ 1129 */
1123static size_t 1130static size_t
1124core_transmit_notify (void *cls, 1131core_transmit_notify (void *cls, size_t size, void *buf)
1125 size_t size, void *buf)
1126{ 1132{
1127 struct PeerInfo *peer = cls; 1133 struct PeerInfo *peer = cls;
1128 char *cbuf = buf; 1134 char *cbuf = buf;
@@ -1135,7 +1141,8 @@ core_transmit_notify (void *cls,
1135 { 1141 {
1136 /* client disconnected */ 1142 /* client disconnected */
1137#if DEBUG_DHT 1143#if DEBUG_DHT
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n", my_short_id, "DHT"); 1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s:%s': buffer was NULL\n",
1145 my_short_id, "DHT");
1139#endif 1146#endif
1140 return 0; 1147 return 0;
1141 } 1148 }
@@ -1146,18 +1153,18 @@ core_transmit_notify (void *cls,
1146 off = 0; 1153 off = 0;
1147 pending = peer->head; 1154 pending = peer->head;
1148#if DUMB 1155#if DUMB
1149 reply_times[reply_counter] = GNUNET_TIME_absolute_get_difference(pending->scheduled, GNUNET_TIME_absolute_get()); 1156 reply_times[reply_counter] =
1150 msize = ntohs(pending->msg->size); 1157 GNUNET_TIME_absolute_get_difference (pending->scheduled,
1158 GNUNET_TIME_absolute_get ());
1159 msize = ntohs (pending->msg->size);
1151 if (msize <= size) 1160 if (msize <= size)
1152 { 1161 {
1153 off = msize; 1162 off = msize;
1154 memcpy (cbuf, pending->msg, msize); 1163 memcpy (cbuf, pending->msg, msize);
1155 peer->pending_count--; 1164 peer->pending_count--;
1156 increment_stats("# pending messages sent"); 1165 increment_stats ("# pending messages sent");
1157 GNUNET_assert(peer->pending_count >= 0); 1166 GNUNET_assert (peer->pending_count >= 0);
1158 GNUNET_CONTAINER_DLL_remove (peer->head, 1167 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
1159 peer->tail,
1160 pending);
1161 GNUNET_free (pending); 1168 GNUNET_free (pending);
1162 } 1169 }
1163#else 1170#else
@@ -1167,11 +1174,9 @@ core_transmit_notify (void *cls,
1167 memcpy (&cbuf[off], pending->msg, msize); 1174 memcpy (&cbuf[off], pending->msg, msize);
1168 off += msize; 1175 off += msize;
1169 peer->pending_count--; 1176 peer->pending_count--;
1170 increment_stats("# pending messages sent"); 1177 increment_stats ("# pending messages sent");
1171 GNUNET_assert(peer->pending_count >= 0); 1178 GNUNET_assert (peer->pending_count >= 0);
1172 GNUNET_CONTAINER_DLL_remove (peer->head, 1179 GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
1173 peer->tail,
1174 pending);
1175 GNUNET_free (pending); 1180 GNUNET_free (pending);
1176 pending = peer->head; 1181 pending = peer->head;
1177 } 1182 }
@@ -1232,7 +1237,8 @@ distance (const GNUNET_HashCode * target, const GNUNET_HashCode * have)
1232 for (i = bucket + 1; 1237 for (i = bucket + 1;
1233 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++) 1238 (i < sizeof (GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++)
1234 { 1239 {
1235 if (GNUNET_CRYPTO_hash_get_bit (target, i) != GNUNET_CRYPTO_hash_get_bit (have, i)) 1240 if (GNUNET_CRYPTO_hash_get_bit (target, i) !=
1241 GNUNET_CRYPTO_hash_get_bit (have, i))
1236 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10, 1242 lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10,
1237 last bit set will be 31 -- if 1243 last bit set will be 31 -- if
1238 i does not reach 512 first... */ 1244 i does not reach 512 first... */
@@ -1251,8 +1257,8 @@ static unsigned int
1251inverse_distance (const GNUNET_HashCode * target, 1257inverse_distance (const GNUNET_HashCode * target,
1252 const GNUNET_HashCode * have) 1258 const GNUNET_HashCode * have)
1253{ 1259{
1254 if (GNUNET_CRYPTO_hash_matching_bits(target, have) == 0) 1260 if (GNUNET_CRYPTO_hash_matching_bits (target, have) == 0)
1255 return 1; /* Never return 0! */ 1261 return 1; /* Never return 0! */
1256 return ((unsigned int) -1) - distance (target, have); 1262 return ((unsigned int) -1) - distance (target, have);
1257} 1263}
1258 1264
@@ -1265,11 +1271,12 @@ inverse_distance (const GNUNET_HashCode * target,
1265 * @return the proper bucket index, or GNUNET_SYSERR 1271 * @return the proper bucket index, or GNUNET_SYSERR
1266 * on error (same hashcode) 1272 * on error (same hashcode)
1267 */ 1273 */
1268static int find_bucket(const GNUNET_HashCode *hc) 1274static int
1275find_bucket (const GNUNET_HashCode * hc)
1269{ 1276{
1270 unsigned int bits; 1277 unsigned int bits;
1271 1278
1272 bits = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, hc); 1279 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, hc);
1273 if (bits == MAX_BUCKETS) 1280 if (bits == MAX_BUCKETS)
1274 return GNUNET_SYSERR; 1281 return GNUNET_SYSERR;
1275 return MAX_BUCKETS - bits - 1; 1282 return MAX_BUCKETS - bits - 1;
@@ -1287,14 +1294,15 @@ static int find_bucket(const GNUNET_HashCode *hc)
1287 * @return the proper bucket index for this key, 1294 * @return the proper bucket index for this key,
1288 * or GNUNET_SYSERR on error (same hashcode) 1295 * or GNUNET_SYSERR on error (same hashcode)
1289 */ 1296 */
1290static int find_current_bucket(const GNUNET_HashCode *hc) 1297static int
1298find_current_bucket (const GNUNET_HashCode * hc)
1291{ 1299{
1292 int actual_bucket; 1300 int actual_bucket;
1293 actual_bucket = find_bucket(hc); 1301 actual_bucket = find_bucket (hc);
1294 1302
1295 if (actual_bucket == GNUNET_SYSERR) /* hc and our peer identity match! */ 1303 if (actual_bucket == GNUNET_SYSERR) /* hc and our peer identity match! */
1296 return lowest_bucket; 1304 return lowest_bucket;
1297 else if (actual_bucket < lowest_bucket) /* actual_bucket not yet used */ 1305 else if (actual_bucket < lowest_bucket) /* actual_bucket not yet used */
1298 return lowest_bucket; 1306 return lowest_bucket;
1299 else 1307 else
1300 return actual_bucket; 1308 return actual_bucket;
@@ -1309,7 +1317,7 @@ static int find_current_bucket(const GNUNET_HashCode *hc)
1309 * @return the bucket number holding the peer, GNUNET_SYSERR if not found 1317 * @return the bucket number holding the peer, GNUNET_SYSERR if not found
1310 */ 1318 */
1311static int 1319static int
1312find_bucket_by_peer(const struct PeerInfo *peer) 1320find_bucket_by_peer (const struct PeerInfo *peer)
1313{ 1321{
1314 int bucket; 1322 int bucket;
1315 struct PeerInfo *pos; 1323 struct PeerInfo *pos;
@@ -1325,7 +1333,7 @@ find_bucket_by_peer(const struct PeerInfo *peer)
1325 } 1333 }
1326 } 1334 }
1327 1335
1328 return GNUNET_SYSERR; /* No such peer. */ 1336 return GNUNET_SYSERR; /* No such peer. */
1329} 1337}
1330#endif 1338#endif
1331 1339
@@ -1340,24 +1348,32 @@ print_routing_table ()
1340 struct PeerInfo *pos; 1348 struct PeerInfo *pos;
1341 char char_buf[30000]; 1349 char char_buf[30000];
1342 int char_pos; 1350 int char_pos;
1343 memset(char_buf, 0, sizeof(char_buf)); 1351 memset (char_buf, 0, sizeof (char_buf));
1344 char_pos = 0; 1352 char_pos = 0;
1345 char_pos += sprintf(&char_buf[char_pos], "Printing routing table for peer %s\n", my_short_id); 1353 char_pos +=
1354 sprintf (&char_buf[char_pos], "Printing routing table for peer %s\n",
1355 my_short_id);
1346 //fprintf(stderr, "Printing routing table for peer %s\n", my_short_id); 1356 //fprintf(stderr, "Printing routing table for peer %s\n", my_short_id);
1347 for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++) 1357 for (bucket = lowest_bucket; bucket < MAX_BUCKETS; bucket++)
1348 { 1358 {
1349 pos = k_buckets[bucket].head; 1359 pos = k_buckets[bucket].head;
1350 char_pos += sprintf(&char_buf[char_pos], "Bucket %d:\n", bucket); 1360 char_pos += sprintf (&char_buf[char_pos], "Bucket %d:\n", bucket);
1351 //fprintf(stderr, "Bucket %d:\n", bucket); 1361 //fprintf(stderr, "Bucket %d:\n", bucket);
1352 while (pos != NULL) 1362 while (pos != NULL)
1353 { 1363 {
1354 //fprintf(stderr, "\tPeer %s, best bucket %d, %d bits match\n", GNUNET_i2s(&pos->id), find_bucket(&pos->id.hashPubKey), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey)); 1364 //fprintf(stderr, "\tPeer %s, best bucket %d, %d bits match\n", GNUNET_i2s(&pos->id), find_bucket(&pos->id.hashPubKey), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey));
1355 char_pos += sprintf(&char_buf[char_pos], "\tPeer %s, best bucket %d, %d bits match\n", GNUNET_i2s(&pos->id), find_bucket(&pos->id.hashPubKey), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey)); 1365 char_pos +=
1366 sprintf (&char_buf[char_pos],
1367 "\tPeer %s, best bucket %d, %d bits match\n",
1368 GNUNET_i2s (&pos->id), find_bucket (&pos->id.hashPubKey),
1369 GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
1370 &my_identity.
1371 hashPubKey));
1356 pos = pos->next; 1372 pos = pos->next;
1357 } 1373 }
1358 } 1374 }
1359 fprintf(stderr, "%s", char_buf); 1375 fprintf (stderr, "%s", char_buf);
1360 fflush(stderr); 1376 fflush (stderr);
1361} 1377}
1362#endif 1378#endif
1363 1379
@@ -1369,23 +1385,23 @@ print_routing_table ()
1369 * @return the routing table entry, or NULL if not found 1385 * @return the routing table entry, or NULL if not found
1370 */ 1386 */
1371static struct PeerInfo * 1387static struct PeerInfo *
1372find_peer_by_id(const struct GNUNET_PeerIdentity *peer) 1388find_peer_by_id (const struct GNUNET_PeerIdentity *peer)
1373{ 1389{
1374 int bucket; 1390 int bucket;
1375 struct PeerInfo *pos; 1391 struct PeerInfo *pos;
1376 bucket = find_current_bucket(&peer->hashPubKey); 1392 bucket = find_current_bucket (&peer->hashPubKey);
1377 1393
1378 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity))) 1394 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1379 return NULL; 1395 return NULL;
1380 1396
1381 pos = k_buckets[bucket].head; 1397 pos = k_buckets[bucket].head;
1382 while (pos != NULL) 1398 while (pos != NULL)
1383 { 1399 {
1384 if (0 == memcmp(&pos->id, peer, sizeof(struct GNUNET_PeerIdentity))) 1400 if (0 == memcmp (&pos->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1385 return pos; 1401 return pos;
1386 pos = pos->next; 1402 pos = pos->next;
1387 } 1403 }
1388 return NULL; /* No such peer. */ 1404 return NULL; /* No such peer. */
1389} 1405}
1390 1406
1391/* Forward declaration */ 1407/* Forward declaration */
@@ -1404,13 +1420,14 @@ update_core_preference (void *cls,
1404 */ 1420 */
1405static void 1421static void
1406update_core_preference_finish (void *cls, 1422update_core_preference_finish (void *cls,
1407 const struct GNUNET_PeerIdentity * peer, 1423 const struct GNUNET_PeerIdentity *peer,
1408 struct GNUNET_BANDWIDTH_Value32NBO bpm_out, 1424 struct GNUNET_BANDWIDTH_Value32NBO bpm_out,
1409 int amount, uint64_t preference) 1425 int amount, uint64_t preference)
1410{ 1426{
1411 struct PeerInfo *peer_info = cls; 1427 struct PeerInfo *peer_info = cls;
1412 peer_info->info_ctx = NULL; 1428 peer_info->info_ctx = NULL;
1413 GNUNET_SCHEDULER_add_delayed(DHT_DEFAULT_PREFERENCE_INTERVAL, &update_core_preference, peer_info); 1429 GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL,
1430 &update_core_preference, peer_info);
1414} 1431}
1415 1432
1416static void 1433static void
@@ -1424,18 +1441,23 @@ update_core_preference (void *cls,
1424 { 1441 {
1425 return; 1442 return;
1426 } 1443 }
1427 matching = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &peer->id.hashPubKey); 1444 matching =
1445 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
1446 &peer->id.hashPubKey);
1428 if (matching >= 64) 1447 if (matching >= 64)
1429 { 1448 {
1430 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer identifier matches by %u bits, only shifting as much as we can!\n", matching); 1449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1450 "Peer identifier matches by %u bits, only shifting as much as we can!\n",
1451 matching);
1431 matching = 63; 1452 matching = 63;
1432 } 1453 }
1433 preference = 1LL << matching; 1454 preference = 1LL << matching;
1434 peer->info_ctx = GNUNET_CORE_peer_change_preference (coreAPI, 1455 peer->info_ctx = GNUNET_CORE_peer_change_preference (coreAPI,
1435 &peer->id, 1456 &peer->id,
1436 GNUNET_TIME_relative_get_forever(), 1457 GNUNET_TIME_relative_get_forever
1437 GNUNET_BANDWIDTH_value_init (UINT32_MAX), 1458 (),
1438 0, 1459 GNUNET_BANDWIDTH_value_init
1460 (UINT32_MAX), 0,
1439 preference, 1461 preference,
1440 &update_core_preference_finish, 1462 &update_core_preference_finish,
1441 peer); 1463 peer);
@@ -1453,31 +1475,33 @@ update_core_preference (void *cls,
1453 * @return the newly added PeerInfo 1475 * @return the newly added PeerInfo
1454 */ 1476 */
1455static struct PeerInfo * 1477static struct PeerInfo *
1456add_peer(const struct GNUNET_PeerIdentity *peer, 1478add_peer (const struct GNUNET_PeerIdentity *peer,
1457 unsigned int bucket, 1479 unsigned int bucket,
1458 const struct GNUNET_TRANSPORT_ATS_Information *atsi) 1480 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1459{ 1481{
1460 struct PeerInfo *new_peer; 1482 struct PeerInfo *new_peer;
1461 GNUNET_assert(bucket < MAX_BUCKETS); 1483 GNUNET_assert (bucket < MAX_BUCKETS);
1462 GNUNET_assert(peer != NULL); 1484 GNUNET_assert (peer != NULL);
1463 new_peer = GNUNET_malloc(sizeof(struct PeerInfo)); 1485 new_peer = GNUNET_malloc (sizeof (struct PeerInfo));
1464#if 0 1486#if 0
1465 new_peer->latency = latency; 1487 new_peer->latency = latency;
1466 new_peer->distance = distance; 1488 new_peer->distance = distance;
1467#endif 1489#endif
1468 1490
1469 memcpy(&new_peer->id, peer, sizeof(struct GNUNET_PeerIdentity)); 1491 memcpy (&new_peer->id, peer, sizeof (struct GNUNET_PeerIdentity));
1470 1492
1471 GNUNET_CONTAINER_DLL_insert_after(k_buckets[bucket].head, 1493 GNUNET_CONTAINER_DLL_insert_after (k_buckets[bucket].head,
1472 k_buckets[bucket].tail, 1494 k_buckets[bucket].tail,
1473 k_buckets[bucket].tail, 1495 k_buckets[bucket].tail, new_peer);
1474 new_peer);
1475 k_buckets[bucket].peers_size++; 1496 k_buckets[bucket].peers_size++;
1476 1497
1477 if ((GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &peer->hashPubKey) > 0) && (k_buckets[bucket].peers_size <= bucket_size)) 1498 if ((GNUNET_CRYPTO_hash_matching_bits
1499 (&my_identity.hashPubKey, &peer->hashPubKey) > 0)
1500 && (k_buckets[bucket].peers_size <= bucket_size))
1478 { 1501 {
1479#if DO_UPDATE_PREFERENCE 1502#if DO_UPDATE_PREFERENCE
1480 new_peer->preference_task = GNUNET_SCHEDULER_add_now(&update_core_preference, new_peer); 1503 new_peer->preference_task =
1504 GNUNET_SCHEDULER_add_now (&update_core_preference, new_peer);
1481#endif 1505#endif
1482 } 1506 }
1483 1507
@@ -1494,16 +1518,16 @@ add_peer(const struct GNUNET_PeerIdentity *peer,
1494 * @param peer the peer to remove 1518 * @param peer the peer to remove
1495 * @param bucket the bucket the peer belongs to 1519 * @param bucket the bucket the peer belongs to
1496 */ 1520 */
1497static void remove_peer (struct PeerInfo *peer, 1521static void
1498 unsigned int bucket) 1522remove_peer (struct PeerInfo *peer, unsigned int bucket)
1499{ 1523{
1500 GNUNET_assert(k_buckets[bucket].peers_size > 0); 1524 GNUNET_assert (k_buckets[bucket].peers_size > 0);
1501 GNUNET_CONTAINER_DLL_remove(k_buckets[bucket].head, 1525 GNUNET_CONTAINER_DLL_remove (k_buckets[bucket].head,
1502 k_buckets[bucket].tail, 1526 k_buckets[bucket].tail, peer);
1503 peer);
1504 k_buckets[bucket].peers_size--; 1527 k_buckets[bucket].peers_size--;
1505#if CHANGE_LOWEST 1528#if CHANGE_LOWEST
1506 if ((bucket == lowest_bucket) && (k_buckets[lowest_bucket].peers_size == 0) && (lowest_bucket < MAX_BUCKETS - 1)) 1529 if ((bucket == lowest_bucket) && (k_buckets[lowest_bucket].peers_size == 0)
1530 && (lowest_bucket < MAX_BUCKETS - 1))
1507 lowest_bucket++; 1531 lowest_bucket++;
1508#endif 1532#endif
1509} 1533}
@@ -1515,8 +1539,8 @@ static void remove_peer (struct PeerInfo *peer,
1515 * @param peer peer to be removed and freed 1539 * @param peer peer to be removed and freed
1516 * @param bucket which bucket this peer belongs to 1540 * @param bucket which bucket this peer belongs to
1517 */ 1541 */
1518static void delete_peer (struct PeerInfo *peer, 1542static void
1519 unsigned int bucket) 1543delete_peer (struct PeerInfo *peer, unsigned int bucket)
1520{ 1544{
1521 struct P2PPendingMessage *pos; 1545 struct P2PPendingMessage *pos;
1522 struct P2PPendingMessage *next; 1546 struct P2PPendingMessage *next;
@@ -1528,30 +1552,41 @@ static void delete_peer (struct PeerInfo *peer,
1528 peer_pos = peer_pos->next; 1552 peer_pos = peer_pos->next;
1529 if (peer_pos == NULL) 1553 if (peer_pos == NULL)
1530 { 1554 {
1531 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s: Expected peer `%s' in bucket %d\n", my_short_id, "DHT", GNUNET_i2s(&peer->id), bucket); 1555 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1532 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s: Lowest bucket: %d, find_current_bucket: %d, peer resides in bucket: %d\n", my_short_id, "DHT", lowest_bucket, find_current_bucket(&peer->id.hashPubKey), find_bucket_by_peer(peer)); 1556 "%s:%s: Expected peer `%s' in bucket %d\n", my_short_id,
1557 "DHT", GNUNET_i2s (&peer->id), bucket);
1558 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1559 "%s:%s: Lowest bucket: %d, find_current_bucket: %d, peer resides in bucket: %d\n",
1560 my_short_id, "DHT", lowest_bucket,
1561 find_current_bucket (&peer->id.hashPubKey),
1562 find_bucket_by_peer (peer));
1533 } 1563 }
1534 GNUNET_assert(peer_pos != NULL); 1564 GNUNET_assert (peer_pos != NULL);
1535#endif 1565#endif
1536 remove_peer(peer, bucket); /* First remove the peer from its bucket */ 1566 remove_peer (peer, bucket); /* First remove the peer from its bucket */
1537 1567
1538 if (peer->send_task != GNUNET_SCHEDULER_NO_TASK) 1568 if (peer->send_task != GNUNET_SCHEDULER_NO_TASK)
1539 GNUNET_SCHEDULER_cancel(peer->send_task); 1569 GNUNET_SCHEDULER_cancel (peer->send_task);
1540 if ((peer->th != NULL) && (coreAPI != NULL)) 1570 if ((peer->th != NULL) && (coreAPI != NULL))
1541 GNUNET_CORE_notify_transmit_ready_cancel(peer->th); 1571 GNUNET_CORE_notify_transmit_ready_cancel (peer->th);
1542 1572
1543 pos = peer->head; 1573 pos = peer->head;
1544 while (pos != NULL) /* Remove any pending messages for this peer */ 1574 while (pos != NULL) /* Remove any pending messages for this peer */
1545 { 1575 {
1546 increment_stats("# dht pending messages discarded (due to disconnect/shutdown)"); 1576 increment_stats
1577 ("# dht pending messages discarded (due to disconnect/shutdown)");
1547 next = pos->next; 1578 next = pos->next;
1548 GNUNET_free(pos); 1579 GNUNET_free (pos);
1549 pos = next; 1580 pos = next;
1550 } 1581 }
1551 1582
1552 GNUNET_assert(GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->id.hashPubKey)); 1583 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
1553 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (all_known_peers, &peer->id.hashPubKey, peer)); 1584 (all_known_peers, &peer->id.hashPubKey));
1554 GNUNET_free(peer); 1585 GNUNET_assert (GNUNET_YES ==
1586 GNUNET_CONTAINER_multihashmap_remove (all_known_peers,
1587 &peer->id.hashPubKey,
1588 peer));
1589 GNUNET_free (peer);
1555} 1590}
1556 1591
1557 1592
@@ -1565,20 +1600,18 @@ static void delete_peer (struct PeerInfo *peer,
1565 * iterate, 1600 * iterate,
1566 * GNUNET_NO if not. 1601 * GNUNET_NO if not.
1567 */ 1602 */
1568static int move_lowest_bucket (void *cls, 1603static int
1569 const GNUNET_HashCode * key, 1604move_lowest_bucket (void *cls, const GNUNET_HashCode * key, void *value)
1570 void *value)
1571{ 1605{
1572 struct PeerInfo *peer = value; 1606 struct PeerInfo *peer = value;
1573 int new_bucket; 1607 int new_bucket;
1574 1608
1575 GNUNET_assert(lowest_bucket > 0); 1609 GNUNET_assert (lowest_bucket > 0);
1576 new_bucket = lowest_bucket - 1; 1610 new_bucket = lowest_bucket - 1;
1577 remove_peer(peer, lowest_bucket); 1611 remove_peer (peer, lowest_bucket);
1578 GNUNET_CONTAINER_DLL_insert_after(k_buckets[new_bucket].head, 1612 GNUNET_CONTAINER_DLL_insert_after (k_buckets[new_bucket].head,
1579 k_buckets[new_bucket].tail, 1613 k_buckets[new_bucket].tail,
1580 k_buckets[new_bucket].tail, 1614 k_buckets[new_bucket].tail, peer);
1581 peer);
1582 k_buckets[new_bucket].peers_size++; 1615 k_buckets[new_bucket].peers_size++;
1583 return GNUNET_YES; 1616 return GNUNET_YES;
1584} 1617}
@@ -1589,33 +1622,37 @@ static int move_lowest_bucket (void *cls,
1589 * bucket to the next lower down, and move any appropriate 1622 * bucket to the next lower down, and move any appropriate
1590 * entries in the current lowest bucket to the new bucket. 1623 * entries in the current lowest bucket to the new bucket.
1591 */ 1624 */
1592static void enable_next_bucket() 1625static void
1626enable_next_bucket ()
1593{ 1627{
1594 struct GNUNET_CONTAINER_MultiHashMap *to_remove; 1628 struct GNUNET_CONTAINER_MultiHashMap *to_remove;
1595 struct PeerInfo *pos; 1629 struct PeerInfo *pos;
1596 GNUNET_assert(lowest_bucket > 0); 1630 GNUNET_assert (lowest_bucket > 0);
1597 to_remove = GNUNET_CONTAINER_multihashmap_create(bucket_size); 1631 to_remove = GNUNET_CONTAINER_multihashmap_create (bucket_size);
1598 pos = k_buckets[lowest_bucket].head; 1632 pos = k_buckets[lowest_bucket].head;
1599 1633
1600#if PRINT_TABLES 1634#if PRINT_TABLES
1601 fprintf(stderr, "Printing RT before new bucket\n"); 1635 fprintf (stderr, "Printing RT before new bucket\n");
1602 print_routing_table(); 1636 print_routing_table ();
1603#endif 1637#endif
1604 /* Populate the array of peers which should be in the next lowest bucket */ 1638 /* Populate the array of peers which should be in the next lowest bucket */
1605 while (pos != NULL) 1639 while (pos != NULL)
1606 { 1640 {
1607 if (find_bucket(&pos->id.hashPubKey) < lowest_bucket) 1641 if (find_bucket (&pos->id.hashPubKey) < lowest_bucket)
1608 GNUNET_CONTAINER_multihashmap_put(to_remove, &pos->id.hashPubKey, pos, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 1642 GNUNET_CONTAINER_multihashmap_put (to_remove, &pos->id.hashPubKey,
1643 pos,
1644 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1609 pos = pos->next; 1645 pos = pos->next;
1610 } 1646 }
1611 1647
1612 /* Remove peers from lowest bucket, insert into next lowest bucket */ 1648 /* Remove peers from lowest bucket, insert into next lowest bucket */
1613 GNUNET_CONTAINER_multihashmap_iterate(to_remove, &move_lowest_bucket, NULL); 1649 GNUNET_CONTAINER_multihashmap_iterate (to_remove, &move_lowest_bucket,
1614 GNUNET_CONTAINER_multihashmap_destroy(to_remove); 1650 NULL);
1651 GNUNET_CONTAINER_multihashmap_destroy (to_remove);
1615 lowest_bucket = lowest_bucket - 1; 1652 lowest_bucket = lowest_bucket - 1;
1616#if PRINT_TABLES 1653#if PRINT_TABLES
1617 fprintf(stderr, "Printing RT after new bucket\n"); 1654 fprintf (stderr, "Printing RT after new bucket\n");
1618 print_routing_table(); 1655 print_routing_table ();
1619#endif 1656#endif
1620} 1657}
1621 1658
@@ -1627,7 +1664,7 @@ static void enable_next_bucket()
1627 * key, or NULL on error. 1664 * key, or NULL on error.
1628 */ 1665 */
1629static struct PeerInfo * 1666static struct PeerInfo *
1630find_closest_peer (const GNUNET_HashCode *hc) 1667find_closest_peer (const GNUNET_HashCode * hc)
1631{ 1668{
1632 struct PeerInfo *pos; 1669 struct PeerInfo *pos;
1633 struct PeerInfo *current_closest; 1670 struct PeerInfo *current_closest;
@@ -1648,7 +1685,7 @@ find_closest_peer (const GNUNET_HashCode *hc)
1648 count = 0; 1685 count = 0;
1649 while ((pos != NULL) && (count < bucket_size)) 1686 while ((pos != NULL) && (count < bucket_size))
1650 { 1687 {
1651 temp_distance = distance(&pos->id.hashPubKey, hc); 1688 temp_distance = distance (&pos->id.hashPubKey, hc);
1652 if (temp_distance <= lowest_distance) 1689 if (temp_distance <= lowest_distance)
1653 { 1690 {
1654 lowest_distance = temp_distance; 1691 lowest_distance = temp_distance;
@@ -1658,7 +1695,7 @@ find_closest_peer (const GNUNET_HashCode *hc)
1658 count++; 1695 count++;
1659 } 1696 }
1660 } 1697 }
1661 GNUNET_assert(current_closest != NULL); 1698 GNUNET_assert (current_closest != NULL);
1662 return current_closest; 1699 return current_closest;
1663} 1700}
1664 1701
@@ -1672,47 +1709,55 @@ find_closest_peer (const GNUNET_HashCode *hc)
1672 * @param peer the peer to forward the message to 1709 * @param peer the peer to forward the message to
1673 * @param msg_ctx the context of the message (hop count, bloom, etc.) 1710 * @param msg_ctx the context of the message (hop count, bloom, etc.)
1674 */ 1711 */
1675static void forward_message (const struct GNUNET_MessageHeader *msg, 1712static void
1676 struct PeerInfo *peer, 1713forward_message (const struct GNUNET_MessageHeader *msg,
1677 struct DHT_MessageContext *msg_ctx) 1714 struct PeerInfo *peer, struct DHT_MessageContext *msg_ctx)
1678{ 1715{
1679 struct GNUNET_DHT_P2PRouteMessage *route_message; 1716 struct GNUNET_DHT_P2PRouteMessage *route_message;
1680 struct P2PPendingMessage *pending; 1717 struct P2PPendingMessage *pending;
1681 size_t msize; 1718 size_t msize;
1682 size_t psize; 1719 size_t psize;
1683 1720
1684 increment_stats(STAT_ROUTE_FORWARDS); 1721 increment_stats (STAT_ROUTE_FORWARDS);
1685 GNUNET_assert(peer != NULL); 1722 GNUNET_assert (peer != NULL);
1686 if ((msg_ctx->closest != GNUNET_YES) && (peer == find_closest_peer(&msg_ctx->key))) 1723 if ((msg_ctx->closest != GNUNET_YES)
1687 increment_stats(STAT_ROUTE_FORWARDS_CLOSEST); 1724 && (peer == find_closest_peer (&msg_ctx->key)))
1688 1725 increment_stats (STAT_ROUTE_FORWARDS_CLOSEST);
1689 msize = sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs(msg->size); 1726
1690 GNUNET_assert(msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE); 1727 msize = sizeof (struct GNUNET_DHT_P2PRouteMessage) + ntohs (msg->size);
1691 psize = sizeof(struct P2PPendingMessage) + msize; 1728 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
1692 pending = GNUNET_malloc(psize); 1729 psize = sizeof (struct P2PPendingMessage) + msize;
1693 pending->msg = (struct GNUNET_MessageHeader *)&pending[1]; 1730 pending = GNUNET_malloc (psize);
1731 pending->msg = (struct GNUNET_MessageHeader *) &pending[1];
1694 pending->importance = msg_ctx->importance; 1732 pending->importance = msg_ctx->importance;
1695 pending->timeout = msg_ctx->timeout; 1733 pending->timeout = msg_ctx->timeout;
1696 route_message = (struct GNUNET_DHT_P2PRouteMessage *)pending->msg; 1734 route_message = (struct GNUNET_DHT_P2PRouteMessage *) pending->msg;
1697 route_message->header.size = htons(msize); 1735 route_message->header.size = htons (msize);
1698 route_message->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE); 1736 route_message->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_ROUTE);
1699 route_message->options = htonl(msg_ctx->msg_options); 1737 route_message->options = htonl (msg_ctx->msg_options);
1700 route_message->hop_count = htonl(msg_ctx->hop_count + 1); 1738 route_message->hop_count = htonl (msg_ctx->hop_count + 1);
1701 route_message->network_size = htonl(msg_ctx->network_size); 1739 route_message->network_size = htonl (msg_ctx->network_size);
1702 route_message->desired_replication_level = htonl(msg_ctx->replication); 1740 route_message->desired_replication_level = htonl (msg_ctx->replication);
1703 route_message->unique_id = GNUNET_htonll(msg_ctx->unique_id); 1741 route_message->unique_id = GNUNET_htonll (msg_ctx->unique_id);
1704 if (msg_ctx->bloom != NULL) 1742 if (msg_ctx->bloom != NULL)
1705 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(msg_ctx->bloom, route_message->bloomfilter, DHT_BLOOM_SIZE)); 1743 GNUNET_assert (GNUNET_OK ==
1706 memcpy(&route_message->key, &msg_ctx->key, sizeof(GNUNET_HashCode)); 1744 GNUNET_CONTAINER_bloomfilter_get_raw_data (msg_ctx->bloom,
1707 memcpy(&route_message[1], msg, ntohs(msg->size)); 1745 route_message->
1746 bloomfilter,
1747 DHT_BLOOM_SIZE));
1748 memcpy (&route_message->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
1749 memcpy (&route_message[1], msg, ntohs (msg->size));
1708#if DEBUG_DHT > 1 1750#if DEBUG_DHT > 1
1709 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Adding pending message size %d for peer %s\n", my_short_id, "DHT", msize, GNUNET_i2s(&peer->id)); 1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1752 "%s:%s Adding pending message size %d for peer %s\n",
1753 my_short_id, "DHT", msize, GNUNET_i2s (&peer->id));
1710#endif 1754#endif
1711 peer->pending_count++; 1755 peer->pending_count++;
1712 increment_stats("# pending messages scheduled"); 1756 increment_stats ("# pending messages scheduled");
1713 GNUNET_CONTAINER_DLL_insert_after(peer->head, peer->tail, peer->tail, pending); 1757 GNUNET_CONTAINER_DLL_insert_after (peer->head, peer->tail, peer->tail,
1758 pending);
1714 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK) 1759 if (peer->send_task == GNUNET_SCHEDULER_NO_TASK)
1715 peer->send_task = GNUNET_SCHEDULER_add_now(&try_core_send, peer); 1760 peer->send_task = GNUNET_SCHEDULER_add_now (&try_core_send, peer);
1716} 1761}
1717 1762
1718#if DO_PING 1763#if DO_PING
@@ -1733,22 +1778,27 @@ periodic_ping_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1733 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) 1778 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1734 return; 1779 return;
1735 1780
1736 ping_message.size = htons(sizeof(struct GNUNET_MessageHeader)); 1781 ping_message.size = htons (sizeof (struct GNUNET_MessageHeader));
1737 ping_message.type = htons(GNUNET_MESSAGE_TYPE_DHT_P2P_PING); 1782 ping_message.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PING);
1738 1783
1739 memset(&msg_ctx, 0, sizeof(struct DHT_MessageContext)); 1784 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
1740#if DEBUG_PING 1785#if DEBUG_PING
1741 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Sending periodic ping to %s\n", my_short_id, "DHT", GNUNET_i2s(&peer->id)); 1786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1787 "%s:%s Sending periodic ping to %s\n", my_short_id, "DHT",
1788 GNUNET_i2s (&peer->id));
1742#endif 1789#endif
1743 forward_message(&ping_message, peer, &msg_ctx); 1790 forward_message (&ping_message, peer, &msg_ctx);
1744 peer->ping_task = GNUNET_SCHEDULER_add_delayed(DHT_DEFAULT_PING_DELAY, &periodic_ping_task, peer); 1791 peer->ping_task =
1792 GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PING_DELAY, &periodic_ping_task,
1793 peer);
1745} 1794}
1746 1795
1747/** 1796/**
1748 * Schedule PING messages for the top X peers in each 1797 * Schedule PING messages for the top X peers in each
1749 * bucket of the routing table (so core won't disconnect them!) 1798 * bucket of the routing table (so core won't disconnect them!)
1750 */ 1799 */
1751void schedule_ping_messages() 1800void
1801schedule_ping_messages ()
1752{ 1802{
1753 unsigned int bucket; 1803 unsigned int bucket;
1754 unsigned int count; 1804 unsigned int count;
@@ -1759,11 +1809,13 @@ void schedule_ping_messages()
1759 count = 0; 1809 count = 0;
1760 while (pos != NULL) 1810 while (pos != NULL)
1761 { 1811 {
1762 if ((count < bucket_size) && (pos->ping_task == GNUNET_SCHEDULER_NO_TASK)) 1812 if ((count < bucket_size)
1763 GNUNET_SCHEDULER_add_now(&periodic_ping_task, pos); 1813 && (pos->ping_task == GNUNET_SCHEDULER_NO_TASK))
1764 else if ((count >= bucket_size) && (pos->ping_task != GNUNET_SCHEDULER_NO_TASK)) 1814 GNUNET_SCHEDULER_add_now (&periodic_ping_task, pos);
1815 else if ((count >= bucket_size)
1816 && (pos->ping_task != GNUNET_SCHEDULER_NO_TASK))
1765 { 1817 {
1766 GNUNET_SCHEDULER_cancel(pos->ping_task); 1818 GNUNET_SCHEDULER_cancel (pos->ping_task);
1767 pos->ping_task = GNUNET_SCHEDULER_NO_TASK; 1819 pos->ping_task = GNUNET_SCHEDULER_NO_TASK;
1768 } 1820 }
1769 pos = pos->next; 1821 pos = pos->next;
@@ -1785,25 +1837,25 @@ void schedule_ping_messages()
1785 * pointer to PeerInfo for new peer otherwise 1837 * pointer to PeerInfo for new peer otherwise
1786 */ 1838 */
1787static struct PeerInfo * 1839static struct PeerInfo *
1788try_add_peer(const struct GNUNET_PeerIdentity *peer, 1840try_add_peer (const struct GNUNET_PeerIdentity *peer,
1789 unsigned int bucket, 1841 unsigned int bucket,
1790 const struct GNUNET_TRANSPORT_ATS_Information *atsi) 1842 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1791{ 1843{
1792 int peer_bucket; 1844 int peer_bucket;
1793 struct PeerInfo *new_peer; 1845 struct PeerInfo *new_peer;
1794 1846
1795 if (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity))) 1847 if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
1796 return NULL; 1848 return NULL;
1797 1849
1798 peer_bucket = find_current_bucket(&peer->hashPubKey); 1850 peer_bucket = find_current_bucket (&peer->hashPubKey);
1799 1851
1800 GNUNET_assert(peer_bucket >= lowest_bucket); 1852 GNUNET_assert (peer_bucket >= lowest_bucket);
1801 new_peer = add_peer(peer, peer_bucket, atsi); 1853 new_peer = add_peer (peer, peer_bucket, atsi);
1802 1854
1803 if ((k_buckets[lowest_bucket].peers_size) >= bucket_size) 1855 if ((k_buckets[lowest_bucket].peers_size) >= bucket_size)
1804 enable_next_bucket(); 1856 enable_next_bucket ();
1805#if DO_PING 1857#if DO_PING
1806 schedule_ping_messages(); 1858 schedule_ping_messages ();
1807#endif 1859#endif
1808 return new_peer; 1860 return new_peer;
1809} 1861}
@@ -1816,18 +1868,18 @@ try_add_peer(const struct GNUNET_PeerIdentity *peer,
1816 */ 1868 */
1817static void 1869static void
1818process_pending_messages (struct ClientList *client) 1870process_pending_messages (struct ClientList *client)
1819{ 1871{
1820 if (client->pending_head == NULL) 1872 if (client->pending_head == NULL)
1821 return; 1873 return;
1822 if (client->transmit_handle != NULL) 1874 if (client->transmit_handle != NULL)
1823 return; 1875 return;
1824 1876
1825 client->transmit_handle = 1877 client->transmit_handle =
1826 GNUNET_SERVER_notify_transmit_ready (client->client_handle, 1878 GNUNET_SERVER_notify_transmit_ready (client->client_handle,
1827 ntohs (client->pending_head->msg-> 1879 ntohs (client->pending_head->
1828 size), 1880 msg->size),
1829 GNUNET_TIME_UNIT_FOREVER_REL, 1881 GNUNET_TIME_UNIT_FOREVER_REL,
1830 &send_generic_reply, client); 1882 &send_generic_reply, client);
1831} 1883}
1832 1884
1833/** 1885/**
@@ -1852,18 +1904,17 @@ send_generic_reply (void *cls, size_t size, void *buf)
1852 size_t msize; 1904 size_t msize;
1853 1905
1854 client->transmit_handle = NULL; 1906 client->transmit_handle = NULL;
1855 if (buf == NULL) 1907 if (buf == NULL)
1856 { 1908 {
1857 /* client disconnected */ 1909 /* client disconnected */
1858 return 0; 1910 return 0;
1859 } 1911 }
1860 off = 0; 1912 off = 0;
1861 while ( (NULL != (reply = client->pending_head)) && 1913 while ((NULL != (reply = client->pending_head)) &&
1862 (size >= off + (msize = ntohs (reply->msg->size)))) 1914 (size >= off + (msize = ntohs (reply->msg->size))))
1863 { 1915 {
1864 GNUNET_CONTAINER_DLL_remove (client->pending_head, 1916 GNUNET_CONTAINER_DLL_remove (client->pending_head,
1865 client->pending_tail, 1917 client->pending_tail, reply);
1866 reply);
1867 memcpy (&cbuf[off], reply->msg, msize); 1918 memcpy (&cbuf[off], reply->msg, msize);
1868 GNUNET_free (reply); 1919 GNUNET_free (reply);
1869 off += msize; 1920 off += msize;
@@ -1871,8 +1922,8 @@ send_generic_reply (void *cls, size_t size, void *buf)
1871 process_pending_messages (client); 1922 process_pending_messages (client);
1872#if DEBUG_DHT 1923#if DEBUG_DHT
1873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1874 "Transmitted %u bytes of replies to client\n", 1925 "Transmitted %u bytes of replies to client\n",
1875 (unsigned int) off); 1926 (unsigned int) off);
1876#endif 1927#endif
1877 return off; 1928 return off;
1878} 1929}
@@ -1889,9 +1940,8 @@ add_pending_message (struct ClientList *client,
1889 struct PendingMessage *pending_message) 1940 struct PendingMessage *pending_message)
1890{ 1941{
1891 GNUNET_CONTAINER_DLL_insert_after (client->pending_head, 1942 GNUNET_CONTAINER_DLL_insert_after (client->pending_head,
1892 client->pending_tail, 1943 client->pending_tail,
1893 client->pending_tail, 1944 client->pending_tail, pending_message);
1894 pending_message);
1895 process_pending_messages (client); 1945 process_pending_messages (client);
1896} 1946}
1897 1947
@@ -1907,8 +1957,7 @@ add_pending_message (struct ClientList *client,
1907static void 1957static void
1908send_reply_to_client (struct ClientList *client, 1958send_reply_to_client (struct ClientList *client,
1909 const struct GNUNET_MessageHeader *message, 1959 const struct GNUNET_MessageHeader *message,
1910 unsigned long long uid, 1960 unsigned long long uid, const GNUNET_HashCode * key)
1911 const GNUNET_HashCode *key)
1912{ 1961{
1913 struct GNUNET_DHT_RouteResultMessage *reply; 1962 struct GNUNET_DHT_RouteResultMessage *reply;
1914 struct PendingMessage *pending_message; 1963 struct PendingMessage *pending_message;
@@ -1926,12 +1975,12 @@ send_reply_to_client (struct ClientList *client,
1926 return; 1975 return;
1927 } 1976 }
1928 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize); 1977 pending_message = GNUNET_malloc (sizeof (struct PendingMessage) + tsize);
1929 pending_message->msg = (struct GNUNET_MessageHeader *)&pending_message[1]; 1978 pending_message->msg = (struct GNUNET_MessageHeader *) &pending_message[1];
1930 reply = (struct GNUNET_DHT_RouteResultMessage *)&pending_message[1]; 1979 reply = (struct GNUNET_DHT_RouteResultMessage *) &pending_message[1];
1931 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT); 1980 reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_RESULT);
1932 reply->header.size = htons (tsize); 1981 reply->header.size = htons (tsize);
1933 reply->put_path_length = htons(0); /* FIXME: implement */ 1982 reply->put_path_length = htons (0); /* FIXME: implement */
1934 reply->get_path_length = htons(0); /* FIXME: implement */ 1983 reply->get_path_length = htons (0); /* FIXME: implement */
1935 reply->unique_id = GNUNET_htonll (uid); 1984 reply->unique_id = GNUNET_htonll (uid);
1936 reply->key = *key; 1985 reply->key = *key;
1937 memcpy (&reply[1], message, msize); 1986 memcpy (&reply[1], message, msize);
@@ -1950,15 +1999,21 @@ send_reply_to_client (struct ClientList *client,
1950 * @return GNUNET_YES if we want this peer, GNUNET_NO if not (bucket 1999 * @return GNUNET_YES if we want this peer, GNUNET_NO if not (bucket
1951 * already full) 2000 * already full)
1952 */ 2001 */
1953static int consider_peer (struct GNUNET_PeerIdentity *peer) 2002static int
2003consider_peer (struct GNUNET_PeerIdentity *peer)
1954{ 2004{
1955 int bucket; 2005 int bucket;
1956 2006
1957 if ((GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey)) || (0 == memcmp(&my_identity, peer, sizeof(struct GNUNET_PeerIdentity)))) 2007 if ((GNUNET_YES ==
1958 return GNUNET_NO; /* We already know this peer (are connected even!) */ 2008 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
1959 bucket = find_current_bucket(&peer->hashPubKey); 2009 &peer->hashPubKey))
2010 || (0 ==
2011 memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))))
2012 return GNUNET_NO; /* We already know this peer (are connected even!) */
2013 bucket = find_current_bucket (&peer->hashPubKey);
1960 2014
1961 if ((k_buckets[bucket].peers_size < bucket_size) || ((bucket == lowest_bucket) && (lowest_bucket > 0))) 2015 if ((k_buckets[bucket].peers_size < bucket_size)
2016 || ((bucket == lowest_bucket) && (lowest_bucket > 0)))
1962 return GNUNET_YES; 2017 return GNUNET_YES;
1963 2018
1964 return GNUNET_NO; 2019 return GNUNET_NO;
@@ -1973,22 +2028,27 @@ static int consider_peer (struct GNUNET_PeerIdentity *peer)
1973 * @param tc context, reason, etc. 2028 * @param tc context, reason, etc.
1974 */ 2029 */
1975static void 2030static void
1976remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 2031remove_forward_entry (void *cls,
2032 const struct GNUNET_SCHEDULER_TaskContext *tc)
1977{ 2033{
1978 struct DHTRouteSource *source_info = cls; 2034 struct DHTRouteSource *source_info = cls;
1979 struct DHTQueryRecord *record; 2035 struct DHTQueryRecord *record;
1980 source_info = GNUNET_CONTAINER_heap_remove_node(forward_list.minHeap, source_info->hnode); 2036 source_info =
2037 GNUNET_CONTAINER_heap_remove_node (forward_list.minHeap,
2038 source_info->hnode);
1981 record = source_info->record; 2039 record = source_info->record;
1982 GNUNET_CONTAINER_DLL_remove(record->head, record->tail, source_info); 2040 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
1983 2041
1984 if (record->head == NULL) /* No more entries in DLL */ 2042 if (record->head == NULL) /* No more entries in DLL */
1985 { 2043 {
1986 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (forward_list.hashmap, &record->key, record)); 2044 GNUNET_assert (GNUNET_YES ==
1987 GNUNET_free(record); 2045 GNUNET_CONTAINER_multihashmap_remove
2046 (forward_list.hashmap, &record->key, record));
2047 GNUNET_free (record);
1988 } 2048 }
1989 if (source_info->find_peers_responded != NULL) 2049 if (source_info->find_peers_responded != NULL)
1990 GNUNET_CONTAINER_bloomfilter_free(source_info->find_peers_responded); 2050 GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
1991 GNUNET_free(source_info); 2051 GNUNET_free (source_info);
1992} 2052}
1993 2053
1994/** 2054/**
@@ -2001,8 +2061,9 @@ remove_forward_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2001 * @return the number of peers the message was routed to, 2061 * @return the number of peers the message was routed to,
2002 * GNUNET_SYSERR on failure 2062 * GNUNET_SYSERR on failure
2003 */ 2063 */
2004static int route_result_message(struct GNUNET_MessageHeader *msg, 2064static int
2005 struct DHT_MessageContext *msg_ctx) 2065route_result_message (struct GNUNET_MessageHeader *msg,
2066 struct DHT_MessageContext *msg_ctx)
2006{ 2067{
2007 struct GNUNET_PeerIdentity new_peer; 2068 struct GNUNET_PeerIdentity new_peer;
2008 struct DHTQueryRecord *record; 2069 struct DHTQueryRecord *record;
@@ -2010,48 +2071,57 @@ static int route_result_message(struct GNUNET_MessageHeader *msg,
2010 struct PeerInfo *peer_info; 2071 struct PeerInfo *peer_info;
2011 const struct GNUNET_MessageHeader *hello_msg; 2072 const struct GNUNET_MessageHeader *hello_msg;
2012 2073
2013 increment_stats(STAT_RESULTS); 2074 increment_stats (STAT_RESULTS);
2014 /** 2075 /**
2015 * If a find peer result message is received and contains a valid 2076 * If a find peer result message is received and contains a valid
2016 * HELLO for another peer, offer it to the transport service. 2077 * HELLO for another peer, offer it to the transport service.
2017 */ 2078 */
2018 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) 2079 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
2019 { 2080 {
2020 if (ntohs(msg->size) <= sizeof(struct GNUNET_MessageHeader)) 2081 if (ntohs (msg->size) <= sizeof (struct GNUNET_MessageHeader))
2021 GNUNET_break_op(0); 2082 GNUNET_break_op (0);
2022 2083
2023 hello_msg = &msg[1]; 2084 hello_msg = &msg[1];
2024 if ((ntohs(hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO) || (GNUNET_SYSERR == GNUNET_HELLO_get_id((const struct GNUNET_HELLO_Message *)hello_msg, &new_peer))) 2085 if ((ntohs (hello_msg->type) != GNUNET_MESSAGE_TYPE_HELLO)
2025 { 2086 || (GNUNET_SYSERR ==
2026 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s:%s Received non-HELLO message type in find peer result message!\n", my_short_id, "DHT"); 2087 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
2027 GNUNET_break_op(0); 2088 hello_msg, &new_peer)))
2028 return GNUNET_NO;
2029 }
2030 else /* We have a valid hello, and peer id stored in new_peer */
2031 {
2032 find_peer_context.count++;
2033 increment_stats(STAT_FIND_PEER_REPLY);
2034 if (GNUNET_YES == consider_peer(&new_peer))
2035 { 2089 {
2036 increment_stats(STAT_HELLOS_PROVIDED); 2090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2037 GNUNET_TRANSPORT_offer_hello(transport_handle, hello_msg); 2091 "%s:%s Received non-HELLO message type in find peer result message!\n",
2038 GNUNET_CORE_peer_request_connect(coreAPI, 2092 my_short_id, "DHT");
2039 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), &new_peer, NULL, NULL); 2093 GNUNET_break_op (0);
2094 return GNUNET_NO;
2095 }
2096 else /* We have a valid hello, and peer id stored in new_peer */
2097 {
2098 find_peer_context.count++;
2099 increment_stats (STAT_FIND_PEER_REPLY);
2100 if (GNUNET_YES == consider_peer (&new_peer))
2101 {
2102 increment_stats (STAT_HELLOS_PROVIDED);
2103 GNUNET_TRANSPORT_offer_hello (transport_handle, hello_msg);
2104 GNUNET_CORE_peer_request_connect (coreAPI,
2105 GNUNET_TIME_relative_multiply
2106 (GNUNET_TIME_UNIT_SECONDS, 5),
2107 &new_peer, NULL, NULL);
2108 }
2040 } 2109 }
2041 }
2042 } 2110 }
2043 2111
2044 if (malicious_dropper == GNUNET_YES) 2112 if (malicious_dropper == GNUNET_YES)
2045 record = NULL; 2113 record = NULL;
2046 else 2114 else
2047 record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, &msg_ctx->key); 2115 record =
2116 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
2048 2117
2049 if (record == NULL) /* No record of this message! */ 2118 if (record == NULL) /* No record of this message! */
2050 { 2119 {
2051#if DEBUG_DHT 2120#if DEBUG_DHT
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2053 "`%s:%s': Have no record of response key %s uid %llu\n", my_short_id, 2122 "`%s:%s': Have no record of response key %s uid %llu\n",
2054 "DHT", GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id); 2123 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
2124 msg_ctx->unique_id);
2055#endif 2125#endif
2056#if DEBUG_DHT_ROUTING 2126#if DEBUG_DHT_ROUTING
2057 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 2127 if ((debug_routes_extended) && (dhtlog_handle != NULL))
@@ -2062,13 +2132,12 @@ static int route_result_message(struct GNUNET_MessageHeader *msg,
2062 msg_ctx->hop_count, 2132 msg_ctx->hop_count,
2063 GNUNET_SYSERR, 2133 GNUNET_SYSERR,
2064 &my_identity, 2134 &my_identity,
2065 &msg_ctx->key, 2135 &msg_ctx->key, msg_ctx->peer, NULL);
2066 msg_ctx->peer, NULL);
2067 } 2136 }
2068#endif 2137#endif
2069 if (msg_ctx->bloom != NULL) 2138 if (msg_ctx->bloom != NULL)
2070 { 2139 {
2071 GNUNET_CONTAINER_bloomfilter_free(msg_ctx->bloom); 2140 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
2072 msg_ctx->bloom = NULL; 2141 msg_ctx->bloom = NULL;
2073 } 2142 }
2074 return 0; 2143 return 0;
@@ -2078,61 +2147,72 @@ static int route_result_message(struct GNUNET_MessageHeader *msg,
2078 while (pos != NULL) 2147 while (pos != NULL)
2079 { 2148 {
2080#if STRICT_FORWARDING 2149#if STRICT_FORWARDING
2081 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) /* If we have already forwarded this peer id, don't do it again! */ 2150 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT) /* If we have already forwarded this peer id, don't do it again! */
2082 { 2151 {
2083 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (pos->find_peers_responded, &new_peer.hashPubKey)) 2152 if (GNUNET_YES ==
2084 { 2153 GNUNET_CONTAINER_bloomfilter_test (pos->find_peers_responded,
2085 increment_stats("# find peer responses NOT forwarded (bloom match)"); 2154 &new_peer.hashPubKey))
2086 pos = pos->next; 2155 {
2087 continue; 2156 increment_stats
2088 } 2157 ("# find peer responses NOT forwarded (bloom match)");
2158 pos = pos->next;
2159 continue;
2160 }
2089 else 2161 else
2090 GNUNET_CONTAINER_bloomfilter_add(pos->find_peers_responded, &new_peer.hashPubKey); 2162 GNUNET_CONTAINER_bloomfilter_add (pos->find_peers_responded,
2163 &new_peer.hashPubKey);
2091 } 2164 }
2092#endif 2165#endif
2093 2166
2094 if (0 == memcmp(&pos->source, &my_identity, sizeof(struct GNUNET_PeerIdentity))) /* Local client (or DHT) initiated request! */ 2167 if (0 == memcmp (&pos->source, &my_identity, sizeof (struct GNUNET_PeerIdentity))) /* Local client (or DHT) initiated request! */
2095 { 2168 {
2096#if DEBUG_DHT 2169#if DEBUG_DHT
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2098 "`%s:%s': Sending response key %s uid %llu to client\n", my_short_id, 2171 "`%s:%s': Sending response key %s uid %llu to client\n",
2099 "DHT", GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id); 2172 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
2173 msg_ctx->unique_id);
2100#endif 2174#endif
2101#if DEBUG_DHT_ROUTING 2175#if DEBUG_DHT_ROUTING
2102 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 2176 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2103 { 2177 {
2104 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_RESULT, 2178 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id,
2105 msg_ctx->hop_count, 2179 DHTLOG_RESULT, msg_ctx->hop_count,
2106 GNUNET_YES, &my_identity, &msg_ctx->key, 2180 GNUNET_YES, &my_identity,
2107 msg_ctx->peer, NULL); 2181 &msg_ctx->key, msg_ctx->peer,
2182 NULL);
2108 } 2183 }
2109#endif 2184#endif
2110 increment_stats(STAT_RESULTS_TO_CLIENT); 2185 increment_stats (STAT_RESULTS_TO_CLIENT);
2111 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT) 2186 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET_RESULT)
2112 increment_stats(STAT_GET_REPLY); 2187 increment_stats (STAT_GET_REPLY);
2113 2188
2114 send_reply_to_client(pos->client, msg, 2189 send_reply_to_client (pos->client, msg,
2115 msg_ctx->unique_id, 2190 msg_ctx->unique_id, &msg_ctx->key);
2116 &msg_ctx->key);
2117 } 2191 }
2118 else /* Send to peer */ 2192 else /* Send to peer */
2119 { 2193 {
2120 peer_info = find_peer_by_id(&pos->source); 2194 peer_info = find_peer_by_id (&pos->source);
2121 if (peer_info == NULL) /* Didn't find the peer in our routing table, perhaps peer disconnected! */ 2195 if (peer_info == NULL) /* Didn't find the peer in our routing table, perhaps peer disconnected! */
2122 { 2196 {
2123 pos = pos->next; 2197 pos = pos->next;
2124 continue; 2198 continue;
2125 } 2199 }
2126 2200
2127 if (msg_ctx->bloom == NULL) 2201 if (msg_ctx->bloom == NULL)
2128 msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K); 2202 msg_ctx->bloom =
2129 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom, &my_identity.hashPubKey); 2203 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE,
2130 if ((GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (msg_ctx->bloom, &peer_info->id.hashPubKey))) 2204 DHT_BLOOM_K);
2205 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
2206 &my_identity.hashPubKey);
2207 if ((GNUNET_NO ==
2208 GNUNET_CONTAINER_bloomfilter_test (msg_ctx->bloom,
2209 &peer_info->id.hashPubKey)))
2131 { 2210 {
2132#if DEBUG_DHT 2211#if DEBUG_DHT
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2134 "`%s:%s': Forwarding response key %s uid %llu to peer %s\n", my_short_id, 2213 "`%s:%s': Forwarding response key %s uid %llu to peer %s\n",
2135 "DHT", GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id, GNUNET_i2s(&peer_info->id)); 2214 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
2215 msg_ctx->unique_id, GNUNET_i2s (&peer_info->id));
2136#endif 2216#endif
2137#if DEBUG_DHT_ROUTING 2217#if DEBUG_DHT_ROUTING
2138 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 2218 if ((debug_routes_extended) && (dhtlog_handle != NULL))
@@ -2140,34 +2220,37 @@ static int route_result_message(struct GNUNET_MessageHeader *msg,
2140 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, 2220 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id,
2141 DHTLOG_RESULT, 2221 DHTLOG_RESULT,
2142 msg_ctx->hop_count, 2222 msg_ctx->hop_count,
2143 GNUNET_NO, &my_identity, &msg_ctx->key, 2223 GNUNET_NO, &my_identity,
2144 msg_ctx->peer, &pos->source); 2224 &msg_ctx->key, msg_ctx->peer,
2225 &pos->source);
2145 } 2226 }
2146#endif 2227#endif
2147 forward_result_message (msg, peer_info, msg_ctx); 2228 forward_result_message (msg, peer_info, msg_ctx);
2148 /* Try removing forward entries after sending once, only allows ONE response per request */ 2229 /* Try removing forward entries after sending once, only allows ONE response per request */
2149 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK) 2230 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
2150 { 2231 {
2151 GNUNET_SCHEDULER_cancel(pos->delete_task); 2232 GNUNET_SCHEDULER_cancel (pos->delete_task);
2152 pos->delete_task = GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos); 2233 pos->delete_task =
2234 GNUNET_SCHEDULER_add_now (&remove_forward_entry, pos);
2153 } 2235 }
2154 } 2236 }
2155 else 2237 else
2156 { 2238 {
2157#if DEBUG_DHT 2239#if DEBUG_DHT
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 "`%s:%s': NOT Forwarding response (bloom match) key %s uid %llu to peer %s\n", my_short_id, 2241 "`%s:%s': NOT Forwarding response (bloom match) key %s uid %llu to peer %s\n",
2160 "DHT", GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id, GNUNET_i2s(&peer_info->id)); 2242 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
2243 msg_ctx->unique_id, GNUNET_i2s (&peer_info->id));
2161#endif 2244#endif
2162 } 2245 }
2163 } 2246 }
2164 pos = pos->next; 2247 pos = pos->next;
2165 } 2248 }
2166 if (msg_ctx->bloom != NULL) 2249 if (msg_ctx->bloom != NULL)
2167 { 2250 {
2168 GNUNET_CONTAINER_bloomfilter_free(msg_ctx->bloom); 2251 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
2169 msg_ctx->bloom = NULL; 2252 msg_ctx->bloom = NULL;
2170 } 2253 }
2171 return 0; 2254 return 0;
2172} 2255}
2173 2256
@@ -2188,8 +2271,8 @@ static int
2188datacache_get_iterator (void *cls, 2271datacache_get_iterator (void *cls,
2189 struct GNUNET_TIME_Absolute exp, 2272 struct GNUNET_TIME_Absolute exp,
2190 const GNUNET_HashCode * key, 2273 const GNUNET_HashCode * key,
2191 size_t size, const char *data, 2274 size_t size, const char *data,
2192 enum GNUNET_BLOCK_Type type) 2275 enum GNUNET_BLOCK_Type type)
2193{ 2276{
2194 struct DHT_MessageContext *msg_ctx = cls; 2277 struct DHT_MessageContext *msg_ctx = cls;
2195 struct DHT_MessageContext *new_msg_ctx; 2278 struct DHT_MessageContext *new_msg_ctx;
@@ -2198,40 +2281,40 @@ datacache_get_iterator (void *cls,
2198 2281
2199#if DEBUG_DHT 2282#if DEBUG_DHT
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2201 "`%s:%s': Received `%s' response from datacache\n", my_short_id, "DHT", "GET"); 2284 "`%s:%s': Received `%s' response from datacache\n", my_short_id,
2202#endif 2285 "DHT", "GET");
2286#endif
2203 eval = GNUNET_BLOCK_evaluate (block_context, 2287 eval = GNUNET_BLOCK_evaluate (block_context,
2204 type, 2288 type,
2205 key, 2289 key,
2206 &msg_ctx->reply_bf, 2290 &msg_ctx->reply_bf,
2207 msg_ctx->reply_bf_mutator, 2291 msg_ctx->reply_bf_mutator,
2208 msg_ctx->xquery, 2292 msg_ctx->xquery,
2209 msg_ctx->xquery_size, 2293 msg_ctx->xquery_size, data, size);
2210 data,
2211 size);
2212 switch (eval) 2294 switch (eval)
2213 { 2295 {
2214 case GNUNET_BLOCK_EVALUATION_OK_LAST: 2296 case GNUNET_BLOCK_EVALUATION_OK_LAST:
2215 msg_ctx->do_forward = GNUNET_NO; 2297 msg_ctx->do_forward = GNUNET_NO;
2216 case GNUNET_BLOCK_EVALUATION_OK_MORE: 2298 case GNUNET_BLOCK_EVALUATION_OK_MORE:
2217 new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext)); 2299 new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
2218 memcpy(new_msg_ctx, msg_ctx, sizeof(struct DHT_MessageContext)); 2300 memcpy (new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2219 get_result = 2301 get_result =
2220 GNUNET_malloc (sizeof (struct GNUNET_DHT_GetResultMessage) + size); 2302 GNUNET_malloc (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
2221 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT); 2303 get_result->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET_RESULT);
2222 get_result->header.size = 2304 get_result->header.size =
2223 htons (sizeof (struct GNUNET_DHT_GetResultMessage) + size); 2305 htons (sizeof (struct GNUNET_DHT_GetResultMessage) + size);
2224 get_result->expiration = GNUNET_TIME_absolute_hton(exp); 2306 get_result->expiration = GNUNET_TIME_absolute_hton (exp);
2225 get_result->type = htons (type); 2307 get_result->type = htons (type);
2226 memcpy (&get_result[1], data, size); 2308 memcpy (&get_result[1], data, size);
2227 new_msg_ctx->peer = &my_identity; 2309 new_msg_ctx->peer = &my_identity;
2228 new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K); 2310 new_msg_ctx->bloom =
2311 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2229 new_msg_ctx->hop_count = 0; 2312 new_msg_ctx->hop_count = 0;
2230 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */ 2313 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */
2231 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT; 2314 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
2232 increment_stats(STAT_GET_RESPONSE_START); 2315 increment_stats (STAT_GET_RESPONSE_START);
2233 route_result_message(&get_result->header, new_msg_ctx); 2316 route_result_message (&get_result->header, new_msg_ctx);
2234 GNUNET_free(new_msg_ctx); 2317 GNUNET_free (new_msg_ctx);
2235 GNUNET_free (get_result); 2318 GNUNET_free (get_result);
2236 break; 2319 break;
2237 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE: 2320 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
@@ -2249,7 +2332,8 @@ datacache_get_iterator (void *cls,
2249 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID: 2332 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
2250#if DEBUG_DHT 2333#if DEBUG_DHT
2251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2252 "`%s:%s': Valid request, no results.\n", my_short_id, "DHT"); 2335 "`%s:%s': Valid request, no results.\n", my_short_id,
2336 "DHT");
2253#endif 2337#endif
2254 GNUNET_break (0); 2338 GNUNET_break (0);
2255 break; 2339 break;
@@ -2260,11 +2344,12 @@ datacache_get_iterator (void *cls,
2260 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED: 2344 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
2261#if DEBUG_DHT 2345#if DEBUG_DHT
2262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2263 "`%s:%s': Unsupported block type (%u) in response!\n", my_short_id, "DHT", type); 2347 "`%s:%s': Unsupported block type (%u) in response!\n",
2348 my_short_id, "DHT", type);
2264#endif 2349#endif
2265 /* msg_ctx->do_forward = GNUNET_NO; // not sure... */ 2350 /* msg_ctx->do_forward = GNUNET_NO; // not sure... */
2266 break; 2351 break;
2267 } 2352 }
2268 return GNUNET_OK; 2353 return GNUNET_OK;
2269} 2354}
2270 2355
@@ -2277,8 +2362,8 @@ datacache_get_iterator (void *cls,
2277 * @param msg_ctx the context containing all pertinent information about the message 2362 * @param msg_ctx the context containing all pertinent information about the message
2278 */ 2363 */
2279static void 2364static void
2280route_message(const struct GNUNET_MessageHeader *msg, 2365route_message (const struct GNUNET_MessageHeader *msg,
2281 struct DHT_MessageContext *msg_ctx); 2366 struct DHT_MessageContext *msg_ctx);
2282 2367
2283 2368
2284/** 2369/**
@@ -2310,20 +2395,21 @@ handle_dht_get (const struct GNUNET_MessageHeader *msg,
2310 get_msg = (const struct GNUNET_DHT_GetMessage *) msg; 2395 get_msg = (const struct GNUNET_DHT_GetMessage *) msg;
2311 bf_size = ntohs (get_msg->bf_size); 2396 bf_size = ntohs (get_msg->bf_size);
2312 msg_ctx->xquery_size = ntohs (get_msg->xquery_size); 2397 msg_ctx->xquery_size = ntohs (get_msg->xquery_size);
2313 msg_ctx->reply_bf_mutator = get_msg->bf_mutator; /* FIXME: ntohl? */ 2398 msg_ctx->reply_bf_mutator = get_msg->bf_mutator; /* FIXME: ntohl? */
2314 if (msize != sizeof (struct GNUNET_DHT_GetMessage) + bf_size + msg_ctx->xquery_size) 2399 if (msize !=
2400 sizeof (struct GNUNET_DHT_GetMessage) + bf_size + msg_ctx->xquery_size)
2315 { 2401 {
2316 GNUNET_break (0); 2402 GNUNET_break (0);
2317 return 0; 2403 return 0;
2318 } 2404 }
2319 end = (const char*) &get_msg[1]; 2405 end = (const char *) &get_msg[1];
2320 if (msg_ctx->xquery_size == 0) 2406 if (msg_ctx->xquery_size == 0)
2321 { 2407 {
2322 msg_ctx->xquery = NULL; 2408 msg_ctx->xquery = NULL;
2323 } 2409 }
2324 else 2410 else
2325 { 2411 {
2326 msg_ctx->xquery = (const void*) end; 2412 msg_ctx->xquery = (const void *) end;
2327 end += msg_ctx->xquery_size; 2413 end += msg_ctx->xquery_size;
2328 } 2414 }
2329 if (bf_size == 0) 2415 if (bf_size == 0)
@@ -2340,13 +2426,11 @@ handle_dht_get (const struct GNUNET_MessageHeader *msg,
2340#if DEBUG_DHT 2426#if DEBUG_DHT
2341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2342 "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n", 2428 "`%s:%s': Received `%s' request, message type %u, key %s, uid %llu\n",
2343 my_short_id, 2429 my_short_id,
2344 "DHT", "GET", 2430 "DHT", "GET",
2345 type, 2431 type, GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id);
2346 GNUNET_h2s (&msg_ctx->key),
2347 msg_ctx->unique_id);
2348#endif 2432#endif
2349 increment_stats(STAT_GETS); 2433 increment_stats (STAT_GETS);
2350 results = 0; 2434 results = 0;
2351#if HAVE_MALICIOUS 2435#if HAVE_MALICIOUS
2352 if (type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE) 2436 if (type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
@@ -2359,13 +2443,12 @@ handle_dht_get (const struct GNUNET_MessageHeader *msg,
2359 if (datacache != NULL) 2443 if (datacache != NULL)
2360 results 2444 results
2361 = GNUNET_DATACACHE_get (datacache, 2445 = GNUNET_DATACACHE_get (datacache,
2362 &msg_ctx->key, type, 2446 &msg_ctx->key, type,
2363 &datacache_get_iterator, 2447 &datacache_get_iterator, msg_ctx);
2364 msg_ctx);
2365#if DEBUG_DHT 2448#if DEBUG_DHT
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367 "`%s:%s': Found %d results for `%s' request uid %llu\n", my_short_id, "DHT", 2450 "`%s:%s': Found %d results for `%s' request uid %llu\n",
2368 results, "GET", msg_ctx->unique_id); 2451 my_short_id, "DHT", results, "GET", msg_ctx->unique_id);
2369#endif 2452#endif
2370 if (results >= 1) 2453 if (results >= 1)
2371 { 2454 {
@@ -2373,16 +2456,16 @@ handle_dht_get (const struct GNUNET_MessageHeader *msg,
2373 if ((debug_routes) && (dhtlog_handle != NULL)) 2456 if ((debug_routes) && (dhtlog_handle != NULL))
2374 { 2457 {
2375 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET, 2458 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
2376 msg_ctx->hop_count, GNUNET_YES, &my_identity, 2459 msg_ctx->hop_count, GNUNET_YES,
2377 &msg_ctx->key); 2460 &my_identity, &msg_ctx->key);
2378 } 2461 }
2379 2462
2380 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 2463 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2381 { 2464 {
2382 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE, 2465 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2383 msg_ctx->hop_count, GNUNET_YES, 2466 msg_ctx->hop_count, GNUNET_YES,
2384 &my_identity, &msg_ctx->key, msg_ctx->peer, 2467 &my_identity, &msg_ctx->key,
2385 NULL); 2468 msg_ctx->peer, NULL);
2386 } 2469 }
2387#endif 2470#endif
2388 } 2471 }
@@ -2390,29 +2473,28 @@ handle_dht_get (const struct GNUNET_MessageHeader *msg,
2390 { 2473 {
2391 /* check query valid */ 2474 /* check query valid */
2392 if (GNUNET_BLOCK_EVALUATION_REQUEST_INVALID 2475 if (GNUNET_BLOCK_EVALUATION_REQUEST_INVALID
2393 == GNUNET_BLOCK_evaluate (block_context, 2476 == GNUNET_BLOCK_evaluate (block_context,
2394 type, 2477 type,
2395 &msg_ctx->key, 2478 &msg_ctx->key,
2396 &msg_ctx->reply_bf, 2479 &msg_ctx->reply_bf,
2397 msg_ctx->reply_bf_mutator, 2480 msg_ctx->reply_bf_mutator,
2398 msg_ctx->xquery, 2481 msg_ctx->xquery,
2399 msg_ctx->xquery_size, 2482 msg_ctx->xquery_size, NULL, 0))
2400 NULL, 0)) 2483 {
2401 { 2484 GNUNET_break_op (0);
2402 GNUNET_break_op (0); 2485 msg_ctx->do_forward = GNUNET_NO;
2403 msg_ctx->do_forward = GNUNET_NO; 2486 }
2404 } 2487 }
2405 } 2488
2406 2489 if (msg_ctx->hop_count == 0) /* Locally initiated request */
2407 if (msg_ctx->hop_count == 0) /* Locally initiated request */
2408 { 2490 {
2409#if DEBUG_DHT_ROUTING 2491#if DEBUG_DHT_ROUTING
2410 if ((debug_routes) && (dhtlog_handle != NULL)) 2492 if ((debug_routes) && (dhtlog_handle != NULL))
2411 { 2493 {
2412 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET, 2494 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_GET,
2413 msg_ctx->hop_count, GNUNET_NO, &my_identity, 2495 msg_ctx->hop_count, GNUNET_NO,
2414 &msg_ctx->key); 2496 &my_identity, &msg_ctx->key);
2415 } 2497 }
2416#endif 2498#endif
2417 } 2499 }
2418 if (msg_ctx->do_forward == GNUNET_YES) 2500 if (msg_ctx->do_forward == GNUNET_YES)
@@ -2422,11 +2504,14 @@ handle_dht_get (const struct GNUNET_MessageHeader *msg,
2422} 2504}
2423 2505
2424static void 2506static void
2425remove_recent_find_peer(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 2507remove_recent_find_peer (void *cls,
2508 const struct GNUNET_SCHEDULER_TaskContext *tc)
2426{ 2509{
2427 GNUNET_HashCode *key = cls; 2510 GNUNET_HashCode *key = cls;
2428 2511
2429 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(recent_find_peer_requests, key, NULL)); 2512 GNUNET_assert (GNUNET_YES ==
2513 GNUNET_CONTAINER_multihashmap_remove
2514 (recent_find_peer_requests, key, NULL));
2430 GNUNET_free (key); 2515 GNUNET_free (key);
2431} 2516}
2432 2517
@@ -2452,40 +2537,49 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
2452 size_t other_hello_size; 2537 size_t other_hello_size;
2453 struct GNUNET_PeerIdentity peer_id; 2538 struct GNUNET_PeerIdentity peer_id;
2454 2539
2455 find_peer_message = (struct GNUNET_DHT_FindPeerMessage *)find_msg; 2540 find_peer_message = (struct GNUNET_DHT_FindPeerMessage *) find_msg;
2456 GNUNET_break_op(ntohs(find_msg->size) >= (sizeof(struct GNUNET_DHT_FindPeerMessage))); 2541 GNUNET_break_op (ntohs (find_msg->size) >=
2457 if (ntohs(find_msg->size) < sizeof(struct GNUNET_DHT_FindPeerMessage)) 2542 (sizeof (struct GNUNET_DHT_FindPeerMessage)));
2543 if (ntohs (find_msg->size) < sizeof (struct GNUNET_DHT_FindPeerMessage))
2458 return; 2544 return;
2459 other_hello = NULL; 2545 other_hello = NULL;
2460 other_hello_size = 0; 2546 other_hello_size = 0;
2461 if (ntohs(find_msg->size) > sizeof(struct GNUNET_DHT_FindPeerMessage)) 2547 if (ntohs (find_msg->size) > sizeof (struct GNUNET_DHT_FindPeerMessage))
2462 { 2548 {
2463 other_hello_size = ntohs(find_msg->size) - sizeof(struct GNUNET_DHT_FindPeerMessage); 2549 other_hello_size =
2464 other_hello = GNUNET_malloc(other_hello_size); 2550 ntohs (find_msg->size) - sizeof (struct GNUNET_DHT_FindPeerMessage);
2465 memcpy(other_hello, &find_peer_message[1], other_hello_size); 2551 other_hello = GNUNET_malloc (other_hello_size);
2466 if ((GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)other_hello) == 0) || (GNUNET_OK != GNUNET_HELLO_get_id((struct GNUNET_HELLO_Message *)other_hello, &peer_id))) 2552 memcpy (other_hello, &find_peer_message[1], other_hello_size);
2553 if ((GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) other_hello) ==
2554 0)
2555 || (GNUNET_OK !=
2556 GNUNET_HELLO_get_id ((struct GNUNET_HELLO_Message *)
2557 other_hello, &peer_id)))
2467 { 2558 {
2468 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Received invalid HELLO message in find peer request!\n"); 2559 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2469 GNUNET_free(other_hello); 2560 "Received invalid HELLO message in find peer request!\n");
2561 GNUNET_free (other_hello);
2470 return; 2562 return;
2471 } 2563 }
2472#if FIND_PEER_WITH_HELLO 2564#if FIND_PEER_WITH_HELLO
2473 if (GNUNET_YES == consider_peer(&peer_id)) 2565 if (GNUNET_YES == consider_peer (&peer_id))
2474 { 2566 {
2475 increment_stats(STAT_HELLOS_PROVIDED); 2567 increment_stats (STAT_HELLOS_PROVIDED);
2476 GNUNET_TRANSPORT_offer_hello(transport_handle, other_hello); 2568 GNUNET_TRANSPORT_offer_hello (transport_handle, other_hello);
2477 GNUNET_CORE_peer_request_connect(coreAPI, 2569 GNUNET_CORE_peer_request_connect (coreAPI,
2478 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5), &peer_id, NULL, NULL); 2570 GNUNET_TIME_relative_multiply
2479 route_message (find_msg, msg_ctx); 2571 (GNUNET_TIME_UNIT_SECONDS, 5),
2480 GNUNET_free (other_hello); 2572 &peer_id, NULL, NULL);
2573 route_message (find_msg, msg_ctx);
2574 GNUNET_free (other_hello);
2575 return;
2576 }
2577 else /* We don't want this peer! */
2578 {
2579 route_message (find_msg, msg_ctx);
2580 GNUNET_free (other_hello);
2481 return; 2581 return;
2482 } 2582 }
2483 else /* We don't want this peer! */
2484 {
2485 route_message (find_msg, msg_ctx);
2486 GNUNET_free (other_hello);
2487 return;
2488 }
2489#endif 2583#endif
2490 } 2584 }
2491 2585
@@ -2493,43 +2587,45 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 "`%s:%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n", 2588 "`%s:%s': Received `%s' request from client, key %s (msg size %d, we expected %d)\n",
2495 my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&msg_ctx->key), 2589 my_short_id, "DHT", "FIND PEER", GNUNET_h2s (&msg_ctx->key),
2496 ntohs (find_msg->size), 2590 ntohs (find_msg->size), sizeof (struct GNUNET_MessageHeader));
2497 sizeof (struct GNUNET_MessageHeader));
2498#endif 2591#endif
2499 if (my_hello == NULL) 2592 if (my_hello == NULL)
2500 { 2593 {
2501#if DEBUG_DHT 2594#if DEBUG_DHT
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "`%s': Our HELLO is null, can't return.\n", 2596 "`%s': Our HELLO is null, can't return.\n", "DHT");
2504 "DHT");
2505#endif 2597#endif
2506 GNUNET_free_non_null (other_hello); 2598 GNUNET_free_non_null (other_hello);
2507 route_message (find_msg, msg_ctx); 2599 route_message (find_msg, msg_ctx);
2508 return; 2600 return;
2509 } 2601 }
2510 2602
2511 incoming_bloom = GNUNET_CONTAINER_bloomfilter_init(find_peer_message->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K); 2603 incoming_bloom =
2512 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(incoming_bloom, &my_identity.hashPubKey)) 2604 GNUNET_CONTAINER_bloomfilter_init (find_peer_message->bloomfilter,
2605 DHT_BLOOM_SIZE, DHT_BLOOM_K);
2606 if (GNUNET_YES ==
2607 GNUNET_CONTAINER_bloomfilter_test (incoming_bloom,
2608 &my_identity.hashPubKey))
2513 { 2609 {
2514 increment_stats(STAT_BLOOM_FIND_PEER); 2610 increment_stats (STAT_BLOOM_FIND_PEER);
2515 GNUNET_CONTAINER_bloomfilter_free(incoming_bloom); 2611 GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
2516 GNUNET_free_non_null(other_hello); 2612 GNUNET_free_non_null (other_hello);
2517 route_message (find_msg, msg_ctx); 2613 route_message (find_msg, msg_ctx);
2518 return; /* We match the bloomfilter, do not send a response to this peer (they likely already know us!)*/ 2614 return; /* We match the bloomfilter, do not send a response to this peer (they likely already know us!) */
2519 } 2615 }
2520 GNUNET_CONTAINER_bloomfilter_free(incoming_bloom); 2616 GNUNET_CONTAINER_bloomfilter_free (incoming_bloom);
2521 2617
2522#if RESTRICT_FIND_PEER 2618#if RESTRICT_FIND_PEER
2523 2619
2524 /** 2620 /**
2525 * Ignore any find peer requests from a peer we have seen very recently. 2621 * Ignore any find peer requests from a peer we have seen very recently.
2526 */ 2622 */
2527 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(recent_find_peer_requests, &msg_ctx->key)) /* We have recently responded to a find peer request for this peer! */ 2623 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (recent_find_peer_requests, &msg_ctx->key)) /* We have recently responded to a find peer request for this peer! */
2528 { 2624 {
2529 increment_stats("# dht find peer requests ignored (recently seen!)"); 2625 increment_stats ("# dht find peer requests ignored (recently seen!)");
2530 GNUNET_free_non_null(other_hello); 2626 GNUNET_free_non_null (other_hello);
2531 return; 2627 return;
2532 } 2628 }
2533 2629
2534 /** 2630 /**
2535 * Use this check to only allow the peer to respond to find peer requests if 2631 * Use this check to only allow the peer to respond to find peer requests if
@@ -2539,41 +2635,46 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
2539 * the network and has no other peers this is a problem (assume all buckets 2635 * the network and has no other peers this is a problem (assume all buckets
2540 * full, no one will respond!). 2636 * full, no one will respond!).
2541 */ 2637 */
2542 memcpy(&peer_id.hashPubKey, &msg_ctx->key, sizeof(GNUNET_HashCode)); 2638 memcpy (&peer_id.hashPubKey, &msg_ctx->key, sizeof (GNUNET_HashCode));
2543 if (GNUNET_NO == consider_peer(&peer_id)) 2639 if (GNUNET_NO == consider_peer (&peer_id))
2544 { 2640 {
2545 increment_stats("# dht find peer requests ignored (do not need!)"); 2641 increment_stats ("# dht find peer requests ignored (do not need!)");
2546 GNUNET_free_non_null(other_hello); 2642 GNUNET_free_non_null (other_hello);
2547 route_message (find_msg, msg_ctx); 2643 route_message (find_msg, msg_ctx);
2548 return; 2644 return;
2549 } 2645 }
2550#endif 2646#endif
2551 2647
2552 recent_hash = GNUNET_malloc(sizeof(GNUNET_HashCode)); 2648 recent_hash = GNUNET_malloc (sizeof (GNUNET_HashCode));
2553 memcpy(recent_hash, &msg_ctx->key, sizeof(GNUNET_HashCode)); 2649 memcpy (recent_hash, &msg_ctx->key, sizeof (GNUNET_HashCode));
2554 if (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests, 2650 if (GNUNET_SYSERR !=
2555 &msg_ctx->key, NULL, 2651 GNUNET_CONTAINER_multihashmap_put (recent_find_peer_requests,
2556 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 2652 &msg_ctx->key, NULL,
2653 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2557 { 2654 {
2558 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Adding recent remove task for key `%s`!\n", GNUNET_h2s(&msg_ctx->key)); 2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2656 "Adding recent remove task for key `%s`!\n",
2657 GNUNET_h2s (&msg_ctx->key));
2559 /* Only add a task if there wasn't one for this key already! */ 2658 /* Only add a task if there wasn't one for this key already! */
2560 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30), 2659 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2660 (GNUNET_TIME_UNIT_SECONDS, 30),
2561 &remove_recent_find_peer, recent_hash); 2661 &remove_recent_find_peer, recent_hash);
2562 } 2662 }
2563 else 2663 else
2564 { 2664 {
2565 GNUNET_free(recent_hash); 2665 GNUNET_free (recent_hash);
2566 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received duplicate find peer request too soon!\n"); 2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 "Received duplicate find peer request too soon!\n");
2567 } 2668 }
2568 2669
2569 /* Simplistic find_peer functionality, always return our hello */ 2670 /* Simplistic find_peer functionality, always return our hello */
2570 hello_size = ntohs(my_hello->size); 2671 hello_size = ntohs (my_hello->size);
2571 tsize = hello_size + sizeof (struct GNUNET_MessageHeader); 2672 tsize = hello_size + sizeof (struct GNUNET_MessageHeader);
2572 2673
2573 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) 2674 if (tsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2574 { 2675 {
2575 GNUNET_break_op (0); 2676 GNUNET_break_op (0);
2576 GNUNET_free_non_null(other_hello); 2677 GNUNET_free_non_null (other_hello);
2577 return; 2678 return;
2578 } 2679 }
2579 2680
@@ -2586,26 +2687,27 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
2586 "`%s': Sending hello size %d to requesting peer.\n", 2687 "`%s': Sending hello size %d to requesting peer.\n",
2587 "DHT", hello_size); 2688 "DHT", hello_size);
2588 2689
2589 new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext)); 2690 new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
2590 memcpy(new_msg_ctx, msg_ctx, sizeof(struct DHT_MessageContext)); 2691 memcpy (new_msg_ctx, msg_ctx, sizeof (struct DHT_MessageContext));
2591 new_msg_ctx->peer = &my_identity; 2692 new_msg_ctx->peer = &my_identity;
2592 new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K); 2693 new_msg_ctx->bloom =
2694 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
2593 new_msg_ctx->hop_count = 0; 2695 new_msg_ctx->hop_count = 0;
2594 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make find peer requests a higher priority */ 2696 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make find peer requests a higher priority */
2595 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT; 2697 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
2596 increment_stats(STAT_FIND_PEER_ANSWER); 2698 increment_stats (STAT_FIND_PEER_ANSWER);
2597 route_result_message(find_peer_result, new_msg_ctx); 2699 route_result_message (find_peer_result, new_msg_ctx);
2598 GNUNET_free(new_msg_ctx); 2700 GNUNET_free (new_msg_ctx);
2599#if DEBUG_DHT_ROUTING 2701#if DEBUG_DHT_ROUTING
2600 if ((debug_routes) && (dhtlog_handle != NULL)) 2702 if ((debug_routes) && (dhtlog_handle != NULL))
2601 { 2703 {
2602 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER, 2704 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER,
2603 msg_ctx->hop_count, GNUNET_YES, &my_identity, 2705 msg_ctx->hop_count, GNUNET_YES,
2604 &msg_ctx->key); 2706 &my_identity, &msg_ctx->key);
2605 } 2707 }
2606#endif 2708#endif
2607 GNUNET_free_non_null(other_hello); 2709 GNUNET_free_non_null (other_hello);
2608 GNUNET_free(find_peer_result); 2710 GNUNET_free (find_peer_result);
2609 route_message (find_msg, msg_ctx); 2711 route_message (find_msg, msg_ctx);
2610} 2712}
2611 2713
@@ -2617,7 +2719,7 @@ handle_dht_find_peer (const struct GNUNET_MessageHeader *find_msg,
2617 * @param tc runtime context for this task 2719 * @param tc runtime context for this task
2618 */ 2720 */
2619static void 2721static void
2620republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); 2722republish_content (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2621 2723
2622/** 2724/**
2623 * Server handler for initiating local dht put requests 2725 * Server handler for initiating local dht put requests
@@ -2636,11 +2738,10 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
2636 struct RepublishContext *put_context; 2738 struct RepublishContext *put_context;
2637 GNUNET_HashCode key; 2739 GNUNET_HashCode key;
2638 2740
2639 GNUNET_assert (ntohs (msg->size) >= 2741 GNUNET_assert (ntohs (msg->size) >= sizeof (struct GNUNET_DHT_PutMessage));
2640 sizeof (struct GNUNET_DHT_PutMessage));
2641 2742
2642 2743
2643 put_msg = (const struct GNUNET_DHT_PutMessage *)msg; 2744 put_msg = (const struct GNUNET_DHT_PutMessage *) msg;
2644 put_type = (enum GNUNET_BLOCK_Type) ntohl (put_msg->type); 2745 put_type = (enum GNUNET_BLOCK_Type) ntohl (put_msg->type);
2645#if HAVE_MALICIOUS 2746#if HAVE_MALICIOUS
2646 if (put_type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE) 2747 if (put_type == GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE)
@@ -2651,19 +2752,18 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
2651 /** Log routes that die due to high load! */ 2752 /** Log routes that die due to high load! */
2652 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE, 2753 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2653 msg_ctx->hop_count, GNUNET_SYSERR, 2754 msg_ctx->hop_count, GNUNET_SYSERR,
2654 &my_identity, &msg_ctx->key, msg_ctx->peer, 2755 &my_identity, &msg_ctx->key,
2655 NULL); 2756 msg_ctx->peer, NULL);
2656 } 2757 }
2657#endif 2758#endif
2658 return; 2759 return;
2659 } 2760 }
2660#endif 2761#endif
2661 data_size = ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage); 2762 data_size =
2662 ret = GNUNET_BLOCK_get_key (block_context, 2763 ntohs (put_msg->header.size) - sizeof (struct GNUNET_DHT_PutMessage);
2663 put_type, 2764 ret =
2664 &put_msg[1], 2765 GNUNET_BLOCK_get_key (block_context, put_type, &put_msg[1], data_size,
2665 data_size, 2766 &key);
2666 &key);
2667 if (GNUNET_NO == ret) 2767 if (GNUNET_NO == ret)
2668 { 2768 {
2669#if DEBUG_DHT_ROUTING 2769#if DEBUG_DHT_ROUTING
@@ -2671,26 +2771,24 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
2671 { 2771 {
2672 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE, 2772 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2673 msg_ctx->hop_count, GNUNET_SYSERR, 2773 msg_ctx->hop_count, GNUNET_SYSERR,
2674 &my_identity, &msg_ctx->key, msg_ctx->peer, 2774 &my_identity, &msg_ctx->key,
2675 NULL); 2775 msg_ctx->peer, NULL);
2676 } 2776 }
2677#endif 2777#endif
2678 /* invalid reply */ 2778 /* invalid reply */
2679 GNUNET_break_op (0); 2779 GNUNET_break_op (0);
2680 return; 2780 return;
2681 } 2781 }
2682 if ( (GNUNET_YES == ret) && 2782 if ((GNUNET_YES == ret) &&
2683 (0 != memcmp (&key, 2783 (0 != memcmp (&key, &msg_ctx->key, sizeof (GNUNET_HashCode))))
2684 &msg_ctx->key,
2685 sizeof (GNUNET_HashCode))) )
2686 { 2784 {
2687#if DEBUG_DHT_ROUTING 2785#if DEBUG_DHT_ROUTING
2688 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 2786 if ((debug_routes_extended) && (dhtlog_handle != NULL))
2689 { 2787 {
2690 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE, 2788 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
2691 msg_ctx->hop_count, GNUNET_SYSERR, 2789 msg_ctx->hop_count, GNUNET_SYSERR,
2692 &my_identity, &msg_ctx->key, msg_ctx->peer, 2790 &my_identity, &msg_ctx->key,
2693 NULL); 2791 msg_ctx->peer, NULL);
2694 } 2792 }
2695#endif 2793#endif
2696 /* invalid wrapper: key mismatch! */ 2794 /* invalid wrapper: key mismatch! */
@@ -2702,16 +2800,17 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
2702#if DEBUG_DHT 2800#if DEBUG_DHT
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2704 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n", 2802 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2705 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id); 2803 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
2804 msg_ctx->unique_id);
2706#endif 2805#endif
2707#if DEBUG_DHT_ROUTING 2806#if DEBUG_DHT_ROUTING
2708 if (msg_ctx->hop_count == 0) /* Locally initiated request */ 2807 if (msg_ctx->hop_count == 0) /* Locally initiated request */
2709 { 2808 {
2710 if ((debug_routes) && (dhtlog_handle != NULL)) 2809 if ((debug_routes) && (dhtlog_handle != NULL))
2711 { 2810 {
2712 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT, 2811 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
2713 msg_ctx->hop_count, GNUNET_NO, &my_identity, 2812 msg_ctx->hop_count, GNUNET_NO,
2714 &msg_ctx->key); 2813 &my_identity, &msg_ctx->key);
2715 } 2814 }
2716 } 2815 }
2717#endif 2816#endif
@@ -2725,7 +2824,8 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
2725#if DEBUG_DHT 2824#if DEBUG_DHT
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2727 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n", 2826 "`%s:%s': Received `%s' request (inserting data!), message type %d, key %s, uid %llu\n",
2728 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id); 2827 my_short_id, "DHT", "PUT", put_type, GNUNET_h2s (&msg_ctx->key),
2828 msg_ctx->unique_id);
2729#endif 2829#endif
2730 2830
2731#if DEBUG_DHT_ROUTING 2831#if DEBUG_DHT_ROUTING
@@ -2740,24 +2840,26 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
2740 if ((debug_routes) && (dhtlog_handle != NULL)) 2840 if ((debug_routes) && (dhtlog_handle != NULL))
2741 { 2841 {
2742 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT, 2842 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_PUT,
2743 msg_ctx->hop_count, GNUNET_YES, &my_identity, 2843 msg_ctx->hop_count, GNUNET_YES,
2744 &msg_ctx->key); 2844 &my_identity, &msg_ctx->key);
2745 } 2845 }
2746#endif 2846#endif
2747 2847
2748 increment_stats(STAT_PUTS_INSERTED); 2848 increment_stats (STAT_PUTS_INSERTED);
2749 if (datacache != NULL) 2849 if (datacache != NULL)
2750 { 2850 {
2751 ret = GNUNET_DATACACHE_put (datacache, &msg_ctx->key, data_size, 2851 ret = GNUNET_DATACACHE_put (datacache, &msg_ctx->key, data_size,
2752 (char *) &put_msg[1], put_type, 2852 (char *) &put_msg[1], put_type,
2753 GNUNET_TIME_absolute_ntoh(put_msg->expiration)); 2853 GNUNET_TIME_absolute_ntoh
2854 (put_msg->expiration));
2754 2855
2755 if ((ret == GNUNET_YES) && (do_republish == GNUNET_YES)) 2856 if ((ret == GNUNET_YES) && (do_republish == GNUNET_YES))
2756 { 2857 {
2757 put_context = GNUNET_malloc(sizeof(struct RepublishContext)); 2858 put_context = GNUNET_malloc (sizeof (struct RepublishContext));
2758 memcpy(&put_context->key, &msg_ctx->key, sizeof(GNUNET_HashCode)); 2859 memcpy (&put_context->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
2759 put_context->type = put_type; 2860 put_context->type = put_type;
2760 GNUNET_SCHEDULER_add_delayed (dht_republish_frequency, &republish_content, put_context); 2861 GNUNET_SCHEDULER_add_delayed (dht_republish_frequency,
2862 &republish_content, put_context);
2761 } 2863 }
2762 } 2864 }
2763 else 2865 else
@@ -2783,7 +2885,8 @@ handle_dht_put (const struct GNUNET_MessageHeader *msg,
2783 * 2885 *
2784 * @return ballpark diameter figure 2886 * @return ballpark diameter figure
2785 */ 2887 */
2786static unsigned int estimate_diameter() 2888static unsigned int
2889estimate_diameter ()
2787{ 2890{
2788 return MAX_BUCKETS - lowest_bucket; 2891 return MAX_BUCKETS - lowest_bucket;
2789} 2892}
@@ -2836,26 +2939,30 @@ get_forward_count (unsigned int hop_count, size_t target_replication)
2836 { 2939 {
2837#if DEBUG_DHT 2940#if DEBUG_DHT
2838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2839 "`%s:%s': Hop count too high (est %d, lowest %d), NOT Forwarding request\n", my_short_id, 2942 "`%s:%s': Hop count too high (est %d, lowest %d), NOT Forwarding request\n",
2840 "DHT", estimate_diameter(), lowest_bucket); 2943 my_short_id, "DHT", estimate_diameter (), lowest_bucket);
2841#endif 2944#endif
2842 return 0; 2945 return 0;
2843 } 2946 }
2844 2947
2845 random_value = 0; 2948 random_value = 0;
2846 forward_count = 1; 2949 forward_count = 1;
2847 target_value = target_replication / (diameter + ((float)target_replication * hop_count)); 2950 target_value =
2951 target_replication / (diameter +
2952 ((float) target_replication * hop_count));
2848 if (target_value > 1) 2953 if (target_value > 1)
2849 { 2954 {
2850 /* Set forward count to floor of target_value */ 2955 /* Set forward count to floor of target_value */
2851 forward_count = (unsigned int)target_value; 2956 forward_count = (unsigned int) target_value;
2852 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */ 2957 /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */
2853 target_value = target_value - forward_count; 2958 target_value = target_value - forward_count;
2854 } 2959 }
2855 else 2960 else
2856 random_value = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, (unsigned int)-1); 2961 random_value =
2962 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
2963 (unsigned int) -1);
2857 2964
2858 if (random_value < (target_value * (unsigned int)-1)) 2965 if (random_value < (target_value * (unsigned int) -1))
2859 forward_count += 1; 2966 forward_count += 1;
2860 2967
2861 return forward_count; 2968 return forward_count;
@@ -2873,7 +2980,8 @@ get_forward_count (unsigned int hop_count, size_t target_replication)
2873 * otherwise. 2980 * otherwise.
2874 */ 2981 */
2875int 2982int
2876am_closest_peer (const GNUNET_HashCode * target, struct GNUNET_CONTAINER_BloomFilter *bloom) 2983am_closest_peer (const GNUNET_HashCode * target,
2984 struct GNUNET_CONTAINER_BloomFilter *bloom)
2877{ 2985{
2878 int bits; 2986 int bits;
2879 int other_bits; 2987 int other_bits;
@@ -2882,31 +2990,34 @@ am_closest_peer (const GNUNET_HashCode * target, struct GNUNET_CONTAINER_BloomFi
2882 struct PeerInfo *pos; 2990 struct PeerInfo *pos;
2883 unsigned int my_distance; 2991 unsigned int my_distance;
2884 2992
2885 if (0 == memcmp(&my_identity.hashPubKey, target, sizeof(GNUNET_HashCode))) 2993 if (0 == memcmp (&my_identity.hashPubKey, target, sizeof (GNUNET_HashCode)))
2886 return GNUNET_YES; 2994 return GNUNET_YES;
2887 2995
2888 bucket_num = find_current_bucket(target); 2996 bucket_num = find_current_bucket (target);
2889 2997
2890 bits = GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, target); 2998 bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey, target);
2891 my_distance = distance(&my_identity.hashPubKey, target); 2999 my_distance = distance (&my_identity.hashPubKey, target);
2892 pos = k_buckets[bucket_num].head; 3000 pos = k_buckets[bucket_num].head;
2893 count = 0; 3001 count = 0;
2894 while ((pos != NULL) && (count < bucket_size)) 3002 while ((pos != NULL) && (count < bucket_size))
2895 { 3003 {
2896 if ((bloom != NULL) && (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(bloom, &pos->id.hashPubKey))) 3004 if ((bloom != NULL)
3005 && (GNUNET_YES ==
3006 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)))
2897 { 3007 {
2898 pos = pos->next; 3008 pos = pos->next;
2899 continue; /* Skip already checked entries */ 3009 continue; /* Skip already checked entries */
2900 } 3010 }
2901 3011
2902 other_bits = GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target); 3012 other_bits =
3013 GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, target);
2903 if (other_bits > bits) 3014 if (other_bits > bits)
2904 return GNUNET_NO; 3015 return GNUNET_NO;
2905 else if (other_bits == bits) /* We match the same number of bits */ 3016 else if (other_bits == bits) /* We match the same number of bits */
2906 { 3017 {
2907 if (strict_kademlia != GNUNET_YES) /* Return that we at as close as any other peer */ 3018 if (strict_kademlia != GNUNET_YES) /* Return that we at as close as any other peer */
2908 return GNUNET_YES; 3019 return GNUNET_YES;
2909 else if (distance(&pos->id.hashPubKey, target) < my_distance) /* Check all known peers, only return if we are the true closest */ 3020 else if (distance (&pos->id.hashPubKey, target) < my_distance) /* Check all known peers, only return if we are the true closest */
2910 return GNUNET_NO; 3021 return GNUNET_NO;
2911 } 3022 }
2912 pos = pos->next; 3023 pos = pos->next;
@@ -2931,13 +3042,12 @@ am_closest_peer (const GNUNET_HashCode * target, struct GNUNET_CONTAINER_BloomFi
2931 * 3042 *
2932 */ 3043 */
2933static unsigned long long 3044static unsigned long long
2934converge_distance (const GNUNET_HashCode *target, 3045converge_distance (const GNUNET_HashCode * target,
2935 struct PeerInfo *peer, 3046 struct PeerInfo *peer, unsigned int hops)
2936 unsigned int hops)
2937{ 3047{
2938 unsigned long long ret; 3048 unsigned long long ret;
2939 unsigned int other_matching_bits; 3049 unsigned int other_matching_bits;
2940 double base_converge_modifier = .1; /* Value that "looks" good (when plotted), have to start somewhere */ 3050 double base_converge_modifier = .1; /* Value that "looks" good (when plotted), have to start somewhere */
2941 double temp_modifier; 3051 double temp_modifier;
2942 double calc_value; 3052 double calc_value;
2943 double exponent; 3053 double exponent;
@@ -2946,7 +3056,7 @@ converge_distance (const GNUNET_HashCode *target,
2946 if (use_max_hops) 3056 if (use_max_hops)
2947 curr_max_hops = max_hops; 3057 curr_max_hops = max_hops;
2948 else 3058 else
2949 curr_max_hops = (estimate_diameter() + 1) * 2; 3059 curr_max_hops = (estimate_diameter () + 1) * 2;
2950 3060
2951 if (converge_modifier > 0) 3061 if (converge_modifier > 0)
2952 temp_modifier = converge_modifier * base_converge_modifier; 3062 temp_modifier = converge_modifier * base_converge_modifier;
@@ -2956,33 +3066,37 @@ converge_distance (const GNUNET_HashCode *target,
2956 base_converge_modifier = 0.0; 3066 base_converge_modifier = 0.0;
2957 } 3067 }
2958 3068
2959 GNUNET_assert(temp_modifier > 0); 3069 GNUNET_assert (temp_modifier > 0);
2960 3070
2961 other_matching_bits = GNUNET_CRYPTO_hash_matching_bits(target, &peer->id.hashPubKey); 3071 other_matching_bits =
3072 GNUNET_CRYPTO_hash_matching_bits (target, &peer->id.hashPubKey);
2962 3073
2963 switch (converge_option) 3074 switch (converge_option)
2964 { 3075 {
2965 case DHT_CONVERGE_RANDOM: 3076 case DHT_CONVERGE_RANDOM:
2966 return 1; /* Always return 1, choose equally among all peers */ 3077 return 1; /* Always return 1, choose equally among all peers */
2967 case DHT_CONVERGE_LINEAR: 3078 case DHT_CONVERGE_LINEAR:
2968 calc_value = hops * curr_max_hops * temp_modifier; 3079 calc_value = hops * curr_max_hops * temp_modifier;
2969 break; 3080 break;
2970 case DHT_CONVERGE_SQUARE: 3081 case DHT_CONVERGE_SQUARE:
2971 /** 3082 /**
2972 * Simple square based curve. 3083 * Simple square based curve.
2973 */ 3084 */
2974 calc_value = (sqrt(hops) / sqrt(curr_max_hops)) * (curr_max_hops / (curr_max_hops * temp_modifier)); 3085 calc_value =
2975 break; 3086 (sqrt (hops) / sqrt (curr_max_hops)) * (curr_max_hops /
2976 case DHT_CONVERGE_EXPONENTIAL: 3087 (curr_max_hops *
3088 temp_modifier));
3089 break;
3090 case DHT_CONVERGE_EXPONENTIAL:
2977 /** 3091 /**
2978 * Simple exponential curve. 3092 * Simple exponential curve.
2979 */ 3093 */
2980 if (base_converge_modifier > 0) 3094 if (base_converge_modifier > 0)
2981 calc_value = (temp_modifier * hops * hops) / curr_max_hops; 3095 calc_value = (temp_modifier * hops * hops) / curr_max_hops;
2982 else 3096 else
2983 calc_value = (hops * hops) / curr_max_hops; 3097 calc_value = (hops * hops) / curr_max_hops;
2984 break; 3098 break;
2985 case DHT_CONVERGE_BINARY: 3099 case DHT_CONVERGE_BINARY:
2986 /** 3100 /**
2987 * If below the cutoff, route randomly (return 1), 3101 * If below the cutoff, route randomly (return 1),
2988 * If above the cutoff, return the maximum possible 3102 * If above the cutoff, return the maximum possible
@@ -2990,17 +3104,17 @@ converge_distance (const GNUNET_HashCode *target,
2990 * they are sorted.) 3104 * they are sorted.)
2991 */ 3105 */
2992 3106
2993 if (hops >= converge_modifier) /* Past cutoff */ 3107 if (hops >= converge_modifier) /* Past cutoff */
2994 { 3108 {
2995 return ULLONG_MAX; 3109 return ULLONG_MAX;
2996 } 3110 }
2997 /* Fall through */ 3111 /* Fall through */
2998 default: 3112 default:
2999 return 1; 3113 return 1;
3000 } 3114 }
3001 3115
3002 /* Take the log (base e) of the number of bits matching the other peer */ 3116 /* Take the log (base e) of the number of bits matching the other peer */
3003 exponent = log(other_matching_bits); 3117 exponent = log (other_matching_bits);
3004 3118
3005 /* Check if we would overflow; our largest possible value is 2^64 approx. e^44.361419555836498 */ 3119 /* Check if we would overflow; our largest possible value is 2^64 approx. e^44.361419555836498 */
3006 if (exponent * calc_value >= 44.361419555836498) 3120 if (exponent * calc_value >= 44.361419555836498)
@@ -3008,17 +3122,17 @@ converge_distance (const GNUNET_HashCode *target,
3008 3122
3009 /* Clear errno and all math exceptions */ 3123 /* Clear errno and all math exceptions */
3010 errno = 0; 3124 errno = 0;
3011 feclearexcept(FE_ALL_EXCEPT); 3125 feclearexcept (FE_ALL_EXCEPT);
3012 ret = (unsigned long long)pow(other_matching_bits, calc_value); 3126 ret = (unsigned long long) pow (other_matching_bits, calc_value);
3013 if ((errno != 0) || fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | 3127 if ((errno != 0) || fetestexcept (FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
3014 FE_UNDERFLOW)) 3128 FE_UNDERFLOW))
3015 { 3129 {
3016 if (0 != fetestexcept(FE_OVERFLOW)) 3130 if (0 != fetestexcept (FE_OVERFLOW))
3017 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_OVERFLOW\n"); 3131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "FE_OVERFLOW\n");
3018 if (0 != fetestexcept(FE_INVALID)) 3132 if (0 != fetestexcept (FE_INVALID))
3019 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_INVALID\n"); 3133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "FE_INVALID\n");
3020 if (0 != fetestexcept(FE_UNDERFLOW)) 3134 if (0 != fetestexcept (FE_UNDERFLOW))
3021 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "FE_UNDERFLOW\n"); 3135 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "FE_UNDERFLOW\n");
3022 return 0; 3136 return 0;
3023 } 3137 }
3024 else 3138 else
@@ -3040,8 +3154,8 @@ converge_distance (const GNUNET_HashCode *target,
3040static int 3154static int
3041compare_peers (const void *p1, const void *p2) 3155compare_peers (const void *p1, const void *p2)
3042{ 3156{
3043 struct PeerInfo **first = (struct PeerInfo **)p1; 3157 struct PeerInfo **first = (struct PeerInfo **) p1;
3044 struct PeerInfo **second = (struct PeerInfo **)p2; 3158 struct PeerInfo **second = (struct PeerInfo **) p2;
3045 3159
3046 if ((*first)->matching_bits > (*second)->matching_bits) 3160 if ((*first)->matching_bits > (*second)->matching_bits)
3047 return -1; 3161 return -1;
@@ -3090,12 +3204,14 @@ select_peer (const GNUNET_HashCode * target,
3090 unsigned int largest_distance; 3204 unsigned int largest_distance;
3091 struct PeerInfo *chosen; 3205 struct PeerInfo *chosen;
3092 3206
3093 my_matching_bits = GNUNET_CRYPTO_hash_matching_bits(target, &my_identity.hashPubKey); 3207 my_matching_bits =
3208 GNUNET_CRYPTO_hash_matching_bits (target, &my_identity.hashPubKey);
3094 3209
3095 total_distance = 0; 3210 total_distance = 0;
3096 /** If we are doing kademlia routing, or converge is binary (saves some cycles) */ 3211 /** If we are doing kademlia routing, or converge is binary (saves some cycles) */
3097 if ((strict_kademlia == GNUNET_YES) || 3212 if ((strict_kademlia == GNUNET_YES) ||
3098 ((converge_option == DHT_CONVERGE_BINARY) && (hops >= converge_modifier))) 3213 ((converge_option == DHT_CONVERGE_BINARY)
3214 && (hops >= converge_modifier)))
3099 { 3215 {
3100 largest_distance = 0; 3216 largest_distance = 0;
3101 chosen = NULL; 3217 chosen = NULL;
@@ -3106,7 +3222,9 @@ select_peer (const GNUNET_HashCode * target,
3106 while ((pos != NULL) && (count < bucket_size)) 3222 while ((pos != NULL) && (count < bucket_size))
3107 { 3223 {
3108 /* If we are doing strict Kademlia routing, then checking the bloomfilter is basically cheating! */ 3224 /* If we are doing strict Kademlia routing, then checking the bloomfilter is basically cheating! */
3109 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) 3225 if (GNUNET_NO ==
3226 GNUNET_CONTAINER_bloomfilter_test (bloom,
3227 &pos->id.hashPubKey))
3110 { 3228 {
3111 distance = inverse_distance (target, &pos->id.hashPubKey); 3229 distance = inverse_distance (target, &pos->id.hashPubKey);
3112 if (distance > largest_distance) 3230 if (distance > largest_distance)
@@ -3122,7 +3240,7 @@ select_peer (const GNUNET_HashCode * target,
3122 3240
3123 if ((largest_distance > 0) && (chosen != NULL)) 3241 if ((largest_distance > 0) && (chosen != NULL))
3124 { 3242 {
3125 GNUNET_CONTAINER_bloomfilter_add(bloom, &chosen->id.hashPubKey); 3243 GNUNET_CONTAINER_bloomfilter_add (bloom, &chosen->id.hashPubKey);
3126 return chosen; 3244 return chosen;
3127 } 3245 }
3128 else 3246 else
@@ -3138,22 +3256,24 @@ select_peer (const GNUNET_HashCode * target,
3138 * Then go over all HIGHER buckets (matching less then we do) 3256 * Then go over all HIGHER buckets (matching less then we do)
3139 */ 3257 */
3140 3258
3141 closest_bucket = find_current_bucket(target); 3259 closest_bucket = find_current_bucket (target);
3142 GNUNET_assert(closest_bucket >= lowest_bucket); 3260 GNUNET_assert (closest_bucket >= lowest_bucket);
3143 pos = k_buckets[closest_bucket].head; 3261 pos = k_buckets[closest_bucket].head;
3144 count = 0; 3262 count = 0;
3145 offset = 0; /* Need offset as well as count in case peers are bloomfiltered */ 3263 offset = 0; /* Need offset as well as count in case peers are bloomfiltered */
3146 memset(sorted_closest, 0, sizeof(sorted_closest)); 3264 memset (sorted_closest, 0, sizeof (sorted_closest));
3147 /* Put any peers in the closest bucket in the sorting array */ 3265 /* Put any peers in the closest bucket in the sorting array */
3148 while ((pos != NULL) && (count < bucket_size)) 3266 while ((pos != NULL) && (count < bucket_size))
3149 { 3267 {
3150 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) 3268 if (GNUNET_YES ==
3269 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3151 { 3270 {
3152 count++; 3271 count++;
3153 pos = pos->next; 3272 pos = pos->next;
3154 continue; /* Ignore bloomfiltered peers */ 3273 continue; /* Ignore bloomfiltered peers */
3155 } 3274 }
3156 pos->matching_bits = GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target); 3275 pos->matching_bits =
3276 GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey, target);
3157 sorted_closest[offset] = pos; 3277 sorted_closest[offset] = pos;
3158 pos = pos->next; 3278 pos = pos->next;
3159 offset++; 3279 offset++;
@@ -3161,18 +3281,23 @@ select_peer (const GNUNET_HashCode * target,
3161 } 3281 }
3162 3282
3163 /* Sort the peers in descending order */ 3283 /* Sort the peers in descending order */
3164 qsort(&sorted_closest[0], offset, sizeof(struct PeerInfo *), &compare_peers); 3284 qsort (&sorted_closest[0], offset, sizeof (struct PeerInfo *),
3285 &compare_peers);
3165 3286
3166 /* Put the sorted closest peers into the possible bins first, in case of overflow. */ 3287 /* Put the sorted closest peers into the possible bins first, in case of overflow. */
3167 for (i = 0; i < offset; i++) 3288 for (i = 0; i < offset; i++)
3168 { 3289 {
3169 temp_converge_distance = converge_distance(target, sorted_closest[i], hops); 3290 temp_converge_distance =
3170 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey)) 3291 converge_distance (target, sorted_closest[i], hops);
3171 break; /* Ignore bloomfiltered peers */ 3292 if (GNUNET_YES ==
3293 GNUNET_CONTAINER_bloomfilter_test (bloom,
3294 &sorted_closest[i]->id.
3295 hashPubKey))
3296 break; /* Ignore bloomfiltered peers */
3172 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */ 3297 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
3173 total_distance += temp_converge_distance; 3298 total_distance += temp_converge_distance;
3174 else 3299 else
3175 break; /* overflow case */ 3300 break; /* overflow case */
3176 } 3301 }
3177 3302
3178 /* Now handle peers in lower buckets (matches same # of bits as target) */ 3303 /* Now handle peers in lower buckets (matches same # of bits as target) */
@@ -3182,17 +3307,18 @@ select_peer (const GNUNET_HashCode * target,
3182 count = 0; 3307 count = 0;
3183 while ((pos != NULL) && (count < bucket_size)) 3308 while ((pos != NULL) && (count < bucket_size))
3184 { 3309 {
3185 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) 3310 if (GNUNET_YES ==
3311 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3186 { 3312 {
3187 count++; 3313 count++;
3188 pos = pos->next; 3314 pos = pos->next;
3189 continue; /* Ignore bloomfiltered peers */ 3315 continue; /* Ignore bloomfiltered peers */
3190 } 3316 }
3191 temp_converge_distance = converge_distance(target, pos, hops); 3317 temp_converge_distance = converge_distance (target, pos, hops);
3192 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */ 3318 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
3193 total_distance += temp_converge_distance; 3319 total_distance += temp_converge_distance;
3194 else 3320 else
3195 break; /* overflow case */ 3321 break; /* overflow case */
3196 pos = pos->next; 3322 pos = pos->next;
3197 count++; 3323 count++;
3198 } 3324 }
@@ -3205,25 +3331,26 @@ select_peer (const GNUNET_HashCode * target,
3205 count = 0; 3331 count = 0;
3206 while ((pos != NULL) && (count < bucket_size)) 3332 while ((pos != NULL) && (count < bucket_size))
3207 { 3333 {
3208 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) 3334 if (GNUNET_YES ==
3335 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3209 { 3336 {
3210 count++; 3337 count++;
3211 pos = pos->next; 3338 pos = pos->next;
3212 continue; /* Ignore bloomfiltered peers */ 3339 continue; /* Ignore bloomfiltered peers */
3213 } 3340 }
3214 temp_converge_distance = converge_distance(target, pos, hops); 3341 temp_converge_distance = converge_distance (target, pos, hops);
3215 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */ 3342 if ((temp_converge_distance <= ULLONG_MAX) && (total_distance + temp_converge_distance > total_distance)) /* Handle largest case and overflow */
3216 total_distance += temp_converge_distance; 3343 total_distance += temp_converge_distance;
3217 else 3344 else
3218 break; /* overflow case */ 3345 break; /* overflow case */
3219 pos = pos->next; 3346 pos = pos->next;
3220 count++; 3347 count++;
3221 } 3348 }
3222 } 3349 }
3223 3350
3224 if (total_distance == 0) /* No peers to select from! */ 3351 if (total_distance == 0) /* No peers to select from! */
3225 { 3352 {
3226 increment_stats("# failed to select peer"); 3353 increment_stats ("# failed to select peer");
3227 return NULL; 3354 return NULL;
3228 } 3355 }
3229 3356
@@ -3234,14 +3361,26 @@ select_peer (const GNUNET_HashCode * target,
3234 stats_total_distance = 0; 3361 stats_total_distance = 0;
3235 for (i = 0; i < offset; i++) 3362 for (i = 0; i < offset; i++)
3236 { 3363 {
3237 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey)) 3364 if (GNUNET_YES ==
3238 break; /* Ignore bloomfiltered peers */ 3365 GNUNET_CONTAINER_bloomfilter_test (bloom,
3239 temp_converge_distance = converge_distance(target, sorted_closest[i], hops); 3366 &sorted_closest[i]->id.
3240 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */ 3367 hashPubKey))
3368 break; /* Ignore bloomfiltered peers */
3369 temp_converge_distance =
3370 converge_distance (target, sorted_closest[i], hops);
3371 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
3241 stats_total_distance += temp_converge_distance; 3372 stats_total_distance += temp_converge_distance;
3242 else 3373 else
3243 break; /* overflow case */ 3374 break; /* overflow case */
3244 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&sorted_closest[i]->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&sorted_closest[i]->id.hashPubKey, &my_identity.hashPubKey), (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance); 3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3376 "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n",
3377 GNUNET_CRYPTO_hash_matching_bits (&sorted_closest[i]->id.
3378 hashPubKey, target),
3379 GNUNET_CRYPTO_hash_matching_bits (&sorted_closest[i]->id.
3380 hashPubKey,
3381 &my_identity.hashPubKey),
3382 (temp_converge_distance / (double) total_distance) * 100,
3383 temp_converge_distance);
3245 } 3384 }
3246 3385
3247 /* Now handle peers in lower buckets (matches same # of bits as target) */ 3386 /* Now handle peers in lower buckets (matches same # of bits as target) */
@@ -3251,18 +3390,27 @@ select_peer (const GNUNET_HashCode * target,
3251 count = 0; 3390 count = 0;
3252 while ((pos != NULL) && (count < bucket_size)) 3391 while ((pos != NULL) && (count < bucket_size))
3253 { 3392 {
3254 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) 3393 if (GNUNET_YES ==
3394 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3255 { 3395 {
3256 count++; 3396 count++;
3257 pos = pos->next; 3397 pos = pos->next;
3258 continue; /* Ignore bloomfiltered peers */ 3398 continue; /* Ignore bloomfiltered peers */
3259 } 3399 }
3260 temp_converge_distance = converge_distance(target, pos, hops); 3400 temp_converge_distance = converge_distance (target, pos, hops);
3261 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */ 3401 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
3262 stats_total_distance += temp_converge_distance; 3402 stats_total_distance += temp_converge_distance;
3263 else 3403 else
3264 break; /* overflow case */ 3404 break; /* overflow case */
3265 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey), (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance); 3405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3406 "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n",
3407 GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
3408 target),
3409 GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
3410 &my_identity.
3411 hashPubKey),
3412 (temp_converge_distance / (double) total_distance) *
3413 100, temp_converge_distance);
3266 pos = pos->next; 3414 pos = pos->next;
3267 count++; 3415 count++;
3268 } 3416 }
@@ -3275,18 +3423,27 @@ select_peer (const GNUNET_HashCode * target,
3275 count = 0; 3423 count = 0;
3276 while ((pos != NULL) && (count < bucket_size)) 3424 while ((pos != NULL) && (count < bucket_size))
3277 { 3425 {
3278 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) 3426 if (GNUNET_YES ==
3427 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3279 { 3428 {
3280 count++; 3429 count++;
3281 pos = pos->next; 3430 pos = pos->next;
3282 continue; /* Ignore bloomfiltered peers */ 3431 continue; /* Ignore bloomfiltered peers */
3283 } 3432 }
3284 temp_converge_distance = converge_distance(target, pos, hops); 3433 temp_converge_distance = converge_distance (target, pos, hops);
3285 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */ 3434 if ((temp_converge_distance <= ULLONG_MAX) && (stats_total_distance + temp_converge_distance > stats_total_distance)) /* Handle largest case and overflow */
3286 stats_total_distance += temp_converge_distance; 3435 stats_total_distance += temp_converge_distance;
3287 else 3436 else
3288 break; /* overflow case */ 3437 break; /* overflow case */
3289 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n", GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, target), GNUNET_CRYPTO_hash_matching_bits(&pos->id.hashPubKey, &my_identity.hashPubKey), (temp_converge_distance / (double)total_distance) * 100, temp_converge_distance); 3438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3439 "Choose %d matching bits (%d bits match me) (%.2f percent) converge ret %llu\n",
3440 GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
3441 target),
3442 GNUNET_CRYPTO_hash_matching_bits (&pos->id.hashPubKey,
3443 &my_identity.
3444 hashPubKey),
3445 (temp_converge_distance / (double) total_distance) *
3446 100, temp_converge_distance);
3290 pos = pos->next; 3447 pos = pos->next;
3291 count++; 3448 count++;
3292 } 3449 }
@@ -3295,14 +3452,19 @@ select_peer (const GNUNET_HashCode * target,
3295#endif 3452#endif
3296 3453
3297 /* Now actually choose a peer */ 3454 /* Now actually choose a peer */
3298 selected = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, total_distance); 3455 selected =
3456 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, total_distance);
3299 3457
3300 /* Go over closest sorted peers. */ 3458 /* Go over closest sorted peers. */
3301 for (i = 0; i < offset; i++) 3459 for (i = 0; i < offset; i++)
3302 { 3460 {
3303 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &sorted_closest[i]->id.hashPubKey)) 3461 if (GNUNET_YES ==
3304 break; /* Ignore bloomfiltered peers */ 3462 GNUNET_CONTAINER_bloomfilter_test (bloom,
3305 temp_converge_distance = converge_distance(target, sorted_closest[i], hops); 3463 &sorted_closest[i]->id.
3464 hashPubKey))
3465 break; /* Ignore bloomfiltered peers */
3466 temp_converge_distance =
3467 converge_distance (target, sorted_closest[i], hops);
3306 if (temp_converge_distance >= selected) 3468 if (temp_converge_distance >= selected)
3307 return sorted_closest[i]; 3469 return sorted_closest[i];
3308 else 3470 else
@@ -3316,13 +3478,14 @@ select_peer (const GNUNET_HashCode * target,
3316 count = 0; 3478 count = 0;
3317 while ((pos != NULL) && (count < bucket_size)) 3479 while ((pos != NULL) && (count < bucket_size))
3318 { 3480 {
3319 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) 3481 if (GNUNET_YES ==
3482 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3320 { 3483 {
3321 count++; 3484 count++;
3322 pos = pos->next; 3485 pos = pos->next;
3323 continue; /* Ignore bloomfiltered peers */ 3486 continue; /* Ignore bloomfiltered peers */
3324 } 3487 }
3325 temp_converge_distance = converge_distance(target, pos, hops); 3488 temp_converge_distance = converge_distance (target, pos, hops);
3326 if (temp_converge_distance >= selected) 3489 if (temp_converge_distance >= selected)
3327 return pos; 3490 return pos;
3328 else 3491 else
@@ -3339,13 +3502,14 @@ select_peer (const GNUNET_HashCode * target,
3339 count = 0; 3502 count = 0;
3340 while ((pos != NULL) && (count < bucket_size)) 3503 while ((pos != NULL) && (count < bucket_size))
3341 { 3504 {
3342 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey)) 3505 if (GNUNET_YES ==
3506 GNUNET_CONTAINER_bloomfilter_test (bloom, &pos->id.hashPubKey))
3343 { 3507 {
3344 count++; 3508 count++;
3345 pos = pos->next; 3509 pos = pos->next;
3346 continue; /* Ignore bloomfiltered peers */ 3510 continue; /* Ignore bloomfiltered peers */
3347 } 3511 }
3348 temp_converge_distance = converge_distance(target, pos, hops); 3512 temp_converge_distance = converge_distance (target, pos, hops);
3349 if (temp_converge_distance >= selected) 3513 if (temp_converge_distance >= selected)
3350 return pos; 3514 return pos;
3351 else 3515 else
@@ -3355,7 +3519,7 @@ select_peer (const GNUNET_HashCode * target,
3355 } 3519 }
3356 } 3520 }
3357 3521
3358 increment_stats("# failed to select peer"); 3522 increment_stats ("# failed to select peer");
3359 return NULL; 3523 return NULL;
3360} 3524}
3361 3525
@@ -3373,20 +3537,22 @@ remove_recent (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3373 struct RecentRequest *req = cls; 3537 struct RecentRequest *req = cls;
3374 static GNUNET_HashCode hash; 3538 static GNUNET_HashCode hash;
3375 3539
3376 GNUNET_assert(req != NULL); 3540 GNUNET_assert (req != NULL);
3377 hash_from_uid(req->uid, &hash); 3541 hash_from_uid (req->uid, &hash);
3378 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(recent.hashmap, &hash, req)); 3542 GNUNET_assert (GNUNET_YES ==
3379 GNUNET_CONTAINER_heap_remove_node(recent.minHeap, req->heap_node); 3543 GNUNET_CONTAINER_multihashmap_remove (recent.hashmap, &hash,
3380 GNUNET_CONTAINER_bloomfilter_free(req->bloom); 3544 req));
3381 GNUNET_free(req); 3545 GNUNET_CONTAINER_heap_remove_node (recent.minHeap, req->heap_node);
3546 GNUNET_CONTAINER_bloomfilter_free (req->bloom);
3547 GNUNET_free (req);
3382 3548
3383 /* 3549 /*
3384 if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) && (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)) && (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap))) 3550 if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) && (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)) && (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap)))
3385 { 3551 {
3386 GNUNET_CONTAINER_multihashmap_destroy(recent.hashmap); 3552 GNUNET_CONTAINER_multihashmap_destroy(recent.hashmap);
3387 GNUNET_CONTAINER_heap_destroy(recent.minHeap); 3553 GNUNET_CONTAINER_heap_destroy(recent.minHeap);
3388 } 3554 }
3389 */ 3555 */
3390} 3556}
3391 3557
3392/** 3558/**
@@ -3398,7 +3564,8 @@ remove_recent (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3398 * 3564 *
3399 * @return GNUNET_YES if this response was cached, GNUNET_NO if not 3565 * @return GNUNET_YES if this response was cached, GNUNET_NO if not
3400 */ 3566 */
3401static int cache_response(struct DHT_MessageContext *msg_ctx) 3567static int
3568cache_response (struct DHT_MessageContext *msg_ctx)
3402{ 3569{
3403 struct DHTQueryRecord *record; 3570 struct DHTQueryRecord *record;
3404 struct DHTRouteSource *source_info; 3571 struct DHTRouteSource *source_info;
@@ -3412,66 +3579,84 @@ static int cache_response(struct DHT_MessageContext *msg_ctx)
3412 while (current_size >= MAX_OUTSTANDING_FORWARDS) 3579 while (current_size >= MAX_OUTSTANDING_FORWARDS)
3413 { 3580 {
3414 source_info = GNUNET_CONTAINER_heap_remove_root (forward_list.minHeap); 3581 source_info = GNUNET_CONTAINER_heap_remove_root (forward_list.minHeap);
3415 GNUNET_assert(source_info != NULL); 3582 GNUNET_assert (source_info != NULL);
3416 record = source_info->record; 3583 record = source_info->record;
3417 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info); 3584 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, source_info);
3418 if (record->head == NULL) /* No more entries in DLL */ 3585 if (record->head == NULL) /* No more entries in DLL */
3419 { 3586 {
3420 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(forward_list.hashmap, &record->key, record)); 3587 GNUNET_assert (GNUNET_YES ==
3421 GNUNET_free(record); 3588 GNUNET_CONTAINER_multihashmap_remove
3589 (forward_list.hashmap, &record->key, record));
3590 GNUNET_free (record);
3422 } 3591 }
3423 if (source_info->delete_task != GNUNET_SCHEDULER_NO_TASK) 3592 if (source_info->delete_task != GNUNET_SCHEDULER_NO_TASK)
3424 GNUNET_SCHEDULER_cancel(source_info->delete_task); 3593 GNUNET_SCHEDULER_cancel (source_info->delete_task);
3425 if (source_info->find_peers_responded != NULL) 3594 if (source_info->find_peers_responded != NULL)
3426 GNUNET_CONTAINER_bloomfilter_free(source_info->find_peers_responded); 3595 GNUNET_CONTAINER_bloomfilter_free (source_info->find_peers_responded);
3427 GNUNET_free(source_info); 3596 GNUNET_free (source_info);
3428 current_size = GNUNET_CONTAINER_multihashmap_size(forward_list.hashmap); 3597 current_size =
3598 GNUNET_CONTAINER_multihashmap_size (forward_list.hashmap);
3429 } 3599 }
3430#endif 3600#endif
3431 /** Non-local request and have too many outstanding forwards, discard! */ 3601 /** Non-local request and have too many outstanding forwards, discard! */
3432 if ((current_size >= MAX_OUTSTANDING_FORWARDS) && (msg_ctx->client == NULL)) 3602 if ((current_size >= MAX_OUTSTANDING_FORWARDS) && (msg_ctx->client == NULL))
3433 return GNUNET_NO; 3603 return GNUNET_NO;
3434 3604
3435 now = GNUNET_TIME_absolute_get(); 3605 now = GNUNET_TIME_absolute_get ();
3436 record = GNUNET_CONTAINER_multihashmap_get(forward_list.hashmap, &msg_ctx->key); 3606 record =
3437 if (record != NULL) /* Already know this request! */ 3607 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &msg_ctx->key);
3608 if (record != NULL) /* Already know this request! */
3438 { 3609 {
3439 pos = record->head; 3610 pos = record->head;
3440 while (pos != NULL) 3611 while (pos != NULL)
3441 { 3612 {
3442 if (0 == memcmp(msg_ctx->peer, &pos->source, sizeof(struct GNUNET_PeerIdentity))) 3613 if (0 ==
3443 break; /* Already have this peer in reply list! */ 3614 memcmp (msg_ctx->peer, &pos->source,
3615 sizeof (struct GNUNET_PeerIdentity)))
3616 break; /* Already have this peer in reply list! */
3444 pos = pos->next; 3617 pos = pos->next;
3445 } 3618 }
3446 if ((pos != NULL) && (pos->client == msg_ctx->client)) /* Seen this already */ 3619 if ((pos != NULL) && (pos->client == msg_ctx->client)) /* Seen this already */
3447 { 3620 {
3448 GNUNET_CONTAINER_heap_update_cost(forward_list.minHeap, pos->hnode, now.abs_value); 3621 GNUNET_CONTAINER_heap_update_cost (forward_list.minHeap, pos->hnode,
3622 now.abs_value);
3449 return GNUNET_NO; 3623 return GNUNET_NO;
3450 } 3624 }
3451 } 3625 }
3452 else 3626 else
3453 { 3627 {
3454 record = GNUNET_malloc(sizeof (struct DHTQueryRecord)); 3628 record = GNUNET_malloc (sizeof (struct DHTQueryRecord));
3455 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(forward_list.hashmap, &msg_ctx->key, record, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 3629 GNUNET_assert (GNUNET_OK ==
3456 memcpy(&record->key, &msg_ctx->key, sizeof(GNUNET_HashCode)); 3630 GNUNET_CONTAINER_multihashmap_put (forward_list.hashmap,
3631 &msg_ctx->key, record,
3632 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3633 memcpy (&record->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
3457 } 3634 }
3458 3635
3459 source_info = GNUNET_malloc(sizeof(struct DHTRouteSource)); 3636 source_info = GNUNET_malloc (sizeof (struct DHTRouteSource));
3460 source_info->record = record; 3637 source_info->record = record;
3461 source_info->delete_task = GNUNET_SCHEDULER_add_delayed (DHT_FORWARD_TIMEOUT, &remove_forward_entry, source_info); 3638 source_info->delete_task =
3462 source_info->find_peers_responded = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K); 3639 GNUNET_SCHEDULER_add_delayed (DHT_FORWARD_TIMEOUT, &remove_forward_entry,
3463 memcpy(&source_info->source, msg_ctx->peer, sizeof(struct GNUNET_PeerIdentity)); 3640 source_info);
3464 GNUNET_CONTAINER_DLL_insert_after (record->head, record->tail, record->tail, source_info); 3641 source_info->find_peers_responded =
3465 if (msg_ctx->client != NULL) /* For local request, set timeout so high it effectively never gets pushed out */ 3642 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3643 memcpy (&source_info->source, msg_ctx->peer,
3644 sizeof (struct GNUNET_PeerIdentity));
3645 GNUNET_CONTAINER_DLL_insert_after (record->head, record->tail, record->tail,
3646 source_info);
3647 if (msg_ctx->client != NULL) /* For local request, set timeout so high it effectively never gets pushed out */
3466 { 3648 {
3467 source_info->client = msg_ctx->client; 3649 source_info->client = msg_ctx->client;
3468 now = GNUNET_TIME_absolute_get_forever(); 3650 now = GNUNET_TIME_absolute_get_forever ();
3469 } 3651 }
3470 source_info->hnode = GNUNET_CONTAINER_heap_insert(forward_list.minHeap, source_info, now.abs_value); 3652 source_info->hnode =
3653 GNUNET_CONTAINER_heap_insert (forward_list.minHeap, source_info,
3654 now.abs_value);
3471#if DEBUG_DHT > 1 3655#if DEBUG_DHT > 1
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3473 "`%s:%s': Created new forward source info for %s uid %llu\n", my_short_id, 3657 "`%s:%s': Created new forward source info for %s uid %llu\n",
3474 "DHT", GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id); 3658 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
3659 msg_ctx->unique_id);
3475#endif 3660#endif
3476 return GNUNET_YES; 3661 return GNUNET_YES;
3477} 3662}
@@ -3485,8 +3670,8 @@ static int cache_response(struct DHT_MessageContext *msg_ctx)
3485 * @param msg_ctx the context containing all pertinent information about the message 3670 * @param msg_ctx the context containing all pertinent information about the message
3486 */ 3671 */
3487static void 3672static void
3488route_message(const struct GNUNET_MessageHeader *msg, 3673route_message (const struct GNUNET_MessageHeader *msg,
3489 struct DHT_MessageContext *msg_ctx) 3674 struct DHT_MessageContext *msg_ctx)
3490{ 3675{
3491 int i; 3676 int i;
3492 struct PeerInfo *selected; 3677 struct PeerInfo *selected;
@@ -3510,27 +3695,31 @@ route_message(const struct GNUNET_MessageHeader *msg,
3510 { 3695 {
3511 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE, 3696 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE,
3512 msg_ctx->hop_count, GNUNET_SYSERR, 3697 msg_ctx->hop_count, GNUNET_SYSERR,
3513 &my_identity, &msg_ctx->key, msg_ctx->peer, 3698 &my_identity, &msg_ctx->key,
3514 NULL); 3699 msg_ctx->peer, NULL);
3515 } 3700 }
3516#endif 3701#endif
3517 if (msg_ctx->bloom != NULL) 3702 if (msg_ctx->bloom != NULL)
3518 { 3703 {
3519 GNUNET_CONTAINER_bloomfilter_free(msg_ctx->bloom); 3704 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
3520 msg_ctx->bloom = NULL; 3705 msg_ctx->bloom = NULL;
3521 } 3706 }
3522 return; 3707 return;
3523 } 3708 }
3524 3709
3525 increment_stats(STAT_ROUTES); 3710 increment_stats (STAT_ROUTES);
3526 target_forward_count = get_forward_count(msg_ctx->hop_count, msg_ctx->replication); 3711 target_forward_count =
3527 GNUNET_asprintf(&stat_forward_count, "# forward counts of %d", target_forward_count); 3712 get_forward_count (msg_ctx->hop_count, msg_ctx->replication);
3528 increment_stats(stat_forward_count); 3713 GNUNET_asprintf (&stat_forward_count, "# forward counts of %d",
3529 GNUNET_free(stat_forward_count); 3714 target_forward_count);
3715 increment_stats (stat_forward_count);
3716 GNUNET_free (stat_forward_count);
3530 if (msg_ctx->bloom == NULL) 3717 if (msg_ctx->bloom == NULL)
3531 msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K); 3718 msg_ctx->bloom =
3719 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3532 3720
3533 if ((stop_on_closest == GNUNET_YES) && (msg_ctx->closest == GNUNET_YES) && (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)) 3721 if ((stop_on_closest == GNUNET_YES) && (msg_ctx->closest == GNUNET_YES)
3722 && (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT))
3534 target_forward_count = 0; 3723 target_forward_count = 0;
3535 3724
3536 /** 3725 /**
@@ -3548,75 +3737,105 @@ route_message(const struct GNUNET_MessageHeader *msg,
3548 * testing driver) we will ignore this restriction for FIND_PEER messages so that 3737 * testing driver) we will ignore this restriction for FIND_PEER messages so that
3549 * routing tables still get constructed. 3738 * routing tables still get constructed.
3550 */ 3739 */
3551 if ((GNUNET_YES == strict_kademlia) && (msg_ctx->closest == GNUNET_YES) && (msg_ctx->hop_count > 0) && (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER)) 3740 if ((GNUNET_YES == strict_kademlia) && (msg_ctx->closest == GNUNET_YES)
3741 && (msg_ctx->hop_count > 0)
3742 && (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER))
3552 target_forward_count = 0; 3743 target_forward_count = 0;
3553 3744
3554 3745
3555 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom, &my_identity.hashPubKey); 3746 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom, &my_identity.hashPubKey);
3556 hash_from_uid (msg_ctx->unique_id, &unique_hash); 3747 hash_from_uid (msg_ctx->unique_id, &unique_hash);
3557 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (recent.hashmap, &unique_hash)) 3748 if (GNUNET_YES ==
3558 { 3749 GNUNET_CONTAINER_multihashmap_contains (recent.hashmap, &unique_hash))
3559 recent_req = GNUNET_CONTAINER_multihashmap_get(recent.hashmap, &unique_hash); 3750 {
3560 GNUNET_assert(recent_req != NULL); 3751 recent_req =
3561 if (0 != memcmp(&recent_req->key, &msg_ctx->key, sizeof(GNUNET_HashCode))) 3752 GNUNET_CONTAINER_multihashmap_get (recent.hashmap, &unique_hash);
3562 increment_stats(STAT_DUPLICATE_UID); 3753 GNUNET_assert (recent_req != NULL);
3563 else 3754 if (0 !=
3564 { 3755 memcmp (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode)))
3565 increment_stats(STAT_RECENT_SEEN); 3756 increment_stats (STAT_DUPLICATE_UID);
3566 GNUNET_CONTAINER_bloomfilter_or2(msg_ctx->bloom, recent_req->bloom, DHT_BLOOM_SIZE); 3757 else
3567 } 3758 {
3759 increment_stats (STAT_RECENT_SEEN);
3760 GNUNET_CONTAINER_bloomfilter_or2 (msg_ctx->bloom, recent_req->bloom,
3761 DHT_BLOOM_SIZE);
3762 }
3568 } 3763 }
3569 else 3764 else
3570 { 3765 {
3571 recent_req = GNUNET_malloc(sizeof(struct RecentRequest)); 3766 recent_req = GNUNET_malloc (sizeof (struct RecentRequest));
3572 recent_req->uid = msg_ctx->unique_id; 3767 recent_req->uid = msg_ctx->unique_id;
3573 memcpy(&recent_req->key, &msg_ctx->key, sizeof(GNUNET_HashCode)); 3768 memcpy (&recent_req->key, &msg_ctx->key, sizeof (GNUNET_HashCode));
3574 recent_req->remove_task = GNUNET_SCHEDULER_add_delayed(DEFAULT_RECENT_REMOVAL, &remove_recent, recent_req); 3769 recent_req->remove_task =
3575 recent_req->heap_node = GNUNET_CONTAINER_heap_insert(recent.minHeap, recent_req, GNUNET_TIME_absolute_get().abs_value); 3770 GNUNET_SCHEDULER_add_delayed (DEFAULT_RECENT_REMOVAL, &remove_recent,
3576 recent_req->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K); 3771 recent_req);
3577 GNUNET_CONTAINER_multihashmap_put(recent.hashmap, &unique_hash, recent_req, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 3772 recent_req->heap_node =
3773 GNUNET_CONTAINER_heap_insert (recent.minHeap, recent_req,
3774 GNUNET_TIME_absolute_get ().abs_value);
3775 recent_req->bloom =
3776 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3777 GNUNET_CONTAINER_multihashmap_put (recent.hashmap, &unique_hash,
3778 recent_req,
3779 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3578 } 3780 }
3579 3781
3580 if (GNUNET_CONTAINER_multihashmap_size(recent.hashmap) > DHT_MAX_RECENT) 3782 if (GNUNET_CONTAINER_multihashmap_size (recent.hashmap) > DHT_MAX_RECENT)
3581 { 3783 {
3582 recent_req = GNUNET_CONTAINER_heap_peek(recent.minHeap); 3784 recent_req = GNUNET_CONTAINER_heap_peek (recent.minHeap);
3583 GNUNET_assert(recent_req != NULL); 3785 GNUNET_assert (recent_req != NULL);
3584 GNUNET_SCHEDULER_cancel(recent_req->remove_task); 3786 GNUNET_SCHEDULER_cancel (recent_req->remove_task);
3585 GNUNET_SCHEDULER_add_now(&remove_recent, recent_req); 3787 GNUNET_SCHEDULER_add_now (&remove_recent, recent_req);
3586 } 3788 }
3587 3789
3588 forward_count = 0; 3790 forward_count = 0;
3589 for (i = 0; i < target_forward_count; i++) 3791 for (i = 0; i < target_forward_count; i++)
3590 { 3792 {
3591 selected = select_peer(&msg_ctx->key, msg_ctx->bloom, msg_ctx->hop_count); 3793 selected =
3794 select_peer (&msg_ctx->key, msg_ctx->bloom, msg_ctx->hop_count);
3592 3795
3593 if (selected != NULL) 3796 if (selected != NULL)
3594 { 3797 {
3595 forward_count++; 3798 forward_count++;
3596 if (GNUNET_CRYPTO_hash_matching_bits(&selected->id.hashPubKey, &msg_ctx->key) >= GNUNET_CRYPTO_hash_matching_bits(&my_identity.hashPubKey, &msg_ctx->key)) 3799 if (GNUNET_CRYPTO_hash_matching_bits
3597 GNUNET_asprintf(&temp_stat_str, "# requests routed to close(r) peer hop %u", msg_ctx->hop_count); 3800 (&selected->id.hashPubKey,
3801 &msg_ctx->key) >=
3802 GNUNET_CRYPTO_hash_matching_bits (&my_identity.hashPubKey,
3803 &msg_ctx->key))
3804 GNUNET_asprintf (&temp_stat_str,
3805 "# requests routed to close(r) peer hop %u",
3806 msg_ctx->hop_count);
3598 else 3807 else
3599 GNUNET_asprintf(&temp_stat_str, "# requests routed to less close peer hop %u", msg_ctx->hop_count); 3808 GNUNET_asprintf (&temp_stat_str,
3809 "# requests routed to less close peer hop %u",
3810 msg_ctx->hop_count);
3600 if (temp_stat_str != NULL) 3811 if (temp_stat_str != NULL)
3601 { 3812 {
3602 increment_stats(temp_stat_str); 3813 increment_stats (temp_stat_str);
3603 GNUNET_free(temp_stat_str); 3814 GNUNET_free (temp_stat_str);
3604 } 3815 }
3605 GNUNET_CONTAINER_bloomfilter_add(msg_ctx->bloom, &selected->id.hashPubKey); 3816 GNUNET_CONTAINER_bloomfilter_add (msg_ctx->bloom,
3817 &selected->id.hashPubKey);
3606#if DEBUG_DHT_ROUTING > 1 3818#if DEBUG_DHT_ROUTING > 1
3607 nearest = find_closest_peer(&msg_ctx->key); 3819 nearest = find_closest_peer (&msg_ctx->key);
3608 nearest_buf = GNUNET_strdup(GNUNET_i2s(&nearest->id)); 3820 nearest_buf = GNUNET_strdup (GNUNET_i2s (&nearest->id));
3609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3610 "`%s:%s': Forwarding request key %s uid %llu to peer %s (closest %s, bits %d, distance %u)\n", my_short_id, 3822 "`%s:%s': Forwarding request key %s uid %llu to peer %s (closest %s, bits %d, distance %u)\n",
3611 "DHT", GNUNET_h2s (&msg_ctx->key), msg_ctx->unique_id, GNUNET_i2s(&selected->id), nearest_buf, GNUNET_CRYPTO_hash_matching_bits(&nearest->id.hashPubKey, msg_ctx->key), distance(&nearest->id.hashPubKey, msg_ctx->key)); 3823 my_short_id, "DHT", GNUNET_h2s (&msg_ctx->key),
3612 GNUNET_free(nearest_buf); 3824 msg_ctx->unique_id, GNUNET_i2s (&selected->id),
3825 nearest_buf,
3826 GNUNET_CRYPTO_hash_matching_bits (&nearest->id.
3827 hashPubKey,
3828 msg_ctx->key),
3829 distance (&nearest->id.hashPubKey, msg_ctx->key));
3830 GNUNET_free (nearest_buf);
3613#endif 3831#endif
3614#if DEBUG_DHT_ROUTING 3832#if DEBUG_DHT_ROUTING
3615 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 3833 if ((debug_routes_extended) && (dhtlog_handle != NULL))
3616 { 3834 {
3617 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id, DHTLOG_ROUTE, 3835 dhtlog_handle->insert_route (NULL, msg_ctx->unique_id,
3618 msg_ctx->hop_count, GNUNET_NO, 3836 DHTLOG_ROUTE, msg_ctx->hop_count,
3619 &my_identity, &msg_ctx->key, msg_ctx->peer, 3837 GNUNET_NO, &my_identity,
3838 &msg_ctx->key, msg_ctx->peer,
3620 &selected->id); 3839 &selected->id);
3621 } 3840 }
3622#endif 3841#endif
@@ -3626,8 +3845,9 @@ route_message(const struct GNUNET_MessageHeader *msg,
3626 3845
3627 if (msg_ctx->bloom != NULL) 3846 if (msg_ctx->bloom != NULL)
3628 { 3847 {
3629 GNUNET_CONTAINER_bloomfilter_or2(recent_req->bloom, msg_ctx->bloom, DHT_BLOOM_SIZE); 3848 GNUNET_CONTAINER_bloomfilter_or2 (recent_req->bloom, msg_ctx->bloom,
3630 GNUNET_CONTAINER_bloomfilter_free(msg_ctx->bloom); 3849 DHT_BLOOM_SIZE);
3850 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
3631 msg_ctx->bloom = NULL; 3851 msg_ctx->bloom = NULL;
3632 } 3852 }
3633 3853
@@ -3657,47 +3877,55 @@ route_message(const struct GNUNET_MessageHeader *msg,
3657 * @param msg_ctx the context containing all pertinent information about the message 3877 * @param msg_ctx the context containing all pertinent information about the message
3658 */ 3878 */
3659static void 3879static void
3660demultiplex_message(const struct GNUNET_MessageHeader *msg, 3880demultiplex_message (const struct GNUNET_MessageHeader *msg,
3661 struct DHT_MessageContext *msg_ctx) 3881 struct DHT_MessageContext *msg_ctx)
3662{ 3882{
3663 /* FIXME: Should we use closest excluding those we won't route to (the bloomfilter problem)? */ 3883 /* FIXME: Should we use closest excluding those we won't route to (the bloomfilter problem)? */
3664 msg_ctx->closest = am_closest_peer(&msg_ctx->key, msg_ctx->bloom); 3884 msg_ctx->closest = am_closest_peer (&msg_ctx->key, msg_ctx->bloom);
3665 switch (ntohs(msg->type)) 3885 switch (ntohs (msg->type))
3666 { 3886 {
3667 case GNUNET_MESSAGE_TYPE_DHT_GET: /* Add to hashmap of requests seen, search for data (always) */ 3887 case GNUNET_MESSAGE_TYPE_DHT_GET: /* Add to hashmap of requests seen, search for data (always) */
3668 cache_response (msg_ctx); 3888 cache_response (msg_ctx);
3669 handle_dht_get (msg, msg_ctx); 3889 handle_dht_get (msg, msg_ctx);
3670 break; 3890 break;
3671 case GNUNET_MESSAGE_TYPE_DHT_PUT: /* Check if closest, if so insert data. */ 3891 case GNUNET_MESSAGE_TYPE_DHT_PUT: /* Check if closest, if so insert data. */
3672 increment_stats(STAT_PUTS); 3892 increment_stats (STAT_PUTS);
3673 handle_dht_put (msg, msg_ctx); 3893 handle_dht_put (msg, msg_ctx);
3674 break; 3894 break;
3675 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: /* Check if closest and not started by us, check options, add to requests seen */ 3895 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: /* Check if closest and not started by us, check options, add to requests seen */
3676 increment_stats(STAT_FIND_PEER); 3896 increment_stats (STAT_FIND_PEER);
3677 if (((msg_ctx->hop_count > 0) && (0 != memcmp(msg_ctx->peer, &my_identity, sizeof(struct GNUNET_PeerIdentity)))) || (msg_ctx->client != NULL)) 3897 if (((msg_ctx->hop_count > 0)
3678 { 3898 && (0 !=
3679 cache_response (msg_ctx); 3899 memcmp (msg_ctx->peer, &my_identity,
3680 if ((msg_ctx->closest == GNUNET_YES) || (msg_ctx->msg_options == GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) 3900 sizeof (struct GNUNET_PeerIdentity))))
3681 handle_dht_find_peer (msg, msg_ctx); 3901 || (msg_ctx->client != NULL))
3682 } 3902 {
3903 cache_response (msg_ctx);
3904 if ((msg_ctx->closest == GNUNET_YES)
3905 || (msg_ctx->msg_options ==
3906 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE))
3907 handle_dht_find_peer (msg, msg_ctx);
3908 }
3683 else 3909 else
3684 route_message (msg, msg_ctx); 3910 route_message (msg, msg_ctx);
3685#if DEBUG_DHT_ROUTING 3911#if DEBUG_DHT_ROUTING
3686 if (msg_ctx->hop_count == 0) /* Locally initiated request */ 3912 if (msg_ctx->hop_count == 0) /* Locally initiated request */
3687 { 3913 {
3688 if ((debug_routes) && (dhtlog_handle != NULL)) 3914 if ((debug_routes) && (dhtlog_handle != NULL))
3689 { 3915 {
3690 dhtlog_handle->insert_dhtkey(NULL, &msg_ctx->key); 3916 dhtlog_handle->insert_dhtkey (NULL, &msg_ctx->key);
3691 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id, DHTLOG_FIND_PEER, 3917 dhtlog_handle->insert_query (NULL, msg_ctx->unique_id,
3692 msg_ctx->hop_count, GNUNET_NO, &my_identity, 3918 DHTLOG_FIND_PEER,
3693 &msg_ctx->key); 3919 msg_ctx->hop_count, GNUNET_NO,
3920 &my_identity, &msg_ctx->key);
3694 } 3921 }
3695 } 3922 }
3696#endif 3923#endif
3697 break; 3924 break;
3698 default: 3925 default:
3699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 3926 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3700 "`%s': Message type (%d) not handled, forwarding anyway!\n", "DHT", ntohs(msg->type)); 3927 "`%s': Message type (%d) not handled, forwarding anyway!\n",
3928 "DHT", ntohs (msg->type));
3701 route_message (msg, msg_ctx); 3929 route_message (msg, msg_ctx);
3702 } 3930 }
3703} 3931}
@@ -3729,30 +3957,33 @@ republish_content_iterator (void *cls,
3729 struct GNUNET_DHT_PutMessage *put_msg; 3957 struct GNUNET_DHT_PutMessage *put_msg;
3730#if DEBUG_DHT 3958#if DEBUG_DHT
3731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3732 "`%s:%s': Received `%s' response from datacache\n", my_short_id, "DHT", "GET"); 3960 "`%s:%s': Received `%s' response from datacache\n", my_short_id,
3961 "DHT", "GET");
3733#endif 3962#endif
3734 new_msg_ctx = GNUNET_malloc(sizeof(struct DHT_MessageContext)); 3963 new_msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
3735 3964
3736 put_msg = 3965 put_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_PutMessage) + size);
3737 GNUNET_malloc (sizeof (struct GNUNET_DHT_PutMessage) + size);
3738 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT); 3966 put_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
3739 put_msg->header.size = htons (sizeof (struct GNUNET_DHT_PutMessage) + size); 3967 put_msg->header.size = htons (sizeof (struct GNUNET_DHT_PutMessage) + size);
3740 put_msg->expiration = GNUNET_TIME_absolute_hton(exp); 3968 put_msg->expiration = GNUNET_TIME_absolute_hton (exp);
3741 put_msg->type = htons (type); 3969 put_msg->type = htons (type);
3742 memcpy (&put_msg[1], data, size); 3970 memcpy (&put_msg[1], data, size);
3743 new_msg_ctx->unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1)); 3971 new_msg_ctx->unique_id =
3972 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
3973 (GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t) - 1));
3744 new_msg_ctx->replication = ntohl (DEFAULT_PUT_REPLICATION); 3974 new_msg_ctx->replication = ntohl (DEFAULT_PUT_REPLICATION);
3745 new_msg_ctx->msg_options = ntohl (0); 3975 new_msg_ctx->msg_options = ntohl (0);
3746 new_msg_ctx->network_size = estimate_diameter(); 3976 new_msg_ctx->network_size = estimate_diameter ();
3747 new_msg_ctx->peer = &my_identity; 3977 new_msg_ctx->peer = &my_identity;
3748 new_msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K); 3978 new_msg_ctx->bloom =
3979 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
3749 new_msg_ctx->hop_count = 0; 3980 new_msg_ctx->hop_count = 0;
3750 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE; 3981 new_msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE;
3751 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT; 3982 new_msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
3752 increment_stats(STAT_PUT_START); 3983 increment_stats (STAT_PUT_START);
3753 demultiplex_message(&put_msg->header, new_msg_ctx); 3984 demultiplex_message (&put_msg->header, new_msg_ctx);
3754 3985
3755 GNUNET_free(new_msg_ctx); 3986 GNUNET_free (new_msg_ctx);
3756 GNUNET_free (put_msg); 3987 GNUNET_free (put_msg);
3757 return GNUNET_OK; 3988 return GNUNET_OK;
3758} 3989}
@@ -3764,7 +3995,7 @@ republish_content_iterator (void *cls,
3764 * @param tc runtime context for this task 3995 * @param tc runtime context for this task
3765 */ 3996 */
3766static void 3997static void
3767republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 3998republish_content (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3768{ 3999{
3769 struct RepublishContext *put_context = cls; 4000 struct RepublishContext *put_context = cls;
3770 4001
@@ -3772,16 +4003,19 @@ republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3772 4003
3773 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) 4004 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3774 { 4005 {
3775 GNUNET_free(put_context); 4006 GNUNET_free (put_context);
3776 return; 4007 return;
3777 } 4008 }
3778 4009
3779 GNUNET_assert (datacache != NULL); /* If we have no datacache we never should have scheduled this! */ 4010 GNUNET_assert (datacache != NULL); /* If we have no datacache we never should have scheduled this! */
3780 results = GNUNET_DATACACHE_get(datacache, &put_context->key, put_context->type, &republish_content_iterator, NULL); 4011 results =
3781 if (results == 0) /* Data must have expired */ 4012 GNUNET_DATACACHE_get (datacache, &put_context->key, put_context->type,
3782 GNUNET_free(put_context); 4013 &republish_content_iterator, NULL);
3783 else /* Reschedule task for next time period */ 4014 if (results == 0) /* Data must have expired */
3784 GNUNET_SCHEDULER_add_delayed(dht_republish_frequency, &republish_content, put_context); 4015 GNUNET_free (put_context);
4016 else /* Reschedule task for next time period */
4017 GNUNET_SCHEDULER_add_delayed (dht_republish_frequency, &republish_content,
4018 put_context);
3785 4019
3786} 4020}
3787 4021
@@ -3796,8 +4030,8 @@ republish_content(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3796 * iterate, 4030 * iterate,
3797 * GNUNET_NO if not. 4031 * GNUNET_NO if not.
3798 */ 4032 */
3799static int find_client_records (void *cls, 4033static int
3800 const GNUNET_HashCode * key, void *value) 4034find_client_records (void *cls, const GNUNET_HashCode * key, void *value)
3801{ 4035{
3802 struct ClientList *client = cls; 4036 struct ClientList *client = cls;
3803 struct DHTQueryRecord *record = value; 4037 struct DHTQueryRecord *record = value;
@@ -3811,19 +4045,21 @@ static int find_client_records (void *cls,
3811 } 4045 }
3812 if (pos != NULL) 4046 if (pos != NULL)
3813 { 4047 {
3814 GNUNET_CONTAINER_DLL_remove(record->head, record->tail, pos); 4048 GNUNET_CONTAINER_DLL_remove (record->head, record->tail, pos);
3815 GNUNET_CONTAINER_heap_remove_node(forward_list.minHeap, pos->hnode); 4049 GNUNET_CONTAINER_heap_remove_node (forward_list.minHeap, pos->hnode);
3816 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK) 4050 if (pos->delete_task != GNUNET_SCHEDULER_NO_TASK)
3817 GNUNET_SCHEDULER_cancel(pos->delete_task); 4051 GNUNET_SCHEDULER_cancel (pos->delete_task);
3818 4052
3819 if (pos->find_peers_responded != NULL) 4053 if (pos->find_peers_responded != NULL)
3820 GNUNET_CONTAINER_bloomfilter_free(pos->find_peers_responded); 4054 GNUNET_CONTAINER_bloomfilter_free (pos->find_peers_responded);
3821 GNUNET_free(pos); 4055 GNUNET_free (pos);
3822 } 4056 }
3823 if (record->head == NULL) /* No more entries in DLL */ 4057 if (record->head == NULL) /* No more entries in DLL */
3824 { 4058 {
3825 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(forward_list.hashmap, &record->key, record)); 4059 GNUNET_assert (GNUNET_YES ==
3826 GNUNET_free(record); 4060 GNUNET_CONTAINER_multihashmap_remove
4061 (forward_list.hashmap, &record->key, record));
4062 GNUNET_free (record);
3827 } 4063 }
3828 return GNUNET_YES; 4064 return GNUNET_YES;
3829} 4065}
@@ -3836,8 +4072,8 @@ static int find_client_records (void *cls,
3836 * @param client identification of the client; NULL 4072 * @param client identification of the client; NULL
3837 * for the last call when the server is destroyed 4073 * for the last call when the server is destroyed
3838 */ 4074 */
3839static void handle_client_disconnect (void *cls, 4075static void
3840 struct GNUNET_SERVER_Client* client) 4076handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3841{ 4077{
3842 struct ClientList *pos = client_list; 4078 struct ClientList *pos = client_list;
3843 struct ClientList *prev; 4079 struct ClientList *prev;
@@ -3864,15 +4100,18 @@ static void handle_client_disconnect (void *cls,
3864 if (found != NULL) 4100 if (found != NULL)
3865 { 4101 {
3866 if (found->transmit_handle != NULL) 4102 if (found->transmit_handle != NULL)
3867 GNUNET_CONNECTION_notify_transmit_ready_cancel(found->transmit_handle); 4103 GNUNET_CONNECTION_notify_transmit_ready_cancel
4104 (found->transmit_handle);
3868 4105
3869 while(NULL != (reply = found->pending_head)) 4106 while (NULL != (reply = found->pending_head))
3870 { 4107 {
3871 GNUNET_CONTAINER_DLL_remove(found->pending_head, found->pending_tail, reply); 4108 GNUNET_CONTAINER_DLL_remove (found->pending_head,
3872 GNUNET_free(reply); 4109 found->pending_tail, reply);
4110 GNUNET_free (reply);
3873 } 4111 }
3874 GNUNET_CONTAINER_multihashmap_iterate(forward_list.hashmap, &find_client_records, found); 4112 GNUNET_CONTAINER_multihashmap_iterate (forward_list.hashmap,
3875 GNUNET_free(found); 4113 &find_client_records, found);
4114 GNUNET_free (found);
3876 } 4115 }
3877} 4116}
3878 4117
@@ -3921,29 +4160,38 @@ malicious_put_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3921 4160
3922 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) 4161 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3923 return; 4162 return;
3924 put_message.header.size = htons(sizeof(struct GNUNET_DHT_PutMessage)); 4163 put_message.header.size = htons (sizeof (struct GNUNET_DHT_PutMessage));
3925 put_message.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_PUT); 4164 put_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_PUT);
3926 put_message.type = htonl(GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE); 4165 put_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
3927 put_message.expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_forever()); 4166 put_message.expiration =
3928 memset(&msg_ctx, 0, sizeof(struct DHT_MessageContext)); 4167 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_forever ());
3929 random_key = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t)-1); 4168 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3930 GNUNET_CRYPTO_hash(&random_key, sizeof(uint32_t), &key); 4169 random_key =
3931 memcpy(&msg_ctx.key, &key, sizeof(GNUNET_HashCode)); 4170 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) - 1);
3932 msg_ctx.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1)); 4171 GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
4172 memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
4173 msg_ctx.unique_id =
4174 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
4175 (GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t) - 1));
3933 msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION); 4176 msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3934 msg_ctx.msg_options = ntohl (0); 4177 msg_ctx.msg_options = ntohl (0);
3935 msg_ctx.network_size = estimate_diameter(); 4178 msg_ctx.network_size = estimate_diameter ();
3936 msg_ctx.peer = &my_identity; 4179 msg_ctx.peer = &my_identity;
3937 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE; 4180 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
3938 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT; 4181 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3939#if DEBUG_DHT_ROUTING 4182#if DEBUG_DHT_ROUTING
3940 if (dhtlog_handle != NULL) 4183 if (dhtlog_handle != NULL)
3941 dhtlog_handle->insert_dhtkey(NULL, &key); 4184 dhtlog_handle->insert_dhtkey (NULL, &key);
3942#endif 4185#endif
3943 increment_stats(STAT_PUT_START); 4186 increment_stats (STAT_PUT_START);
3944 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Sending malicious PUT message with hash %s\n", my_short_id, "DHT", GNUNET_h2s(&key)); 4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3945 demultiplex_message(&put_message.header, &msg_ctx); 4188 "%s:%s Sending malicious PUT message with hash %s\n",
3946 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, malicious_put_frequency), &malicious_put_task, NULL); 4189 my_short_id, "DHT", GNUNET_h2s (&key));
4190 demultiplex_message (&put_message.header, &msg_ctx);
4191 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4192 (GNUNET_TIME_UNIT_MILLISECONDS,
4193 malicious_put_frequency),
4194 &malicious_put_task, NULL);
3947} 4195}
3948 4196
3949 4197
@@ -3964,28 +4212,36 @@ malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3964 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) 4212 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3965 return; 4213 return;
3966 4214
3967 get_message.header.size = htons(sizeof(struct GNUNET_DHT_GetMessage)); 4215 get_message.header.size = htons (sizeof (struct GNUNET_DHT_GetMessage));
3968 get_message.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_GET); 4216 get_message.header.type = htons (GNUNET_MESSAGE_TYPE_DHT_GET);
3969 get_message.type = htonl(GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE); 4217 get_message.type = htonl (GNUNET_BLOCK_DHT_MALICIOUS_MESSAGE_TYPE);
3970 memset(&msg_ctx, 0, sizeof(struct DHT_MessageContext)); 4218 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
3971 random_key = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t)-1); 4219 random_key =
3972 GNUNET_CRYPTO_hash(&random_key, sizeof(uint32_t), &key); 4220 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, (uint32_t) - 1);
3973 memcpy(&msg_ctx.key, &key, sizeof(GNUNET_HashCode)); 4221 GNUNET_CRYPTO_hash (&random_key, sizeof (uint32_t), &key);
3974 msg_ctx.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t)-1)); 4222 memcpy (&msg_ctx.key, &key, sizeof (GNUNET_HashCode));
4223 msg_ctx.unique_id =
4224 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
4225 (GNUNET_CRYPTO_QUALITY_WEAK, (uint64_t) - 1));
3975 msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION); 4226 msg_ctx.replication = ntohl (DHT_DEFAULT_FIND_PEER_REPLICATION);
3976 msg_ctx.msg_options = ntohl (0); 4227 msg_ctx.msg_options = ntohl (0);
3977 msg_ctx.network_size = estimate_diameter(); 4228 msg_ctx.network_size = estimate_diameter ();
3978 msg_ctx.peer = &my_identity; 4229 msg_ctx.peer = &my_identity;
3979 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE; 4230 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE;
3980 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT; 4231 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
3981#if DEBUG_DHT_ROUTING 4232#if DEBUG_DHT_ROUTING
3982 if (dhtlog_handle != NULL) 4233 if (dhtlog_handle != NULL)
3983 dhtlog_handle->insert_dhtkey(NULL, &key); 4234 dhtlog_handle->insert_dhtkey (NULL, &key);
3984#endif 4235#endif
3985 increment_stats(STAT_GET_START); 4236 increment_stats (STAT_GET_START);
3986 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Sending malicious GET message with hash %s\n", my_short_id, "DHT", GNUNET_h2s(&key)); 4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4238 "%s:%s Sending malicious GET message with hash %s\n",
4239 my_short_id, "DHT", GNUNET_h2s (&key));
3987 demultiplex_message (&get_message.header, &msg_ctx); 4240 demultiplex_message (&get_message.header, &msg_ctx);
3988 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, malicious_get_frequency), &malicious_get_task, NULL); 4241 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4242 (GNUNET_TIME_UNIT_MILLISECONDS,
4243 malicious_get_frequency),
4244 &malicious_get_task, NULL);
3989} 4245}
3990#endif 4246#endif
3991 4247
@@ -4001,9 +4257,7 @@ malicious_get_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4001 * GNUNET_NO if not. 4257 * GNUNET_NO if not.
4002 */ 4258 */
4003static int 4259static int
4004add_known_to_bloom (void *cls, 4260add_known_to_bloom (void *cls, const GNUNET_HashCode * key, void *value)
4005 const GNUNET_HashCode * key,
4006 void *value)
4007{ 4261{
4008 struct GNUNET_CONTAINER_BloomFilter *bloom = cls; 4262 struct GNUNET_CONTAINER_BloomFilter *bloom = cls;
4009 GNUNET_CONTAINER_bloomfilter_add (bloom, key); 4263 GNUNET_CONTAINER_bloomfilter_add (bloom, key);
@@ -4019,7 +4273,8 @@ add_known_to_bloom (void *cls,
4019 * @param tc the context under which the task is running 4273 * @param tc the context under which the task is running
4020 */ 4274 */
4021static void 4275static void
4022send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 4276send_find_peer_message (void *cls,
4277 const struct GNUNET_SCHEDULER_TaskContext *tc)
4023{ 4278{
4024 struct GNUNET_DHT_FindPeerMessage *find_peer_msg; 4279 struct GNUNET_DHT_FindPeerMessage *find_peer_msg;
4025 struct DHT_MessageContext msg_ctx; 4280 struct DHT_MessageContext msg_ctx;
@@ -4034,19 +4289,22 @@ send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
4034 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) 4289 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
4035 return; 4290 return;
4036 4291
4037 if ((newly_found_peers > bucket_size) && (GNUNET_YES == do_find_peer)) /* If we are finding peers already, no need to send out our request right now! */ 4292 if ((newly_found_peers > bucket_size) && (GNUNET_YES == do_find_peer)) /* If we are finding peers already, no need to send out our request right now! */
4038 { 4293 {
4039 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Have %d newly found peers since last find peer message sent!\n", newly_found_peers); 4294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4295 "Have %d newly found peers since last find peer message sent!\n",
4296 newly_found_peers);
4040 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, 4297 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
4041 &send_find_peer_message, NULL); 4298 &send_find_peer_message, NULL);
4042 newly_found_peers = 0; 4299 newly_found_peers = 0;
4043 return; 4300 return;
4044 } 4301 }
4045 4302
4046 increment_stats(STAT_FIND_PEER_START); 4303 increment_stats (STAT_FIND_PEER_START);
4047#if COUNT_INTERVAL 4304#if COUNT_INTERVAL
4048 end = GNUNET_TIME_absolute_get(); 4305 end = GNUNET_TIME_absolute_get ();
4049 time_diff = GNUNET_TIME_absolute_get_difference(find_peer_context.start, end); 4306 time_diff =
4307 GNUNET_TIME_absolute_get_difference (find_peer_context.start, end);
4050 4308
4051 if (time_diff.abs_value > FIND_PEER_CALC_INTERVAL.abs_value) 4309 if (time_diff.abs_value > FIND_PEER_CALC_INTERVAL.abs_value)
4052 { 4310 {
@@ -4061,55 +4319,73 @@ send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
4061#endif 4319#endif
4062 4320
4063#if FIND_PEER_WITH_HELLO 4321#if FIND_PEER_WITH_HELLO
4064 find_peer_msg = GNUNET_malloc(sizeof(struct GNUNET_DHT_FindPeerMessage) + GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello)); 4322 find_peer_msg =
4065 find_peer_msg->header.size = htons(sizeof(struct GNUNET_DHT_FindPeerMessage) + GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello)); 4323 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage) +
4066 memcpy(&find_peer_msg[1], my_hello, GNUNET_HELLO_size((struct GNUNET_HELLO_Message *)my_hello)); 4324 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *)
4325 my_hello));
4326 find_peer_msg->header.size =
4327 htons (sizeof (struct GNUNET_DHT_FindPeerMessage) +
4328 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
4329 memcpy (&find_peer_msg[1], my_hello,
4330 GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) my_hello));
4067#else 4331#else
4068 find_peer_msg = GNUNET_malloc(sizeof(struct GNUNET_DHT_FindPeerMessage)); 4332 find_peer_msg = GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerMessage));
4069 find_peer_msg->header.size = htons(sizeof(struct GNUNET_DHT_FindPeerMessage)); 4333 find_peer_msg->header.size =
4334 htons (sizeof (struct GNUNET_DHT_FindPeerMessage));
4070#endif 4335#endif
4071 find_peer_msg->header.type = htons(GNUNET_MESSAGE_TYPE_DHT_FIND_PEER); 4336 find_peer_msg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
4072 temp_bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K); 4337 temp_bloom =
4073 GNUNET_CONTAINER_multihashmap_iterate(all_known_peers, &add_known_to_bloom, temp_bloom); 4338 GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, DHT_BLOOM_K);
4074 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_bloomfilter_get_raw_data(temp_bloom, find_peer_msg->bloomfilter, DHT_BLOOM_SIZE)); 4339 GNUNET_CONTAINER_multihashmap_iterate (all_known_peers, &add_known_to_bloom,
4340 temp_bloom);
4341 GNUNET_assert (GNUNET_OK ==
4342 GNUNET_CONTAINER_bloomfilter_get_raw_data (temp_bloom,
4343 find_peer_msg->
4344 bloomfilter,
4345 DHT_BLOOM_SIZE));
4075 GNUNET_CONTAINER_bloomfilter_free (temp_bloom); 4346 GNUNET_CONTAINER_bloomfilter_free (temp_bloom);
4076 memset(&msg_ctx, 0, sizeof(struct DHT_MessageContext)); 4347 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
4077 memcpy(&msg_ctx.key, &my_identity.hashPubKey, sizeof(GNUNET_HashCode)); 4348 memcpy (&msg_ctx.key, &my_identity.hashPubKey, sizeof (GNUNET_HashCode));
4078 msg_ctx.unique_id = GNUNET_ntohll (GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, (uint64_t)-1)); 4349 msg_ctx.unique_id =
4350 GNUNET_ntohll (GNUNET_CRYPTO_random_u64
4351 (GNUNET_CRYPTO_QUALITY_STRONG, (uint64_t) - 1));
4079 msg_ctx.replication = DHT_DEFAULT_FIND_PEER_REPLICATION; 4352 msg_ctx.replication = DHT_DEFAULT_FIND_PEER_REPLICATION;
4080 msg_ctx.msg_options = DHT_DEFAULT_FIND_PEER_OPTIONS; 4353 msg_ctx.msg_options = DHT_DEFAULT_FIND_PEER_OPTIONS;
4081 msg_ctx.network_size = estimate_diameter(); 4354 msg_ctx.network_size = estimate_diameter ();
4082 msg_ctx.peer = &my_identity; 4355 msg_ctx.peer = &my_identity;
4083 msg_ctx.importance = DHT_DEFAULT_FIND_PEER_IMPORTANCE; 4356 msg_ctx.importance = DHT_DEFAULT_FIND_PEER_IMPORTANCE;
4084 msg_ctx.timeout = DHT_DEFAULT_FIND_PEER_TIMEOUT; 4357 msg_ctx.timeout = DHT_DEFAULT_FIND_PEER_TIMEOUT;
4085 4358
4086 demultiplex_message(&find_peer_msg->header, &msg_ctx); 4359 demultiplex_message (&find_peer_msg->header, &msg_ctx);
4087 GNUNET_free(find_peer_msg); 4360 GNUNET_free (find_peer_msg);
4088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4089 "`%s:%s': Sent `%s' request to some (?) peers\n", my_short_id, "DHT", 4362 "`%s:%s': Sent `%s' request to some (?) peers\n", my_short_id,
4090 "FIND PEER"); 4363 "DHT", "FIND PEER");
4091 if (newly_found_peers < bucket_size) 4364 if (newly_found_peers < bucket_size)
4092 { 4365 {
4093 next_send_time.rel_value = (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) + 4366 next_send_time.rel_value =
4094 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, 4367 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) +
4095 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2); 4368 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
4369 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
4370 2);
4096 } 4371 }
4097 else 4372 else
4098 { 4373 {
4099 next_send_time.rel_value = DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value + 4374 next_send_time.rel_value = DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
4100 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, 4375 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
4101 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value - DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value); 4376 DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value -
4377 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
4102 } 4378 }
4103 4379
4104 GNUNET_assert (next_send_time.rel_value != 0); 4380 GNUNET_assert (next_send_time.rel_value != 0);
4105 find_peer_context.count = 0; 4381 find_peer_context.count = 0;
4106 newly_found_peers = 0; 4382 newly_found_peers = 0;
4107 find_peer_context.start = GNUNET_TIME_absolute_get(); 4383 find_peer_context.start = GNUNET_TIME_absolute_get ();
4108 if (GNUNET_YES == do_find_peer) 4384 if (GNUNET_YES == do_find_peer)
4109 { 4385 {
4110 GNUNET_SCHEDULER_add_delayed (next_send_time, 4386 GNUNET_SCHEDULER_add_delayed (next_send_time,
4111 &send_find_peer_message, NULL); 4387 &send_find_peer_message, NULL);
4112 } 4388 }
4113} 4389}
4114 4390
4115/** 4391/**
@@ -4122,10 +4398,12 @@ send_find_peer_message (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc
4122 * @param message the actual message received 4398 * @param message the actual message received
4123 */ 4399 */
4124static void 4400static void
4125handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client, 4401handle_dht_local_route_request (void *cls,
4402 struct GNUNET_SERVER_Client *client,
4126 const struct GNUNET_MessageHeader *message) 4403 const struct GNUNET_MessageHeader *message)
4127{ 4404{
4128 const struct GNUNET_DHT_RouteMessage *dht_msg = (const struct GNUNET_DHT_RouteMessage *) message; 4405 const struct GNUNET_DHT_RouteMessage *dht_msg =
4406 (const struct GNUNET_DHT_RouteMessage *) message;
4129 const struct GNUNET_MessageHeader *enc_msg; 4407 const struct GNUNET_MessageHeader *enc_msg;
4130 struct DHT_MessageContext msg_ctx; 4408 struct DHT_MessageContext msg_ctx;
4131 4409
@@ -4133,56 +4411,57 @@ handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client,
4133#if DEBUG_DHT 4411#if DEBUG_DHT
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4135 "`%s:%s': Received `%s' request from client, message type %d, key %s, uid %llu\n", 4413 "`%s:%s': Received `%s' request from client, message type %d, key %s, uid %llu\n",
4136 my_short_id, 4414 my_short_id,
4137 "DHT", 4415 "DHT",
4138 "GENERIC", 4416 "GENERIC",
4139 ntohs (message->type), 4417 ntohs (message->type),
4140 GNUNET_h2s (&dht_msg->key), 4418 GNUNET_h2s (&dht_msg->key), GNUNET_ntohll (dht_msg->unique_id));
4141 GNUNET_ntohll (dht_msg->unique_id));
4142#endif 4419#endif
4143#if DEBUG_DHT_ROUTING 4420#if DEBUG_DHT_ROUTING
4144 if (dhtlog_handle != NULL) 4421 if (dhtlog_handle != NULL)
4145 dhtlog_handle->insert_dhtkey (NULL, &dht_msg->key); 4422 dhtlog_handle->insert_dhtkey (NULL, &dht_msg->key);
4146#endif 4423#endif
4147 4424
4148 memset(&msg_ctx, 0, sizeof(struct DHT_MessageContext)); 4425 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
4149 msg_ctx.client = find_active_client (client); 4426 msg_ctx.client = find_active_client (client);
4150 memcpy(&msg_ctx.key, &dht_msg->key, sizeof(GNUNET_HashCode)); 4427 memcpy (&msg_ctx.key, &dht_msg->key, sizeof (GNUNET_HashCode));
4151 msg_ctx.unique_id = GNUNET_ntohll (dht_msg->unique_id); 4428 msg_ctx.unique_id = GNUNET_ntohll (dht_msg->unique_id);
4152 msg_ctx.replication = ntohl (dht_msg->desired_replication_level); 4429 msg_ctx.replication = ntohl (dht_msg->desired_replication_level);
4153 msg_ctx.msg_options = ntohl (dht_msg->options); 4430 msg_ctx.msg_options = ntohl (dht_msg->options);
4154 msg_ctx.network_size = estimate_diameter(); 4431 msg_ctx.network_size = estimate_diameter ();
4155 msg_ctx.peer = &my_identity; 4432 msg_ctx.peer = &my_identity;
4156 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 4; /* Make local routing a higher priority */ 4433 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 4; /* Make local routing a higher priority */
4157 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT; 4434 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
4158 4435
4159 if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET) 4436 if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
4160 increment_stats(STAT_GET_START); 4437 increment_stats (STAT_GET_START);
4161 else if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT) 4438 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
4162 increment_stats(STAT_PUT_START); 4439 increment_stats (STAT_PUT_START);
4163 else if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER) 4440 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_FIND_PEER)
4164 increment_stats(STAT_FIND_PEER_START); 4441 increment_stats (STAT_FIND_PEER_START);
4165 4442
4166 if (GNUNET_YES == malicious_dropper) 4443 if (GNUNET_YES == malicious_dropper)
4167 { 4444 {
4168 if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET) 4445 if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_GET)
4169 { 4446 {
4170#if DEBUG_DHT_ROUTING 4447#if DEBUG_DHT_ROUTING
4171 if ((debug_routes) && (dhtlog_handle != NULL)) 4448 if ((debug_routes) && (dhtlog_handle != NULL))
4172 { 4449 {
4173 dhtlog_handle->insert_query (NULL, msg_ctx.unique_id, DHTLOG_GET, 4450 dhtlog_handle->insert_query (NULL, msg_ctx.unique_id,
4174 msg_ctx.hop_count, GNUNET_NO, &my_identity, 4451 DHTLOG_GET, msg_ctx.hop_count,
4175 &msg_ctx.key); 4452 GNUNET_NO, &my_identity,
4453 &msg_ctx.key);
4176 } 4454 }
4177#endif 4455#endif
4178 } 4456 }
4179 else if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT) 4457 else if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_PUT)
4180 { 4458 {
4181#if DEBUG_DHT_ROUTING 4459#if DEBUG_DHT_ROUTING
4182 if ((debug_routes) && (dhtlog_handle != NULL)) 4460 if ((debug_routes) && (dhtlog_handle != NULL))
4183 { 4461 {
4184 dhtlog_handle->insert_query (NULL, msg_ctx.unique_id, DHTLOG_PUT, 4462 dhtlog_handle->insert_query (NULL, msg_ctx.unique_id,
4185 msg_ctx.hop_count, GNUNET_NO, &my_identity, 4463 DHTLOG_PUT, msg_ctx.hop_count,
4464 GNUNET_NO, &my_identity,
4186 &msg_ctx.key); 4465 &msg_ctx.key);
4187 } 4466 }
4188#endif 4467#endif
@@ -4191,7 +4470,7 @@ handle_dht_local_route_request (void *cls, struct GNUNET_SERVER_Client *client,
4191 return; 4470 return;
4192 } 4471 }
4193 4472
4194 demultiplex_message(enc_msg, &msg_ctx); 4473 demultiplex_message (enc_msg, &msg_ctx);
4195 GNUNET_SERVER_receive_done (client, GNUNET_OK); 4474 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4196 4475
4197} 4476}
@@ -4210,59 +4489,63 @@ handle_dht_control_message (void *cls, struct GNUNET_SERVER_Client *client,
4210 const struct GNUNET_MessageHeader *message) 4489 const struct GNUNET_MessageHeader *message)
4211{ 4490{
4212 const struct GNUNET_DHT_ControlMessage *dht_control_msg = 4491 const struct GNUNET_DHT_ControlMessage *dht_control_msg =
4213 (const struct GNUNET_DHT_ControlMessage *) message; 4492 (const struct GNUNET_DHT_ControlMessage *) message;
4214#if DEBUG_DHT 4493#if DEBUG_DHT
4215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4216 "`%s:%s': Received `%s' request from client, command %d\n", my_short_id, "DHT", 4495 "`%s:%s': Received `%s' request from client, command %d\n",
4217 "CONTROL", ntohs(dht_control_msg->command)); 4496 my_short_id, "DHT", "CONTROL",
4497 ntohs (dht_control_msg->command));
4218#endif 4498#endif
4219 4499
4220 switch (ntohs(dht_control_msg->command)) 4500 switch (ntohs (dht_control_msg->command))
4221 { 4501 {
4222 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER: 4502 case GNUNET_MESSAGE_TYPE_DHT_FIND_PEER:
4223 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending self seeking find peer request!\n"); 4503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4224 GNUNET_SCHEDULER_add_now(&send_find_peer_message, NULL); 4504 "Sending self seeking find peer request!\n");
4225 break; 4505 GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL);
4506 break;
4226#if HAVE_MALICIOUS 4507#if HAVE_MALICIOUS
4227 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET: 4508 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
4228 if (ntohs(dht_control_msg->variable) > 0) 4509 if (ntohs (dht_control_msg->variable) > 0)
4229 malicious_get_frequency = ntohs(dht_control_msg->variable); 4510 malicious_get_frequency = ntohs (dht_control_msg->variable);
4230 if (malicious_get_frequency == 0) 4511 if (malicious_get_frequency == 0)
4231 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY; 4512 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
4232 if (malicious_getter != GNUNET_YES) 4513 if (malicious_getter != GNUNET_YES)
4233 GNUNET_SCHEDULER_add_now(&malicious_get_task, NULL); 4514 GNUNET_SCHEDULER_add_now (&malicious_get_task, NULL);
4234 malicious_getter = GNUNET_YES; 4515 malicious_getter = GNUNET_YES;
4235 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 4516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4236 "%s:%s Initiating malicious GET behavior, frequency %d\n", my_short_id, "DHT", malicious_get_frequency); 4517 "%s:%s Initiating malicious GET behavior, frequency %d\n",
4237 break; 4518 my_short_id, "DHT", malicious_get_frequency);
4238 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT: 4519 break;
4239 if (ntohs(dht_control_msg->variable) > 0) 4520 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
4240 malicious_put_frequency = ntohs(dht_control_msg->variable); 4521 if (ntohs (dht_control_msg->variable) > 0)
4241 if (malicious_put_frequency == 0) 4522 malicious_put_frequency = ntohs (dht_control_msg->variable);
4242 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY; 4523 if (malicious_put_frequency == 0)
4243 if (malicious_putter != GNUNET_YES) 4524 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
4244 GNUNET_SCHEDULER_add_now(&malicious_put_task, NULL); 4525 if (malicious_putter != GNUNET_YES)
4245 malicious_putter = GNUNET_YES; 4526 GNUNET_SCHEDULER_add_now (&malicious_put_task, NULL);
4246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 4527 malicious_putter = GNUNET_YES;
4247 "%s:%s Initiating malicious PUT behavior, frequency %d\n", my_short_id, "DHT", malicious_put_frequency); 4528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4248 break; 4529 "%s:%s Initiating malicious PUT behavior, frequency %d\n",
4249 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP: 4530 my_short_id, "DHT", malicious_put_frequency);
4531 break;
4532 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
4250#if DEBUG_DHT_ROUTING 4533#if DEBUG_DHT_ROUTING
4251 if ((malicious_dropper != GNUNET_YES) && (dhtlog_handle != NULL)) 4534 if ((malicious_dropper != GNUNET_YES) && (dhtlog_handle != NULL))
4252 dhtlog_handle->set_malicious(&my_identity); 4535 dhtlog_handle->set_malicious (&my_identity);
4253#endif 4536#endif
4254 malicious_dropper = GNUNET_YES; 4537 malicious_dropper = GNUNET_YES;
4255 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 4538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4256 "%s:%s Initiating malicious DROP behavior\n", my_short_id, "DHT"); 4539 "%s:%s Initiating malicious DROP behavior\n", my_short_id,
4257 break; 4540 "DHT");
4541 break;
4258#endif 4542#endif
4259 default: 4543 default:
4260 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 4544 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4261 "%s:%s Unknown control command type `%d'!\n", 4545 "%s:%s Unknown control command type `%d'!\n",
4262 my_short_id, "DHT", 4546 my_short_id, "DHT", ntohs (dht_control_msg->command));
4263 ntohs(dht_control_msg->command)); 4547 break;
4264 break; 4548 }
4265 }
4266 4549
4267 GNUNET_SERVER_receive_done (client, GNUNET_OK); 4550 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4268} 4551}
@@ -4277,8 +4560,8 @@ handle_dht_control_message (void *cls, struct GNUNET_SERVER_Client *client,
4277 * 4560 *
4278 */ 4561 */
4279static void 4562static void
4280handle_dht_local_route_stop(void *cls, struct GNUNET_SERVER_Client *client, 4563handle_dht_local_route_stop (void *cls, struct GNUNET_SERVER_Client *client,
4281 const struct GNUNET_MessageHeader *message) 4564 const struct GNUNET_MessageHeader *message)
4282{ 4565{
4283 4566
4284 const struct GNUNET_DHT_StopMessage *dht_stop_msg = 4567 const struct GNUNET_DHT_StopMessage *dht_stop_msg =
@@ -4287,10 +4570,13 @@ handle_dht_local_route_stop(void *cls, struct GNUNET_SERVER_Client *client,
4287 struct DHTRouteSource *pos; 4570 struct DHTRouteSource *pos;
4288#if DEBUG_DHT 4571#if DEBUG_DHT
4289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4290 "`%s:%s': Received `%s' request from client, uid %llu\n", my_short_id, "DHT", 4573 "`%s:%s': Received `%s' request from client, uid %llu\n",
4291 "GENERIC STOP", GNUNET_ntohll (dht_stop_msg->unique_id)); 4574 my_short_id, "DHT", "GENERIC STOP",
4575 GNUNET_ntohll (dht_stop_msg->unique_id));
4292#endif 4576#endif
4293 record = GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap, &dht_stop_msg->key); 4577 record =
4578 GNUNET_CONTAINER_multihashmap_get (forward_list.hashmap,
4579 &dht_stop_msg->key);
4294 if (record != NULL) 4580 if (record != NULL)
4295 { 4581 {
4296 pos = record->head; 4582 pos = record->head;
@@ -4300,9 +4586,10 @@ handle_dht_local_route_stop(void *cls, struct GNUNET_SERVER_Client *client,
4300 /* If the client is non-null (local request) and the client matches the requesting client, remove the entry. */ 4586 /* If the client is non-null (local request) and the client matches the requesting client, remove the entry. */
4301 if ((pos->client != NULL) && (pos->client->client_handle == client)) 4587 if ((pos->client != NULL) && (pos->client->client_handle == client))
4302 { 4588 {
4303 GNUNET_SCHEDULER_cancel(pos->delete_task); 4589 GNUNET_SCHEDULER_cancel (pos->delete_task);
4304 pos->delete_task = GNUNET_SCHEDULER_NO_TASK; 4590 pos->delete_task = GNUNET_SCHEDULER_NO_TASK;
4305 GNUNET_SCHEDULER_add_continuation (&remove_forward_entry, pos, GNUNET_SCHEDULER_REASON_PREREQ_DONE); 4591 GNUNET_SCHEDULER_add_continuation (&remove_forward_entry, pos,
4592 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
4306 } 4593 }
4307 pos = pos->next; 4594 pos = pos->next;
4308 } 4595 }
@@ -4317,29 +4604,34 @@ handle_dht_local_route_stop(void *cls, struct GNUNET_SERVER_Client *client,
4317 */ 4604 */
4318static int 4605static int
4319handle_dht_p2p_route_request (void *cls, 4606handle_dht_p2p_route_request (void *cls,
4320 const struct GNUNET_PeerIdentity *peer, 4607 const struct GNUNET_PeerIdentity *peer,
4321 const struct GNUNET_MessageHeader *message, 4608 const struct GNUNET_MessageHeader *message,
4322 const struct GNUNET_TRANSPORT_ATS_Information *atsi) 4609 const struct GNUNET_TRANSPORT_ATS_Information
4610 *atsi)
4323{ 4611{
4324#if DEBUG_DHT 4612#if DEBUG_DHT
4325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4326 "`%s:%s': Received P2P request from peer %s\n", my_short_id, "DHT", GNUNET_i2s(peer)); 4614 "`%s:%s': Received P2P request from peer %s\n", my_short_id,
4615 "DHT", GNUNET_i2s (peer));
4327#endif 4616#endif
4328 struct GNUNET_DHT_P2PRouteMessage *incoming = (struct GNUNET_DHT_P2PRouteMessage *)message; 4617 struct GNUNET_DHT_P2PRouteMessage *incoming =
4329 struct GNUNET_MessageHeader *enc_msg = (struct GNUNET_MessageHeader *)&incoming[1]; 4618 (struct GNUNET_DHT_P2PRouteMessage *) message;
4619 struct GNUNET_MessageHeader *enc_msg =
4620 (struct GNUNET_MessageHeader *) &incoming[1];
4330 struct DHT_MessageContext *msg_ctx; 4621 struct DHT_MessageContext *msg_ctx;
4331 4622
4332 if (ntohs(enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_P2P_PING) /* Throw these away. FIXME: Don't throw these away? (reply)*/ 4623 if (ntohs (enc_msg->type) == GNUNET_MESSAGE_TYPE_DHT_P2P_PING) /* Throw these away. FIXME: Don't throw these away? (reply) */
4333 { 4624 {
4334#if DEBUG_PING 4625#if DEBUG_PING
4335 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s Received P2P Ping message.\n", my_short_id, "DHT"); 4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 "%s:%s Received P2P Ping message.\n", my_short_id, "DHT");
4336#endif 4628#endif
4337 return GNUNET_YES; 4629 return GNUNET_YES;
4338 } 4630 }
4339 4631
4340 if (ntohs(enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1) 4632 if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4341 { 4633 {
4342 GNUNET_break_op(0); 4634 GNUNET_break_op (0);
4343 return GNUNET_YES; 4635 return GNUNET_YES;
4344 } 4636 }
4345 4637
@@ -4349,51 +4641,58 @@ handle_dht_p2p_route_request (void *cls,
4349 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 4641 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4350 { 4642 {
4351 /** Log routes that die due to high load! */ 4643 /** Log routes that die due to high load! */
4352 dhtlog_handle->insert_route (NULL, GNUNET_ntohll(incoming->unique_id), DHTLOG_ROUTE, 4644 dhtlog_handle->insert_route (NULL,
4353 ntohl(incoming->hop_count), GNUNET_SYSERR, 4645 GNUNET_ntohll (incoming->unique_id),
4354 &my_identity, &incoming->key, peer, 4646 DHTLOG_ROUTE,
4355 NULL); 4647 ntohl (incoming->hop_count),
4648 GNUNET_SYSERR, &my_identity,
4649 &incoming->key, peer, NULL);
4356 } 4650 }
4357#endif 4651#endif
4358 return GNUNET_YES; 4652 return GNUNET_YES;
4359 } 4653 }
4360 4654
4361 if (get_max_send_delay().rel_value > MAX_REQUEST_TIME.rel_value) 4655 if (get_max_send_delay ().rel_value > MAX_REQUEST_TIME.rel_value)
4362 { 4656 {
4363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending of previous replies took too long, backing off!\n"); 4657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4364 increment_stats("# route requests dropped due to high load"); 4658 "Sending of previous replies took too long, backing off!\n");
4365 decrease_max_send_delay(get_max_send_delay()); 4659 increment_stats ("# route requests dropped due to high load");
4660 decrease_max_send_delay (get_max_send_delay ());
4366#if DEBUG_DHT_ROUTING 4661#if DEBUG_DHT_ROUTING
4367 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 4662 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4368 { 4663 {
4369 /** Log routes that die due to high load! */ 4664 /** Log routes that die due to high load! */
4370 dhtlog_handle->insert_route (NULL, GNUNET_ntohll(incoming->unique_id), DHTLOG_ROUTE, 4665 dhtlog_handle->insert_route (NULL,
4371 ntohl(incoming->hop_count), GNUNET_SYSERR, 4666 GNUNET_ntohll (incoming->unique_id),
4372 &my_identity, &incoming->key, peer, 4667 DHTLOG_ROUTE,
4373 NULL); 4668 ntohl (incoming->hop_count),
4374 } 4669 GNUNET_SYSERR, &my_identity,
4670 &incoming->key, peer, NULL);
4671 }
4375#endif 4672#endif
4376 return GNUNET_YES; 4673 return GNUNET_YES;
4377 } 4674 }
4378 msg_ctx = GNUNET_malloc(sizeof (struct DHT_MessageContext)); 4675 msg_ctx = GNUNET_malloc (sizeof (struct DHT_MessageContext));
4379 msg_ctx->bloom = GNUNET_CONTAINER_bloomfilter_init(incoming->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K); 4676 msg_ctx->bloom =
4380 GNUNET_assert(msg_ctx->bloom != NULL); 4677 GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
4381 msg_ctx->hop_count = ntohl(incoming->hop_count); 4678 DHT_BLOOM_K);
4382 memcpy(&msg_ctx->key, &incoming->key, sizeof(GNUNET_HashCode)); 4679 GNUNET_assert (msg_ctx->bloom != NULL);
4383 msg_ctx->replication = ntohl(incoming->desired_replication_level); 4680 msg_ctx->hop_count = ntohl (incoming->hop_count);
4384 msg_ctx->unique_id = GNUNET_ntohll(incoming->unique_id); 4681 memcpy (&msg_ctx->key, &incoming->key, sizeof (GNUNET_HashCode));
4385 msg_ctx->msg_options = ntohl(incoming->options); 4682 msg_ctx->replication = ntohl (incoming->desired_replication_level);
4386 msg_ctx->network_size = ntohl(incoming->network_size); 4683 msg_ctx->unique_id = GNUNET_ntohll (incoming->unique_id);
4684 msg_ctx->msg_options = ntohl (incoming->options);
4685 msg_ctx->network_size = ntohl (incoming->network_size);
4387 msg_ctx->peer = peer; 4686 msg_ctx->peer = peer;
4388 msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE; 4687 msg_ctx->importance = DHT_DEFAULT_P2P_IMPORTANCE;
4389 msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT; 4688 msg_ctx->timeout = DHT_DEFAULT_P2P_TIMEOUT;
4390 demultiplex_message (enc_msg, msg_ctx); 4689 demultiplex_message (enc_msg, msg_ctx);
4391 if (msg_ctx->bloom != NULL) 4690 if (msg_ctx->bloom != NULL)
4392 { 4691 {
4393 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom); 4692 GNUNET_CONTAINER_bloomfilter_free (msg_ctx->bloom);
4394 msg_ctx->bloom = NULL; 4693 msg_ctx->bloom = NULL;
4395 } 4694 }
4396 GNUNET_free(msg_ctx); 4695 GNUNET_free (msg_ctx);
4397 return GNUNET_YES; 4696 return GNUNET_YES;
4398} 4697}
4399 4698
@@ -4403,21 +4702,25 @@ handle_dht_p2p_route_request (void *cls,
4403 */ 4702 */
4404static int 4703static int
4405handle_dht_p2p_route_result (void *cls, 4704handle_dht_p2p_route_result (void *cls,
4406 const struct GNUNET_PeerIdentity *peer, 4705 const struct GNUNET_PeerIdentity *peer,
4407 const struct GNUNET_MessageHeader *message, 4706 const struct GNUNET_MessageHeader *message,
4408 const struct GNUNET_TRANSPORT_ATS_Information *atsi) 4707 const struct GNUNET_TRANSPORT_ATS_Information
4708 *atsi)
4409{ 4709{
4410#if DEBUG_DHT 4710#if DEBUG_DHT
4411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4412 "`%s:%s': Received request from peer %s\n", my_short_id, "DHT", GNUNET_i2s(peer)); 4712 "`%s:%s': Received request from peer %s\n", my_short_id, "DHT",
4713 GNUNET_i2s (peer));
4413#endif 4714#endif
4414 struct GNUNET_DHT_P2PRouteResultMessage *incoming = (struct GNUNET_DHT_P2PRouteResultMessage *)message; 4715 struct GNUNET_DHT_P2PRouteResultMessage *incoming =
4415 struct GNUNET_MessageHeader *enc_msg = (struct GNUNET_MessageHeader *)&incoming[1]; 4716 (struct GNUNET_DHT_P2PRouteResultMessage *) message;
4717 struct GNUNET_MessageHeader *enc_msg =
4718 (struct GNUNET_MessageHeader *) &incoming[1];
4416 struct DHT_MessageContext msg_ctx; 4719 struct DHT_MessageContext msg_ctx;
4417 4720
4418 if (ntohs(enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1) 4721 if (ntohs (enc_msg->size) >= GNUNET_SERVER_MAX_MESSAGE_SIZE - 1)
4419 { 4722 {
4420 GNUNET_break_op(0); 4723 GNUNET_break_op (0);
4421 return GNUNET_YES; 4724 return GNUNET_YES;
4422 } 4725 }
4423 4726
@@ -4427,27 +4730,31 @@ handle_dht_p2p_route_result (void *cls,
4427 if ((debug_routes_extended) && (dhtlog_handle != NULL)) 4730 if ((debug_routes_extended) && (dhtlog_handle != NULL))
4428 { 4731 {
4429 /** Log routes that die due to high load! */ 4732 /** Log routes that die due to high load! */
4430 dhtlog_handle->insert_route (NULL, GNUNET_ntohll(incoming->unique_id), DHTLOG_ROUTE, 4733 dhtlog_handle->insert_route (NULL,
4431 ntohl(incoming->hop_count), GNUNET_SYSERR, 4734 GNUNET_ntohll (incoming->unique_id),
4432 &my_identity, &incoming->key, peer, 4735 DHTLOG_ROUTE,
4433 NULL); 4736 ntohl (incoming->hop_count),
4737 GNUNET_SYSERR, &my_identity,
4738 &incoming->key, peer, NULL);
4434 } 4739 }
4435#endif 4740#endif
4436 return GNUNET_YES; 4741 return GNUNET_YES;
4437 } 4742 }
4438 4743
4439 memset(&msg_ctx, 0, sizeof(struct DHT_MessageContext)); 4744 memset (&msg_ctx, 0, sizeof (struct DHT_MessageContext));
4440 // FIXME: call GNUNET_BLOCK_evaluate (...) -- instead of doing your own bloomfilter! 4745 // FIXME: call GNUNET_BLOCK_evaluate (...) -- instead of doing your own bloomfilter!
4441 msg_ctx.bloom = GNUNET_CONTAINER_bloomfilter_init(incoming->bloomfilter, DHT_BLOOM_SIZE, DHT_BLOOM_K); 4746 msg_ctx.bloom =
4442 GNUNET_assert(msg_ctx.bloom != NULL); 4747 GNUNET_CONTAINER_bloomfilter_init (incoming->bloomfilter, DHT_BLOOM_SIZE,
4443 memcpy(&msg_ctx.key, &incoming->key, sizeof(GNUNET_HashCode)); 4748 DHT_BLOOM_K);
4444 msg_ctx.unique_id = GNUNET_ntohll(incoming->unique_id); 4749 GNUNET_assert (msg_ctx.bloom != NULL);
4445 msg_ctx.msg_options = ntohl(incoming->options); 4750 memcpy (&msg_ctx.key, &incoming->key, sizeof (GNUNET_HashCode));
4446 msg_ctx.hop_count = ntohl(incoming->hop_count); 4751 msg_ctx.unique_id = GNUNET_ntohll (incoming->unique_id);
4752 msg_ctx.msg_options = ntohl (incoming->options);
4753 msg_ctx.hop_count = ntohl (incoming->hop_count);
4447 msg_ctx.peer = peer; 4754 msg_ctx.peer = peer;
4448 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */ 4755 msg_ctx.importance = DHT_DEFAULT_P2P_IMPORTANCE + 2; /* Make result routing a higher priority */
4449 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT; 4756 msg_ctx.timeout = DHT_DEFAULT_P2P_TIMEOUT;
4450 route_result_message(enc_msg, &msg_ctx); 4757 route_result_message (enc_msg, &msg_ctx);
4451 return GNUNET_YES; 4758 return GNUNET_YES;
4452} 4759}
4453 4760
@@ -4464,14 +4771,13 @@ process_hello (void *cls, const struct GNUNET_MessageHeader *message)
4464{ 4771{
4465#if DEBUG_DHT 4772#if DEBUG_DHT
4466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4467 "Received our `%s' from transport service\n", 4774 "Received our `%s' from transport service\n", "HELLO");
4468 "HELLO");
4469#endif 4775#endif
4470 4776
4471 GNUNET_assert (message != NULL); 4777 GNUNET_assert (message != NULL);
4472 GNUNET_free_non_null(my_hello); 4778 GNUNET_free_non_null (my_hello);
4473 my_hello = GNUNET_malloc(ntohs(message->size)); 4779 my_hello = GNUNET_malloc (ntohs (message->size));
4474 memcpy(my_hello, message, ntohs(message->size)); 4780 memcpy (my_hello, message, ntohs (message->size));
4475} 4781}
4476 4782
4477 4783
@@ -4489,18 +4795,21 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4489 4795
4490 if (transport_handle != NULL) 4796 if (transport_handle != NULL)
4491 { 4797 {
4492 GNUNET_free_non_null(my_hello); 4798 GNUNET_free_non_null (my_hello);
4493 GNUNET_TRANSPORT_get_hello_cancel(transport_handle, &process_hello, NULL); 4799 GNUNET_TRANSPORT_get_hello_cancel (transport_handle, &process_hello,
4494 GNUNET_TRANSPORT_disconnect(transport_handle); 4800 NULL);
4801 GNUNET_TRANSPORT_disconnect (transport_handle);
4495 } 4802 }
4496 for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS; bucket_count++) 4803 for (bucket_count = lowest_bucket; bucket_count < MAX_BUCKETS;
4804 bucket_count++)
4497 { 4805 {
4498 while (k_buckets[bucket_count].head != NULL) 4806 while (k_buckets[bucket_count].head != NULL)
4499 { 4807 {
4500 pos = k_buckets[bucket_count].head; 4808 pos = k_buckets[bucket_count].head;
4501#if DEBUG_DHT 4809#if DEBUG_DHT
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4503 "%s:%s Removing peer %s from bucket %d!\n", my_short_id, "DHT", GNUNET_i2s(&pos->id), bucket_count); 4811 "%s:%s Removing peer %s from bucket %d!\n", my_short_id,
4812 "DHT", GNUNET_i2s (&pos->id), bucket_count);
4504#endif 4813#endif
4505 delete_peer (pos, bucket_count); 4814 delete_peer (pos, bucket_count);
4506 } 4815 }
@@ -4530,7 +4839,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4530 } 4839 }
4531 if (dhtlog_handle != NULL) 4840 if (dhtlog_handle != NULL)
4532 { 4841 {
4533 GNUNET_DHTLOG_disconnect(dhtlog_handle); 4842 GNUNET_DHTLOG_disconnect (dhtlog_handle);
4534 dhtlog_handle = NULL; 4843 dhtlog_handle = NULL;
4535 } 4844 }
4536 if (block_context != NULL) 4845 if (block_context != NULL)
@@ -4538,7 +4847,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4538 GNUNET_BLOCK_context_destroy (block_context); 4847 GNUNET_BLOCK_context_destroy (block_context);
4539 block_context = NULL; 4848 block_context = NULL;
4540 } 4849 }
4541 GNUNET_free_non_null(my_short_id); 4850 GNUNET_free_non_null (my_short_id);
4542 my_short_id = NULL; 4851 my_short_id = NULL;
4543} 4852}
4544 4853
@@ -4578,8 +4887,10 @@ core_init (void *cls,
4578 /* Copy our identity so we can use it */ 4887 /* Copy our identity so we can use it */
4579 memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity)); 4888 memcpy (&my_identity, identity, sizeof (struct GNUNET_PeerIdentity));
4580 if (my_short_id != NULL) 4889 if (my_short_id != NULL)
4581 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "%s Receive CORE INIT message but have already been initialized! Did CORE fail?\n", "DHT SERVICE"); 4890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4582 my_short_id = GNUNET_strdup(GNUNET_i2s(&my_identity)); 4891 "%s Receive CORE INIT message but have already been initialized! Did CORE fail?\n",
4892 "DHT SERVICE");
4893 my_short_id = GNUNET_strdup (GNUNET_i2s (&my_identity));
4583 /* Set the server to local variable */ 4894 /* Set the server to local variable */
4584 coreAPI = server; 4895 coreAPI = server;
4585 4896
@@ -4589,8 +4900,10 @@ core_init (void *cls,
4589 4900
4590 4901
4591static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = { 4902static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = {
4592 {&handle_dht_local_route_request, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE, 0}, 4903 {&handle_dht_local_route_request, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE,
4593 {&handle_dht_local_route_stop, NULL, GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP, 0}, 4904 0},
4905 {&handle_dht_local_route_stop, NULL,
4906 GNUNET_MESSAGE_TYPE_DHT_LOCAL_ROUTE_STOP, 0},
4594 {&handle_dht_control_message, NULL, GNUNET_MESSAGE_TYPE_DHT_CONTROL, 0}, 4907 {&handle_dht_control_message, NULL, GNUNET_MESSAGE_TYPE_DHT_CONTROL, 0},
4595 {NULL, NULL, 0, 0} 4908 {NULL, NULL, 0, 0}
4596}; 4909};
@@ -4610,37 +4923,46 @@ static struct GNUNET_CORE_MessageHandler core_handlers[] = {
4610 * @param peer peer identity this notification is about 4923 * @param peer peer identity this notification is about
4611 * @param atsi performance data 4924 * @param atsi performance data
4612 */ 4925 */
4613static void 4926static void
4614handle_core_connect (void *cls, 4927handle_core_connect (void *cls,
4615 const struct GNUNET_PeerIdentity * peer, 4928 const struct GNUNET_PeerIdentity *peer,
4616 const struct GNUNET_TRANSPORT_ATS_Information *atsi) 4929 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
4617{ 4930{
4618 struct PeerInfo *ret; 4931 struct PeerInfo *ret;
4619 4932
4620#if DEBUG_DHT 4933#if DEBUG_DHT
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4622 "%s:%s Receives core connect message for peer %s distance %d!\n", my_short_id, "dht", GNUNET_i2s(peer), distance); 4935 "%s:%s Receives core connect message for peer %s distance %d!\n",
4936 my_short_id, "dht", GNUNET_i2s (peer), distance);
4623#endif 4937#endif
4624 4938
4625 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey)) 4939 if (GNUNET_YES ==
4940 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
4941 &peer->hashPubKey))
4626 { 4942 {
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s:%s Received %s message for peer %s, but already have peer in RT!", my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s(peer)); 4943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4944 "%s:%s Received %s message for peer %s, but already have peer in RT!",
4945 my_short_id, "DHT", "CORE CONNECT", GNUNET_i2s (peer));
4628 return; 4946 return;
4629 } 4947 }
4630 4948
4631 if (datacache != NULL) 4949 if (datacache != NULL)
4632 GNUNET_DATACACHE_put(datacache, &peer->hashPubKey, sizeof(struct GNUNET_PeerIdentity), (const char *)peer, GNUNET_BLOCK_TYPE_DHT_HELLO, GNUNET_TIME_absolute_get_forever()); 4950 GNUNET_DATACACHE_put (datacache, &peer->hashPubKey,
4633 ret = try_add_peer(peer, 4951 sizeof (struct GNUNET_PeerIdentity),
4634 find_current_bucket(&peer->hashPubKey), 4952 (const char *) peer, GNUNET_BLOCK_TYPE_DHT_HELLO,
4635 atsi); 4953 GNUNET_TIME_absolute_get_forever ());
4954 ret = try_add_peer (peer, find_current_bucket (&peer->hashPubKey), atsi);
4636 if (ret != NULL) 4955 if (ret != NULL)
4637 { 4956 {
4638 newly_found_peers++; 4957 newly_found_peers++;
4639 GNUNET_CONTAINER_multihashmap_put(all_known_peers, &peer->hashPubKey, ret, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 4958 GNUNET_CONTAINER_multihashmap_put (all_known_peers, &peer->hashPubKey,
4959 ret,
4960 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4640 } 4961 }
4641#if DEBUG_DHT 4962#if DEBUG_DHT
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 "%s:%s Adding peer to routing list: %s\n", my_short_id, "DHT", ret == NULL ? "NOT ADDED" : "PEER ADDED"); 4964 "%s:%s Adding peer to routing list: %s\n", my_short_id, "DHT",
4965 ret == NULL ? "NOT ADDED" : "PEER ADDED");
4644#endif 4966#endif
4645} 4967}
4646 4968
@@ -4652,27 +4974,35 @@ handle_core_connect (void *cls,
4652 * @param peer peer identity this notification is about 4974 * @param peer peer identity this notification is about
4653 */ 4975 */
4654static void 4976static void
4655handle_core_disconnect (void *cls, 4977handle_core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4656 const struct
4657 GNUNET_PeerIdentity * peer)
4658{ 4978{
4659 struct PeerInfo *to_remove; 4979 struct PeerInfo *to_remove;
4660 int current_bucket; 4980 int current_bucket;
4661 4981
4662 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s: Received peer disconnect message for peer `%s' from %s\n", my_short_id, "DHT", GNUNET_i2s(peer), "CORE"); 4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4983 "%s:%s: Received peer disconnect message for peer `%s' from %s\n",
4984 my_short_id, "DHT", GNUNET_i2s (peer), "CORE");
4663 4985
4664 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey)) 4986 if (GNUNET_YES !=
4987 GNUNET_CONTAINER_multihashmap_contains (all_known_peers,
4988 &peer->hashPubKey))
4665 { 4989 {
4666 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s:%s: do not have peer `%s' in RT, can't disconnect!\n", my_short_id, "DHT", GNUNET_i2s(peer)); 4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4991 "%s:%s: do not have peer `%s' in RT, can't disconnect!\n",
4992 my_short_id, "DHT", GNUNET_i2s (peer));
4667 return; 4993 return;
4668 } 4994 }
4669 increment_stats(STAT_DISCONNECTS); 4995 increment_stats (STAT_DISCONNECTS);
4670 GNUNET_assert(GNUNET_CONTAINER_multihashmap_contains(all_known_peers, &peer->hashPubKey)); 4996 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains
4671 to_remove = GNUNET_CONTAINER_multihashmap_get(all_known_peers, &peer->hashPubKey); 4997 (all_known_peers, &peer->hashPubKey));
4998 to_remove =
4999 GNUNET_CONTAINER_multihashmap_get (all_known_peers, &peer->hashPubKey);
4672 GNUNET_assert (to_remove != NULL); 5000 GNUNET_assert (to_remove != NULL);
4673 GNUNET_assert(0 == memcmp(peer, &to_remove->id, sizeof(struct GNUNET_PeerIdentity))); 5001 GNUNET_assert (0 ==
4674 current_bucket = find_current_bucket(&to_remove->id.hashPubKey); 5002 memcmp (peer, &to_remove->id,
4675 delete_peer(to_remove, current_bucket); 5003 sizeof (struct GNUNET_PeerIdentity)));
5004 current_bucket = find_current_bucket (&to_remove->id.hashPubKey);
5005 delete_peer (to_remove, current_bucket);
4676} 5006}
4677 5007
4678 5008
@@ -4696,12 +5026,12 @@ run (void *cls,
4696 datacache = GNUNET_DATACACHE_create (cfg, "dhtcache"); 5026 datacache = GNUNET_DATACACHE_create (cfg, "dhtcache");
4697 GNUNET_SERVER_add_handlers (server, plugin_handlers); 5027 GNUNET_SERVER_add_handlers (server, plugin_handlers);
4698 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL); 5028 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
4699 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */ 5029 coreAPI = GNUNET_CORE_connect (cfg, /* Main configuration */
4700 DEFAULT_CORE_QUEUE_SIZE, /* queue size */ 5030 DEFAULT_CORE_QUEUE_SIZE, /* queue size */
4701 NULL, /* Closure passed to DHT functions */ 5031 NULL, /* Closure passed to DHT functions */
4702 &core_init, /* Call core_init once connected */ 5032 &core_init, /* Call core_init once connected */
4703 &handle_core_connect, /* Handle connects */ 5033 &handle_core_connect, /* Handle connects */
4704 &handle_core_disconnect, /* remove peers on disconnects */ 5034 &handle_core_disconnect, /* remove peers on disconnects */
4705 NULL, /* Do we care about "status" updates? */ 5035 NULL, /* Do we care about "status" updates? */
4706 NULL, /* Don't want notified about all incoming messages */ 5036 NULL, /* Don't want notified about all incoming messages */
4707 GNUNET_NO, /* For header only inbound notification */ 5037 GNUNET_NO, /* For header only inbound notification */
@@ -4711,60 +5041,61 @@ run (void *cls,
4711 5041
4712 if (coreAPI == NULL) 5042 if (coreAPI == NULL)
4713 return; 5043 return;
4714 transport_handle = GNUNET_TRANSPORT_connect(cfg, 5044 transport_handle = GNUNET_TRANSPORT_connect (cfg,
4715 NULL, NULL, NULL, NULL, NULL); 5045 NULL, NULL, NULL, NULL, NULL);
4716 if (transport_handle != NULL) 5046 if (transport_handle != NULL)
4717 GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL); 5047 GNUNET_TRANSPORT_get_hello (transport_handle, &process_hello, NULL);
4718 else 5048 else
4719 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 5049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4720 "Failed to connect to transport service!\n"); 5050 "Failed to connect to transport service!\n");
4721 block_context = GNUNET_BLOCK_context_create (cfg); 5051 block_context = GNUNET_BLOCK_context_create (cfg);
4722 lowest_bucket = MAX_BUCKETS - 1; 5052 lowest_bucket = MAX_BUCKETS - 1;
4723 forward_list.hashmap = GNUNET_CONTAINER_multihashmap_create(MAX_OUTSTANDING_FORWARDS / 10); 5053 forward_list.hashmap =
4724 forward_list.minHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN); 5054 GNUNET_CONTAINER_multihashmap_create (MAX_OUTSTANDING_FORWARDS / 10);
4725 all_known_peers = GNUNET_CONTAINER_multihashmap_create(MAX_BUCKETS / 8); 5055 forward_list.minHeap =
4726 recent_find_peer_requests = GNUNET_CONTAINER_multihashmap_create(MAX_BUCKETS / 8); 5056 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4727 GNUNET_assert(all_known_peers != NULL); 5057 all_known_peers = GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
4728 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging")) 5058 recent_find_peer_requests =
5059 GNUNET_CONTAINER_multihashmap_create (MAX_BUCKETS / 8);
5060 GNUNET_assert (all_known_peers != NULL);
5061 if (GNUNET_YES ==
5062 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
5063 "mysql_logging"))
4729 { 5064 {
4730 debug_routes = GNUNET_YES; 5065 debug_routes = GNUNET_YES;
4731 } 5066 }
4732 5067
4733 if (GNUNET_YES == 5068 if (GNUNET_YES ==
4734 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5069 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "strict_kademlia"))
4735 "strict_kademlia"))
4736 { 5070 {
4737 strict_kademlia = GNUNET_YES; 5071 strict_kademlia = GNUNET_YES;
4738 } 5072 }
4739 5073
4740 if (GNUNET_YES == 5074 if (GNUNET_YES ==
4741 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5075 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_on_closest"))
4742 "stop_on_closest"))
4743 { 5076 {
4744 stop_on_closest = GNUNET_YES; 5077 stop_on_closest = GNUNET_YES;
4745 } 5078 }
4746 5079
4747 if (GNUNET_YES == 5080 if (GNUNET_YES ==
4748 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5081 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_found"))
4749 "stop_found"))
4750 { 5082 {
4751 stop_on_found = GNUNET_YES; 5083 stop_on_found = GNUNET_YES;
4752 } 5084 }
4753 5085
4754 if (GNUNET_YES == 5086 if (GNUNET_YES ==
4755 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5087 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_getter"))
4756 "malicious_getter"))
4757 { 5088 {
4758 malicious_getter = GNUNET_YES; 5089 malicious_getter = GNUNET_YES;
4759 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", 5090 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
4760 "MALICIOUS_GET_FREQUENCY", 5091 "MALICIOUS_GET_FREQUENCY",
4761 &malicious_get_frequency)) 5092 &malicious_get_frequency))
4762 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY; 5093 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
4763 } 5094 }
4764 5095
4765 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", 5096 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
4766 "MAX_HOPS", 5097 "MAX_HOPS",
4767 &max_hops)) 5098 &max_hops))
4768 { 5099 {
4769 max_hops = DEFAULT_MAX_HOPS; 5100 max_hops = DEFAULT_MAX_HOPS;
4770 } 5101 }
@@ -4776,47 +5107,52 @@ run (void *cls,
4776 } 5107 }
4777 5108
4778 if (GNUNET_YES == 5109 if (GNUNET_YES ==
4779 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5110 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_putter"))
4780 "malicious_putter"))
4781 { 5111 {
4782 malicious_putter = GNUNET_YES; 5112 malicious_putter = GNUNET_YES;
4783 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", 5113 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
4784 "MALICIOUS_PUT_FREQUENCY", 5114 "MALICIOUS_PUT_FREQUENCY",
4785 &malicious_put_frequency)) 5115 &malicious_put_frequency))
4786 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY; 5116 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
4787 } 5117 }
4788 5118
4789 dht_republish_frequency = GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY; 5119 dht_republish_frequency = GNUNET_DHT_DEFAULT_REPUBLISH_FREQUENCY;
4790 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "DHT", "REPLICATION_FREQUENCY", &temp_config_num)) 5120 if (GNUNET_OK ==
5121 GNUNET_CONFIGURATION_get_value_number (cfg, "DHT",
5122 "REPLICATION_FREQUENCY",
5123 &temp_config_num))
4791 { 5124 {
4792 dht_republish_frequency = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, temp_config_num); 5125 dht_republish_frequency =
5126 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES,
5127 temp_config_num);
4793 } 5128 }
4794 5129
4795 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "DHT", "bucket_size", &temp_config_num)) 5130 if (GNUNET_OK ==
5131 GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "bucket_size",
5132 &temp_config_num))
4796 { 5133 {
4797 bucket_size = (unsigned int)temp_config_num; 5134 bucket_size = (unsigned int) temp_config_num;
4798 } 5135 }
4799 5136
4800 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, "DHT", "kad_alpha", &kademlia_replication)) 5137 if (GNUNET_OK !=
5138 GNUNET_CONFIGURATION_get_value_number (cfg, "DHT", "kad_alpha",
5139 &kademlia_replication))
4801 { 5140 {
4802 kademlia_replication = DEFAULT_KADEMLIA_REPLICATION; 5141 kademlia_replication = DEFAULT_KADEMLIA_REPLICATION;
4803 } 5142 }
4804 5143
4805 if (GNUNET_YES == 5144 if (GNUNET_YES ==
4806 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5145 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "malicious_dropper"))
4807 "malicious_dropper"))
4808 { 5146 {
4809 malicious_dropper = GNUNET_YES; 5147 malicious_dropper = GNUNET_YES;
4810 } 5148 }
4811 5149
4812 if (GNUNET_YES == 5150 if (GNUNET_YES ==
4813 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5151 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "republish"))
4814 "republish"))
4815 do_republish = GNUNET_NO; 5152 do_republish = GNUNET_NO;
4816 5153
4817 if (GNUNET_NO == 5154 if (GNUNET_NO ==
4818 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5155 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "do_find_peer"))
4819 "do_find_peer"))
4820 { 5156 {
4821 do_find_peer = GNUNET_NO; 5157 do_find_peer = GNUNET_NO;
4822 } 5158 }
@@ -4824,13 +5160,12 @@ run (void *cls,
4824 do_find_peer = GNUNET_YES; 5160 do_find_peer = GNUNET_YES;
4825 5161
4826 if (GNUNET_YES == 5162 if (GNUNET_YES ==
4827 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5163 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "use_real_distance"))
4828 "use_real_distance"))
4829 use_real_distance = GNUNET_YES; 5164 use_real_distance = GNUNET_YES;
4830 5165
4831 if (GNUNET_YES == 5166 if (GNUNET_YES ==
4832 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", 5167 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
4833 "mysql_logging_extended")) 5168 "mysql_logging_extended"))
4834 { 5169 {
4835 debug_routes = GNUNET_YES; 5170 debug_routes = GNUNET_YES;
4836 debug_routes_extended = GNUNET_YES; 5171 debug_routes_extended = GNUNET_YES;
@@ -4839,7 +5174,7 @@ run (void *cls,
4839#if DEBUG_DHT_ROUTING 5174#if DEBUG_DHT_ROUTING
4840 if (GNUNET_YES == debug_routes) 5175 if (GNUNET_YES == debug_routes)
4841 { 5176 {
4842 dhtlog_handle = GNUNET_DHTLOG_connect(cfg); 5177 dhtlog_handle = GNUNET_DHTLOG_connect (cfg);
4843 if (dhtlog_handle == NULL) 5178 if (dhtlog_handle == NULL)
4844 { 5179 {
4845 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 5180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -4850,77 +5185,85 @@ run (void *cls,
4850 5185
4851 converge_option = DHT_CONVERGE_SQUARE; 5186 converge_option = DHT_CONVERGE_SQUARE;
4852 if (GNUNET_YES == 5187 if (GNUNET_YES ==
4853 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5188 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "converge_linear"))
4854 "converge_linear"))
4855 { 5189 {
4856 converge_option = DHT_CONVERGE_LINEAR; 5190 converge_option = DHT_CONVERGE_LINEAR;
4857 } 5191 }
4858 else if (GNUNET_YES == 5192 else if (GNUNET_YES ==
4859 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5193 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
4860 "converge_exponential")) 5194 "converge_exponential"))
4861 { 5195 {
4862 converge_option = DHT_CONVERGE_EXPONENTIAL; 5196 converge_option = DHT_CONVERGE_EXPONENTIAL;
4863 } 5197 }
4864 else if (GNUNET_YES == 5198 else if (GNUNET_YES ==
4865 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5199 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
4866 "converge_random")) 5200 "converge_random"))
4867 { 5201 {
4868 converge_option = DHT_CONVERGE_RANDOM; 5202 converge_option = DHT_CONVERGE_RANDOM;
4869 } 5203 }
4870 else if (GNUNET_YES == 5204 else if (GNUNET_YES ==
4871 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", 5205 GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
4872 "converge_binary")) 5206 "converge_binary"))
4873 { 5207 {
4874 converge_option = DHT_CONVERGE_BINARY; 5208 converge_option = DHT_CONVERGE_BINARY;
4875 } 5209 }
4876 5210
4877 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "dht", "converge_modifier", &converge_modifier_buf)) 5211 if (GNUNET_OK ==
5212 GNUNET_CONFIGURATION_get_value_string (cfg, "dht", "converge_modifier",
5213 &converge_modifier_buf))
4878 { 5214 {
4879 if (1 != sscanf(converge_modifier_buf, "%f", &converge_modifier)) 5215 if (1 != sscanf (converge_modifier_buf, "%f", &converge_modifier))
4880 { 5216 {
4881 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read decimal value for %s from `%s'\n", "CONVERGE_MODIFIER", converge_modifier_buf); 5217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5218 "Failed to read decimal value for %s from `%s'\n",
5219 "CONVERGE_MODIFIER", converge_modifier_buf);
4882 converge_modifier = 0.0; 5220 converge_modifier = 0.0;
4883 } 5221 }
4884 GNUNET_free(converge_modifier_buf); 5222 GNUNET_free (converge_modifier_buf);
4885 } 5223 }
4886 5224
4887 stats = GNUNET_STATISTICS_create("dht", cfg); 5225 stats = GNUNET_STATISTICS_create ("dht", cfg);
4888 5226
4889 if (stats != NULL) 5227 if (stats != NULL)
4890 { 5228 {
4891 GNUNET_STATISTICS_set(stats, STAT_ROUTES, 0, GNUNET_NO); 5229 GNUNET_STATISTICS_set (stats, STAT_ROUTES, 0, GNUNET_NO);
4892 GNUNET_STATISTICS_set(stats, STAT_ROUTE_FORWARDS, 0, GNUNET_NO); 5230 GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS, 0, GNUNET_NO);
4893 GNUNET_STATISTICS_set(stats, STAT_ROUTE_FORWARDS_CLOSEST, 0, GNUNET_NO); 5231 GNUNET_STATISTICS_set (stats, STAT_ROUTE_FORWARDS_CLOSEST, 0,
4894 GNUNET_STATISTICS_set(stats, STAT_RESULTS, 0, GNUNET_NO); 5232 GNUNET_NO);
4895 GNUNET_STATISTICS_set(stats, STAT_RESULTS_TO_CLIENT, 0, GNUNET_NO); 5233 GNUNET_STATISTICS_set (stats, STAT_RESULTS, 0, GNUNET_NO);
4896 GNUNET_STATISTICS_set(stats, STAT_RESULT_FORWARDS, 0, GNUNET_NO); 5234 GNUNET_STATISTICS_set (stats, STAT_RESULTS_TO_CLIENT, 0, GNUNET_NO);
4897 GNUNET_STATISTICS_set(stats, STAT_GETS, 0, GNUNET_NO); 5235 GNUNET_STATISTICS_set (stats, STAT_RESULT_FORWARDS, 0, GNUNET_NO);
4898 GNUNET_STATISTICS_set(stats, STAT_PUTS, 0, GNUNET_NO); 5236 GNUNET_STATISTICS_set (stats, STAT_GETS, 0, GNUNET_NO);
4899 GNUNET_STATISTICS_set(stats, STAT_PUTS_INSERTED, 0, GNUNET_NO); 5237 GNUNET_STATISTICS_set (stats, STAT_PUTS, 0, GNUNET_NO);
4900 GNUNET_STATISTICS_set(stats, STAT_FIND_PEER, 0, GNUNET_NO); 5238 GNUNET_STATISTICS_set (stats, STAT_PUTS_INSERTED, 0, GNUNET_NO);
4901 GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_START, 0, GNUNET_NO); 5239 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER, 0, GNUNET_NO);
4902 GNUNET_STATISTICS_set(stats, STAT_GET_START, 0, GNUNET_NO); 5240 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_START, 0, GNUNET_NO);
4903 GNUNET_STATISTICS_set(stats, STAT_PUT_START, 0, GNUNET_NO); 5241 GNUNET_STATISTICS_set (stats, STAT_GET_START, 0, GNUNET_NO);
4904 GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_REPLY, 0, GNUNET_NO); 5242 GNUNET_STATISTICS_set (stats, STAT_PUT_START, 0, GNUNET_NO);
4905 GNUNET_STATISTICS_set(stats, STAT_FIND_PEER_ANSWER, 0, GNUNET_NO); 5243 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_REPLY, 0, GNUNET_NO);
4906 GNUNET_STATISTICS_set(stats, STAT_BLOOM_FIND_PEER, 0, GNUNET_NO); 5244 GNUNET_STATISTICS_set (stats, STAT_FIND_PEER_ANSWER, 0, GNUNET_NO);
4907 GNUNET_STATISTICS_set(stats, STAT_GET_REPLY, 0, GNUNET_NO); 5245 GNUNET_STATISTICS_set (stats, STAT_BLOOM_FIND_PEER, 0, GNUNET_NO);
4908 GNUNET_STATISTICS_set(stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO); 5246 GNUNET_STATISTICS_set (stats, STAT_GET_REPLY, 0, GNUNET_NO);
4909 GNUNET_STATISTICS_set(stats, STAT_HELLOS_PROVIDED, 0, GNUNET_NO); 5247 GNUNET_STATISTICS_set (stats, STAT_GET_RESPONSE_START, 0, GNUNET_NO);
4910 GNUNET_STATISTICS_set(stats, STAT_DISCONNECTS, 0, GNUNET_NO); 5248 GNUNET_STATISTICS_set (stats, STAT_HELLOS_PROVIDED, 0, GNUNET_NO);
5249 GNUNET_STATISTICS_set (stats, STAT_DISCONNECTS, 0, GNUNET_NO);
4911 } 5250 }
4912 /* FIXME: if there are no recent requests then these never get freed, but alternative is _annoying_! */ 5251 /* FIXME: if there are no recent requests then these never get freed, but alternative is _annoying_! */
4913 recent.hashmap = GNUNET_CONTAINER_multihashmap_create(DHT_MAX_RECENT / 2); 5252 recent.hashmap = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT / 2);
4914 recent.minHeap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN); 5253 recent.minHeap =
5254 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4915 if (GNUNET_YES == do_find_peer) 5255 if (GNUNET_YES == do_find_peer)
4916 { 5256 {
4917 next_send_time.rel_value = DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value + 5257 next_send_time.rel_value = DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value +
4918 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_STRONG, 5258 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
4919 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value / 2) - DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value); 5259 (DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value /
4920 find_peer_context.start = GNUNET_TIME_absolute_get(); 5260 2) -
4921 GNUNET_SCHEDULER_add_delayed (next_send_time, 5261 DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value);
4922 &send_find_peer_message, &find_peer_context); 5262 find_peer_context.start = GNUNET_TIME_absolute_get ();
4923 } 5263 GNUNET_SCHEDULER_add_delayed (next_send_time,
5264 &send_find_peer_message,
5265 &find_peer_context);
5266 }
4924 5267
4925 /* Scheduled the task to clean up when shutdown is called */ 5268 /* Scheduled the task to clean up when shutdown is called */
4926 cleanup_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 5269 cleanup_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
@@ -4940,13 +5283,12 @@ main (int argc, char *const *argv)
4940 int ret; 5283 int ret;
4941 5284
4942 ret = (GNUNET_OK == 5285 ret = (GNUNET_OK ==
4943 GNUNET_SERVICE_run (argc, 5286 GNUNET_SERVICE_run (argc,
4944 argv, 5287 argv,
4945 "dht", 5288 "dht",
4946 GNUNET_SERVICE_OPTION_NONE, 5289 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
4947 &run, NULL)) ? 0 : 1; 5290 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent.hashmap));
4948 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size(recent.hashmap)); 5291 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent.minHeap));
4949 GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size(recent.minHeap));
4950 GNUNET_CONTAINER_multihashmap_destroy (recent_find_peer_requests); 5292 GNUNET_CONTAINER_multihashmap_destroy (recent_find_peer_requests);
4951 GNUNET_CONTAINER_multihashmap_destroy (recent.hashmap); 5293 GNUNET_CONTAINER_multihashmap_destroy (recent.hashmap);
4952 GNUNET_CONTAINER_heap_destroy (recent.minHeap); 5294 GNUNET_CONTAINER_heap_destroy (recent.minHeap);