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