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