3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2005, Andreas Tonnesen(andreto@olsr.org)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
42 #include "olsr_host_switch.h"
43 #include "olsr_types.h"
45 #include "link_rules.h"
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <sys/types.h>
58 #define TOK_BUF_SIZE 500
59 static char tok_buf[TOK_BUF_SIZE];
61 #define MAX_OLSRD_ARGS 10
62 static char olsrd_path[FILENAME_MAX];
65 get_next_token(const char *src, char *dst, size_t buflen)
70 /* Skip leading spaces */
71 while (src[j] == ' ' && src[j] != 0) {
77 while ((src[i] != ' ') && (src[i] != 0) && (i < ((int)buflen - 1))) {
84 //printf("Extracted token: %s\n", dst);
89 ohs_set_olsrd_path(const char *path)
91 strscpy(olsrd_path, path, sizeof(olsrd_path));
97 ohs_cmd_olsrd(const char *args __attribute__ ((unused)))
99 printf("olsrd command not available in windows version\nStart instances manually\n");
104 ohs_cmd_olsrd(const char *args)
106 const char *olsrd_args[MAX_OLSRD_ARGS];
107 struct in_addr iaddr;
109 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
111 if (!strlen(tok_buf))
114 /* Start olsrd instance */
115 if (!strncmp(tok_buf, "start", strlen("start"))) {
118 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
120 if (!strlen(tok_buf))
123 if (!inet_aton(tok_buf, &iaddr)) {
124 printf("Invalid IP %s\n", tok_buf);
128 olsrd_args[argc++] = olsrd_path;
130 if (1) { /* config file is set */
131 olsrd_args[argc++] = "-f";
132 olsrd_args[argc++] = "./olsrd.emu.conf";
134 olsrd_args[argc++] = "-hemu";
135 olsrd_args[argc++] = tok_buf;
137 olsrd_args[argc++] = "-d";
138 olsrd_args[argc++] = "0";
139 olsrd_args[argc++] = "-nofork";
140 olsrd_args[argc] = NULL;
142 printf("Executing: %s", olsrd_path);
143 for (i = 0; i < argc; i++)
144 printf(" %s", olsrd_args[i]);
150 if (execve(olsrd_path, (char *const *)olsrd_args, NULL) < 0) {
151 printf("Error executing olsrd: %s\n", strerror(errno));
155 /* Stop olsrd instance */
156 else if (!strncmp(tok_buf, "stop", strlen("stop"))) {
157 struct ohs_connection *oc;
159 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
161 if (!strlen(tok_buf))
164 if (!inet_aton(tok_buf, &iaddr)) {
165 printf("Invalid IP %s\n", tok_buf);
169 oc = get_client_by_addr((union olsr_ip_addr *)&iaddr.s_addr);
172 printf("No such client: %s\n", tok_buf);
175 ohs_delete_connection(oc);
179 /* Set olsrd binary path */
180 else if (!strncmp(tok_buf, "setb", strlen("setb"))) {
183 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
185 if (!strlen(tok_buf))
188 if (stat(tok_buf, &sbuf) < 0) {
189 printf("Error setting binary \"%s\": %s\n", tok_buf, strerror(errno));
193 if ((sbuf.st_mode & S_IFDIR) || !(sbuf.st_mode & S_IXUSR)) {
194 printf("Error setting binary \"%s\": Not a regular execuatble file!\n", tok_buf);
198 printf("New olsrd binary path:\"%s\"\n", tok_buf);
199 ohs_set_olsrd_path(tok_buf);
205 else if (!strncmp(tok_buf, "seta", strlen("seta"))) {
206 printf("Error - NOT IMPLEMENTED YET\n");
210 else if (!strncmp(tok_buf, "show", strlen("show"))) {
211 printf("olsrd command settings:\n\tBinary path: %s\n\tArguments : \n", olsrd_path);
216 printf("Usage: olsrd [start|stop|show|setb|seta] [IP|path|args]\n");
222 ohs_cmd_link(const char *args)
224 uint8_t bi = 0, wildc_src = 0, wildc_dst = 0;
225 struct ohs_connection *src, *dst;
226 struct in_addr iaddr;
228 struct ohs_ip_link *my_link, *inv_link;
230 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
232 if (!strlen(tok_buf)) {
235 if (!strncmp(tok_buf, "bi", strlen("bi"))) {
237 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
239 if (!strlen(tok_buf)) {
244 if (tok_buf[0] == '*') {
248 if (!inet_aton(tok_buf, &iaddr)) {
249 printf("Invalid src IP %s\n", tok_buf);
253 src = get_client_by_addr((union olsr_ip_addr *)&iaddr.s_addr);
256 printf("No such client: %s!\n", tok_buf);
261 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
263 if (!strlen(tok_buf)) {
267 if (tok_buf[0] == '*') {
271 if (!inet_aton(tok_buf, &iaddr)) {
272 printf("Invalid src IP %s\n", tok_buf);
276 dst = get_client_by_addr((union olsr_ip_addr *)&iaddr.s_addr);
278 printf("No such client: %s!\n", tok_buf);
283 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
285 if (!strlen(tok_buf)) {
289 /* No use for bi if both src and dst are widcards */
290 if (wildc_src && wildc_dst) {
294 qual = atoi(tok_buf);
296 if (qual < 0 || qual > 100) {
297 printf("Link quality out of range(0-100)\n");
303 struct ipaddr_str srcaddrstr, dstaddrstr;
306 my_link = get_link(src, &dst->ip_addr);
307 inv_link = bi ? get_link(dst, &src->ip_addr) : NULL;
309 /* Remove link entry */
311 remove_link(src, my_link);
314 remove_link(dst, inv_link);
318 /* Create new link */
319 my_link = add_link(src, dst);
322 my_link->quality = qual;
326 /* Create new link */
327 inv_link = add_link(dst, src);
329 inv_link->quality = qual;
332 printf("%s %sdirectional link(s) %s %c=> %s quality %d\n", (qual == 100) ? "Removing" : "Setting", bi ? "bi" : "uni",
333 olsr_ip_to_string(&srcaddrstr, &src->ip_addr), bi ? '<' : '=', olsr_ip_to_string(&dstaddrstr, &dst->ip_addr), qual);
341 dst = wildc_dst ? ohs_conns : dst;
342 src = wildc_src ? src->next : NULL;
347 printf("link <bi> srcIP dstIP [0-100]");
352 ohs_cmd_list(const char *args)
354 struct ohs_connection *oc = ohs_conns;
356 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
358 if (!strlen(tok_buf) || !strncmp(tok_buf, "clients", strlen("clients"))) {
359 printf("All connected clients:\n");
362 struct ipaddr_str addrstr;
363 printf("\t%s - Rx: %d Tx: %d LinkCnt: %d\n", olsr_ip_to_string(&addrstr, &oc->ip_addr), oc->rx, oc->tx, oc->linkcnt);
366 } else if (!strncmp(tok_buf, "links", strlen("links"))) {
367 printf("All configured links:\n");
369 struct ohs_ip_link *links = oc->links;
371 struct ipaddr_str addrstr, dststr;
372 printf("\t%s => %s Quality: %d\n", olsr_ip_to_string(&addrstr, &oc->ip_addr), olsr_ip_to_string(&dststr, &links->dst),
380 printf("list [clients|links]");
387 ohs_cmd_help(const char *args)
391 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
393 if (!strlen(tok_buf)) {
394 printf("Olsrd host switch version %s\n", OHS_VERSION);
395 printf("Available commands:\n");
397 for (i = 0; ohs_commands[i].cmd; i++) {
398 if (ohs_commands[i].helptext_brief)
399 printf("\t%s - %s\n", ohs_commands[i].cmd, ohs_commands[i].helptext_brief);
401 printf("\nType 'help cmd' for help on a specific command\n");
403 for (i = 0; ohs_commands[i].cmd; i++) {
404 if (!strncmp(tok_buf, ohs_commands[i].cmd, strlen(ohs_commands[i].cmd))) {
405 printf("Usage: %s\nDescription:\n%s\n", ohs_commands[i].syntax, ohs_commands[i].helptext_long);
410 printf("Usage: help <command>\n");
417 ohs_cmd_log(const char *args)
421 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
423 if (strlen(tok_buf) && ((set = !strncmp(tok_buf, "set", strlen("set"))) || !strncmp(tok_buf, "unset", strlen("unset")))) {
424 uint32_t new_bit = 0;
426 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
428 if (!strlen(tok_buf))
431 if (!strncmp(tok_buf, "CON", strlen("CON")))
432 new_bit = LOG_CONNECT;
433 else if (!strncmp(tok_buf, "FOR", strlen("FOR")))
434 new_bit = LOG_FORWARD;
435 else if (!strncmp(tok_buf, "LIN", strlen("LIN")))
446 printf("%s log bit: 0x%08x, new log: 0x%08x\n", set ? "Setting" : "Removing", new_bit, logbits);
452 printf("Log: (0x%08x) ", logbits);
453 if (logbits & LOG_CONNECT)
455 if (logbits & LOG_FORWARD)
457 if (logbits & LOG_LINK)
465 printf("Usage: log <[set|unset] [CONNECT|FORWARD|LINK]>\n");
471 ohs_cmd_exit(const char *args __attribute__ ((unused)))
474 printf("Exitting... bye-bye!\n");
480 ohs_parse_command(void)
482 static char cmd_line[500];
483 static int cmd_len = 0;
491 KEY_EVENT_RECORD *KeyEventRec;
492 #endif /* defined _WIN32 */
495 if (!ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &InRec, sizeof(InRec), &Read)) {
496 fprintf(stderr, "ReadConsoleInput failed: %s\n", strerror(GetLastError()));
500 if (InRec.EventType != KEY_EVENT)
503 KeyEventRec = &InRec.Event.KeyEvent;
505 if (!KeyEventRec->bKeyDown)
508 c = KeyEventRec->uChar.AsciiChar;
528 if (c != '\n' && c != '\r' && cmd_len < (int)sizeof(cmd_line) - 1)
529 cmd_line[cmd_len++] = (char)c;
532 #else /* defined _WIN32 */
533 if (fgets(cmd_line, sizeof(cmd_line), stdin) == NULL) {
537 for (cmd_len = 0; cmd_line[cmd_len] != 0 && cmd_line[cmd_len] != '\n'; cmd_len++);
538 #endif /* defined _WIN32 */
541 cmd_line[cmd_len] = 0;
544 args = cmd_line + get_next_token(cmd_line, cmd_token, sizeof(cmd_token));
546 for (i = 0; ohs_commands[i].cmd != NULL; i++) {
547 if (strcmp(cmd_token, ohs_commands[i].cmd) == 0) {
548 if (ohs_commands[i].cmd_cb != NULL)
549 ohs_commands[i].cmd_cb(args);
552 printf("No action registered on cmd %s!\n", cmd_token);
558 if (ohs_commands[i].cmd == NULL)
559 printf("%s: no such cmd!\n", cmd_token);
569 * indent-tabs-mode: nil