]>
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 | ||
130 | /* Integrated configuration file. */ | |
e8f2984c | 131 | #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf" |
718e3744 | 132 | |
133 | /* Small macro to determine newline is newline only or linefeed needed. */ | |
134 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") | |
135 | ||
136 | /* Default time out value */ | |
137 | #define VTY_TIMEOUT_DEFAULT 600 | |
138 | ||
139 | /* Vty read buffer size. */ | |
140 | #define VTY_READ_BUFSIZ 512 | |
141 | ||
142 | /* Directory separator. */ | |
143 | #ifndef DIRECTORY_SEP | |
144 | #define DIRECTORY_SEP '/' | |
145 | #endif /* DIRECTORY_SEP */ | |
146 | ||
147 | #ifndef IS_DIRECTORY_SEP | |
148 | #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP) | |
149 | #endif | |
150 | ||
151 | /* GCC have printf type attribute check. */ | |
152 | #ifdef __GNUC__ | |
153 | #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) | |
154 | #else | |
155 | #define PRINTF_ATTRIBUTE(a,b) | |
156 | #endif /* __GNUC__ */ | |
157 | ||
7798b632 AC |
158 | /* Utility macros to convert VTY argument to unsigned long */ |
159 | #define VTY_GET_ULONG(NAME,V,STR) \ | |
d4f0960c | 160 | do { \ |
42d49865 | 161 | char *endptr = NULL; \ |
664711c1 | 162 | errno = 0; \ |
42d49865 | 163 | (V) = strtoul ((STR), &endptr, 10); \ |
813d4307 | 164 | if (*(STR) == '-') \ |
42d49865 | 165 | { \ |
813d4307 DW |
166 | vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \ |
167 | return CMD_WARNING; \ | |
168 | } \ | |
169 | if (*endptr != '\0') \ | |
170 | { \ | |
171 | vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \ | |
172 | return CMD_WARNING; \ | |
173 | } \ | |
174 | if (errno) \ | |
175 | { \ | |
176 | vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \ | |
42d49865 | 177 | return CMD_WARNING; \ |
178 | } \ | |
d4f0960c | 179 | } while (0) |
718e3744 | 180 | |
8fe8a7f6 DS |
181 | /* Utility macros to convert VTY argument to unsigned long long */ |
182 | #define VTY_GET_ULL(NAME,V,STR) \ | |
183 | do { \ | |
184 | char *endptr = NULL; \ | |
185 | errno = 0; \ | |
186 | (V) = strtoull ((STR), &endptr, 10); \ | |
813d4307 DW |
187 | if (*(STR) == '-') \ |
188 | { \ | |
189 | vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \ | |
190 | return CMD_WARNING; \ | |
191 | } \ | |
192 | if (*endptr != '\0') \ | |
193 | { \ | |
194 | vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \ | |
195 | return CMD_WARNING; \ | |
196 | } \ | |
197 | if (errno) \ | |
8fe8a7f6 | 198 | { \ |
813d4307 | 199 | vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \ |
8fe8a7f6 DS |
200 | return CMD_WARNING; \ |
201 | } \ | |
202 | } while (0) | |
203 | ||
7798b632 AC |
204 | /* |
205 | * The logic below ((TMPL) <= ((MIN) && (TMPL) != (MIN)) is | |
206 | * done to circumvent the compiler complaining about | |
207 | * comparing unsigned numbers against zero, if MIN is zero. | |
208 | * NB: The compiler isn't smart enough to supress the warning | |
209 | * if you write (MIN) != 0 && tmpl < (MIN). | |
210 | */ | |
211 | #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \ | |
212 | do { \ | |
213 | VTY_GET_ULONG(NAME, (TMPL), STR); \ | |
214 | if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \ | |
215 | { \ | |
216 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\ | |
217 | return CMD_WARNING; \ | |
218 | } \ | |
219 | } while (0) | |
220 | ||
221 | #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \ | |
222 | do { \ | |
223 | unsigned long tmpl; \ | |
224 | VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \ | |
225 | (V) = tmpl; \ | |
226 | } while (0) | |
227 | ||
228 | #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \ | |
229 | do { \ | |
230 | unsigned long tmpl; \ | |
231 | VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \ | |
d4f0960c | 232 | } while (0) |
718e3744 | 233 | |
7798b632 AC |
234 | #define VTY_GET_INTEGER(NAME,V,STR) \ |
235 | VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX) | |
42d49865 | 236 | |
8cc4198f | 237 | #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \ |
d4f0960c | 238 | do { \ |
8cc4198f | 239 | int retv; \ |
240 | retv = inet_aton ((STR), &(V)); \ | |
241 | if (!retv) \ | |
242 | { \ | |
243 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \ | |
244 | return CMD_WARNING; \ | |
245 | } \ | |
d4f0960c | 246 | } while (0) |
8cc4198f | 247 | |
248 | #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \ | |
d4f0960c | 249 | do { \ |
8cc4198f | 250 | int retv; \ |
251 | retv = str2prefix_ipv4 ((STR), &(V)); \ | |
252 | if (retv <= 0) \ | |
253 | { \ | |
254 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \ | |
255 | return CMD_WARNING; \ | |
256 | } \ | |
d4f0960c | 257 | } while (0) |
8cc4198f | 258 | |
0b0668e6 DL |
259 | #define VTY_WARN_EXPERIMENTAL() \ |
260 | do { \ | |
261 | vty_out (vty, "%% WARNING: this command is experimental. Both its name and" \ | |
262 | " parameters may%s%% change in a future version of Quagga," \ | |
263 | " possibly breaking your configuration!%s", \ | |
264 | VTY_NEWLINE, VTY_NEWLINE); \ | |
265 | } while (0) | |
266 | ||
718e3744 | 267 | /* Exported variables */ |
268 | extern char integrate_default[]; | |
269 | ||
270 | /* Prototypes. */ | |
8cc4198f | 271 | extern void vty_init (struct thread_master *); |
272 | extern void vty_init_vtysh (void); | |
228da428 | 273 | extern void vty_terminate (void); |
8cc4198f | 274 | extern void vty_reset (void); |
275 | extern struct vty *vty_new (void); | |
276 | extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); | |
277 | extern void vty_read_config (char *, char *); | |
278 | extern void vty_time_print (struct vty *, int); | |
279 | extern void vty_serv_sock (const char *, unsigned short, const char *); | |
280 | extern void vty_close (struct vty *); | |
281 | extern char *vty_get_cwd (void); | |
282 | extern void vty_log (const char *level, const char *proto, | |
1ed72e0b | 283 | const char *fmt, struct timestamp_control *, va_list); |
8cc4198f | 284 | extern int vty_config_lock (struct vty *); |
285 | extern int vty_config_unlock (struct vty *); | |
286 | extern int vty_shell (struct vty *); | |
287 | extern int vty_shell_serv (struct vty *); | |
288 | extern void vty_hello (struct vty *); | |
718e3744 | 289 | |
274a4a44 | 290 | /* Send a fixed-size message to all vty terminal monitors; this should be |
291 | an async-signal-safe function. */ | |
24873f0c | 292 | extern void vty_log_fixed (char *buf, size_t len); |
274a4a44 | 293 | |
718e3744 | 294 | #endif /* _ZEBRA_VTY_H */ |