]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
lib/vty: add vty_stdio at-close hook
[mirror_frr.git] / lib / vty.h
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
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
37 /* output FD, to support stdin/stdout combination */
38 int wfd;
39
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
46 /* Failure count */
47 int fail;
48
49 /* Output buffer. */
50 struct buffer *obuf;
51
52 /* Command input buffer */
53 char *buf;
54
55 /* Command input error buffer */
56 char *error_buf;
57
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. */
87 enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;
88
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. */
92 unsigned char iac;
93
94 /* IAC SB (option subnegotiation) handling */
95 unsigned char iac_sb_in_progress;
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;
104
105 /* Window width/height. */
106 int width;
107 int height;
108
109 /* Configure lines. */
110 int lines;
111
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;
125
126 /* What address is this vty comming from. */
127 char address[SU_ADDRSTRLEN];
128 };
129
130 /* Integrated configuration file. */
131 #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
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
158 /* Utility macros to convert VTY argument to unsigned long */
159 #define VTY_GET_ULONG(NAME,V,STR) \
160 do { \
161 char *endptr = NULL; \
162 errno = 0; \
163 (V) = strtoul ((STR), &endptr, 10); \
164 if (*(STR) == '-') \
165 { \
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); \
177 return CMD_WARNING; \
178 } \
179 } while (0)
180
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); \
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) \
198 { \
199 vty_out (vty, "%% Invalid %s value (error %d)%s", NAME, errno, VTY_NEWLINE); \
200 return CMD_WARNING; \
201 } \
202 } while (0)
203
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); \
232 } while (0)
233
234 #define VTY_GET_INTEGER(NAME,V,STR) \
235 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
236
237 #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
238 do { \
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 } \
246 } while (0)
247
248 #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
249 do { \
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 } \
257 } while (0)
258
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
267 /* Exported variables */
268 extern char integrate_default[];
269
270 /* Prototypes. */
271 extern void vty_init (struct thread_master *);
272 extern void vty_init_vtysh (void);
273 extern void vty_terminate (void);
274 extern void vty_reset (void);
275 extern struct vty *vty_new (void);
276 extern struct vty *vty_stdio (void (*atclose)(void));
277 extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
278 extern void vty_read_config (char *, char *);
279 extern void vty_time_print (struct vty *, int);
280 extern void vty_serv_sock (const char *, unsigned short, const char *);
281 extern void vty_close (struct vty *);
282 extern char *vty_get_cwd (void);
283 extern void vty_log (const char *level, const char *proto,
284 const char *fmt, struct timestamp_control *, va_list);
285 extern int vty_config_lock (struct vty *);
286 extern int vty_config_unlock (struct vty *);
287 extern int vty_shell (struct vty *);
288 extern int vty_shell_serv (struct vty *);
289 extern void vty_hello (struct vty *);
290
291 /* Send a fixed-size message to all vty terminal monitors; this should be
292 an async-signal-safe function. */
293 extern void vty_log_fixed (char *buf, size_t len);
294
295 #endif /* _ZEBRA_VTY_H */