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
|
/*
* 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.
*/
#include <limits.h> // LONG_MAX, LONG_MIN
#include <signal.h>
#include <hicn/ctrl.h>
#ifndef _WIN32
#include <getopt.h>
#endif
#include "color.h"
#include <hicn/ctrl/parse.h>
#include <hicn/ctrl/hicn-light.h>
#include <hicn/util/log.h>
#include <hicn/util/sstrncpy.h>
#define PORT 9695
/*
* Duplicated from hicn_light_ng_api.c while is only available as a module in
* libhicnctrl
*/
const char *command_type_str[] = {
#define _(l, u) [COMMAND_TYPE_##u] = STRINGIZE(u),
foreach_command_type
#undef _
};
static struct option longFormOptions[] = {{"help", no_argument, 0, 'h'},
{"server", required_argument, 0, 'S'},
{"port", required_argument, 0, 'P'},
{0, 0, 0, 0}};
static void usage(char *prog) {
printf("%s: portable hICN forwarder\n", prog);
printf("\n");
printf("Usage: %s COMMAND [PARAMETERS]\n", prog);
printf("\n");
printf(" %s -h This help screen.\n", prog);
printf(" %s help Obtain a list of available commands.\n", prog);
printf("\n");
}
static bool stop = false;
void signal_handler(int sig) {
fprintf(stderr, "Received ^C... quitting !\n");
stop = true;
}
#if 0
int hc_active_interface_snprintf(char *buf, size_t size,
hc_event_active_interface_update_t *event) {
int rc;
char *pos = buf;
rc = ip_prefix_snprintf(pos, size, &event->prefix);
if ((rc < 0) || (rc >= size)) return rc;
pos += rc;
size -= rc;
for (netdevice_type_t type = NETDEVICE_TYPE_UNDEFINED + 1;
type < NETDEVICE_TYPE_N; type++) {
if (!netdevice_flags_has(event->interface_type, type)) continue;
rc = snprintf(pos, size, " %s", netdevice_type_str(type));
if ((rc < 0) || (rc >= size)) return pos - buf + rc;
pos += rc;
size -= rc;
}
return pos - buf;
}
// XXX hc_object_snprintf
void hc_subscription_display(command_type_t command_type,
const uint8_t *buffer) {
char buf[65535];
switch (command_type) {
case COMMAND_TYPE_CONNECTION_ADD:
case COMMAND_TYPE_CONNECTION_REMOVE:
case COMMAND_TYPE_CONNECTION_UPDATE:
hc_connection_snprintf(buf, sizeof(buf), (hc_connection_t *)buffer);
break;
case COMMAND_TYPE_ACTIVE_INTERFACE_UPDATE:
hc_active_interface_snprintf(
buf, sizeof(buf), (hc_event_active_interface_update_t *)buffer);
break;
case COMMAND_TYPE_ROUTE_LIST:
hc_route_snprintf(buf, sizeof(buf), (hc_route_t *)buffer);
break;
default:
INFO("Unknown event received");
return;
}
INFO("%s %s", command_type_str(command_type), buf);
}
#endif
int main(int argc, char *const *argv) {
log_conf.log_level = LOG_INFO;
// Handle termination signal
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler;
sigaction(SIGINT, &sa, NULL);
/* Parse commandline */
char *server_ip = NULL;
uint16_t server_port = 0;
for (;;) {
// getopt_long stores the option index here.
int optind = 0;
int c = getopt_long(argc, argv, "hS:P:", longFormOptions, &optind);
if (c == -1) break;
switch (c) {
case 'S':
server_ip = optarg;
break;
case 'P': {
char *endptr;
long val = strtol(optarg, &endptr, 10);
errno = 0; /* To distinguish success/failure after call */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
(errno != 0 && val == 0)) {
perror("strtol");
exit(EXIT_FAILURE);
}
if (endptr == optarg) {
fprintf(stderr, "No digits were found.\n");
exit(EXIT_FAILURE);
}
if (*endptr != '\0') {
fprintf(stderr, "Spurious characters after number: %s.\n", endptr);
exit(EXIT_FAILURE);
}
if ((val < 1) || (val > 65535)) {
fprintf(stderr, "Invalid port number: %ld.\n", val);
exit(EXIT_FAILURE);
}
server_port = val;
break;
}
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "Invalid argument.\n");
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
/* Parse */
char *param = argv[optind];
for (; optind < argc - 1; optind++) {
char *arg = argv[optind];
arg[strlen(arg)] = ' ';
}
if (!param) {
usage(argv[0]);
goto ERR_PARAM;
}
if (strncmp(param, "help", 4) == 0) {
if (help(param) < 0) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
hc_command_t command = {};
if (parse(param, &command) < 0) {
fprintf(stderr, "Error parsing command : '%s'\n", param);
goto ERR_PARSE;
}
hc_sock_t *s;
if (server_ip) {
if (server_port == 0) server_port = PORT;
#define BUFSIZE 255
char url[BUFSIZE];
snprintf(url, BUFSIZE, "tcp://%s:%d/", server_ip, server_port);
s = hc_sock_create(FORWARDER_TYPE_HICNLIGHT, url);
} else {
s = hc_sock_create(FORWARDER_TYPE_HICNLIGHT, NULL);
}
if (!s) {
fprintf(stderr, "Could not create socket.\n");
goto ERR_SOCKET;
}
if (hc_sock_connect(s) < 0) {
fprintf(stderr, "Could not establish connection to forwarder.\n");
goto ERR_CONNECT;
}
if (!IS_VALID_OBJECT_TYPE(command.object_type) ||
!IS_VALID_ACTION(command.action)) {
fprintf(stderr, "Unsupported command");
goto ERR_PARAM;
}
int rc = UNSUPPORTED_CMD_ERROR;
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;
if (command.action == ACTION_LIST) {
char buf[MAXSZ_HC_OBJECT];
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_PARSE:
ERR_PARAM:
ERROR("Error");
return EXIT_FAILURE;
}
|