aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/command_line/controller/hicnLightControl_main.c
blob: e6a97c4e2a9ad7a3a3f9adb326ab97fa2c599655 (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
/*
 * Copyright (c) 2017-2019 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.
 */

#include <hicn/hicn-light/config.h>
#include <hicn/utils/utils.h>


#ifndef _WIN32
#include <arpa/inet.h>
#include <getopt.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#include <arpa/inet.h>
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <parc/assert/parc_Assert.h>
#include <string.h>

#include <parc/security/parc_IdentityFile.h>
#include <parc/security/parc_Security.h>

#include <parc/algol/parc_ArrayList.h>
#include <parc/algol/parc_List.h>
#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_SafeMemory.h>

#include <hicn/core/dispatcher.h>
#include <hicn/core/forwarder.h>

#include <errno.h>
#include <hicn/config/controlRoot.h>
#include <hicn/config/controlState.h>

#include <hicn/utils/commands.h>



size_t commandOutputLen = 0;  // preserve the number of structs composing
                              // payload in case on not interactive call.

// REMINDER: when a new_command is added, the following array has to be updated
// with the sizeof(new_command). It allows to allocate the buffer for receiving
// the payload of the DAEMON RESPONSE after the header has beed read. Each
// command identifier (typedef enum command_id) corresponds to a position in the
// following array.
static int payloadLengthController[LAST_COMMAND_VALUE] = {
    sizeof(add_listener_command),
    sizeof(add_connection_command),
    sizeof(list_connections_command),  // needed when get response from FWD
    sizeof(add_route_command),
    sizeof(list_routes_command),  // needed when get response from FWD
    sizeof(remove_connection_command),
    sizeof(remove_listener_command),
    sizeof(remove_route_command),
    sizeof(cache_store_command),
    sizeof(cache_serve_command),
    0,  // cache clear
    sizeof(set_strategy_command),
    sizeof(set_wldr_command),
    sizeof(add_punting_command),
    sizeof(list_listeners_command),  // needed when get response from FWD
    sizeof(mapme_activator_command),
    sizeof(mapme_activator_command),
    sizeof(mapme_timing_command),
    sizeof(mapme_timing_command),
    sizeof(mapme_send_update_command),
    sizeof(connection_set_admin_state_command),
#ifdef WITH_POLICY
    sizeof(add_policy_command),
    sizeof(list_policies_command),
    sizeof(remove_policy_command),
    sizeof(update_connection_command),
    sizeof(connection_set_priority_command),
    sizeof(connection_set_tags_command),
#endif
};

typedef struct controller_main_state {
  ControlState *controlState;
} ControlMainState;

static void _printRed(const char *output) {
#ifndef _WIN32
  printf("\033[0;31m%s", output);
#else
  HANDLE hConsole = NULL;
  WORD currentConsoleAttr;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  if (GetConsoleScreenBufferInfo(hConsole, &csbi))
    currentConsoleAttr = csbi.wAttributes;
  SetConsoleTextAttribute(hConsole, 4);
  printf("%s", output);
  SetConsoleTextAttribute(hConsole, currentConsoleAttr);
#endif
}

static void _printWhite(const char *output) {
#ifndef _WIN32
  printf("\033[0m%s", output);
#else
  HANDLE hConsole = NULL;
  WORD currentConsoleAttr;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  if (GetConsoleScreenBufferInfo(hConsole, &csbi))
    currentConsoleAttr = csbi.wAttributes;
  SetConsoleTextAttribute(hConsole, 7);
  printf("%s", output);
  SetConsoleTextAttribute(hConsole, currentConsoleAttr);
#endif
}

static void _displayForwarderLogo(void) {
  _printRed("   ____ ___      _       ");
  _printWhite("  __    _               __ _        __   __\n");
  _printRed("  / __// _ \\    (_)___  ");
  _printWhite("  / /   (_)____ ___ ____/ /(_)___ _ / /  / /_\n");
  _printRed(" / _/ / // /_  / // _ \\ ");
  _printWhite(" / _ \\ / // __// _ \\___/ // // _ `// _ \\/ __/\n");
  _printRed("/_/  /____/(_)/_/ \\___/ ");
  _printWhite("/_//_//_/ \\__//_//_/  /_//_/ \\_, //_//_/\\__/\n");
  _printWhite(
      "                                                    /___/            "
      "\n");
  printf("\n");
}

static void _displayUsage(char *programName) {
  printf("Usage: %s -h\n", programName);
  printf(
      "hicn-light is the 1.0 source, which runs on each end system and as a "
      "software source\n");
  printf(
      "on intermediate systems.  controller is the program to configure the "
      "source, daemon.\n");
  printf("\n");
  printf("Options:\n");
  printf("-h              = This help screen\n");
  printf(
      "commands        = configuration line to send to hicn-light (use 'help' "
      "for list)\n");
  printf("\n");
}

static int _parseArgs(int argc, char *argv[], char **server_ip,
                      uint16_t *server_port, PARCList *commandList){
  static struct option longFormOptions[] = {
      {"help", no_argument, 0, 'h'},
      {"server", required_argument, 0, 'S'},
      {"port", required_argument, 0, 'P'},
      {0, 0, 0, 0}};

  int c;

  while (1) {
    // getopt_long stores the option index here.
    int optionIndex = 0;

    c = getopt_long(argc, argv, "hS:P:", longFormOptions, &optionIndex);

    // Detect the end of the options.
    if (c == -1) {
      break;
    }

    switch (c) {
      case 'S':
        {
          *server_ip = optarg;
          struct sockaddr_in sa;
          int result = inet_pton(AF_INET, *server_ip, &(sa.sin_addr));
          //inet_pton() returns 1 on success
          if(result != 1){
            return 0;
          }
          break;
        }
      case 'P':
        {
          char * port_str = optarg;
          if(utils_IsNumber(port_str)){
            *server_port = (uint16_t) strtol(port_str, NULL, 10);
          } else {
            return 0;
          }
          break;
        }
      case 'h':
      default:
        return 0;
    }
  }

  if (optind < argc) {
    while (optind < argc) {
      parcList_Add(commandList, argv[optind]);
      optind++;
    }
  }

  return 1;
}

struct iovec *_writeAndReadMessage(ControlState *state, struct iovec *msg) {
  parcAssertNotNull(msg, "Parameter msg must be non-null");
  int sockfd = controlState_GetSockfd(state);

  // check if request has a payload
  if (((header_control_message *)msg[0].iov_base)->length >
      0) {  // command with payload
    // write header + payload (compatibility issue: two write needed instead of
    // the writev)
#ifndef _WIN32
    if (write(sockfd, msg[0].iov_base, (unsigned int)msg[0].iov_len) < 0 ||
        write(sockfd, msg[1].iov_base, (unsigned int)msg[1].iov_len) < 0) {
#else
    if (send(sockfd, msg[0].iov_base, (int)msg[0].iov_len, 0) == SOCKET_ERROR ||
        send(sockfd, msg[1].iov_base, (int)msg[1].iov_len, 0) == SOCKET_ERROR) {
#endif
      printf("\nError while sending the Message: cannot write on socket \n");
      exit(EXIT_FAILURE);
    }
    parcMemory_Deallocate(&msg[1].iov_base);
  } else {  // command without payload, e.g. 'list'
            // write header only
#ifndef _WIN32
    if (write(sockfd, msg[0].iov_base, msg[0].iov_len) < 0) {
#else
    int result = send(sockfd, msg[0].iov_base, (int)msg[0].iov_len, 0);
    if (result == SOCKET_ERROR) {
#endif
      printf("\nError while sending the Message: cannot write on socket \n");
      exit(EXIT_FAILURE);
    }
  }
  parcMemory_Deallocate(&msg[0].iov_base);

  // ======= RECEIVE =======

  header_control_message *headerResponse =
      (header_control_message *)parcMemory_AllocateAndClear(
          sizeof(header_control_message));
  if (recv(sockfd, (char *)headerResponse, sizeof(header_control_message), 0) <
      0) {
    printf("\nError in Receiving the Message \n");
    exit(EXIT_FAILURE);
  }

  if (headerResponse->messageType < RESPONSE_LIGHT ||
      headerResponse->messageType >= LAST_MSG_TYPE_VALUE) {
    char *checkFinMsg = parcMemory_Reallocate(headerResponse, 32);
#ifndef _WIN32
    if (recv(sockfd, checkFinMsg, sizeof(checkFinMsg),
             MSG_PEEK | MSG_DONTWAIT) == 0) {
#else
    if (recv(sockfd, checkFinMsg, sizeof(checkFinMsg), MSG_PEEK) == 0) {
#endif
      // if recv returns zero, that means the connection has been closed:
      close(sockfd);
      printf("\nConnection terminated by the Daemon. Exiting... \n");
      exit(EXIT_SUCCESS);
    } else {
      printf("\nError: Unrecognized message type received \n");
      exit(EXIT_FAILURE);
    }
  }

  void *payloadResponse = NULL;

  if ((commandOutputLen = headerResponse->length) > 0) {
    payloadResponse = parcMemory_AllocateAndClear(
        payloadLengthController[headerResponse->commandID] *
        headerResponse->length);

    if (recv(sockfd, payloadResponse,
             payloadLengthController[headerResponse->commandID] *
                 headerResponse->length,
             0) < 0) {
      printf("\nError in Receiving the Message \n");
      exit(EXIT_FAILURE);
    }
  }

  struct iovec *response =
      parcMemory_AllocateAndClear(sizeof(struct iovec) * 2);

  response[0].iov_base = headerResponse;
  response[0].iov_len = sizeof(header_control_message);
  response[1].iov_base = payloadResponse;
  response[1].iov_len = payloadLengthController[headerResponse->commandID] *
                        headerResponse->length;

  return response;
}

int main(int argc, char *argv[]) {
  _displayForwarderLogo();

#ifdef _WIN32
  WSADATA wsaData;
  WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif

  if (argc == 2 && strcmp("-h", argv[1]) == 0) {
    _displayUsage(argv[0]);
    exit(EXIT_SUCCESS);
  }

  PARCList *commands =
      parcList(parcArrayList_Create(NULL), PARCArrayListAsPARCList);

  char *server_ip = SRV_CTRL_IP;
  uint16_t server_port = SRV_CTRL_PORT;
  if (!_parseArgs(argc, argv, &server_ip,
            &server_port, commands)) {
    _displayUsage(argv[0]);
    parcList_Release(&commands);
    exit(EXIT_FAILURE);
  }

  ControlMainState mainState;
  mainState.controlState =
      controlState_Create(&mainState, _writeAndReadMessage, true,
                          server_ip, server_port);
  if (mainState.controlState == NULL) {
      exit(EXIT_FAILURE);
  }

  controlState_RegisterCommand(mainState.controlState,
                               controlRoot_HelpCreate(mainState.controlState));
  controlState_RegisterCommand(mainState.controlState,
                               controlRoot_Create(mainState.controlState));

  if (parcList_Size(commands) > 0) {
    controlState_SetInteractiveFlag(mainState.controlState, false);
    char output[8192];
    controlState_DispatchCommand(mainState.controlState, commands, output, sizeof(output));
    printf("%s", output);
    char **commandOutputMain =
        controlState_GetCommandOutput(mainState.controlState);
    if (commandOutputMain != NULL && commandOutputLen > 0) {
#if 0
      for (size_t j = 0; j < commandOutputLen; j++) {
        printf("Output %zu: %s \n", j, commandOutputMain[j]);
      }
#endif
      controlState_ReleaseCommandOutput(mainState.controlState,
                                        commandOutputMain, commandOutputLen);
    }
    // release

  } else {
    controlState_Interactive(mainState.controlState);
  }

  parcList_Release(&commands);

  controlState_Destroy(&mainState.controlState);
#ifdef _WIN32
  WSACleanup();
#endif
  return EXIT_SUCCESS;
}