]>
git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
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
39 char error_buf
[VTY_BUFSIZ
];
45 /* File descripter of this vty. */
48 /* output FD, to support stdin/stdout combination */
51 /* File output, used for VTYSH only */
55 /* whether we are using pager or not */
58 /* Is this vty connect to file or not */
59 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
61 /* Node status of this vty */
67 /* Output filer regex */
77 /* Command input buffer */
80 /* Command input error buffer */
83 /* Command cursor point */
89 /* Command max length. */
92 /* Histry of command */
93 char *hist
[VTY_MAXHIST
];
95 /* History lookup current point */
98 /* History insert end point */
101 /* XPath of the current node */
103 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
105 /* Private candidate configuration mode. */
108 /* Candidate configuration. */
109 struct nb_config
*candidate_config
;
111 /* Base candidate configuration. */
112 struct nb_config
*candidate_config_base
;
114 /* qobj object ID (replacement for "index") */
117 /* qobj second-level object ID (replacement for "index_sub") */
118 uint64_t qobj_index_sub
;
120 /* For escape character. */
121 unsigned char escape
;
123 /* Current vty status. */
124 enum { VTY_NORMAL
, VTY_CLOSE
, VTY_MORE
, VTY_MORELINE
} status
;
126 /* IAC handling: was the last character received the
127 IAC (interpret-as-command) escape character (and therefore the next
128 character will be the command code)? Refer to Telnet RFC 854. */
131 /* IAC SB (option subnegotiation) handling */
132 unsigned char iac_sb_in_progress
;
133 /* At the moment, we care only about the NAWS (window size) negotiation,
134 and that requires just a 5-character buffer (RFC 1073):
135 <NAWS char> <16-bit width> <16-bit height> */
136 #define TELNET_NAWS_SB_LEN 5
137 unsigned char sb_buf
[TELNET_NAWS_SB_LEN
];
138 /* How many subnegotiation characters have we received? We just drop
139 those that do not fit in the buffer. */
142 /* Window width/height. */
146 /* Configure lines. */
149 /* Terminal monitor. */
152 /* In configure mode. */
155 /* Read and write thread. */
156 struct thread
*t_read
;
157 struct thread
*t_write
;
159 /* Timeout seconds and thread. */
160 unsigned long v_timeout
;
161 struct thread
*t_timeout
;
163 /* What address is this vty comming from. */
164 char address
[SU_ADDRSTRLEN
];
166 /* "frame" output. This is buffered and will be printed if some
167 * actual output follows, or will be discarded if the frame ends
168 * without any output. */
173 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
176 vty
->qobj_index
= id
;
179 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
180 * dereference "NULL->qobj_node.nid" */
181 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
182 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
183 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
184 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
186 vty->node = nodeval; \
187 /* qobj_index stays untouched */ \
188 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
191 /* can return NULL if context is invalid! */
192 #define VTY_GET_CONTEXT(structname) \
193 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
194 #define VTY_GET_CONTEXT_SUB(structname) \
195 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
197 /* will return if ptr is NULL. */
198 #define VTY_CHECK_CONTEXT(ptr) \
201 "Current configuration object was deleted " \
202 "by another process.\n"); \
203 return CMD_WARNING; \
206 /* struct structname *ptr = <context>; ptr will never be NULL. */
207 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
208 struct structname *ptr = VTY_GET_CONTEXT(structname); \
209 VTY_CHECK_CONTEXT(ptr);
210 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
211 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
212 VTY_CHECK_CONTEXT(ptr);
213 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
214 if (vty->qobj_index == 0) \
215 return CMD_NOT_MY_INSTANCE; \
216 struct structname *ptr = VTY_GET_CONTEXT(structname); \
217 VTY_CHECK_CONTEXT(ptr);
220 #define VTY_PUSH_XPATH(nodeval, value) \
222 if (vty->xpath_index >= VTY_MAXDEPTH) { \
223 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
225 return CMD_WARNING; \
227 vty->node = nodeval; \
228 strlcpy(vty->xpath[vty->xpath_index], value, \
229 sizeof(vty->xpath[0])); \
230 vty->xpath_index++; \
233 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
235 #define VTY_CHECK_XPATH \
237 if (vty->xpath_index > 0 \
238 && !yang_dnode_exists(vty->candidate_config->dnode, \
241 "Current configuration object was deleted " \
242 "by another process.\n\n"); \
243 return CMD_WARNING; \
254 /* Integrated configuration file. */
255 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
257 /* Default time out value */
258 #define VTY_TIMEOUT_DEFAULT 600
260 /* Vty read buffer size. */
261 #define VTY_READ_BUFSIZ 512
263 /* Directory separator. */
264 #ifndef DIRECTORY_SEP
265 #define DIRECTORY_SEP '/'
266 #endif /* DIRECTORY_SEP */
268 #ifndef IS_DIRECTORY_SEP
269 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
272 /* Exported variables */
273 extern char integrate_default
[];
274 extern struct vty
*vty_exclusive_lock
;
277 extern void vty_init(struct thread_master
*);
278 extern void vty_init_vtysh(void);
279 extern void vty_terminate(void);
280 extern void vty_reset(void);
281 extern struct vty
*vty_new(void);
282 extern struct vty
*vty_stdio(void (*atclose
)(int isexit
));
284 /* - vty_frame() output goes to a buffer (for context-begin markers)
285 * - vty_out() will first print this buffer, and clear it
286 * - vty_endframe() clears the buffer without printing it, and prints an
287 * extra string if the buffer was empty before (for context-end markers)
289 extern int vty_out(struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
290 extern void vty_frame(struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
291 extern void vty_endframe(struct vty
*, const char *);
292 bool vty_set_include(struct vty
*vty
, const char *regexp
);
294 extern bool vty_read_config(struct nb_config
*config
, const char *config_file
,
295 char *config_default_dir
);
296 extern void vty_time_print(struct vty
*, int);
297 extern void vty_serv_sock(const char *, unsigned short, const char *);
298 extern void vty_close(struct vty
*);
299 extern char *vty_get_cwd(void);
300 extern void vty_log(const char *level
, const char *proto
, const char *fmt
,
301 struct timestamp_control
*, va_list);
302 extern int vty_config_lock(struct vty
*);
303 extern int vty_config_unlock(struct vty
*);
304 extern void vty_config_lockless(void);
305 extern int vty_config_exclusive_lock(struct vty
*vty
);
306 extern void vty_config_exclusive_unlock(struct vty
*vty
);
307 extern int vty_shell(struct vty
*);
308 extern int vty_shell_serv(struct vty
*);
309 extern void vty_hello(struct vty
*);
311 /* ^Z / SIGTSTP handling */
312 extern void vty_stdio_suspend(void);
313 extern void vty_stdio_resume(void);
314 extern void vty_stdio_close(void);
316 /* Send a fixed-size message to all vty terminal monitors; this should be
317 an async-signal-safe function. */
318 extern void vty_log_fixed(char *buf
, size_t len
);
320 #endif /* _ZEBRA_VTY_H */