]>
git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
1 /* Virtual terminal [aka TeletYpe] interface routine
2 Copyright (C) 1997 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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
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.
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
26 #include "sockunion.h"
29 #define VTY_BUFSIZ 512
30 #define VTY_MAXHIST 20
32 #if defined(VTY_DEPRECATE_INDEX) && defined(__GNUC__) && \
33 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
34 #define INDEX_WARNING __attribute__((deprecated))
42 /* File descripter of this vty. */
45 /* output FD, to support stdin/stdout combination */
48 /* Is this vty connect to file or not */
49 enum {VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
51 /* Node status of this vty */
60 /* Command input buffer */
63 /* Command input error buffer */
66 /* Command cursor point */
72 /* Command max length. */
75 /* Histry of command */
76 char *hist
[VTY_MAXHIST
];
78 /* History lookup current point */
81 /* History insert end point */
84 /* For current referencing point of interface, route-map,
86 void *index INDEX_WARNING
;
88 /* qobj object ID (replacement for "index") */
91 /* For multiple level index treatment such as key chain and key. */
94 /* For escape character. */
97 /* Current vty status. */
98 enum {VTY_NORMAL
, VTY_CLOSE
, VTY_MORE
, VTY_MORELINE
} status
;
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. */
105 /* IAC SB (option subnegotiation) handling */
106 unsigned char iac_sb_in_progress
;
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. */
116 /* Window width/height. */
120 /* Configure lines. */
123 /* Terminal monitor. */
126 /* In configure mode. */
129 /* Read and write thread. */
130 struct thread
*t_read
;
131 struct thread
*t_write
;
133 /* Timeout seconds and thread. */
134 unsigned long v_timeout
;
135 struct thread
*t_timeout
;
137 /* What address is this vty comming from. */
138 char address
[SU_ADDRSTRLEN
];
143 static inline void vty_push_context(struct vty
*vty
,
144 int node
, uint64_t id
, void *idx
)
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"
153 #pragma GCC diagnostic pop
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)
164 /* can return NULL if context is invalid! */
165 #define VTY_GET_CONTEXT(structname) \
166 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
168 /* will return if ptr is NULL. */
169 #define VTY_CHECK_CONTEXT(ptr) \
171 vty_out (vty, "Current configuration object was deleted " \
172 "by another process.%s", VTY_NEWLINE); \
173 return CMD_WARNING; \
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);
189 /* Integrated configuration file. */
190 #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
192 /* Small macro to determine newline is newline only or linefeed needed. */
193 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
195 /* Default time out value */
196 #define VTY_TIMEOUT_DEFAULT 600
198 /* Vty read buffer size. */
199 #define VTY_READ_BUFSIZ 512
201 /* Directory separator. */
202 #ifndef DIRECTORY_SEP
203 #define DIRECTORY_SEP '/'
204 #endif /* DIRECTORY_SEP */
206 #ifndef IS_DIRECTORY_SEP
207 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
210 /* GCC have printf type attribute check. */
212 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
214 #define PRINTF_ATTRIBUTE(a,b)
215 #endif /* __GNUC__ */
217 /* Utility macros to convert VTY argument to unsigned long */
218 #define VTY_GET_ULONG(NAME,V,STR) \
220 char *endptr = NULL; \
222 (V) = strtoul ((STR), &endptr, 10); \
225 vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \
226 return CMD_WARNING; \
228 if (*endptr != '\0') \
230 vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \
231 return CMD_WARNING; \
235 vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \
236 return CMD_WARNING; \
240 /* Utility macros to convert VTY argument to unsigned long long */
241 #define VTY_GET_ULL(NAME,V,STR) \
243 char *endptr = NULL; \
245 (V) = strtoull ((STR), &endptr, 10); \
248 vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \
249 return CMD_WARNING; \
251 if (*endptr != '\0') \
253 vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \
254 return CMD_WARNING; \
258 vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \
259 return CMD_WARNING; \
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).
270 #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \
272 VTY_GET_ULONG(NAME, (TMPL), STR); \
273 if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \
275 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\
276 return CMD_WARNING; \
280 #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
282 unsigned long long tmpl; \
283 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
287 #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \
289 unsigned long tmpl; \
290 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
293 #define VTY_GET_INTEGER(NAME,V,STR) \
294 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
296 #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
299 retv = inet_aton ((STR), &(V)); \
302 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
303 return CMD_WARNING; \
307 #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
310 retv = str2prefix_ipv4 ((STR), &(V)); \
313 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
314 return CMD_WARNING; \
318 #define VTY_WARN_EXPERIMENTAL() \
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); \
326 /* Exported variables */
327 extern char integrate_default
[];
330 extern void vty_init (struct thread_master
*);
331 extern void vty_init_vtysh (void);
332 extern void vty_terminate (void);
333 extern void vty_reset (void);
334 extern struct vty
*vty_new (void);
335 extern struct vty
*vty_stdio (void (*atclose
)(void));
336 extern int vty_out (struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
337 extern void vty_read_config (char *, char *);
338 extern void vty_time_print (struct vty
*, int);
339 extern void vty_serv_sock (const char *, unsigned short, const char *);
340 extern void vty_close (struct vty
*);
341 extern char *vty_get_cwd (void);
342 extern void vty_log (const char *level
, const char *proto
,
343 const char *fmt
, struct timestamp_control
*, va_list);
344 extern int vty_config_lock (struct vty
*);
345 extern int vty_config_unlock (struct vty
*);
346 extern int vty_shell (struct vty
*);
347 extern int vty_shell_serv (struct vty
*);
348 extern void vty_hello (struct vty
*);
350 /* Send a fixed-size message to all vty terminal monitors; this should be
351 an async-signal-safe function. */
352 extern void vty_log_fixed (char *buf
, size_t len
);
354 extern const char *vty_get_arg_value (struct vty_arg
**, const char *);
355 extern struct vty_arg
*vty_get_arg (struct vty_arg
**, const char *);
357 #endif /* _ZEBRA_VTY_H */