paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Virtual terminal [aka TeletYpe] interface routine |
| 2 | Copyright (C) 1997 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #ifndef _ZEBRA_VTY_H |
| 22 | #define _ZEBRA_VTY_H |
| 23 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 24 | #include "thread.h" |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 25 | #include "log.h" |
Jorge Boncompte [DTI2] | d227617 | 2012-04-10 16:57:23 +0200 | [diff] [blame] | 26 | #include "sockunion.h" |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 27 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | #define VTY_BUFSIZ 512 |
| 29 | #define VTY_MAXHIST 20 |
| 30 | |
| 31 | /* VTY struct. */ |
| 32 | struct vty |
| 33 | { |
| 34 | /* File descripter of this vty. */ |
| 35 | int fd; |
| 36 | |
David Lamparter | 4715a53 | 2013-05-30 16:31:49 +0200 | [diff] [blame] | 37 | /* output FD, to support stdin/stdout combination */ |
| 38 | int wfd; |
| 39 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | /* Is this vty connect to file or not */ |
| 41 | enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type; |
| 42 | |
| 43 | /* Node status of this vty */ |
| 44 | int node; |
| 45 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 46 | /* Failure count */ |
| 47 | int fail; |
| 48 | |
| 49 | /* Output buffer. */ |
| 50 | struct buffer *obuf; |
| 51 | |
| 52 | /* Command input buffer */ |
| 53 | char *buf; |
| 54 | |
| 55 | /* Command cursor point */ |
| 56 | int cp; |
| 57 | |
| 58 | /* Command length */ |
| 59 | int length; |
| 60 | |
| 61 | /* Command max length. */ |
| 62 | int max; |
| 63 | |
| 64 | /* Histry of command */ |
| 65 | char *hist[VTY_MAXHIST]; |
| 66 | |
| 67 | /* History lookup current point */ |
| 68 | int hp; |
| 69 | |
| 70 | /* History insert end point */ |
| 71 | int hindex; |
| 72 | |
| 73 | /* For current referencing point of interface, route-map, |
| 74 | access-list etc... */ |
| 75 | void *index; |
| 76 | |
| 77 | /* For multiple level index treatment such as key chain and key. */ |
| 78 | void *index_sub; |
| 79 | |
| 80 | /* For escape character. */ |
| 81 | unsigned char escape; |
| 82 | |
| 83 | /* Current vty status. */ |
ajs | 5a64665 | 2004-11-05 01:25:55 +0000 | [diff] [blame] | 84 | enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 86 | /* IAC handling: was the last character received the |
| 87 | IAC (interpret-as-command) escape character (and therefore the next |
| 88 | character will be the command code)? Refer to Telnet RFC 854. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | unsigned char iac; |
| 90 | |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 91 | /* IAC SB (option subnegotiation) handling */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | unsigned char iac_sb_in_progress; |
ajs | 9fc7ebf | 2005-02-23 15:12:34 +0000 | [diff] [blame] | 93 | /* At the moment, we care only about the NAWS (window size) negotiation, |
| 94 | and that requires just a 5-character buffer (RFC 1073): |
| 95 | <NAWS char> <16-bit width> <16-bit height> */ |
| 96 | #define TELNET_NAWS_SB_LEN 5 |
| 97 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
| 98 | /* How many subnegotiation characters have we received? We just drop |
| 99 | those that do not fit in the buffer. */ |
| 100 | size_t sb_len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 101 | |
| 102 | /* Window width/height. */ |
| 103 | int width; |
| 104 | int height; |
| 105 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 106 | /* Configure lines. */ |
| 107 | int lines; |
| 108 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 109 | /* Terminal monitor. */ |
| 110 | int monitor; |
| 111 | |
| 112 | /* In configure mode. */ |
| 113 | int config; |
| 114 | |
| 115 | /* Read and write thread. */ |
| 116 | struct thread *t_read; |
| 117 | struct thread *t_write; |
| 118 | |
| 119 | /* Timeout seconds and thread. */ |
| 120 | unsigned long v_timeout; |
| 121 | struct thread *t_timeout; |
Jorge Boncompte [DTI2] | d227617 | 2012-04-10 16:57:23 +0200 | [diff] [blame] | 122 | |
| 123 | /* What address is this vty comming from. */ |
| 124 | char address[SU_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | /* Integrated configuration file. */ |
paul | e8f2984 | 2003-08-12 13:08:31 +0000 | [diff] [blame] | 128 | #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 129 | |
| 130 | /* Small macro to determine newline is newline only or linefeed needed. */ |
| 131 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") |
| 132 | |
| 133 | /* Default time out value */ |
| 134 | #define VTY_TIMEOUT_DEFAULT 600 |
| 135 | |
| 136 | /* Vty read buffer size. */ |
| 137 | #define VTY_READ_BUFSIZ 512 |
| 138 | |
| 139 | /* Directory separator. */ |
| 140 | #ifndef DIRECTORY_SEP |
| 141 | #define DIRECTORY_SEP '/' |
| 142 | #endif /* DIRECTORY_SEP */ |
| 143 | |
| 144 | #ifndef IS_DIRECTORY_SEP |
| 145 | #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP) |
| 146 | #endif |
| 147 | |
| 148 | /* GCC have printf type attribute check. */ |
| 149 | #ifdef __GNUC__ |
| 150 | #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) |
| 151 | #else |
| 152 | #define PRINTF_ATTRIBUTE(a,b) |
| 153 | #endif /* __GNUC__ */ |
| 154 | |
Andrew Certain | 7798b63 | 2012-12-04 13:33:24 -0800 | [diff] [blame] | 155 | /* Utility macros to convert VTY argument to unsigned long */ |
| 156 | #define VTY_GET_ULONG(NAME,V,STR) \ |
paul | d4f0960 | 2005-05-23 12:43:34 +0000 | [diff] [blame] | 157 | do { \ |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 158 | char *endptr = NULL; \ |
Ulrich Weber | 664711c | 2011-12-21 02:24:11 +0400 | [diff] [blame] | 159 | errno = 0; \ |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 160 | (V) = strtoul ((STR), &endptr, 10); \ |
Ulrich Weber | 664711c | 2011-12-21 02:24:11 +0400 | [diff] [blame] | 161 | if (*(STR) == '-' || *endptr != '\0' || errno) \ |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 162 | { \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \ |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 164 | return CMD_WARNING; \ |
| 165 | } \ |
paul | d4f0960 | 2005-05-23 12:43:34 +0000 | [diff] [blame] | 166 | } while (0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | |
Andrew Certain | 7798b63 | 2012-12-04 13:33:24 -0800 | [diff] [blame] | 168 | /* |
| 169 | * The logic below ((TMPL) <= ((MIN) && (TMPL) != (MIN)) is |
| 170 | * done to circumvent the compiler complaining about |
| 171 | * comparing unsigned numbers against zero, if MIN is zero. |
| 172 | * NB: The compiler isn't smart enough to supress the warning |
| 173 | * if you write (MIN) != 0 && tmpl < (MIN). |
| 174 | */ |
| 175 | #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \ |
| 176 | do { \ |
| 177 | VTY_GET_ULONG(NAME, (TMPL), STR); \ |
| 178 | if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \ |
| 179 | { \ |
| 180 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\ |
| 181 | return CMD_WARNING; \ |
| 182 | } \ |
paul | d4f0960 | 2005-05-23 12:43:34 +0000 | [diff] [blame] | 183 | } while (0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 184 | |
Andrew Certain | 7798b63 | 2012-12-04 13:33:24 -0800 | [diff] [blame] | 185 | #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \ |
| 186 | do { \ |
| 187 | unsigned long tmpl; \ |
| 188 | VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \ |
| 189 | (V) = tmpl; \ |
| 190 | } while (0) |
| 191 | |
| 192 | #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \ |
| 193 | do { \ |
| 194 | unsigned long tmpl; \ |
| 195 | VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \ |
| 196 | } while (0) |
| 197 | |
| 198 | #define VTY_GET_INTEGER(NAME,V,STR) \ |
| 199 | VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX) |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 200 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 201 | #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \ |
paul | d4f0960 | 2005-05-23 12:43:34 +0000 | [diff] [blame] | 202 | do { \ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 203 | int retv; \ |
| 204 | retv = inet_aton ((STR), &(V)); \ |
| 205 | if (!retv) \ |
| 206 | { \ |
| 207 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \ |
| 208 | return CMD_WARNING; \ |
| 209 | } \ |
paul | d4f0960 | 2005-05-23 12:43:34 +0000 | [diff] [blame] | 210 | } while (0) |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 211 | |
| 212 | #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \ |
paul | d4f0960 | 2005-05-23 12:43:34 +0000 | [diff] [blame] | 213 | do { \ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 214 | int retv; \ |
| 215 | retv = str2prefix_ipv4 ((STR), &(V)); \ |
| 216 | if (retv <= 0) \ |
| 217 | { \ |
| 218 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \ |
| 219 | return CMD_WARNING; \ |
| 220 | } \ |
paul | d4f0960 | 2005-05-23 12:43:34 +0000 | [diff] [blame] | 221 | } while (0) |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 222 | |
David Lamparter | 863f20c | 2015-01-27 20:24:15 +0100 | [diff] [blame] | 223 | #define VTY_WARN_EXPERIMENTAL() \ |
| 224 | do { \ |
| 225 | vty_out (vty, "%% WARNING: this command is experimental. Both its name and" \ |
| 226 | " parameters may%s%% change in a future version of Quagga," \ |
| 227 | " possibly breaking your configuration!%s", \ |
| 228 | VTY_NEWLINE, VTY_NEWLINE); \ |
| 229 | } while (0) |
| 230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 231 | /* Exported variables */ |
| 232 | extern char integrate_default[]; |
| 233 | |
| 234 | /* Prototypes. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 235 | extern void vty_init (struct thread_master *); |
| 236 | extern void vty_init_vtysh (void); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 237 | extern void vty_terminate (void); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 238 | extern void vty_reset (void); |
| 239 | extern struct vty *vty_new (void); |
David Lamparter | 464ccf3 | 2015-05-12 21:56:18 +0200 | [diff] [blame^] | 240 | extern struct vty *vty_stdio (void (*atclose)(void)); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 241 | extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); |
| 242 | extern void vty_read_config (char *, char *); |
| 243 | extern void vty_time_print (struct vty *, int); |
| 244 | extern void vty_serv_sock (const char *, unsigned short, const char *); |
| 245 | extern void vty_close (struct vty *); |
| 246 | extern char *vty_get_cwd (void); |
| 247 | extern void vty_log (const char *level, const char *proto, |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 248 | const char *fmt, struct timestamp_control *, va_list); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 249 | extern int vty_config_lock (struct vty *); |
| 250 | extern int vty_config_unlock (struct vty *); |
| 251 | extern int vty_shell (struct vty *); |
| 252 | extern int vty_shell_serv (struct vty *); |
| 253 | extern void vty_hello (struct vty *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 255 | /* Send a fixed-size message to all vty terminal monitors; this should be |
| 256 | an async-signal-safe function. */ |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 257 | extern void vty_log_fixed (char *buf, size_t len); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 258 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | #endif /* _ZEBRA_VTY_H */ |