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