]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
Merge pull request #10447 from ton31337/fix/json_with_whitespaces
[mirror_frr.git] / lib / vty.h
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 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 */
20
21 #ifndef _ZEBRA_VTY_H
22 #define _ZEBRA_VTY_H
23
24 #include <sys/types.h>
25 #ifdef HAVE_LIBPCREPOSIX
26 #include <pcreposix.h>
27 #else
28 #include <regex.h>
29 #endif /* HAVE_LIBPCREPOSIX */
30
31 #include "thread.h"
32 #include "log.h"
33 #include "sockunion.h"
34 #include "qobj.h"
35 #include "compiler.h"
36 #include "northbound.h"
37 #include "zlog_live.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 struct json_object;
44
45 #define VTY_BUFSIZ 4096
46 #define VTY_MAXHIST 20
47 #define VTY_MAXDEPTH 8
48
49 #define VTY_MAXCFGCHANGES 16
50
51 struct vty_error {
52 char error_buf[VTY_BUFSIZ];
53 uint32_t line_num;
54 };
55
56 struct vty_cfg_change {
57 char xpath[XPATH_MAXLEN];
58 enum nb_operation operation;
59 const char *value;
60 };
61
62 PREDECL_DLIST(vtys);
63
64 /* VTY struct. */
65 struct vty {
66 struct vtys_item itm;
67
68 /* File descripter of this vty. */
69 int fd;
70
71 /* output FD, to support stdin/stdout combination */
72 int wfd;
73
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
81 /* Is this vty connect to file or not */
82 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
83
84 /* Node status of this vty */
85 int node;
86
87 /* Failure count */
88 int fail;
89
90 /* Output filer regex */
91 bool filter;
92 regex_t include;
93
94 /* Line buffer */
95 struct buffer *lbuf;
96
97 /* Output buffer. */
98 struct buffer *obuf;
99
100 /* Command input buffer */
101 char *buf;
102
103 /* Command input error buffer */
104 struct list *error;
105
106 /* Command cursor point */
107 int cp;
108
109 /* Command length */
110 int length;
111
112 /* Command max length. */
113 int max;
114
115 /* Histry of command */
116 char *hist[VTY_MAXHIST];
117
118 /* History lookup current point */
119 int hp;
120
121 /* History insert end point */
122 int hindex;
123
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
128 /* XPath of the current node */
129 int xpath_index;
130 char xpath[VTY_MAXDEPTH][XPATH_MAXLEN];
131
132 /* In configure mode. */
133 bool config;
134
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
144 /* Dynamic transaction information. */
145 bool pending_allowed;
146 bool pending_commit;
147 char *pending_cmds_buf;
148 size_t pending_cmds_buflen;
149 size_t pending_cmds_bufpos;
150
151 /* Confirmed-commit timeout and rollback configuration. */
152 struct thread *t_confirmed_commit_timeout;
153 struct nb_config *confirmed_commit_rollback;
154
155 /* qobj object ID (replacement for "index") */
156 uint64_t qobj_index;
157
158 /* qobj second-level object ID (replacement for "index_sub") */
159 uint64_t qobj_index_sub;
160
161 /* For escape character. */
162 unsigned char escape;
163
164 /* Current vty status. */
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];
178
179 /* live logging target / terminal monitor */
180 struct zlog_live_cfg live_log;
181
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;
186
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> */
192 #define TELNET_NAWS_SB_LEN 5
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;
197
198 /* Window width/height. */
199 int width;
200 int height;
201
202 /* Configure lines. */
203 int lines;
204
205 /* Read and write thread. */
206 struct thread *t_read;
207 struct thread *t_write;
208
209 /* Timeout seconds and thread. */
210 unsigned long v_timeout;
211 struct thread *t_timeout;
212
213 /* What address is this vty comming from. */
214 char address[SU_ADDRSTRLEN];
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];
221 };
222
223 static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
224 {
225 vty->node = node;
226 vty->qobj_index = id;
227 }
228
229 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
230 * dereference "NULL->qobj_node.nid" */
231 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
232 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
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); \
239 } while (0)
240
241 /* can return NULL if context is invalid! */
242 #define VTY_GET_CONTEXT(structname) \
243 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
244 #define VTY_GET_CONTEXT_SUB(structname) \
245 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
246
247 /* will return if ptr is NULL. */
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; \
254 }
255
256 /* struct structname *ptr = <context>; ptr will never be NULL. */
257 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
258 struct structname *ptr = VTY_GET_CONTEXT(structname); \
259 VTY_CHECK_CONTEXT(ptr);
260 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
261 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
262 VTY_CHECK_CONTEXT(ptr);
263 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
264 if (vty->qobj_index == 0) \
265 return CMD_NOT_MY_INSTANCE; \
266 struct structname *ptr = VTY_GET_CONTEXT(structname); \
267 VTY_CHECK_CONTEXT(ptr);
268
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 { \
287 if (vty->type != VTY_FILE && !vty->private_config \
288 && vty->xpath_index > 0 \
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
298 struct vty_arg {
299 const char *name;
300 const char *value;
301 const char **argv;
302 int argc;
303 };
304
305 /* Integrated configuration file. */
306 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
307
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
323 /* Prototypes. */
324 extern void vty_init(struct thread_master *, bool do_command_logging);
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);
329 extern struct vty *vty_stdio(void (*atclose)(int isexit));
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 */
336 extern int vty_out(struct vty *, const char *, ...) PRINTFRR(2, 3);
337 extern void vty_frame(struct vty *, const char *, ...) PRINTFRR(2, 3);
338 extern void vty_endframe(struct vty *, const char *);
339 extern bool vty_set_include(struct vty *vty, const char *regexp);
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);
344
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
350 extern bool vty_read_config(struct nb_config *config, const char *config_file,
351 char *config_default_dir);
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);
356 extern void vty_update_xpath(const char *oldpath, const char *newpath);
357 extern int vty_config_enter(struct vty *vty, bool private_config,
358 bool exclusive);
359 extern void vty_config_exit(struct vty *);
360 extern int vty_config_node_exit(struct vty *);
361 extern int vty_shell(struct vty *);
362 extern int vty_shell_serv(struct vty *);
363 extern void vty_hello(struct vty *);
364
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
370 #ifdef __cplusplus
371 }
372 #endif
373
374 #endif /* _ZEBRA_VTY_H */