]>
Commit | Line | Data |
---|---|---|
1 | /* Virtual terminal [aka TeletYpe] interface routine | |
2 | * Copyright (C) 1997 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
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 | |
9 | * later version. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | ||
21 | #ifndef _ZEBRA_VTY_H | |
22 | #define _ZEBRA_VTY_H | |
23 | ||
24 | #include <sys/types.h> | |
25 | #include <regex.h> | |
26 | ||
27 | #include "thread.h" | |
28 | #include "log.h" | |
29 | #include "sockunion.h" | |
30 | #include "qobj.h" | |
31 | #include "compiler.h" | |
32 | #include "northbound.h" | |
33 | ||
34 | #ifdef __cplusplus | |
35 | extern "C" { | |
36 | #endif | |
37 | ||
38 | #define VTY_BUFSIZ 4096 | |
39 | #define VTY_MAXHIST 20 | |
40 | #define VTY_MAXDEPTH 8 | |
41 | ||
42 | #define VTY_MAXCFGCHANGES 8 | |
43 | ||
44 | struct vty_error { | |
45 | char error_buf[VTY_BUFSIZ]; | |
46 | uint32_t line_num; | |
47 | }; | |
48 | ||
49 | struct vty_cfg_change { | |
50 | char xpath[XPATH_MAXLEN]; | |
51 | enum nb_operation operation; | |
52 | const char *value; | |
53 | }; | |
54 | ||
55 | /* VTY struct. */ | |
56 | struct vty { | |
57 | /* File descripter of this vty. */ | |
58 | int fd; | |
59 | ||
60 | /* output FD, to support stdin/stdout combination */ | |
61 | int wfd; | |
62 | ||
63 | /* File output, used for VTYSH only */ | |
64 | FILE *of; | |
65 | FILE *of_saved; | |
66 | ||
67 | /* whether we are using pager or not */ | |
68 | bool is_paged; | |
69 | ||
70 | /* Is this vty connect to file or not */ | |
71 | enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type; | |
72 | ||
73 | /* Node status of this vty */ | |
74 | int node; | |
75 | ||
76 | /* Failure count */ | |
77 | int fail; | |
78 | ||
79 | /* Output filer regex */ | |
80 | bool filter; | |
81 | regex_t include; | |
82 | ||
83 | /* Line buffer */ | |
84 | struct buffer *lbuf; | |
85 | ||
86 | /* Output buffer. */ | |
87 | struct buffer *obuf; | |
88 | ||
89 | /* Command input buffer */ | |
90 | char *buf; | |
91 | ||
92 | /* Command input error buffer */ | |
93 | struct list *error; | |
94 | ||
95 | /* Command cursor point */ | |
96 | int cp; | |
97 | ||
98 | /* Command length */ | |
99 | int length; | |
100 | ||
101 | /* Command max length. */ | |
102 | int max; | |
103 | ||
104 | /* Histry of command */ | |
105 | char *hist[VTY_MAXHIST]; | |
106 | ||
107 | /* History lookup current point */ | |
108 | int hp; | |
109 | ||
110 | /* History insert end point */ | |
111 | int hindex; | |
112 | ||
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]; | |
116 | ||
117 | /* XPath of the current node */ | |
118 | int xpath_index; | |
119 | char xpath[VTY_MAXDEPTH][XPATH_MAXLEN]; | |
120 | ||
121 | /* In configure mode. */ | |
122 | bool config; | |
123 | ||
124 | /* Private candidate configuration mode. */ | |
125 | bool private_config; | |
126 | ||
127 | /* Candidate configuration. */ | |
128 | struct nb_config *candidate_config; | |
129 | ||
130 | /* Base candidate configuration. */ | |
131 | struct nb_config *candidate_config_base; | |
132 | ||
133 | /* Confirmed-commit timeout and rollback configuration. */ | |
134 | struct thread *t_confirmed_commit_timeout; | |
135 | struct nb_config *confirmed_commit_rollback; | |
136 | ||
137 | /* qobj object ID (replacement for "index") */ | |
138 | uint64_t qobj_index; | |
139 | ||
140 | /* qobj second-level object ID (replacement for "index_sub") */ | |
141 | uint64_t qobj_index_sub; | |
142 | ||
143 | /* For escape character. */ | |
144 | unsigned char escape; | |
145 | ||
146 | /* Current vty status. */ | |
147 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; | |
148 | ||
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. */ | |
152 | unsigned char iac; | |
153 | ||
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. */ | |
163 | size_t sb_len; | |
164 | ||
165 | /* Window width/height. */ | |
166 | int width; | |
167 | int height; | |
168 | ||
169 | /* Configure lines. */ | |
170 | int lines; | |
171 | ||
172 | /* Terminal monitor. */ | |
173 | int monitor; | |
174 | ||
175 | /* Read and write thread. */ | |
176 | struct thread *t_read; | |
177 | struct thread *t_write; | |
178 | ||
179 | /* Timeout seconds and thread. */ | |
180 | unsigned long v_timeout; | |
181 | struct thread *t_timeout; | |
182 | ||
183 | /* What address is this vty comming from. */ | |
184 | char address[SU_ADDRSTRLEN]; | |
185 | ||
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. */ | |
189 | size_t frame_pos; | |
190 | char frame[1024]; | |
191 | }; | |
192 | ||
193 | static inline void vty_push_context(struct vty *vty, int node, uint64_t id) | |
194 | { | |
195 | vty->node = node; | |
196 | vty->qobj_index = id; | |
197 | } | |
198 | ||
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) \ | |
205 | do { \ | |
206 | vty->node = nodeval; \ | |
207 | /* qobj_index stays untouched */ \ | |
208 | vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \ | |
209 | } while (0) | |
210 | ||
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) | |
216 | ||
217 | /* will return if ptr is NULL. */ | |
218 | #define VTY_CHECK_CONTEXT(ptr) \ | |
219 | if (!ptr) { \ | |
220 | vty_out(vty, \ | |
221 | "Current configuration object was deleted " \ | |
222 | "by another process.\n"); \ | |
223 | return CMD_WARNING; \ | |
224 | } | |
225 | ||
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); | |
238 | ||
239 | /* XPath macros. */ | |
240 | #define VTY_PUSH_XPATH(nodeval, value) \ | |
241 | do { \ | |
242 | if (vty->xpath_index >= VTY_MAXDEPTH) { \ | |
243 | vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \ | |
244 | VTY_MAXDEPTH); \ | |
245 | return CMD_WARNING; \ | |
246 | } \ | |
247 | vty->node = nodeval; \ | |
248 | strlcpy(vty->xpath[vty->xpath_index], value, \ | |
249 | sizeof(vty->xpath[0])); \ | |
250 | vty->xpath_index++; \ | |
251 | } while (0) | |
252 | ||
253 | #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1] | |
254 | ||
255 | #define VTY_CHECK_XPATH \ | |
256 | do { \ | |
257 | if (vty->xpath_index > 0 \ | |
258 | && !yang_dnode_exists(vty->candidate_config->dnode, \ | |
259 | VTY_CURR_XPATH)) { \ | |
260 | vty_out(vty, \ | |
261 | "Current configuration object was deleted " \ | |
262 | "by another process.\n\n"); \ | |
263 | return CMD_WARNING; \ | |
264 | } \ | |
265 | } while (0) | |
266 | ||
267 | struct vty_arg { | |
268 | const char *name; | |
269 | const char *value; | |
270 | const char **argv; | |
271 | int argc; | |
272 | }; | |
273 | ||
274 | /* Integrated configuration file. */ | |
275 | #define INTEGRATE_DEFAULT_CONFIG "frr.conf" | |
276 | ||
277 | /* Default time out value */ | |
278 | #define VTY_TIMEOUT_DEFAULT 600 | |
279 | ||
280 | /* Vty read buffer size. */ | |
281 | #define VTY_READ_BUFSIZ 512 | |
282 | ||
283 | /* Directory separator. */ | |
284 | #ifndef DIRECTORY_SEP | |
285 | #define DIRECTORY_SEP '/' | |
286 | #endif /* DIRECTORY_SEP */ | |
287 | ||
288 | #ifndef IS_DIRECTORY_SEP | |
289 | #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP) | |
290 | #endif | |
291 | ||
292 | /* Prototypes. */ | |
293 | extern void vty_init(struct thread_master *, bool do_command_logging); | |
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)); | |
299 | ||
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) | |
304 | */ | |
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); | |
309 | ||
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, | |
319 | bool exclusive); | |
320 | extern void vty_config_exit(struct vty *); | |
321 | extern int vty_shell(struct vty *); | |
322 | extern int vty_shell_serv(struct vty *); | |
323 | extern void vty_hello(struct vty *); | |
324 | ||
325 | /* ^Z / SIGTSTP handling */ | |
326 | extern void vty_stdio_suspend(void); | |
327 | extern void vty_stdio_resume(void); | |
328 | extern void vty_stdio_close(void); | |
329 | ||
330 | /* Send a fixed-size message to all vty terminal monitors; this should be | |
331 | an async-signal-safe function. */ | |
332 | extern void vty_log_fixed(char *buf, size_t len); | |
333 | ||
334 | #ifdef __cplusplus | |
335 | } | |
336 | #endif | |
337 | ||
338 | #endif /* _ZEBRA_VTY_H */ |