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"
28 #define VTY_MAXHIST 20
33 /* File descripter of this vty. */
36 /* output FD, to support stdin/stdout combination */
39 /* Is this vty connect to file or not */
40 enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;
42 /* Node status of this vty */
51 /* Command input buffer */
54 /* Command cursor point */
60 /* Command max length. */
63 /* Histry of command */
64 char *hist[VTY_MAXHIST];
66 /* History lookup current point */
69 /* History insert end point */
72 /* For current referencing point of interface, route-map,
76 /* For multiple level index treatment such as key chain and key. */
79 /* For escape character. */
82 /* Current vty status. */
83 enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;
85 /* IAC handling: was the last character received the
86 IAC (interpret-as-command) escape character (and therefore the next
87 character will be the command code)? Refer to Telnet RFC 854. */
90 /* IAC SB (option subnegotiation) handling */
91 unsigned char iac_sb_in_progress;
92 /* At the moment, we care only about the NAWS (window size) negotiation,
93 and that requires just a 5-character buffer (RFC 1073):
94 <NAWS char> <16-bit width> <16-bit height> */
95 #define TELNET_NAWS_SB_LEN 5
96 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
97 /* How many subnegotiation characters have we received? We just drop
98 those that do not fit in the buffer. */
101 /* Window width/height. */
105 /* Configure lines. */
108 /* Terminal monitor. */
111 /* In configure mode. */
114 /* Read and write thread. */
115 struct thread *t_read;
116 struct thread *t_write;
118 /* Timeout seconds and thread. */
119 unsigned long v_timeout;
120 struct thread *t_timeout;
122 /* What address is this vty comming from. */
123 char address[SU_ADDRSTRLEN];
126 /* Integrated configuration file. */
127 #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
129 /* Small macro to determine newline is newline only or linefeed needed. */
130 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
132 /* Default time out value */
133 #define VTY_TIMEOUT_DEFAULT 600
135 /* Vty read buffer size. */
136 #define VTY_READ_BUFSIZ 512
138 /* Directory separator. */
139 #ifndef DIRECTORY_SEP
140 #define DIRECTORY_SEP '/'
141 #endif /* DIRECTORY_SEP */
143 #ifndef IS_DIRECTORY_SEP
144 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
147 /* GCC have printf type attribute check. */
149 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
151 #define PRINTF_ATTRIBUTE(a,b)
152 #endif /* __GNUC__ */
154 /* Utility macros to convert VTY argument to unsigned long */
155 #define VTY_GET_ULONG(NAME,V,STR) \
157 char *endptr = NULL; \
159 (V) = strtoul ((STR), &endptr, 10); \
160 if (*(STR) == '-' || *endptr != '\0' || errno) \
162 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
163 return CMD_WARNING; \
168 * The logic below ((TMPL) <= ((MIN) && (TMPL) != (MIN)) is
169 * done to circumvent the compiler complaining about
170 * comparing unsigned numbers against zero, if MIN is zero.
171 * NB: The compiler isn't smart enough to suprress the warning
172 * if you write (MIN) != 0 && tmpl < (MIN).
174 #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \
176 VTY_GET_ULONG(NAME, (TMPL), STR); \
177 if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \
179 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\
180 return CMD_WARNING; \
184 #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
186 unsigned long tmpl; \
187 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
191 #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \
193 unsigned long tmpl; \
194 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
197 #define VTY_GET_INTEGER(NAME,V,STR) \
198 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
200 #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
203 retv = inet_aton ((STR), &(V)); \
206 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
207 return CMD_WARNING; \
211 #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
214 retv = str2prefix_ipv4 ((STR), &(V)); \
217 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
218 return CMD_WARNING; \
222 #define VTY_WARN_EXPERIMENTAL() \
224 vty_out (vty, "%% WARNING: this command is experimental. Both its name and" \
225 " parameters may%s%% change in a future version of Quagga," \
226 " possibly breaking your configuration!%s", \
227 VTY_NEWLINE, VTY_NEWLINE); \
230 /* Exported variables */
231 extern char integrate_default[];
234 extern void vty_init (struct thread_master *);
235 extern void vty_init_vtysh (void);
236 extern void vty_terminate (void);
237 extern void vty_reset (void);
238 extern struct vty *vty_new (void);
239 extern struct vty *vty_stdio (void (*atclose)(void));
240 extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
241 extern void vty_read_config (char *, char *);
242 extern void vty_time_print (struct vty *, int);
243 extern void vty_serv_sock (const char *, unsigned short, const char *);
244 extern void vty_close (struct vty *);
245 extern char *vty_get_cwd (void);
246 extern void vty_log (const char *level, const char *proto,
247 const char *fmt, struct timestamp_control *, va_list);
248 extern int vty_config_lock (struct vty *);
249 extern int vty_config_unlock (struct vty *);
250 extern int vty_shell (struct vty *);
251 extern int vty_shell_serv (struct vty *);
252 extern void vty_hello (struct vty *);
254 /* Send a fixed-size message to all vty terminal monitors; this should be
255 an async-signal-safe function. */
256 extern void vty_log_fixed (char *buf, size_t len);
258 #endif /* _ZEBRA_VTY_H */