blob: 4d6048c952efc1dd5016d06d7bcc454b9b180e9f [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_BUFSIZ 512
29#define VTY_MAXHIST 20
30
31/* VTY struct. */
32struct vty
33{
34 /* File descripter of this vty. */
35 int fd;
36
37 /* Is this vty connect to file or not */
38 enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;
39
40 /* Node status of this vty */
41 int node;
42
paul718e3742002-12-13 20:15:29 +000043 /* Failure count */
44 int fail;
45
46 /* Output buffer. */
47 struct buffer *obuf;
48
49 /* Command input buffer */
50 char *buf;
51
52 /* Command cursor point */
53 int cp;
54
55 /* Command length */
56 int length;
57
58 /* Command max length. */
59 int max;
60
61 /* Histry of command */
62 char *hist[VTY_MAXHIST];
63
64 /* History lookup current point */
65 int hp;
66
67 /* History insert end point */
68 int hindex;
69
70 /* For current referencing point of interface, route-map,
71 access-list etc... */
72 void *index;
73
74 /* For multiple level index treatment such as key chain and key. */
75 void *index_sub;
76
77 /* For escape character. */
78 unsigned char escape;
79
80 /* Current vty status. */
ajs5a646652004-11-05 01:25:55 +000081 enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;
paul718e3742002-12-13 20:15:29 +000082
ajs9fc7ebf2005-02-23 15:12:34 +000083 /* IAC handling: was the last character received the
84 IAC (interpret-as-command) escape character (and therefore the next
85 character will be the command code)? Refer to Telnet RFC 854. */
paul718e3742002-12-13 20:15:29 +000086 unsigned char iac;
87
ajs9fc7ebf2005-02-23 15:12:34 +000088 /* IAC SB (option subnegotiation) handling */
paul718e3742002-12-13 20:15:29 +000089 unsigned char iac_sb_in_progress;
ajs9fc7ebf2005-02-23 15:12:34 +000090 /* At the moment, we care only about the NAWS (window size) negotiation,
91 and that requires just a 5-character buffer (RFC 1073):
92 <NAWS char> <16-bit width> <16-bit height> */
93#define TELNET_NAWS_SB_LEN 5
94 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
95 /* How many subnegotiation characters have we received? We just drop
96 those that do not fit in the buffer. */
97 size_t sb_len;
paul718e3742002-12-13 20:15:29 +000098
99 /* Window width/height. */
100 int width;
101 int height;
102
paul718e3742002-12-13 20:15:29 +0000103 /* Configure lines. */
104 int lines;
105
paul718e3742002-12-13 20:15:29 +0000106 /* Terminal monitor. */
107 int monitor;
108
109 /* In configure mode. */
110 int config;
111
112 /* Read and write thread. */
113 struct thread *t_read;
114 struct thread *t_write;
115
116 /* Timeout seconds and thread. */
117 unsigned long v_timeout;
118 struct thread *t_timeout;
Jorge Boncompte [DTI2]d2276172012-04-10 16:57:23 +0200119
120 /* What address is this vty comming from. */
121 char address[SU_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +0000122};
123
124/* Integrated configuration file. */
paule8f29842003-08-12 13:08:31 +0000125#define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
paul718e3742002-12-13 20:15:29 +0000126
127/* Small macro to determine newline is newline only or linefeed needed. */
128#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
129
130/* Default time out value */
131#define VTY_TIMEOUT_DEFAULT 600
132
133/* Vty read buffer size. */
134#define VTY_READ_BUFSIZ 512
135
136/* Directory separator. */
137#ifndef DIRECTORY_SEP
138#define DIRECTORY_SEP '/'
139#endif /* DIRECTORY_SEP */
140
141#ifndef IS_DIRECTORY_SEP
142#define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
143#endif
144
145/* GCC have printf type attribute check. */
146#ifdef __GNUC__
147#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
148#else
149#define PRINTF_ATTRIBUTE(a,b)
150#endif /* __GNUC__ */
151
Andrew Certain7798b632012-12-04 13:33:24 -0800152/* Utility macros to convert VTY argument to unsigned long */
153#define VTY_GET_ULONG(NAME,V,STR) \
pauld4f09602005-05-23 12:43:34 +0000154do { \
paul42d49862004-10-13 05:22:18 +0000155 char *endptr = NULL; \
Ulrich Weber664711c2011-12-21 02:24:11 +0400156 errno = 0; \
paul42d49862004-10-13 05:22:18 +0000157 (V) = strtoul ((STR), &endptr, 10); \
Ulrich Weber664711c2011-12-21 02:24:11 +0400158 if (*(STR) == '-' || *endptr != '\0' || errno) \
paul42d49862004-10-13 05:22:18 +0000159 { \
paul718e3742002-12-13 20:15:29 +0000160 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
paul42d49862004-10-13 05:22:18 +0000161 return CMD_WARNING; \
162 } \
pauld4f09602005-05-23 12:43:34 +0000163} while (0)
paul718e3742002-12-13 20:15:29 +0000164
Andrew Certain7798b632012-12-04 13:33:24 -0800165/*
166 * The logic below ((TMPL) <= ((MIN) && (TMPL) != (MIN)) is
167 * done to circumvent the compiler complaining about
168 * comparing unsigned numbers against zero, if MIN is zero.
169 * NB: The compiler isn't smart enough to supress the warning
170 * if you write (MIN) != 0 && tmpl < (MIN).
171 */
172#define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \
173do { \
174 VTY_GET_ULONG(NAME, (TMPL), STR); \
175 if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \
176 { \
177 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\
178 return CMD_WARNING; \
179 } \
pauld4f09602005-05-23 12:43:34 +0000180} while (0)
paul718e3742002-12-13 20:15:29 +0000181
Andrew Certain7798b632012-12-04 13:33:24 -0800182#define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
183do { \
184 unsigned long tmpl; \
185 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
186 (V) = tmpl; \
187} while (0)
188
189#define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \
190do { \
191 unsigned long tmpl; \
192 VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
193} while (0)
194
195#define VTY_GET_INTEGER(NAME,V,STR) \
196 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
paul42d49862004-10-13 05:22:18 +0000197
paul8cc41982005-05-06 21:25:49 +0000198#define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
pauld4f09602005-05-23 12:43:34 +0000199do { \
paul8cc41982005-05-06 21:25:49 +0000200 int retv; \
201 retv = inet_aton ((STR), &(V)); \
202 if (!retv) \
203 { \
204 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
205 return CMD_WARNING; \
206 } \
pauld4f09602005-05-23 12:43:34 +0000207} while (0)
paul8cc41982005-05-06 21:25:49 +0000208
209#define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
pauld4f09602005-05-23 12:43:34 +0000210do { \
paul8cc41982005-05-06 21:25:49 +0000211 int retv; \
212 retv = str2prefix_ipv4 ((STR), &(V)); \
213 if (retv <= 0) \
214 { \
215 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
216 return CMD_WARNING; \
217 } \
pauld4f09602005-05-23 12:43:34 +0000218} while (0)
paul8cc41982005-05-06 21:25:49 +0000219
paul718e3742002-12-13 20:15:29 +0000220/* Exported variables */
221extern char integrate_default[];
222
223/* Prototypes. */
paul8cc41982005-05-06 21:25:49 +0000224extern void vty_init (struct thread_master *);
225extern void vty_init_vtysh (void);
Chris Caputo228da422009-07-18 05:44:03 +0000226extern void vty_terminate (void);
paul8cc41982005-05-06 21:25:49 +0000227extern void vty_reset (void);
228extern struct vty *vty_new (void);
229extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
230extern void vty_read_config (char *, char *);
231extern void vty_time_print (struct vty *, int);
232extern void vty_serv_sock (const char *, unsigned short, const char *);
233extern void vty_close (struct vty *);
234extern char *vty_get_cwd (void);
235extern void vty_log (const char *level, const char *proto,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000236 const char *fmt, struct timestamp_control *, va_list);
paul8cc41982005-05-06 21:25:49 +0000237extern int vty_config_lock (struct vty *);
238extern int vty_config_unlock (struct vty *);
239extern int vty_shell (struct vty *);
240extern int vty_shell_serv (struct vty *);
241extern void vty_hello (struct vty *);
paul718e3742002-12-13 20:15:29 +0000242
ajs274a4a42004-12-07 15:39:31 +0000243/* Send a fixed-size message to all vty terminal monitors; this should be
244 an async-signal-safe function. */
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100245extern void vty_log_fixed (char *buf, size_t len);
ajs274a4a42004-12-07 15:39:31 +0000246
paul718e3742002-12-13 20:15:29 +0000247#endif /* _ZEBRA_VTY_H */