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>
29 #include "sockunion.h"
32 #include "northbound.h"
38 #define VTY_BUFSIZ 4096
39 #define VTY_MAXHIST 20
40 #define VTY_MAXDEPTH 8
42 #define VTY_MAXCFGCHANGES 8
45 char error_buf
[VTY_BUFSIZ
];
49 struct vty_cfg_change
{
50 char xpath
[XPATH_MAXLEN
];
51 enum nb_operation operation
;
57 /* File descripter of this vty. */
60 /* output FD, to support stdin/stdout combination */
63 /* File output, used for VTYSH only */
67 /* whether we are using pager or not */
70 /* Is this vty connect to file or not */
71 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
73 /* Node status of this vty */
79 /* Output filer regex */
89 /* Command input buffer */
92 /* Command input error buffer */
95 /* Command cursor point */
101 /* Command max length. */
104 /* Histry of command */
105 char *hist
[VTY_MAXHIST
];
107 /* History lookup current point */
110 /* History insert end point */
113 /* Changes enqueued to be applied in the candidate configuration. */
114 size_t num_cfg_changes
;
115 struct vty_cfg_change cfg_changes
[VTY_MAXCFGCHANGES
];
117 /* XPath of the current node */
119 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
121 /* In configure mode. */
124 /* Private candidate configuration mode. */
127 /* Candidate configuration. */
128 struct nb_config
*candidate_config
;
130 /* Base candidate configuration. */
131 struct nb_config
*candidate_config_base
;
133 /* Confirmed-commit timeout and rollback configuration. */
134 struct thread
*t_confirmed_commit_timeout
;
135 struct nb_config
*confirmed_commit_rollback
;
137 /* qobj object ID (replacement for "index") */
140 /* qobj second-level object ID (replacement for "index_sub") */
141 uint64_t qobj_index_sub
;
143 /* For escape character. */
144 unsigned char escape
;
146 /* Current vty status. */
147 enum { VTY_NORMAL
, VTY_CLOSE
, VTY_MORE
, VTY_MORELINE
} status
;
149 /* IAC handling: was the last character received the
150 IAC (interpret-as-command) escape character (and therefore the next
151 character will be the command code)? Refer to Telnet RFC 854. */
154 /* IAC SB (option subnegotiation) handling */
155 unsigned char iac_sb_in_progress
;
156 /* At the moment, we care only about the NAWS (window size) negotiation,
157 and that requires just a 5-character buffer (RFC 1073):
158 <NAWS char> <16-bit width> <16-bit height> */
159 #define TELNET_NAWS_SB_LEN 5
160 unsigned char sb_buf
[TELNET_NAWS_SB_LEN
];
161 /* How many subnegotiation characters have we received? We just drop
162 those that do not fit in the buffer. */
165 /* Window width/height. */
169 /* Configure lines. */
172 /* Terminal monitor. */
175 /* Read and write thread. */
176 struct thread
*t_read
;
177 struct thread
*t_write
;
179 /* Timeout seconds and thread. */
180 unsigned long v_timeout
;
181 struct thread
*t_timeout
;
183 /* What address is this vty comming from. */
184 char address
[SU_ADDRSTRLEN
];
186 /* "frame" output. This is buffered and will be printed if some
187 * actual output follows, or will be discarded if the frame ends
188 * without any output. */
193 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
196 vty
->qobj_index
= id
;
199 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
200 * dereference "NULL->qobj_node.nid" */
201 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
202 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
203 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
204 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
206 vty->node = nodeval; \
207 /* qobj_index stays untouched */ \
208 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
211 /* can return NULL if context is invalid! */
212 #define VTY_GET_CONTEXT(structname) \
213 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
214 #define VTY_GET_CONTEXT_SUB(structname) \
215 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
217 /* will return if ptr is NULL. */
218 #define VTY_CHECK_CONTEXT(ptr) \
221 "Current configuration object was deleted " \
222 "by another process.\n"); \
223 return CMD_WARNING; \
226 /* struct structname *ptr = <context>; ptr will never be NULL. */
227 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
228 struct structname *ptr = VTY_GET_CONTEXT(structname); \
229 VTY_CHECK_CONTEXT(ptr);
230 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
231 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
232 VTY_CHECK_CONTEXT(ptr);
233 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
234 if (vty->qobj_index == 0) \
235 return CMD_NOT_MY_INSTANCE; \
236 struct structname *ptr = VTY_GET_CONTEXT(structname); \
237 VTY_CHECK_CONTEXT(ptr);
240 #define VTY_PUSH_XPATH(nodeval, value) \
242 if (vty->xpath_index >= VTY_MAXDEPTH) { \
243 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
245 return CMD_WARNING; \
247 vty->node = nodeval; \
248 strlcpy(vty->xpath[vty->xpath_index], value, \
249 sizeof(vty->xpath[0])); \
250 vty->xpath_index++; \
253 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
255 #define VTY_CHECK_XPATH \
257 if (vty->type != VTY_FILE && !vty->private_config \
258 && vty->xpath_index > 0 \
259 && !yang_dnode_exists(vty->candidate_config->dnode, \
262 "Current configuration object was deleted " \
263 "by another process.\n\n"); \
264 return CMD_WARNING; \
275 /* Integrated configuration file. */
276 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
278 /* Default time out value */
279 #define VTY_TIMEOUT_DEFAULT 600
281 /* Vty read buffer size. */
282 #define VTY_READ_BUFSIZ 512
284 /* Directory separator. */
285 #ifndef DIRECTORY_SEP
286 #define DIRECTORY_SEP '/'
287 #endif /* DIRECTORY_SEP */
289 #ifndef IS_DIRECTORY_SEP
290 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
294 extern void vty_init(struct thread_master
*, bool do_command_logging
);
295 extern void vty_init_vtysh(void);
296 extern void vty_terminate(void);
297 extern void vty_reset(void);
298 extern struct vty
*vty_new(void);
299 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
301 /* - vty_frame() output goes to a buffer (for context-begin markers)
302 * - vty_out() will first print this buffer, and clear it
303 * - vty_endframe() clears the buffer without printing it, and prints an
304 * extra string if the buffer was empty before (for context-end markers)
306 extern int vty_out(struct vty
*, const char *, ...) PRINTFRR(2, 3);
307 extern void vty_frame(struct vty
*, const char *, ...) PRINTFRR(2, 3);
308 extern void vty_endframe(struct vty
*, const char *);
309 bool vty_set_include(struct vty
*vty
, const char *regexp
);
311 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
312 char *config_default_dir
);
313 extern void vty_time_print(struct vty
*, int);
314 extern void vty_serv_sock(const char *, unsigned short, const char *);
315 extern void vty_close(struct vty
*);
316 extern char *vty_get_cwd(void);
317 extern void vty_log(const char *level
, const char *proto
, const char *msg
,
318 struct timestamp_control
*);
319 extern int vty_config_enter(struct vty
*vty
, bool private_config
,
321 extern void vty_config_exit(struct vty
*);
322 extern int vty_shell(struct vty
*);
323 extern int vty_shell_serv(struct vty
*);
324 extern void vty_hello(struct vty
*);
326 /* ^Z / SIGTSTP handling */
327 extern void vty_stdio_suspend(void);
328 extern void vty_stdio_resume(void);
329 extern void vty_stdio_close(void);
331 /* Send a fixed-size message to all vty terminal monitors; this should be
332 an async-signal-safe function. */
333 extern void vty_log_fixed(char *buf
, size_t len
);
339 #endif /* _ZEBRA_VTY_H */