blob: 1e3b124399c83a5b157bcdb05288669d5adb479d [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Virtual terminal [aka TeletYpe] interface routine
2 Copyright (C) 1997 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#ifndef _ZEBRA_VTY_H
22#define _ZEBRA_VTY_H
23
paulb21b19c2003-06-15 01:28:29 +000024#include "thread.h"
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000025#include "log.h"
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +020026#include "sockunion.h"
paulb21b19c2003-06-15 01:28:29 +000027
paul718e3742002-12-13 20:15:29 +000028#define VTY_MAXHIST 20
29
30/* VTY struct. */
31struct vty
32{
33 /* File descripter of this vty. */
34 int fd;
35
David Lamparter4715a532013-05-30 16:31:49 +020036 /* output FD, to support stdin/stdout combination */
37 int wfd;
38
paul718e3742002-12-13 20:15:29 +000039 /* Is this vty connect to file or not */
40 enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;
41
42 /* Node status of this vty */
43 int node;
44
paul718e3742002-12-13 20:15:29 +000045 /* Failure count */
46 int fail;
47
48 /* Output buffer. */
49 struct buffer *obuf;
50
51 /* Command input buffer */
52 char *buf;
53
54 /* Command cursor point */
55 int cp;
56
57 /* Command length */
58 int length;
59
60 /* Command max length. */
61 int max;
62
63 /* Histry of command */
64 char *hist[VTY_MAXHIST];
65
66 /* History lookup current point */
67 int hp;
68
69 /* History insert end point */
70 int hindex;
71
72 /* For current referencing point of interface, route-map,
73 access-list etc... */
74 void *index;
75
76 /* For multiple level index treatment such as key chain and key. */
77 void *index_sub;
78
79 /* For escape character. */
80 unsigned char escape;
81
82 /* Current vty status. */
ajs5a646652004-11-05 01:25:55 +000083 enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;
paul718e3742002-12-13 20:15:29 +000084
ajs9fc7ebf2005-02-23 15:12:34 +000085 /* IAC handling: was the last character received the
86 IAC (interpret-as-command) escape character (and therefore the next
87 character will be the command code)? Refer to Telnet RFC 854. */
paul718e3742002-12-13 20:15:29 +000088 unsigned char iac;
89
ajs9fc7ebf2005-02-23 15:12:34 +000090 /* IAC SB (option subnegotiation) handling */
paul718e3742002-12-13 20:15:29 +000091 unsigned char iac_sb_in_progress;
ajs9fc7ebf2005-02-23 15:12:34 +000092 /* At the moment, we care only about the NAWS (window size) negotiation,
93 and that requires just a 5-character buffer (RFC 1073):
94 <NAWS char> <16-bit width> <16-bit height> */
95#define TELNET_NAWS_SB_LEN 5
96 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
97 /* How many subnegotiation characters have we received? We just drop
98 those that do not fit in the buffer. */
99 size_t sb_len;
paul718e3742002-12-13 20:15:29 +0000100
101 /* Window width/height. */
102 int width;
103 int height;
104
paul718e3742002-12-13 20:15:29 +0000105 /* Configure lines. */
106 int lines;
107
paul718e3742002-12-13 20:15:29 +0000108 /* Terminal monitor. */
109 int monitor;
110
111 /* In configure mode. */
112 int config;
113
114 /* Read and write thread. */
115 struct thread *t_read;
116 struct thread *t_write;
117
118 /* Timeout seconds and thread. */
119 unsigned long v_timeout;
120 struct thread *t_timeout;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +0200121
122 /* What address is this vty comming from. */
123 char address[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000124};
125
126/* Integrated configuration file. */
paule8f29842003-08-12 13:08:31 +0000127#define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
paul718e3742002-12-13 20:15:29 +0000128
129/* Small macro to determine newline is newline only or linefeed needed. */
130#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
131
132/* Default time out value */
133#define VTY_TIMEOUT_DEFAULT 600
134
135/* Vty read buffer size. */
136#define VTY_READ_BUFSIZ 512
137
138/* Directory separator. */
139#ifndef DIRECTORY_SEP
140#define DIRECTORY_SEP '/'
141#endif /* DIRECTORY_SEP */
142
143#ifndef IS_DIRECTORY_SEP
144#define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
145#endif
146
147/* GCC have printf type attribute check. */
148#ifdef __GNUC__
149#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
150#else
151#define PRINTF_ATTRIBUTE(a,b)
152#endif /* __GNUC__ */
153
Andrew Certain7798b632012-12-04 13:33:24 -0800154/* Utility macros to convert VTY argument to unsigned long */
155#define VTY_GET_ULONG(NAME,V,STR) \
pauld4f09602005-05-23 12:43:34 +0000156do { \
paul42d49862004-10-13 05:22:18 +0000157 char *endptr = NULL; \
Ulrich Weber664711c2011-12-21 02:24:11 +0400158 errno = 0; \
paul42d49862004-10-13 05:22:18 +0000159 (V) = strtoul ((STR), &endptr, 10); \
Ulrich Weber664711c2011-12-21 02:24:11 +0400160 if (*(STR) == '-' || *endptr != '\0' || errno) \
paul42d49862004-10-13 05:22:18 +0000161 { \
paul718e3742002-12-13 20:15:29 +0000162 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
paul42d49862004-10-13 05:22:18 +0000163 return CMD_WARNING; \
164 } \
pauld4f09602005-05-23 12:43:34 +0000165} while (0)
paul718e3742002-12-13 20:15:29 +0000166
Andrew Certain7798b632012-12-04 13:33:24 -0800167/*
168 * The logic below ((TMPL) <= ((MIN) && (TMPL) != (MIN)) is
169 * done to circumvent the compiler complaining about
170 * comparing unsigned numbers against zero, if MIN is zero.
171 * NB: The compiler isn't smart enough to supress the warning
172 * if you write (MIN) != 0 && tmpl < (MIN).
173 */
174#define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \
175do { \
176 VTY_GET_ULONG(NAME, (TMPL), STR); \
177 if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \
178 { \
179 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\
180 return CMD_WARNING; \
181 } \
pauld4f09602005-05-23 12:43:34 +0000182} while (0)
paul718e3742002-12-13 20:15:29 +0000183
Andrew Certain7798b632012-12-04 13:33:24 -0800184#define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
185do { \
186 unsigned long tmpl; \
187 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
188 (V) = tmpl; \
189} while (0)
190
191#define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \
192do { \
193 unsigned long tmpl; \
194 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
195} while (0)
196
197#define VTY_GET_INTEGER(NAME,V,STR) \
198 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
paul42d49862004-10-13 05:22:18 +0000199
paul8cc41982005-05-06 21:25:49 +0000200#define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
pauld4f09602005-05-23 12:43:34 +0000201do { \
paul8cc41982005-05-06 21:25:49 +0000202 int retv; \
203 retv = inet_aton ((STR), &(V)); \
204 if (!retv) \
205 { \
206 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
207 return CMD_WARNING; \
208 } \
pauld4f09602005-05-23 12:43:34 +0000209} while (0)
paul8cc41982005-05-06 21:25:49 +0000210
211#define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
pauld4f09602005-05-23 12:43:34 +0000212do { \
paul8cc41982005-05-06 21:25:49 +0000213 int retv; \
214 retv = str2prefix_ipv4 ((STR), &(V)); \
215 if (retv <= 0) \
216 { \
217 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
218 return CMD_WARNING; \
219 } \
pauld4f09602005-05-23 12:43:34 +0000220} while (0)
paul8cc41982005-05-06 21:25:49 +0000221
David Lamparter863f20c2015-01-27 20:24:15 +0100222#define VTY_WARN_EXPERIMENTAL() \
223do { \
224 vty_out (vty, "%% WARNING: this command is experimental. Both its name and" \
225 " parameters may%s%% change in a future version of Quagga," \
226 " possibly breaking your configuration!%s", \
227 VTY_NEWLINE, VTY_NEWLINE); \
228} while (0)
229
paul718e3742002-12-13 20:15:29 +0000230/* Exported variables */
231extern char integrate_default[];
232
233/* Prototypes. */
paul8cc41982005-05-06 21:25:49 +0000234extern void vty_init (struct thread_master *);
235extern void vty_init_vtysh (void);
Chris Caputo228da422009-07-18 05:44:03 +0000236extern void vty_terminate (void);
paul8cc41982005-05-06 21:25:49 +0000237extern void vty_reset (void);
238extern struct vty *vty_new (void);
David Lamparter464ccf32015-05-12 21:56:18 +0200239extern struct vty *vty_stdio (void (*atclose)(void));
paul8cc41982005-05-06 21:25:49 +0000240extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
241extern void vty_read_config (char *, char *);
242extern void vty_time_print (struct vty *, int);
243extern void vty_serv_sock (const char *, unsigned short, const char *);
244extern void vty_close (struct vty *);
245extern char *vty_get_cwd (void);
246extern void vty_log (const char *level, const char *proto,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000247 const char *fmt, struct timestamp_control *, va_list);
paul8cc41982005-05-06 21:25:49 +0000248extern int vty_config_lock (struct vty *);
249extern int vty_config_unlock (struct vty *);
250extern int vty_shell (struct vty *);
251extern int vty_shell_serv (struct vty *);
252extern void vty_hello (struct vty *);
paul718e3742002-12-13 20:15:29 +0000253
ajs274a4a42004-12-07 15:39:31 +0000254/* Send a fixed-size message to all vty terminal monitors; this should be
255 an async-signal-safe function. */
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100256extern void vty_log_fixed (char *buf, size_t len);
ajs274a4a42004-12-07 15:39:31 +0000257
paul718e3742002-12-13 20:15:29 +0000258#endif /* _ZEBRA_VTY_H */