aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/libhicnctrl/src/hicnctrl.c
blob: fd028f64ad19ce041c52f5edfef6bc46c46b6bbc (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright (c) 2021-2022 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file cli.c
 * \brief Command line interface
 */
#include <ctype.h>  // isalpha isalnum
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>  // getopt

#include <hicn/ctrl.h>
#include <hicn/util/ip_address.h>
#include <hicn/util/log.h>
#include <hicn/util/token.h>
#include <hicn/validation.h>

#include <hicn/ctrl/parse.h>

#define die(LABEL, MESSAGE) \
  do {                      \
    printf(MESSAGE "\n");   \
    goto ERR_##LABEL;       \
  } while (0)

void usage_header() { fprintf(stderr, "Usage:\n"); }

void usage_face_create(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr,
          "%s -f TYPE LOCAL_ADDRESS LOCAL_PORT REMOTE_ADDRESS REMOTE_PORT "
          "[INTERFACE_NAME]\n",
          prog);
  if (verbose)
    fprintf(stderr, "    Create a face on specified address and port.\n");
}

void usage_face_delete(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -df ID\n", prog);
  // fprintf(stderr, "%s -df NAME\n", prog);
  fprintf(stderr,
          "%s -df TYPE LOCAL_ADDRESS LOCAL_PORT REMOTE_ADDRESS REMOTE_PORT "
          "[INTERFACE_NAME]\n",
          prog);
  if (verbose) fprintf(stderr, "    Delete a face...\n");
}

void usage_face_list(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -F\n", prog);
  if (verbose) fprintf(stderr, "    List all faces.\n");
}

void usage_face(const char *prog, bool header, bool verbose) {
  usage_face_create(prog, header, verbose);
  usage_face_delete(prog, header, verbose);
  usage_face_list(prog, header, verbose);
}

void usage_route_create(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -r FACE_ID PREFIX [COST]\n", prog);
  // fprintf(stderr, "%s -r [FACE_ID|NAME] PREFIX [COST]\n", prog);
  if (verbose) fprintf(stderr, "    Create a route...\n");
}

void usage_route_delete(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -dr FACE_ID PREFIX\n", prog);
  // fprintf(stderr, "%s -dr [FACE_ID|NAME] PREFIX\n", prog);
  if (verbose) fprintf(stderr, "    Delete a route...\n");
}

void usage_route_list(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -R\n", prog);
  if (verbose) fprintf(stderr, "    List all routes.\n");
}

void usage_route(const char *prog, bool header, bool verbose) {
  usage_route_create(prog, header, verbose);
  usage_route_delete(prog, header, verbose);
  usage_route_list(prog, header, verbose);
}

void usage_forwarding_strategy_create(const char *prog, bool header,
                                      bool verbose) {
  if (header) usage_header();
}
void usage_forwarding_strategy_delete(const char *prog, bool header,
                                      bool verbose) {
  if (header) usage_header();
}

void usage_forwarding_strategy_list(const char *prog, bool header,
                                    bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -S\n", prog);
  if (verbose)
    fprintf(stderr, "    List all availble forwarding strategies.\n");
}

void usage_forwarding_strategy(const char *prog, bool header, bool verbose) {
  usage_forwarding_strategy_create(prog, header, verbose);
  usage_forwarding_strategy_delete(prog, header, verbose);
  usage_forwarding_strategy_list(prog, header, verbose);
}

void usage_listener_create(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -l NAME TYPE LOCAL_ADDRESS LOCAL_PORT [INTERFACE_NAME]\n",
          prog);
  if (verbose)
    fprintf(stderr, "    Create a listener on specified address and port.\n");
}

void usage_listener_delete(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -dl ID\n", prog);
  fprintf(stderr, "%s -dl NAME\n", prog);
  fprintf(stderr, "%s -dl TYPE LOCAL_ADDRESS LOCAL_PORT [INTERFACE_NAME]\n",
          prog);
  if (verbose) fprintf(stderr, "    Delete a listener...\n");
}

void usage_listener_list(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -L\n", prog);
  if (verbose) fprintf(stderr, "    List all listeners.\n");
}

