1999-10-29 12:10:10 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Command-Line Interface
|
|
|
|
*
|
2017-07-18 21:51:09 +00:00
|
|
|
* (c) 1999--2017 Martin Mares <mj@ucw.cz>
|
1999-10-29 12:10:10 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BIRD_CLI_H_
|
|
|
|
#define _BIRD_CLI_H_
|
|
|
|
|
|
|
|
#include "lib/resource.h"
|
2017-07-18 21:51:09 +00:00
|
|
|
#include "lib/coroutine.h"
|
1999-10-29 12:10:10 +00:00
|
|
|
#include "lib/event.h"
|
2017-07-18 21:51:09 +00:00
|
|
|
#include "lib/socket.h"
|
1999-10-29 12:10:10 +00:00
|
|
|
|
|
|
|
#define CLI_RX_BUF_SIZE 4096
|
|
|
|
#define CLI_TX_BUF_SIZE 4096
|
1999-12-06 12:34:45 +00:00
|
|
|
#define CLI_MAX_ASYNC_QUEUE 4096
|
1999-10-29 12:10:10 +00:00
|
|
|
|
2011-07-03 17:43:30 +00:00
|
|
|
#define CLI_MSG_SIZE 500
|
|
|
|
#define CLI_LINE_SIZE 512
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
struct cli_out {
|
|
|
|
struct cli_out *next;
|
|
|
|
byte *wpos, *outpos, *end;
|
|
|
|
byte buf[0];
|
|
|
|
};
|
|
|
|
|
2017-07-18 21:51:09 +00:00
|
|
|
enum cli_state {
|
|
|
|
CLI_STATE_INIT,
|
|
|
|
CLI_STATE_RUN,
|
|
|
|
CLI_STATE_WAIT_RX,
|
|
|
|
CLI_STATE_WAIT_TX,
|
|
|
|
CLI_STATE_YIELD,
|
2018-09-13 11:24:49 +00:00
|
|
|
CLI_STATE_SLEEP,
|
2017-07-18 21:51:09 +00:00
|
|
|
};
|
2017-07-18 20:21:27 +00:00
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
typedef struct cli {
|
1999-12-06 12:34:45 +00:00
|
|
|
node n; /* Node in list of all log hooks */
|
1999-10-29 12:10:10 +00:00
|
|
|
pool *pool;
|
2017-07-18 21:51:09 +00:00
|
|
|
coroutine *coro;
|
|
|
|
enum cli_state state;
|
|
|
|
int restricted; /* CLI is restricted to read-only commands */
|
|
|
|
|
|
|
|
/* I/O */
|
|
|
|
sock *socket;
|
|
|
|
byte *rx_buf, *rx_pos, *rx_aux;
|
1999-10-29 12:10:10 +00:00
|
|
|
struct cli_out *tx_buf, *tx_pos, *tx_write;
|
|
|
|
event *event;
|
2017-07-18 21:51:09 +00:00
|
|
|
|
|
|
|
/* Continuation mechanism */
|
1999-10-31 15:43:44 +00:00
|
|
|
void (*cont)(struct cli *c);
|
1999-11-17 12:00:21 +00:00
|
|
|
void (*cleanup)(struct cli *c);
|
1999-10-31 15:43:44 +00:00
|
|
|
void *rover; /* Private to continuation routine */
|
|
|
|
int last_reply;
|
2017-07-18 21:51:09 +00:00
|
|
|
|
|
|
|
/* Pools */
|
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
|
|
|
struct linpool *parser_pool; /* Pool used during parsing */
|
2017-05-15 10:10:51 +00:00
|
|
|
struct linpool *show_pool; /* Pool used during route show */
|
2017-07-18 21:51:09 +00:00
|
|
|
|
|
|
|
/* Asynchronous messages */
|
1999-12-06 12:34:45 +00:00
|
|
|
byte *ring_buf; /* Ring buffer for asynchronous messages */
|
|
|
|
byte *ring_end, *ring_read, *ring_write; /* Pointers to the ring buffer */
|
2015-05-19 06:53:34 +00:00
|
|
|
uint ring_overflow; /* Counter of ring overflows */
|
|
|
|
uint log_mask; /* Mask of allowed message levels */
|
|
|
|
uint log_threshold; /* When free < log_threshold, store only important messages */
|
|
|
|
uint async_msg_size; /* Total size of async messages queued in tx_buf */
|
2018-08-30 14:45:45 +00:00
|
|
|
struct conf_order *running_config; /* Asynchronous config pointer */
|
1999-10-29 12:10:10 +00:00
|
|
|
} cli;
|
|
|
|
|
|
|
|
extern pool *cli_pool;
|
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
|
|
|
extern struct cli *this_cli; /* Used during parsing */
|
1999-10-29 12:10:10 +00:00
|
|
|
|
2012-12-26 11:40:48 +00:00
|
|
|
#define CLI_ASYNC_CODE 10000
|
|
|
|
|
1999-10-31 15:43:44 +00:00
|
|
|
/* Functions to be called by command handlers */
|
|
|
|
|
|
|
|
void cli_printf(cli *, int, char *, ...);
|
1999-11-25 15:34:51 +00:00
|
|
|
#define cli_msg(x...) cli_printf(this_cli, x)
|
2017-07-18 21:51:09 +00:00
|
|
|
void cli_write_trigger(cli *c);
|
2015-05-19 06:53:34 +00:00
|
|
|
void cli_set_log_echo(cli *, uint mask, uint size);
|
2017-07-18 21:51:09 +00:00
|
|
|
void cli_yield(cli *c);
|
2018-09-13 11:24:49 +00:00
|
|
|
void cli_sleep(cli *c);
|
|
|
|
void cli_wakeup(cli *c);
|
1999-10-31 15:43:44 +00:00
|
|
|
|
|
|
|
/* Functions provided to sysdep layer */
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
void cli_init(void);
|
2017-07-18 21:51:09 +00:00
|
|
|
cli *cli_new(sock *s);
|
|
|
|
void cli_run(cli *);
|
1999-10-29 12:10:10 +00:00
|
|
|
void cli_free(cli *);
|
2015-05-19 06:53:34 +00:00
|
|
|
void cli_echo(uint class, byte *msg);
|
1999-10-29 12:10:10 +00:00
|
|
|
|
2010-02-21 08:57:26 +00:00
|
|
|
static inline int cli_access_restricted(void)
|
|
|
|
{
|
|
|
|
if (this_cli && this_cli->restricted)
|
|
|
|
return (cli_printf(this_cli, 8007, "Access denied"), 1);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
#endif
|