]>
git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
1 /* Virtual terminal [aka TeletYpe] interface routine
2 * Copyright (C) 1997 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
26 #include "sockunion.h"
30 #define VTY_BUFSIZ 4096
31 #define VTY_MAXHIST 20
35 /* File descripter of this vty. */
38 /* output FD, to support stdin/stdout combination */
41 /* Is this vty connect to file or not */
42 enum { VTY_TERM
, VTY_FILE
, VTY_SHELL
, VTY_SHELL_SERV
} type
;
44 /* Node status of this vty */
53 /* Command input buffer */
56 /* Command input error buffer */
59 /* Command cursor point */
65 /* Command max length. */
68 /* Histry of command */
69 char *hist
[VTY_MAXHIST
];
71 /* History lookup current point */
74 /* History insert end point */
77 /* qobj object ID (replacement for "index") */
80 /* qobj second-level object ID (replacement for "index_sub") */
81 uint64_t qobj_index_sub
;
83 /* For escape character. */
86 /* Current vty status. */
87 enum { VTY_NORMAL
, VTY_CLOSE
, VTY_MORE
, VTY_MORELINE
} status
;
89 /* IAC handling: was the last character received the
90 IAC (interpret-as-command) escape character (and therefore the next
91 character will be the command code)? Refer to Telnet RFC 854. */
94 /* IAC SB (option subnegotiation) handling */
95 unsigned char iac_sb_in_progress
;
96 /* At the moment, we care only about the NAWS (window size) negotiation,
97 and that requires just a 5-character buffer (RFC 1073):
98 <NAWS char> <16-bit width> <16-bit height> */
99 #define TELNET_NAWS_SB_LEN 5
100 unsigned char sb_buf
[TELNET_NAWS_SB_LEN
];
101 /* How many subnegotiation characters have we received? We just drop
102 those that do not fit in the buffer. */
105 /* Window width/height. */
109 /* Configure lines. */
112 /* Terminal monitor. */
115 /* In configure mode. */
118 /* Read and write thread. */
119 struct thread
*t_read
;
120 struct thread
*t_write
;
122 /* Timeout seconds and thread. */
123 unsigned long v_timeout
;
124 struct thread
*t_timeout
;
126 /* What address is this vty comming from. */
127 char address
[SU_ADDRSTRLEN
];
129 /* "frame" output. This is buffered and will be printed if some
130 * actual output follows, or will be discarded if the frame ends
131 * without any output. */
136 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
139 vty
->qobj_index
= id
;
142 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
143 * dereference "NULL->qobj_node.nid" */
144 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
145 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
146 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
147 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
149 vty->node = nodeval; \
150 /* qobj_index stays untouched */ \
151 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
154 /* can return NULL if context is invalid! */
155 #define VTY_GET_CONTEXT(structname) \
156 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
157 #define VTY_GET_CONTEXT_SUB(structname) \
158 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
160 /* will return if ptr is NULL. */
161 #define VTY_CHECK_CONTEXT(ptr) \
164 "Current configuration object was deleted " \
165 "by another process.\n"); \
166 return CMD_WARNING; \
169 /* struct structname *ptr = <context>; ptr will never be NULL. */
170 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
171 struct structname *ptr = VTY_GET_CONTEXT(structname); \
172 VTY_CHECK_CONTEXT(ptr);
173 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
174 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
175 VTY_CHECK_CONTEXT(ptr);
176 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
177 if (vty->qobj_index == 0) \
178 return CMD_NOT_MY_INSTANCE; \
179 struct structname *ptr = VTY_GET_CONTEXT(structname); \
180 VTY_CHECK_CONTEXT(ptr);
189 /* Integrated configuration file. */
190 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
192 #if CONFDATE > 20180401
193 CPP_NOTICE("It's probably time to remove VTY_NEWLINE compatibility foo.")
196 /* for compatibility */
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) \
202 (v) = strtoul((str), NULL, 10); \
204 CPP_WARN("VTY_GET_INTEGER is no longer useful, use strtoul() or DEFPY.")
205 #define VTY_GET_INTEGER_RANGE(desc, v, str, min, max) \
207 (v) = strtoul((str), NULL, 10); \
210 "VTY_GET_INTEGER_RANGE is no longer useful, use strtoul() or DEFPY.")
211 #define VTY_GET_ULONG(desc, v, str) \
213 (v) = strtoul((str), NULL, 10); \
215 CPP_WARN("VTY_GET_ULONG is no longer useful, use strtoul() or DEFPY.")
216 #define VTY_GET_ULL(desc, v, str) \
218 (v) = strtoull((str), NULL, 10); \
220 CPP_WARN("VTY_GET_ULL is no longer useful, use strtoull() or DEFPY.")
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...)")
231 /* Default time out value */
232 #define VTY_TIMEOUT_DEFAULT 600
234 /* Vty read buffer size. */
235 #define VTY_READ_BUFSIZ 512
237 /* Directory separator. */
238 #ifndef DIRECTORY_SEP
239 #define DIRECTORY_SEP '/'
240 #endif /* DIRECTORY_SEP */
242 #ifndef IS_DIRECTORY_SEP
243 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
246 /* Exported variables */
247 extern char integrate_default
[];
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
)(int isexit
));
257 /* - vty_frame() output goes to a buffer (for context-begin markers)
258 * - vty_out() will first print this buffer, and clear it
259 * - vty_endframe() clears the buffer without printing it, and prints an
260 * extra string if the buffer was empty before (for context-end markers)
262 extern int vty_out(struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
263 extern void vty_frame(struct vty
*, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
264 extern void vty_endframe(struct vty
*, const char *);
266 extern void vty_read_config(const char *, char *);
267 extern void vty_time_print(struct vty
*, int);
268 extern void vty_serv_sock(const char *, unsigned short, const char *);
269 extern void vty_close(struct vty
*);
270 extern char *vty_get_cwd(void);
271 extern void vty_log(const char *level
, const char *proto
, const char *fmt
,
272 struct timestamp_control
*, va_list);
273 extern int vty_config_lock(struct vty
*);
274 extern int vty_config_unlock(struct vty
*);
275 extern void vty_config_lockless(void);
276 extern int vty_shell(struct vty
*);
277 extern int vty_shell_serv(struct vty
*);
278 extern void vty_hello(struct vty
*);
280 /* ^Z / SIGTSTP handling */
281 extern void vty_stdio_suspend(void);
282 extern void vty_stdio_resume(void);
283 extern void vty_stdio_close(void);
285 /* Send a fixed-size message to all vty terminal monitors; this should be
286 an async-signal-safe function. */
287 extern void vty_log_fixed(char *buf
, size_t len
);
289 #endif /* _ZEBRA_VTY_H */