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