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 | |
| 44 | /* Privilege level of this vty. */ |
| 45 | int privilege; |
| 46 | |
| 47 | /* Failure count */ |
| 48 | int fail; |
| 49 | |
| 50 | /* Output buffer. */ |
| 51 | struct buffer *obuf; |
| 52 | |
| 53 | /* Command input buffer */ |
| 54 | char *buf; |
| 55 | |
| 56 | /* Command cursor point */ |
| 57 | int cp; |
| 58 | |
| 59 | /* Command length */ |
| 60 | int length; |
| 61 | |
| 62 | /* Command max length. */ |
| 63 | int max; |
| 64 | |
| 65 | /* Histry of command */ |
| 66 | char *hist[VTY_MAXHIST]; |
| 67 | |
| 68 | /* History lookup current point */ |
| 69 | int hp; |
| 70 | |
| 71 | /* History insert end point */ |
| 72 | int hindex; |
| 73 | |
| 74 | /* For current referencing point of interface, route-map, |
| 75 | access-list etc... */ |
| 76 | void *index; |
| 77 | |
| 78 | /* For multiple level index treatment such as key chain and key. */ |
| 79 | void *index_sub; |
| 80 | |
| 81 | /* For escape character. */ |
| 82 | unsigned char escape; |
| 83 | |
| 84 | /* Current vty status. */ |
| 85 | enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE, |
| 86 | VTY_START, VTY_CONTINUE} status; |
| 87 | |
| 88 | /* IAC handling */ |
| 89 | unsigned char iac; |
| 90 | |
| 91 | /* IAC SB handling */ |
| 92 | unsigned char iac_sb_in_progress; |
| 93 | struct buffer *sb_buffer; |
| 94 | |
| 95 | /* Window width/height. */ |
| 96 | int width; |
| 97 | int height; |
| 98 | |
| 99 | int scroll_one; |
| 100 | |
| 101 | /* Configure lines. */ |
| 102 | int lines; |
| 103 | |
| 104 | /* Current executing function pointer. */ |
| 105 | int (*func) (struct vty *, void *arg); |
| 106 | |
| 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; |
| 120 | |
| 121 | /* Thread output function. */ |
| 122 | struct thread *t_output; |
| 123 | |
| 124 | /* Output data pointer. */ |
| 125 | int (*output_func) (struct vty *, int); |
| 126 | void (*output_clean) (struct vty *); |
| 127 | void *output_rn; |
| 128 | unsigned long output_count; |
| 129 | int output_type; |
| 130 | void *output_arg; |
| 131 | }; |
| 132 | |
| 133 | /* Integrated configuration file. */ |
paul | e8f2984 | 2003-08-12 13:08:31 +0000 | [diff] [blame] | 134 | #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | |
| 136 | /* Small macro to determine newline is newline only or linefeed needed. */ |
| 137 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") |
| 138 | |
| 139 | /* Default time out value */ |
| 140 | #define VTY_TIMEOUT_DEFAULT 600 |
| 141 | |
| 142 | /* Vty read buffer size. */ |
| 143 | #define VTY_READ_BUFSIZ 512 |
| 144 | |
| 145 | /* Directory separator. */ |
| 146 | #ifndef DIRECTORY_SEP |
| 147 | #define DIRECTORY_SEP '/' |
| 148 | #endif /* DIRECTORY_SEP */ |
| 149 | |
| 150 | #ifndef IS_DIRECTORY_SEP |
| 151 | #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP) |
| 152 | #endif |
| 153 | |
| 154 | /* GCC have printf type attribute check. */ |
| 155 | #ifdef __GNUC__ |
| 156 | #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) |
| 157 | #else |
| 158 | #define PRINTF_ATTRIBUTE(a,b) |
| 159 | #endif /* __GNUC__ */ |
| 160 | |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 161 | /* Utility macros to convert VTY argument to unsigned long or integer. */ |
| 162 | #define VTY_GET_LONG(NAME,V,STR) \ |
| 163 | { \ |
| 164 | char *endptr = NULL; \ |
| 165 | (V) = strtoul ((STR), &endptr, 10); \ |
| 166 | if (*endptr != '\0' || (V) == ULONG_MAX) \ |
| 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 | } \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | } |
| 172 | |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 173 | #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \ |
| 174 | { \ |
| 175 | unsigned long tmpl; \ |
| 176 | VTY_GET_LONG(NAME, tmpl, STR) \ |
| 177 | if ( tmpl < (MIN) || tmpl > (MAX)) \ |
| 178 | { \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 179 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \ |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 180 | return CMD_WARNING; \ |
| 181 | } \ |
| 182 | (V) = tmpl; \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 183 | } |
| 184 | |
paul | 42d4986 | 2004-10-13 05:22:18 +0000 | [diff] [blame] | 185 | #define VTY_GET_INTEGER(NAME,V,STR) \ |
| 186 | VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX) |
| 187 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 188 | /* Exported variables */ |
| 189 | extern char integrate_default[]; |
| 190 | |
| 191 | /* Prototypes. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 192 | void vty_init (struct thread_master *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | void vty_init_vtysh (void); |
| 194 | void vty_reset (void); |
| 195 | void vty_finish (void); |
| 196 | struct vty *vty_new (void); |
| 197 | int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 198 | void vty_read_config (char *, char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 199 | void vty_time_print (struct vty *, int); |
hasso | 6ad96ea | 2004-10-07 19:33:46 +0000 | [diff] [blame] | 200 | void vty_serv_sock (const char *, unsigned short, const char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 201 | void vty_close (struct vty *); |
| 202 | char *vty_get_cwd (void); |
| 203 | void vty_log (const char *, const char *, va_list); |
| 204 | int vty_config_lock (struct vty *); |
| 205 | int vty_config_unlock (struct vty *); |
| 206 | int vty_shell (struct vty *); |
| 207 | int vty_shell_serv (struct vty *); |
| 208 | void vty_hello (struct vty *); |
| 209 | |
| 210 | #endif /* _ZEBRA_VTY_H */ |