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