aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/config/configuration_file.c
blob: 8649e01439ec3c0792e292836b99e117010801be (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
/*
 * Copyright (c) 2021 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.
 */

#ifndef _WIN32
#include <unistd.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#include <hicn/ctrl/hicn-light.h>
#include <hicn/config/configuration_file.h>
#include <hicn/util/sstrncpy.h>

#include "commands.h"
#include <hicn/ctrl/parse.h>

#define BUFFERLEN 2048

static char *_trim(char *str) {
  char *end;

  // Trim leading space
  while (isspace((unsigned char)*str)) str++;

  if (*str == 0)  // All spaces?
    return str;

  // Trim trailing space
  end = str + strnlen_s(str, BUFFERLEN) - 1;
  while (end > str && isspace((unsigned char)*end)) end--;

  // Write new null terminator character
  end[1] = '\0';

  return str;
}

bool configuration_file_process(forwarder_t *forwarder, const char *filename) {
  assert(forwarder);
  assert(filename);

  int linesRead = 0;
  FILE *f = fopen(filename, "r");
  if (!f) {
    ERROR("Could not open configuration file %s: (%d) %s", filename, errno,
          strerror(errno));
    goto ERR_OPEN;
  }
  DEBUG("Opening configuration file %s", filename);

  char buffer[BUFFERLEN];
  bool success = true;

#if 0
  // TODO(eloparco): We could use a fake socket since we only need the vft
  hc_sock_t *s = hc_sock_create(FORWARDER_TYPE_HICNLIGHT, NULL);
  if (!s) {
    ERROR("Could not create socket");
    goto ERR_SOCK;
  }
#else
  hc_sock_initialize_module(NULL);
#endif

  while (success && fgets(buffer, BUFFERLEN, f) != NULL) {
    linesRead++;

    char *cmd = _trim(buffer);
    if (strnlen_s(cmd, BUFFERLEN) <= 0) continue;
    if (cmd[0] == '#') continue;

    INFO("Processing command: %s", cmd);
    hc_command_t command = {};
    if (parse(cmd, &command) < 0) {
      ERROR("Error parsing command : '%s'", cmd);
      continue;
    }

    /* Serialize request into message */
    // hc_msg_t msg;
    uint8_t msg[1024];
    ssize_t msg_len = hc_light_command_serialize(
        command.action, command.object_type, &command.object, msg);
    switch (msg_len) {
      case -1:
      case -2:
        ERROR("Command '%s' not supported", cmd);
        continue;
      case -3:
        ERROR("Error during command serialization '%s'", cmd);
        continue;
      default:
        break;
    }

    size_t _unused;
    command_process(forwarder, (uint8_t *)msg, CONNECTION_ID_UNDEFINED,
                    &_unused);
  }

#if 0
  hc_sock_free(s);
#endif

  if (ferror(f)) {
    ERROR("Error on input file %s line %d: (%d) %s", filename, linesRead, errno,
          strerror(errno));
    goto ERR_READ;
  }
  fclose(f);
  return true;

#if 0
ERR_SOCK:
  hc_sock_free(s);
#endif
ERR_READ:
  fclose(f);
ERR_OPEN:
  return false;
}