]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/cli.h
423b2180315a132559720ebefd92b204b1b7bf63
[thirdparty/bird.git] / nest / cli.h
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 #include "lib/timer.h"
15 #include "lib/tlists.h"
16 #include "conf/conf.h"
17
18 #define CLI_RX_BUF_SIZE 4096
19 #define CLI_TX_BUF_SIZE 4096
20 #define CLI_MAX_ASYNC_QUEUE 4096
21
22 #define CLI_MSG_SIZE 500
23 #define CLI_LINE_SIZE 512
24
25 struct cli_out {
26 struct cli_out *next;
27 byte *wpos, *outpos, *end;
28 byte buf[0];
29 };
30
31 typedef struct cli {
32 node n; /* Node in list of all log hooks */
33 pool *pool;
34 void *priv; /* Private to sysdep layer */
35 byte *rx_buf, *rx_pos; /* sysdep */
36 struct cli_out *tx_buf, *tx_pos, *tx_write;
37 event *event;
38 void (*cont)(struct cli *c);
39 void (*cleanup)(struct cli *c);
40 void *rover; /* Private to continuation routine */
41 int last_reply;
42 int restricted; /* CLI is restricted to read-only commands */
43 struct timeformat *tf; /* Time format override */
44 struct linpool *parser_pool; /* Pool used during parsing */
45 struct linpool *show_pool; /* Pool used during route show */
46 byte *ring_buf; /* Ring buffer for asynchronous messages */
47 byte *ring_end, *ring_read, *ring_write; /* Pointers to the ring buffer */
48 uint ring_overflow; /* Counter of ring overflows */
49 uint log_mask; /* Mask of allowed message levels */
50 uint log_threshold; /* When free < log_threshold, store only important messages */
51 uint async_msg_size; /* Total size of async messages queued in tx_buf */
52 } cli;
53
54 struct cli_config {
55 #define TLIST_PREFIX cli_config
56 #define TLIST_TYPE struct cli_config
57 #define TLIST_ITEM n
58 #define TLIST_DEFINED_BEFORE
59 #define TLIST_WANT_ADD_TAIL
60 #define TLIST_WANT_WALK
61 TLIST_DEFAULT_NODE;
62 const char *name;
63 struct config *config;
64 uint uid, gid, mode;
65 _Bool restricted;
66 };
67 #include "lib/tlists.h"
68
69 void cli_config_listen(struct cli_config *, const char *);
70
71 extern pool *cli_pool;
72 extern struct cli *this_cli; /* Used during parsing */
73
74 #define CLI_ASYNC_CODE 10000
75
76 /* Functions to be called by command handlers */
77
78 void cli_vprintf(cli *, int, const char *, va_list);
79 static inline void cli_printf(cli *cli, int code, const char *fmt, ...)
80 {
81 va_list args;
82 va_start(args, fmt);
83 cli_vprintf(cli, code, fmt, args);
84 va_end(args);
85 }
86
87 #define cli_msg(x...) cli_printf(this_cli, x)
88 void cli_set_log_echo(cli *, uint mask, uint size);
89 void cli_set_timeformat(cli *c, const struct timeformat tf);
90
91 static inline void cli_separator(cli *c)
92 { if (c->last_reply) cli_printf(c, -c->last_reply, ""); };
93
94 /* Functions provided to sysdep layer */
95
96 cli *cli_new(void *, struct cli_config *);
97 void cli_init(void);
98 void cli_free(cli *);
99 void cli_kick(cli *);
100 void cli_written(cli *);
101 void cli_echo(uint class, byte *msg);
102
103 static inline int cli_access_restricted(void)
104 {
105 if (this_cli && this_cli->restricted)
106 return (cli_printf(this_cli, 8007, "Access denied"), 1);
107 else
108 return 0;
109 }
110
111 /* Functions provided by sysdep layer */
112
113 void cli_write_trigger(cli *);
114 int cli_get_command(cli *);
115
116 #endif