blob: 43cb0cfeb2e2505a933e5a6ab2e556a54b00fbb5 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002
3#include <stdio.h>
4#include <stdarg.h>
5
Harald Welte7acb30c2011-08-17 17:13:48 +02006/*! \defgroup vty VTY (Virtual TTY) interface
7 * @{
8 */
9/*! \file vty.h */
10
Harald Welte3fb0b6f2010-05-19 19:02:52 +020011/* GCC have printf type attribute check. */
12#ifdef __GNUC__
13#define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
14#else
15#define VTY_PRINTF_ATTRIBUTE(a,b)
16#endif /* __GNUC__ */
17
18/* Does the I/O error indicate that the operation should be retried later? */
19#define ERRNO_IO_RETRY(EN) \
20 (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
21
22/* Vty read buffer size. */
23#define VTY_READ_BUFSIZ 512
24
25#define VTY_BUFSIZ 512
26#define VTY_MAXHIST 20
27
Harald Welte7acb30c2011-08-17 17:13:48 +020028/*! \brief VTY events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020029enum event {
30 VTY_SERV,
31 VTY_READ,
32 VTY_WRITE,
33 VTY_CLOSED,
34 VTY_TIMEOUT_RESET,
35#ifdef VTYSH
36 VTYSH_SERV,
37 VTYSH_READ,
38 VTYSH_WRITE
39#endif /* VTYSH */
40};
41
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020042enum vty_type {
43 VTY_TERM,
44 VTY_FILE,
45 VTY_SHELL,
46 VTY_SHELL_SERV
47};
48
Harald Welte7acb30c2011-08-17 17:13:48 +020049/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020050struct vty {
Harald Welte7acb30c2011-08-17 17:13:48 +020051 /*! \brief underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020052 FILE *file;
53
Harald Welte7acb30c2011-08-17 17:13:48 +020054 /*! \brief private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020055 void *priv;
56
Harald Welte7acb30c2011-08-17 17:13:48 +020057 /*! \brief File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020058 int fd;
59
Harald Welte7acb30c2011-08-17 17:13:48 +020060 /*! \brief Is this vty connect to file or not */
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020061 enum vty_type type;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020062
Harald Welte7acb30c2011-08-17 17:13:48 +020063 /*! \brief Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020064 int node;
65
Harald Welte7acb30c2011-08-17 17:13:48 +020066 /*! \brief Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020067 int fail;
68
Harald Welte7acb30c2011-08-17 17:13:48 +020069 /*! \brief Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020070 struct buffer *obuf;
71
Harald Welte7acb30c2011-08-17 17:13:48 +020072 /*! \brief Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020073 char *buf;
74
Harald Welte7acb30c2011-08-17 17:13:48 +020075 /*! \brief Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020076 int cp;
77
Harald Welte7acb30c2011-08-17 17:13:48 +020078 /*! \brief Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020079 int length;
80
Harald Welte7acb30c2011-08-17 17:13:48 +020081 /*! \brief Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020082 int max;
83
Harald Welte7acb30c2011-08-17 17:13:48 +020084 /*! \brief Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020085 char *hist[VTY_MAXHIST];
86
Harald Welte7acb30c2011-08-17 17:13:48 +020087 /*! \brief History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020088 int hp;
89
Harald Welte7acb30c2011-08-17 17:13:48 +020090 /*! \brief History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020091 int hindex;
92
Harald Welte7acb30c2011-08-17 17:13:48 +020093 /*! \brief For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +020094 access-list etc... */
95 void *index;
96
Harald Welte7acb30c2011-08-17 17:13:48 +020097 /*! \brief For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020098 void *index_sub;
99
Harald Welte7acb30c2011-08-17 17:13:48 +0200100 /*! \brief For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200101 unsigned char escape;
102
Harald Welte7acb30c2011-08-17 17:13:48 +0200103 /*! \brief Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200104 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
105
Harald Welte7acb30c2011-08-17 17:13:48 +0200106 /*! \brief IAC handling
107 *
108 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200109 * (interpret-as-command) escape character (and therefore the next
110 * character will be the command code)? Refer to Telnet RFC 854. */
111 unsigned char iac;
112
Harald Welte7acb30c2011-08-17 17:13:48 +0200113 /*! \brief IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200114 unsigned char iac_sb_in_progress;
115 /* At the moment, we care only about the NAWS (window size) negotiation,
116 * and that requires just a 5-character buffer (RFC 1073):
117 * <NAWS char> <16-bit width> <16-bit height> */
118#define TELNET_NAWS_SB_LEN 5
Harald Welte7acb30c2011-08-17 17:13:48 +0200119 /*! \brief sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200120 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Harald Welte7acb30c2011-08-17 17:13:48 +0200121 /*! \brief How many subnegotiation characters have we received?
122 *
123 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200124 size_t sb_len;
125
Harald Welte7acb30c2011-08-17 17:13:48 +0200126 /*! \brief Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200127 int width;
Harald Welte7acb30c2011-08-17 17:13:48 +0200128 /*! \brief Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200129 int height;
130
Harald Welte7acb30c2011-08-17 17:13:48 +0200131 /*! \brief Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200132 int lines;
133
134 int monitor;
135
Harald Welte7acb30c2011-08-17 17:13:48 +0200136 /*! \brief In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200137 int config;
138};
139
140/* Small macro to determine newline is newline only or linefeed needed. */
141#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
142
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +0200143static inline const char *vty_newline(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200144{
145 return VTY_NEWLINE;
146}
147
Harald Welte7acb30c2011-08-17 17:13:48 +0200148/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200149struct vty_app_info {
Harald Welte7acb30c2011-08-17 17:13:48 +0200150 /*! \brief name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800151 const char *name;
Harald Welte7acb30c2011-08-17 17:13:48 +0200152 /*! \brief version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800153 const char *version;
Harald Welte7acb30c2011-08-17 17:13:48 +0200154 /*! \brief copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800155 const char *copyright;
Harald Welte7acb30c2011-08-17 17:13:48 +0200156 /*! \brief \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200157 void *tall_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200158 /*! \brief call-back for returning to parent n ode */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000159 int (*go_parent_cb)(struct vty *vty);
Harald Welte7acb30c2011-08-17 17:13:48 +0200160 /*! \brief call-back to determine if node is config node */
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800161 int (*is_config_node)(struct vty *vty, int node);
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +0100162 /*! \brief Check if the config is consistent before write */
163 int (*config_is_consistent)(struct vty *vty);
Harald Welte237f6242010-05-25 23:00:45 +0200164};
165
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200166/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200167void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200168int vty_read_config_file(const char *file_name, void *priv);
169void vty_init_vtysh (void);
170void vty_reset (void);
171struct vty *vty_new (void);
172struct vty *vty_create (int vty_sock, void *priv);
173int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
174int vty_out_newline(struct vty *);
175int vty_read(struct vty *vty);
176//void vty_time_print (struct vty *, int);
177void vty_close (struct vty *);
178char *vty_get_cwd (void);
179void vty_log (const char *level, const char *proto, const char *fmt, va_list);
180int vty_config_lock (struct vty *);
181int vty_config_unlock (struct vty *);
182int vty_shell (struct vty *);
183int vty_shell_serv (struct vty *);
184void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800185void *vty_current_index(struct vty *);
186int vty_current_node(struct vty *vty);
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000187int vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200188
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100189/* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
190const char *vty_get_bind_addr(void);
191
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800192extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200193
Harald Welted61d5172011-09-04 22:56:10 +0200194extern struct cmd_element cfg_description_cmd;
195extern struct cmd_element cfg_no_description_cmd;
196
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200197
198/**
199 * signal handling
200 */
201enum signal_vty {
202 S_VTY_EVENT,
203};
204
205struct vty_signal_data {
206 enum event event;
207 int sock;
208 struct vty *vty;
209};
210
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200211/*! @} */