Blame view

RIOT/sys/shell/commands/sc_ccnl.c 7.89 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  /*
   * Copyright (C) 2015, 2016  INRIA.
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     sys_shell_commands
   * @{
   *
   * @file
   * @brief       Shell commands to interact with the CCN-Lite stack
   *
   * @author      Oliver Hahm <oliver.hahm@inria.fr>
   *
   * @}
   */
  
  #include "random.h"
  #include "sched.h"
  #include "net/gnrc/netif.h"
  #include "ccn-lite-riot.h"
  #include "ccnl-pkt-ndntlv.h"
  
  #define BUF_SIZE (64)
  
  #define MAX_ADDR_LEN            (8U)
  
  static unsigned char _int_buf[BUF_SIZE];
  static unsigned char _cont_buf[BUF_SIZE];
  
  static const char *_default_content = "Start the RIOT!";
  static unsigned char _out[CCNL_MAX_PACKET_SIZE];
  
  /* usage for open command */
  static void _open_usage(void)
  {
      puts("ccnl <interface>");
  }
  
  int _ccnl_open(int argc, char **argv)
  {
      /* check if already running */
      if (ccnl_relay.ifcount >= CCNL_MAX_INTERFACES) {
          puts("Already opened max. number of interfaces for CCN!");
          return -1;
      }
  
      /* check if parameter is given */
      if (argc != 2) {
          _open_usage();
          return -1;
      }
  
      /* check if given number is a valid netif PID */
      int pid = atoi(argv[1]);
      if (!gnrc_netif_exist(pid)) {
          printf("%i is not a valid interface!\n", pid);
          return -1;
      }
  
      ccnl_start();
  
      /* set the relay's PID, configure the interface to interface to use CCN
       * nettype */
      if (ccnl_open_netif(pid, GNRC_NETTYPE_CCN) < 0) {
          puts("Error registering at network interface!");
          return -1;
      }
  
      return 0;
  }
  
  
  static void _content_usage(char *argv)
  {
      printf("usage: %s <URI> [content]\n"
              "%% %s /riot/peter/schmerzl             (default content)\n"
              "%% %s /riot/peter/schmerzl RIOT\n",
              argv, argv, argv);
  }
  
  int _ccnl_content(int argc, char **argv)
  {
      if (argc < 2) {
          _content_usage(argv[0]);
          return -1;
      }
  
      int arg_len;
      char *body = (char*) _default_content;
      char buf[BUF_SIZE+1]; /* add one extra space to fit trailing '\0' */
  
      if (argc > 2) {
          unsigned pos = 0;
          for (int i = 2; (i < argc) && (pos < BUF_SIZE); ++i) {
              arg_len = strlen(argv[i]);
              if ((pos + arg_len) > BUF_SIZE) {
                  arg_len = BUF_SIZE - pos;
              }
              strncpy(&buf[pos], argv[i], arg_len);
              pos += arg_len;
              /* increment pos _after_ adding ' ' */
              buf[pos++] = ' ';
          }
          /* decrement pos _before_ to overwrite last ' ' with '\0' */
          buf[--pos] = '\0';
          body = buf;
      }
      arg_len = strlen(body);
  
      struct ccnl_prefix_s *prefix = ccnl_URItoPrefix(argv[1], CCNL_SUITE_NDNTLV, NULL, NULL);
      int offs = CCNL_MAX_PACKET_SIZE;
      arg_len = ccnl_ndntlv_prependContent(prefix, (unsigned char*) body, arg_len, NULL, NULL, &offs, _out);
  
      free_prefix(prefix);
  
      unsigned char *olddata;
      unsigned char *data = olddata = _out + offs;
  
      int len;
      unsigned typ;
  
      if (ccnl_ndntlv_dehead(&data, &arg_len, (int*) &typ, &len) ||
          typ != NDN_TLV_Data) {
          return -1;
      }
  
      struct ccnl_content_s *c = 0;
      struct ccnl_pkt_s *pk = ccnl_ndntlv_bytes2pkt(typ, olddata, &data, &arg_len);
      c = ccnl_content_new(&ccnl_relay, &pk);
      ccnl_content_add2cache(&ccnl_relay, c);
      c->flags |= CCNL_CONTENT_FLAGS_STATIC;
  
      return 0;
  }
  
  static struct ccnl_face_s *_intern_face_get(char *addr_str)
  {
      /* initialize address with 0xFF for broadcast */
      uint8_t relay_addr[MAX_ADDR_LEN];
      memset(relay_addr, UINT8_MAX, MAX_ADDR_LEN);
      size_t 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 NULL;
      }
  
      sockunion sun;
      sun.sa.sa_family = AF_PACKET;
      memcpy(&(sun.linklayer.sll_addr), relay_addr, addr_len);
      sun.linklayer.sll_halen = addr_len;
      sun.linklayer.sll_protocol = htons(ETHERTYPE_NDN);
  
      /* 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) {
          printf("Error adding (%s : %s) to the FIB\n", pfx, addr_str);
          return -1;
      }
  
      return 0;
  }
  
  static void _interest_usage(char *arg)
  {
      printf("usage: %s <URI> [relay]\n"
              "%% %s /riot/peter/schmerzl                     (classic lookup)\n",
              arg, arg);
  }
  
  int _ccnl_interest(int argc, char **argv)
  {
      if (argc < 2) {
          _interest_usage(argv[0]);
          return -1;
      }
  
      if (argc > 2) {
          if (_intern_fib_add(argv[1], argv[2]) < 0) {
              _interest_usage(argv[0]);
              return -1;
          }
      }
  
      memset(_int_buf, '\0', BUF_SIZE);
      memset(_cont_buf, '\0', BUF_SIZE);
  
      gnrc_netreg_entry_t _ne =
          GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
                                     sched_active_pid);
      /* register for content chunks */
      gnrc_netreg_register(GNRC_NETTYPE_CCN_CHUNK, &_ne);
  
      struct ccnl_prefix_s *prefix = ccnl_URItoPrefix(argv[1], CCNL_SUITE_NDNTLV, NULL, 0);
      ccnl_send_interest(prefix, _int_buf, BUF_SIZE);
      int res = 0;
      if (ccnl_wait_for_chunk(_cont_buf, BUF_SIZE, 0) > 0) {
          printf("Content received: %s\n", _cont_buf);
      }
      else {
          printf("Timeout! No content received in response to the Interest for %s.\n", argv[1]);
          res = -1;
      }
      free_prefix(prefix);
      gnrc_netreg_unregister(GNRC_NETTYPE_CCN_CHUNK, &_ne);
  
      return res;
  }
  
  static void _ccnl_fib_usage(char *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)
  {
      if (argc < 2) {
          ccnl_fib_show(&ccnl_relay);
      }
      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;
          }
      }
      else {
          _ccnl_fib_usage(argv[0]);
          return -1;
      }
      return 0;
  }