]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
Merge pull request #3397 from mjstapp/fix_stream_macros
[mirror_frr.git] / lib / vty.h
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 #define VTY_BUFSIZ 4096
35 #define VTY_MAXHIST 20
36 #define VTY_MAXDEPTH 8
37
38 #define VTY_MAXCFGCHANGES 8
39
40 struct vty_error {
41 char error_buf[VTY_BUFSIZ];
42 uint32_t line_num;
43 };
44
45 struct vty_cfg_change {
46 const char *xpath;
47 enum nb_operation operation;
48 const char *value;
49 };
50
51 /* VTY struct. */
52 struct vty {
53 /* File descripter of this vty. */
54 int fd;
55
56 /* output FD, to support stdin/stdout combination */
57 int wfd;
58
59 /* File output, used for VTYSH only */
60 FILE *of;
61 FILE *of_saved;
62
63 /* whether we are using pager or not */
64 bool is_paged;
65
66 /* Is this vty connect to file or not */
67 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
68
69 /* Node status of this vty */
70 int node;
71
72 /* Failure count */
73 int fail;
74
75 /* Output filer regex */
76 bool filter;
77 regex_t include;
78
79 /* Line buffer */
80 struct buffer *lbuf;
81
82 /* Output buffer. */
83 struct buffer *obuf;
84
85 /* Command input buffer */
86 char *buf;
87
88 /* Command input error buffer */
89 struct list *error;
90
91 /* Command cursor point */
92 int cp;
93
94 /* Command length */
95 int length;
96
97 /* Command max length. */
98 int max;
99
100 /* Histry of command */
101 char *hist[VTY_MAXHIST];
102
103 /* History lookup current point */
104 int hp;
105
106 /* History insert end point */
107 int hindex;
108
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];
112
113 /* XPath of the current node */
114 int xpath_index;
115 char xpath[VTY_MAXDEPTH][XPATH_MAXLEN];
116
117 /* Private candidate configuration mode. */
118 bool private_config;
119
120 /* Candidate configuration. */
121 struct nb_config *candidate_config;
122
123 /* Base candidate configuration. */
124 struct nb_config *candidate_config_base;
125
126 /* qobj object ID (replacement for "index") */
127 uint64_t qobj_index;
128
129 /* qobj second-level object ID (replacement for "index_sub") */
130 uint64_t qobj_index_sub;
131
132 /* For escape character. */
133 unsigned char escape;
134
135 /* Current vty status. */
136 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
137
138 /* IAC handling: was the last character received the
139 IAC (interpret-as-command) escape character (and therefore the next
140 character will be the command code)? Refer to Telnet RFC 854. */
141 unsigned char iac;
142
143 /* IAC SB (option subnegotiation) handling */
144 unsigned char iac_sb_in_progress;
145 /* At the moment, we care only about the NAWS (window size) negotiation,
146 and that requires just a 5-character buffer (RFC 1073):
147 <NAWS char> <16-bit width> <16-bit height> */
148 #define TELNET_NAWS_SB_LEN 5
149 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
150 /* How many subnegotiation characters have we received? We just drop
151 those that do not fit in the buffer. */
152 size_t sb_len;
153
154 /* Window width/height. */
155 int width;
156 int height;
157
158 /* Configure lines. */
159 int lines;
160
161 /* Terminal monitor. */
162 int monitor;
163
164 /* In configure mode. */
165 int config;
166
167 /* Read and write thread. */
168 struct thread *t_read;
169 struct thread *t_write;
170
171 /* Timeout seconds and thread. */
172 unsigned long v_timeout;
173 struct thread *t_timeout;
174
175 /* What address is this vty comming from. */
176 char address[SU_ADDRSTRLEN];
177
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. */
181 size_t frame_pos;
182 char frame[1024];
183 };
184
185 static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
186 {
187 vty->node = node;
188 vty->qobj_index = id;
189 }
190
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) \
197 do { \
198 vty->node = nodeval; \
199 /* qobj_index stays untouched */ \
200 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
201 } while (0)
202
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)
208
209 /* will return if ptr is NULL. */
210 #define VTY_CHECK_CONTEXT(ptr) \
211 if (!ptr) { \
212 vty_out(vty, \
213 "Current configuration object was deleted " \
214 "by another process.\n"); \
215 return CMD_WARNING; \
216 }
217
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);
230
231 /* XPath macros. */
232 #define VTY_PUSH_XPATH(nodeval, value) \
233 do { \
234 if (vty->xpath_index >= VTY_MAXDEPTH) { \
235 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
236 VTY_MAXDEPTH); \
237 return CMD_WARNING; \
238 } \
239 vty->node = nodeval; \
240 strlcpy(vty->xpath[vty->xpath_index], value, \
241 sizeof(vty->xpath[0])); \
242 vty->xpath_index++; \
243 } while (0)
244
245 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
246
247 #define VTY_CHECK_XPATH \
248 do { \
249 if (vty->xpath_index > 0 \
250 && !yang_dnode_exists(vty->candidate_config->dnode, \
251 VTY_CURR_XPATH)) { \
252 vty_out(vty, \
253 "Current configuration object was deleted " \
254 "by another process.\n\n"); \
255 return CMD_WARNING; \
256 } \
257 } while (0)
258
259 struct vty_arg {
260 const char *name;
261 const char *value;
262 const char **argv;
263 int argc;
264 };
265
266 /* Integrated configuration file. */
267 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
268
269 /* Default time out value */
270 #define VTY_TIMEOUT_DEFAULT 600
271
272 /* Vty read buffer size. */
273 #define VTY_READ_BUFSIZ 512
274
275 /* Directory separator. */
276 #ifndef DIRECTORY_SEP
277 #define DIRECTORY_SEP '/'
278 #endif /* DIRECTORY_SEP */
279
280 #ifndef IS_DIRECTORY_SEP
281 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
282 #endif
283
284 /* Exported variables */
285 extern char integrate_default[];
286 extern struct vty *vty_exclusive_lock;
287
288 /* Prototypes. */
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));
295
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)
300 */
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);
305
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_lock(struct vty *);
315 extern int vty_config_unlock(struct vty *);
316 extern void vty_config_lockless(void);
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 *);
322
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);
327
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);
331
332 #endif /* _ZEBRA_VTY_H */