aboutsummaryrefslogtreecommitdiff
path: root/src/mesh/gnunet-service-mesh_connection.c
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2013-10-08 10:30:26 +0000
committerBart Polot <bart@net.in.tum.de>2013-10-08 10:30:26 +0000
commitd83640d04d57f6388b37d19a814eb6770ea4c485 (patch)
tree09bcf5d5702aced1c504031dbfbaf787f84a0890 /src/mesh/gnunet-service-mesh_connection.c
parent45072973ee81ffa01a2fc3450a5a7dcbf57c0464 (diff)
downloadgnunet-d83640d04d57f6388b37d19a814eb6770ea4c485.tar.gz
gnunet-d83640d04d57f6388b37d19a814eb6770ea4c485.zip
- move all core/PeerIdentity interacting code to _peer, remove GNUNET_PeerIdentity from _connection
Diffstat (limited to 'src/mesh/gnunet-service-mesh_connection.c')
-rw-r--r--src/mesh/gnunet-service-mesh_connection.c472
1 files changed, 158 insertions, 314 deletions
diff --git a/src/mesh/gnunet-service-mesh_connection.c b/src/mesh/gnunet-service-mesh_connection.c
index 782b6b859..31e11879e 100644
--- a/src/mesh/gnunet-service-mesh_connection.c
+++ b/src/mesh/gnunet-service-mesh_connection.c
@@ -27,16 +27,12 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#include "gnunet_core_service.h"
31
32#include "gnunet-service-mesh_connection.h" 30#include "gnunet-service-mesh_connection.h"
33#include "gnunet-service-mesh_peer.h" 31#include "gnunet-service-mesh_peer.h"
34#include "gnunet-service-mesh_local.h"
35#include "mesh_protocol_enc.h" 32#include "mesh_protocol_enc.h"
36#include "mesh_path.h" 33#include "mesh_path.h"
37 34
38 35
39#define MESH_DEBUG_CONNECTION GNUNET_NO
40#define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\ 36#define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
41 GNUNET_TIME_UNIT_MINUTES,\ 37 GNUNET_TIME_UNIT_MINUTES,\
42 10) 38 10)
@@ -44,13 +40,6 @@
44#define MESH_RETRANSMIT_MARGIN 4 40#define MESH_RETRANSMIT_MARGIN 4
45 41
46 42
47#if MESH_DEBUG_CONNECTION
48#define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
49#else
50#define DEBUG_CONN(...)
51#endif
52
53
54/** 43/**
55 * All the states a connection can be in. 44 * All the states a connection can be in.
56 */ 45 */
@@ -281,10 +270,6 @@ static unsigned long long max_msgs_queue;
281 */ 270 */
282static struct GNUNET_TIME_Relative refresh_connection_time; 271static struct GNUNET_TIME_Relative refresh_connection_time;
283 272
284/**
285 * Handle to communicate with core.
286 */
287static struct GNUNET_CORE_Handle *core_handle;
288 273
289#if 0 // avoid compiler warning for unused static function 274#if 0 // avoid compiler warning for unused static function
290static void 275static void
@@ -377,98 +362,6 @@ connection_get (const struct GNUNET_HashCode *cid)
377 362
378 363
379/** 364/**
380 * Sends an already built message on a connection, properly registering
381 * all used resources.
382 *
383 * @param message Message to send. Function makes a copy of it.
384 * If message is not hop-by-hop, decrements TTL of copy.
385 * @param c Connection on which this message is transmitted.
386 * @param ch Channel on which this message is transmitted, or NULL.
387 * @param fwd Is this a fwd message?
388 */
389static void
390send_prebuilt_message_connection (const struct GNUNET_MessageHeader *message,
391 struct MeshConnection *c,
392 struct MeshChannel *ch,
393 int fwd)
394{
395 void *data;
396 size_t size;
397 uint16_t type;
398
399 size = ntohs (message->size);
400 data = GNUNET_malloc (size);
401 memcpy (data, message, size);
402 type = ntohs (message->type);
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
404 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
405
406 switch (type)
407 {
408 struct GNUNET_MESH_Encrypted *emsg;
409 struct GNUNET_MESH_ACK *amsg;
410 struct GNUNET_MESH_Poll *pmsg;
411 struct GNUNET_MESH_ConnectionDestroy *dmsg;
412 struct GNUNET_MESH_ConnectionBroken *bmsg;
413 uint32_t ttl;
414
415 case GNUNET_MESSAGE_TYPE_MESH_FWD:
416 case GNUNET_MESSAGE_TYPE_MESH_BCK:
417 emsg = (struct GNUNET_MESH_Encrypted *) data;
418 ttl = ntohl (emsg->ttl);
419 if (0 == ttl)
420 {
421 GNUNET_break_op (0);
422 return;
423 }
424 emsg->cid = c->id;
425 emsg->ttl = htonl (ttl - 1);
426 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
428 break;
429
430 case GNUNET_MESSAGE_TYPE_MESH_ACK:
431 amsg = (struct GNUNET_MESH_ACK *) data;
432 amsg->cid = c->id;
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
434 break;
435
436 case GNUNET_MESSAGE_TYPE_MESH_POLL:
437 pmsg = (struct GNUNET_MESH_Poll *) data;
438 pmsg->cid = c->id;
439 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
441 break;
442
443 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
444 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
445 dmsg->cid = c->id;
446 dmsg->reserved = 0;
447 break;
448
449 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
450 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
451 bmsg->cid = c->id;
452 bmsg->reserved = 0;
453 break;
454
455 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
456 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
457 break;
458
459 default:
460 GNUNET_break (0);
461 }
462
463 queue_add (data,
464 type,
465 size,
466 c,
467 ch,
468 fwd);
469}
470
471/**
472 * Send an ACK informing the predecessor about the available buffer space. 365 * Send an ACK informing the predecessor about the available buffer space.
473 * 366 *
474 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest), 367 * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
@@ -1099,128 +992,6 @@ connection_reset_timeout (struct MeshConnection *c, int fwd)
1099 992
1100 993
1101/** 994/**
1102 * Method called whenever a given peer connects.
1103 *
1104 * @param cls closure
1105 * @param peer peer identity this notification is about
1106 */
1107static void
1108core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
1109{
1110 struct MeshPeer *pi;
1111 struct MeshPeerPath *path;
1112
1113 DEBUG_CONN ("Peer connected\n");
1114 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
1115 pi = peer_get (peer);
1116 if (myid == pi->id)
1117 {
1118 DEBUG_CONN (" (self)\n");
1119 path = path_new (1);
1120 }
1121 else
1122 {
1123 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
1124 path = path_new (2);
1125 path->peers[1] = pi->id;
1126 GNUNET_PEER_change_rc (pi->id, 1);
1127 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
1128 }
1129 path->peers[0] = myid;
1130 GNUNET_PEER_change_rc (myid, 1);
1131 peer_add_path (pi, path, GNUNET_YES);
1132
1133 pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
1134 return;
1135}
1136
1137
1138/**
1139 * Method called whenever a peer disconnects.
1140 *
1141 * @param cls closure
1142 * @param peer peer identity this notification is about
1143 */
1144static void
1145core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1146{
1147 struct MeshPeer *pi;
1148
1149 DEBUG_CONN ("Peer disconnected\n");
1150 pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
1151 if (NULL == pi)
1152 {
1153 GNUNET_break (0);
1154 return;
1155 }
1156
1157 GNUNET_CONTAINER_multihashmap_iterate (pi->connections,
1158 GMC_notify_broken,
1159 pi);
1160 GNUNET_CONTAINER_multihashmap_destroy (pi->connections);
1161 pi->connections = NULL;
1162 if (NULL != pi->core_transmit)
1163 {
1164 GNUNET_CORE_notify_transmit_ready_cancel (pi->core_transmit);
1165 pi->core_transmit = NULL;
1166 }
1167 if (myid == pi->id)
1168 {
1169 DEBUG_CONN (" (self)\n");
1170 }
1171 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
1172
1173 return;
1174}
1175
1176
1177
1178/**
1179 * To be called on core init/fail.
1180 *
1181 * @param cls Closure (config)
1182 * @param identity the public identity of this peer
1183 */
1184static void
1185core_init (void *cls,
1186 const struct GNUNET_PeerIdentity *identity)
1187{
1188 const struct GNUNET_CONFIGURATION_Handle *c = cls;
1189 static int i = 0;
1190
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
1192 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)))
1193 {
1194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
1195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1196 " core id %s\n",
1197 GNUNET_i2s (identity));
1198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1199 " my id %s\n",
1200 GNUNET_i2s (&my_full_id));
1201 GNUNET_CORE_disconnect (core_handle);
1202 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
1203 NULL, /* Closure passed to MESH functions */
1204 &core_init, /* Call core_init once connected */
1205 &core_connect, /* Handle connects */
1206 &core_disconnect, /* remove peers on disconnects */
1207 NULL, /* Don't notify about all incoming messages */
1208 GNUNET_NO, /* For header only in notification */
1209 NULL, /* Don't notify about all outbound messages */
1210 GNUNET_NO, /* For header-only out notification */
1211 core_handlers); /* Register these handlers */
1212 if (10 < i++)
1213 GNUNET_abort();
1214 }
1215 GML_start ();
1216 return;
1217}
1218
1219
1220
1221
1222
1223/**
1224 * Core handler for connection creation. 995 * Core handler for connection creation.
1225 * 996 *
1226 * @param cls Closure (unused). 997 * @param cls Closure (unused).
@@ -1998,6 +1769,61 @@ send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1998} 1769}
1999 1770
2000 1771
1772
1773/**
1774 * Initialize the connections subsystem
1775 *
1776 * @param c Configuration handle.
1777 */
1778void
1779GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1780{
1781 if (GNUNET_OK !=
1782 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1783 &max_msgs_queue))
1784 {
1785 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1786 "MESH", "MAX_MSGS_QUEUE", "MISSING");
1787 GNUNET_SCHEDULER_shutdown ();
1788 return;
1789 }
1790
1791 if (GNUNET_OK !=
1792 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1793 &max_connections))
1794 {
1795 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1796 "MESH", "MAX_CONNECTIONS", "MISSING");
1797 GNUNET_SCHEDULER_shutdown ();
1798 return;
1799 }
1800
1801 if (GNUNET_OK !=
1802 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1803 &refresh_connection_time))
1804 {
1805 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1806 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1807 GNUNET_SCHEDULER_shutdown ();
1808 return;
1809 }
1810 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1811}
1812
1813/**
1814 * Shut down the connections subsystem.
1815 */
1816void
1817GMC_shutdown (void)
1818{
1819 if (core_handle != NULL)
1820 {
1821 GNUNET_CORE_disconnect (core_handle);
1822 core_handle = NULL;
1823 }
1824}
1825
1826
2001struct MeshConnection * 1827struct MeshConnection *
2002GMC_new (const struct GNUNET_HashCode *cid) 1828GMC_new (const struct GNUNET_HashCode *cid)
2003{ 1829{
@@ -2054,22 +1880,18 @@ GMC_destroy (struct MeshConnection *c)
2054 1880
2055 1881
2056/** 1882/**
2057 * Iterator to notify all connections of a broken link. Mark connections 1883 * Notify other peers on a connection of a broken link. Mark connections
2058 * to destroy after all traffic has been sent. 1884 * to destroy after all traffic has been sent.
2059 * 1885 *
2060 * @param cls Closure (peer disconnected). 1886 * @param c Connection on which there has been a disconnection.
2061 * @param key Current key code (tid). 1887 * @param peer Peer that disconnected.
2062 * @param value Value in the hash map (connection). 1888 * @param my_full_id My ID (to send to other peers).
2063 *
2064 * @return GNUNET_YES if we should continue to iterate,
2065 * GNUNET_NO if not.
2066 */ 1889 */
2067int 1890void
2068GMC_notify_broken (void *cls, 1891GMC_notify_broken (struct MeshConnection *c,
2069 const struct GNUNET_HashCode *key, 1892 struct MeshPeer *peer,
2070 void *value) 1893 struct GNUNET_PeerIdentity *my_full_id)
2071{ 1894{
2072 struct MeshPeer *peer = cls;
2073 struct MeshConnection *c = value; 1895 struct MeshConnection *c = value;
2074 struct GNUNET_MESH_ConnectionBroken msg; 1896 struct GNUNET_MESH_ConnectionBroken msg;
2075 int fwd; 1897 int fwd;
@@ -2087,9 +1909,9 @@ GMC_notify_broken (void *cls,
2087 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken)); 1909 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
2088 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN); 1910 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
2089 msg.cid = c->id; 1911 msg.cid = c->id;
2090 msg.peer1 = my_full_id; 1912 msg.peer1 = *my_full_id;
2091 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id); 1913 msg.peer2 = *GNUNET_PEER_resolve2 (peer->id);
2092 send_prebuilt_message_connection (&msg.header, c, NULL, fwd); 1914 GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
2093 c->destroy = GNUNET_YES; 1915 c->destroy = GNUNET_YES;
2094 1916
2095 return GNUNET_YES; 1917 return GNUNET_YES;
@@ -2097,77 +1919,6 @@ GMC_notify_broken (void *cls,
2097 1919
2098 1920
2099/** 1921/**
2100 * Initialize the connections subsystem
2101 *
2102 * @param c Configuration handle.
2103 */
2104void
2105GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2106{
2107 if (GNUNET_OK !=
2108 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
2109 &max_msgs_queue))
2110 {
2111 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2112 "MESH", "MAX_MSGS_QUEUE", "MISSING");
2113 GNUNET_SCHEDULER_shutdown ();
2114 return;
2115 }
2116
2117 if (GNUNET_OK !=
2118 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
2119 &max_connections))
2120 {
2121 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2122 "MESH", "MAX_CONNECTIONS", "MISSING");
2123 GNUNET_SCHEDULER_shutdown ();
2124 return;
2125 }
2126
2127 if (GNUNET_OK !=
2128 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
2129 &refresh_connection_time))
2130 {
2131 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2132 "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
2133 GNUNET_SCHEDULER_shutdown ();
2134 return;
2135 }
2136 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
2137
2138 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
2139 NULL, /* Closure passed to MESH functions */
2140 &core_init, /* Call core_init once connected */
2141 &core_connect, /* Handle connects */
2142 &core_disconnect, /* remove peers on disconnects */
2143 NULL, /* Don't notify about all incoming messages */
2144 GNUNET_NO, /* For header only in notification */
2145 NULL, /* Don't notify about all outbound messages */
2146 GNUNET_NO, /* For header-only out notification */
2147 core_handlers); /* Register these handlers */
2148 if (NULL == core_handle)
2149 {
2150 GNUNET_break (0);
2151 GNUNET_SCHEDULER_shutdown ();
2152 return;
2153 }
2154}
2155
2156/**
2157 * Shut down the connections subsystem.
2158 */
2159void
2160GMC_shutdown (void)
2161{
2162 if (core_handle != NULL)
2163 {
2164 GNUNET_CORE_disconnect (core_handle);
2165 core_handle = NULL;
2166 }
2167}
2168
2169
2170/**
2171 * Is this peer the first one on the connection? 1922 * Is this peer the first one on the connection?
2172 * 1923 *
2173 * @param c Connection. 1924 * @param c Connection.
@@ -2214,4 +1965,97 @@ GMC_count (const struct MeshConnection *head)
2214 for (count = 0, iter = head; NULL != iter; iter = iter->next, count++); 1965 for (count = 0, iter = head; NULL != iter; iter = iter->next, count++);
2215 1966
2216 return count; 1967 return count;
1968}
1969
1970
1971/**
1972 * Sends an already built message on a connection, properly registering
1973 * all used resources.
1974 *
1975 * @param message Message to send. Function makes a copy of it.
1976 * If message is not hop-by-hop, decrements TTL of copy.
1977 * @param c Connection on which this message is transmitted.
1978 * @param ch Channel on which this message is transmitted, or NULL.
1979 * @param fwd Is this a fwd message?
1980 */
1981void
1982GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1983 struct MeshConnection *c,
1984 struct MeshChannel *ch,
1985 int fwd)
1986{
1987 void *data;
1988 size_t size;
1989 uint16_t type;
1990
1991 size = ntohs (message->size);
1992 data = GNUNET_malloc (size);
1993 memcpy (data, message, size);
1994 type = ntohs (message->type);
1995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
1996 GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
1997
1998 switch (type)
1999 {
2000 struct GNUNET_MESH_Encrypted *emsg;
2001 struct GNUNET_MESH_ACK *amsg;
2002 struct GNUNET_MESH_Poll *pmsg;
2003 struct GNUNET_MESH_ConnectionDestroy *dmsg;
2004 struct GNUNET_MESH_ConnectionBroken *bmsg;
2005 uint32_t ttl;
2006
2007 case GNUNET_MESSAGE_TYPE_MESH_FWD:
2008 case GNUNET_MESSAGE_TYPE_MESH_BCK:
2009 emsg = (struct GNUNET_MESH_Encrypted *) data;
2010 ttl = ntohl (emsg->ttl);
2011 if (0 == ttl)
2012 {
2013 GNUNET_break_op (0);
2014 return;
2015 }
2016 emsg->cid = c->id;
2017 emsg->ttl = htonl (ttl - 1);
2018 emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2020 break;
2021
2022 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2023 amsg = (struct GNUNET_MESH_ACK *) data;
2024 amsg->cid = c->id;
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2026 break;
2027
2028 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2029 pmsg = (struct GNUNET_MESH_Poll *) data;
2030 pmsg->cid = c->id;
2031 pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2033 break;
2034
2035 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2036 dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2037 dmsg->cid = c->id;
2038 dmsg->reserved = 0;
2039 break;
2040
2041 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2042 bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2043 bmsg->cid = c->id;
2044 bmsg->reserved = 0;
2045 break;
2046
2047 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2048 case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2049 break;
2050
2051 default:
2052 GNUNET_break (0);
2053 }
2054
2055 GMP_queue_add (data,
2056 type,
2057 size,
2058 c,
2059 ch,
2060 fwd);
2217} \ No newline at end of file 2061} \ No newline at end of file