3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2005, Andreas Tønnesen(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.
40 * $Id: ohs_cmd.c,v 1.8 2005/05/31 14:04:21 kattemat Exp $
43 #include "olsr_host_switch.h"
44 #include "olsr_types.h"
46 #include "link_rules.h"
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
54 #define ARG_BUF_SIZE 500
55 static char arg_buf[ARG_BUF_SIZE];
57 #define TOK_BUF_SIZE 500
58 static char tok_buf[TOK_BUF_SIZE];
61 get_arg_buf(FILE *handle, char *buf, size_t size)
66 while(((c = fgetc(handle)) != '\n') &&
75 printf("Args: %s\n", buf);
79 get_next_token(char *src, char *dst, size_t buflen)
84 /* Skip leading spaces */
85 while(src[j] == ' ' && src[j] != 0)
92 while((src[i] != ' ') && (src[i] != 0) && (i < (buflen - 1)))
99 printf("Extracted token: %s\n", dst);
104 ohs_cmd_link(FILE *handle, char *args)
107 struct ohs_connection *src, *dst;
108 struct in_addr iaddr;
110 struct ohs_ip_link *link, *inv_link;
112 if(strlen(args) < strlen("bi"))
115 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
120 if(!strncmp(tok_buf, "bi", strlen("bi")))
123 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
129 if(!inet_aton(tok_buf, &iaddr))
131 printf("Invalid src IP %s\n", tok_buf);
135 src = get_client_by_addr((union olsr_ip_addr *)&iaddr.s_addr);
139 printf("No such client: %s!\n", tok_buf);
143 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
148 if(!inet_aton(tok_buf, &iaddr))
150 printf("Invalid src IP %s\n", tok_buf);
154 dst = get_client_by_addr((union olsr_ip_addr *)&iaddr.s_addr);
158 printf("No such client: %s!\n", tok_buf);
162 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
167 qual = atoi(tok_buf);
169 if(qual < 0 || qual > 100)
171 printf("Link quality out of range(0-100)\n");
175 printf("%s %sdirectional link %s %c=> %s quality %d\n",
176 (qual == 100) ? "Removing" : "Setting", bi ? "bi" : "uni",
177 olsr_ip_to_string(&src->ip_addr), bi ? '<' : '=',
178 olsr_ip_to_string(&dst->ip_addr), qual);
180 link = get_link(src, &dst->ip_addr);
182 inv_link = get_link(dst, &src->ip_addr);
188 /* Remove link entry */
190 remove_link(src, link);
192 remove_link(dst, inv_link);
198 /* Create new link */
199 link = malloc(sizeof(link));
201 OHS_OUT_OF_MEMORY("New link");
203 link->next = src->links;
205 COPY_IP(&link->dst, &dst->ip_addr);
209 link->quality = qual;
215 /* Create new link */
216 inv_link = malloc(sizeof(link));
218 OHS_OUT_OF_MEMORY("New link");
220 inv_link->next = dst->links;
221 dst->links = inv_link;
222 COPY_IP(&inv_link->dst, &src->ip_addr);
225 inv_link->quality = qual;
231 printf("link <bi> srcIP dstIP [0-100]");
236 ohs_cmd_list(FILE *handle, char *args)
238 struct ohs_connection *oc = ohs_conns;
240 printf("All connected clients:\n");
244 printf("\t%s - Rx: %d Tx: %d LinkCnt: %d\n", olsr_ip_to_string(&oc->ip_addr),
245 oc->rx, oc->tx, oc->linkcnt);
253 ohs_cmd_help(FILE *handle, char *args)
257 printf("Olsrd host switch version %s\n", OHS_VERSION);
258 printf("Available commands:\n");
260 for(i = 0; ohs_commands[i].cmd; i++)
262 if(ohs_commands[i].helptext_brief)
263 printf("\t%s - %s\n",
265 ohs_commands[i].helptext_brief);
267 printf("\nType 'help cmd' for help on a specific command(NIY)\n");
272 ohs_cmd_log(FILE *handle, char *args)
276 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
278 if(strlen(tok_buf) &&
279 ((set = !strncmp(tok_buf, "set", strlen("set"))) ||
280 !strncmp(tok_buf, "unset", strlen("unset"))))
282 olsr_u32_t new_bit = 0;
284 args += get_next_token(args, tok_buf, TOK_BUF_SIZE);
290 if(!strncmp(tok_buf, "CON", strlen("CON")))
291 new_bit = LOG_CONNECT;
292 else if(!strncmp(tok_buf, "FOR", strlen("FOR")))
293 new_bit = LOG_FORWARD;
294 else if(!strncmp(tok_buf, "LIN", strlen("LIN")))
305 printf("%s log bit: 0x%08x, new log: 0x%08x\n", set ? "Setting" : "Removing",
314 printf("Log: (0x%08x) ", logbits);
315 if(logbits & LOG_CONNECT)
317 if(logbits & LOG_FORWARD)
319 if(logbits & LOG_LINK)
327 printf("Usage: log <[set|unset] [CONNECT|FORWARD|LINK]>\n");
333 ohs_cmd_exit(FILE *handle, char *args)
336 printf("Exitting... bye-bye!\n");
348 ohs_parse_command(FILE *handle)
350 char input_data[100];
353 fscanf(handle, "%s", input_data);
355 get_arg_buf(handle, arg_buf, ARG_BUF_SIZE);
357 printf("ohs_parse_command: %s\n", input_data);
358 for(i = 0; ohs_commands[i].cmd; i++)
360 if(!strcmp(input_data, ohs_commands[i].cmd))
362 if(ohs_commands[i].cmd_cb)
364 ohs_commands[i].cmd_cb(handle, arg_buf);
368 printf("No action registered on cmd %s!\n", input_data);
374 if(!ohs_commands[i].cmd)
376 printf("%s: no such cmd!\n", input_data);