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