]>
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 | ||
b21b19c5 | 24 | #include "thread.h" |
1ed72e0b | 25 | #include "log.h" |
d227617a | 26 | #include "sockunion.h" |
0878c8d4 | 27 | #include "qobj.h" |
b21b19c5 | 28 | |
2af38873 | 29 | #define VTY_BUFSIZ 4096 |
718e3744 | 30 | #define VTY_MAXHIST 20 |
31 | ||
32 | /* VTY struct. */ | |
d62a17ae | 33 | struct vty { |
34 | /* File descripter of this vty. */ | |
35 | int fd; | |
718e3744 | 36 | |
d62a17ae | 37 | /* output FD, to support stdin/stdout combination */ |
38 | int wfd; | |
c5e69a02 | 39 | |
d62a17ae | 40 | /* Is this vty connect to file or not */ |
41 | enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type; | |
718e3744 | 42 | |
d62a17ae | 43 | /* Node status of this vty */ |
44 | int node; | |
718e3744 | 45 | |
d62a17ae | 46 | /* Failure count */ |
47 | int fail; | |
718e3744 | 48 | |
d62a17ae | 49 | /* Output buffer. */ |
50 | struct buffer *obuf; | |
718e3744 | 51 | |
d62a17ae | 52 | /* Command input buffer */ |
53 | char *buf; | |
718e3744 | 54 | |
d62a17ae | 55 | /* Command input error buffer */ |
56 | char *error_buf; | |
5689fe5f | 57 | |
d62a17ae | 58 | /* Command cursor point */ |
59 | int cp; | |
718e3744 | 60 | |
d62a17ae | 61 | /* Command length */ |
62 | int length; | |
718e3744 | 63 | |
d62a17ae | 64 | /* Command max length. */ |
65 | int max; | |
718e3744 | 66 | |
d62a17ae | 67 | /* Histry of command */ |
68 | char *hist[VTY_MAXHIST]; | |
718e3744 | 69 | |
d62a17ae | 70 | /* History lookup current point */ |
71 | int hp; | |
718e3744 | 72 | |
d62a17ae | 73 | /* History insert end point */ |
74 | int hindex; | |
718e3744 | 75 | |
d62a17ae | 76 | /* qobj object ID (replacement for "index") */ |
77 | uint64_t qobj_index; | |
718e3744 | 78 | |
d62a17ae | 79 | /* qobj second-level object ID (replacement for "index_sub") */ |
80 | uint64_t qobj_index_sub; | |
718e3744 | 81 | |
d62a17ae | 82 | /* For escape character. */ |
83 | unsigned char escape; | |
718e3744 | 84 | |
d62a17ae | 85 | /* Current vty status. */ |
86 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; | |
718e3744 | 87 | |
d62a17ae | 88 | /* IAC handling: was the last character received the |
89 | IAC (interpret-as-command) escape character (and therefore the next | |
90 | character will be the command code)? Refer to Telnet RFC 854. */ | |
91 | unsigned char iac; | |
718e3744 | 92 | |
d62a17ae | 93 | /* IAC SB (option subnegotiation) handling */ |
94 | unsigned char iac_sb_in_progress; | |
95 | /* At the moment, we care only about the NAWS (window size) negotiation, | |
96 | and that requires just a 5-character buffer (RFC 1073): | |
97 | <NAWS char> <16-bit width> <16-bit height> */ | |
9fc7ebf1 | 98 | #define TELNET_NAWS_SB_LEN 5 |
d62a17ae | 99 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
100 | /* How many subnegotiation characters have we received? We just drop | |
101 | those that do not fit in the buffer. */ | |
102 | size_t sb_len; | |
718e3744 | 103 | |
d62a17ae | 104 | /* Window width/height. */ |
105 | int width; | |
106 | int height; | |
718e3744 | 107 | |
d62a17ae | 108 | /* Configure lines. */ |
109 | int lines; | |
718e3744 | 110 | |
d62a17ae | 111 | /* Terminal monitor. */ |
112 | int monitor; | |
718e3744 | 113 | |
d62a17ae | 114 | /* In configure mode. */ |
115 | int config; | |
718e3744 | 116 | |
d62a17ae | 117 | /* Read and write thread. */ |
118 | struct thread *t_read; | |
119 | struct thread *t_write; | |
718e3744 | 120 | |
d62a17ae | 121 | /* Timeout seconds and thread. */ |
122 | unsigned long v_timeout; | |
123 | struct thread *t_timeout; | |
d227617a | 124 | |
d62a17ae | 125 | /* What address is this vty comming from. */ |
126 | char address[SU_ADDRSTRLEN]; | |
718e3744 | 127 | }; |
128 | ||
d62a17ae | 129 | static inline void vty_push_context(struct vty *vty, int node, uint64_t id) |
0878c8d4 | 130 | { |
d62a17ae | 131 | vty->node = node; |
132 | vty->qobj_index = id; | |
0878c8d4 DL |
133 | } |
134 | ||
3c5070be DL |
135 | /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to |
136 | * dereference "NULL->qobj_node.nid" */ | |
d62a17ae | 137 | #define VTY_PUSH_CONTEXT(nodeval, ptr) \ |
a50b7ceb | 138 | vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr)) |
d62a17ae | 139 | #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL) |
140 | #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \ | |
141 | do { \ | |
142 | vty->node = nodeval; \ | |
143 | /* qobj_index stays untouched */ \ | |
144 | vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \ | |
6a098b3a | 145 | } while (0) |
0878c8d4 DL |
146 | |
147 | /* can return NULL if context is invalid! */ | |
d62a17ae | 148 | #define VTY_GET_CONTEXT(structname) \ |
0878c8d4 | 149 | QOBJ_GET_TYPESAFE(vty->qobj_index, structname) |
d62a17ae | 150 | #define VTY_GET_CONTEXT_SUB(structname) \ |
6a098b3a | 151 | QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname) |
0878c8d4 DL |
152 | |
153 | /* will return if ptr is NULL. */ | |
d62a17ae | 154 | #define VTY_CHECK_CONTEXT(ptr) \ |
155 | if (!ptr) { \ | |
156 | vty_out(vty, \ | |
157 | "Current configuration object was deleted " \ | |
158 | "by another process.\n"); \ | |
159 | return CMD_WARNING; \ | |
0878c8d4 DL |
160 | } |
161 | ||
162 | /* struct structname *ptr = <context>; ptr will never be NULL. */ | |
d62a17ae | 163 | #define VTY_DECLVAR_CONTEXT(structname, ptr) \ |
164 | struct structname *ptr = VTY_GET_CONTEXT(structname); \ | |
0878c8d4 | 165 | VTY_CHECK_CONTEXT(ptr); |
d62a17ae | 166 | #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \ |
167 | struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \ | |
6a098b3a | 168 | VTY_CHECK_CONTEXT(ptr); |
0878c8d4 | 169 | |
d62a17ae | 170 | struct vty_arg { |
171 | const char *name; | |
172 | const char *value; | |
173 | const char **argv; | |
174 | int argc; | |
eac6e3f0 RW |
175 | }; |
176 | ||
718e3744 | 177 | /* Integrated configuration file. */ |
e20dc2ba | 178 | #define INTEGRATE_DEFAULT_CONFIG "frr.conf" |
718e3744 | 179 | |
dcc0a0d1 | 180 | /* for compatibility */ |
8867927f DL |
181 | #if defined(__ICC) |
182 | #define CPP_WARN_STR(X) #X | |
183 | #define CPP_WARN(text) _Pragma(CPP_WARN_STR(message __FILE__ ": " text)) | |
184 | ||
d62a17ae | 185 | #elif (defined(__GNUC__) \ |
186 | && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))) \ | |
187 | || (defined(__clang__) \ | |
188 | && (__clang_major__ >= 4 \ | |
189 | || (__clang_major__ == 3 && __clang_minor__ >= 5))) | |
8867927f DL |
190 | #define CPP_WARN_STR(X) #X |
191 | #define CPP_WARN(text) _Pragma(CPP_WARN_STR(GCC warning text)) | |
192 | ||
193 | #else | |
194 | #define CPP_WARN(text) | |
195 | #endif | |
196 | ||
d62a17ae | 197 | #define VNL "\n" CPP_WARN("VNL has been replaced with \\n.") |
198 | #define VTYNL "\n" CPP_WARN("VTYNL has been replaced with \\n.") | |
199 | #define VTY_NEWLINE "\n" CPP_WARN("VTY_NEWLINE has been replaced with \\n.") | |
200 | #define VTY_GET_INTEGER(desc, v, str) \ | |
201 | { \ | |
202 | (v) = strtoul((str), NULL, 10); \ | |
203 | } \ | |
8867927f | 204 | CPP_WARN("VTY_GET_INTEGER is no longer useful, use strtoul() or DEFPY.") |
d62a17ae | 205 | #define VTY_GET_INTEGER_RANGE(desc, v, str, min, max) \ |
206 | { \ | |
207 | (v) = strtoul((str), NULL, 10); \ | |
208 | } \ | |
209 | CPP_WARN( \ | |
210 | "VTY_GET_INTEGER_RANGE is no longer useful, use strtoul() or DEFPY.") | |
211 | #define VTY_GET_ULONG(desc, v, str) \ | |
212 | { \ | |
213 | (v) = strtoul((str), NULL, 10); \ | |
214 | } \ | |
8867927f | 215 | CPP_WARN("VTY_GET_ULONG is no longer useful, use strtoul() or DEFPY.") |
d62a17ae | 216 | #define VTY_GET_ULL(desc, v, str) \ |
217 | { \ | |
218 | (v) = strtoull((str), NULL, 10); \ | |
219 | } \ | |
8867927f | 220 | CPP_WARN("VTY_GET_ULL is no longer useful, use strtoull() or DEFPY.") |
d62a17ae | 221 | #define VTY_GET_IPV4_ADDRESS(desc, v, str) \ |
222 | inet_aton((str), &(v)) CPP_WARN( \ | |
223 | "VTY_GET_IPV4_ADDRESS is no longer useful, use inet_aton() or DEFPY.") | |
224 | #define VTY_GET_IPV4_PREFIX(desc, v, str) \ | |
225 | str2prefix_ipv4((str), &(v)) CPP_WARN( \ | |
226 | "VTY_GET_IPV4_PREFIX is no longer useful, use str2prefix_ipv4() or DEFPY.") | |
227 | #define vty_outln(vty, str, ...) \ | |
228 | vty_out(vty, str "\n", ##__VA_ARGS__) CPP_WARN( \ | |
229 | "vty_outln is no longer useful, use vty_out(...\\n...)") | |
dcc0a0d1 | 230 | |
718e3744 | 231 | /* Default time out value */ |
232 | #define VTY_TIMEOUT_DEFAULT 600 | |
233 | ||
234 | /* Vty read buffer size. */ | |
235 | #define VTY_READ_BUFSIZ 512 | |
236 | ||
237 | /* Directory separator. */ | |
238 | #ifndef DIRECTORY_SEP | |
239 | #define DIRECTORY_SEP '/' | |
240 | #endif /* DIRECTORY_SEP */ | |
241 | ||
242 | #ifndef IS_DIRECTORY_SEP | |
243 | #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP) | |
244 | #endif | |
245 | ||
718e3744 | 246 | /* Exported variables */ |
247 | extern char integrate_default[]; | |
248 | ||
249 | /* Prototypes. */ | |
d62a17ae | 250 | extern void vty_init(struct thread_master *); |
251 | extern void vty_init_vtysh(void); | |
252 | extern void vty_terminate(void); | |
253 | extern void vty_reset(void); | |
254 | extern struct vty *vty_new(void); | |
255 | extern struct vty *vty_stdio(void (*atclose)(void)); | |
256 | extern int vty_out(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); | |
257 | extern void vty_read_config(const char *, char *); | |
258 | extern void vty_time_print(struct vty *, int); | |
259 | extern void vty_serv_sock(const char *, unsigned short, const char *); | |
260 | extern void vty_close(struct vty *); | |
261 | extern char *vty_get_cwd(void); | |
262 | extern void vty_log(const char *level, const char *proto, const char *fmt, | |
263 | struct timestamp_control *, va_list); | |
264 | extern int vty_config_lock(struct vty *); | |
265 | extern int vty_config_unlock(struct vty *); | |
266 | extern void vty_config_lockless(void); | |
267 | extern int vty_shell(struct vty *); | |
268 | extern int vty_shell_serv(struct vty *); | |
269 | extern void vty_hello(struct vty *); | |
718e3744 | 270 | |
274a4a44 | 271 | /* Send a fixed-size message to all vty terminal monitors; this should be |
272 | an async-signal-safe function. */ | |
d62a17ae | 273 | extern void vty_log_fixed(char *buf, size_t len); |
274a4a44 | 274 | |
718e3744 | 275 | #endif /* _ZEBRA_VTY_H */ |