]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
zebra: add 3 fields to ipset_entry : src,dst port, and proto
[mirror_frr.git] / lib / vty.h
1 /* Virtual terminal [aka TeletYpe] interface routine
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 */
20
21 #ifndef _ZEBRA_VTY_H
22 #define _ZEBRA_VTY_H
23
24 #include "thread.h"
25 #include "log.h"
26 #include "sockunion.h"
27 #include "qobj.h"
28 #include "compiler.h"
29
30 #define VTY_BUFSIZ 4096
31 #define VTY_MAXHIST 20
32
33 /* VTY struct. */
34 struct vty {
35 /* File descripter of this vty. */
36 int fd;
37
38 /* output FD, to support stdin/stdout combination */
39 int wfd;
40
41 /* Is this vty connect to file or not */
42 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
43
44 /* Node status of this vty */
45 int node;
46
47 /* Failure count */
48 int fail;
49
50 /* Output buffer. */
51 struct buffer *obuf;
52
53 /* Command input buffer */
54 char *buf;
55
56 /* Command input error buffer */
57 char *error_buf;
58
59 /* Command cursor point */
60 int cp;
61
62 /* Command length */
63 int length;
64
65 /* Command max length. */
66 int max;
67
68 /* Histry of command */
69 char *hist[VTY_MAXHIST];
70
71 /* History lookup current point */
72 int hp;
73
74 /* History insert end point */
75 int hindex;
76
77 /* qobj object ID (replacement for "index") */
78 uint64_t qobj_index;
79
80 /* qobj second-level object ID (replacement for "index_sub") */
81 uint64_t qobj_index_sub;
82
83 /* For escape character. */
84 unsigned char escape;
85
86 /* Current vty status. */
87 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
88
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;
93
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. */
103 size_t sb_len;
104
105 /* Window width/height. */
106 int width;
107 int height;
108
109 /* Configure lines. */
110 int lines;
111
112 /* Terminal monitor. */
113 int monitor;
114
115 /* In configure mode. */
116 int config;
117
118 /* Read and write thread. */
119 struct thread *t_read;
120 struct thread *t_write;
121
122 /* Timeout seconds and thread. */
123 unsigned long v_timeout;
124 struct thread *t_timeout;
125
126 /* What address is this vty comming from. */
127 char address[SU_ADDRSTRLEN];
128
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. */
132 size_t frame_pos;
133 char frame[1024];
134 };
135
136 static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
137 {
138 vty->node = node;
139 vty->qobj_index = id;
140 }
141
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) \
148 do { \
149 vty->node = nodeval; \
150 /* qobj_index stays untouched */ \
151 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
152 } while (0)
153
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)
159
160 /* will return if ptr is NULL. */
161 #define VTY_CHECK_CONTEXT(ptr) \
162 if (!ptr) { \
163 vty_out(vty, \
164 "Current configuration object was deleted " \
165 "by another process.\n"); \
166 return CMD_WARNING; \
167 }
168
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);
181
182 struct vty_arg {
183 const char *name;
184 const char *value;
185 const char **argv;
186 int argc;
187 };
188
189 /* Integrated configuration file. */
190 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
191
192 /* Default time out value */
193 #define VTY_TIMEOUT_DEFAULT 600
194
195 /* Vty read buffer size. */
196 #define VTY_READ_BUFSIZ 512
197
198 /* Directory separator. */
199 #ifndef DIRECTORY_SEP
200 #define DIRECTORY_SEP '/'
201 #endif /* DIRECTORY_SEP */
202
203 #ifndef IS_DIRECTORY_SEP
204 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
205 #endif
206
207 /* Exported variables */
208 extern char integrate_default[];
209
210 /* Prototypes. */
211 extern void vty_init(struct thread_master *);
212 extern void vty_init_vtysh(void);
213 extern void vty_terminate(void);
214 extern void vty_reset(void);
215 extern struct vty *vty_new(void);
216 extern struct vty *vty_stdio(void (*atclose)(int isexit));
217
218 /* - vty_frame() output goes to a buffer (for context-begin markers)
219 * - vty_out() will first print this buffer, and clear it
220 * - vty_endframe() clears the buffer without printing it, and prints an
221 * extra string if the buffer was empty before (for context-end markers)
222 */
223 extern int vty_out(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
224 extern void vty_frame(struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
225 extern void vty_endframe(struct vty *, const char *);
226
227 extern void vty_read_config(const char *, char *);
228 extern void vty_time_print(struct vty *, int);
229 extern void vty_serv_sock(const char *, unsigned short, const char *);
230 extern void vty_close(struct vty *);
231 extern char *vty_get_cwd(void);
232 extern void vty_log(const char *level, const char *proto, const char *fmt,
233 struct timestamp_control *, va_list);
234 extern int vty_config_lock(struct vty *);
235 extern int vty_config_unlock(struct vty *);
236 extern void vty_config_lockless(void);
237 extern int vty_shell(struct vty *);
238 extern int vty_shell_serv(struct vty *);
239 extern void vty_hello(struct vty *);
240
241 /* ^Z / SIGTSTP handling */
242 extern void vty_stdio_suspend(void);
243 extern void vty_stdio_resume(void);
244 extern void vty_stdio_close(void);
245
246 /* Send a fixed-size message to all vty terminal monitors; this should be
247 an async-signal-safe function. */
248 extern void vty_log_fixed(char *buf, size_t len);
249
250 #endif /* _ZEBRA_VTY_H */