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