1 /*
2  *	BIRD Internet Routing Daemon -- Command-Line Interface
3  *
4  *	(c) 1999--2000 Martin Mares <mj@ucw.cz>
5  *
6  *	Can be freely distributed and used under the terms of the GNU GPL.
7  */
8 
9 #ifndef _BIRD_CLI_H_
10 #define _BIRD_CLI_H_
11 
12 #include "lib/resource.h"
13 #include "lib/event.h"
14 
15 #define CLI_RX_BUF_SIZE 4096
16 #define CLI_TX_BUF_SIZE 4096
17 #define CLI_MAX_ASYNC_QUEUE 4096
18 
19 #define CLI_MSG_SIZE 500
20 #define CLI_LINE_SIZE 512
21 
22 struct cli_out {
23   struct cli_out *next;
24   byte *wpos, *outpos, *end;
25   byte buf[0];
26 };
27 
28 typedef struct cli {
29   node n;				/* Node in list of all log hooks */
30   pool *pool;
31   void *priv;				/* Private to sysdep layer */
32   byte *rx_buf, *rx_pos, *rx_aux;	/* sysdep */
33   struct cli_out *tx_buf, *tx_pos, *tx_write;
34   event *event;
35   void (*cont)(struct cli *c);
36   void (*cleanup)(struct cli *c);
37   void *rover;				/* Private to continuation routine */
38   int last_reply;
39   int restricted;			/* CLI is restricted to read-only commands */
40   struct linpool *parser_pool;		/* Pool used during parsing */
41   struct linpool *show_pool;		/* Pool used during route show */
42   byte *ring_buf;			/* Ring buffer for asynchronous messages */
43   byte *ring_end, *ring_read, *ring_write;	/* Pointers to the ring buffer */
44   uint ring_overflow;			/* Counter of ring overflows */
45   uint log_mask;			/* Mask of allowed message levels */
46   uint log_threshold;			/* When free < log_threshold, store only important messages */
47   uint async_msg_size;			/* Total size of async messages queued in tx_buf */
48 } cli;
49 
50 extern pool *cli_pool;
51 extern struct cli *this_cli;		/* Used during parsing */
52 
53 #define CLI_ASYNC_CODE 10000
54 
55 /* Functions to be called by command handlers */
56 
57 void cli_printf(cli *, int, char *, ...);
58 #define cli_msg(x...) cli_printf(this_cli, x)
59 void cli_set_log_echo(cli *, uint mask, uint size);
60 
cli_separator(cli * c)61 static inline void cli_separator(cli *c)
62 { if (c->last_reply) cli_printf(c, -c->last_reply, ""); };
63 
64 /* Functions provided to sysdep layer */
65 
66 cli *cli_new(void *);
67 void cli_init(void);
68 void cli_free(cli *);
69 void cli_kick(cli *);
70 void cli_written(cli *);
71 void cli_echo(uint class, byte *msg);
72 
cli_access_restricted(void)73 static inline int cli_access_restricted(void)
74 {
75   if (this_cli && this_cli->restricted)
76     return (cli_printf(this_cli, 8007, "Access denied"), 1);
77   else
78     return 0;
79 }
80 
81 /* Functions provided by sysdep layer */
82 
83 void cli_write_trigger(cli *);
84 int cli_get_command(cli *);
85 
86 #endif
87