aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2013-07-15 19:05:28 +0000
committerBart Polot <bart@net.in.tum.de>2013-07-15 19:05:28 +0000
commite881c2babf24d321c9b20d1d3175203bc125107e (patch)
treebc6fe00899a9acc4eb417318a541e8eaf8c26dc5
parent92e0fb20c9bf5144e80e0225680f47253cca7d6f (diff)
downloadgnunet-e881c2babf24d321c9b20d1d3175203bc125107e.tar.gz
gnunet-e881c2babf24d321c9b20d1d3175203bc125107e.zip
Refactored code to eliminate duplicates depending on FWD/BCK traffic
-rw-r--r--src/mesh/gnunet-service-mesh.c525
1 files changed, 185 insertions, 340 deletions
diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c
index 22714545f..d8a298e9b 100644
--- a/src/mesh/gnunet-service-mesh.c
+++ b/src/mesh/gnunet-service-mesh.c
@@ -25,7 +25,6 @@
25 * 25 *
26 * FIXME in progress: 26 * FIXME in progress:
27 * - when sending in-order buffered data, wait for client ACKs 27 * - when sending in-order buffered data, wait for client ACKs
28 * - refactor unicast to make generic handling, assigning *rel and *fc
29 * 28 *
30 * TODO: 29 * TODO:
31 * - relay corking down to core 30 * - relay corking down to core
@@ -581,10 +580,6 @@ mesh_debug (void *cls, int success)
581} 580}
582#endif 581#endif
583 582
584/* FIXME */
585unsigned int debug_fwd_ack;
586unsigned int debug_bck_ack;
587
588#endif 583#endif
589 584
590/******************************************************************************/ 585/******************************************************************************/
@@ -2097,35 +2092,39 @@ send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack)
2097 * Send an end-to-end FWD ACK message for the most recent in-sequence payload. 2092 * Send an end-to-end FWD ACK message for the most recent in-sequence payload.
2098 * 2093 *
2099 * @param t Tunnel this is about. 2094 * @param t Tunnel this is about.
2095 * @param fwd Is for FWD traffic? (ACK dest->owner)
2100 */ 2096 */
2101static void 2097static void
2102tunnel_send_fwd_data_ack (struct MeshTunnel *t) 2098tunnel_send_data_ack (struct MeshTunnel *t, int fwd)
2103{ 2099{
2104 struct GNUNET_MESH_DataACK msg; 2100 struct GNUNET_MESH_DataACK msg;
2105 struct MeshTunnelReliability *rel; 2101 struct MeshTunnelReliability *rel;
2106 struct MeshReliableMessage *copy; 2102 struct MeshReliableMessage *copy;
2103 GNUNET_PEER_Id hop;
2107 uint64_t mask; 2104 uint64_t mask;
2108 unsigned int delta; 2105 unsigned int delta;
2109 2106
2107 rel = fwd ? t->bck_rel : t->fwd_rel;
2108 hop = fwd ? t->prev_hop : t->next_hop;
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "send_fwd_data_ack for %llu\n", 2110 "send_data_ack for %llu\n",
2112 t->bck_rel->mid_recv - 1); 2111 rel->mid_recv - 1);
2113 2112
2114 if (GNUNET_NO == t->reliable) 2113 if (GNUNET_NO == t->reliable)
2115 { 2114 {
2116 GNUNET_break_op (0); 2115 GNUNET_break_op (0);
2117 return; 2116 return;
2118 } 2117 }
2119 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK); 2118 msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK :
2119 GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2120 msg.header.size = htons (sizeof (msg)); 2120 msg.header.size = htons (sizeof (msg));
2121 msg.tid = htonl (t->id.tid); 2121 msg.tid = htonl (t->id.tid);
2122 GNUNET_PEER_resolve (t->id.oid, &msg.oid); 2122 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2123 msg.mid = GNUNET_htonll (t->bck_rel->mid_recv - 1); 2123 msg.mid = GNUNET_htonll (rel->mid_recv - 1);
2124 msg.futures = 0; 2124 msg.futures = 0;
2125 rel = t->bck_rel;
2126 for (copy = rel->head_recv; NULL != copy; copy = copy->next) 2125 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
2127 { 2126 {
2128 delta = copy->mid - t->bck_rel->mid_recv; 2127 delta = copy->mid - rel->mid_recv;
2129 if (63 < delta) 2128 if (63 < delta)
2130 break; 2129 break;
2131 mask = 0x1LL << delta; 2130 mask = 0x1LL << delta;
@@ -2136,29 +2135,8 @@ tunnel_send_fwd_data_ack (struct MeshTunnel *t)
2136 } 2135 }
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures); 2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
2138 2137
2139 send_prebuilt_message (&msg.header, t->prev_hop, t); 2138 send_prebuilt_message (&msg.header, hop, t);
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_fwd_data_ack END\n"); 2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
2141}
2142
2143
2144/**
2145 * Send an end-to-end BCK ACK message for the most recent in-sequence payload.
2146 *
2147 * @param t Tunnel this is about.
2148 */
2149static void
2150tunnel_send_bck_data_ack (struct MeshTunnel *t)
2151{
2152 struct GNUNET_MESH_DataACK msg;
2153
2154 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK);
2155 msg.header.size = htons (sizeof (msg));
2156 msg.tid = htonl (t->id.tid);
2157 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2158 msg.mid = GNUNET_htonll (t->fwd_rel->mid_recv - 1);
2159 msg.futures = 0; // FIXME set bits of other newer messages received
2160
2161 send_prebuilt_message (&msg.header, t->next_hop, t);
2162} 2140}
2163 2141
2164 2142
@@ -2172,28 +2150,42 @@ tunnel_send_bck_data_ack (struct MeshTunnel *t)
2172 * 2150 *
2173 * @param t Tunnel on which to send the ACK. 2151 * @param t Tunnel on which to send the ACK.
2174 * @param type Type of message that triggered the ACK transmission. 2152 * @param type Type of message that triggered the ACK transmission.
2153 * @param fwd Is this FWD ACK? (Going dest->owner)
2175 */ 2154 */
2176static void 2155static void
2177tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type) 2156tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd)
2178{ 2157{
2179 struct MeshTunnelReliability *rel = t->fwd_rel; 2158 struct MeshTunnelReliability *rel;
2159 struct MeshFlowControl *next_fc;
2160 struct MeshFlowControl *prev_fc;
2161 struct MeshClient *c;
2162 struct MeshClient *o;
2163 GNUNET_PEER_Id hop;
2180 uint64_t delta_mid; 2164 uint64_t delta_mid;
2181 uint32_t ack; 2165 uint32_t ack;
2182 int delta; 2166 int delta;
2183 2167
2168 rel = fwd ? t->fwd_rel : t->bck_rel;
2169 c = fwd ? t->client : t->owner;
2170 o = fwd ? t->owner : t->client;
2171 next_fc = fwd ? &t->next_fc : &t->prev_fc;
2172 prev_fc = fwd ? &t->prev_fc : &t->next_fc;
2173 hop = fwd ? t->prev_hop : t->next_hop;
2174
2184 /* Is it after unicast retransmission? */ 2175 /* Is it after unicast retransmission? */
2185 switch (type) 2176 switch (type)
2186 { 2177 {
2187 case GNUNET_MESSAGE_TYPE_MESH_UNICAST: 2178 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2179 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 "ACK due to FWD DATA retransmission\n"); 2181 "ACK due to DATA retransmission\n");
2190 if (GNUNET_YES == t->nobuffer) 2182 if (GNUNET_YES == t->nobuffer)
2191 { 2183 {
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n"); 2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
2193 return; 2185 return;
2194 } 2186 }
2195 if (GNUNET_YES == t->reliable && NULL != t->client) 2187 if (GNUNET_YES == t->reliable && NULL != c)
2196 tunnel_send_fwd_data_ack (t); 2188 tunnel_send_data_ack (t, fwd);
2197 break; 2189 break;
2198 case GNUNET_MESSAGE_TYPE_MESH_ACK: 2190 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2199 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK: 2191 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
@@ -2208,119 +2200,51 @@ tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
2208 } 2200 }
2209 2201
2210 /* Check if we need to transmit the ACK */ 2202 /* Check if we need to transmit the ACK */
2211 if (0 && NULL == t->owner && 2203 /* FIXME unlock */
2212 t->queue_max > t->next_fc.queue_n * 4 && 2204 if (0 && NULL == o &&
2213 GMC_is_pid_bigger(t->prev_fc.last_ack_sent, t->prev_fc.last_pid_recv) && 2205 t->queue_max > next_fc->queue_n * 4 &&
2206 GMC_is_pid_bigger (prev_fc->last_ack_sent, prev_fc->last_pid_recv) &&
2214 GNUNET_NO == t->force_ack) 2207 GNUNET_NO == t->force_ack)
2215 { 2208 {
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, buffer free\n"); 2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2218 " t->qmax: %u, t->qn: %u\n", 2211 " t->qmax: %u, t->qn: %u\n",
2219 t->queue_max, t->next_fc.queue_n); 2212 t->queue_max, next_fc->queue_n);
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 " t->pid: %u, t->ack: %u\n", 2214 " t->pid: %u, t->ack: %u\n",
2222 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent); 2215 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
2223 return; 2216 return;
2224 } 2217 }
2225 2218
2226 /* Ok, ACK might be necessary, what PID to ACK? */ 2219 /* Ok, ACK might be necessary, what PID to ACK? */
2227 delta = t->queue_max - t->next_fc.queue_n; 2220 delta = t->queue_max - next_fc->queue_n;
2228 if (NULL != t->owner && GNUNET_YES == t->reliable && NULL != rel->head_sent) 2221 if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent)
2229 delta_mid = rel->mid_sent - rel->head_sent->mid; 2222 delta_mid = rel->mid_sent - rel->head_sent->mid;
2230 else 2223 else
2231 delta_mid = 0; 2224 delta_mid = 0;
2232 if (0 > delta || (GNUNET_YES == t->reliable && 2225 if (0 > delta || (GNUNET_YES == t->reliable &&
2233 NULL != t->owner && 2226 NULL != o &&
2234 (rel->n_sent > 10 || delta_mid > 64))) 2227 (rel->n_sent > 10 || delta_mid > 64)))
2235 delta = 0; 2228 delta = 0;
2236 if (NULL != t->owner && delta > 1) 2229 if (NULL != o && delta > 1)
2237 delta = 1; 2230 delta = 1;
2238 ack = t->prev_fc.last_pid_recv + delta; 2231 ack = prev_fc->last_pid_recv + delta;
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK %u\n", ack); 2232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2241 " last pid %u, last ack %u, qmax %u, q %u\n", 2234 " last pid %u, last ack %u, qmax %u, q %u\n",
2242 t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent, 2235 prev_fc->last_pid_recv, prev_fc->last_ack_sent,
2243 t->queue_max, t->next_fc.queue_n); 2236 t->queue_max, next_fc->queue_n);
2244 if (ack == t->prev_fc.last_ack_sent && GNUNET_NO == t->force_ack) 2237 if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack)
2245 { 2238 {
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n"); 2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
2247 return; 2240 return;
2248 } 2241 }
2249 2242
2250 t->prev_fc.last_ack_sent = ack; 2243 prev_fc->last_ack_sent = ack;
2251 if (NULL != t->owner) 2244 if (NULL != o)
2252 send_local_ack (t, t->owner, GNUNET_YES); 2245 send_local_ack (t, o, fwd);
2253 else if (0 != t->prev_hop) 2246 else if (0 != hop)
2254 send_ack (t, t->prev_hop, ack); 2247 send_ack (t, hop, ack);
2255 else
2256 GNUNET_break (0);
2257 debug_fwd_ack++;
2258 t->force_ack = GNUNET_NO;
2259}
2260
2261
2262/**
2263 * Send an ACK informing the children node/client about the available
2264 * buffer space.
2265 * If buffering is off, send only on behalf of root (can be self).
2266 * If buffering is on, send when sent to predecessor and buffer space is free.
2267 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
2268 * the ACK itself goes "forward" (towards children/clients).
2269 *
2270 * @param t Tunnel on which to send the ACK.
2271 * @param type Type of message that triggered the ACK transmission.
2272 */
2273static void
2274tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2275{
2276 uint32_t ack;
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
2279 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
2280 /* Is it after data to_origin retransmission? */
2281 switch (type)
2282 {
2283 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2284 if (GNUNET_YES == t->nobuffer)
2285 {
2286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2287 " Not sending ACK, nobuffer + traffic\n");
2288 return;
2289 }
2290 if (GNUNET_YES == t->reliable && NULL != t->owner)
2291 tunnel_send_bck_data_ack (t);
2292 break;
2293 case GNUNET_MESSAGE_TYPE_MESH_ACK:
2294 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
2295 break;
2296 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2297 case GNUNET_MESSAGE_TYPE_MESH_POLL:
2298 t->force_ack = GNUNET_YES;
2299 break;
2300 default:
2301 GNUNET_break (0);
2302 }
2303
2304 /* TODO: Check if we need to transmit the ACK (as in fwd) */
2305
2306 ack = t->next_fc.last_pid_recv + t->queue_max - t->prev_fc.queue_n;
2307
2308 if (t->next_fc.last_ack_sent == ack && GNUNET_NO == t->force_ack)
2309 {
2310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2311 " Not sending ACK, not needed, last ack sent was %u\n",
2312 t->next_fc.last_ack_sent);
2313 return;
2314 }
2315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2316 " Sending BCK ACK %u (last sent: %u)\n",
2317 ack, t->next_fc.last_ack_sent);
2318 t->next_fc.last_ack_sent = ack;
2319
2320 if (NULL != t->client)
2321 send_local_ack (t, t->client, GNUNET_NO);
2322 else if (0 != t->next_hop)
2323 send_ack (t, t->next_hop, ack);
2324 else 2248 else
2325 GNUNET_break (0); 2249 GNUNET_break (0);
2326 t->force_ack = GNUNET_NO; 2250 t->force_ack = GNUNET_NO;
@@ -2336,9 +2260,9 @@ tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
2336 * @param tid Tunnel ID to use (c can be both owner and client). 2260 * @param tid Tunnel ID to use (c can be both owner and client).
2337 */ 2261 */
2338static void 2262static void
2339tunnel_send_client_data (struct MeshTunnel *t, 2263tunnel_send_client_to_tid (struct MeshTunnel *t,
2340 const struct GNUNET_MESH_Data *msg, 2264 const struct GNUNET_MESH_Data *msg,
2341 struct MeshClient *c, MESH_TunnelNumber tid) 2265 struct MeshClient *c, MESH_TunnelNumber tid)
2342{ 2266{
2343 struct GNUNET_MESH_LocalData *copy; 2267 struct GNUNET_MESH_LocalData *copy;
2344 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data); 2268 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
@@ -2368,12 +2292,17 @@ tunnel_send_client_data (struct MeshTunnel *t,
2368 * 2292 *
2369 * @param t Tunnel on which to send the message. 2293 * @param t Tunnel on which to send the message.
2370 * @param msg Message to modify and send. 2294 * @param msg Message to modify and send.
2295 * @param fwd Forward?
2371 */ 2296 */
2372static void 2297static void
2373tunnel_send_client_ucast (struct MeshTunnel *t, 2298tunnel_send_client_data (struct MeshTunnel *t,
2374 const struct GNUNET_MESH_Data *msg) 2299 const struct GNUNET_MESH_Data *msg,
2300 int fwd)
2375{ 2301{
2376 tunnel_send_client_data (t, msg, t->client, t->local_tid_dest); 2302 if (fwd)
2303 tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest);
2304 else
2305 tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid);
2377} 2306}
2378 2307
2379 2308
@@ -2381,16 +2310,19 @@ tunnel_send_client_ucast (struct MeshTunnel *t,
2381 * Send up to 64 buffered messages to the client for in order delivery. 2310 * Send up to 64 buffered messages to the client for in order delivery.
2382 * 2311 *
2383 * @param t Tunnel on which to empty the message buffer. 2312 * @param t Tunnel on which to empty the message buffer.
2313 * @param c Client to send to.
2314 * @param rel Reliability structure to corresponding peer.
2315 * If rel == t->bck_rel, this is FWD data.
2384 */ 2316 */
2385static void 2317static void
2386tunnel_send_client_buffered_ucast (struct MeshTunnel *t) 2318tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c,
2319 struct MeshTunnelReliability *rel)
2387{ 2320{
2388 struct MeshTunnelReliability *rel; 2321 ;
2389 struct MeshReliableMessage *copy; 2322 struct MeshReliableMessage *copy;
2390 struct MeshReliableMessage *next; 2323 struct MeshReliableMessage *next;
2391 2324
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast\n"); 2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
2393 rel = t->bck_rel;
2394 for (copy = rel->head_recv; NULL != copy; copy = next) 2326 for (copy = rel->head_recv; NULL != copy; copy = next)
2395 { 2327 {
2396 next = copy->next; 2328 next = copy->next;
@@ -2401,7 +2333,7 @@ tunnel_send_client_buffered_ucast (struct MeshTunnel *t)
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 " have %llu! now expecting %llu\n", 2334 " have %llu! now expecting %llu\n",
2403 copy->mid, rel->mid_recv + 1LL); 2335 copy->mid, rel->mid_recv + 1LL);
2404 tunnel_send_client_ucast (t, msg); 2336 tunnel_send_client_data (t, msg, (rel == t->bck_rel));
2405 rel->mid_recv++; 2337 rel->mid_recv++;
2406 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy); 2338 GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
2407 GNUNET_free (copy); 2339 GNUNET_free (copy);
@@ -2409,34 +2341,37 @@ tunnel_send_client_buffered_ucast (struct MeshTunnel *t)
2409 else 2341 else
2410 { 2342 {
2411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2412 " don't have %llu, (%llu)\n", 2344 " don't have %llu, next is %llu\n",
2413 rel->mid_recv, 2345 rel->mid_recv,
2414 copy->mid); 2346 copy->mid);
2415 return; 2347 return;
2416 } 2348 }
2417 } 2349 }
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_unicast END\n"); 2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
2419} 2351}
2420 2352
2421 2353
2422/** 2354/**
2423 * We have received a message out of order, buffer it until we receive 2355 * We have received a message out of order, buffer it until we receive
2424 * the missing one and we can feed the rest to the client. 2356 * the missing one and we can feed the rest to the client.
2357 *
2358 * @param t Tunnel to add to.
2359 * @param msg Message to buffer.
2360 * @param rel Reliability data to the corresponding direction.
2425 */ 2361 */
2426static void 2362static void
2427tunnel_add_buffer_ucast (struct MeshTunnel *t, 2363tunnel_add_buffered_data (struct MeshTunnel *t,
2428 const struct GNUNET_MESH_Data *msg) 2364 const struct GNUNET_MESH_Data *msg,
2365 struct MeshTunnelReliability *rel)
2429{ 2366{
2430 struct MeshTunnelReliability *rel;
2431 struct MeshReliableMessage *copy; 2367 struct MeshReliableMessage *copy;
2432 struct MeshReliableMessage *prev; 2368 struct MeshReliableMessage *prev;
2433 uint64_t mid; 2369 uint64_t mid;
2434 uint16_t size; 2370 uint16_t size;
2435 2371
2436 rel = t->bck_rel;
2437 size = ntohs (msg->header.size); 2372 size = ntohs (msg->header.size);
2438 mid = GNUNET_ntohll (msg->mid); 2373 mid = GNUNET_ntohll (msg->mid);
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast %llu\n", mid); 2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %llu\n", mid);
2440 2375
2441 copy = GNUNET_malloc (sizeof (*copy) + size); 2376 copy = GNUNET_malloc (sizeof (*copy) + size);
2442 copy->mid = mid; 2377 copy->mid = mid;
@@ -2444,6 +2379,7 @@ tunnel_add_buffer_ucast (struct MeshTunnel *t,
2444 memcpy (&copy[1], msg, size); 2379 memcpy (&copy[1], msg, size);
2445 2380
2446 // FIXME do something better than O(n), although n < 64... 2381 // FIXME do something better than O(n), although n < 64...
2382 // FIXME start from the end (most messages are the latest ones)
2447 for (prev = rel->head_recv; NULL != prev; prev = prev->next) 2383 for (prev = rel->head_recv; NULL != prev; prev = prev->next)
2448 { 2384 {
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %llu\n", prev->mid); 2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %llu\n", prev->mid);
@@ -2457,7 +2393,7 @@ tunnel_add_buffer_ucast (struct MeshTunnel *t,
2457 } 2393 }
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n"); 2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
2459 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy); 2395 GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffer_ucast END\n"); 2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
2461} 2397}
2462 2398
2463 2399
@@ -2498,12 +2434,13 @@ tunnel_free_reliable_message (struct MeshReliableMessage *copy)
2498 * 2434 *
2499 * @param t Tunnel whose sent buffer to clean. 2435 * @param t Tunnel whose sent buffer to clean.
2500 * @param msg DataACK message with a bitfield of future ACK'd messages. 2436 * @param msg DataACK message with a bitfield of future ACK'd messages.
2437 * @param rel Reliability data.
2501 */ 2438 */
2502static void 2439static void
2503tunnel_free_buffer_ucast (struct MeshTunnel *t, 2440tunnel_free_sent_reliable (struct MeshTunnel *t,
2504 const struct GNUNET_MESH_DataACK *msg) 2441 const struct GNUNET_MESH_DataACK *msg,
2442 struct MeshTunnelReliability *rel)
2505{ 2443{
2506 struct MeshTunnelReliability *rel;
2507 struct MeshReliableMessage *copy; 2444 struct MeshReliableMessage *copy;
2508 struct MeshReliableMessage *next; 2445 struct MeshReliableMessage *next;
2509 uint64_t bitfield; 2446 uint64_t bitfield;
@@ -2514,9 +2451,8 @@ tunnel_free_buffer_ucast (struct MeshTunnel *t,
2514 2451
2515 bitfield = msg->futures; 2452 bitfield = msg->futures;
2516 mid = GNUNET_ntohll (msg->mid); 2453 mid = GNUNET_ntohll (msg->mid);
2517 rel = t->fwd_rel;
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 "free_sent_buffer %llu %llX\n", 2455 "free_sent_reliable %llu %llX\n",
2520 mid, bitfield); 2456 mid, bitfield);
2521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2522 " rel %p, head %p\n", 2458 " rel %p, head %p\n",
@@ -2562,21 +2498,7 @@ tunnel_free_buffer_ucast (struct MeshTunnel *t,
2562 tunnel_free_reliable_message (copy); 2498 tunnel_free_reliable_message (copy);
2563 copy = next; 2499 copy = next;
2564 } 2500 }
2565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_buffer END\n"); 2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
2566}
2567
2568
2569/**
2570 * Modify the to_origin message TID from global to local and send to client.
2571 *
2572 * @param t Tunnel on which to send the message.
2573 * @param msg Message to modify and send.
2574 */
2575static void
2576tunnel_send_client_to_orig (struct MeshTunnel *t,
2577 const struct GNUNET_MESH_Data *msg)
2578{
2579 tunnel_send_client_data (t, msg, t->owner, t->local_tid);
2580} 2502}
2581 2503
2582 2504
@@ -2614,8 +2536,8 @@ tunnel_retransmit_message (void *cls,
2614 /* Search the message to be retransmitted in the outgoing queue */ 2536 /* Search the message to be retransmitted in the outgoing queue */
2615 payload = (struct GNUNET_MESH_Data *) &copy[1]; 2537 payload = (struct GNUNET_MESH_Data *) &copy[1];
2616 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop; 2538 hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop;
2617 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc; 2539 fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc;
2618 pi = peer_get_short (hop); 2540 pi = peer_get_short (hop);
2619 for (q = pi->queue_head; NULL != q; q = q->next) 2541 for (q = pi->queue_head; NULL != q; q = q->next)
2620 { 2542 {
2621 if (ntohs (payload->header.type) == q->type) 2543 if (ntohs (payload->header.type) == q->type)
@@ -3085,11 +3007,13 @@ tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3085 * Resets the tunnel timeout. Starts it if no timeout was running. 3007 * Resets the tunnel timeout. Starts it if no timeout was running.
3086 * 3008 *
3087 * @param t Tunnel whose timeout to reset. 3009 * @param t Tunnel whose timeout to reset.
3010 * @param fwd Is this forward?
3088 * 3011 *
3089 * TODO use heap to improve efficiency of scheduler. 3012 * TODO use heap to improve efficiency of scheduler.
3013 * FIXME use fwd, keep 2 timers
3090 */ 3014 */
3091static void 3015static void
3092tunnel_reset_timeout (struct MeshTunnel *t) 3016tunnel_reset_timeout (struct MeshTunnel *t, int fwd)
3093{ 3017{
3094 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop) 3018 if (NULL != t->owner || 0 != t->local_tid || 0 == t->prev_hop)
3095 return; 3019 return;
@@ -3435,14 +3359,14 @@ queue_send (void *cls, size_t size, void *buf)
3435 { 3359 {
3436 case GNUNET_MESSAGE_TYPE_MESH_UNICAST: 3360 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3437 t->next_fc.last_pid_sent = pid; 3361 t->next_fc.last_pid_sent = pid;
3438 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST); 3362 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
3439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3440 "!!! SEND %llu\n", 3364 "!!! SEND %llu\n",
3441 GNUNET_ntohll ( ((struct GNUNET_MESH_Data *) buf)->mid )); 3365 GNUNET_ntohll ( ((struct GNUNET_MESH_Data *) buf)->mid ));
3442 break; 3366 break;
3443 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN: 3367 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3444 t->prev_fc.last_pid_sent = pid; 3368 t->prev_fc.last_pid_sent = pid;
3445 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN); 3369 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
3446 break; 3370 break;
3447 default: 3371 default:
3448 break; 3372 break;
@@ -3683,7 +3607,7 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
3683 } 3607 }
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer); 3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
3685 3609
3686 tunnel_reset_timeout (t); 3610 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
3687 } 3611 }
3688 t->state = MESH_TUNNEL_WAITING; 3612 t->state = MESH_TUNNEL_WAITING;
3689 dest_peer_info = 3613 dest_peer_info =
@@ -3853,8 +3777,8 @@ handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3853 GNUNET_DHT_get_stop (peer_info->dhtget); 3777 GNUNET_DHT_get_stop (peer_info->dhtget);
3854 peer_info->dhtget = NULL; 3778 peer_info->dhtget = NULL;
3855 } 3779 }
3856 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK); 3780 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES);
3857 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK); 3781 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO);
3858 return GNUNET_OK; 3782 return GNUNET_OK;
3859 } 3783 }
3860 3784
@@ -3974,25 +3898,32 @@ handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3974 3898
3975 3899
3976/** 3900/**
3977 * Core handler for mesh network traffic going from the origin to a peer 3901 * Generic handler for mesh network payload traffic.
3902 *
3903 * @param peer Peer identity this notification is about.
3904 * @param message Data message.
3905 * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
3978 * 3906 *
3979 * @param cls closure
3980 * @param peer peer identity this notification is about
3981 * @param message message
3982 * @return GNUNET_OK to keep the connection open, 3907 * @return GNUNET_OK to keep the connection open,
3983 * GNUNET_SYSERR to close it (signal serious error) 3908 * GNUNET_SYSERR to close it (signal serious error)
3984 */ 3909 */
3985static int 3910static int
3986handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer, 3911handle_mesh_data (const struct GNUNET_PeerIdentity *peer,
3987 const struct GNUNET_MessageHeader *message) 3912 const struct GNUNET_MessageHeader *message,
3913 int fwd)
3988{ 3914{
3989 struct GNUNET_MESH_Data *msg; 3915 struct GNUNET_MESH_Data *msg;
3916 struct MeshFlowControl *fc;
3917 struct MeshTunnelReliability *rel;
3990 struct MeshTunnel *t; 3918 struct MeshTunnel *t;
3919 struct MeshClient *c;
3920 GNUNET_PEER_Id hop;
3991 uint32_t pid; 3921 uint32_t pid;
3992 uint32_t ttl; 3922 uint32_t ttl;
3993 size_t size; 3923 size_t size;
3994 3924
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n", 3925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
3926 GNUNET_MESH_DEBUG_M2S (ntohs (message->type)),
3996 GNUNET_i2s (peer)); 3927 GNUNET_i2s (peer));
3997 /* Check size */ 3928 /* Check size */
3998 size = ntohs (message->size); 3929 size = ntohs (message->size);
@@ -4004,7 +3935,7 @@ handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4004 return GNUNET_OK; 3935 return GNUNET_OK;
4005 } 3936 }
4006 msg = (struct GNUNET_MESH_Data *) message; 3937 msg = (struct GNUNET_MESH_Data *) message;
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n", 3938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n",
4008 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type))); 3939 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4009 /* Check tunnel */ 3940 /* Check tunnel */
4010 t = tunnel_get (&msg->oid, ntohl (msg->tid)); 3941 t = tunnel_get (&msg->oid, ntohl (msg->tid));
@@ -4015,58 +3946,62 @@ handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4015 GNUNET_break_op (0); 3946 GNUNET_break_op (0);
4016 return GNUNET_OK; 3947 return GNUNET_OK;
4017 } 3948 }
3949
3950 /* Initialize FWD/BCK data */
4018 pid = ntohl (msg->pid); 3951 pid = ntohl (msg->pid);
4019 if (GMC_is_pid_bigger (pid, t->prev_fc.last_ack_sent)) 3952 fc = fwd ? &t->prev_fc : &t->next_fc;
3953 c = fwd ? t->client : t->owner;
3954 rel = fwd ? t->bck_rel : t->fwd_rel;
3955 hop = fwd ? t->next_hop : t->prev_hop;
3956 if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
4020 { 3957 {
4021 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO); 3958 GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO);
4022 GNUNET_break_op (0); 3959 GNUNET_break_op (0);
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4024 "Received PID %u, (prev %u), ACK %u\n", 3961 "Received PID %u, (prev %u), ACK %u\n",
4025 pid, t->prev_fc.last_pid_recv, t->prev_fc.last_ack_sent); 3962 pid, fc->last_pid_recv, fc->last_ack_sent);
4026 return GNUNET_OK; 3963 return GNUNET_OK;
4027 } 3964 }
4028 3965
4029 tunnel_reset_timeout (t); 3966 tunnel_reset_timeout (t, fwd);
4030 if (t->dest == myid) 3967 if (NULL != c)
4031 { 3968 {
4032 /* TODO signature verification */ 3969 /* TODO signature verification */
4033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n");
4034 " it's for us! sending to clients...\n"); 3971 GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
4035 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO); 3972 if (GMC_is_pid_bigger (pid, fc->last_pid_recv))
4036// if (GMC_is_pid_bigger(pid, t->prev_fc.last_pid_recv)) FIXME use
4037 if (GMC_is_pid_bigger (pid, t->prev_fc.last_pid_recv))
4038 { 3973 {
4039 uint64_t mid; 3974 uint64_t mid;
4040 3975
4041 mid = GNUNET_ntohll (msg->mid); 3976 mid = GNUNET_ntohll (msg->mid);
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4043 " pid %u (%llu) not seen yet\n", pid, mid); 3978 " pid %u (%llu) not seen yet\n", pid, mid);
4044 t->prev_fc.last_pid_recv = pid; 3979 fc->last_pid_recv = pid;
4045 3980
4046 if (GNUNET_NO == t->reliable || 3981 if (GNUNET_NO == t->reliable ||
4047 (mid >= t->bck_rel->mid_recv && mid <= t->bck_rel->mid_recv + 64)) 3982 (mid >= rel->mid_recv && mid <= rel->mid_recv + 64))
4048 { 3983 {
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4050 "!!! RECV %llu\n", GNUNET_ntohll(msg->mid)); 3985 "!!! RECV %llu\n", GNUNET_ntohll (msg->mid));
4051 if (GNUNET_YES == t->reliable) 3986 if (GNUNET_YES == t->reliable)
4052 { 3987 {
4053 /* Is this the exact next expected messasge? */ 3988 /* Is this the exact next expected messasge? */
4054 if (mid == t->bck_rel->mid_recv) 3989 if (mid == rel->mid_recv)
4055 { 3990 {
4056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n"); 3991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
4057 t->bck_rel->mid_recv++; 3992 rel->mid_recv++;
4058 tunnel_send_client_ucast (t, msg); 3993 tunnel_send_client_data (t, msg, fwd);
4059 tunnel_send_client_buffered_ucast (t); 3994 tunnel_send_client_buffered_data (t, c, rel);
4060 } 3995 }
4061 else 3996 else
4062 { 3997 {
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n"); 3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
4064 tunnel_add_buffer_ucast (t, msg); 3999 tunnel_add_buffered_data (t, msg, rel);
4065 } 4000 }
4066 } 4001 }
4067 else /* Tunnel unreliable, send to clients directly */ 4002 else /* Tunnel unreliable, send to clients directly */
4068 { 4003 {
4069 tunnel_send_client_ucast (t, msg); 4004 tunnel_send_client_data (t, msg, fwd);
4070 } 4005 }
4071 } 4006 }
4072 else 4007 else
@@ -4074,7 +4009,7 @@ handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4075 " MID %llu not expected (%llu - %llu), dropping!\n", 4010 " MID %llu not expected (%llu - %llu), dropping!\n",
4076 GNUNET_ntohll (msg->mid), 4011 GNUNET_ntohll (msg->mid),
4077 t->bck_rel->mid_recv, t->bck_rel->mid_recv + 64LL); 4012 rel->mid_recv, rel->mid_recv + 64LL);
4078 } 4013 }
4079 } 4014 }
4080 else 4015 else
@@ -4082,13 +4017,13 @@ handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4082// GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO); 4017// GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 4018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4084 " Pid %u not expected (%u), dropping!\n", 4019 " Pid %u not expected (%u), dropping!\n",
4085 pid, t->prev_fc.last_pid_recv + 1); 4020 pid, fc->last_pid_recv + 1);
4086 } 4021 }
4087 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST); 4022 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, fwd);
4088 return GNUNET_OK; 4023 return GNUNET_OK;
4089 } 4024 }
4090 t->prev_fc.last_pid_recv = pid; 4025 fc->last_pid_recv = pid;
4091 if (0 == t->next_hop) 4026 if (0 == hop)
4092 { 4027 {
4093 GNUNET_break (0); 4028 GNUNET_break (0);
4094 return GNUNET_OK; 4029 return GNUNET_OK;
@@ -4099,133 +4034,52 @@ handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4099 { 4034 {
4100 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO); 4035 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n"); 4036 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4102 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK); 4037 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd);
4103 return GNUNET_OK; 4038 return GNUNET_OK;
4104 } 4039 }
4105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4106 " not for us, retransmitting...\n");
4107 4040
4108 send_prebuilt_message (message, t->next_hop, t); 4041 if (myid != hop)
4109 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO); 4042 {
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
4044 send_prebuilt_message (message, hop, t);
4045 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4046 }
4110 return GNUNET_OK; 4047 return GNUNET_OK;
4111} 4048}
4112 4049
4113 4050
4114/** 4051/**
4115 * Core handler for mesh network traffic toward the owner of a tunnel 4052 * Core handler for mesh network traffic going from the origin to a peer
4116 * 4053 *
4117 * @param cls closure 4054 * @param cls Closure (unused).
4118 * @param message message 4055 * @param message Message received.
4119 * @param peer peer identity this notification is about 4056 * @param peer Peer who sent the message.
4120 * 4057 *
4121 * @return GNUNET_OK to keep the connection open, 4058 * @return GNUNET_OK to keep the connection open,
4122 * GNUNET_SYSERR to close it (signal serious error) 4059 * GNUNET_SYSERR to close it (signal serious error)
4123 */ 4060 */
4124static int 4061static int
4125handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer, 4062handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4126 const struct GNUNET_MessageHeader *message) 4063 const struct GNUNET_MessageHeader *message)
4127{ 4064{
4128 struct GNUNET_MESH_Data *msg; 4065 return handle_mesh_data (peer, message, GNUNET_YES);
4129 struct MeshTunnel *t; 4066}
4130 size_t size;
4131 uint32_t pid;
4132 uint32_t ttl;
4133
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4135 GNUNET_i2s (peer));
4136 size = ntohs (message->size);
4137 if (size < sizeof (struct GNUNET_MESH_Data) + /* Payload must be */
4138 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4139 {
4140 GNUNET_break_op (0);
4141 return GNUNET_OK;
4142 }
4143 msg = (struct GNUNET_MESH_Data *) message;
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4145 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4146 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4147 pid = ntohl (msg->pid);
4148 if (NULL == t)
4149 {
4150 /* TODO notify that we dont know this tunnel (whom)? */
4151 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4153 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4154 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4155 return GNUNET_OK;
4156 }
4157
4158 if (GMC_is_pid_bigger (pid, t->next_fc.last_ack_sent))
4159 {
4160 GNUNET_STATISTICS_update (stats, "# unsolicited to_orig", 1, GNUNET_NO);
4161 GNUNET_break_op (0);
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4163 "Received PID %u, ACK %u\n",
4164 pid, t->next_fc.last_ack_sent);
4165 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
4166 return GNUNET_OK;
4167 }
4168
4169 if (myid == t->id.oid)
4170 {
4171 /* TODO signature verification */
4172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4173 " it's for us! sending to clients...\n");
4174 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
4175 if ( (GNUNET_NO == t->reliable &&
4176 GMC_is_pid_bigger(pid, t->next_fc.last_pid_recv))
4177 ||
4178 (GNUNET_YES == t->reliable &&
4179 pid == t->next_fc.last_pid_recv + 1) ) // FIXME use "futures" as accepting
4180 {
4181 t->next_fc.last_pid_recv = pid;
4182 tunnel_send_client_to_orig (t, msg);
4183 }
4184 else
4185 {
4186// GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
4187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4188 " Pid %u not expected, sending FWD ACK!\n", pid);
4189 }
4190 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4191 return GNUNET_OK;
4192 }
4193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4194 " not for us, retransmitting...\n");
4195 t->next_fc.last_pid_recv = pid;
4196 if (0 == t->prev_hop) /* No owner AND no prev hop */
4197 {
4198 if (GNUNET_YES == t->destroy)
4199 {
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 "to orig received on a dying tunnel %s [%X]\n",
4202 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4203 return GNUNET_OK;
4204 }
4205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4206 "unknown to origin at %s\n",
4207 GNUNET_i2s (&my_full_id));
4208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4209 "from peer %s\n",
4210 GNUNET_i2s (peer));
4211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4212 "on tunnel %s [%X]\n",
4213 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
4214 return GNUNET_OK;
4215 }
4216 ttl = ntohl (msg->ttl);
4217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4218 if (ttl == 0)
4219 {
4220 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4221 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4222 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4223 return GNUNET_OK;
4224 }
4225 send_prebuilt_message (message, t->prev_hop, t);
4226 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
4227 4067
4228 return GNUNET_OK; 4068/**
4069 * Core handler for mesh network traffic towards the owner of a tunnel.
4070 *
4071 * @param cls Closure (unused).
4072 * @param message Message received.
4073 * @param peer Peer who sent the message.
4074 *
4075 * @return GNUNET_OK to keep the connection open,
4076 * GNUNET_SYSERR to close it (signal serious error)
4077 */
4078static int
4079handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4080 const struct GNUNET_MessageHeader *message)
4081{
4082 return handle_mesh_data (peer, message, GNUNET_NO);
4229} 4083}
4230 4084
4231 4085
@@ -4279,7 +4133,7 @@ handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4279 return GNUNET_OK; 4133 return GNUNET_OK;
4280 } 4134 }
4281 rel = t->fwd_rel; 4135 rel = t->fwd_rel;
4282 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST); 4136 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES);
4283 } 4137 }
4284 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type) 4138 else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type)
4285 { 4139 {
@@ -4290,7 +4144,7 @@ handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4290 return GNUNET_OK; 4144 return GNUNET_OK;
4291 } 4145 }
4292 rel = t->bck_rel; 4146 rel = t->bck_rel;
4293 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN); 4147 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO);
4294 } 4148 }
4295 else 4149 else
4296 { 4150 {
@@ -4304,7 +4158,7 @@ handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4304 if (copy->mid > ack) 4158 if (copy->mid > ack)
4305 { 4159 {
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %llu, out!\n", copy->mid); 4160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %llu, out!\n", copy->mid);
4307 tunnel_free_buffer_ucast (t, msg); 4161 tunnel_free_sent_reliable (t, msg, rel);
4308 break; 4162 break;
4309 } 4163 }
4310 work = GNUNET_YES; 4164 work = GNUNET_YES;
@@ -4340,7 +4194,7 @@ handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4340 } 4194 }
4341 else 4195 else
4342 GNUNET_break (0); 4196 GNUNET_break (0);
4343 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK); 4197 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK, GNUNET_YES);
4344 } 4198 }
4345 return GNUNET_OK; 4199 return GNUNET_OK;
4346} 4200}
@@ -4384,13 +4238,11 @@ handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4384 id = GNUNET_PEER_search (peer); 4238 id = GNUNET_PEER_search (peer);
4385 if (t->next_hop == id) 4239 if (t->next_hop == id)
4386 { 4240 {
4387 debug_fwd_ack++;
4388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n"); 4241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
4389 fc = &t->next_fc; 4242 fc = &t->next_fc;
4390 } 4243 }
4391 else if (t->prev_hop == id) 4244 else if (t->prev_hop == id)
4392 { 4245 {
4393 debug_bck_ack++;
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n"); 4246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
4395 fc = &t->prev_fc; 4247 fc = &t->prev_fc;
4396 } 4248 }
@@ -4410,10 +4262,7 @@ handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
4410 fc->last_ack_recv = ack; 4262 fc->last_ack_recv = ack;
4411 peer_unlock_queue (id); 4263 peer_unlock_queue (id);
4412 4264
4413 if (t->next_hop == id) 4265 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id);
4414 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4415 else
4416 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4417 4266
4418 return GNUNET_OK; 4267 return GNUNET_OK;
4419} 4268}
@@ -4466,7 +4315,7 @@ handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4466 fc = &t->next_fc; 4315 fc = &t->next_fc;
4467 old = fc->last_pid_recv; 4316 old = fc->last_pid_recv;
4468 fc->last_pid_recv = pid; 4317 fc->last_pid_recv = pid;
4469 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL); 4318 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, GNUNET_NO);
4470 } 4319 }
4471 else if (t->prev_hop == id) 4320 else if (t->prev_hop == id)
4472 { 4321 {
@@ -4475,11 +4324,11 @@ handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
4475 fc = &t->prev_fc; 4324 fc = &t->prev_fc;
4476 old = fc->last_pid_recv; 4325 old = fc->last_pid_recv;
4477 fc->last_pid_recv = pid; 4326 fc->last_pid_recv = pid;
4478 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL); 4327 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, GNUNET_YES);
4479 } 4328 }
4480 else 4329 else
4481 GNUNET_break (0); 4330 GNUNET_break (0);
4482 4331
4483 if (GNUNET_YES == t->reliable) 4332 if (GNUNET_YES == t->reliable)
4484 fc->last_pid_recv = old; 4333 fc->last_pid_recv = old;
4485 4334
@@ -4519,7 +4368,7 @@ handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
4519 return GNUNET_OK; 4368 return GNUNET_OK;
4520 } 4369 }
4521 4370
4522 tunnel_reset_timeout (t); 4371 tunnel_reset_timeout (t, GNUNET_YES); // FIXME
4523 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop) 4372 if (NULL != t->client || 0 == t->next_hop || myid == t->next_hop)
4524 return GNUNET_OK; 4373 return GNUNET_OK;
4525 4374
@@ -5187,17 +5036,17 @@ handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
5187 } 5036 }
5188 5037
5189 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */ 5038 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
5190 if (t->owner == c) 5039 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5191 { 5040 {
5192 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */ 5041 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
5193 t->prev_fc.last_ack_recv++; 5042 t->prev_fc.last_ack_recv++;
5194 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK); 5043 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO);
5195 } 5044 }
5196 else 5045 else
5197 { 5046 {
5198 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */ 5047 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
5199 t->next_fc.last_ack_recv++; 5048 t->next_fc.last_ack_recv++;
5200 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK); 5049 tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES);
5201 } 5050 }
5202 5051
5203 GNUNET_SERVER_receive_done (client, GNUNET_OK); 5052 GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -5803,9 +5652,5 @@ main (int argc, char *const *argv)
5803 5652
5804 INTERVAL_SHOW; 5653 INTERVAL_SHOW;
5805 5654
5806 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5807 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
5808 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);
5809
5810 return ret; 5655 return ret;
5811} 5656}