]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Virtual terminal [aka TeletYpe] interface routine |
896014f4 DL |
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 | */ | |
718e3744 | 20 | |
21 | #ifndef _ZEBRA_VTY_H | |
22 | #define _ZEBRA_VTY_H | |
23 | ||
fe6b47b9 QY |
24 | #include <sys/types.h> |
25 | #include <regex.h> | |
26 | ||
b21b19c5 | 27 | #include "thread.h" |
1ed72e0b | 28 | #include "log.h" |
d227617a | 29 | #include "sockunion.h" |
0878c8d4 | 30 | #include "qobj.h" |
de1a880c | 31 | #include "compiler.h" |
1c2facd1 | 32 | #include "northbound.h" |
b21b19c5 | 33 | |
5e244469 RW |
34 | #ifdef __cplusplus |
35 | extern "C" { | |
36 | #endif | |
37 | ||
2af38873 | 38 | #define VTY_BUFSIZ 4096 |
718e3744 | 39 | #define VTY_MAXHIST 20 |
1c2facd1 | 40 | #define VTY_MAXDEPTH 8 |
718e3744 | 41 | |
a6233bfc RW |
42 | #define VTY_MAXCFGCHANGES 8 |
43 | ||
7ab57d19 DS |
44 | struct vty_error { |
45 | char error_buf[VTY_BUFSIZ]; | |
46 | uint32_t line_num; | |
47 | }; | |
48 | ||
a6233bfc | 49 | struct vty_cfg_change { |
8427e0e6 | 50 | char xpath[XPATH_MAXLEN]; |
a6233bfc RW |
51 | enum nb_operation operation; |
52 | const char *value; | |
53 | }; | |
54 | ||
718e3744 | 55 | /* VTY struct. */ |
d62a17ae | 56 | struct vty { |
57 | /* File descripter of this vty. */ | |
58 | int fd; | |
718e3744 | 59 | |
d62a17ae | 60 | /* output FD, to support stdin/stdout combination */ |
61 | int wfd; | |
c5e69a02 | 62 | |
2cddf2ff QY |
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 | ||
d62a17ae | 70 | /* Is this vty connect to file or not */ |
71 | enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type; | |
718e3744 | 72 | |
d62a17ae | 73 | /* Node status of this vty */ |
74 | int node; | |
718e3744 | 75 | |
d62a17ae | 76 | /* Failure count */ |
77 | int fail; | |
718e3744 | 78 | |
fe6b47b9 QY |
79 | /* Output filer regex */ |
80 | bool filter; | |
81 | regex_t include; | |
82 | ||
0b42d81a QY |
83 | /* Line buffer */ |
84 | struct buffer *lbuf; | |
85 | ||
d62a17ae | 86 | /* Output buffer. */ |
87 | struct buffer *obuf; | |
718e3744 | 88 | |
d62a17ae | 89 | /* Command input buffer */ |
90 | char *buf; | |
718e3744 | 91 | |
d62a17ae | 92 | /* Command input error buffer */ |
7ab57d19 | 93 | struct list *error; |
5689fe5f | 94 | |
d62a17ae | 95 | /* Command cursor point */ |
96 | int cp; | |
718e3744 | 97 | |
d62a17ae | 98 | /* Command length */ |
99 | int length; | |
718e3744 | 100 | |
d62a17ae | 101 | /* Command max length. */ |
102 | int max; | |
718e3744 | 103 | |
d62a17ae | 104 | /* Histry of command */ |
105 | char *hist[VTY_MAXHIST]; | |
718e3744 | 106 | |
d62a17ae | 107 | /* History lookup current point */ |
108 | int hp; | |
718e3744 | 109 | |
d62a17ae | 110 | /* History insert end point */ |
111 | int hindex; | |
718e3744 | 112 | |
a6233bfc RW |
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 | ||
1c2facd1 RW |
117 | /* XPath of the current node */ |
118 | int xpath_index; | |
119 | char xpath[VTY_MAXDEPTH][XPATH_MAXLEN]; | |
120 | ||
f344c66e RW |
121 | /* In configure mode. */ |
122 | bool config; | |
123 | ||
1c2facd1 RW |
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 | ||
fbdc1c0a RW |
133 | /* Confirmed-commit timeout and rollback configuration. */ |
134 | struct thread *t_confirmed_commit_timeout; | |
135 | struct nb_config *confirmed_commit_rollback; | |
136 | ||
d62a17ae | 137 | /* qobj object ID (replacement for "index") */ |
138 | uint64_t qobj_index; | |
718e3744 | 139 | |
d62a17ae | 140 | /* qobj second-level object ID (replacement for "index_sub") */ |
141 | uint64_t qobj_index_sub; | |
718e3744 | 142 | |
d62a17ae | 143 | /* For escape character. */ |
144 | unsigned char escape; | |
718e3744 | 145 | |
d62a17ae | 146 | /* Current vty status. */ |
147 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; | |
718e3744 | 148 | |
d62a17ae | 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; | |
718e3744 | 153 | |
d62a17ae | 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> */ | |
9fc7ebf1 | 159 | #define TELNET_NAWS_SB_LEN 5 |
d62a17ae | 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; | |
718e3744 | 164 | |
d62a17ae | 165 | /* Window width/height. */ |
166 | int width; | |
167 | int height; | |
718e3744 | 168 | |
d62a17ae | 169 | /* Configure lines. */ |
170 | int lines; | |
718e3744 | 171 | |
d62a17ae | 172 | /* Terminal monitor. */ |
173 | int monitor; | |
718e3744 | 174 | |
d62a17ae | 175 | /* Read and write thread. */ |
176 | struct thread *t_read; | |
177 | struct thread *t_write; | |
718e3744 | 178 | |
d62a17ae | 179 | /* Timeout seconds and thread. */ |
180 | unsigned long v_timeout; | |
181 | struct thread *t_timeout; | |
d227617a | 182 | |
d62a17ae | 183 | /* What address is this vty comming from. */ |
184 | char address[SU_ADDRSTRLEN]; | |
2071aa0e DL |
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]; | |
718e3744 | 191 | }; |
192 | ||
d62a17ae | 193 | static inline void vty_push_context(struct vty *vty, int node, uint64_t id) |
0878c8d4 | 194 | { |
d62a17ae | 195 | vty->node = node; |
196 | vty->qobj_index = id; | |
0878c8d4 DL |
197 | } |
198 | ||
3c5070be DL |
199 | /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to |
200 | * dereference "NULL->qobj_node.nid" */ | |
d62a17ae | 201 | #define VTY_PUSH_CONTEXT(nodeval, ptr) \ |
a50b7ceb | 202 | vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr)) |
d62a17ae | 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); \ | |
6a098b3a | 209 | } while (0) |
0878c8d4 DL |
210 | |
211 | /* can return NULL if context is invalid! */ | |
d62a17ae | 212 | #define VTY_GET_CONTEXT(structname) \ |
0878c8d4 | 213 | QOBJ_GET_TYPESAFE(vty->qobj_index, structname) |
d62a17ae | 214 | #define VTY_GET_CONTEXT_SUB(structname) \ |
6a098b3a | 215 | QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname) |
0878c8d4 DL |
216 | |
217 | /* will return if ptr is NULL. */ | |
d62a17ae | 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; \ | |
0878c8d4 DL |
224 | } |
225 | ||
226 | /* struct structname *ptr = <context>; ptr will never be NULL. */ | |
d62a17ae | 227 | #define VTY_DECLVAR_CONTEXT(structname, ptr) \ |
228 | struct structname *ptr = VTY_GET_CONTEXT(structname); \ | |
0878c8d4 | 229 | VTY_CHECK_CONTEXT(ptr); |
d62a17ae | 230 | #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \ |
231 | struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \ | |
6a098b3a | 232 | VTY_CHECK_CONTEXT(ptr); |
a3d826f0 | 233 | #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \ |
996c9314 LB |
234 | if (vty->qobj_index == 0) \ |
235 | return CMD_NOT_MY_INSTANCE; \ | |
a3d826f0 CS |
236 | struct structname *ptr = VTY_GET_CONTEXT(structname); \ |
237 | VTY_CHECK_CONTEXT(ptr); | |
0878c8d4 | 238 | |
1c2facd1 RW |
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 | ||
d62a17ae | 267 | struct vty_arg { |
268 | const char *name; | |
269 | const char *value; | |
270 | const char **argv; | |
271 | int argc; | |
eac6e3f0 RW |
272 | }; |
273 | ||
718e3744 | 274 | /* Integrated configuration file. */ |
e20dc2ba | 275 | #define INTEGRATE_DEFAULT_CONFIG "frr.conf" |
718e3744 | 276 | |
718e3744 | 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 | ||
718e3744 | 292 | /* Exported variables */ |
1c2facd1 | 293 | extern struct vty *vty_exclusive_lock; |
718e3744 | 294 | |
295 | /* Prototypes. */ | |
d62a17ae | 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); | |
154b9e8f | 301 | extern struct vty *vty_stdio(void (*atclose)(int isexit)); |
2071aa0e DL |
302 | |
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) | |
307 | */ | |
d62a17ae | 308 | extern int vty_out(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); |
2071aa0e DL |
309 | extern void vty_frame(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); |
310 | extern void vty_endframe(struct vty *, const char *); | |
fe6b47b9 | 311 | bool vty_set_include(struct vty *vty, const char *regexp); |
2071aa0e | 312 | |
1c2facd1 RW |
313 | extern bool vty_read_config(struct nb_config *config, const char *config_file, |
314 | char *config_default_dir); | |
d62a17ae | 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); | |
f344c66e RW |
321 | extern int vty_config_enter(struct vty *vty, bool private_config, |
322 | bool exclusive); | |
323 | extern void vty_config_exit(struct vty *); | |
1c2facd1 RW |
324 | extern int vty_config_exclusive_lock(struct vty *vty); |
325 | extern void vty_config_exclusive_unlock(struct vty *vty); | |
d62a17ae | 326 | extern int vty_shell(struct vty *); |
327 | extern int vty_shell_serv(struct vty *); | |
328 | extern void vty_hello(struct vty *); | |
718e3744 | 329 | |
154b9e8f DL |
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); | |
334 | ||
274a4a44 | 335 | /* Send a fixed-size message to all vty terminal monitors; this should be |
336 | an async-signal-safe function. */ | |
d62a17ae | 337 | extern void vty_log_fixed(char *buf, size_t len); |
274a4a44 | 338 | |
5e244469 RW |
339 | #ifdef __cplusplus |
340 | } | |
341 | #endif | |
342 | ||
718e3744 | 343 | #endif /* _ZEBRA_VTY_H */ |