iof-bird-daemon / nest / cli.c @ 725270cb
History | View | Annotate | Download (9.11 KB)
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 |
/**
|
10 |
* DOC: Command line interface
|
11 |
*
|
12 |
* This module takes care of the BIRD's command-line interface (CLI).
|
13 |
* The CLI exists to provide a way to control BIRD remotely and to inspect
|
14 |
* its status. It uses a very simple textual protocol over a stream
|
15 |
* connection provided by the platform dependent code (on UNIX systems,
|
16 |
* it's a UNIX domain socket).
|
17 |
*
|
18 |
* Each session of the CLI consists of a sequence of request and replies,
|
19 |
* slightly resembling the FTP and SMTP protocols.
|
20 |
* Requests are commands encoded as a single line of text, replies are
|
21 |
* sequences of lines starting with a four-digit code followed by either
|
22 |
* a space (if it's the last line of the reply) or a minus sign (when the
|
23 |
* reply is going to continue with the next line), the rest of the line
|
24 |
* contains a textual message semantics of which depends on the numeric
|
25 |
* code. If a reply line has the same code as the previous one and it's
|
26 |
* a continuation line, the whole prefix can be replaced by a single
|
27 |
* white space character.
|
28 |
*
|
29 |
* Reply codes starting with 0 stand for `action successfully completed' messages,
|
30 |
* 1 means `table entry', 8 `runtime error' and 9 `syntax error'.
|
31 |
*
|
32 |
* Each CLI session is internally represented by a &cli structure and a
|
33 |
* resource pool containing all resources associated with the connection,
|
34 |
* so that it can be easily freed whenever the connection gets closed, not depending
|
35 |
* on the current state of command processing.
|
36 |
*
|
37 |
* The CLI commands are declared as a part of the configuration grammar
|
38 |
* by using the |CF_CLI| macro. When a command is received, it is processed
|
39 |
* by the same lexical analyzer and parser as used for the configuration, but
|
40 |
* it's switched to a special mode by prepending a fake token to the text,
|
41 |
* so that it uses only the CLI command rules. Then the parser invokes
|
42 |
* an execution routine corresponding to the command, which either constructs
|
43 |
* the whole reply and returns it back or (in case it expects the reply will be long)
|
44 |
* it prints a partial reply and asks the CLI module (using the @cont hook)
|
45 |
* to call it again when the output is transferred to the user.
|
46 |
*
|
47 |
* The @this_cli variable points to a &cli structure of the session being
|
48 |
* currently parsed, but it's of course available only in command handlers
|
49 |
* not entered using the @cont hook.
|
50 |
*/
|
51 |
|
52 |
#include "nest/bird.h" |
53 |
#include "nest/cli.h" |
54 |
#include "conf/conf.h" |
55 |
#include "lib/string.h" |
56 |
|
57 |
pool *cli_pool; |
58 |
|
59 |
static byte *
|
60 |
cli_alloc_out(cli *c, int size)
|
61 |
{ |
62 |
struct cli_out *o;
|
63 |
|
64 |
if (!(o = c->tx_write) || o->wpos + size > o->end)
|
65 |
{ |
66 |
if (!o && c->tx_buf)
|
67 |
o = c->tx_buf; |
68 |
else
|
69 |
{ |
70 |
o = mb_alloc(c->pool, sizeof(struct cli_out) + CLI_TX_BUF_SIZE); |
71 |
if (c->tx_write)
|
72 |
c->tx_write->next = o; |
73 |
else
|
74 |
c->tx_buf = o; |
75 |
o->wpos = o->outpos = o->buf; |
76 |
o->end = o->buf + CLI_TX_BUF_SIZE; |
77 |
} |
78 |
c->tx_write = o; |
79 |
if (!c->tx_pos)
|
80 |
c->tx_pos = o; |
81 |
o->next = NULL;
|
82 |
} |
83 |
o->wpos += size; |
84 |
return o->wpos - size;
|
85 |
} |
86 |
|
87 |
/**
|
88 |
* cli_printf - send reply to a CLI connection
|
89 |
* @c: CLI connection
|
90 |
* @code: numeric code of the reply, negative for continuation lines
|
91 |
* @msg: a printf()-like formatting string.
|
92 |
*
|
93 |
* This function send a single line of reply to a given CLI connection.
|
94 |
* In works in all aspects like bsprintf() except that it automatically
|
95 |
* prepends the reply line prefix.
|
96 |
*
|
97 |
* Please note that if the connection can be already busy sending some
|
98 |
* data in which case cli_printf() stores the output to a temporary buffer,
|
99 |
* so please avoid sending a large batch of replies without waiting
|
100 |
* for the buffers to be flushed.
|
101 |
*
|
102 |
* If you want to write to the current CLI output, you can use the cli_msg()
|
103 |
* macro instead.
|
104 |
*/
|
105 |
void
|
106 |
cli_printf(cli *c, int code, char *msg, ...) |
107 |
{ |
108 |
va_list args; |
109 |
byte buf[1024];
|
110 |
int cd = code;
|
111 |
int size, cnt;
|
112 |
|
113 |
va_start(args, msg); |
114 |
if (cd < 0) |
115 |
{ |
116 |
cd = -cd; |
117 |
if (cd == c->last_reply)
|
118 |
size = bsprintf(buf, " ");
|
119 |
else
|
120 |
size = bsprintf(buf, "%04d-", cd);
|
121 |
} |
122 |
else
|
123 |
size = bsprintf(buf, "%04d ", cd);
|
124 |
c->last_reply = cd; |
125 |
cnt = bvsnprintf(buf+size, sizeof(buf)-size-1, msg, args); |
126 |
if (cnt < 0) |
127 |
{ |
128 |
cli_printf(c, code < 0 ? -8000 : 8000, "<line overflow>"); |
129 |
return;
|
130 |
} |
131 |
size += cnt; |
132 |
buf[size++] = '\n';
|
133 |
memcpy(cli_alloc_out(c, size), buf, size); |
134 |
} |
135 |
|
136 |
static void |
137 |
cli_copy_message(cli *c) |
138 |
{ |
139 |
byte *p, *q; |
140 |
unsigned int cnt = 2; |
141 |
|
142 |
if (c->ring_overflow)
|
143 |
{ |
144 |
byte buf[64];
|
145 |
int n = bsprintf(buf, "<%d messages lost>\n", c->ring_overflow); |
146 |
c->ring_overflow = 0;
|
147 |
memcpy(cli_alloc_out(c, n), buf, n); |
148 |
} |
149 |
p = c->ring_read; |
150 |
while (*p)
|
151 |
{ |
152 |
cnt++; |
153 |
p++; |
154 |
if (p == c->ring_end)
|
155 |
p = c->ring_buf; |
156 |
ASSERT(p != c->ring_write); |
157 |
} |
158 |
c->async_msg_size += cnt; |
159 |
q = cli_alloc_out(c, cnt); |
160 |
*q++ = '+';
|
161 |
p = c->ring_read; |
162 |
do
|
163 |
{ |
164 |
*q = *p++; |
165 |
if (p == c->ring_end)
|
166 |
p = c->ring_buf; |
167 |
} |
168 |
while (*q++);
|
169 |
c->ring_read = p; |
170 |
q[-1] = '\n'; |
171 |
} |
172 |
|
173 |
static void |
174 |
cli_hello(cli *c) |
175 |
{ |
176 |
cli_printf(c, 1, "BIRD " BIRD_VERSION " ready."); |
177 |
c->cont = NULL;
|
178 |
} |
179 |
|
180 |
static void |
181 |
cli_free_out(cli *c) |
182 |
{ |
183 |
struct cli_out *o, *p;
|
184 |
|
185 |
if (o = c->tx_buf)
|
186 |
{ |
187 |
o->wpos = o->outpos = o->buf; |
188 |
while (p = o->next)
|
189 |
{ |
190 |
o->next = p->next; |
191 |
mb_free(p); |
192 |
} |
193 |
} |
194 |
c->tx_write = c->tx_pos = NULL;
|
195 |
c->async_msg_size = 0;
|
196 |
} |
197 |
|
198 |
static byte *cli_rh_pos;
|
199 |
static unsigned int cli_rh_len; |
200 |
static int cli_rh_trick_flag; |
201 |
struct cli *this_cli;
|
202 |
|
203 |
static int |
204 |
cli_cmd_read_hook(byte *buf, unsigned int max) |
205 |
{ |
206 |
if (!cli_rh_trick_flag)
|
207 |
{ |
208 |
cli_rh_trick_flag = 1;
|
209 |
buf[0] = '!'; |
210 |
return 1; |
211 |
} |
212 |
if (max > cli_rh_len)
|
213 |
max = cli_rh_len; |
214 |
memcpy(buf, cli_rh_pos, max); |
215 |
cli_rh_pos += max; |
216 |
cli_rh_len -= max; |
217 |
return max;
|
218 |
} |
219 |
|
220 |
static void |
221 |
cli_command(struct cli *c)
|
222 |
{ |
223 |
struct config f;
|
224 |
int res;
|
225 |
|
226 |
if (config->cli_debug > 1) |
227 |
log(L_TRACE "CLI: %s", c->rx_buf);
|
228 |
bzero(&f, sizeof(f));
|
229 |
f.mem = c->parser_pool; |
230 |
cf_read_hook = cli_cmd_read_hook; |
231 |
cli_rh_pos = c->rx_buf; |
232 |
cli_rh_len = strlen(c->rx_buf); |
233 |
cli_rh_trick_flag = 0;
|
234 |
this_cli = c; |
235 |
lp_flush(c->parser_pool); |
236 |
res = cli_parse(&f); |
237 |
if (!res)
|
238 |
cli_printf(c, 9001, f.err_msg);
|
239 |
} |
240 |
|
241 |
static void |
242 |
cli_event(void *data)
|
243 |
{ |
244 |
cli *c = data; |
245 |
int err;
|
246 |
|
247 |
while (c->ring_read != c->ring_write &&
|
248 |
c->async_msg_size < CLI_MAX_ASYNC_QUEUE) |
249 |
cli_copy_message(c); |
250 |
|
251 |
if (c->tx_pos)
|
252 |
; |
253 |
else if (c->cont) |
254 |
c->cont(c); |
255 |
else
|
256 |
{ |
257 |
err = cli_get_command(c); |
258 |
if (!err)
|
259 |
return;
|
260 |
if (err < 0) |
261 |
cli_printf(c, 9000, "Command too long"); |
262 |
else
|
263 |
cli_command(c); |
264 |
} |
265 |
if (cli_write(c))
|
266 |
{ |
267 |
cli_free_out(c); |
268 |
ev_schedule(c->event); |
269 |
} |
270 |
} |
271 |
|
272 |
cli * |
273 |
cli_new(void *priv)
|
274 |
{ |
275 |
pool *p = rp_new(cli_pool, "CLI");
|
276 |
cli *c = mb_alloc(p, sizeof(cli));
|
277 |
|
278 |
bzero(c, sizeof(cli));
|
279 |
c->pool = p; |
280 |
c->priv = priv; |
281 |
c->event = ev_new(p); |
282 |
c->event->hook = cli_event; |
283 |
c->event->data = c; |
284 |
c->cont = cli_hello; |
285 |
c->parser_pool = lp_new(c->pool, 4096);
|
286 |
c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE); |
287 |
ev_schedule(c->event); |
288 |
return c;
|
289 |
} |
290 |
|
291 |
void
|
292 |
cli_kick(cli *c) |
293 |
{ |
294 |
if (!c->cont && !c->tx_pos)
|
295 |
ev_schedule(c->event); |
296 |
} |
297 |
|
298 |
void
|
299 |
cli_written(cli *c) |
300 |
{ |
301 |
cli_free_out(c); |
302 |
ev_schedule(c->event); |
303 |
} |
304 |
|
305 |
static list cli_log_hooks;
|
306 |
static int cli_log_inited; |
307 |
|
308 |
void
|
309 |
cli_set_log_echo(cli *c, unsigned int mask, unsigned int size) |
310 |
{ |
311 |
if (c->ring_buf)
|
312 |
{ |
313 |
mb_free(c->ring_buf); |
314 |
c->ring_buf = c->ring_end = c->ring_read = c->ring_write = NULL;
|
315 |
rem_node(&c->n); |
316 |
} |
317 |
c->log_mask = mask; |
318 |
if (mask && size)
|
319 |
{ |
320 |
c->ring_buf = mb_alloc(c->pool, size); |
321 |
c->ring_end = c->ring_buf + size; |
322 |
c->ring_read = c->ring_write = c->ring_buf; |
323 |
add_tail(&cli_log_hooks, &c->n); |
324 |
c->log_threshold = size / 8;
|
325 |
} |
326 |
c->ring_overflow = 0;
|
327 |
} |
328 |
|
329 |
void
|
330 |
cli_echo(unsigned int class, byte *msg) |
331 |
{ |
332 |
unsigned len, free, i, l;
|
333 |
cli *c; |
334 |
byte *m; |
335 |
|
336 |
if (!cli_log_inited || EMPTY_LIST(cli_log_hooks))
|
337 |
return;
|
338 |
len = strlen(msg) + 1;
|
339 |
WALK_LIST(c, cli_log_hooks) |
340 |
{ |
341 |
if (!(c->log_mask & (1 << class))) |
342 |
continue;
|
343 |
if (c->ring_read <= c->ring_write)
|
344 |
free = (c->ring_end - c->ring_buf) - (c->ring_write - c->ring_read + 1);
|
345 |
else
|
346 |
free = c->ring_read - c->ring_write - 1;
|
347 |
if (len > free ||
|
348 |
free < c->log_threshold && class < (unsigned) L_INFO[0]) |
349 |
{ |
350 |
c->ring_overflow++; |
351 |
continue;
|
352 |
} |
353 |
if (c->ring_read == c->ring_write)
|
354 |
ev_schedule(c->event); |
355 |
m = msg; |
356 |
l = len; |
357 |
while (l)
|
358 |
{ |
359 |
if (c->ring_read <= c->ring_write)
|
360 |
i = c->ring_end - c->ring_write; |
361 |
else
|
362 |
i = c->ring_read - c->ring_write; |
363 |
if (i > l)
|
364 |
i = l; |
365 |
memcpy(c->ring_write, m, i); |
366 |
m += i; |
367 |
l -= i; |
368 |
c->ring_write += i; |
369 |
if (c->ring_write == c->ring_end)
|
370 |
c->ring_write = c->ring_buf; |
371 |
} |
372 |
} |
373 |
} |
374 |
|
375 |
void
|
376 |
cli_free(cli *c) |
377 |
{ |
378 |
cli_set_log_echo(c, 0, 0); |
379 |
if (c->cleanup)
|
380 |
c->cleanup(c); |
381 |
rfree(c->pool); |
382 |
} |
383 |
|
384 |
/**
|
385 |
* cli_init - initialize the CLI module
|
386 |
*
|
387 |
* This function is called during BIRD startup to initialize
|
388 |
* the internal data structures of the CLI module.
|
389 |
*/
|
390 |
void
|
391 |
cli_init(void)
|
392 |
{ |
393 |
cli_pool = rp_new(&root_pool, "CLI");
|
394 |
init_list(&cli_log_hooks); |
395 |
cli_log_inited = 1;
|
396 |
} |