blob: 1518894b64775f0962ef9d44d459c668f6e189c1 [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
Harald Welte7acb30c2011-08-17 17:13:48 +020043/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020044struct vty {
Harald Welte7acb30c2011-08-17 17:13:48 +020045 /*! \brief underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020046 FILE *file;
47
Harald Welte7acb30c2011-08-17 17:13:48 +020048 /*! \brief private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020049 void *priv;
50
Harald Welte7acb30c2011-08-17 17:13:48 +020051 /*! \brief File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020052 int fd;
53
Harald Welte7acb30c2011-08-17 17:13:48 +020054 /*! \brief Is this vty connect to file or not */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020055 enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
56
Harald Welte7acb30c2011-08-17 17:13:48 +020057 /*! \brief Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020058 int node;
59
Harald Welte7acb30c2011-08-17 17:13:48 +020060 /*! \brief Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020061 int fail;
62
Harald Welte7acb30c2011-08-17 17:13:48 +020063 /*! \brief Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020064 struct buffer *obuf;
65
Harald Welte7acb30c2011-08-17 17:13:48 +020066 /*! \brief Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020067 char *buf;
68
Harald Welte7acb30c2011-08-17 17:13:48 +020069 /*! \brief Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020070 int cp;
71
Harald Welte7acb30c2011-08-17 17:13:48 +020072 /*! \brief Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020073 int length;
74
Harald Welte7acb30c2011-08-17 17:13:48 +020075 /*! \brief Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020076 int max;
77
Harald Welte7acb30c2011-08-17 17:13:48 +020078 /*! \brief Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020079 char *hist[VTY_MAXHIST];
80
Harald Welte7acb30c2011-08-17 17:13:48 +020081 /*! \brief History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020082 int hp;
83
Harald Welte7acb30c2011-08-17 17:13:48 +020084 /*! \brief History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020085 int hindex;
86
Harald Welte7acb30c2011-08-17 17:13:48 +020087 /*! \brief For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +020088 access-list etc... */
89 void *index;
90
Harald Welte7acb30c2011-08-17 17:13:48 +020091 /*! \brief For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020092 void *index_sub;
93
Harald Welte7acb30c2011-08-17 17:13:48 +020094 /*! \brief For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095 unsigned char escape;
96
Harald Welte7acb30c2011-08-17 17:13:48 +020097 /*! \brief Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020098 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
99
Harald Welte7acb30c2011-08-17 17:13:48 +0200100 /*! \brief IAC handling
101 *
102 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200103 * (interpret-as-command) escape character (and therefore the next
104 * character will be the command code)? Refer to Telnet RFC 854. */
105 unsigned char iac;
106
Harald Welte7acb30c2011-08-17 17:13:48 +0200107 /*! \brief IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200108 unsigned char iac_sb_in_progress;
109 /* At the moment, we care only about the NAWS (window size) negotiation,
110 * and that requires just a 5-character buffer (RFC 1073):
111 * <NAWS char> <16-bit width> <16-bit height> */
112#define TELNET_NAWS_SB_LEN 5
Harald Welte7acb30c2011-08-17 17:13:48 +0200113 /*! \brief sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200114 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Harald Welte7acb30c2011-08-17 17:13:48 +0200115 /*! \brief How many subnegotiation characters have we received?
116 *
117 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200118 size_t sb_len;
119
Harald Welte7acb30c2011-08-17 17:13:48 +0200120 /*! \brief Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200121 int width;
Harald Welte7acb30c2011-08-17 17:13:48 +0200122 /*! \brief Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200123 int height;
124
Harald Welte7acb30c2011-08-17 17:13:48 +0200125 /*! \brief Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126 int lines;
127
128 int monitor;
129
Harald Welte7acb30c2011-08-17 17:13:48 +0200130 /*! \brief In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200131 int config;
132};
133
134/* Small macro to determine newline is newline only or linefeed needed. */
135#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
136
137static inline char *vty_newline(struct vty *vty)
138{
139 return VTY_NEWLINE;
140}
141
Harald Welte7acb30c2011-08-17 17:13:48 +0200142/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200143struct vty_app_info {
Harald Welte7acb30c2011-08-17 17:13:48 +0200144 /*! \brief name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800145 const char *name;
Harald Welte7acb30c2011-08-17 17:13:48 +0200146 /*! \brief version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800147 const char *version;
Harald Welte7acb30c2011-08-17 17:13:48 +0200148 /*! \brief copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800149 const char *copyright;
Harald Welte7acb30c2011-08-17 17:13:48 +0200150 /*! \brief \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200151 void *tall_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200152 /*! \brief call-back for returning to parent n ode */
Harald Welte237f6242010-05-25 23:00:45 +0200153 enum node_type (*go_parent_cb)(struct vty *vty);
Harald Welte7acb30c2011-08-17 17:13:48 +0200154 /*! \brief call-back to determine if node is config node */
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800155 int (*is_config_node)(struct vty *vty, int node);
Harald Welte237f6242010-05-25 23:00:45 +0200156};
157
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200158/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200159void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200160int vty_read_config_file(const char *file_name, void *priv);
161void vty_init_vtysh (void);
162void vty_reset (void);
163struct vty *vty_new (void);
164struct vty *vty_create (int vty_sock, void *priv);
165int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
166int vty_out_newline(struct vty *);
167int vty_read(struct vty *vty);
168//void vty_time_print (struct vty *, int);
169void vty_close (struct vty *);
170char *vty_get_cwd (void);
171void vty_log (const char *level, const char *proto, const char *fmt, va_list);
172int vty_config_lock (struct vty *);
173int vty_config_unlock (struct vty *);
174int vty_shell (struct vty *);
175int vty_shell_serv (struct vty *);
176void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800177void *vty_current_index(struct vty *);
178int vty_current_node(struct vty *vty);
Andreas.Eversbergf948dbc2011-11-10 23:09:35 +0100179enum node_type vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200180
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800181extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200182
Harald Welted61d5172011-09-04 22:56:10 +0200183extern struct cmd_element cfg_description_cmd;
184extern struct cmd_element cfg_no_description_cmd;
185
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200186/*! @} */
Harald Welte7acb30c2011-08-17 17:13:48 +0200187
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200188#endif