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