]> git.proxmox.com Git - mirror_frr.git/blame - lib/vty.h
Add user `frr` into group `frrvty`
[mirror_frr.git] / lib / vty.h
CommitLineData
718e3744 1/* Virtual terminal [aka TeletYpe] interface routine
2 Copyright (C) 1997 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#ifndef _ZEBRA_VTY_H
22#define _ZEBRA_VTY_H
23
b21b19c5 24#include "thread.h"
1ed72e0b 25#include "log.h"
d227617a 26#include "sockunion.h"
0878c8d4 27#include "qobj.h"
b21b19c5 28
78af6edc 29#define VTY_BUFSIZ 4096
718e3744 30#define VTY_MAXHIST 20
31
0878c8d4 32#if defined(VTY_DEPRECATE_INDEX) && defined(__GNUC__) && \
fac5f480
DL
33 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && \
34 !defined(__ICC)
0878c8d4
DL
35#define INDEX_WARNING __attribute__((deprecated))
36#else
37#define INDEX_WARNING
38#endif
39
718e3744 40/* VTY struct. */
41struct vty
42{
43 /* File descripter of this vty. */
44 int fd;
45
c5e69a02
DL
46 /* output FD, to support stdin/stdout combination */
47 int wfd;
48
718e3744 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
718e3744 55 /* Failure count */
56 int fail;
57
58 /* Output buffer. */
59 struct buffer *obuf;
60
61 /* Command input buffer */
62 char *buf;
63
5689fe5f
DW
64 /* Command input error buffer */
65 char *error_buf;
66
718e3744 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... */
0878c8d4
DL
87 void *index INDEX_WARNING;
88
89 /* qobj object ID (replacement for "index") */
90 uint64_t qobj_index;
718e3744 91
6a098b3a
DL
92 /* qobj second-level object ID (replacement for "index_sub") */
93 uint64_t qobj_index_sub;
718e3744 94
95 /* For escape character. */
96 unsigned char escape;
97
98 /* Current vty status. */
5a646650 99 enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;
718e3744 100
9fc7ebf1 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. */
718e3744 104 unsigned char iac;
105
9fc7ebf1 106 /* IAC SB (option subnegotiation) handling */
718e3744 107 unsigned char iac_sb_in_progress;
9fc7ebf1 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;
718e3744 116
117 /* Window width/height. */
118 int width;
119 int height;
120
718e3744 121 /* Configure lines. */
122 int lines;
123
718e3744 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;
d227617a
JBD
137
138 /* What address is this vty comming from. */
139 char address[SU_ADDRSTRLEN];
718e3744 140};
141
0878c8d4
DL
142#undef INDEX_WARNING
143
144static 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)
6a098b3a
DL
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)
0878c8d4
DL
169
170/* can return NULL if context is invalid! */
171#define VTY_GET_CONTEXT(structname) \
172 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
6a098b3a
DL
173#define VTY_GET_CONTEXT_SUB(structname) \
174 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
0878c8d4
DL
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);
6a098b3a
DL
188#define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
189 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
190 VTY_CHECK_CONTEXT(ptr);
0878c8d4 191
eac6e3f0
RW
192struct vty_arg
193{
194 const char *name;
195 const char *value;
196 const char **argv;
197 int argc;
198};
199
718e3744 200/* Integrated configuration file. */
685e701e 201#define INTEGRATE_DEFAULT_CONFIG "frr.conf"
718e3744 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
7798b632
AC
228/* Utility macros to convert VTY argument to unsigned long */
229#define VTY_GET_ULONG(NAME,V,STR) \
d4f0960c 230do { \
42d49865 231 char *endptr = NULL; \
664711c1 232 errno = 0; \
42d49865 233 (V) = strtoul ((STR), &endptr, 10); \
813d4307 234 if (*(STR) == '-') \
42d49865 235 { \
813d4307
DW
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); \
42d49865 247 return CMD_WARNING; \
248 } \
d4f0960c 249} while (0)
718e3744 250
8fe8a7f6
DS
251/* Utility macros to convert VTY argument to unsigned long long */
252#define VTY_GET_ULL(NAME,V,STR) \
253do { \
254 char *endptr = NULL; \
255 errno = 0; \
256 (V) = strtoull ((STR), &endptr, 10); \
813d4307
DW
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) \
8fe8a7f6 268 { \
813d4307 269 vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \
8fe8a7f6
DS
270 return CMD_WARNING; \
271 } \
272} while (0)
273
7798b632
AC
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) \
282do { \
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) \
292do { \
c152e0c1 293 unsigned long long tmpl; \
7798b632
AC
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) \
299do { \
300 unsigned long tmpl; \
301 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
d4f0960c 302} while (0)
718e3744 303
7798b632
AC
304#define VTY_GET_INTEGER(NAME,V,STR) \
305 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
42d49865 306
8cc4198f 307#define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
d4f0960c 308do { \
8cc4198f 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 } \
d4f0960c 316} while (0)
8cc4198f 317
318#define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
d4f0960c 319do { \
8cc4198f 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 } \
d4f0960c 327} while (0)
8cc4198f 328
0b0668e6
DL
329#define VTY_WARN_EXPERIMENTAL() \
330do { \
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
718e3744 337/* Exported variables */
338extern char integrate_default[];
339
340/* Prototypes. */
8cc4198f 341extern void vty_init (struct thread_master *);
342extern void vty_init_vtysh (void);
228da428 343extern void vty_terminate (void);
8cc4198f 344extern void vty_reset (void);
345extern struct vty *vty_new (void);
dbf78092 346extern struct vty *vty_stdio (void (*atclose)(void));
8cc4198f 347extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
348extern void vty_read_config (char *, char *);
349extern void vty_time_print (struct vty *, int);
350extern void vty_serv_sock (const char *, unsigned short, const char *);
351extern void vty_close (struct vty *);
352extern char *vty_get_cwd (void);
353extern void vty_log (const char *level, const char *proto,
1ed72e0b 354 const char *fmt, struct timestamp_control *, va_list);
8cc4198f 355extern int vty_config_lock (struct vty *);
356extern int vty_config_unlock (struct vty *);
cc933ef9 357extern void vty_config_lockless (void);
8cc4198f 358extern int vty_shell (struct vty *);
359extern int vty_shell_serv (struct vty *);
360extern void vty_hello (struct vty *);
718e3744 361
274a4a44 362/* Send a fixed-size message to all vty terminal monitors; this should be
363 an async-signal-safe function. */
24873f0c 364extern void vty_log_fixed (char *buf, size_t len);
274a4a44 365
eac6e3f0
RW
366extern const char *vty_get_arg_value (struct vty_arg **, const char *);
367extern struct vty_arg *vty_get_arg (struct vty_arg **, const char *);
368
718e3744 369#endif /* _ZEBRA_VTY_H */