]>
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 4096
30 #define VTY_MAXHIST 20
32 #if defined(VTY_DEPRECATE_INDEX) && defined(__GNUC__) && \
33 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && \
35 #define INDEX_WARNING __attribute__((deprecated))
43 /* File descripter of this vty. */
46 /* output FD, to support stdin/stdout combination */
49 /* Is this vty connect to file or not */
50 enum {VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
52 /* Node status of this vty */
61 /* Command input buffer */
64 /* Command input error buffer */
67 /* Command cursor point */
73 /* Command max length. */
76 /* Histry of command */
77 char *hist
[VTY_MAXHIST
];
79 /* History lookup current point */
82 /* History insert end point */
85 /* For current referencing point of interface, route-map,
87 void *index INDEX_WARNING
;
89 /* qobj object ID (replacement for "index") */
92 /* qobj second-level object ID (replacement for "index_sub") */
93 uint64_t qobj_index_sub
;
95 /* For escape character. */
98 /* Current vty status. */
99 enum {VTY_NORMAL
, VTY_CLOSE
, VTY_MORE
, VTY_MORELINE
} status
;
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. */
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. */
117 /* Window width/height. */
121 /* Configure lines. */
124 /* Terminal monitor. */
127 /* In configure mode. */
130 /* Read and write thread. */
131 struct thread
*t_read
;
132 struct thread
*t_write
;
134 /* Timeout seconds and thread. */
135 unsigned long v_timeout
;
136 struct thread
*t_timeout
;
138 /* What address is this vty comming from. */
139 char address
[SU_ADDRSTRLEN
];
144 static inline void vty_push_context(struct vty
*vty
,
145 int node
, uint64_t id
, void *idx
)
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"
154 #pragma GCC diagnostic pop
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); \
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)
176 /* will return if ptr is NULL. */
177 #define VTY_CHECK_CONTEXT(ptr) \
179 vty_out (vty, "Current configuration object was deleted " \
180 "by another process.%s", VTY_NEWLINE); \
181 return CMD_WARNING; \
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);
200 /* Integrated configuration file. */
201 #define INTEGRATE_DEFAULT_CONFIG "Frr.conf"
203 /* Small macro to determine newline is newline only or linefeed needed. */
204 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
206 /* Default time out value */
207 #define VTY_TIMEOUT_DEFAULT 600
209 /* Vty read buffer size. */
210 #define VTY_READ_BUFSIZ 512
212 /* Directory separator. */
213 #ifndef DIRECTORY_SEP
214 #define DIRECTORY_SEP '/'
215 #endif /* DIRECTORY_SEP */
217 #ifndef IS_DIRECTORY_SEP
218 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
221 /* GCC have printf type attribute check. */
223 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
225 #define PRINTF_ATTRIBUTE(a,b)
226 #endif /* __GNUC__ */
228 /* Utility macros to convert VTY argument to unsigned long */
229 #define VTY_GET_ULONG(NAME,V,STR) \
231 char *endptr = NULL; \
233 (V) = strtoul ((STR), &endptr, 10); \
236 vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \
237 return CMD_WARNING; \
239 if (*endptr != '\0') \
241 vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \
242 return CMD_WARNING; \
246 vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \
247 return CMD_WARNING; \
251 /* Utility macros to convert VTY argument to unsigned long long */
252 #define VTY_GET_ULL(NAME,V,STR) \
254 char *endptr = NULL; \
256 (V) = strtoull ((STR), &endptr, 10); \
259 vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \
260 return CMD_WARNING; \
262 if (*endptr != '\0') \
264 vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \
265 return CMD_WARNING; \
269 vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \
270 return CMD_WARNING; \
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).
281 #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \
283 VTY_GET_ULONG(NAME, (TMPL), STR); \
284 if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \
286 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\
287 return CMD_WARNING; \
291 #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
293 unsigned long long tmpl; \
294 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
298 #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \
300 unsigned long tmpl; \
301 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
304 #define VTY_GET_INTEGER(NAME,V,STR) \
305 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
307 #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
310 retv = inet_aton ((STR), &(V)); \
313 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
314 return CMD_WARNING; \
318 #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
321 retv = str2prefix_ipv4 ((STR), &(V)); \
324 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
325 return CMD_WARNING; \
329 #define VTY_WARN_EXPERIMENTAL() \
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); \
337 /* Exported variables */
338 extern char integrate_default
[];
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
*);
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
);
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 *);
369 #endif /* _ZEBRA_VTY_H */