void usage_listener(const char *prog, bool header, bool verbose) {
  usage_listener_create(prog, header, verbose);
  usage_listener_delete(prog, header, verbose);
  usage_listener_list(prog, header, verbose);
}
void usage_connection_create(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr,
          "%s -c NAME TYPE LOCAL_ADDRESS LOCAL_PORT REMOTE_ADDRESS REMOTE_PORT "
          "[INTERFACE_NAME]\n",
          prog);
  if (verbose)
    fprintf(stderr, "    Create a connection on specified address and port.\n");
}

void usage_connection_delete(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -dc ID\n", prog);
  fprintf(stderr, "%s -dc NAME\n", prog);
  fprintf(stderr,
          "%s -dc TYPE LOCAL_ADDRESS LOCAL_PORT REMOTE_ADDRESS REMOTE_PORT "
          "[INTERFACE_NAME]\n",
          prog);
  if (verbose) fprintf(stderr, "    Delete a connection...\n");
}

void usage_connection_list(const char *prog, bool header, bool verbose) {
  if (header) usage_header();
  fprintf(stderr, "%s -C\n", prog);
  if (verbose) fprintf(stderr, "    List all connections.\n");
}

void usage_connection(const char *prog, bool header, bool verbose) {
  usage_connection_create(prog, header, verbose);
  usage_connection_delete(prog, header, verbose);
  usage_connection_list(prog, header, verbose);
}

void usage(const char *prog) {
  fprintf(stderr,
          "Usage: %s [ -z forwarder (hicnlight | vpp) ] [ [-d] [-f|-l|-c|-r] "
          "PARAMETERS | [-F|-L|-C|-R] ]\n",
          prog);
  fprintf(stderr, "\n");
  fprintf(stderr, "High-level commands\n");
  fprintf(stderr, "\n");
  usage_face(prog, false, true);
  usage_route(prog, false, true);
  usage_forwarding_strategy(prog, false, true);
  fprintf(stderr, "\n");
  fprintf(stderr, "Low level commands (hicn-light specific)\n");
  fprintf(stderr, "\n");
  usage_listener(prog, false, true);
  usage_connection(prog, false, true);
}

/*
 * We only allow settings commands and object types once, with the default
 * action being set to CREATE
 */
#define set_command(ACTION, OBJECT_TYPE)                             \
  do {                                                               \
    if ((ACTION) != ACTION_UNDEFINED) {                              \
      if (command->action != ACTION_CREATE) goto USAGE;              \
      command->action = (ACTION);                                    \
    }                                                                \
    if ((OBJECT_TYPE) != OBJECT_TYPE_UNDEFINED) {                    \
      if (command->object_type != OBJECT_TYPE_UNDEFINED) goto USAGE; \
      command->object_type = (OBJECT_TYPE);                          \
    }                                                                \
  } while (0)

