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