aboutsummaryrefslogtreecommitdiffstats
path: root/src/vpp/app/vppctl.c
blob: 66fe00ab341ee1e35a088688dba1a960cb6100a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sys/socket.h>
#include <sys/un.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>

#define DEBUG 0

#if DEBUG
#define TELCMDS
#define TELOPTS
#endif

#include <arpa/telnet.h>

#include <vppinfra/mem.h>
#include <vppinfra/format.h>
#include <vppinfra/socket.h>

#define SOCKET_FILE "/run/vpp/cli.sock"

volatile int window_resized = 0;
struct termios orig_tio;

static void
send_ttype (clib_socket_t * s, int is_interactive)
{
  char *term;

  term = is_interactive ? getenv ("TERM") : "vppctl";
  if (term == NULL)
    term = "dumb";

  clib_socket_tx_add_formatted (s, "%c%c%c" "%c%s" "%c%c",
				IAC, SB, TELOPT_TTYPE, 0, term, IAC, SE);
  clib_socket_tx (s);
}

static void
send_naws (clib_socket_t * s)
{
  struct winsize ws;

  if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
    {
      clib_unix_warning ("ioctl(TIOCGWINSZ)");
      return;
    }

  clib_socket_tx_add_formatted (s, "%c%c%c" "%c%c%c%c" "%c%c",
				IAC, SB, TELOPT_NAWS,
				ws.ws_col >> 8, ws.ws_col & 0xff,
				ws.ws_row >> 8, ws.ws_row & 0xff, IAC, SE);
  clib_socket_tx (s);
}

static void
signal_handler_winch (int signum)
{
  window_resized = 1;
}

static void
signal_handler_term (int signum)
{
  tcsetattr (STDIN_FILENO, TCSAFLUSH, &orig_tio);
}

static u8 *
process_input (u8 * str, clib_socket_t * s, int is_interactive,
	       int *sent_ttype)
{
  int i = 0;

  while (i < vec_len (s->rx_buffer))
    {
      if (s->rx_buffer[i] == IAC)
	{
	  if (s->rx_buffer[i + 1] == SB)
	    {
	      u8 *sb = 0;
	      char opt = s->rx_buffer[i + 2];
	      i += 3;
	      while (s->rx_buffer[i] != IAC)
		vec_add1 (sb, s->rx_buffer[i++]);

#if DEBUG
	      clib_warning ("SB %s\n  %U", TELOPT (opt),
			    format_hexdump, sb, vec_len (sb));
#endif
	      vec_free (sb);
	      i += 2;
	      if (opt == TELOPT_TTYPE)
		{
		  send_ttype (s, is_interactive);
		  *sent_ttype = 1;
		}
	      else if (is_interactive && opt == TELOPT_NAWS)
		send_naws (s);
	    }
	  else
	    {
#if DEBUG
	      clib_warning ("IAC at %d, IAC %s %s", i,
			    TELCMD (s->rx_buffer[i + 1]),
			    TELOPT (s->rx_buffer[i + 2]));
#endif
	      i += 3;
	    }
	}
      else
	vec_add1 (str, s->rx_buffer[i++]);
    }
  vec_reset_length (s->rx_buffer);
  return str;
}


