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