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"
42 #define VTY_BUFSIZ 4096
43 #define VTY_MAXHIST 20
44 #define VTY_MAXDEPTH 8
46 #define VTY_MAXCFGCHANGES 8
49 char error_buf
[VTY_BUFSIZ
];
53 struct vty_cfg_change
{
54 char xpath
[XPATH_MAXLEN
];
55 enum nb_operation operation
;
61 /* File descripter of this vty. */
64 /* output FD, to support stdin/stdout combination */
67 /* File output, used for VTYSH only */
71 /* whether we are using pager or not */
74 /* Is this vty connect to file or not */
75 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
77 /* Node status of this vty */
83 /* Output filer regex */
93 /* Command input buffer */
96 /* Command input error buffer */
99 /* Command cursor point */
105 /* Command max length. */
108 /* Histry of command */
109 char *hist
[VTY_MAXHIST
];
111 /* History lookup current point */
114 /* History insert end point */
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
];
121 /* XPath of the current node */
123 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
125 /* In configure mode. */
128 /* Private candidate configuration mode. */
131 /* Candidate configuration. */
132 struct nb_config
*candidate_config
;
134 /* Base candidate configuration. */
135 struct nb_config
*candidate_config_base
;
137 /* Confirmed-commit timeout and rollback configuration. */
138 struct thread
*t_confirmed_commit_timeout
;
139 struct nb_config
*confirmed_commit_rollback
;
141 /* qobj object ID (replacement for "index") */
144 /* qobj second-level object ID (replacement for "index_sub") */
145 uint64_t qobj_index_sub
;
147 /* For escape character. */
148 unsigned char escape
;
150 /* Current vty status. */
151 enum { VTY_NORMAL
, VTY_CLOSE
, VTY_MORE
, VTY_MORELINE
} status
;
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. */
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> */
163 #define TELNET_NAWS_SB_LEN 5
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. */
169 /* Window width/height. */
173 /* Configure lines. */
176 /* Terminal monitor. */
179 /* Read and write thread. */
180 struct thread
*t_read
;
181 struct thread
*t_write
;
183 /* Timeout seconds and thread. */
184 unsigned long v_timeout
;
185 struct thread
*t_timeout
;
187 /* What address is this vty comming from. */
188 char address
[SU_ADDRSTRLEN
];
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. */
197 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
200 vty
->qobj_index
= id
;
203 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
204 * dereference "NULL->qobj_node.nid" */
205 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
206 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
207 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
208 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
210 vty->node = nodeval; \
211 /* qobj_index stays untouched */ \
212 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
215 /* can return NULL if context is invalid! */
216 #define VTY_GET_CONTEXT(structname) \
217 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
218 #define VTY_GET_CONTEXT_SUB(structname) \
219 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
221 /* will return if ptr is NULL. */
222 #define VTY_CHECK_CONTEXT(ptr) \
225 "Current configuration object was deleted " \
226 "by another process.\n"); \
227 return CMD_WARNING; \
230 /* struct structname *ptr = <context>; ptr will never be NULL. */
231 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
232 struct structname *ptr = VTY_GET_CONTEXT(structname); \
233 VTY_CHECK_CONTEXT(ptr);
234 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
235 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
236 VTY_CHECK_CONTEXT(ptr);
237 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
238 if (vty->qobj_index == 0) \
239 return CMD_NOT_MY_INSTANCE; \
240 struct structname *ptr = VTY_GET_CONTEXT(structname); \
241 VTY_CHECK_CONTEXT(ptr);
244 #define VTY_PUSH_XPATH(nodeval, value) \
246 if (vty->xpath_index >= VTY_MAXDEPTH) { \
247 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
249 return CMD_WARNING; \
251 vty->node = nodeval; \
252 strlcpy(vty->xpath[vty->xpath_index], value, \
253 sizeof(vty->xpath[0])); \
254 vty->xpath_index++; \
257 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
259 #define VTY_CHECK_XPATH \
261 if (vty->type != VTY_FILE && !vty->private_config \
262 && vty->xpath_index > 0 \
263 && !yang_dnode_exists(vty->candidate_config->dnode, \
266 "Current configuration object was deleted " \
267 "by another process.\n\n"); \
268 return CMD_WARNING; \
279 /* Integrated configuration file. */
280 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
282 /* Default time out value */
283 #define VTY_TIMEOUT_DEFAULT 600
285 /* Vty read buffer size. */
286 #define VTY_READ_BUFSIZ 512
288 /* Directory separator. */
289 #ifndef DIRECTORY_SEP
290 #define DIRECTORY_SEP '/'
291 #endif /* DIRECTORY_SEP */
293 #ifndef IS_DIRECTORY_SEP
294 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
298 extern void vty_init(struct thread_master
*, bool do_command_logging
);
299 extern void vty_init_vtysh(void);
300 extern void vty_terminate(void);
301 extern void vty_reset(void);
302 extern struct vty
*vty_new(void);
303 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
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)
310 extern int vty_out(struct vty
*, const char *, ...) PRINTFRR(2, 3);
311 extern void vty_frame(struct vty
*, const char *, ...) PRINTFRR(2, 3);
312 extern void vty_endframe(struct vty
*, const char *);
313 bool vty_set_include(struct vty
*vty
, const char *regexp
);
315 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
316 char *config_default_dir
);
317 extern void vty_time_print(struct vty
*, int);
318 extern void vty_serv_sock(const char *, unsigned short, const char *);
319 extern void vty_close(struct vty
*);
320 extern char *vty_get_cwd(void);
321 extern void vty_log(const char *level
, const char *proto
, const char *msg
,
322 struct timestamp_control
*);
323 extern int vty_config_enter(struct vty
*vty
, bool private_config
,
325 extern void vty_config_exit(struct vty
*);
326 extern int vty_shell(struct vty
*);
327 extern int vty_shell_serv(struct vty
*);
328 extern void vty_hello(struct vty
*);
330 /* ^Z / SIGTSTP handling */
331 extern void vty_stdio_suspend(void);
332 extern void vty_stdio_resume(void);
333 extern void vty_stdio_close(void);
335 /* Send a fixed-size message to all vty terminal monitors; this should be
336 an async-signal-safe function. */
337 extern void vty_log_fixed(char *buf
, size_t len
);
343 #endif /* _ZEBRA_VTY_H */