]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Virtual terminal [aka TeletYpe] interface routine |
896014f4 DL |
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 along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
718e3744 | 20 | |
21 | #ifndef _ZEBRA_VTY_H | |
22 | #define _ZEBRA_VTY_H | |
23 | ||
fe6b47b9 QY |
24 | #include <sys/types.h> |
25 | #include <regex.h> | |
26 | ||
b21b19c5 | 27 | #include "thread.h" |
1ed72e0b | 28 | #include "log.h" |
d227617a | 29 | #include "sockunion.h" |
0878c8d4 | 30 | #include "qobj.h" |
de1a880c | 31 | #include "compiler.h" |
1c2facd1 | 32 | #include "northbound.h" |
b21b19c5 | 33 | |
2af38873 | 34 | #define VTY_BUFSIZ 4096 |
718e3744 | 35 | #define VTY_MAXHIST 20 |
1c2facd1 | 36 | #define VTY_MAXDEPTH 8 |
718e3744 | 37 | |
7ab57d19 DS |
38 | struct vty_error { |
39 | char error_buf[VTY_BUFSIZ]; | |
40 | uint32_t line_num; | |
41 | }; | |
42 | ||
718e3744 | 43 | /* VTY struct. */ |
d62a17ae | 44 | struct vty { |
45 | /* File descripter of this vty. */ | |
46 | int fd; | |
718e3744 | 47 | |
d62a17ae | 48 | /* output FD, to support stdin/stdout combination */ |
49 | int wfd; | |
c5e69a02 | 50 | |
2cddf2ff QY |
51 | /* File output, used for VTYSH only */ |
52 | FILE *of; | |
53 | FILE *of_saved; | |
54 | ||
55 | /* whether we are using pager or not */ | |
56 | bool is_paged; | |
57 | ||
d62a17ae | 58 | /* Is this vty connect to file or not */ |
59 | enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type; | |
718e3744 | 60 | |
d62a17ae | 61 | /* Node status of this vty */ |
62 | int node; | |
718e3744 | 63 | |
d62a17ae | 64 | /* Failure count */ |
65 | int fail; | |
718e3744 | 66 | |
fe6b47b9 QY |
67 | /* Output filer regex */ |
68 | bool filter; | |
69 | regex_t include; | |
70 | ||
0b42d81a QY |
71 | /* Line buffer */ |
72 | struct buffer *lbuf; | |
73 | ||
d62a17ae | 74 | /* Output buffer. */ |
75 | struct buffer *obuf; | |
718e3744 | 76 | |
d62a17ae | 77 | /* Command input buffer */ |
78 | char *buf; | |
718e3744 | 79 | |
d62a17ae | 80 | /* Command input error buffer */ |
7ab57d19 | 81 | struct list *error; |
5689fe5f | 82 | |
d62a17ae | 83 | /* Command cursor point */ |
84 | int cp; | |
718e3744 | 85 | |
d62a17ae | 86 | /* Command length */ |
87 | int length; | |
718e3744 | 88 | |
d62a17ae | 89 | /* Command max length. */ |
90 | int max; | |
718e3744 | 91 | |
d62a17ae | 92 | /* Histry of command */ |
93 | char *hist[VTY_MAXHIST]; | |
718e3744 | 94 | |
d62a17ae | 95 | /* History lookup current point */ |
96 | int hp; | |
718e3744 | 97 | |
d62a17ae | 98 | /* History insert end point */ |
99 | int hindex; | |
718e3744 | 100 | |
1c2facd1 RW |
101 | /* XPath of the current node */ |
102 | int xpath_index; | |
103 | char xpath[VTY_MAXDEPTH][XPATH_MAXLEN]; | |
104 | ||
105 | /* Private candidate configuration mode. */ | |
106 | bool private_config; | |
107 | ||
108 | /* Candidate configuration. */ | |
109 | struct nb_config *candidate_config; | |
110 | ||
111 | /* Base candidate configuration. */ | |
112 | struct nb_config *candidate_config_base; | |
113 | ||
d62a17ae | 114 | /* qobj object ID (replacement for "index") */ |
115 | uint64_t qobj_index; | |
718e3744 | 116 | |
d62a17ae | 117 | /* qobj second-level object ID (replacement for "index_sub") */ |
118 | uint64_t qobj_index_sub; | |
718e3744 | 119 | |
d62a17ae | 120 | /* For escape character. */ |
121 | unsigned char escape; | |
718e3744 | 122 | |
d62a17ae | 123 | /* Current vty status. */ |
124 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; | |
718e3744 | 125 | |
d62a17ae | 126 | /* IAC handling: was the last character received the |
127 | IAC (interpret-as-command) escape character (and therefore the next | |
128 | character will be the command code)? Refer to Telnet RFC 854. */ | |
129 | unsigned char iac; | |
718e3744 | 130 | |
d62a17ae | 131 | /* IAC SB (option subnegotiation) handling */ |
132 | unsigned char iac_sb_in_progress; | |
133 | /* At the moment, we care only about the NAWS (window size) negotiation, | |
134 | and that requires just a 5-character buffer (RFC 1073): | |
135 | <NAWS char> <16-bit width> <16-bit height> */ | |
9fc7ebf1 | 136 | #define TELNET_NAWS_SB_LEN 5 |
d62a17ae | 137 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
138 | /* How many subnegotiation characters have we received? We just drop | |
139 | those that do not fit in the buffer. */ | |
140 | size_t sb_len; | |
718e3744 | 141 | |
d62a17ae | 142 | /* Window width/height. */ |
143 | int width; | |
144 | int height; | |
718e3744 | 145 | |
d62a17ae | 146 | /* Configure lines. */ |
147 | int lines; | |
718e3744 | 148 | |
d62a17ae | 149 | /* Terminal monitor. */ |
150 | int monitor; | |
718e3744 | 151 | |
d62a17ae | 152 | /* In configure mode. */ |
153 | int config; | |
718e3744 | 154 | |
d62a17ae | 155 | /* Read and write thread. */ |
156 | struct thread *t_read; | |
157 | struct thread *t_write; | |
718e3744 | 158 | |
d62a17ae | 159 | /* Timeout seconds and thread. */ |
160 | unsigned long v_timeout; | |
161 | struct thread *t_timeout; | |
d227617a | 162 | |
d62a17ae | 163 | /* What address is this vty comming from. */ |
164 | char address[SU_ADDRSTRLEN]; | |
2071aa0e DL |
165 | |
166 | /* "frame" output. This is buffered and will be printed if some | |
167 | * actual output follows, or will be discarded if the frame ends | |
168 | * without any output. */ | |
169 | size_t frame_pos; | |
170 | char frame[1024]; | |
718e3744 | 171 | }; |
172 | ||
d62a17ae | 173 | static inline void vty_push_context(struct vty *vty, int node, uint64_t id) |
0878c8d4 | 174 | { |
d62a17ae | 175 | vty->node = node; |
176 | vty->qobj_index = id; | |
0878c8d4 DL |
177 | } |
178 | ||
3c5070be DL |
179 | /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to |
180 | * dereference "NULL->qobj_node.nid" */ | |
d62a17ae | 181 | #define VTY_PUSH_CONTEXT(nodeval, ptr) \ |
a50b7ceb | 182 | vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr)) |
d62a17ae | 183 | #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL) |
184 | #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \ | |
185 | do { \ | |
186 | vty->node = nodeval; \ | |
187 | /* qobj_index stays untouched */ \ | |
188 | vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \ | |
6a098b3a | 189 | } while (0) |
0878c8d4 DL |
190 | |
191 | /* can return NULL if context is invalid! */ | |
d62a17ae | 192 | #define VTY_GET_CONTEXT(structname) \ |
0878c8d4 | 193 | QOBJ_GET_TYPESAFE(vty->qobj_index, structname) |
d62a17ae | 194 | #define VTY_GET_CONTEXT_SUB(structname) \ |
6a098b3a | 195 | QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname) |
0878c8d4 DL |
196 | |
197 | /* will return if ptr is NULL. */ | |
d62a17ae | 198 | #define VTY_CHECK_CONTEXT(ptr) \ |
199 | if (!ptr) { \ | |
200 | vty_out(vty, \ | |
201 | "Current configuration object was deleted " \ | |
202 | "by another process.\n"); \ | |
203 | return CMD_WARNING; \ | |
0878c8d4 DL |
204 | } |
205 | ||
206 | /* struct structname *ptr = <context>; ptr will never be NULL. */ | |
d62a17ae | 207 | #define VTY_DECLVAR_CONTEXT(structname, ptr) \ |
208 | struct structname *ptr = VTY_GET_CONTEXT(structname); \ | |
0878c8d4 | 209 | VTY_CHECK_CONTEXT(ptr); |
d62a17ae | 210 | #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \ |
211 | struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \ | |
6a098b3a | 212 | VTY_CHECK_CONTEXT(ptr); |
a3d826f0 | 213 | #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \ |
996c9314 LB |
214 | if (vty->qobj_index == 0) \ |
215 | return CMD_NOT_MY_INSTANCE; \ | |
a3d826f0 CS |
216 | struct structname *ptr = VTY_GET_CONTEXT(structname); \ |
217 | VTY_CHECK_CONTEXT(ptr); | |
0878c8d4 | 218 | |
1c2facd1 RW |
219 | /* XPath macros. */ |
220 | #define VTY_PUSH_XPATH(nodeval, value) \ | |
221 | do { \ | |
222 | if (vty->xpath_index >= VTY_MAXDEPTH) { \ | |
223 | vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \ | |
224 | VTY_MAXDEPTH); \ | |
225 | return CMD_WARNING; \ | |
226 | } \ | |
227 | vty->node = nodeval; \ | |
228 | strlcpy(vty->xpath[vty->xpath_index], value, \ | |
229 | sizeof(vty->xpath[0])); \ | |
230 | vty->xpath_index++; \ | |
231 | } while (0) | |
232 | ||
233 | #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1] | |
234 | ||
235 | #define VTY_CHECK_XPATH \ | |
236 | do { \ | |
237 | if (vty->xpath_index > 0 \ | |
238 | && !yang_dnode_exists(vty->candidate_config->dnode, \ | |
239 | VTY_CURR_XPATH)) { \ | |
240 | vty_out(vty, \ | |
241 | "Current configuration object was deleted " \ | |
242 | "by another process.\n\n"); \ | |
243 | return CMD_WARNING; \ | |
244 | } \ | |
245 | } while (0) | |
246 | ||
d62a17ae | 247 | struct vty_arg { |
248 | const char *name; | |
249 | const char *value; | |
250 | const char **argv; | |
251 | int argc; | |
eac6e3f0 RW |
252 | }; |
253 | ||
718e3744 | 254 | /* Integrated configuration file. */ |
e20dc2ba | 255 | #define INTEGRATE_DEFAULT_CONFIG "frr.conf" |
718e3744 | 256 | |
718e3744 | 257 | /* Default time out value */ |
258 | #define VTY_TIMEOUT_DEFAULT 600 | |
259 | ||
260 | /* Vty read buffer size. */ | |
261 | #define VTY_READ_BUFSIZ 512 | |
262 | ||
263 | /* Directory separator. */ | |
264 | #ifndef DIRECTORY_SEP | |
265 | #define DIRECTORY_SEP '/' | |
266 | #endif /* DIRECTORY_SEP */ | |
267 | ||
268 | #ifndef IS_DIRECTORY_SEP | |
269 | #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP) | |
270 | #endif | |
271 | ||
718e3744 | 272 | /* Exported variables */ |
273 | extern char integrate_default[]; | |
1c2facd1 | 274 | extern struct vty *vty_exclusive_lock; |
718e3744 | 275 | |
276 | /* Prototypes. */ | |
d62a17ae | 277 | extern void vty_init(struct thread_master *); |
278 | extern void vty_init_vtysh(void); | |
279 | extern void vty_terminate(void); | |
280 | extern void vty_reset(void); | |
281 | extern struct vty *vty_new(void); | |
154b9e8f | 282 | extern struct vty *vty_stdio(void (*atclose)(int isexit)); |
2071aa0e DL |
283 | |
284 | /* - vty_frame() output goes to a buffer (for context-begin markers) | |
285 | * - vty_out() will first print this buffer, and clear it | |
286 | * - vty_endframe() clears the buffer without printing it, and prints an | |
287 | * extra string if the buffer was empty before (for context-end markers) | |
288 | */ | |
d62a17ae | 289 | extern int vty_out(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); |
2071aa0e DL |
290 | extern void vty_frame(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); |
291 | extern void vty_endframe(struct vty *, const char *); | |
fe6b47b9 | 292 | bool vty_set_include(struct vty *vty, const char *regexp); |
2071aa0e | 293 | |
1c2facd1 RW |
294 | extern bool vty_read_config(struct nb_config *config, const char *config_file, |
295 | char *config_default_dir); | |
d62a17ae | 296 | extern void vty_time_print(struct vty *, int); |
297 | extern void vty_serv_sock(const char *, unsigned short, const char *); | |
298 | extern void vty_close(struct vty *); | |
299 | extern char *vty_get_cwd(void); | |
300 | extern void vty_log(const char *level, const char *proto, const char *fmt, | |
301 | struct timestamp_control *, va_list); | |
302 | extern int vty_config_lock(struct vty *); | |
303 | extern int vty_config_unlock(struct vty *); | |
304 | extern void vty_config_lockless(void); | |
1c2facd1 RW |
305 | extern int vty_config_exclusive_lock(struct vty *vty); |
306 | extern void vty_config_exclusive_unlock(struct vty *vty); | |
d62a17ae | 307 | extern int vty_shell(struct vty *); |
308 | extern int vty_shell_serv(struct vty *); | |
309 | extern void vty_hello(struct vty *); | |
718e3744 | 310 | |
154b9e8f DL |
311 | /* ^Z / SIGTSTP handling */ |
312 | extern void vty_stdio_suspend(void); | |
313 | extern void vty_stdio_resume(void); | |
314 | extern void vty_stdio_close(void); | |
315 | ||
274a4a44 | 316 | /* Send a fixed-size message to all vty terminal monitors; this should be |
317 | an async-signal-safe function. */ | |
d62a17ae | 318 | extern void vty_log_fixed(char *buf, size_t len); |
274a4a44 | 319 | |
718e3744 | 320 | #endif /* _ZEBRA_VTY_H */ |