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