]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | #include "thread.h" | |
25 | #include "log.h" | |
26 | #include "sockunion.h" | |
27 | #include "qobj.h" | |
28 | ||
29 | #define VTY_BUFSIZ 512 | |
30 | #define VTY_MAXHIST 20 | |
31 | ||
32 | #if defined(VTY_DEPRECATE_INDEX) && defined(__GNUC__) && \ | |
33 | (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) | |
34 | #define INDEX_WARNING __attribute__((deprecated)) | |
35 | #else | |
36 | #define INDEX_WARNING | |
37 | #endif | |
38 | ||
39 | /* VTY struct. */ | |
40 | struct vty | |
41 | { | |
42 | /* File descripter of this vty. */ | |
43 | int fd; | |
44 | ||
45 | /* output FD, to support stdin/stdout combination */ | |
46 | int wfd; | |
47 | ||
48 | /* Is this vty connect to file or not */ | |
49 | enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type; | |
50 | ||
51 | /* Node status of this vty */ | |
52 | int node; | |
53 | ||
54 | /* Failure count */ | |
55 | int fail; | |
56 | ||
57 | /* Output buffer. */ | |
58 | struct buffer *obuf; | |
59 | ||
60 | /* Command input buffer */ | |
61 | char *buf; | |
62 | ||
63 | /* Command input error buffer */ | |
64 | char *error_buf; | |
65 | ||
66 | /* Command cursor point */ | |
67 | int cp; | |
68 | ||
69 | /* Command length */ | |
70 | int length; | |
71 | ||
72 | /* Command max length. */ | |
73 | int max; | |
74 | ||
75 | /* Histry of command */ | |
76 | char *hist[VTY_MAXHIST]; | |
77 | ||
78 | /* History lookup current point */ | |
79 | int hp; | |
80 | ||
81 | /* History insert end point */ | |
82 | int hindex; | |
83 | ||
84 | /* For current referencing point of interface, route-map, | |
85 | access-list etc... */ | |
86 | void *index INDEX_WARNING; | |
87 | ||
88 | /* qobj object ID (replacement for "index") */ | |
89 | uint64_t qobj_index; | |
90 | ||
91 | /* For multiple level index treatment such as key chain and key. */ | |
92 | void *index_sub; | |
93 | ||
94 | /* For escape character. */ | |
95 | unsigned char escape; | |
96 | ||
97 | /* Current vty status. */ | |
98 | enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status; | |
99 | ||
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. */ | |
103 | unsigned char iac; | |
104 | ||
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. */ | |
114 | size_t sb_len; | |
115 | ||
116 | /* Window width/height. */ | |
117 | int width; | |
118 | int height; | |
119 | ||
120 | /* Configure lines. */ | |
121 | int lines; | |
122 | ||
123 | /* Terminal monitor. */ | |
124 | int monitor; | |
125 | ||
126 | /* In configure mode. */ | |
127 | int config; | |
128 | ||
129 | /* Read and write thread. */ | |
130 | struct thread *t_read; | |
131 | struct thread *t_write; | |
132 | ||
133 | /* Timeout seconds and thread. */ | |
134 | unsigned long v_timeout; | |
135 | struct thread *t_timeout; | |
136 | ||
137 | /* What address is this vty comming from. */ | |
138 | char address[SU_ADDRSTRLEN]; | |
139 | }; | |
140 | ||
141 | #undef INDEX_WARNING | |
142 | ||
143 | static inline void vty_push_context(struct vty *vty, | |
144 | int node, uint64_t id, void *idx) | |
145 | { | |
146 | vty->node = node; | |
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" | |
152 | vty->index = idx; | |
153 | #pragma GCC diagnostic pop | |
154 | #else | |
155 | vty->index = idx; | |
156 | #endif | |
157 | } | |
158 | ||
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) | |
163 | ||
164 | /* can return NULL if context is invalid! */ | |
165 | #define VTY_GET_CONTEXT(structname) \ | |
166 | QOBJ_GET_TYPESAFE(vty->qobj_index, structname) | |
167 | ||
168 | /* will return if ptr is NULL. */ | |
169 | #define VTY_CHECK_CONTEXT(ptr) \ | |
170 | if (!ptr) { \ | |
171 | vty_out (vty, "Current configuration object was deleted " \ | |
172 | "by another process.%s", VTY_NEWLINE); \ | |
173 | return CMD_WARNING; \ | |
174 | } | |
175 | ||
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); | |
180 | ||
181 | struct vty_arg | |
182 | { | |
183 | const char *name; | |
184 | const char *value; | |
185 | const char **argv; | |
186 | int argc; | |
187 | }; | |
188 | ||
189 | /* Integrated configuration file. */ | |
190 | #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf" | |
191 | ||
192 | /* Small macro to determine newline is newline only or linefeed needed. */ | |
193 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") | |
194 | ||
195 | /* Default time out value */ | |
196 | #define VTY_TIMEOUT_DEFAULT 600 | |
197 | ||
198 | /* Vty read buffer size. */ | |
199 | #define VTY_READ_BUFSIZ 512 | |
200 | ||
201 | /* Directory separator. */ | |
202 | #ifndef DIRECTORY_SEP | |
203 | #define DIRECTORY_SEP '/' | |
204 | #endif /* DIRECTORY_SEP */ | |
205 | ||
206 | #ifndef IS_DIRECTORY_SEP | |
207 | #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP) | |
208 | #endif | |
209 | ||
210 | /* GCC have printf type attribute check. */ | |
211 | #ifdef __GNUC__ | |
212 | #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) | |
213 | #else | |
214 | #define PRINTF_ATTRIBUTE(a,b) | |
215 | #endif /* __GNUC__ */ | |
216 | ||
217 | /* Utility macros to convert VTY argument to unsigned long */ | |
218 | #define VTY_GET_ULONG(NAME,V,STR) \ | |
219 | do { \ | |
220 | char *endptr = NULL; \ | |
221 | errno = 0; \ | |
222 | (V) = strtoul ((STR), &endptr, 10); \ | |
223 | if (*(STR) == '-') \ | |
224 | { \ | |
225 | vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \ | |
226 | return CMD_WARNING; \ | |
227 | } \ | |
228 | if (*endptr != '\0') \ | |
229 | { \ | |
230 | vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \ | |
231 | return CMD_WARNING; \ | |
232 | } \ | |
233 | if (errno) \ | |
234 | { \ | |
235 | vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \ | |
236 | return CMD_WARNING; \ | |
237 | } \ | |
238 | } while (0) | |
239 | ||
240 | /* Utility macros to convert VTY argument to unsigned long long */ | |
241 | #define VTY_GET_ULL(NAME,V,STR) \ | |
242 | do { \ | |
243 | char *endptr = NULL; \ | |
244 | errno = 0; \ | |
245 | (V) = strtoull ((STR), &endptr, 10); \ | |
246 | if (*(STR) == '-') \ | |
247 | { \ | |
248 | vty_out (vty, "%% Invalid %s value (dash)%s", NAME, VTY_NEWLINE); \ | |
249 | return CMD_WARNING; \ | |
250 | } \ | |
251 | if (*endptr != '\0') \ | |
252 | { \ | |
253 | vty_out (vty, "%% Invalid %s value (%s)%s", NAME, endptr, VTY_NEWLINE); \ | |
254 | return CMD_WARNING; \ | |
255 | } \ | |
256 | if (errno) \ | |
257 | { \ | |
258 | vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \ | |
259 | return CMD_WARNING; \ | |
260 | } \ | |
261 | } while (0) | |
262 | ||
263 | /* | |
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). | |
269 | */ | |
270 | #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \ | |
271 | do { \ | |
272 | VTY_GET_ULONG(NAME, (TMPL), STR); \ | |
273 | if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \ | |
274 | { \ | |
275 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\ | |
276 | return CMD_WARNING; \ | |
277 | } \ | |
278 | } while (0) | |
279 | ||
280 | #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \ | |
281 | do { \ | |
282 | unsigned long long tmpl; \ | |
283 | VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \ | |
284 | (V) = tmpl; \ | |
285 | } while (0) | |
286 | ||
287 | #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \ | |
288 | do { \ | |
289 | unsigned long tmpl; \ | |
290 | VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \ | |
291 | } while (0) | |
292 | ||
293 | #define VTY_GET_INTEGER(NAME,V,STR) \ | |
294 | VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX) | |
295 | ||
296 | #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \ | |
297 | do { \ | |
298 | int retv; \ | |
299 | retv = inet_aton ((STR), &(V)); \ | |
300 | if (!retv) \ | |
301 | { \ | |
302 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \ | |
303 | return CMD_WARNING; \ | |
304 | } \ | |
305 | } while (0) | |
306 | ||
307 | #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \ | |
308 | do { \ | |
309 | int retv; \ | |
310 | retv = str2prefix_ipv4 ((STR), &(V)); \ | |
311 | if (retv <= 0) \ | |
312 | { \ | |
313 | vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \ | |
314 | return CMD_WARNING; \ | |
315 | } \ | |
316 | } while (0) | |
317 | ||
318 | #define VTY_WARN_EXPERIMENTAL() \ | |
319 | do { \ | |
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); \ | |
324 | } while (0) | |
325 | ||
326 | /* Exported variables */ | |
327 | extern char integrate_default[]; | |
328 | ||
329 | /* Prototypes. */ | |
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 *); | |
349 | ||
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); | |
353 | ||
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 *); | |
356 | ||
357 | #endif /* _ZEBRA_VTY_H */ |