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