diff options
author | Bart Polot <bart@net.in.tum.de> | 2013-10-09 14:42:15 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2013-10-09 14:42:15 +0000 |
commit | 84acdaf73e35f0851ec0fb6dc2c4d6afb840bd6e (patch) | |
tree | df2cd93bf38082842bb6d1d90af803f8085dcec8 /src/mesh/gnunet-service-mesh_tunnel.c | |
parent | b3f4f964227220a10639f4484b86f396ca2dceed (diff) | |
download | gnunet-84acdaf73e35f0851ec0fb6dc2c4d6afb840bd6e.tar.gz gnunet-84acdaf73e35f0851ec0fb6dc2c4d6afb840bd6e.zip |
- fixing channel functions
Diffstat (limited to 'src/mesh/gnunet-service-mesh_tunnel.c')
-rw-r--r-- | src/mesh/gnunet-service-mesh_tunnel.c | 192 |
1 files changed, 157 insertions, 35 deletions
diff --git a/src/mesh/gnunet-service-mesh_tunnel.c b/src/mesh/gnunet-service-mesh_tunnel.c index e709a0be5..362ce7e74 100644 --- a/src/mesh/gnunet-service-mesh_tunnel.c +++ b/src/mesh/gnunet-service-mesh_tunnel.c | |||
@@ -224,33 +224,6 @@ GMT_state2s (enum MeshTunnelState s) | |||
224 | } | 224 | } |
225 | } | 225 | } |
226 | 226 | ||
227 | |||
228 | /** | ||
229 | * Search for a channel by global ID using full PeerIdentities. | ||
230 | * | ||
231 | * @param t Tunnel containing the channel. | ||
232 | * @param chid Public channel number. | ||
233 | * | ||
234 | * @return channel handler, NULL if doesn't exist | ||
235 | */ | ||
236 | static struct MeshChannel * | ||
237 | get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid) | ||
238 | { | ||
239 | struct MeshTChannel *iter; | ||
240 | |||
241 | if (NULL == t) | ||
242 | return NULL; | ||
243 | |||
244 | for (iter = t->channel_head; NULL != iter; iter = iter->next) | ||
245 | { | ||
246 | if (GMCH_get_id (iter->ch) == chid) | ||
247 | break; | ||
248 | } | ||
249 | |||
250 | return NULL == iter ? NULL : iter->ch; | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | 227 | /** |
255 | * Pick a connection on which send the next data message. | 228 | * Pick a connection on which send the next data message. |
256 | * | 229 | * |
@@ -314,7 +287,7 @@ handle_data (struct MeshTunnel3 *t, | |||
314 | GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type))); | 287 | GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type))); |
315 | 288 | ||
316 | /* Check channel */ | 289 | /* Check channel */ |
317 | ch = get_channel (t, ntohl (msg->chid)); | 290 | ch = GMT_get_channel (t, ntohl (msg->chid)); |
318 | if (NULL == ch) | 291 | if (NULL == ch) |
319 | { | 292 | { |
320 | GNUNET_STATISTICS_update (stats, "# data on unknown channel", | 293 | GNUNET_STATISTICS_update (stats, "# data on unknown channel", |
@@ -345,7 +318,7 @@ handle_data_ack (struct MeshTunnel3 *t, | |||
345 | } | 318 | } |
346 | 319 | ||
347 | /* Check channel */ | 320 | /* Check channel */ |
348 | ch = get_channel (t, ntohl (msg->chid)); | 321 | ch = GMT_get_channel (t, ntohl (msg->chid)); |
349 | if (NULL == ch) | 322 | if (NULL == ch) |
350 | { | 323 | { |
351 | GNUNET_STATISTICS_update (stats, "# data ack on unknown channel", | 324 | GNUNET_STATISTICS_update (stats, "# data ack on unknown channel", |
@@ -376,7 +349,7 @@ handle_ch_create (struct MeshTunnel3 *t, | |||
376 | } | 349 | } |
377 | 350 | ||
378 | /* Check channel */ | 351 | /* Check channel */ |
379 | ch = get_channel (t, ntohl (msg->chid)); | 352 | ch = GMT_get_channel (t, ntohl (msg->chid)); |
380 | if (NULL != ch) | 353 | if (NULL != ch) |
381 | { | 354 | { |
382 | /* Probably a retransmission, safe to ignore */ | 355 | /* Probably a retransmission, safe to ignore */ |
@@ -409,7 +382,7 @@ handle_ch_ack (struct MeshTunnel3 *t, | |||
409 | } | 382 | } |
410 | 383 | ||
411 | /* Check channel */ | 384 | /* Check channel */ |
412 | ch = get_channel (t, ntohl (msg->chid)); | 385 | ch = GMT_get_channel (t, ntohl (msg->chid)); |
413 | if (NULL == ch) | 386 | if (NULL == ch) |
414 | { | 387 | { |
415 | GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel", | 388 | GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel", |
@@ -439,7 +412,7 @@ handle_ch_destroy (struct MeshTunnel3 *t, | |||
439 | } | 412 | } |
440 | 413 | ||
441 | /* Check channel */ | 414 | /* Check channel */ |
442 | ch = get_channel (t, ntohl (msg->chid)); | 415 | ch = GMT_get_channel (t, ntohl (msg->chid)); |
443 | if (NULL == ch) | 416 | if (NULL == ch) |
444 | { | 417 | { |
445 | /* Probably a retransmission, safe to ignore */ | 418 | /* Probably a retransmission, safe to ignore */ |
@@ -711,6 +684,74 @@ GMT_remove_connection (struct MeshTunnel3 *t, struct MeshConnection *c) | |||
711 | 684 | ||
712 | 685 | ||
713 | /** | 686 | /** |
687 | * Add a channel to a tunnel. | ||
688 | * | ||
689 | * @param t Tunnel. | ||
690 | * @param ch Channel. | ||
691 | */ | ||
692 | void | ||
693 | GMT_add_channel (struct MeshTunnel3 *t, struct MeshChannel *ch) | ||
694 | { | ||
695 | struct MeshTChannel *aux; | ||
696 | |||
697 | for (aux = t->channel_head; aux != NULL; aux = aux->next) | ||
698 | if (aux->ch == ch) | ||
699 | return; | ||
700 | |||
701 | aux = GNUNET_new (struct MeshTChannel); | ||
702 | aux->ch = ch; | ||
703 | GNUNET_CONTAINER_DLL_insert_tail (t->channel_head, t->channel_tail, aux); | ||
704 | } | ||
705 | |||
706 | |||
707 | /** | ||
708 | * Remove a channel from a tunnel. | ||
709 | * | ||
710 | * @param t Tunnel. | ||
711 | * @param ch Channel. | ||
712 | */ | ||
713 | void | ||
714 | GMT_remove_channel (struct MeshTunnel3 *t, struct MeshChannel *ch) | ||
715 | { | ||
716 | struct MeshTChannel *aux; | ||
717 | |||
718 | for (aux = t->channel_head; aux != NULL; aux = aux->next) | ||
719 | if (aux->ch == ch) | ||
720 | { | ||
721 | GNUNET_CONTAINER_DLL_remove (t->channel_head, t->channel_tail, aux); | ||
722 | GNUNET_free (aux); | ||
723 | return; | ||
724 | } | ||
725 | } | ||
726 | |||
727 | |||
728 | /** | ||
729 | * Search for a channel by global ID. | ||
730 | * | ||
731 | * @param t Tunnel containing the channel. | ||
732 | * @param chid Public channel number. | ||
733 | * | ||
734 | * @return channel handler, NULL if doesn't exist | ||
735 | */ | ||
736 | struct MeshChannel * | ||
737 | GMT_get_channel (struct MeshTunnel3 *t, MESH_ChannelNumber chid) | ||
738 | { | ||
739 | struct MeshTChannel *iter; | ||
740 | |||
741 | if (NULL == t) | ||
742 | return NULL; | ||
743 | |||
744 | for (iter = t->channel_head; NULL != iter; iter = iter->next) | ||
745 | { | ||
746 | if (GMCH_get_id (iter->ch) == chid) | ||
747 | break; | ||
748 | } | ||
749 | |||
750 | return NULL == iter ? NULL : iter->ch; | ||
751 | } | ||
752 | |||
753 | |||
754 | /** | ||
714 | * Tunnel is empty: destroy it. | 755 | * Tunnel is empty: destroy it. |
715 | * | 756 | * |
716 | * Notifies all connections about the destruction. | 757 | * Notifies all connections about the destruction. |
@@ -874,6 +915,7 @@ GMT_use_path (struct MeshTunnel3 *t, struct MeshPeerPath *p) | |||
874 | /** | 915 | /** |
875 | * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME | 916 | * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME |
876 | * Encrypt data with the tunnel key. | 917 | * Encrypt data with the tunnel key. |
918 | * Make static? | ||
877 | * | 919 | * |
878 | * @param t Tunnel whose key to use. | 920 | * @param t Tunnel whose key to use. |
879 | * @param dst Destination for the encrypted data. | 921 | * @param dst Destination for the encrypted data. |
@@ -894,6 +936,7 @@ GMT_encrypt (struct MeshTunnel3 *t, | |||
894 | /** | 936 | /** |
895 | * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME | 937 | * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME |
896 | * Decrypt data with the tunnel key. | 938 | * Decrypt data with the tunnel key. |
939 | * Make static? | ||
897 | * | 940 | * |
898 | * @param t Tunnel whose key to use. | 941 | * @param t Tunnel whose key to use. |
899 | * @param dst Destination for the plaintext. | 942 | * @param dst Destination for the plaintext. |
@@ -1016,8 +1059,49 @@ GMT_get_buffer (struct MeshTunnel3 *t, int fwd) | |||
1016 | return buffer; | 1059 | return buffer; |
1017 | } | 1060 | } |
1018 | 1061 | ||
1062 | |||
1063 | /** | ||
1064 | * Get the tunnel's destination. | ||
1065 | * | ||
1066 | * @param t Tunnel. | ||
1067 | * | ||
1068 | * @return ID of the destination peer. | ||
1069 | */ | ||
1070 | const struct GNUNET_PeerIdentity * | ||
1071 | GMT_get_destination (struct MeshTunnel3 *t) | ||
1072 | { | ||
1073 | return GMP_get_id (t->peer); | ||
1074 | } | ||
1075 | |||
1076 | |||
1077 | |||
1019 | /** | 1078 | /** |
1020 | * Sends an already built message on a tunnel, choosing the best connection. | 1079 | * Get the tunnel's next free Channel ID. |
1080 | * | ||
1081 | * @param t Tunnel. | ||
1082 | * | ||
1083 | * @return ID of a channel free to use. | ||
1084 | */ | ||
1085 | MESH_ChannelNumber | ||
1086 | GMT_get_next_chid (struct MeshTunnel3 *t) | ||
1087 | { | ||
1088 | MESH_ChannelNumber chid; | ||
1089 | |||
1090 | while (NULL != GMT_get_channel (t, t->next_chid)) | ||
1091 | { | ||
1092 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid); | ||
1093 | t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI; | ||
1094 | } | ||
1095 | chid = t->next_chid; | ||
1096 | t->next_chid = (t->next_chid + 1) & ~GNUNET_MESH_LOCAL_CHANNEL_ID_CLI; | ||
1097 | |||
1098 | return chid; | ||
1099 | } | ||
1100 | |||
1101 | |||
1102 | /** | ||
1103 | * Sends an already built message on a tunnel, encrypting it and | ||
1104 | * choosing the best connection. | ||
1021 | * | 1105 | * |
1022 | * @param message Message to send. Function modifies it. | 1106 | * @param message Message to send. Function modifies it. |
1023 | * @param t Tunnel on which this message is transmitted. | 1107 | * @param t Tunnel on which this message is transmitted. |
@@ -1025,22 +1109,33 @@ GMT_get_buffer (struct MeshTunnel3 *t, int fwd) | |||
1025 | * @param fwd Is this a fwd message? | 1109 | * @param fwd Is this a fwd message? |
1026 | */ | 1110 | */ |
1027 | void | 1111 | void |
1028 | GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg, | 1112 | GMT_send_prebuilt_message (const struct GNUNET_MessageHeader *message, |
1029 | struct MeshTunnel3 *t, | 1113 | struct MeshTunnel3 *t, |
1030 | struct MeshChannel *ch, | 1114 | struct MeshChannel *ch, |
1031 | int fwd) | 1115 | int fwd) |
1032 | { | 1116 | { |
1033 | struct MeshConnection *c; | 1117 | struct MeshConnection *c; |
1118 | struct GNUNET_MESH_Encrypted *msg; | ||
1119 | size_t size = ntohs (message->size); | ||
1120 | char *cbuf[sizeof (struct GNUNET_MESH_Encrypted) + size]; | ||
1121 | uint64_t iv; | ||
1034 | uint16_t type; | 1122 | uint16_t type; |
1035 | 1123 | ||
1036 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n", GMP_2s (t->peer)); | 1124 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n", GMP_2s (t->peer)); |
1125 | |||
1126 | iv = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX); | ||
1127 | msg = (struct GNUNET_MESH_Encrypted *) cbuf; | ||
1128 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED); | ||
1129 | msg->header.size = htons (sizeof (struct GNUNET_MESH_Encrypted) + size); | ||
1130 | msg->iv = GNUNET_htonll (iv); | ||
1131 | GMT_encrypt (t, &msg[1], message, size, iv, fwd); | ||
1037 | c = tunnel_get_connection (t, fwd); | 1132 | c = tunnel_get_connection (t, fwd); |
1038 | if (NULL == c) | 1133 | if (NULL == c) |
1039 | { | 1134 | { |
1040 | GNUNET_break (GNUNET_YES == t->destroy); | 1135 | GNUNET_break (GNUNET_YES == t->destroy); |
1041 | return; | 1136 | return; |
1042 | } | 1137 | } |
1043 | type = ntohs (msg->header.type); | 1138 | type = ntohs (message->type); |
1044 | switch (type) | 1139 | switch (type) |
1045 | { | 1140 | { |
1046 | case GNUNET_MESSAGE_TYPE_MESH_FWD: | 1141 | case GNUNET_MESSAGE_TYPE_MESH_FWD: |
@@ -1059,3 +1154,30 @@ GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg, | |||
1059 | 1154 | ||
1060 | GMC_send_prebuilt_message (&msg->header, c, ch, fwd); | 1155 | GMC_send_prebuilt_message (&msg->header, c, ch, fwd); |
1061 | } | 1156 | } |
1157 | |||
1158 | /** | ||
1159 | * Is the tunnel directed towards the local peer? | ||
1160 | * | ||
1161 | * @param t Tunnel. | ||
1162 | * | ||
1163 | * @return GNUNET_YES if it is loopback. | ||
1164 | */ | ||
1165 | int | ||
1166 | GMT_is_loopback (const struct MeshTunnel3 *t) | ||
1167 | { | ||
1168 | return (my_short_id == GMP_get_short_id(t->peer)); | ||
1169 | } | ||
1170 | |||
1171 | |||
1172 | /** | ||
1173 | * Get the static string for the peer this tunnel is directed. | ||
1174 | * | ||
1175 | * @param t Tunnel. | ||
1176 | * | ||
1177 | * @return Static string the destination peer's ID. | ||
1178 | */ | ||
1179 | const char * | ||
1180 | GMT_2s (const struct MeshTunnel3 *t) | ||
1181 | { | ||
1182 | return GMP_2s (t->peer); | ||
1183 | } \ No newline at end of file | ||