]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
Merge branch 'vtysh-grammar'
[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
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
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 512
30 #define VTY_MAXHIST 20
31
32 #if defined(VTY_DEPRECATE_INDEX) && defined(__GNUC__) && \
33 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && \
34 !defined(__ICC)
35 #define INDEX_WARNING __attribute__((deprecated))
36 #else
37 #define INDEX_WARNING
38 #endif
39
40 /* VTY struct. */
41 struct vty
42 {
43 /* File descripter of this vty. */
44 int fd;
45
46 /* output FD, to support stdin/stdout combination */
47 int wfd;
48
49 /* Is this vty connect to file or not */
50 enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;
51
52 /* Node status of this vty */
53 int node;
54
55 /* Failure count */
56 int fail;
57
58 /* Output buffer. */
59 struct buffer *obuf;
60
61 /* Command input buffer */
62 char *buf;
63
64 /* Command input error buffer */
65 char *error_buf;
66
67 /* Command cursor point */
68 int cp;
69
70 /* Command length */
71 int length;
72
73 /* Command max length. */
74 int max;
75
76 /* Histry of command */
77 char *hist[VTY_MAXHIST];
78
79 /* History lookup current point */
80 int hp;
81
82 /* History insert end point */
83 int hindex;
84
85 /* For current referencing point of interface, route-map,
86 access-list etc... */
87 void *index INDEX_WARNING;
88
89 /* qobj object ID (replacement for "index") */
90 uint64_t qobj_index;
91
92 /* qobj second-level object ID (replacement for "index_sub") */
93 uint64_t qobj_index_sub;
94
95 /* For escape character. */
96 unsigned char escape;
97
98 /* Current vty status. */
99 enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;
100
101 /* IAC handling: was the last character received the
102 IAC (interpret-as-command) escape character (and therefore the next
103 character will be the command code)? Refer to Telnet RFC 854. */
104 unsigned char iac;
105
106 /* IAC SB (option subnegotiation) handling */
107 unsigned char iac_sb_in_progress;
108 /* At the moment, we care only about the NAWS (window size) negotiation,
109 and that requires just a 5-character buffer (RFC 1073):
110 <NAWS char> <16-bit width> <16-bit height> */
111 #define TELNET_NAWS_SB_LEN 5
112 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
113 /* How many subnegotiation characters have we received? We just drop
114 those that do not fit in the buffer. */
115 size_t sb_len;
116
117 /* Window width/height. */
118 int width;
119 int height;
120
121 /* Configure lines. */
122 int lines;
123
124 /* Terminal monitor. */
125 int monitor;
126
127 /* In configure mode. */
128 int config;
129
130 /* Read and write thread. */
131 struct thread *t_read;
132 struct thread *t_write;
133
134 /* Timeout seconds and thread. */
135 unsigned long v_timeout;
136 struct thread *t_timeout;
137
138 /* What address is this vty comming from. */
139 char address[SU_ADDRSTRLEN];
140 };
141
142 #undef INDEX_WARNING
143
144 static inline void vty_push_context(struct vty *vty,
145 int node, uint64_t id, void *idx)
146 {
147 vty->node = node;
148 vty->qobj_index = id;
149 #if defined(VTY_DEPRECATE_INDEX) && defined(__GNUC__) && \
150 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
151 #pragma GCC diagnostic push
152 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
153 vty->index = idx;
154 #pragma GCC diagnostic pop
155 #else
156 vty->index = idx;
157 #endif
158 }
159
160 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
161 vty_push_context(vty, nodeval, QOBJ_ID(ptr), NULL)
162 #define VTY_PUSH_CONTEXT_COMPAT(nodeval, ptr) \
163 vty_push_context(vty, nodeval, QOBJ_ID(ptr), ptr)
164 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) do { \
165 vty->node = nodeval; \
166 /* qobj_index stays untouched */ \
167 vty->qobj_index_sub = QOBJ_ID(ptr); \
168 } while (0)
169
170 /* can return NULL if context is invalid! */
171 #define VTY_GET_CONTEXT(structname) \
172 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
173 #define VTY_GET_CONTEXT_SUB(structname) \
174 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
175
176 /* will return if ptr is NULL. */
177 #define VTY_CHECK_CONTEXT(ptr) \
178 if (!ptr) { \
179 vty_out (vty, "Current configuration object was deleted " \
180 "by another process.%s", VTY_NEWLINE); \
181 return CMD_WARNING; \
182 }
183
184 /* struct structname *ptr = <context>; ptr will never be NULL. */
185 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
186 struct structname *ptr = VTY_GET_CONTEXT(structname); \
187 VTY_CHECK_CONTEXT(ptr);
188 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
189 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
190 VTY_CHECK_CONTEXT(ptr);
191
192 struct vty_arg
193 {
194 const char *name;
195 const char *value;
196 const char **argv;
197 int argc;
198 };
199
200 /* Integrated configuration file. */
201 #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
202
203 /* Small macro to determine newline is newline only or linefeed needed. */
204 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
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 /* GCC have printf type attribute check. */
222 #ifdef __GNUC__
223 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
224 #else
225 #define PRINTF_ATTRIBUTE(a,b)
226 #endif /* __GNUC__ */
227
228 /* Utility macros to convert VTY argument to unsigned long */
229 #define VTY_GET_ULONG(NAME,V,STR) \
230 do { \
231 char *endptr = NULL; \
232 errno = 0; \
233 (V) = strtoul ((STR), &endptr, 10); \
234 if (*(STR) == '-') \
235 { \
236 vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \
237 return CMD_WARNING; \
238 } \
239 if (*endptr != '\0') \
240 { \
241 vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \
242 return CMD_WARNING; \
243 } \
244 if (errno) \
245 { \
246 vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \
247 return CMD_WARNING; \
248 } \
249 } while (0)
250
251 /* Utility macros to convert VTY argument to unsigned long long */
252 #define VTY_GET_ULL(NAME,V,STR) \
253 do { \
254 char *endptr = NULL; \
255 errno = 0; \
256 (V) = strtoull ((STR), &endptr, 10); \
257 if (*(STR) == '-') \
258 { \
259 vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \
260 return CMD_WARNING; \
261 } \
262 if (*endptr != '\0') \
263 { \
264 vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \
265 return CMD_WARNING; \
266 } \
267 if (errno) \
268 { \
269 vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \
270 return CMD_WARNING; \
271 } \
272 } while (0)
273
274 /*
275 * The logic below ((TMPL) <= ((MIN) && (TMPL) != (MIN)) is
276 * done to circumvent the compiler complaining about
277 * comparing unsigned numbers against zero, if MIN is zero.
278 * NB: The compiler isn't smart enough to supress the warning
279 * if you write (MIN) != 0 && tmpl < (MIN).
280 */
281 #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \
282 do { \
283 VTY_GET_ULONG(NAME, (TMPL), STR); \
284 if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \
285 { \
286 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\
287 return CMD_WARNING; \
288 } \
289 } while (0)
290
291 #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
292 do { \
293 unsigned long long tmpl; \
294 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
295 (V) = tmpl; \
296 } while (0)
297
298 #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \
299 do { \
300 unsigned long tmpl; \
301 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
302 } while (0)
303
304 #define VTY_GET_INTEGER(NAME,V,STR) \
305 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
306
307 #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
308 do { \
309 int retv; \
310 retv = inet_aton ((STR), &(V)); \
311 if (!retv) \
312 { \
313 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
314 return CMD_WARNING; \
315 } \
316 } while (0)
317
318 #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
319 do { \
320 int retv; \
321 retv = str2prefix_ipv4 ((STR), &(V)); \
322 if (retv <= 0) \
323 { \
324 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
325 return CMD_WARNING; \
326 } \
327 } while (0)
328
329 #define VTY_WARN_EXPERIMENTAL() \
330 do { \
331 vty_out (vty, "%% WARNING: this command is experimental. Both its name and" \
332 " parameters may%s%% change in a future version of Quagga," \
333 " possibly breaking your configuration!%s", \
334 VTY_NEWLINE, VTY_NEWLINE); \
335 } while (0)
336
337 /* Exported variables */
338 extern char integrate_default[];
339
340 /* Prototypes. */
341 extern void vty_init (struct thread_master *);
342 extern void vty_init_vtysh (void);
343 extern void vty_terminate (void);
344 extern void vty_reset (void);
345 extern struct vty *vty_new (void);
346 extern struct vty *vty_stdio (void (*atclose)(void));
347 extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
348 extern void vty_read_config (char *, char *);
349 extern void vty_time_print (struct vty *, int);
350 extern void vty_serv_sock (const char *, unsigned short, const char *);
351 extern void vty_close (struct vty *);
352 extern char *vty_get_cwd (void);
353 extern void vty_log (const char *level, const char *proto,
354 const char *fmt, struct timestamp_control *, va_list);
355 extern int vty_config_lock (struct vty *);
356 extern int vty_config_unlock (struct vty *);
357 extern void vty_config_lockless (void);
358 extern int vty_shell (struct vty *);
359 extern int vty_shell_serv (struct vty *);
360 extern void vty_hello (struct vty *);
361
362 /* Send a fixed-size message to all vty terminal monitors; this should be
363 an async-signal-safe function. */
364 extern void vty_log_fixed (char *buf, size_t len);
365
366 extern const char *vty_get_arg_value (struct vty_arg **, const char *);
367 extern struct vty_arg *vty_get_arg (struct vty_arg **, const char *);
368
369 #endif /* _ZEBRA_VTY_H */