aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/config/configuration.c
blob: b7e931e5302feaa8089cb293c3e33818fe3ff3fb (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
/*
 * 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.
 */

/**
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */

#ifndef _WIN32
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <ctype.h>
#include <hicn/hicn-light/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hicn/core/connection.h>
#include <hicn/core/connection_table.h>
#include <hicn/core/forwarder.h>
//#include <hicn/core/system.h>
#ifdef WITH_MAPME
#include <hicn/core/mapme.h>
#endif /* WITH_MAPME */

#include <hicn/core/listener.h>  //the listener list
#include <hicn/core/listener_table.h>
#include <hicn/ctrl/hicn-light.h>
//#include <hicn/utils/utils.h>
#include <hicn/utils/punting.h>
#include <hicn/util/log.h>
#include <hicn/face.h>
#include <hicn/util/slab.h>
#include <hicn/util/sstrncpy.h>

#include "configuration.h"

#define ETHERTYPE 0x0801
#define DEFAULT_COST 1
#define DEFAULT_PORT 1234
#define DEFAULT_LOGLEVEL "info"
#define DEFAULT_CS_CAPACITY 100000

#define msg_malloc_list(msg, N, seq_number)                           \
  do {                                                                \
    msg = malloc(sizeof((msg)->header) + N * sizeof((msg)->payload)); \
    (msg)->header.message_type = RESPONSE_LIGHT;                      \
    (msg)->header.length = (uint16_t)(N);                             \
    (msg)->header.seq_num = (seq_number);                             \
  } while (0);

typedef struct {
  char prefix[MAXSZ_HICN_PREFIX];
} prefix_key_t;

struct configuration_s {
  const char *fn_config;
  uint16_t port;
  uint16_t configuration_port;
  size_t cs_capacity;
  int loglevel;
  const char *logfile;
  int logfile_fd;
  bool daemon;

  kh_strategy_map_t *strategy_map;
  slab_t *prefix_keys;

  size_t n_suffixes_per_split;
  int_manifest_split_strategy_t split_strategy;
};

configuration_t *configuration_create() {
  configuration_t *config = malloc(sizeof(configuration_t));
  if (!config) return NULL;

  config->fn_config = NULL;
  config->port = PORT_NUMBER;
  config->configuration_port = 2001;  // TODO(eloparco): What is this?
  config->cs_capacity = DEFAULT_CS_CAPACITY;
  config->logfile = NULL;
  config->logfile_fd = -1;
#ifndef _WIN32
  config->daemon = false;
#else
  WSADATA wsaData = {0};
  WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
  configuration_set_loglevel(config, loglevel_from_str(DEFAULT_LOGLEVEL));
  config->strategy_map = kh_init_strategy_map();
  config->prefix_keys = slab_create(prefix_key_t, SLAB_INIT_SIZE);
  config->n_suffixes_per_split = DEFAULT_N_SUFFIXES_PER_SPLIT;
  config->split_strategy = DEFAULT_DISAGGREGATION_STRATEGY;

  return config;
}

void configuration_free(configuration_t *config) {
  assert(config);

  kh_destroy_strategy_map(config->strategy_map);
  slab_free(config->prefix_keys);

  free(config);
}

size_t configuration_get_cs_size(const configuration_t *config) {
  return config->cs_capacity;
}

void configuration_set_cs_size(configuration_t *config, size_t size) {
  config->cs_capacity = size;
}

const char *configuration_get_fn_config(const configuration_t *config) {
  return config->fn_config;
}

void configuration_set_fn_config(configuration_t *config,
                                 const char *fn_config) {
  config->fn_config = fn_config;
}

void configuration_set_suffixes_per_split(configuration_t *config,
                                          size_t n_suffixes_per_split) {
  config->n_suffixes_per_split = n_suffixes_per_split;
}

size_t configuration_get_suffixes_per_split(const configuration_t *config) {
  return config->n_suffixes_per_split;
}

void configuration_set_split_strategy(
    configuration_t *config, int_manifest_split_strategy_t split_strategy) {
  config->split_strategy = split_strategy;
}

int_manifest_split_strategy_t configuration_get_split_strategy(
    const configuration_t *config) {
  return config->split_strategy;
}

void configuration_set_port(configuration_t *config, uint16_t port) {
  config->port = port;
}

uint16_t configuration_get_port(const configuration_t *config) {
  return config->port;
}

void configuration_set_configuration_port(configuration_t *config,
                                          uint16_t configuration_port) {
  config->configuration_port = configuration_port;
}

uint16_t configuration_get_configuration_port(const configuration_t *config) {
  return config->configuration_port;
}

void configuration_set_loglevel(configuration_t *config, int loglevel) {
  config->loglevel = loglevel;
  log_conf.log_level = loglevel;
}

int configuration_get_loglevel(const configuration_t *config) {
  return config->loglevel;
}

void configuration_set_logfile(configuration_t *config, const char *logfile) {
  config->logfile = logfile;
  log_conf.log_file = fopen(logfile, "w");
  config->logfile_fd = fileno(log_conf.log_file);
}

const char *configuration_get_logfile(const configuration_t *config) {
  return config->logfile;
}

int configuration_get_logfile_fd(const configuration_t *config) {
  return config->logfile_fd;
}

void configuration_set_daemon(configuration_t *config, bool daemon) {
  config->daemon = daemon;
}

bool configuration_get_daemon(const configuration_t *config) {
  return config->daemon;
}

void configuration_set_strategy(configuration_t *config, const char *prefix,
                                strategy_type_t strategy_type) {
  int res;
  prefix_key_t *prefix_copy = slab_get(prefix_key_t, config->prefix_keys);
  strcpy_s(prefix_copy->prefix, sizeof(prefix_key_t), prefix);

  khiter_t k =
      kh_put_strategy_map(config->strategy_map, prefix_copy->prefix, &res);
  kh_value(config->strategy_map, k) = strategy_type;
}

strategy_type_t configuration_get_strategy(const configuration_t *config,
                                           const char *prefix) {
  khiter_t k = kh_get_strategy_map(config->strategy_map, prefix);
  if (k == kh_end(config->strategy_map)) return STRATEGY_TYPE_UNDEFINED;
  return kh_val(config->strategy_map, k);
}

void configuration_flush_log() {
  if (log_conf.log_file) fclose(log_conf.log_file);
}