int
main (int argc, char *argv[])
{
  clib_socket_t _s = { 0 }, *s = &_s;
  clib_error_t *error = 0;
  struct epoll_event event;
  struct sigaction sa;
  struct termios tio;
  int efd = -1;
  u8 *str = 0;
  u8 *cmd = 0;
  int do_quit = 0;
  int is_interactive = 0;
  int acked = 1;		/* counts messages from VPP; starts at 1 */
  int sent_ttype = 0;


  clib_mem_init (0, 64ULL << 10);

  /* process command line */
  argc--;
  argv++;

  if (argc > 1 && strcmp (argv[0], "-s") == 0)
    {
      s->config = argv[1];
      argc -= 2;
      argv += 2;
    }
  else
    s->config = SOCKET_FILE;

  while (argc--)
    cmd = format (cmd, "%s%c", (argv++)[0], argc ? ' ' : 0);

  s->flags = CLIB_SOCKET_F_IS_CLIENT;

  error = clib_socket_init (s);
  if (error)
    goto done;

  is_interactive = isatty (STDIN_FILENO) && cmd == 0;

  if (is_interactive)
    {
      /* Capture terminal resize events */
      memset (&sa, 0, sizeof (struct sigaction));
      sa.sa_handler = signal_handler_winch;
      if (sigaction (SIGWINCH, &sa, 0) < 0)
	{
	  error = clib_error_return_unix (0, "sigaction");
	  goto done;
	}

      /* Capture SIGTERM to reset tty settings */
      sa.sa_handler = signal_handler_term;
      if (sigaction (SIGTERM, &sa, 0) < 0)
	{
	  error = clib_error_return_unix (0, "sigaction");
	  goto done;
	}

      /* Save the original tty state so we can restore it later */
      if (tcgetattr (STDIN_FILENO, &orig_tio) < 0)
	{
	  error = clib_error_return_unix (0, "tcgetattr");
	  goto done;
	}

      /* Tweak the tty settings */
      tio = orig_tio;
      /* echo off, canonical mode off, ext'd input processing off */
      tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
      tio.c_cc[VMIN] = 1;	/* 1 byte at a time */
      tio.c_cc[VTIME] = 0;	/* no timer */

      if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio) < 0)
	{
	  error = clib_error_return_unix (0, "tcsetattr");
	  goto done;
	}
    }

  efd = epoll_create1 (0);

  /* register STDIN */
  event.events = EPOLLIN | EPOLLPRI | EPOLLERR;
  event.data.fd = STDIN_FILENO;
  if (epoll_ctl (efd, EPOLL_CTL_ADD, STDIN_FILENO, &event) != 0)
    {
      /* ignore EPERM; it means stdin is something like /dev/null */
      if (errno != EPERM)
	{
	  error = clib_error_return_unix (0, "epoll_ctl[%d]", STDIN_FILENO);
	  goto done;
	}
    }

  /* register socket */
  event.events = EPOLLIN | EPOLLPRI | EPOLLERR;
  event.data.fd = s->fd;
  if (epoll_ctl (efd, EPOLL_CTL_ADD, s->fd, &event) != 0)
    {
      error = clib_error_return_unix (0, "epoll_ctl[%d]", s->fd);
      goto done;
    }

  while (1)
    {
      int n;

      if (window_resized)
	{
	  window_resized = 0;
	  send_naws (s);
	}

      if ((n = epoll_wait (efd, &event, 1, -1)) < 0)
	{
	  /* maybe we received signal */
	  if (errno == EINTR)
	    continue;

	  error = clib_error_return_unix (0, "epoll_wait");
	  goto done;
	}

      if (n == 0)
	continue;

      if (event.data.fd == STDIN_FILENO)
	{
	  int n;
	  char c[100];

	  if (!sent_ttype)
	    continue;		/* not ready for this yet */

	  n = read (STDIN_FILENO, c, sizeof (c));
	  if (n > 0)
	    {
	      memcpy (clib_socket_tx_add (s, n), c, n);
	      error = clib_socket_tx (s);
	      if (error)
		goto done;
	    }
	  else if (n < 0)
	    clib_warning ("read rv=%d", n);
	  else			/* EOF */
	    do_quit = 1;
	}
      else if (event.data.fd == s->fd)
	{
	  error = clib_socket_rx (s, 100);
	  if (error)
	    break;

	  if (clib_socket_rx_end_of_file (s))
	    break;

	  str = process_input (str, s, is_interactive, &sent_ttype);

	  if (vec_len (str) > 0)
	    {
	      int len = vec_len (str);
	      u8 *p = str, *q = str;

	      while (len)
		{
		  /* Search for and skip NUL bytes */
		  while (q < (p + len) && *q)
		    q++;

		  n = write (STDOUT_FILENO, p, q - p);
		  if (n < 0)
		    {
		      error = clib_error_return_unix (0, "write");
		      goto done;
		    }

		  while (q < (p + len) && !*q)
		    {
		      q++;
		      acked++;	/* every NUL is an acknowledgement */
		    }
		  len -= q - p;
		  p = q;
		}

	      vec_reset_length (str);
	    }

	  if (do_quit && do_quit < acked)
	    {
	      /* Ask the other end to close the connection */
	      clib_socket_tx_add_formatted (s, "quit\n");
	      clib_socket_tx (s);
	      do_quit = 0;
	    }
	  if (cmd && sent_ttype)
	    {
	      /* We wait until after the TELNET TTYPE option has been sent.
	       * That is to make sure the session at the VPP end has switched
	       * to line-by-line mode, and thus avoid prompts and echoing.
	       * Note that it does also disable further TELNET option processing.
	       */
	      clib_socket_tx_add_formatted (s, "%s\n", cmd);
	      clib_socket_tx (s);
	      vec_free (cmd);
	      do_quit = acked;	/* quit after the next response */
	    }
	}
      else
	{
	  error = clib_error_return (0, "unknown fd");
	  goto done;
	}
    }

  error = clib_socket_close (s);

done:
  vec_free (cmd);
  vec_free (str);
  if (efd > -1)
    close (efd);

  if (is_interactive)
    tcsetattr (STDIN_FILENO, TCSAFLUSH, &orig_tio);

  if (error)
    {
      clib_error_report (error);
      return 1;
    }

  return 0;
}

/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */