]> git.proxmox.com Git - mirror_frr.git/blame - lib/vty.h
Merge branch 'pull/3165'
[mirror_frr.git] / lib / vty.h
CommitLineData
718e3744 1/* Virtual terminal [aka TeletYpe] interface routine
896014f4
DL
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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
718e3744 20
21#ifndef _ZEBRA_VTY_H
22#define _ZEBRA_VTY_H
23
fe6b47b9
QY
24#include <sys/types.h>
25#include <regex.h>
26
b21b19c5 27#include "thread.h"
1ed72e0b 28#include "log.h"
d227617a 29#include "sockunion.h"
0878c8d4 30#include "qobj.h"
de1a880c 31#include "compiler.h"
b21b19c5 32
2af38873 33#define VTY_BUFSIZ 4096
718e3744 34#define VTY_MAXHIST 20
35
7ab57d19
DS
36struct vty_error {
37 char error_buf[VTY_BUFSIZ];
38 uint32_t line_num;
39};
40
718e3744 41/* VTY struct. */
d62a17ae 42struct vty {
43 /* File descripter of this vty. */
44 int fd;
718e3744 45
d62a17ae 46 /* output FD, to support stdin/stdout combination */
47 int wfd;
c5e69a02 48
2cddf2ff
QY
49 /* File output, used for VTYSH only */
50 FILE *of;
51 FILE *of_saved;
52
53 /* whether we are using pager or not */
54 bool is_paged;
55
d62a17ae 56 /* Is this vty connect to file or not */
57 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
718e3744 58
d62a17ae 59 /* Node status of this vty */
60 int node;
718e3744 61
d62a17ae 62 /* Failure count */
63 int fail;
718e3744 64
fe6b47b9
QY
65 /* Output filer regex */
66 bool filter;
67 regex_t include;
68
0b42d81a
QY
69 /* Line buffer */
70 struct buffer *lbuf;
71
d62a17ae 72 /* Output buffer. */
73 struct buffer *obuf;
718e3744 74
d62a17ae 75 /* Command input buffer */
76 char *buf;
718e3744 77
d62a17ae 78 /* Command input error buffer */
7ab57d19 79 struct list *error;
5689fe5f 80
d62a17ae 81 /* Command cursor point */
82 int cp;
718e3744 83
d62a17ae 84 /* Command length */
85 int length;
718e3744 86
d62a17ae 87 /* Command max length. */
88 int max;
718e3744 89
d62a17ae 90 /* Histry of command */
91 char *hist[VTY_MAXHIST];
718e3744 92
d62a17ae 93 /* History lookup current point */
94 int hp;
718e3744 95
d62a17ae 96 /* History insert end point */
97 int hindex;
718e3744 98
d62a17ae 99 /* qobj object ID (replacement for "index") */
100 uint64_t qobj_index;
718e3744 101
d62a17ae 102 /* qobj second-level object ID (replacement for "index_sub") */
103 uint64_t qobj_index_sub;
718e3744 104
d62a17ae 105 /* For escape character. */
106 unsigned char escape;
718e3744 107
d62a17ae 108 /* Current vty status. */
109 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
718e3744 110
d62a17ae 111 /* IAC handling: was the last character received the
112 IAC (interpret-as-command) escape character (and therefore the next
113 character will be the command code)? Refer to Telnet RFC 854. */
114 unsigned char iac;
718e3744 115
d62a17ae 116 /* IAC SB (option subnegotiation) handling */
117 unsigned char iac_sb_in_progress;
118/* At the moment, we care only about the NAWS (window size) negotiation,
119 and that requires just a 5-character buffer (RFC 1073):
120 <NAWS char> <16-bit width> <16-bit height> */
9fc7ebf1 121#define TELNET_NAWS_SB_LEN 5
d62a17ae 122 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
123 /* How many subnegotiation characters have we received? We just drop
124 those that do not fit in the buffer. */
125 size_t sb_len;
718e3744 126
d62a17ae 127 /* Window width/height. */
128 int width;
129 int height;
718e3744 130
d62a17ae 131 /* Configure lines. */
132 int lines;
718e3744 133
d62a17ae 134 /* Terminal monitor. */
135 int monitor;
718e3744 136
d62a17ae 137 /* In configure mode. */
138 int config;
718e3744 139
d62a17ae 140 /* Read and write thread. */
141 struct thread *t_read;
142 struct thread *t_write;
718e3744 143
d62a17ae 144 /* Timeout seconds and thread. */
145 unsigned long v_timeout;
146 struct thread *t_timeout;
d227617a 147
d62a17ae 148 /* What address is this vty comming from. */
149 char address[SU_ADDRSTRLEN];
2071aa0e
DL
150
151 /* "frame" output. This is buffered and will be printed if some
152 * actual output follows, or will be discarded if the frame ends
153 * without any output. */
154 size_t frame_pos;
155 char frame[1024];
718e3744 156};
157
d62a17ae 158static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
0878c8d4 159{
d62a17ae 160 vty->node = node;
161 vty->qobj_index = id;
0878c8d4
DL
162}
163
3c5070be
DL
164/* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
165 * dereference "NULL->qobj_node.nid" */
d62a17ae 166#define VTY_PUSH_CONTEXT(nodeval, ptr) \
a50b7ceb 167 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
d62a17ae 168#define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
169#define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
170 do { \
171 vty->node = nodeval; \
172 /* qobj_index stays untouched */ \
173 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
6a098b3a 174 } while (0)
0878c8d4
DL
175
176/* can return NULL if context is invalid! */
d62a17ae 177#define VTY_GET_CONTEXT(structname) \
0878c8d4 178 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
d62a17ae 179#define VTY_GET_CONTEXT_SUB(structname) \
6a098b3a 180 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
0878c8d4
DL
181
182/* will return if ptr is NULL. */
d62a17ae 183#define VTY_CHECK_CONTEXT(ptr) \
184 if (!ptr) { \
185 vty_out(vty, \
186 "Current configuration object was deleted " \
187 "by another process.\n"); \
188 return CMD_WARNING; \
0878c8d4
DL
189 }
190
191/* struct structname *ptr = <context>; ptr will never be NULL. */
d62a17ae 192#define VTY_DECLVAR_CONTEXT(structname, ptr) \
193 struct structname *ptr = VTY_GET_CONTEXT(structname); \
0878c8d4 194 VTY_CHECK_CONTEXT(ptr);
d62a17ae 195#define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
196 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
6a098b3a 197 VTY_CHECK_CONTEXT(ptr);
a3d826f0 198#define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
996c9314
LB
199 if (vty->qobj_index == 0) \
200 return CMD_NOT_MY_INSTANCE; \
a3d826f0
CS
201 struct structname *ptr = VTY_GET_CONTEXT(structname); \
202 VTY_CHECK_CONTEXT(ptr);
0878c8d4 203
d62a17ae 204struct vty_arg {
205 const char *name;
206 const char *value;
207 const char **argv;
208 int argc;
eac6e3f0
RW
209};
210
718e3744 211/* Integrated configuration file. */
e20dc2ba 212#define INTEGRATE_DEFAULT_CONFIG "frr.conf"
718e3744 213
718e3744 214/* Default time out value */
215#define VTY_TIMEOUT_DEFAULT 600
216
217/* Vty read buffer size. */
218#define VTY_READ_BUFSIZ 512
219
220/* Directory separator. */
221#ifndef DIRECTORY_SEP
222#define DIRECTORY_SEP '/'
223#endif /* DIRECTORY_SEP */
224
225#ifndef IS_DIRECTORY_SEP
226#define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
227#endif
228
718e3744 229/* Exported variables */
230extern char integrate_default[];
231
232/* Prototypes. */
d62a17ae 233extern void vty_init(struct thread_master *);
234extern void vty_init_vtysh(void);
235extern void vty_terminate(void);
236extern void vty_reset(void);
237extern struct vty *vty_new(void);
154b9e8f 238extern struct vty *vty_stdio(void (*atclose)(int isexit));
2071aa0e
DL
239
240/* - vty_frame() output goes to a buffer (for context-begin markers)
241 * - vty_out() will first print this buffer, and clear it
242 * - vty_endframe() clears the buffer without printing it, and prints an
243 * extra string if the buffer was empty before (for context-end markers)
244 */
d62a17ae 245extern int vty_out(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
2071aa0e
DL
246extern void vty_frame(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
247extern void vty_endframe(struct vty *, const char *);
fe6b47b9 248bool vty_set_include(struct vty *vty, const char *regexp);
2071aa0e 249
5ede5e4e 250extern bool vty_read_config(const char *, char *);
d62a17ae 251extern void vty_time_print(struct vty *, int);
252extern void vty_serv_sock(const char *, unsigned short, const char *);
253extern void vty_close(struct vty *);
254extern char *vty_get_cwd(void);
255extern void vty_log(const char *level, const char *proto, const char *fmt,
256 struct timestamp_control *, va_list);
257extern int vty_config_lock(struct vty *);
258extern int vty_config_unlock(struct vty *);
259extern void vty_config_lockless(void);
260extern int vty_shell(struct vty *);
261extern int vty_shell_serv(struct vty *);
262extern void vty_hello(struct vty *);
718e3744 263
154b9e8f
DL
264/* ^Z / SIGTSTP handling */
265extern void vty_stdio_suspend(void);
266extern void vty_stdio_resume(void);
267extern void vty_stdio_close(void);
268
274a4a44 269/* Send a fixed-size message to all vty terminal monitors; this should be
270 an async-signal-safe function. */
d62a17ae 271extern void vty_log_fixed(char *buf, size_t len);
274a4a44 272
718e3744 273#endif /* _ZEBRA_VTY_H */