]> git.proxmox.com Git - mirror_frr.git/blame - lib/vty.h
*: introduce DEFPY_YANG & friends
[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
fbdc1c0a
RW
137 /* Confirmed-commit timeout and rollback configuration. */
138 struct thread *t_confirmed_commit_timeout;
139 struct nb_config *confirmed_commit_rollback;
140
d62a17ae 141 /* qobj object ID (replacement for "index") */
142 uint64_t qobj_index;
718e3744 143
d62a17ae 144 /* qobj second-level object ID (replacement for "index_sub") */
145 uint64_t qobj_index_sub;
718e3744 146
d62a17ae 147 /* For escape character. */
148 unsigned char escape;
718e3744 149
d62a17ae 150 /* Current vty status. */
151 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
718e3744 152
d62a17ae 153 /* IAC handling: was the last character received the
154 IAC (interpret-as-command) escape character (and therefore the next
155 character will be the command code)? Refer to Telnet RFC 854. */
156 unsigned char iac;
718e3744 157
d62a17ae 158 /* IAC SB (option subnegotiation) handling */
159 unsigned char iac_sb_in_progress;
160/* At the moment, we care only about the NAWS (window size) negotiation,
161 and that requires just a 5-character buffer (RFC 1073):
162 <NAWS char> <16-bit width> <16-bit height> */
9fc7ebf1 163#define TELNET_NAWS_SB_LEN 5
d62a17ae 164 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
165 /* How many subnegotiation characters have we received? We just drop
166 those that do not fit in the buffer. */
167 size_t sb_len;
718e3744 168
d62a17ae 169 /* Window width/height. */
170 int width;
171 int height;
718e3744 172
d62a17ae 173 /* Configure lines. */
174 int lines;
718e3744 175
d62a17ae 176 /* Terminal monitor. */
177 int monitor;
718e3744 178
d62a17ae 179 /* Read and write thread. */
180 struct thread *t_read;
181 struct thread *t_write;
718e3744 182
d62a17ae 183 /* Timeout seconds and thread. */
184 unsigned long v_timeout;
185 struct thread *t_timeout;
d227617a 186
d62a17ae 187 /* What address is this vty comming from. */
188 char address[SU_ADDRSTRLEN];
2071aa0e
DL
189
190 /* "frame" output. This is buffered and will be printed if some
191 * actual output follows, or will be discarded if the frame ends
192 * without any output. */
193 size_t frame_pos;
194 char frame[1024];
718e3744 195};
196
d62a17ae 197static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
0878c8d4 198{
d62a17ae 199 vty->node = node;
200 vty->qobj_index = id;
0878c8d4
DL
201}
202
3c5070be
DL
203/* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
204 * dereference "NULL->qobj_node.nid" */
d62a17ae 205#define VTY_PUSH_CONTEXT(nodeval, ptr) \
a50b7ceb 206 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
d62a17ae 207#define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
208#define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
209 do { \
210 vty->node = nodeval; \
211 /* qobj_index stays untouched */ \
212 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
6a098b3a 213 } while (0)
0878c8d4
DL
214
215/* can return NULL if context is invalid! */
d62a17ae 216#define VTY_GET_CONTEXT(structname) \
0878c8d4 217 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
d62a17ae 218#define VTY_GET_CONTEXT_SUB(structname) \
6a098b3a 219 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
0878c8d4
DL
220
221/* will return if ptr is NULL. */
d62a17ae 222#define VTY_CHECK_CONTEXT(ptr) \
223 if (!ptr) { \
224 vty_out(vty, \
225 "Current configuration object was deleted " \
226 "by another process.\n"); \
227 return CMD_WARNING; \
0878c8d4
DL
228 }
229
230/* struct structname *ptr = <context>; ptr will never be NULL. */
d62a17ae 231#define VTY_DECLVAR_CONTEXT(structname, ptr) \
232 struct structname *ptr = VTY_GET_CONTEXT(structname); \
0878c8d4 233 VTY_CHECK_CONTEXT(ptr);
d62a17ae 234#define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
235 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
6a098b3a 236 VTY_CHECK_CONTEXT(ptr);
a3d826f0 237#define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
996c9314
LB
238 if (vty->qobj_index == 0) \
239 return CMD_NOT_MY_INSTANCE; \
a3d826f0
CS
240 struct structname *ptr = VTY_GET_CONTEXT(structname); \
241 VTY_CHECK_CONTEXT(ptr);
0878c8d4 242
1c2facd1
RW
243/* XPath macros. */
244#define VTY_PUSH_XPATH(nodeval, value) \
245 do { \
246 if (vty->xpath_index >= VTY_MAXDEPTH) { \
247 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
248 VTY_MAXDEPTH); \
249 return CMD_WARNING; \
250 } \
251 vty->node = nodeval; \
252 strlcpy(vty->xpath[vty->xpath_index], value, \
253 sizeof(vty->xpath[0])); \
254 vty->xpath_index++; \
255 } while (0)
256
257#define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
258
259#define VTY_CHECK_XPATH \
260 do { \
caaa8b9c
RW
261 if (vty->type != VTY_FILE && !vty->private_config \
262 && vty->xpath_index > 0 \
1c2facd1
RW
263 && !yang_dnode_exists(vty->candidate_config->dnode, \
264 VTY_CURR_XPATH)) { \
265 vty_out(vty, \
266 "Current configuration object was deleted " \
267 "by another process.\n\n"); \
268 return CMD_WARNING; \
269 } \
270 } while (0)
271
d62a17ae 272struct vty_arg {
273 const char *name;
274 const char *value;
275 const char **argv;
276 int argc;
eac6e3f0
RW
277};
278
718e3744 279/* Integrated configuration file. */
e20dc2ba 280#define INTEGRATE_DEFAULT_CONFIG "frr.conf"
718e3744 281
718e3744 282/* Default time out value */
283#define VTY_TIMEOUT_DEFAULT 600
284
285/* Vty read buffer size. */
286#define VTY_READ_BUFSIZ 512
287
288/* Directory separator. */
289#ifndef DIRECTORY_SEP
290#define DIRECTORY_SEP '/'
291#endif /* DIRECTORY_SEP */
292
293#ifndef IS_DIRECTORY_SEP
294#define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
295#endif
296
718e3744 297/* Prototypes. */
2950f5da 298extern void vty_init(struct thread_master *, bool do_command_logging);
d62a17ae 299extern void vty_init_vtysh(void);
300extern void vty_terminate(void);
301extern void vty_reset(void);
302extern struct vty *vty_new(void);
154b9e8f 303extern struct vty *vty_stdio(void (*atclose)(int isexit));
2071aa0e
DL
304
305/* - vty_frame() output goes to a buffer (for context-begin markers)
306 * - vty_out() will first print this buffer, and clear it
307 * - vty_endframe() clears the buffer without printing it, and prints an
308 * extra string if the buffer was empty before (for context-end markers)
309 */
afb35622
DL
310extern int vty_out(struct vty *, const char *, ...) PRINTFRR(2, 3);
311extern void vty_frame(struct vty *, const char *, ...) PRINTFRR(2, 3);
2071aa0e 312extern void vty_endframe(struct vty *, const char *);
fe6b47b9 313bool vty_set_include(struct vty *vty, const char *regexp);
2071aa0e 314
1c2facd1
RW
315extern bool vty_read_config(struct nb_config *config, const char *config_file,
316 char *config_default_dir);
d62a17ae 317extern void vty_time_print(struct vty *, int);
318extern void vty_serv_sock(const char *, unsigned short, const char *);
319extern void vty_close(struct vty *);
320extern char *vty_get_cwd(void);
807f5b98
DL
321extern void vty_log(const char *level, const char *proto, const char *msg,
322 struct timestamp_control *);
f344c66e
RW
323extern int vty_config_enter(struct vty *vty, bool private_config,
324 bool exclusive);
325extern void vty_config_exit(struct vty *);
791ded4a 326extern int vty_config_node_exit(struct vty *);
d62a17ae 327extern int vty_shell(struct vty *);
328extern int vty_shell_serv(struct vty *);
329extern void vty_hello(struct vty *);
718e3744 330
154b9e8f
DL
331/* ^Z / SIGTSTP handling */
332extern void vty_stdio_suspend(void);
333extern void vty_stdio_resume(void);
334extern void vty_stdio_close(void);
335
274a4a44 336/* Send a fixed-size message to all vty terminal monitors; this should be
337 an async-signal-safe function. */
d62a17ae 338extern void vty_log_fixed(char *buf, size_t len);
274a4a44 339
5e244469
RW
340#ifdef __cplusplus
341}
342#endif
343
718e3744 344#endif /* _ZEBRA_VTY_H */