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
{
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 /* qobj object ID (replacement for "index") */
132 /* qobj second-level object ID (replacement for "index_sub") */
133 uint64_t qobj_index_sub
;
135 /* For escape character. */
136 unsigned char escape
;
138 /* Current vty status. */
139 enum { VTY_NORMAL
, VTY_CLOSE
, VTY_MORE
, VTY_MORELINE
} status
;
141 /* IAC handling: was the last character received the
142 IAC (interpret-as-command) escape character (and therefore the next
143 character will be the command code)? Refer to Telnet RFC 854. */
146 /* IAC SB (option subnegotiation) handling */
147 unsigned char iac_sb_in_progress
;
148 /* At the moment, we care only about the NAWS (window size) negotiation,
149 and that requires just a 5-character buffer (RFC 1073):
150 <NAWS char> <16-bit width> <16-bit height> */
151 #define TELNET_NAWS_SB_LEN 5
152 unsigned char sb_buf
[TELNET_NAWS_SB_LEN
];
153 /* How many subnegotiation characters have we received? We just drop
154 those that do not fit in the buffer. */
157 /* Window width/height. */
161 /* Configure lines. */
164 /* Terminal monitor. */
167 /* Read and write thread. */
168 struct thread
*t_read
;
169 struct thread
*t_write
;
171 /* Timeout seconds and thread. */
172 unsigned long v_timeout
;
173 struct thread
*t_timeout
;
175 /* What address is this vty comming from. */
176 char address
[SU_ADDRSTRLEN
];
178 /* "frame" output. This is buffered and will be printed if some
179 * actual output follows, or will be discarded if the frame ends
180 * without any output. */
185 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
188 vty
->qobj_index
= id
;
191 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
192 * dereference "NULL->qobj_node.nid" */
193 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
194 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
195 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
196 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
198 vty->node = nodeval; \
199 /* qobj_index stays untouched */ \
200 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
203 /* can return NULL if context is invalid! */
204 #define VTY_GET_CONTEXT(structname) \
205 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
206 #define VTY_GET_CONTEXT_SUB(structname) \
207 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
209 /* will return if ptr is NULL. */
210 #define VTY_CHECK_CONTEXT(ptr) \
213 "Current configuration object was deleted " \
214 "by another process.\n"); \
215 return CMD_WARNING; \
218 /* struct structname *ptr = <context>; ptr will never be NULL. */
219 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
220 struct structname *ptr = VTY_GET_CONTEXT(structname); \
221 VTY_CHECK_CONTEXT(ptr);
222 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
223 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
224 VTY_CHECK_CONTEXT(ptr);
225 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
226 if (vty->qobj_index == 0) \
227 return CMD_NOT_MY_INSTANCE; \
228 struct structname *ptr = VTY_GET_CONTEXT(structname); \
229 VTY_CHECK_CONTEXT(ptr);
232 #define VTY_PUSH_XPATH(nodeval, value) \
234 if (vty->xpath_index >= VTY_MAXDEPTH) { \
235 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
237 return CMD_WARNING; \
239 vty->node = nodeval; \
240 strlcpy(vty->xpath[vty->xpath_index], value, \
241 sizeof(vty->xpath[0])); \
242 vty->xpath_index++; \
245 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
247 #define VTY_CHECK_XPATH \
249 if (vty->xpath_index > 0 \
250 && !yang_dnode_exists(vty->candidate_config->dnode, \
253 "Current configuration object was deleted " \
254 "by another process.\n\n"); \
255 return CMD_WARNING; \
266 /* Integrated configuration file. */
267 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
269 /* Default time out value */
270 #define VTY_TIMEOUT_DEFAULT 600
272 /* Vty read buffer size. */
273 #define VTY_READ_BUFSIZ 512
275 /* Directory separator. */
276 #ifndef DIRECTORY_SEP
277 #define DIRECTORY_SEP '/'
278 #endif /* DIRECTORY_SEP */
280 #ifndef IS_DIRECTORY_SEP
281 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
284 /* Exported variables */
285 extern char integrate_default
[];
286 extern struct vty
*vty_exclusive_lock
;
289 extern void vty_init(struct thread_master
*);
290 extern void vty_init_vtysh(void);
291 extern void vty_terminate(void);
292 extern void vty_reset(void);
293 extern struct vty
*vty_new(void);
294 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
296 /* - vty_frame() output goes to a buffer (for context-begin markers)
297 * - vty_out() will first print this buffer, and clear it
298 * - vty_endframe() clears the buffer without printing it, and prints an
299 * extra string if the buffer was empty before (for context-end markers)
301 extern int vty_out(struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
302 extern void vty_frame(struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
303 extern void vty_endframe(struct vty
*, const char *);
304 bool vty_set_include(struct vty
*vty
, const char *regexp
);
306 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
307 char *config_default_dir
);
308 extern void vty_time_print(struct vty
*, int);
309 extern void vty_serv_sock(const char *, unsigned short, const char *);
310 extern void vty_close(struct vty
*);
311 extern char *vty_get_cwd(void);
312 extern void vty_log(const char *level
, const char *proto
, const char *fmt
,
313 struct timestamp_control
*, va_list);
314 extern int vty_config_enter(struct vty
*vty
, bool private_config
,
316 extern void vty_config_exit(struct vty
*);
317 extern int vty_config_exclusive_lock(struct vty
*vty
);
318 extern void vty_config_exclusive_unlock(struct vty
*vty
);
319 extern int vty_shell(struct vty
*);
320 extern int vty_shell_serv(struct vty
*);
321 extern void vty_hello(struct vty
*);
323 /* ^Z / SIGTSTP handling */
324 extern void vty_stdio_suspend(void);
325 extern void vty_stdio_resume(void);
326 extern void vty_stdio_close(void);
328 /* Send a fixed-size message to all vty terminal monitors; this should be
329 an async-signal-safe function. */
330 extern void vty_log_fixed(char *buf
, size_t len
);
332 #endif /* _ZEBRA_VTY_H */