Contiki 2.5
routing_flooding.c
Go to the documentation of this file.
1 /**
2  * \addtogroup routing
3  * @{
4  */
5 
6 /**
7  * \defgroup routing_flooding Flooding Routing module
8  *
9  * @{
10  */
11 
12 /**
13  * \file
14  * \brief implementation of flood routing
15  * \author Georg von Zengen <vonzeng@ibr.cs.tu-bs.de>
16  * \author Wolf-Bastian Poettner <poettner@ibr.cs.tu-bs.de>
17  */
18 
19 #include <string.h>
20 
21 #include "net/netstack.h"
22 #include "net/rime/rimeaddr.h"
23 #include "lib/list.h"
24 #include "lib/memb.h"
25 #include "contiki.h"
26 #include "clock.h"
27 #include "logging.h"
28 
29 #include "bundle.h"
30 #include "storage.h"
31 #include "sdnv.h"
32 #include "agent.h"
33 #include "discovery.h"
34 #include "statistics.h"
35 #include "bundleslot.h"
36 #include "delivery.h"
37 #include "convergence_layer.h"
38 #include "registration.h"
39 
40 #include "routing.h"
41 
42 #define BLACKLIST_TIMEOUT 10
43 #define BLACKLIST_THRESHOLD 3
44 #define BLACKLIST_SIZE 3
45 
46 /**
47  * Internally used return values
48  */
49 #define FLOOD_ROUTE_RETURN_OK 1
50 #define FLOOD_ROUTE_RETURN_CONTINUE 0
51 #define FLOOD_ROUTE_RETURN_FAIL -1
52 
53 struct blacklist_entry_t {
54  struct blacklist_entry_t * next;
55 
56  rimeaddr_t node;
57  uint8_t counter;
58  clock_time_t timestamp;
59 };
60 
61 struct routing_list_entry_t {
62  /** pointer to the next entry */
63  struct routing_list_entry_t * next;
64 
65  /** pointer to MMEM containing the routing_entry_t */
66  struct mmem entry;
67 } __attribute__ ((packed));
68 
69 struct routing_entry_t {
70  /** number of the bundle */
71  uint32_t bundle_number;
72 
73  /** bundle flags */
74  uint8_t flags;
75 
76  /** number of nodes the bundle has been sent to already */
77  uint8_t send_to;
78 
79  /** addresses of nodes this bundle was sent to */
81 
82  /** bundle destination */
83  uint32_t destination_node;
84 
85  /** bundle source */
86  uint32_t source_node;
87 
88  /** neighbour from which we have received the bundle */
89  rimeaddr_t received_from_node;
90 } __attribute__ ((packed));
91 
92 /**
93  * Routing process
94  */
95 PROCESS(routing_process, "FLOOD ROUTE process");
96 
97 MEMB(blacklist_mem, struct blacklist_entry_t, BLACKLIST_SIZE);
98 LIST(blacklist_list);
99 
100 MEMB(routing_mem, struct routing_list_entry_t, BUNDLE_STORAGE_SIZE);
101 LIST(routing_list);
102 
105 
106 /**
107  * \brief Adds (or refreshes) the entry of 'neighbour' on the blacklist
108  * \param neighbour Address of the neighbour
109  * \return 1 if neighbour is (now) blacklisted, 0 otherwise
110  */
111 int routing_flooding_blacklist_add(rimeaddr_t * neighbour)
112 {
113  struct blacklist_entry_t * entry;
114 
115  for(entry = list_head(blacklist_list);
116  entry != NULL;
117  entry = list_item_next(entry)) {
118  if( rimeaddr_cmp(neighbour, &entry->node) ) {
119  if( (clock_time() - entry->timestamp) > (BLACKLIST_TIMEOUT * CLOCK_SECOND) ) {
120  // Reusing existing (timedout) entry
121  entry->counter = 0;
122  }
123 
124  entry->counter ++;
125  entry->timestamp = clock_time();
126 
127  if( entry->counter > BLACKLIST_THRESHOLD ) {
128  LOG(LOGD_DTN, LOG_ROUTE, LOGL_INF, "%u.%u blacklisted", neighbour->u8[0], neighbour->u8[1]);
129  return 1;
130  }
131 
132  // Found but not blacklisted
133  return 0;
134  }
135  }
136 
137  entry = memb_alloc(&blacklist_mem);
138 
139  if( entry == NULL ) {
140  LOG(LOGD_DTN, LOG_ROUTE, LOGL_WRN, "Cannot allocate memory for blacklist");
141  return 0;
142  }
143 
144  rimeaddr_copy(&entry->node, neighbour);
145  entry->counter = 1;
146  entry->timestamp = clock_time();
147 
148  list_add(blacklist_list, entry);
149 
150  return 0;
151 }
152 
153 /**
154  * \brief Deletes a neighbour from the blacklist
155  * \param neighbour Address of the neighbour
156  */
157 void routing_flooding_blacklist_delete(rimeaddr_t * neighbour)
158 {
159  struct blacklist_entry_t * entry;
160 
161  for(entry = list_head(blacklist_list);
162  entry != NULL;
163  entry = list_item_next(entry)) {
164  if( rimeaddr_cmp(neighbour, &entry->node) ) {
165  list_remove(blacklist_list, entry);
166  memset(entry, 0, sizeof(struct blacklist_entry_t));
167  memb_free(&blacklist_mem, entry);
168  return;
169  }
170  }
171 }
172 
173 /**
174  * \brief called by agent at startup
175  */
177 {
178  // Start CL process
179  process_start(&routing_process, NULL);
180 }
181 
182 /**
183  * \brief Poll our process, so that we can resubmit bundles
184  */
186 {
187  process_poll(&routing_process);
188 }
189 
190 /**
191  * \brief checks if there are bundle to send to dest
192  * \param dest pointer to the address of the new neighbor
193  */
194 void routing_flooding_new_neighbour(rimeaddr_t *dest)
195 {
197 }
198 
199 /**
200  * \brief Send bundle to neighbour
201  * \param bundle_number Number of the bundle
202  * \param neighbour Address of the neighbour
203  * \return 1 on success, -1 on error
204  */
205 int routing_flooding_send_bundle(uint32_t bundle_number, rimeaddr_t * neighbour)
206 {
207  struct transmit_ticket_t * ticket = NULL;
208 
209  /* Allocate a transmission ticket */
210  ticket = convergence_layer_get_transmit_ticket();
211  if( ticket == NULL ) {
212  LOG(LOGD_DTN, LOG_ROUTE, LOGL_WRN, "unable to allocate transmit ticket");
213  return -1;
214  }
215 
216  /* Specify which bundle */
217  rimeaddr_copy(&ticket->neighbour, neighbour);
218  ticket->bundle_number = bundle_number;
219 
220  /* Put the bundle in the queue */
221  convergence_layer_enqueue_bundle(ticket);
222 
223  return 1;
224 }
225 
226 /**
227  * \brief Deliver a bundle to a local service
228  * \param entry Pointer to the routing entry of the bundle
229  * \return FLOOD_ROUTE_RETURN_OK if delivered, FLOOD_ROUTE_RETURN_CONTINUE otherwise
230  */
231 int routing_flooding_send_to_local(struct routing_entry_t * entry)
232 {
233  struct mmem * bundlemem = NULL;
234  int ret = 0;
235 
236  // Should this bundle be delivered locally?
237  if( !(entry->flags & ROUTING_FLAG_LOCAL) || (entry->flags & ROUTING_FLAG_IN_DELIVERY) ) {
238  return FLOOD_ROUTE_RETURN_CONTINUE;
239  }
240 
241  bundlemem = BUNDLE_STORAGE.read_bundle(entry->bundle_number);
242  if( bundlemem == NULL ) {
243  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "cannot read bundle %lu", entry->bundle_number);
244  return FLOOD_ROUTE_RETURN_CONTINUE;
245  }
246 
247  ret = delivery_deliver_bundle(bundlemem);
248  if( ret == DELIVERY_STATE_WAIT_FOR_APP ) {
249  entry->flags |= ROUTING_FLAG_IN_DELIVERY;
250  return FLOOD_ROUTE_RETURN_OK;
251  } else if( ret == DELIVERY_STATE_DELETE ) {
252  // Bundle can be deleted right away
253  entry->flags &= ~ROUTING_FLAG_LOCAL;
254 
255  // Reschedule ourselves
257 
258  // And remove bundle if applicable
259  routing_flooding_check_keep_bundle(entry->bundle_number);
260  } else if( ret == DELIVERY_STATE_BUSY ) {
261  return FLOOD_ROUTE_RETURN_OK;
262  }
263 
264  return FLOOD_ROUTE_RETURN_CONTINUE;
265 }
266 
267 /**
268  * \brief Forward a bundle to its destination
269  * \param entry Pointer to the routing entry of the bundle
270  * \return FLOOD_ROUTE_RETURN_OK if queued, FLOOD_ROUTE_RETURN_CONTINUE if not queued and FLOOD_ROUTE_RETURN_FAIL of queue is full
271  */
272 int routing_flooding_forward_directly(struct routing_entry_t * entry)
273 {
274  struct discovery_neighbour_list_entry *nei_l = NULL;
275  rimeaddr_t dest_node;
276  int h = 0;
277 
278  /* Who is the destination for this bundle? */
279  dest_node = convert_eid_to_rime(entry->destination_node);
280 
281  /* First step: check, if the destination is one of our neighbours */
282  for( nei_l = DISCOVERY.neighbours();
283  nei_l != NULL;
284  nei_l = list_item_next(nei_l) ) {
285 
286  if( rimeaddr_cmp(&nei_l->neighbour, &dest_node) ) {
287  break;
288  }
289  }
290 
291  if( nei_l == NULL ) {
292  return FLOOD_ROUTE_RETURN_CONTINUE;
293  }
294 
295  /* We know the neighbour, send it directly */
296  LOG(LOGD_DTN, LOG_ROUTE, LOGL_INF, "send bundle %lu to %u.%u directly", entry->bundle_number, nei_l->neighbour.u8[0], nei_l->neighbour.u8[1]);
297 
298  /* Mark bundle as busy */
299  entry->flags |= ROUTING_FLAG_IN_TRANSIT;
300 
301  /* And queue it for sending */
302  h = routing_flooding_send_bundle(entry->bundle_number, &nei_l->neighbour);
303  if( h < 0 ) {
304  /* Enqueuing bundle failed - unblock it */
305  entry->flags &= ~ROUTING_FLAG_IN_TRANSIT;
306 
307  /* If sending the bundle fails, all other will likely also fail */
308  return FLOOD_ROUTE_RETURN_FAIL;
309  }
310 
311  /* We do not want the bundle to be sent to anybody else at the moment, so: */
312  return FLOOD_ROUTE_RETURN_OK;
313 }
314 
315 /**
316  * \brief Forward a bundle to the next hop
317  * \param entry Pointer to the routing entry of the bundle
318  * \return FLOOD_ROUTE_RETURN_OK if queued, FLOOD_ROUTE_RETURN_CONTINUE if not queued and FLOOD_ROUTE_RETURN_FAIL of queue is full
319  */
320 int routing_flooding_forward_normal(struct routing_entry_t * entry)
321 {
322  struct discovery_neighbour_list_entry *nei_l = NULL;
323  rimeaddr_t source_node;
324  int h = 0;
325 
326  /* What is the source node of the bundle? */
327  source_node = convert_eid_to_rime(entry->source_node);
328 
329  for( nei_l = DISCOVERY.neighbours();
330  nei_l != NULL;
331  nei_l = list_item_next(nei_l) ) {
332  int sent = 0;
333  int i;
334 
335  if( rimeaddr_cmp(&nei_l->neighbour, &source_node) ) {
336  LOG(LOGD_DTN, LOG_ROUTE, LOGL_INF, "not sending bundle to originator");
337 
338  /* Go on with the next neighbour */
339  continue;
340  }
341 
342  if( rimeaddr_cmp(&nei_l->neighbour, &entry->received_from_node) ) {
343  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "not sending back to sender");
344 
345  /* Go on with the next neighbour */
346  continue;
347  }
348 
349  /* Did we forward the bundle to that neighbour already? */
350  for (i = 0 ; i < ROUTING_NEI_MEM ; i++) {
351  if ( rimeaddr_cmp(&entry->neighbours[i], &nei_l->neighbour)){
352  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle %lu already sent to node %u.%u!", entry->bundle_number, entry->neighbours[i].u8[0], entry->neighbours[i].u8[1]);
353  sent = 1;
354 
355  // Break the (narrowest) for
356  break;
357  }
358  }
359 
360  if(!sent) {
361  LOG(LOGD_DTN, LOG_ROUTE, LOGL_INF, "send bundle %lu to %u.%u", entry->bundle_number, nei_l->neighbour.u8[0], nei_l->neighbour.u8[1]);
362 
363  /* Mark bundle as busy */
364  entry->flags |= ROUTING_FLAG_IN_TRANSIT;
365 
366  /* And queue it for sending */
367  h = routing_flooding_send_bundle(entry->bundle_number, &nei_l->neighbour);
368  if( h < 0 ) {
369  /* Enqueuing bundle failed - unblock it */
370  entry->flags &= ~ROUTING_FLAG_IN_TRANSIT;
371 
372  /* If sending the bundle fails, all other will likely also fail */
373  return FLOOD_ROUTE_RETURN_FAIL;
374  }
375 
376  /* Only one bundle at a time */
377  return FLOOD_ROUTE_RETURN_OK;
378  }
379  }
380 
381  return FLOOD_ROUTE_RETURN_CONTINUE;
382 }
383 
384 /**
385  * \brief iterate through all bundles and forward bundles
386  */
388 {
389  struct routing_list_entry_t * n = NULL;
390  struct routing_entry_t * entry = NULL;
391  int try_to_forward = 1;
392  int try_local = 1;
393  int h = 0;
394 
395  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "send to known neighbours");
396 
397  /**
398  * It is likely, that we will have less neighbours than bundles - therefore, we want to to go through bundles only once
399  */
400  for( n = (struct routing_list_entry_t *) list_head(routing_list);
401  n != NULL;
402  n = list_item_next(n) ) {
403 
404  entry = (struct routing_entry_t *) MMEM_PTR(&n->entry);
405  if( entry == NULL ) {
406  LOG(LOGD_DTN, LOG_ROUTE, LOGL_WRN, "Bundle with invalid MMEM structure");
407  }
408 
409  if( try_local ) {
410  /* Is the bundle for local? */
412 
413  /* We can only deliver only bundle at a time to local processes to speed up the whole thing */
414  if( h == FLOOD_ROUTE_RETURN_OK ) {
415  try_local = 0;
416  }
417  }
418 
419  /* Skip this bundle, if it is not queued for forwarding */
420  if( !(entry->flags & ROUTING_FLAG_FORWARD) || (entry->flags & ROUTING_FLAG_IN_TRANSIT) || !try_to_forward ) {
421  continue;
422  }
423 
424  /* Try to forward it to the destination, if it is our neighbour */
426  if( h == FLOOD_ROUTE_RETURN_OK ) {
427  /* Bundle will be delivered, to skip the remainder if this function*/
428  continue;
429  } else if( h == FLOOD_ROUTE_RETURN_CONTINUE ) {
430  /* Bundle was not delivered, continue as normal */
431  } else if( h == FLOOD_ROUTE_RETURN_FAIL ) {
432  /* Enqueuing the bundle failed, to stop the forwarding process */
433  try_to_forward = 0;
434  continue;
435  }
436 
437  /* At this point, we know that the bundle is not for one of our neighbours, so send it to all the others */
439  if( h == FLOOD_ROUTE_RETURN_OK ) {
440  /* Bundle will be forwarded, continue as normal */
441  } else if( h == FLOOD_ROUTE_RETURN_CONTINUE ) {
442  /* Bundle will not be forwarded, continue as normal */
443  } else if( h == FLOOD_ROUTE_RETURN_FAIL ) {
444  /* Enqueuing the bundle failed, to stop the forwarding process */
445  try_to_forward = 0;
446  continue;
447  }
448  }
449 }
450 
451 /**
452  * \brief Wrapper function for agent calls to resubmit bundles for already known neighbours
453  */
456 }
457 
458 /**
459  * \brief Checks whether a bundle still has to be kept or can be deleted
460  * \param bundle_number Number of the bundle
461  */
463  struct routing_list_entry_t * n = NULL;
464  struct routing_entry_t * entry = NULL;
465 
466  // Now we have to find the appropriate Storage struct
467  for( n = (struct routing_list_entry_t *) list_head(routing_list);
468  n != NULL;
469  n = list_item_next(n) ) {
470 
471  entry = (struct routing_entry_t *) MMEM_PTR(&n->entry);
472 
473  if( entry->bundle_number == bundle_number ) {
474  break;
475  }
476  }
477 
478  if( n == NULL ) {
479  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "Bundle not in storage yet");
480  return;
481  }
482 
483  if( (entry->flags & ROUTING_FLAG_LOCAL) || (entry->flags & ROUTING_FLAG_FORWARD) ) {
484  return;
485  }
486 
487  LOG(LOGD_DTN, LOG_ROUTE, LOGL_INF, "Deleting bundle %lu", bundle_number);
488  BUNDLE_STORAGE.del_bundle(bundle_number, REASON_DELIVERED);
489 }
490 
491 /**
492  * \brief Adds a new bundle to the list of bundles
493  * \param bundle_number bundle number of the bundle
494  * \return >0 on success, <0 on error
495  */
497 {
498  struct routing_list_entry_t * n = NULL;
499  struct routing_entry_t * entry = NULL;
500  struct mmem * bundlemem = NULL;
501  struct bundle_t * bundle = NULL;
502 
503  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "agent announces bundle %lu", *bundle_number);
504 
505  // Let us see, if we know this bundle already
506  for( n = list_head(routing_list);
507  n != NULL;
508  n = list_item_next(n) ) {
509 
510  entry = (struct routing_entry_t *) MMEM_PTR(&n->entry);
511 
512  if( entry->bundle_number == *bundle_number ) {
513  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "agent announces bundle %lu that is already known", *bundle_number);
514  return -1;
515  }
516  }
517 
518  // Notify statistics
520 
521  // Now allocate new memory for the list entry
522  n = memb_alloc(&routing_mem);
523  if( n == NULL ) {
524  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "cannot allocate list entry for bundle, please increase BUNDLE_STORAGE_SIZE");
525  return -1;
526  }
527 
528  memset(n, 0, sizeof(struct routing_list_entry_t));
529 
530  // Now allocate new MMEM memory for the struct in the list
531  if( !mmem_alloc(&n->entry, sizeof(struct routing_entry_t)) ) {
532  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "cannot allocate routing struct for bundle, MMEM is full");
533  memb_free(&routing_mem, n);
534  return -1;
535  }
536 
537  // Now go and request the bundle from storage
538  bundlemem = BUNDLE_STORAGE.read_bundle(*bundle_number);
539  if( bundlemem == NULL ) {
540  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "unable to read bundle %lu", *bundle_number);
541  mmem_free(&n->entry);
542  memb_free(&routing_mem, n);
543  return -1;
544  }
545 
546  // Get our bundle struct and check the pointer
547  bundle = (struct bundle_t *) MMEM_PTR(bundlemem);
548  if( bundle == NULL ) {
549  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "invalid bundle pointer for bundle %lu", *bundle_number);
550  mmem_free(&n->entry);
551  memb_free(&routing_mem, n);
552  bundle_decrement(bundlemem);
553  return -1;
554  }
555 
556  // Now we have our entry
557  // We have to get the pointer AFTER getting the bundle from storage, because accessing the
558  // storage may change the MMEM structure and thus the pointers!
559  entry = (struct routing_entry_t *) MMEM_PTR(&n->entry);
560  memset(entry, 0, sizeof(struct routing_entry_t));
561 
562  // Nothing can go wrong anymore, add the (surrounding) struct to the list
563  list_add(routing_list, n);
564 
565  /* Here we decide if a bundle is to be delivered locally and/or forwarded */
566  if( bundle->dst_node == dtn_node_id ) {
567  /* This bundle is for our node_id, deliver locally */
568  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle is for local");
569  entry->flags |= ROUTING_FLAG_LOCAL;
570  } else {
571  /* This bundle is not (directly) for us and will be forwarded */
572  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle is for forward");
573  entry->flags |= ROUTING_FLAG_FORWARD;
574  }
575 
576  if( !(bundle->flags & BUNDLE_FLAG_SINGLETON) ) {
577  /* Bundle is not Singleton, so forward it in any case */
578  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle is for forward");
579  entry->flags |= ROUTING_FLAG_FORWARD;
580  }
581 
582  if( registration_is_local(bundle->dst_srv, bundle->dst_node) && bundle->dst_node != dtn_node_id) {
583  /* Bundle is for a local registration, so deliver it locally */
584  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle is for local and forward");
585  entry->flags |= ROUTING_FLAG_LOCAL;
586  entry->flags |= ROUTING_FLAG_FORWARD;
587  }
588 
589  // Now copy the necessary attributes from the bundle
590  entry->bundle_number = *bundle_number;
591  bundle_get_attr(bundlemem, DEST_NODE, &entry->destination_node);
592  bundle_get_attr(bundlemem, SRC_NODE, &entry->source_node);
593  rimeaddr_copy(&entry->received_from_node, &bundle->msrc);
594 
595  // Now that we have the bundle, we do not need the allocated memory anymore
596  bundle_decrement(bundlemem);
597  bundlemem = NULL;
598  bundle = NULL;
599 
600  // Schedule to deliver and forward the bundle
602 
603  // We do not have a failure here, so it must be a success
604  return 1;
605 }
606 
607 /**
608  * \brief deletes bundle from list
609  * \param bundle_number bundle number of the bundle
610  */
612 {
613  struct routing_list_entry_t * n = NULL;
614  struct routing_entry_t * entry = NULL;
615 
616  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "flood_del_bundle for bundle %lu", bundle_number);
617 
618  // Find the bundle in our internal storage
619  for( n = list_head(routing_list);
620  n != NULL;
621  n = list_item_next(n) ) {
622 
623  entry = (struct routing_entry_t *) MMEM_PTR(&n->entry);
624 
625  if( entry->bundle_number == bundle_number ) {
626  break;
627  }
628  }
629 
630  if( n == NULL ) {
631  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "flood_del_bundle for bundle %lu that we do not know", bundle_number);
632  return;
633  }
634 
635  memset(MMEM_PTR(&n->entry), 0, sizeof(struct routing_entry_t));
636 
637  // Free up the memory for the struct
638  mmem_free(&n->entry);
639 
640  list_remove(routing_list, n);
641 
642  memset(n, 0, sizeof(struct routing_list_entry_t));
643 
644  // And also free the memory for the list entry
645  memb_free(&routing_mem, n);
646 }
647 
648 /**
649  * \brief Callback function informing us about the status of a sent bundle
650  * \param ticket CL transmit ticket of the bundle
651  * \param status status code
652  */
653 void routing_flooding_bundle_sent(struct transmit_ticket_t * ticket, uint8_t status)
654 {
655  struct routing_list_entry_t * n = NULL;
656  struct routing_entry_t * entry = NULL;
657 
658  // Tell the agent to call us again to resubmit bundles
660 
661  // Find the bundle in our internal storage
662  for( n = list_head(routing_list);
663  n != NULL;
664  n = list_item_next(n) ) {
665 
666  entry = (struct routing_entry_t *) MMEM_PTR(&n->entry);
667 
668  if( entry->bundle_number == ticket->bundle_number ) {
669  break;
670  }
671  }
672 
673  if( n == NULL ) {
674  /* Free up the ticket */
675  convergence_layer_free_transmit_ticket(ticket);
676 
677  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "Bundle not in storage");
678  return;
679  }
680 
681  /* Bundle is not busy anymore */
682  entry->flags &= ~ROUTING_FLAG_IN_TRANSIT;
683 
684  if( status == ROUTING_STATUS_NACK ||
685  status == ROUTING_STATUS_FAIL ) {
686  // NACK = Other side rejected the bundle, try again later
687  // FAIL = Transmission failed
688  // --> note down address in blacklist
689  if( routing_flooding_blacklist_add(&ticket->neighbour) ) {
690  // Node is now past threshold and blacklisted, notify discovery
691  routing_flooding_blacklist_delete(&ticket->neighbour);
692 
693  // "dead" will free all tickets, we do not have to do it
694  DISCOVERY.dead(&ticket->neighbour);
695 
696  return;
697  }
698 
699  /* Free up the ticket */
700  convergence_layer_free_transmit_ticket(ticket);
701 
702  return;
703  }
704 
705  if( status == ROUTING_STATUS_ERROR ) {
706  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "Bundle %lu has fatal error, deleting", ticket->bundle_number);
707 
708  /* Bundle failed permanently, we can delete it because it will never be delivered anyway */
709  entry->flags = 0;
710 
711  routing_flooding_check_keep_bundle(ticket->bundle_number);
712 
713  /* Free up the ticket */
714  convergence_layer_free_transmit_ticket(ticket);
715 
716  return;
717  }
718 
719  // Here: status == ROUTING_STATUS_OK
721 
722  routing_flooding_blacklist_delete(&ticket->neighbour);
723 
724 #ifndef TEST_DO_NOT_DELETE_ON_DIRECT_DELIVERY
725  rimeaddr_t dest_n = convert_eid_to_rime(entry->destination_node);
726  if (rimeaddr_cmp(&ticket->neighbour, &dest_n)) {
727  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle sent to destination node");
728  uint32_t bundle_number = ticket->bundle_number;
729 
730  /* Free up the ticket */
731  convergence_layer_free_transmit_ticket(ticket);
732  ticket = NULL;
733 
734  // Unset the forward flag
735  entry->flags &= ~ROUTING_FLAG_FORWARD;
736  routing_flooding_check_keep_bundle(bundle_number);
737 
738  return;
739  }
740 
741  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle for %u.%u delivered to %u.%u", dest_n.u8[0], dest_n.u8[1], ticket->neighbour.u8[0], ticket->neighbour.u8[1]);
742 #endif
743 
744 
745  if (entry->send_to < ROUTING_NEI_MEM) {
746  rimeaddr_copy(&entry->neighbours[entry->send_to], &ticket->neighbour);
747  entry->send_to++;
748  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle %lu sent to %u nodes", ticket->bundle_number, entry->send_to);
749  } else if (entry->send_to >= ROUTING_NEI_MEM) {
750  // Here we can delete the bundle from storage, because it will not be routed anyway
751  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle %lu sent to max number of nodes, deleting", ticket->bundle_number);
752 
753  // Unset the forward flag
754  entry->flags &= ~ROUTING_FLAG_FORWARD;
755  routing_flooding_check_keep_bundle(ticket->bundle_number);
756  }
757 
758  /* Free up the ticket */
759  convergence_layer_free_transmit_ticket(ticket);
760 }
761 
762 /**
763  * \brief Incoming notification, that service has finished processing bundle
764  * \param bundlemem Pointer to the MMEM struct of the bundle
765  */
766 void routing_flooding_bundle_delivered_locally(struct mmem * bundlemem) {
767  struct routing_list_entry_t * n = NULL;
768  struct routing_entry_t * entry = NULL;
769  struct bundle_t * bundle = (struct bundle_t *) MMEM_PTR(bundlemem);
770 
771  // Tell the agent to call us again to resubmit bundles
773 
774  if( bundle == NULL ) {
775  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "flood_locally_delivered called with invalid pointer");
776  return;
777  }
778 
779  // Find the bundle in our internal storage
780  for( n = (struct routing_list_entry_t *) list_head(routing_list);
781  n != NULL;
782  n = list_item_next(n) ) {
783 
784  entry = (struct routing_entry_t *) MMEM_PTR(&n->entry);
785 
786  if( entry->bundle_number == bundle->bundle_num ) {
787  break;
788  }
789  }
790 
791  if( n == NULL ) {
792  LOG(LOGD_DTN, LOG_ROUTE, LOGL_ERR, "Bundle not in storage yet");
793  return;
794  }
795 
796  // Unset the IN_DELIVERY flag
797  entry->flags &= ~ROUTING_FLAG_IN_DELIVERY;
798 
799  // Unset the LOCAL flag
800  entry->flags &= ~ROUTING_FLAG_LOCAL;
801 
802  // Unblock the receiving service
803  delivery_unblock_service(bundlemem);
804 
805  // Free the bundle memory
806  bundle_decrement(bundlemem);
807 
808  /* We count ourselves as node as well, so list us as receiver of a bundle copy */
809  if (entry->send_to < ROUTING_NEI_MEM) {
810  rimeaddr_copy(&entry->neighbours[entry->send_to], &rimeaddr_node_addr);
811  entry->send_to++;
812  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle %lu sent to %u nodes", entry->bundle_number, entry->send_to);
813  } else if (entry->send_to >= ROUTING_NEI_MEM) {
814  // Here we can delete the bundle from storage, because it will not be routed anyway
815  LOG(LOGD_DTN, LOG_ROUTE, LOGL_DBG, "bundle %lu sent to max number of nodes, deleting", entry->bundle_number);
816 
817  /* Unsetting the forward flag will make routing_flooding_check_keep_bundle delete the bundle */
818  entry->flags &= ~ROUTING_FLAG_FORWARD;
819  }
820 
821  // Check remaining live of bundle
822  routing_flooding_check_keep_bundle(entry->bundle_number);
823 }
824 
825 /**
826  * \brief Routing persistent process
827  */
828 PROCESS_THREAD(routing_process, ev, data)
829 {
830  PROCESS_BEGIN();
831 
832  LOG(LOGD_DTN, LOG_ROUTE, LOGL_INF, "FLOOD ROUTE process in running");
833 
834  // Initialize memory used to store blacklisted neighbours
835  memb_init(&blacklist_mem);
836  list_init(blacklist_list);
837 
838  // Initialize memory used to store bundles for routing
839  memb_init(&routing_mem);
840  list_init(routing_list);
841 
842  while(1) {
843  PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
844 
846  }
847 
848  PROCESS_END();
849 }
850 
851 const struct routing_driver routing_flooding ={
852  "flood_route",
860 };
861