Statistics
| Branch: | Revision:

iof-bird-daemon / nest / cli.c @ 725270cb

History | View | Annotate | Download (9.11 KB)

1 7d3aab1c Martin Mares
/*
2
 *        BIRD Internet Routing Daemon -- Command-Line Interface
3
 *
4 3d675cdb Martin Mares
 *        (c) 1999--2000 Martin Mares <mj@ucw.cz>
5 7d3aab1c Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 3d675cdb Martin Mares
/**
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 58f7d004 Martin Mares
 * Reply codes starting with 0 stand for `action successfully completed' messages,
30 3d675cdb Martin Mares
 * 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 58f7d004 Martin Mares
 * so that it can be easily freed whenever the connection gets closed, not depending
35 3d675cdb Martin Mares
 * on the current state of command processing.
36
 *
37
 * The CLI commands are declared as a part of the configuration grammar
38 725270cb Martin Mares
 * by using the |CF_CLI| macro. When a command is received, it is processed
39 2e9b2421 Martin Mares
 * by the same lexical analyzer and parser as used for the configuration, but
40 3d675cdb Martin Mares
 * 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 725270cb Martin Mares
 * the whole reply and returns it back or (in case it expects the reply will be long)
44 3d675cdb Martin Mares
 * it prints a partial reply and asks the CLI module (using the @cont hook)
45 58f7d004 Martin Mares
 * to call it again when the output is transferred to the user.
46 3d675cdb Martin Mares
 *
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 7d3aab1c Martin Mares
#include "nest/bird.h"
53
#include "nest/cli.h"
54 bc2fb680 Martin Mares
#include "conf/conf.h"
55
#include "lib/string.h"
56 7d3aab1c Martin Mares
57
pool *cli_pool;
58
59 34350a52 Martin Mares
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 f75e3bbc Martin Mares
      o->next = NULL;
82 34350a52 Martin Mares
    }
83
  o->wpos += size;
84
  return o->wpos - size;
85
}
86
87 3d675cdb Martin Mares
/**
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 7d3aab1c Martin Mares
void
106
cli_printf(cli *c, int code, char *msg, ...)
107
{
108
  va_list args;
109
  byte buf[1024];
110 b9672a84 Martin Mares
  int cd = code;
111
  int size, cnt;
112 7d3aab1c Martin Mares
113
  va_start(args, msg);
114 b9672a84 Martin Mares
  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 7d3aab1c Martin Mares
  else
123 b9672a84 Martin Mares
    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 7d3aab1c Martin Mares
  buf[size++] = '\n';
133 34350a52 Martin Mares
  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 7d3aab1c Martin Mares
    {
144 34350a52 Martin Mares
      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 7d3aab1c Martin Mares
    }
149 34350a52 Martin Mares
  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 7d3aab1c Martin Mares
}
172
173
static void
174 b9672a84 Martin Mares
cli_hello(cli *c)
175
{
176
  cli_printf(c, 1, "BIRD " BIRD_VERSION " ready.");
177
  c->cont = NULL;
178
}
179
180
static void
181 7d3aab1c Martin Mares
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 f75e3bbc Martin Mares
  c->tx_write = c->tx_pos = NULL;
195 34350a52 Martin Mares
  c->async_msg_size = 0;
196 7d3aab1c Martin Mares
}
197
198 bc2fb680 Martin Mares
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 4761efdb Martin Mares
  if (config->cli_debug > 1)
227
    log(L_TRACE "CLI: %s", c->rx_buf);
228 1d2664a4 Martin Mares
  bzero(&f, sizeof(f));
229 bc2fb680 Martin Mares
  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 f611f0ee Martin Mares
  res = cli_parse(&f);
237 bc2fb680 Martin Mares
  if (!res)
238
    cli_printf(c, 9001, f.err_msg);
239
}
240
241 f75e3bbc Martin Mares
static void
242 7d3aab1c Martin Mares
cli_event(void *data)
243
{
244
  cli *c = data;
245
  int err;
246
247 34350a52 Martin Mares
  while (c->ring_read != c->ring_write &&
248
      c->async_msg_size < CLI_MAX_ASYNC_QUEUE)
249
    cli_copy_message(c);
250
251 b9672a84 Martin Mares
  if (c->tx_pos)
252
    ;
253
  else if (c->cont)
254
    c->cont(c);
255
  else
256 7d3aab1c Martin Mares
    {
257 b9672a84 Martin Mares
      err = cli_get_command(c);
258
      if (!err)
259 f75e3bbc Martin Mares
        return;
260 b9672a84 Martin Mares
      if (err < 0)
261
        cli_printf(c, 9000, "Command too long");
262
      else
263 bc2fb680 Martin Mares
        cli_command(c);
264 7d3aab1c Martin Mares
    }
265 b9672a84 Martin Mares
  if (cli_write(c))
266
    {
267
      cli_free_out(c);
268 f75e3bbc Martin Mares
      ev_schedule(c->event);
269 b9672a84 Martin Mares
    }
270 7d3aab1c Martin Mares
}
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 34350a52 Martin Mares
  bzero(c, sizeof(cli));
279 7d3aab1c Martin Mares
  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 b9672a84 Martin Mares
  c->cont = cli_hello;
285 bc2fb680 Martin Mares
  c->parser_pool = lp_new(c->pool, 4096);
286 34350a52 Martin Mares
  c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE);
287 b9672a84 Martin Mares
  ev_schedule(c->event);
288 7d3aab1c Martin Mares
  return c;
289
}
290
291
void
292
cli_kick(cli *c)
293
{
294 b9672a84 Martin Mares
  if (!c->cont && !c->tx_pos)
295
    ev_schedule(c->event);
296 7d3aab1c Martin Mares
}
297
298
void
299
cli_written(cli *c)
300
{
301
  cli_free_out(c);
302 b9672a84 Martin Mares
  ev_schedule(c->event);
303 7d3aab1c Martin Mares
}
304
305 34350a52 Martin Mares
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 7d3aab1c Martin Mares
void
376
cli_free(cli *c)
377
{
378 34350a52 Martin Mares
  cli_set_log_echo(c, 0, 0);
379 ffb59d24 Martin Mares
  if (c->cleanup)
380
    c->cleanup(c);
381 7d3aab1c Martin Mares
  rfree(c->pool);
382
}
383
384 3d675cdb Martin Mares
/**
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 7d3aab1c Martin Mares
void
391
cli_init(void)
392
{
393
  cli_pool = rp_new(&root_pool, "CLI");
394 34350a52 Martin Mares
  init_list(&cli_log_hooks);
395
  cli_log_inited = 1;
396 7d3aab1c Martin Mares
}