]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / lib / vty.h
1 /* Virtual terminal [aka TeletYpe] interface routine
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 */
20
21 #ifndef _ZEBRA_VTY_H
22 #define _ZEBRA_VTY_H
23
24 #include <sys/types.h>
25 #include <regex.h>
26
27 #include "thread.h"
28 #include "log.h"
29 #include "sockunion.h"
30 #include "qobj.h"
31 #include "compiler.h"
32 #include "northbound.h"
33
34 #define VTY_BUFSIZ 4096
35 #define VTY_MAXHIST 20
36 #define VTY_MAXDEPTH 8
37
38 #define VTY_MAXCFGCHANGES 8
39
40 struct vty_error {
41 char error_buf[VTY_BUFSIZ];
42 uint32_t line_num;
43 };
44
45 struct vty_cfg_change {
46 char xpath[XPATH_MAXLEN];
47 enum nb_operation operation;
48 const char *value;
49 };
50
51 /* VTY struct. */
52 struct vty {
53 /* File descripter of this vty. */
54 int fd;
55
56 /* output FD, to support stdin/stdout combination */
57 int wfd;
58
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
66 /* Is this vty connect to file or not */
67 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
68
69 /* Node status of this vty */
70 int node;
71
72 /* Failure count */
73 int fail;
74
75 /* Output filer regex */
76 bool filter;
77 regex_t include;
78
79 /* Line buffer */
80 struct buffer *lbuf;
81
82 /* Output buffer. */
83 struct buffer *obuf;
84
85 /* Command input buffer */
86 char *buf;
87
88 /* Command input error buffer */
89 struct list *error;
90
91 /* Command cursor point */
92 int cp;
93
94 /* Command length */
95 int length;
96
97 /* Command max length. */
98 int max;
99
100 /* Histry of command */
101 char *hist[VTY_MAXHIST];
102
103 /* History lookup current point */
104 int hp;
105
106 /* History insert end point */
107 int hindex;
108
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
113 /* XPath of the current node */
114 int xpath_index;
115 char xpath[VTY_MAXDEPTH][XPATH_MAXLEN];
116
117 /* In configure mode. */
118 bool config;
119
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
129 /* Confirmed-commit timeout and rollback configuration. */
130 struct thread *t_confirmed_commit_timeout;
131 struct nb_config *confirmed_commit_rollback;
132
133 /* qobj object ID (replacement for "index") */
134 uint64_t qobj_index;
135
136 /* qobj second-level object ID (replacement for "index_sub") */
137 uint64_t qobj_index_sub;
138
139 /* For escape character. */
140 unsigned char escape;
141
142 /* Current vty status. */
143 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
144
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;
149
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> */
155 #define TELNET_NAWS_SB_LEN 5
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;
160
161 /* Window width/height. */
162 int width;
163 int height;
164
165 /* Configure lines. */
166 int lines;
167
168 /* Terminal monitor. */
169 int monitor;
170
171 /* Read and write thread. */
172 struct thread *t_read;
173 struct thread *t_write;
174
175 /* Timeout seconds and thread. */
176 unsigned long v_timeout;
177 struct thread *t_timeout;
178
179 /* What address is this vty comming from. */
180 char address[SU_ADDRSTRLEN];
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];
187 };
188
189 static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
190 {
191 vty->node = node;
192 vty->qobj_index = id;
193 }
194
195 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
196 * dereference "NULL->qobj_node.nid" */
197 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
198 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
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); \
205 } while (0)
206
207 /* can return NULL if context is invalid! */
208 #define VTY_GET_CONTEXT(structname) \
209 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
210 #define VTY_GET_CONTEXT_SUB(structname) \
211 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
212
213 /* will return if ptr is NULL. */
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; \
220 }
221
222 /* struct structname *ptr = <context>; ptr will never be NULL. */
223 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
224 struct structname *ptr = VTY_GET_CONTEXT(structname); \
225 VTY_CHECK_CONTEXT(ptr);
226 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
227 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
228 VTY_CHECK_CONTEXT(ptr);
229 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
230 if (vty->qobj_index == 0) \
231 return CMD_NOT_MY_INSTANCE; \
232 struct structname *ptr = VTY_GET_CONTEXT(structname); \
233 VTY_CHECK_CONTEXT(ptr);
234
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
263 struct vty_arg {
264 const char *name;
265 const char *value;
266 const char **argv;
267 int argc;
268 };
269
270 /* Integrated configuration file. */
271 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
272
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
288 /* Exported variables */
289 extern char integrate_default[];
290 extern struct vty *vty_exclusive_lock;
291
292 /* Prototypes. */
293 extern void vty_init(struct thread_master *);
294 extern void vty_init_vtysh(void);
295 extern void vty_terminate(void);
296 extern void vty_reset(void);
297 extern struct vty *vty_new(void);
298 extern struct vty *vty_stdio(void (*atclose)(int isexit));
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 */
305 extern int vty_out(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
306 extern void vty_frame(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
307 extern void vty_endframe(struct vty *, const char *);
308 bool vty_set_include(struct vty *vty, const char *regexp);
309
310 extern bool vty_read_config(struct nb_config *config, const char *config_file,
311 char *config_default_dir);
312 extern void vty_time_print(struct vty *, int);
313 extern void vty_serv_sock(const char *, unsigned short, const char *);
314 extern void vty_close(struct vty *);
315 extern char *vty_get_cwd(void);
316 extern void vty_log(const char *level, const char *proto, const char *fmt,
317 struct timestamp_control *, va_list);
318 extern int vty_config_enter(struct vty *vty, bool private_config,
319 bool exclusive);
320 extern void vty_config_exit(struct vty *);
321 extern int vty_config_exclusive_lock(struct vty *vty);
322 extern void vty_config_exclusive_unlock(struct vty *vty);
323 extern int vty_shell(struct vty *);
324 extern int vty_shell_serv(struct vty *);
325 extern void vty_hello(struct vty *);
326
327 /* ^Z / SIGTSTP handling */
328 extern void vty_stdio_suspend(void);
329 extern void vty_stdio_resume(void);
330 extern void vty_stdio_close(void);
331
332 /* Send a fixed-size message to all vty terminal monitors; this should be
333 an async-signal-safe function. */
334 extern void vty_log_fixed(char *buf, size_t len);
335
336 #endif /* _ZEBRA_VTY_H */