]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
Merge pull request #2553 from opensourcerouting/release_proc
[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
33 #define VTY_BUFSIZ 4096
34 #define VTY_MAXHIST 20
35
36 /* VTY struct. */
37 struct vty {
38 /* File descripter of this vty. */
39 int fd;
40
41 /* output FD, to support stdin/stdout combination */
42 int wfd;
43
44 /* File output, used for VTYSH only */
45 FILE *of;
46 FILE *of_saved;
47
48 /* whether we are using pager or not */
49 bool is_paged;
50
51 /* Is this vty connect to file or not */
52 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
53
54 /* Node status of this vty */
55 int node;
56
57 /* Failure count */
58 int fail;
59
60 /* Output filer regex */
61 bool filter;
62 regex_t include;
63
64 /* Line buffer */
65 struct buffer *lbuf;
66
67 /* Output buffer. */
68 struct buffer *obuf;
69
70 /* Command input buffer */
71 char *buf;
72
73 /* Command input error buffer */
74 char *error_buf;
75
76 /* Command cursor point */
77 int cp;
78
79 /* Command length */
80 int length;
81
82 /* Command max length. */
83 int max;
84
85 /* Histry of command */
86 char *hist[VTY_MAXHIST];
87
88 /* History lookup current point */
89 int hp;
90
91 /* History insert end point */
92 int hindex;
93
94 /* qobj object ID (replacement for "index") */
95 uint64_t qobj_index;
96
97 /* qobj second-level object ID (replacement for "index_sub") */
98 uint64_t qobj_index_sub;
99
100 /* For escape character. */
101 unsigned char escape;
102
103 /* Current vty status. */
104 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
105
106 /* IAC handling: was the last character received the
107 IAC (interpret-as-command) escape character (and therefore the next
108 character will be the command code)? Refer to Telnet RFC 854. */
109 unsigned char iac;
110
111 /* IAC SB (option subnegotiation) handling */
112 unsigned char iac_sb_in_progress;
113 /* At the moment, we care only about the NAWS (window size) negotiation,
114 and that requires just a 5-character buffer (RFC 1073):
115 <NAWS char> <16-bit width> <16-bit height> */
116 #define TELNET_NAWS_SB_LEN 5
117 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
118 /* How many subnegotiation characters have we received? We just drop
119 those that do not fit in the buffer. */
120 size_t sb_len;
121
122 /* Window width/height. */
123 int width;
124 int height;
125
126 /* Configure lines. */
127 int lines;
128
129 /* Terminal monitor. */
130 int monitor;
131
132 /* In configure mode. */
133 int config;
134
135 /* Read and write thread. */
136 struct thread *t_read;
137 struct thread *t_write;
138
139 /* Timeout seconds and thread. */
140 unsigned long v_timeout;
141 struct thread *t_timeout;
142
143 /* What address is this vty comming from. */
144 char address[SU_ADDRSTRLEN];
145
146 /* "frame" output. This is buffered and will be printed if some
147 * actual output follows, or will be discarded if the frame ends
148 * without any output. */
149 size_t frame_pos;
150 char frame[1024];
151 };
152
153 static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
154 {
155 vty->node = node;
156 vty->qobj_index = id;
157 }
158
159 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
160 * dereference "NULL->qobj_node.nid" */
161 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
162 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
163 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
164 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
165 do { \
166 vty->node = nodeval; \
167 /* qobj_index stays untouched */ \
168 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
169 } while (0)
170
171 /* can return NULL if context is invalid! */
172 #define VTY_GET_CONTEXT(structname) \
173 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
174 #define VTY_GET_CONTEXT_SUB(structname) \
175 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
176
177 /* will return if ptr is NULL. */
178 #define VTY_CHECK_CONTEXT(ptr) \
179 if (!ptr) { \
180 vty_out(vty, \
181 "Current configuration object was deleted " \
182 "by another process.\n"); \
183 return CMD_WARNING; \
184 }
185
186 /* struct structname *ptr = <context>; ptr will never be NULL. */
187 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
188 struct structname *ptr = VTY_GET_CONTEXT(structname); \
189 VTY_CHECK_CONTEXT(ptr);
190 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
191 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
192 VTY_CHECK_CONTEXT(ptr);
193 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
194 if (vty->qobj_index == 0) \
195 return CMD_NOT_MY_INSTANCE; \
196 struct structname *ptr = VTY_GET_CONTEXT(structname); \
197 VTY_CHECK_CONTEXT(ptr);
198
199 struct vty_arg {
200 const char *name;
201 const char *value;
202 const char **argv;
203 int argc;
204 };
205
206 /* Integrated configuration file. */
207 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
208
209 /* Default time out value */
210 #define VTY_TIMEOUT_DEFAULT 600
211
212 /* Vty read buffer size. */
213 #define VTY_READ_BUFSIZ 512
214
215 /* Directory separator. */
216 #ifndef DIRECTORY_SEP
217 #define DIRECTORY_SEP '/'
218 #endif /* DIRECTORY_SEP */
219
220 #ifndef IS_DIRECTORY_SEP
221 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
222 #endif
223
224 /* Exported variables */
225 extern char integrate_default[];
226
227 /* Prototypes. */
228 extern void vty_init(struct thread_master *);
229 extern void vty_init_vtysh(void);
230 extern void vty_terminate(void);
231 extern void vty_reset(void);
232 extern struct vty *vty_new(void);
233 extern struct vty *vty_stdio(void (*atclose)(int isexit));
234
235 /* - vty_frame() output goes to a buffer (for context-begin markers)
236 * - vty_out() will first print this buffer, and clear it
237 * - vty_endframe() clears the buffer without printing it, and prints an
238 * extra string if the buffer was empty before (for context-end markers)
239 */
240 extern int vty_out(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
241 extern void vty_frame(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
242 extern void vty_endframe(struct vty *, const char *);
243 bool vty_set_include(struct vty *vty, const char *regexp);
244
245 extern bool vty_read_config(const char *, char *);
246 extern void vty_time_print(struct vty *, int);
247 extern void vty_serv_sock(const char *, unsigned short, const char *);
248 extern void vty_close(struct vty *);
249 extern char *vty_get_cwd(void);
250 extern void vty_log(const char *level, const char *proto, const char *fmt,
251 struct timestamp_control *, va_list);
252 extern int vty_config_lock(struct vty *);
253 extern int vty_config_unlock(struct vty *);
254 extern void vty_config_lockless(void);
255 extern int vty_shell(struct vty *);
256 extern int vty_shell_serv(struct vty *);
257 extern void vty_hello(struct vty *);
258
259 /* ^Z / SIGTSTP handling */
260 extern void vty_stdio_suspend(void);
261 extern void vty_stdio_resume(void);
262 extern void vty_stdio_close(void);
263
264 /* Send a fixed-size message to all vty terminal monitors; this should be
265 an async-signal-safe function. */
266 extern void vty_log_fixed(char *buf, size_t len);
267
268 #endif /* _ZEBRA_VTY_H */