blob: b63dc3bfad7d0d96319d6366f41fac127451f3a9 [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>
Neels Hofmeyr63053002019-04-10 02:41:53 +02005#include <stdbool.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +02006
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02007#include <osmocom/core/linuxlist.h>
Neels Hofmeyr70ce8712019-11-24 19:52:44 +01008#include <osmocom/core/defs.h>
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02009
Harald Welte7acb30c2011-08-17 17:13:48 +020010/*! \defgroup vty VTY (Virtual TTY) interface
11 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020012 * \file vty.h */
Harald Welte7acb30c2011-08-17 17:13:48 +020013
Harald Welte3fb0b6f2010-05-19 19:02:52 +020014/* GCC have printf type attribute check. */
15#ifdef __GNUC__
16#define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
17#else
18#define VTY_PRINTF_ATTRIBUTE(a,b)
19#endif /* __GNUC__ */
20
21/* Does the I/O error indicate that the operation should be retried later? */
22#define ERRNO_IO_RETRY(EN) \
23 (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
24
25/* Vty read buffer size. */
26#define VTY_READ_BUFSIZ 512
27
28#define VTY_BUFSIZ 512
29#define VTY_MAXHIST 20
30
Neels Hofmeyr87e45502017-06-20 00:17:59 +020031/*! VTY events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020032enum event {
33 VTY_SERV,
34 VTY_READ,
35 VTY_WRITE,
36 VTY_CLOSED,
37 VTY_TIMEOUT_RESET,
38#ifdef VTYSH
39 VTYSH_SERV,
40 VTYSH_READ,
41 VTYSH_WRITE
42#endif /* VTYSH */
43};
44
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020045enum vty_type {
46 VTY_TERM,
47 VTY_FILE,
48 VTY_SHELL,
49 VTY_SHELL_SERV
50};
51
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +020052struct vty_parent_node {
53 struct llist_head entry;
54
55 /*! private data, specified by creator */
56 void *priv;
57
58 /*! Node status of this vty */
59 int node;
60
61 /*! When reading from a config file, these are the indenting characters expected for children of
62 * this VTY node. */
63 char *indent;
64};
65
Harald Welte7acb30c2011-08-17 17:13:48 +020066/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020067struct vty {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020068 /*! underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020069 FILE *file;
70
Neels Hofmeyr87e45502017-06-20 00:17:59 +020071 /*! private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020072 void *priv;
73
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074 /*! File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020075 int fd;
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077 /*! Is this vty connect to file or not */
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020078 enum vty_type type;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020079
Neels Hofmeyr87e45502017-06-20 00:17:59 +020080 /*! Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020081 int node;
82
Neels Hofmeyr87e45502017-06-20 00:17:59 +020083 /*! Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020084 int fail;
85
Neels Hofmeyr87e45502017-06-20 00:17:59 +020086 /*! Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020087 struct buffer *obuf;
88
Neels Hofmeyr87e45502017-06-20 00:17:59 +020089 /*! Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020090 char *buf;
91
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092 /*! Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020093 int cp;
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095 /*! Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020096 int length;
97
Neels Hofmeyr87e45502017-06-20 00:17:59 +020098 /*! Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020099 int max;
100
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200101 /*! Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200102 char *hist[VTY_MAXHIST];
103
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200104 /*! History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200105 int hp;
106
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200107 /*! History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200108 int hindex;
109
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200110 /*! For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200111 access-list etc... */
112 void *index;
113
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200114 /*! For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200115 void *index_sub;
116
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200117 /*! For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200118 unsigned char escape;
119
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200120 /*! Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200121 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
122
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200123 /*! IAC handling
Harald Welte7acb30c2011-08-17 17:13:48 +0200124 *
125 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126 * (interpret-as-command) escape character (and therefore the next
127 * character will be the command code)? Refer to Telnet RFC 854. */
128 unsigned char iac;
129
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200130 /*! IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200131 unsigned char iac_sb_in_progress;
132 /* At the moment, we care only about the NAWS (window size) negotiation,
133 * and that requires just a 5-character buffer (RFC 1073):
134 * <NAWS char> <16-bit width> <16-bit height> */
135#define TELNET_NAWS_SB_LEN 5
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136 /*! sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200137 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138 /*! How many subnegotiation characters have we received?
Harald Welte7acb30c2011-08-17 17:13:48 +0200139 *
140 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200141 size_t sb_len;
142
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200143 /*! Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200144 int width;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200145 /*! Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200146 int height;
147
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200148 /*! Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200149 int lines;
150
151 int monitor;
152
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153 /*! In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200154 int config;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200155
156 /*! List of parent nodes, last item is the outermost parent. */
157 struct llist_head parent_nodes;
158
159 /*! When reading from a config file, these are the indenting characters expected for children of
160 * the current VTY node. */
161 char *indent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200162};
163
164/* Small macro to determine newline is newline only or linefeed needed. */
165#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
166
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +0200167static inline const char *vty_newline(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200168{
169 return VTY_NEWLINE;
170}
171
Harald Welte7acb30c2011-08-17 17:13:48 +0200172/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200173struct vty_app_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200174 /*! name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800175 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200176 /*! version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800177 const char *version;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200178 /*! copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800179 const char *copyright;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180 /*! \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200181 void *tall_ctx;
Neels Hofmeyrd31de232019-10-31 16:09:23 +0100182 /*! Call-back for taking actions upon exiting a node.
183 * The return value is ignored, and changes to vty->node and vty->index made in this callback are ignored.
184 * Implicit parent node tracking always sets the correct parent node and vty->index after this callback exits,
185 * so this callback can handle only those nodes that should take specific actions upon node exit, or can be left
186 * NULL entirely. */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000187 int (*go_parent_cb)(struct vty *vty);
Neels Hofmeyrd31de232019-10-31 16:09:23 +0100188 /*! OBSOLETED: Implicit parent node tracking has replaced the use of this callback. This callback is no longer
189 * called, ever, and can be left NULL. */
Neels Hofmeyr70ce8712019-11-24 19:52:44 +0100190 int (*is_config_node)(struct vty *vty, int node)
191 OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is"
192 " no longer called, ever, and can be left NULL.");
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200193 /*! Check if the config is consistent before write */
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +0100194 int (*config_is_consistent)(struct vty *vty);
Harald Welte237f6242010-05-25 23:00:45 +0200195};
196
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200197/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200198void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200199int vty_read_config_file(const char *file_name, void *priv);
200void vty_init_vtysh (void);
201void vty_reset (void);
202struct vty *vty_new (void);
203struct vty *vty_create (int vty_sock, void *priv);
Neels Hofmeyr63053002019-04-10 02:41:53 +0200204bool vty_is_active(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200205int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
Neels Hofmeyrc1aa1782019-02-01 05:38:44 +0100206int vty_out_va(struct vty *vty, const char *format, va_list ap);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200207int vty_out_newline(struct vty *);
208int vty_read(struct vty *vty);
209//void vty_time_print (struct vty *, int);
210void vty_close (struct vty *);
211char *vty_get_cwd (void);
212void vty_log (const char *level, const char *proto, const char *fmt, va_list);
213int vty_config_lock (struct vty *);
214int vty_config_unlock (struct vty *);
215int vty_shell (struct vty *);
216int vty_shell_serv (struct vty *);
217void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800218void *vty_current_index(struct vty *);
219int vty_current_node(struct vty *vty);
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000220int vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200221
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100222/* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
223const char *vty_get_bind_addr(void);
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000224/** Returns configured port passed to the 'line vty'/'bind' command or default_port. */
225int vty_get_bind_port(int default_port);
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100226
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800227extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200228
Harald Welted61d5172011-09-04 22:56:10 +0200229extern struct cmd_element cfg_description_cmd;
230extern struct cmd_element cfg_no_description_cmd;
231
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200232
233/**
234 * signal handling
235 */
236enum signal_vty {
237 S_VTY_EVENT,
238};
239
240struct vty_signal_data {
241 enum event event;
242 int sock;
243 struct vty *vty;
244};
245
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200246/*! @} */