1999-10-29 12:10:10 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Command-Line Interface
|
|
|
|
*
|
|
|
|
* (c) 1999 Martin Mares <mj@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/cli.h"
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
#include "conf/conf.h"
|
|
|
|
#include "lib/string.h"
|
1999-10-29 12:10:10 +00:00
|
|
|
|
|
|
|
pool *cli_pool;
|
|
|
|
|
1999-12-06 12:34:45 +00:00
|
|
|
static byte *
|
|
|
|
cli_alloc_out(cli *c, int size)
|
|
|
|
{
|
|
|
|
struct cli_out *o;
|
|
|
|
|
|
|
|
if (!(o = c->tx_write) || o->wpos + size > o->end)
|
|
|
|
{
|
|
|
|
if (!o && c->tx_buf)
|
|
|
|
o = c->tx_buf;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
o = mb_alloc(c->pool, sizeof(struct cli_out) + CLI_TX_BUF_SIZE);
|
|
|
|
if (c->tx_write)
|
|
|
|
c->tx_write->next = o;
|
|
|
|
else
|
|
|
|
c->tx_buf = o;
|
|
|
|
o->wpos = o->outpos = o->buf;
|
|
|
|
o->end = o->buf + CLI_TX_BUF_SIZE;
|
|
|
|
}
|
|
|
|
c->tx_write = o;
|
|
|
|
if (!c->tx_pos)
|
|
|
|
c->tx_pos = o;
|
2000-04-27 22:35:08 +00:00
|
|
|
o->next = NULL;
|
1999-12-06 12:34:45 +00:00
|
|
|
}
|
|
|
|
o->wpos += size;
|
|
|
|
return o->wpos - size;
|
|
|
|
}
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
void
|
|
|
|
cli_printf(cli *c, int code, char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
byte buf[1024];
|
1999-10-31 15:43:44 +00:00
|
|
|
int cd = code;
|
|
|
|
int size, cnt;
|
1999-10-29 12:10:10 +00:00
|
|
|
|
|
|
|
va_start(args, msg);
|
1999-10-31 15:43:44 +00:00
|
|
|
if (cd < 0)
|
|
|
|
{
|
|
|
|
cd = -cd;
|
|
|
|
if (cd == c->last_reply)
|
|
|
|
size = bsprintf(buf, " ");
|
|
|
|
else
|
|
|
|
size = bsprintf(buf, "%04d-", cd);
|
|
|
|
}
|
1999-10-29 12:10:10 +00:00
|
|
|
else
|
1999-10-31 15:43:44 +00:00
|
|
|
size = bsprintf(buf, "%04d ", cd);
|
|
|
|
c->last_reply = cd;
|
|
|
|
cnt = bvsnprintf(buf+size, sizeof(buf)-size-1, msg, args);
|
|
|
|
if (cnt < 0)
|
|
|
|
{
|
|
|
|
cli_printf(c, code < 0 ? -8000 : 8000, "<line overflow>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size += cnt;
|
1999-10-29 12:10:10 +00:00
|
|
|
buf[size++] = '\n';
|
1999-12-06 12:34:45 +00:00
|
|
|
memcpy(cli_alloc_out(c, size), buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cli_copy_message(cli *c)
|
|
|
|
{
|
|
|
|
byte *p, *q;
|
|
|
|
unsigned int cnt = 2;
|
|
|
|
|
|
|
|
if (c->ring_overflow)
|
1999-10-29 12:10:10 +00:00
|
|
|
{
|
1999-12-06 12:34:45 +00:00
|
|
|
byte buf[64];
|
|
|
|
int n = bsprintf(buf, "<%d messages lost>\n", c->ring_overflow);
|
|
|
|
c->ring_overflow = 0;
|
|
|
|
memcpy(cli_alloc_out(c, n), buf, n);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
1999-12-06 12:34:45 +00:00
|
|
|
p = c->ring_read;
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
cnt++;
|
|
|
|
p++;
|
|
|
|
if (p == c->ring_end)
|
|
|
|
p = c->ring_buf;
|
|
|
|
ASSERT(p != c->ring_write);
|
|
|
|
}
|
|
|
|
c->async_msg_size += cnt;
|
|
|
|
q = cli_alloc_out(c, cnt);
|
|
|
|
*q++ = '+';
|
|
|
|
p = c->ring_read;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*q = *p++;
|
|
|
|
if (p == c->ring_end)
|
|
|
|
p = c->ring_buf;
|
|
|
|
}
|
|
|
|
while (*q++);
|
|
|
|
c->ring_read = p;
|
|
|
|
q[-1] = '\n';
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
1999-10-31 15:43:44 +00:00
|
|
|
static void
|
|
|
|
cli_hello(cli *c)
|
|
|
|
{
|
|
|
|
cli_printf(c, 1, "BIRD " BIRD_VERSION " ready.");
|
|
|
|
c->cont = NULL;
|
|
|
|
}
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
static void
|
|
|
|
cli_free_out(cli *c)
|
|
|
|
{
|
|
|
|
struct cli_out *o, *p;
|
|
|
|
|
|
|
|
if (o = c->tx_buf)
|
|
|
|
{
|
|
|
|
o->wpos = o->outpos = o->buf;
|
|
|
|
while (p = o->next)
|
|
|
|
{
|
|
|
|
o->next = p->next;
|
|
|
|
mb_free(p);
|
|
|
|
}
|
|
|
|
}
|
2000-04-27 22:35:08 +00:00
|
|
|
c->tx_write = c->tx_pos = NULL;
|
1999-12-06 12:34:45 +00:00
|
|
|
c->async_msg_size = 0;
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
static byte *cli_rh_pos;
|
|
|
|
static unsigned int cli_rh_len;
|
|
|
|
static int cli_rh_trick_flag;
|
|
|
|
struct cli *this_cli;
|
|
|
|
|
|
|
|
static int
|
|
|
|
cli_cmd_read_hook(byte *buf, unsigned int max)
|
|
|
|
{
|
|
|
|
if (!cli_rh_trick_flag)
|
|
|
|
{
|
|
|
|
cli_rh_trick_flag = 1;
|
|
|
|
buf[0] = '!';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (max > cli_rh_len)
|
|
|
|
max = cli_rh_len;
|
|
|
|
memcpy(buf, cli_rh_pos, max);
|
|
|
|
cli_rh_pos += max;
|
|
|
|
cli_rh_len -= max;
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cli_command(struct cli *c)
|
|
|
|
{
|
|
|
|
struct config f;
|
|
|
|
int res;
|
|
|
|
|
1999-11-30 14:04:09 +00:00
|
|
|
bzero(&f, sizeof(f));
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
f.mem = c->parser_pool;
|
|
|
|
cf_read_hook = cli_cmd_read_hook;
|
|
|
|
cli_rh_pos = c->rx_buf;
|
|
|
|
cli_rh_len = strlen(c->rx_buf);
|
|
|
|
cli_rh_trick_flag = 0;
|
|
|
|
this_cli = c;
|
|
|
|
lp_flush(c->parser_pool);
|
1999-12-01 15:07:56 +00:00
|
|
|
res = cli_parse(&f);
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
if (!res)
|
|
|
|
cli_printf(c, 9001, f.err_msg);
|
|
|
|
}
|
|
|
|
|
2000-04-27 22:35:08 +00:00
|
|
|
static void
|
1999-10-29 12:10:10 +00:00
|
|
|
cli_event(void *data)
|
|
|
|
{
|
|
|
|
cli *c = data;
|
|
|
|
int err;
|
|
|
|
|
1999-12-06 12:34:45 +00:00
|
|
|
while (c->ring_read != c->ring_write &&
|
|
|
|
c->async_msg_size < CLI_MAX_ASYNC_QUEUE)
|
|
|
|
cli_copy_message(c);
|
|
|
|
|
1999-10-31 15:43:44 +00:00
|
|
|
if (c->tx_pos)
|
|
|
|
;
|
|
|
|
else if (c->cont)
|
|
|
|
c->cont(c);
|
|
|
|
else
|
1999-10-29 12:10:10 +00:00
|
|
|
{
|
1999-10-31 15:43:44 +00:00
|
|
|
err = cli_get_command(c);
|
|
|
|
if (!err)
|
2000-04-27 22:35:08 +00:00
|
|
|
return;
|
1999-10-31 15:43:44 +00:00
|
|
|
if (err < 0)
|
|
|
|
cli_printf(c, 9000, "Command too long");
|
|
|
|
else
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
cli_command(c);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
1999-10-31 15:43:44 +00:00
|
|
|
if (cli_write(c))
|
|
|
|
{
|
|
|
|
cli_free_out(c);
|
2000-04-27 22:35:08 +00:00
|
|
|
ev_schedule(c->event);
|
1999-10-31 15:43:44 +00:00
|
|
|
}
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cli *
|
|
|
|
cli_new(void *priv)
|
|
|
|
{
|
|
|
|
pool *p = rp_new(cli_pool, "CLI");
|
|
|
|
cli *c = mb_alloc(p, sizeof(cli));
|
|
|
|
|
1999-12-06 12:34:45 +00:00
|
|
|
bzero(c, sizeof(cli));
|
1999-10-29 12:10:10 +00:00
|
|
|
c->pool = p;
|
|
|
|
c->priv = priv;
|
|
|
|
c->event = ev_new(p);
|
|
|
|
c->event->hook = cli_event;
|
|
|
|
c->event->data = c;
|
1999-10-31 15:43:44 +00:00
|
|
|
c->cont = cli_hello;
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
c->parser_pool = lp_new(c->pool, 4096);
|
1999-12-06 12:34:45 +00:00
|
|
|
c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE);
|
1999-10-31 15:43:44 +00:00
|
|
|
ev_schedule(c->event);
|
1999-10-29 12:10:10 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_kick(cli *c)
|
|
|
|
{
|
1999-10-31 15:43:44 +00:00
|
|
|
if (!c->cont && !c->tx_pos)
|
|
|
|
ev_schedule(c->event);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_written(cli *c)
|
|
|
|
{
|
|
|
|
cli_free_out(c);
|
1999-10-31 15:43:44 +00:00
|
|
|
ev_schedule(c->event);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
1999-12-06 12:34:45 +00:00
|
|
|
static list cli_log_hooks;
|
|
|
|
static int cli_log_inited;
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_set_log_echo(cli *c, unsigned int mask, unsigned int size)
|
|
|
|
{
|
|
|
|
if (c->ring_buf)
|
|
|
|
{
|
|
|
|
mb_free(c->ring_buf);
|
|
|
|
c->ring_buf = c->ring_end = c->ring_read = c->ring_write = NULL;
|
|
|
|
rem_node(&c->n);
|
|
|
|
}
|
|
|
|
c->log_mask = mask;
|
|
|
|
if (mask && size)
|
|
|
|
{
|
|
|
|
c->ring_buf = mb_alloc(c->pool, size);
|
|
|
|
c->ring_end = c->ring_buf + size;
|
|
|
|
c->ring_read = c->ring_write = c->ring_buf;
|
|
|
|
add_tail(&cli_log_hooks, &c->n);
|
|
|
|
c->log_threshold = size / 8;
|
|
|
|
}
|
|
|
|
c->ring_overflow = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_echo(unsigned int class, byte *msg)
|
|
|
|
{
|
|
|
|
unsigned len, free, i, l;
|
|
|
|
cli *c;
|
|
|
|
byte *m;
|
|
|
|
|
|
|
|
if (!cli_log_inited || EMPTY_LIST(cli_log_hooks))
|
|
|
|
return;
|
|
|
|
len = strlen(msg) + 1;
|
|
|
|
WALK_LIST(c, cli_log_hooks)
|
|
|
|
{
|
|
|
|
if (!(c->log_mask & (1 << class)))
|
|
|
|
continue;
|
|
|
|
if (c->ring_read <= c->ring_write)
|
|
|
|
free = (c->ring_end - c->ring_buf) - (c->ring_write - c->ring_read + 1);
|
|
|
|
else
|
|
|
|
free = c->ring_read - c->ring_write - 1;
|
|
|
|
if (len > free ||
|
|
|
|
free < c->log_threshold && class < (unsigned) L_INFO[0])
|
|
|
|
{
|
|
|
|
c->ring_overflow++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c->ring_read == c->ring_write)
|
|
|
|
ev_schedule(c->event);
|
|
|
|
m = msg;
|
|
|
|
l = len;
|
|
|
|
while (l)
|
|
|
|
{
|
|
|
|
if (c->ring_read <= c->ring_write)
|
|
|
|
i = c->ring_end - c->ring_write;
|
|
|
|
else
|
|
|
|
i = c->ring_read - c->ring_write;
|
|
|
|
if (i > l)
|
|
|
|
i = l;
|
|
|
|
memcpy(c->ring_write, m, i);
|
|
|
|
m += i;
|
|
|
|
l -= i;
|
|
|
|
c->ring_write += i;
|
|
|
|
if (c->ring_write == c->ring_end)
|
|
|
|
c->ring_write = c->ring_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
void
|
|
|
|
cli_free(cli *c)
|
|
|
|
{
|
1999-12-06 12:34:45 +00:00
|
|
|
cli_set_log_echo(c, 0, 0);
|
1999-11-17 12:00:21 +00:00
|
|
|
if (c->cleanup)
|
|
|
|
c->cleanup(c);
|
1999-10-29 12:10:10 +00:00
|
|
|
rfree(c->pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_init(void)
|
|
|
|
{
|
|
|
|
cli_pool = rp_new(&root_pool, "CLI");
|
1999-12-06 12:34:45 +00:00
|
|
|
init_list(&cli_log_hooks);
|
|
|
|
cli_log_inited = 1;
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|