blob: e5158687d0fa99f68056aefaaf0148b90e466bb3 [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
paul42d49862004-10-13 05:22:18 +0000152/* Utility macros to convert VTY argument to unsigned long or integer. */
153#define VTY_GET_LONG(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
paul42d49862004-10-13 05:22:18 +0000165#define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
pauld4f09602005-05-23 12:43:34 +0000166do { \
paul42d49862004-10-13 05:22:18 +0000167 unsigned long tmpl; \
pauld4f09602005-05-23 12:43:34 +0000168 VTY_GET_LONG(NAME, tmpl, STR); \
169 if ( (tmpl < (MIN)) || (tmpl > (MAX))) \
paul42d49862004-10-13 05:22:18 +0000170 { \
paul718e3742002-12-13 20:15:29 +0000171 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
paul42d49862004-10-13 05:22:18 +0000172 return CMD_WARNING; \
173 } \
174 (V) = tmpl; \
pauld4f09602005-05-23 12:43:34 +0000175} while (0)
paul718e3742002-12-13 20:15:29 +0000176
paul42d49862004-10-13 05:22:18 +0000177#define VTY_GET_INTEGER(NAME,V,STR) \
178 VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
179
paul8cc41982005-05-06 21:25:49 +0000180#define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
pauld4f09602005-05-23 12:43:34 +0000181do { \
paul8cc41982005-05-06 21:25:49 +0000182 int retv; \
183 retv = inet_aton ((STR), &(V)); \
184 if (!retv) \
185 { \
186 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
187 return CMD_WARNING; \
188 } \
pauld4f09602005-05-23 12:43:34 +0000189} while (0)
paul8cc41982005-05-06 21:25:49 +0000190
191#define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
pauld4f09602005-05-23 12:43:34 +0000192do { \
paul8cc41982005-05-06 21:25:49 +0000193 int retv; \
194 retv = str2prefix_ipv4 ((STR), &(V)); \
195 if (retv <= 0) \
196 { \
197 vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
198 return CMD_WARNING; \
199 } \
pauld4f09602005-05-23 12:43:34 +0000200} while (0)
paul8cc41982005-05-06 21:25:49 +0000201
paul718e3742002-12-13 20:15:29 +0000202/* Exported variables */
203extern char integrate_default[];
204
205/* Prototypes. */
paul8cc41982005-05-06 21:25:49 +0000206extern void vty_init (struct thread_master *);
207extern void vty_init_vtysh (void);
Chris Caputo228da422009-07-18 05:44:03 +0000208extern void vty_terminate (void);
paul8cc41982005-05-06 21:25:49 +0000209extern void vty_reset (void);
210extern struct vty *vty_new (void);
211extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
212extern void vty_read_config (char *, char *);
213extern void vty_time_print (struct vty *, int);
214extern void vty_serv_sock (const char *, unsigned short, const char *);
215extern void vty_close (struct vty *);
216extern char *vty_get_cwd (void);
217extern void vty_log (const char *level, const char *proto,
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000218 const char *fmt, struct timestamp_control *, va_list);
paul8cc41982005-05-06 21:25:49 +0000219extern int vty_config_lock (struct vty *);
220extern int vty_config_unlock (struct vty *);
221extern int vty_shell (struct vty *);
222extern int vty_shell_serv (struct vty *);
223extern void vty_hello (struct vty *);
paul718e3742002-12-13 20:15:29 +0000224
ajs274a4a42004-12-07 15:39:31 +0000225/* Send a fixed-size message to all vty terminal monitors; this should be
226 an async-signal-safe function. */
227extern void vty_log_fixed (const char *buf, size_t len);
228
paul718e3742002-12-13 20:15:29 +0000229#endif /* _ZEBRA_VTY_H */