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->xpath_index > 0 \
258 && !yang_dnode_exists(vty->candidate_config->dnode, \
261 "Current configuration object was deleted " \
262 "by another process.\n\n"); \
263 return CMD_WARNING; \
274 /* Integrated configuration file. */
275 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
277 /* Default time out value */
278 #define VTY_TIMEOUT_DEFAULT 600
280 /* Vty read buffer size. */
281 #define VTY_READ_BUFSIZ 512
283 /* Directory separator. */
284 #ifndef DIRECTORY_SEP
285 #define DIRECTORY_SEP '/'
286 #endif /* DIRECTORY_SEP */
288 #ifndef IS_DIRECTORY_SEP
289 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
292 /* Exported variables */
293 extern struct vty
*vty_exclusive_lock
;
296 extern void vty_init(struct thread_master
*);
297 extern void vty_init_vtysh(void);
298 extern void vty_terminate(void);
299 extern void vty_reset(void);
300 extern struct vty
*vty_new(void);
301 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
303 /* - vty_frame() output goes to a buffer (for context-begin markers)
304 * - vty_out() will first print this buffer, and clear it
305 * - vty_endframe() clears the buffer without printing it, and prints an
306 * extra string if the buffer was empty before (for context-end markers)
308 extern int vty_out(struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
309 extern void vty_frame(struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
310 extern void vty_endframe(struct vty
*, const char *);
311 bool vty_set_include(struct vty
*vty
, const char *regexp
);
313 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
314 char *config_default_dir
);
315 extern void vty_time_print(struct vty
*, int);
316 extern void vty_serv_sock(const char *, unsigned short, const char *);
317 extern void vty_close(struct vty
*);
318 extern char *vty_get_cwd(void);
319 extern void vty_log(const char *level
, const char *proto
, const char *fmt
,
320 struct timestamp_control
*, va_list);
321 extern int vty_config_enter(struct vty
*vty
, bool private_config
,
323 extern void vty_config_exit(struct vty
*);
324 extern int vty_config_exclusive_lock(struct vty
*vty
);
325 extern void vty_config_exclusive_unlock(struct vty
*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 */