From 5db537fbabb93e81edea17910a6aaf709f69a216 Mon Sep 17 00:00:00 2001
From: Oleg Hahm <oleg@hobbykeller.org>
Date: Wed, 3 Feb 2016 16:46:28 +0100
Subject: [PATCH] ccn-lite: add shell function to remove FIB entry

---
 pkg/ccn-lite/ccn-lite-riot.h |  2 ++
 sys/shell/commands/sc_ccnl.c | 68 ++++++++++++++++++++++++++++--------
 2 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/pkg/ccn-lite/ccn-lite-riot.h b/pkg/ccn-lite/ccn-lite-riot.h
index 297eec46c9..11ac9d5372 100644
--- a/pkg/ccn-lite/ccn-lite-riot.h
+++ b/pkg/ccn-lite/ccn-lite-riot.h
@@ -204,6 +204,8 @@ int ccnl_wait_for_chunk(void *buf, size_t buf_len, uint64_t timeout);
 int ccnl_fib_add_entry(struct ccnl_relay_s *relay, struct ccnl_prefix_s *pfx,
                        struct ccnl_face_s *face);
 
+int ccnl_fib_rem_entry(struct ccnl_relay_s *relay, struct ccnl_prefix_s *pfx, struct ccnl_face_s *face);
+
 /**
  * @brief Prints the current CCN-Lite FIB
  *
diff --git a/sys/shell/commands/sc_ccnl.c b/sys/shell/commands/sc_ccnl.c
index 6ecb397341..1c1dde0828 100644
--- a/sys/shell/commands/sc_ccnl.c
+++ b/sys/shell/commands/sc_ccnl.c
@@ -145,15 +145,8 @@ int _ccnl_content(int argc, char **argv)
     return 0;
 }
 
-static int _intern_fib_add(char *pfx, char *addr_str)
+static struct ccnl_face_s *_intern_face_get(char *addr_str)
 {
-    int suite = CCNL_SUITE_NDNTLV;
-    struct ccnl_prefix_s *prefix = ccnl_URItoPrefix(pfx, suite, NULL, 0);
-    if (!prefix) {
-        puts("Error: prefix could not be created!");
-        return -1;
-    }
-
     /* initialize address with 0xFF for broadcast */
     size_t addr_len = MAX_ADDR_LEN;
     uint8_t relay_addr[MAX_ADDR_LEN];
@@ -162,7 +155,7 @@ static int _intern_fib_add(char *pfx, char *addr_str)
     addr_len = gnrc_netif_addr_from_str(relay_addr, sizeof(relay_addr), addr_str);
     if (addr_len == 0) {
         printf("Error: %s is not a valid link layer address\n", addr_str);
-        return -1;
+        return NULL;
     }
 
     sockunion sun;
@@ -173,6 +166,23 @@ static int _intern_fib_add(char *pfx, char *addr_str)
 
     /* TODO: set correct interface instead of always 0 */
     struct ccnl_face_s *fibface = ccnl_get_face_or_create(&ccnl_relay, 0, &sun.sa, sizeof(sun.linklayer));
+
+    return fibface;
+}
+
+static int _intern_fib_add(char *pfx, char *addr_str)
+{
+    int suite = CCNL_SUITE_NDNTLV;
+    struct ccnl_prefix_s *prefix = ccnl_URItoPrefix(pfx, suite, NULL, 0);
+    if (!prefix) {
+        puts("Error: prefix could not be created!");
+        return -1;
+    }
+
+    struct ccnl_face_s *fibface = _intern_face_get(addr_str);
+    if (fibface == NULL) {
+        return -1;
+    }
     fibface->flags |= CCNL_FACE_FLAGS_STATIC;
 
     if (ccnl_fib_add_entry(&ccnl_relay, prefix, fibface) != 0) {
@@ -228,10 +238,16 @@ int _ccnl_interest(int argc, char **argv)
 
 static void _ccnl_fib_usage(char *argv)
 {
-    printf("usage: %s [<URI> <content>]\n"
-            "%% %s                              (prints the current FIB)\n"
-            "%% %s /riot/peter/schmerzl RIOT\n",
-            argv, argv, argv);
+    printf("usage: %s [<action> <options>]\n"
+           "prints the FIB if called without parameters:\n"
+           "%% %s\n"
+           "<action> may be one of the following\n"
+           "  * \"add\" - adds an entry to the FIB, requires a prefix and a next-hop address, e.g.\n"
+           "            %s add /riot/peter/schmerzl ab:cd:ef:01:23:45:67:89\n"
+           "  * \"del\" - deletes an entry to the FIB, requires a prefix or a next-hop address, e.g.\n"
+           "            %s del /riot/peter/schmerzl\n"
+           "            %s del ab:cd:ef:01:23:45:67:89\n",
+            argv, argv, argv, argv, argv);
 }
 
 int _ccnl_fib(int argc, char **argv)
@@ -239,8 +255,30 @@ int _ccnl_fib(int argc, char **argv)
     if (argc < 2) {
         ccnl_fib_show(&ccnl_relay);
     }
-    else if (argc == 3) {
-        if (_intern_fib_add(argv[1], argv[2]) < 0) {
+    else if ((argc == 3) && (strncmp(argv[1], "del", 3) == 0)) {
+        int suite = CCNL_SUITE_NDNTLV;
+        if (strchr(argv[2], '/')) {
+            struct ccnl_prefix_s *prefix = ccnl_URItoPrefix(argv[2], suite, NULL, 0);
+            if (!prefix) {
+                puts("Error: prefix could not be created!");
+                return -1;
+            }
+            int res = ccnl_fib_rem_entry(&ccnl_relay, prefix, NULL);
+            free_prefix(prefix);
+            return res;
+        }
+        else {
+            struct ccnl_face_s *face = _intern_face_get(argv[2]);
+            if (face == NULL) {
+                printf("There is no face for address %s\n", argv[1]);
+                return -1;
+            }
+            int res = ccnl_fib_rem_entry(&ccnl_relay, NULL, face);
+            return res;
+        }
+    }
+    else if ((argc == 4) && (strncmp(argv[1], "add", 3) == 0)) {
+        if (_intern_fib_add(argv[2], argv[3]) < 0) {
             _ccnl_fib_usage(argv[0]);
             return -1;
         }
-- 
GitLab