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 <arpa/telnet.h>
37 #define SOCKET_FILE "/run/vpp/cli.sock"
39 volatile int window_resized = 0;
40 struct termios orig_tio;
43 send_ttype (int sock_fd, int is_interactive)
46 static char buf[2048];
48 /* wipe the buffer so there is no potential
49 * for inter-invocation leakage */
50 memset (buf, 0, sizeof (buf));
52 term = is_interactive ? getenv ("TERM") : "vppctl";
56 int len = snprintf (buf, sizeof (buf),
60 IAC, SB, TELOPT_TTYPE, 0, term, IAC, SE);
61 if (send (sock_fd, buf, len, 0) < 0)
63 perror ("send_ttype");
68 send_naws (int sock_fd)
71 static char buf[2048];
73 memset (buf, 0, sizeof (buf));
74 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
76 fprintf (stderr, "ioctl(TIOCGWINSZ)");
80 int len = snprintf (buf, sizeof (buf),
84 IAC, SB, TELOPT_NAWS, ws.ws_col >> 8, ws.ws_col & 0xff,
85 ws.ws_row >> 8, ws.ws_row & 0xff, IAC, SE);
86 int n_written = write (sock_fd, buf, len);
94 signal_handler_winch (int signum)
100 signal_handler_term (int signum)
102 tcsetattr (STDIN_FILENO, TCSAFLUSH, &orig_tio);
106 process_input (int sock_fd, unsigned char *rx_buf, int rx_buf_len,
107 int is_interactive, int *sent_ttype)
112 while (i < rx_buf_len)
114 if (rx_buf[i] == IAC)
116 if (rx_buf[i + 1] == SB)
118 char opt = rx_buf[i + 2];
121 if (rx_buf[i] != IAC)
123 fprintf (stderr, "SB ");
125 while (rx_buf[i] != IAC && i < rx_buf_len)
126 fprintf (stderr, "%02x ", rx_buf[i++]);
127 fprintf (stderr, "\n");
129 while (rx_buf[i] != IAC && i < rx_buf_len)
135 if (opt == TELOPT_TTYPE)
137 send_ttype (sock_fd, is_interactive);
140 else if (is_interactive && opt == TELOPT_NAWS)
146 fprintf (stderr, "IAC at %d, IAC %s %s", i,
147 TELCMD (rx_buf[i + 1]), TELOPT (rx_buf[i + 2]));
154 /* i is always the same or ahead of j, so at worst this is a no-op */
155 rx_buf[j] = rx_buf[i];
165 main (int argc, char *argv[])
167 struct epoll_event event;
172 unsigned long cmd_len = 0;
174 int is_interactive = 0;
175 int acked = 1; /* counts messages from VPP; starts at 1 */
177 char *sock_fname = SOCKET_FILE;
182 /* process command line */
186 if (argc > 1 && strncmp (argv[0], "-s", 2) == 0)
188 sock_fname = argv[1];
193 struct sockaddr_un saddr = { 0 };
194 saddr.sun_family = AF_UNIX;
196 if (strlen (sock_fname) > sizeof (saddr.sun_path) - 1)
198 perror ("socket path too long");
202 strncpy (saddr.sun_path, sock_fname, sizeof (saddr.sun_path) - 1);
204 sock_fd = socket (AF_UNIX, SOCK_STREAM, 0);
211 if (connect (sock_fd, (struct sockaddr *) &saddr, sizeof (saddr)) < 0)
217 for (arg = 0; arg < argc; arg++)
219 cmd_len += strlen (argv[arg]) + 1;
223 cmd_len++; // account for 0 at end
224 cmd = malloc (cmd_len);
228 perror ("malloc failed");
231 memset (cmd, 0, cmd_len);
232 unsigned long space_left = cmd_len - 1; // reserve space for 0 at end
235 strncat (cmd, *argv, space_left);
236 space_left -= strlen (*argv);
238 strncat (cmd, " ", space_left);
241 cmd[cmd_len - 2] = '\n';
242 cmd[cmd_len - 1] = 0;
245 is_interactive = isatty (STDIN_FILENO) && cmd == 0;
249 /* Capture terminal resize events */
250 memset (&sa, 0, sizeof (struct sigaction));
251 sa.sa_handler = signal_handler_winch;
252 if (sigaction (SIGWINCH, &sa, 0) < 0)
255 perror ("sigaction for SIGWINCH");
259 /* Capture SIGTERM to reset tty settings */
260 sa.sa_handler = signal_handler_term;
261 if (sigaction (SIGTERM, &sa, 0) < 0)
264 perror ("sigaction for SIGTERM");
268 /* Save the original tty state so we can restore it later */
269 if (tcgetattr (STDIN_FILENO, &orig_tio) < 0)
272 perror ("tcgetattr");
276 /* Tweak the tty settings */
278 /* echo off, canonical mode off, ext'd input processing off */
279 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
280 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
281 tio.c_cc[VTIME] = 0; /* no timer */
283 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio) < 0)
286 perror ("tcsetattr");
291 efd = epoll_create1 (0);
294 event.events = EPOLLIN | EPOLLPRI | EPOLLERR;
295 event.data.fd = STDIN_FILENO;
296 if (epoll_ctl (efd, EPOLL_CTL_ADD, STDIN_FILENO, &event) != 0)
298 /* ignore EPERM; it means stdin is something like /dev/null */
302 fprintf (stderr, "epoll_ctl[%d]", STDIN_FILENO);
308 /* register socket */
309 event.events = EPOLLIN | EPOLLPRI | EPOLLERR;
310 event.data.fd = sock_fd;
311 if (epoll_ctl (efd, EPOLL_CTL_ADD, sock_fd, &event) != 0)
314 fprintf (stderr, "epoll_ctl[%d]", sock_fd);
322 static int sent_cmd = 0;
330 if ((n = epoll_wait (efd, &event, 1, -1)) < 0)
332 /* maybe we received signal */
337 perror ("epoll_wait");
344 if (event.data.fd == STDIN_FILENO)
350 continue; /* not ready for this yet */
352 n = read (STDIN_FILENO, c, sizeof (c));
355 int n_written = write (sock_fd, c, n);
362 fprintf (stderr, "read rv=%d", n);
366 else if (event.data.fd == sock_fd)
368 unsigned char rx_buf[100];
369 memset (rx_buf, 0, sizeof (rx_buf));
370 int nread = recv (sock_fd, rx_buf, sizeof (rx_buf), 0);
380 int len = process_input (sock_fd, rx_buf, nread, is_interactive,
385 unsigned char *p = rx_buf, *q = rx_buf;
389 /* Search for and skip NUL bytes */
390 while (q < (p + len) && *q)
393 n = write (STDOUT_FILENO, p, q - p);
401 while (q < (p + len) && !*q)
404 acked++; /* every NUL is an acknowledgement */
411 if (do_quit && do_quit < acked)
413 /* Ask the other end to close the connection */
414 char quit_str[] = "quit\n";
415 int n = write (sock_fd, quit_str, strlen (quit_str));
416 if (n < strlen (quit_str))
419 perror ("write quit");
423 if (cmd && sent_ttype && !sent_cmd)
425 /* We wait until after the TELNET TTYPE option has been sent.
426 * That is to make sure the session at the VPP end has switched
427 * to line-by-line mode, and thus avoid prompts and echoing.
428 * Note that it does also disable further TELNET option processing.
430 int n_written = write (sock_fd, cmd, strlen (cmd) + 1);
432 if (n_written < strlen (cmd))
435 perror ("write command");
438 do_quit = acked; /* quit after the next response */
444 perror ("unknown fd");
457 tcsetattr (STDIN_FILENO, TCSAFLUSH, &orig_tio);
470 * fd.io coding-style-patch-verification: ON
473 * eval: (c-set-style "gnu")