2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <sys/socket.h>
18 #include <sys/epoll.h>
19 #include <sys/ioctl.h>
35 #include <vppinfra/clib.h>
36 #include <arpa/telnet.h>
37 #include <vpp/vnet/config.h>
39 #define SOCKET_FILE "/run/vpp/cli.sock"
41 volatile int window_resized = 0;
42 struct termios orig_tio;
45 send_ttype (int sock_fd, int is_interactive)
48 static char buf[2048];
50 /* wipe the buffer so there is no potential
51 * for inter-invocation leakage */
52 memset (buf, 0, sizeof (buf));
54 term = is_interactive ? getenv ("TERM") : "vppctl";
58 int len = snprintf (buf, sizeof (buf),
62 IAC, SB, TELOPT_TTYPE, 0, term, IAC, SE);
63 if (send (sock_fd, buf, len, 0) < 0)
65 perror ("send_ttype");
70 send_naws (int sock_fd)
73 static char buf[2048];
75 memset (buf, 0, sizeof (buf));
76 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
78 fprintf (stderr, "ioctl(TIOCGWINSZ)");
82 int len = snprintf (buf, sizeof (buf),
86 IAC, SB, TELOPT_NAWS, ws.ws_col >> 8, ws.ws_col & 0xff,
87 ws.ws_row >> 8, ws.ws_row & 0xff, IAC, SE);
88 int n_written = write (sock_fd, buf, len);
96 signal_handler_winch (int signum)
102 signal_handler_term (int signum)
104 tcsetattr (STDIN_FILENO, TCSAFLUSH, &orig_tio);
108 process_input (int sock_fd, unsigned char *rx_buf, int rx_buf_len,
109 int is_interactive, int *sent_ttype)
114 while (i < rx_buf_len)
116 if (rx_buf[i] == IAC)
118 if (rx_buf[i + 1] == SB)
120 char opt = rx_buf[i + 2];
123 if (rx_buf[i] != IAC)
125 fprintf (stderr, "SB ");
127 while (rx_buf[i] != IAC && i < rx_buf_len)
128 fprintf (stderr, "%02x ", rx_buf[i++]);
129 fprintf (stderr, "\n");
131 while (rx_buf[i] != IAC && i < rx_buf_len)
137 if (opt == TELOPT_TTYPE)
139 send_ttype (sock_fd, is_interactive);
142 else if (is_interactive && opt == TELOPT_NAWS)
148 fprintf (stderr, "IAC at %d, IAC %s %s", i,
149 TELCMD (rx_buf[i + 1]), TELOPT (rx_buf[i + 2]));
156 /* i is always the same or ahead of j, so at worst this is a no-op */
157 rx_buf[j] = rx_buf[i];
165 #ifdef CLIB_SANITIZE_ADDR
166 /* default options for Address Sanitizer */
168 __asan_default_options (void)
170 return VPP_SANITIZE_ADDR_OPTIONS;
172 #endif /* CLIB_SANITIZE_ADDR */
175 main (int argc, char *argv[])
177 struct epoll_event event;
182 unsigned long cmd_len = 0;
184 int is_interactive = 0;
185 int acked = 1; /* counts messages from VPP; starts at 1 */
187 char *sock_fname = SOCKET_FILE;
192 /* process command line */
196 if (argc > 1 && strncmp (argv[0], "-s", 2) == 0)
198 sock_fname = argv[1];
203 struct sockaddr_un saddr = { 0 };
204 saddr.sun_family = AF_UNIX;
206 if (strlen (sock_fname) > sizeof (saddr.sun_path) - 1)
208 perror ("socket path too long");
212 strncpy (saddr.sun_path, sock_fname, sizeof (saddr.sun_path) - 1);
214 sock_fd = socket (AF_UNIX, SOCK_STREAM, 0);
221 if (connect (sock_fd, (struct sockaddr *) &saddr, sizeof (saddr)) < 0)
227 for (arg = 0; arg < argc; arg++)
229 cmd_len += strlen (argv[arg]) + 1;
233 cmd_len++; // account for 0 at end
234 cmd = malloc (cmd_len);
238 perror ("malloc failed");
241 memset (cmd, 0, cmd_len);
242 unsigned long space_left = cmd_len - 1; // reserve space for 0 at end
245 strncat (cmd, *argv, space_left);
246 space_left -= strlen (*argv);
248 strncat (cmd, " ", space_left);
251 cmd[cmd_len - 2] = '\n';
252 cmd[cmd_len - 1] = 0;
255 is_interactive = isatty (STDIN_FILENO) && cmd == 0;
259 /* Capture terminal resize events */
260 memset (&sa, 0, sizeof (struct sigaction));
261 sa.sa_handler = signal_handler_winch;
262 if (sigaction (SIGWINCH, &sa, 0) < 0)
265 perror ("sigaction for SIGWINCH");
269 /* Capture SIGTERM to reset tty settings */
270 sa.sa_handler = signal_handler_term;
271 if (sigaction (SIGTERM, &sa, 0) < 0)
274 perror ("sigaction for SIGTERM");
278 /* Save the original tty state so we can restore it later */
279 if (tcgetattr (STDIN_FILENO, &orig_tio) < 0)
282 perror ("tcgetattr");
286 /* Tweak the tty settings */
288 /* echo off, canonical mode off, ext'd input processing off */
289 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
290 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
291 tio.c_cc[VTIME] = 0; /* no timer */
293 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio) < 0)
296 perror ("tcsetattr");
301 efd = epoll_create1 (0);
306 event.events = EPOLLIN | EPOLLPRI | EPOLLERR;
307 event.data.fd = STDIN_FILENO;
308 if (epoll_ctl (efd, EPOLL_CTL_ADD, STDIN_FILENO, &event) != 0)
310 /* ignore EPERM; it means stdin is something like /dev/null */
314 fprintf (stderr, "epoll_ctl[%d]", STDIN_FILENO);
321 /* register socket */
322 event.events = EPOLLIN | EPOLLPRI | EPOLLERR;
323 event.data.fd = sock_fd;
324 if (epoll_ctl (efd, EPOLL_CTL_ADD, sock_fd, &event) != 0)
327 fprintf (stderr, "epoll_ctl[%d]", sock_fd);
335 static int sent_cmd = 0;
343 if ((n = epoll_wait (efd, &event, 1, -1)) < 0)
345 /* maybe we received signal */
350 perror ("epoll_wait");
357 if (event.data.fd == STDIN_FILENO && cmd == 0)
363 continue; /* not ready for this yet */
365 n = read (STDIN_FILENO, c, sizeof (c));
368 int n_written = write (sock_fd, c, n);
375 fprintf (stderr, "read rv=%d", n);
379 else if (event.data.fd == sock_fd)
381 unsigned char rx_buf[100];
382 memset (rx_buf, 0, sizeof (rx_buf));
383 int nread = recv (sock_fd, rx_buf, sizeof (rx_buf), 0);
393 int len = process_input (sock_fd, rx_buf, nread, is_interactive,
398 unsigned char *p = rx_buf, *q = rx_buf;
402 /* Search for and skip NUL bytes */
403 while (q < (p + len) && *q)
406 n = write (STDOUT_FILENO, p, q - p);
414 while (q < (p + len) && !*q)
417 acked++; /* every NUL is an acknowledgement */
424 if (do_quit && do_quit < acked)
426 /* Ask the other end to close the connection */
427 char quit_str[] = "quit\n";
428 int n = write (sock_fd, quit_str, strlen (quit_str));
429 if (n < strlen (quit_str))
432 perror ("write quit");
436 if (cmd && sent_ttype && !sent_cmd)
438 /* We wait until after the TELNET TTYPE option has been sent.
439 * That is to make sure the session at the VPP end has switched
440 * to line-by-line mode, and thus avoid prompts and echoing.
441 * Note that it does also disable further TELNET option processing.
443 int n_written = write (sock_fd, cmd, strlen (cmd) + 1);
445 if (n_written < strlen (cmd))
448 perror ("write command");
451 do_quit = acked; /* quit after the next response */
457 perror ("unknown fd");
470 tcsetattr (STDIN_FILENO, TCSAFLUSH, &orig_tio);
483 * fd.io coding-style-patch-verification: ON
486 * eval: (c-set-style "gnu")