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