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