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"
34 #define VTY_BUFSIZ 4096
35 #define VTY_MAXHIST 20
36 #define VTY_MAXDEPTH 8
38 #define VTY_MAXCFGCHANGES 8
41 char error_buf
[VTY_BUFSIZ
];
45 struct vty_cfg_change
{
46 char xpath
[XPATH_MAXLEN
];
47 enum nb_operation operation
;
53 /* File descripter of this vty. */
56 /* output FD, to support stdin/stdout combination */
59 /* File output, used for VTYSH only */
63 /* whether we are using pager or not */
66 /* Is this vty connect to file or not */
67 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
69 /* Node status of this vty */
75 /* Output filer regex */
85 /* Command input buffer */
88 /* Command input error buffer */
91 /* Command cursor point */
97 /* Command max length. */
100 /* Histry of command */
101 char *hist
[VTY_MAXHIST
];
103 /* History lookup current point */
106 /* History insert end point */
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
];
113 /* XPath of the current node */
115 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
117 /* In configure mode. */
120 /* Private candidate configuration mode. */
123 /* Candidate configuration. */
124 struct nb_config
*candidate_config
;
126 /* Base candidate configuration. */
127 struct nb_config
*candidate_config_base
;
129 /* Confirmed-commit timeout and rollback configuration. */
130 struct thread
*t_confirmed_commit_timeout
;
131 struct nb_config
*confirmed_commit_rollback
;
133 /* qobj object ID (replacement for "index") */
136 /* qobj second-level object ID (replacement for "index_sub") */
137 uint64_t qobj_index_sub
;
139 /* For escape character. */
140 unsigned char escape
;
142 /* Current vty status. */
143 enum { VTY_NORMAL
, VTY_CLOSE
, VTY_MORE
, VTY_MORELINE
} status
;
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. */
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. */
161 /* Window width/height. */
165 /* Configure lines. */
168 /* Terminal monitor. */
171 /* Read and write thread. */
172 struct thread
*t_read
;
173 struct thread
*t_write
;
175 /* Timeout seconds and thread. */
176 unsigned long v_timeout
;
177 struct thread
*t_timeout
;
179 /* What address is this vty comming from. */
180 char address
[SU_ADDRSTRLEN
];
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. */
189 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
192 vty
->qobj_index
= id
;
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) \
202 vty->node = nodeval; \
203 /* qobj_index stays untouched */ \
204 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
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)
213 /* will return if ptr is NULL. */
214 #define VTY_CHECK_CONTEXT(ptr) \
217 "Current configuration object was deleted " \
218 "by another process.\n"); \
219 return CMD_WARNING; \
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);
236 #define VTY_PUSH_XPATH(nodeval, value) \
238 if (vty->xpath_index >= VTY_MAXDEPTH) { \
239 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
241 return CMD_WARNING; \
243 vty->node = nodeval; \
244 strlcpy(vty->xpath[vty->xpath_index], value, \
245 sizeof(vty->xpath[0])); \
246 vty->xpath_index++; \
249 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
251 #define VTY_CHECK_XPATH \
253 if (vty->xpath_index > 0 \
254 && !yang_dnode_exists(vty->candidate_config->dnode, \
257 "Current configuration object was deleted " \
258 "by another process.\n\n"); \
259 return CMD_WARNING; \
270 /* Integrated configuration file. */
271 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
273 /* Default time out value */
274 #define VTY_TIMEOUT_DEFAULT 600
276 /* Vty read buffer size. */
277 #define VTY_READ_BUFSIZ 512
279 /* Directory separator. */
280 #ifndef DIRECTORY_SEP
281 #define DIRECTORY_SEP '/'
282 #endif /* DIRECTORY_SEP */
284 #ifndef IS_DIRECTORY_SEP
285 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
288 /* Exported variables */
289 extern char integrate_default
[];
290 extern struct vty
*vty_exclusive_lock
;
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
));
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)
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
);
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
,
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
*);
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);
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
);
336 #endif /* _ZEBRA_VTY_H */