1 /* Virtual terminal [aka TeletYpe] interface routine
2 * Copyright (C) 1997 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
24 #include <sys/types.h>
25 #ifdef HAVE_LIBPCREPOSIX
26 #include <pcreposix.h>
29 #endif /* HAVE_LIBPCREPOSIX */
33 #include "sockunion.h"
36 #include "northbound.h"
37 #include "zlog_live.h"
45 #define VTY_BUFSIZ 4096
46 #define VTY_MAXHIST 20
47 #define VTY_MAXDEPTH 8
49 #define VTY_MAXCFGCHANGES 16
52 char error_buf
[VTY_BUFSIZ
];
56 struct vty_cfg_change
{
57 char xpath
[XPATH_MAXLEN
];
58 enum nb_operation operation
;
68 /* File descripter of this vty. */
71 /* output FD, to support stdin/stdout combination */
74 /* File output, used for VTYSH only */
78 /* whether we are using pager or not */
81 /* Is this vty connect to file or not */
82 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
84 /* Node status of this vty */
90 /* Output filer regex */
100 /* Command input buffer */
103 /* Command input error buffer */
106 /* Command cursor point */
112 /* Command max length. */
115 /* Histry of command */
116 char *hist
[VTY_MAXHIST
];
118 /* History lookup current point */
121 /* History insert end point */
124 /* Changes enqueued to be applied in the candidate configuration. */
125 size_t num_cfg_changes
;
126 struct vty_cfg_change cfg_changes
[VTY_MAXCFGCHANGES
];
128 /* XPath of the current node */
130 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
132 /* In configure mode. */
135 /* Private candidate configuration mode. */
138 /* Candidate configuration. */
139 struct nb_config
*candidate_config
;
141 /* Base candidate configuration. */
142 struct nb_config
*candidate_config_base
;
144 /* Dynamic transaction information. */
145 bool pending_allowed
;
147 char *pending_cmds_buf
;
148 size_t pending_cmds_buflen
;
149 size_t pending_cmds_bufpos
;
151 /* Confirmed-commit timeout and rollback configuration. */
152 struct thread
*t_confirmed_commit_timeout
;
153 struct nb_config
*confirmed_commit_rollback
;
155 /* qobj object ID (replacement for "index") */
158 /* qobj second-level object ID (replacement for "index_sub") */
159 uint64_t qobj_index_sub
;
161 /* For escape character. */
162 unsigned char escape
;
164 /* Current vty status. */
173 /* vtysh socket/fd passing (for terminal monitor) */
176 /* CLI command return value (likely CMD_SUCCESS) when pass_fd != -1 */
177 uint8_t pass_fd_status
[4];
179 /* live logging target / terminal monitor */
180 struct zlog_live_cfg live_log
;
182 /* IAC handling: was the last character received the
183 IAC (interpret-as-command) escape character (and therefore the next
184 character will be the command code)? Refer to Telnet RFC 854. */
187 /* IAC SB (option subnegotiation) handling */
188 unsigned char iac_sb_in_progress
;
189 /* At the moment, we care only about the NAWS (window size) negotiation,
190 and that requires just a 5-character buffer (RFC 1073):
191 <NAWS char> <16-bit width> <16-bit height> */
192 #define TELNET_NAWS_SB_LEN 5
193 unsigned char sb_buf
[TELNET_NAWS_SB_LEN
];
194 /* How many subnegotiation characters have we received? We just drop
195 those that do not fit in the buffer. */
198 /* Window width/height. */
202 /* Configure lines. */
205 /* Read and write thread. */
206 struct thread
*t_read
;
207 struct thread
*t_write
;
209 /* Timeout seconds and thread. */
210 unsigned long v_timeout
;
211 struct thread
*t_timeout
;
213 /* What address is this vty comming from. */
214 char address
[SU_ADDRSTRLEN
];
216 /* "frame" output. This is buffered and will be printed if some
217 * actual output follows, or will be discarded if the frame ends
218 * without any output. */
223 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
226 vty
->qobj_index
= id
;
229 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
230 * dereference "NULL->qobj_node.nid" */
231 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
232 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
233 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
234 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
236 vty->node = nodeval; \
237 /* qobj_index stays untouched */ \
238 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
241 /* can return NULL if context is invalid! */
242 #define VTY_GET_CONTEXT(structname) \
243 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
244 #define VTY_GET_CONTEXT_SUB(structname) \
245 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
247 /* will return if ptr is NULL. */
248 #define VTY_CHECK_CONTEXT(ptr) \
251 "Current configuration object was deleted " \
252 "by another process.\n"); \
253 return CMD_WARNING; \
256 /* struct structname *ptr = <context>; ptr will never be NULL. */
257 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
258 struct structname *ptr = VTY_GET_CONTEXT(structname); \
259 VTY_CHECK_CONTEXT(ptr);
260 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
261 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
262 VTY_CHECK_CONTEXT(ptr);
263 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
264 if (vty->qobj_index == 0) \
265 return CMD_NOT_MY_INSTANCE; \
266 struct structname *ptr = VTY_GET_CONTEXT(structname); \
267 VTY_CHECK_CONTEXT(ptr);
270 #define VTY_PUSH_XPATH(nodeval, value) \
272 if (vty->xpath_index >= VTY_MAXDEPTH) { \
273 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
275 return CMD_WARNING; \
277 vty->node = nodeval; \
278 strlcpy(vty->xpath[vty->xpath_index], value, \
279 sizeof(vty->xpath[0])); \
280 vty->xpath_index++; \
283 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
285 #define VTY_CHECK_XPATH \
287 if (vty->type != VTY_FILE && !vty->private_config \
288 && vty->xpath_index > 0 \
289 && !yang_dnode_exists(vty->candidate_config->dnode, \
292 "Current configuration object was deleted " \
293 "by another process.\n\n"); \
294 return CMD_WARNING; \
305 /* Integrated configuration file. */
306 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
308 /* Default time out value */
309 #define VTY_TIMEOUT_DEFAULT 600
311 /* Vty read buffer size. */
312 #define VTY_READ_BUFSIZ 512
314 /* Directory separator. */
315 #ifndef DIRECTORY_SEP
316 #define DIRECTORY_SEP '/'
317 #endif /* DIRECTORY_SEP */
319 #ifndef IS_DIRECTORY_SEP
320 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
324 extern void vty_init(struct thread_master
*, bool do_command_logging
);
325 extern void vty_init_vtysh(void);
326 extern void vty_terminate(void);
327 extern void vty_reset(void);
328 extern struct vty
*vty_new(void);
329 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
331 /* - vty_frame() output goes to a buffer (for context-begin markers)
332 * - vty_out() will first print this buffer, and clear it
333 * - vty_endframe() clears the buffer without printing it, and prints an
334 * extra string if the buffer was empty before (for context-end markers)
336 extern int vty_out(struct vty
*, const char *, ...) PRINTFRR(2, 3);
337 extern void vty_frame(struct vty
*, const char *, ...) PRINTFRR(2, 3);
338 extern void vty_endframe(struct vty
*, const char *);
339 extern bool vty_set_include(struct vty
*vty
, const char *regexp
);
340 /* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)"
341 * NULL check and json_object_free() is included.
343 extern int vty_json(struct vty
*vty
, struct json_object
*json
);
345 /* post fd to be passed to the vtysh client
346 * fd is owned by the VTY code after this and will be closed when done
348 extern void vty_pass_fd(struct vty
*vty
, int fd
);
350 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
351 char *config_default_dir
);
352 extern void vty_time_print(struct vty
*, int);
353 extern void vty_serv_sock(const char *, unsigned short, const char *);
354 extern void vty_close(struct vty
*);
355 extern char *vty_get_cwd(void);
356 extern void vty_update_xpath(const char *oldpath
, const char *newpath
);
357 extern int vty_config_enter(struct vty
*vty
, bool private_config
,
359 extern void vty_config_exit(struct vty
*);
360 extern int vty_config_node_exit(struct vty
*);
361 extern int vty_shell(struct vty
*);
362 extern int vty_shell_serv(struct vty
*);
363 extern void vty_hello(struct vty
*);
365 /* ^Z / SIGTSTP handling */
366 extern void vty_stdio_suspend(void);
367 extern void vty_stdio_resume(void);
368 extern void vty_stdio_close(void);
374 #endif /* _ZEBRA_VTY_H */