blob: ea987bf3c151f1e36733bc888226b537796f6df0 [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001#ifndef _VTY_H
2#define _VTY_H
3
4#include <stdio.h>
5#include <stdarg.h>
6
Harald Welte7acb30c2011-08-17 17:13:48 +02007/*! \defgroup vty VTY (Virtual TTY) interface
8 * @{
9 */
10/*! \file vty.h */
11
Harald Welte3fb0b6f2010-05-19 19:02:52 +020012/* GCC have printf type attribute check. */
13#ifdef __GNUC__
14#define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
15#else
16#define VTY_PRINTF_ATTRIBUTE(a,b)
17#endif /* __GNUC__ */
18
19/* Does the I/O error indicate that the operation should be retried later? */
20#define ERRNO_IO_RETRY(EN) \
21 (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
22
23/* Vty read buffer size. */
24#define VTY_READ_BUFSIZ 512
25
26#define VTY_BUFSIZ 512
27#define VTY_MAXHIST 20
28
Harald Welte7acb30c2011-08-17 17:13:48 +020029/*! \brief VTY events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020030enum event {
31 VTY_SERV,
32 VTY_READ,
33 VTY_WRITE,
34 VTY_CLOSED,
35 VTY_TIMEOUT_RESET,
36#ifdef VTYSH
37 VTYSH_SERV,
38 VTYSH_READ,
39 VTYSH_WRITE
40#endif /* VTYSH */
41};
42
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020043enum vty_type {
44 VTY_TERM,
45 VTY_FILE,
46 VTY_SHELL,
47 VTY_SHELL_SERV
48};
49
Harald Welte7acb30c2011-08-17 17:13:48 +020050/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020051struct vty {
Harald Welte7acb30c2011-08-17 17:13:48 +020052 /*! \brief underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020053 FILE *file;
54
Harald Welte7acb30c2011-08-17 17:13:48 +020055 /*! \brief private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020056 void *priv;
57
Harald Welte7acb30c2011-08-17 17:13:48 +020058 /*! \brief File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020059 int fd;
60
Harald Welte7acb30c2011-08-17 17:13:48 +020061 /*! \brief Is this vty connect to file or not */
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020062 enum vty_type type;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020063
Harald Welte7acb30c2011-08-17 17:13:48 +020064 /*! \brief Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020065 int node;
66
Harald Welte7acb30c2011-08-17 17:13:48 +020067 /*! \brief Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020068 int fail;
69
Harald Welte7acb30c2011-08-17 17:13:48 +020070 /*! \brief Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020071 struct buffer *obuf;
72
Harald Welte7acb30c2011-08-17 17:13:48 +020073 /*! \brief Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020074 char *buf;
75
Harald Welte7acb30c2011-08-17 17:13:48 +020076 /*! \brief Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020077 int cp;
78
Harald Welte7acb30c2011-08-17 17:13:48 +020079 /*! \brief Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020080 int length;
81
Harald Welte7acb30c2011-08-17 17:13:48 +020082 /*! \brief Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020083 int max;
84
Harald Welte7acb30c2011-08-17 17:13:48 +020085 /*! \brief Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020086 char *hist[VTY_MAXHIST];
87
Harald Welte7acb30c2011-08-17 17:13:48 +020088 /*! \brief History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020089 int hp;
90
Harald Welte7acb30c2011-08-17 17:13:48 +020091 /*! \brief History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020092 int hindex;
93
Harald Welte7acb30c2011-08-17 17:13:48 +020094 /*! \brief For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095 access-list etc... */
96 void *index;
97
Harald Welte7acb30c2011-08-17 17:13:48 +020098 /*! \brief For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020099 void *index_sub;
100
Harald Welte7acb30c2011-08-17 17:13:48 +0200101 /*! \brief For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200102 unsigned char escape;
103
Harald Welte7acb30c2011-08-17 17:13:48 +0200104 /*! \brief Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200105 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
106
Harald Welte7acb30c2011-08-17 17:13:48 +0200107 /*! \brief IAC handling
108 *
109 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200110 * (interpret-as-command) escape character (and therefore the next
111 * character will be the command code)? Refer to Telnet RFC 854. */
112 unsigned char iac;
113
Harald Welte7acb30c2011-08-17 17:13:48 +0200114 /*! \brief IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200115 unsigned char iac_sb_in_progress;
116 /* At the moment, we care only about the NAWS (window size) negotiation,
117 * and that requires just a 5-character buffer (RFC 1073):
118 * <NAWS char> <16-bit width> <16-bit height> */
119#define TELNET_NAWS_SB_LEN 5
Harald Welte7acb30c2011-08-17 17:13:48 +0200120 /*! \brief sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200121 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Harald Welte7acb30c2011-08-17 17:13:48 +0200122 /*! \brief How many subnegotiation characters have we received?
123 *
124 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200125 size_t sb_len;
126
Harald Welte7acb30c2011-08-17 17:13:48 +0200127 /*! \brief Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200128 int width;
Harald Welte7acb30c2011-08-17 17:13:48 +0200129 /*! \brief Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200130 int height;
131
Harald Welte7acb30c2011-08-17 17:13:48 +0200132 /*! \brief Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200133 int lines;
134
135 int monitor;
136
Harald Welte7acb30c2011-08-17 17:13:48 +0200137 /*! \brief In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200138 int config;
139};
140
141/* Small macro to determine newline is newline only or linefeed needed. */
142#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
143
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +0200144static inline const char *vty_newline(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200145{
146 return VTY_NEWLINE;
147}
148
Harald Welte7acb30c2011-08-17 17:13:48 +0200149/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200150struct vty_app_info {
Harald Welte7acb30c2011-08-17 17:13:48 +0200151 /*! \brief name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800152 const char *name;
Harald Welte7acb30c2011-08-17 17:13:48 +0200153 /*! \brief version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800154 const char *version;
Harald Welte7acb30c2011-08-17 17:13:48 +0200155 /*! \brief copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800156 const char *copyright;
Harald Welte7acb30c2011-08-17 17:13:48 +0200157 /*! \brief \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200158 void *tall_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200159 /*! \brief call-back for returning to parent n ode */
Harald Welte237f6242010-05-25 23:00:45 +0200160 enum node_type (*go_parent_cb)(struct vty *vty);
Harald Welte7acb30c2011-08-17 17:13:48 +0200161 /*! \brief call-back to determine if node is config node */
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800162 int (*is_config_node)(struct vty *vty, int node);
Harald Welte237f6242010-05-25 23:00:45 +0200163};
164
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200165/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200166void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200167int vty_read_config_file(const char *file_name, void *priv);
168void vty_init_vtysh (void);
169void vty_reset (void);
170struct vty *vty_new (void);
171struct vty *vty_create (int vty_sock, void *priv);
172int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
173int vty_out_newline(struct vty *);
174int vty_read(struct vty *vty);
175//void vty_time_print (struct vty *, int);
176void vty_close (struct vty *);
177char *vty_get_cwd (void);
178void vty_log (const char *level, const char *proto, const char *fmt, va_list);
179int vty_config_lock (struct vty *);
180int vty_config_unlock (struct vty *);
181int vty_shell (struct vty *);
182int vty_shell_serv (struct vty *);
183void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800184void *vty_current_index(struct vty *);
185int vty_current_node(struct vty *vty);
Andreas.Eversbergf948dbc2011-11-10 23:09:35 +0100186enum node_type vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200187
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800188extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200189
Harald Welted61d5172011-09-04 22:56:10 +0200190extern struct cmd_element cfg_description_cmd;
191extern struct cmd_element cfg_no_description_cmd;
192
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200193
194/**
195 * signal handling
196 */
197enum signal_vty {
198 S_VTY_EVENT,
199};
200
201struct vty_signal_data {
202 enum event event;
203 int sock;
204 struct vty *vty;
205};
206
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200207/*! @} */
Harald Welte7acb30c2011-08-17 17:13:48 +0200208
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200209#endif