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