int parse_options(int argc, char *argv[], hc_command_t *command,
                  forwarder_type_t *forwarder) {
  command->object_type = OBJECT_TYPE_UNDEFINED;
  command->action = ACTION_CREATE;
  int opt;

  while ((opt = getopt(argc, argv, "cCdDfFlLrRsShz:")) != -1) {
    switch (opt) {
      case 'z':
        *forwarder = forwarder_type_from_str(optarg);
        if (*forwarder == FORWARDER_TYPE_UNDEFINED) goto USAGE;
        break;
      case 'd':
        set_command(ACTION_DELETE, OBJECT_TYPE_UNDEFINED);
        break;
      case 's':
        set_command(ACTION_SUBSCRIBE, OBJECT_TYPE_UNDEFINED);
        break;
      case 'f':
        set_command(ACTION_UNDEFINED, OBJECT_TYPE_FACE);
        break;
      case 'c':
        set_command(ACTION_UNDEFINED, OBJECT_TYPE_CONNECTION);
        break;
      case 'l':
        set_command(ACTION_UNDEFINED, OBJECT_TYPE_LISTENER);
        break;
      case 'r':
        set_command(ACTION_UNDEFINED, OBJECT_TYPE_ROUTE);
        break;
      case 'F':
        set_command(ACTION_LIST, OBJECT_TYPE_FACE);
        break;
      case 'L':
        set_command(ACTION_LIST, OBJECT_TYPE_LISTENER);
        break;
      case 'C':
        set_command(ACTION_LIST, OBJECT_TYPE_CONNECTION);
        break;
      case 'R':
        set_command(ACTION_LIST, OBJECT_TYPE_ROUTE);
        break;
      case 'S':
        set_command(ACTION_LIST, OBJECT_TYPE_STRATEGY);
        break;
      case 'D':
        log_conf.log_level = LOG_DEBUG;
        break;
      default: /* "h" */
        usage(argv[0]);
        exit(EXIT_SUCCESS);
    }
  }

  // XXX The rest could be made a single parse function

  /* A default action is always defined, let's verify we have an object type,
   * unless we are subscribing to notifications. In that case, we can monitor
   * all objects.
   * XXX handle later
   */
  if ((command->object_type == OBJECT_TYPE_UNDEFINED) &&
      (command->action != ACTION_SUBSCRIBE)) {
    ERROR("Missing object specification");
    goto USAGE;
  }

  /* Check the adequation between the number of parameters and the command */
  size_t nparams = argc - optind;
  if (nparams > 0) {
    if (command->action == ACTION_LIST) command->action = ACTION_GET;
  } else {
    if ((command->action != ACTION_LIST) &&
        (command->action != ACTION_SUBSCRIBE))
      goto USAGE;
  }

  /*
   * This checks is important even with 0 parameters as it checks whether the
   * command exists.
   */
  if (command->action != ACTION_SUBSCRIBE) {
    const command_parser_t *parser =
        command_search(command->action, command->object_type, nparams);
    if (!parser) {
      ERROR("Could not find parser for command '%s %s'",
            action_str(command->action), object_type_str(command->object_type));
      return -1;
    }

    if (nparams > 0) {
      if (parse_getopt_args(parser, argc - optind, argv + optind, command) <
          0) {
        ERROR("Error parsing command arguments");
        goto USAGE;
      }
    }
  }

  return 0;

USAGE:
  usage(argv[0]);
  exit(EXIT_FAILURE);
}

int main(int argc, char *argv[]) {
  int rc = 1;
  hc_command_t command = {0};
  char buf[MAXSZ_HC_OBJECT];

  log_conf.log_level = LOG_INFO;

  forwarder_type_t forwarder = FORWARDER_TYPE_HICNLIGHT;

  if (parse_options(argc, argv, &command, &forwarder) < 0)
    die(OPTIONS, "Bad arguments");

  hc_sock_t *s = hc_sock_create(forwarder, /* url= */ NULL);
  if (!s) die(SOCKET, "Error creating socket.");

  if (hc_sock_connect(s) < 0)
    die(CONNECT, "Error connecting to the forwarder.");

  hc_data_t *data = NULL;

  rc = hc_execute(s, command.action, command.object_type, &command.object,
                  &data);

  if (rc < 0) {
    switch (rc) {
      case INPUT_ERROR:
        ERROR("Wrong input parameters");
        break;
      case UNSUPPORTED_CMD_ERROR:
        ERROR("Unsupported command");
        break;
      default:
        ERROR("Error executing command");
        break;
    }
    goto ERR_COMMAND;
  }

  if (!data) goto ERR_QUERY;

  if (!hc_data_get_result(data)) goto ERR_DATA;

  size_t size = hc_data_get_size(data);
  if (size > 0) {
    printf("Success: got %ld %s\n", size, object_type_str(command.object_type));
  } else {
    printf("Success.\n");
  }

  if (command.action == ACTION_LIST) {
    hc_data_foreach(data, obj, {
      rc = hc_object_snprintf(buf, MAXSZ_HC_OBJECT, command.object_type, obj);
      if (rc < 0)
        WARN("Display error");
      else if (rc >= MAXSZ_HC_OBJECT)
        WARN("Output truncated");
      else
        printf("%s\n", buf);
    });
  }

  hc_data_free(data);
  hc_sock_free(s);
  return EXIT_SUCCESS;

ERR_DATA:
  hc_data_free(data);
ERR_QUERY:
ERR_COMMAND:
ERR_CONNECT:
  hc_sock_free(s);
ERR_SOCKET:
ERR_OPTIONS:
  printf("Error.\n");
  return EXIT_FAILURE;
}