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