blob: 6a82d7e3dcda3c2b44925f047e6f4b0ffcdde1b3 [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
Vadim Yanitskiye566bdd2020-10-06 17:41:22 +070031/* Number of application / library specific VTY attributes */
32#define VTY_CMD_USR_ATTR_NUM 32
Vadim Yanitskiyef4c5972020-10-07 13:44:31 +070033/* Flag characters reserved for global VTY attributes */
34#define VTY_CMD_ATTR_FLAGS_RESERVED \
Vadim Yanitskiy72b90882020-10-21 05:07:34 +070035 { '.', '!', '@', '^' }
Vadim Yanitskiye566bdd2020-10-06 17:41:22 +070036
Neels Hofmeyr87e45502017-06-20 00:17:59 +020037/*! VTY events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020038enum event {
39 VTY_SERV,
40 VTY_READ,
41 VTY_WRITE,
42 VTY_CLOSED,
43 VTY_TIMEOUT_RESET,
44#ifdef VTYSH
45 VTYSH_SERV,
46 VTYSH_READ,
47 VTYSH_WRITE
48#endif /* VTYSH */
49};
50
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020051enum vty_type {
52 VTY_TERM,
53 VTY_FILE,
54 VTY_SHELL,
55 VTY_SHELL_SERV
56};
57
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +020058struct vty_parent_node {
59 struct llist_head entry;
60
61 /*! private data, specified by creator */
62 void *priv;
63
64 /*! Node status of this vty */
65 int node;
66
67 /*! When reading from a config file, these are the indenting characters expected for children of
68 * this VTY node. */
69 char *indent;
70};
71
Harald Welte7acb30c2011-08-17 17:13:48 +020072/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020073struct vty {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074 /*! underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020075 FILE *file;
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077 /*! private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020078 void *priv;
79
Neels Hofmeyr87e45502017-06-20 00:17:59 +020080 /*! File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020081 int fd;
82
Neels Hofmeyr87e45502017-06-20 00:17:59 +020083 /*! Is this vty connect to file or not */
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020084 enum vty_type type;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020085
Neels Hofmeyr87e45502017-06-20 00:17:59 +020086 /*! Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020087 int node;
88
Neels Hofmeyr87e45502017-06-20 00:17:59 +020089 /*! Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020090 int fail;
91
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092 /*! Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020093 struct buffer *obuf;
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095 /*! Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020096 char *buf;
97
Neels Hofmeyr87e45502017-06-20 00:17:59 +020098 /*! Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020099 int cp;
100
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200101 /*! Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200102 int length;
103
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200104 /*! Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200105 int max;
106
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200107 /*! Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200108 char *hist[VTY_MAXHIST];
109
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200110 /*! History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200111 int hp;
112
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200113 /*! History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200114 int hindex;
115
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200116 /*! For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200117 access-list etc... */
118 void *index;
119
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200120 /*! For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200121 void *index_sub;
122
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200123 /*! For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200124 unsigned char escape;
125
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200126 /*! Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200127 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129 /*! IAC handling
Harald Welte7acb30c2011-08-17 17:13:48 +0200130 *
131 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200132 * (interpret-as-command) escape character (and therefore the next
133 * character will be the command code)? Refer to Telnet RFC 854. */
134 unsigned char iac;
135
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136 /*! IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200137 unsigned char iac_sb_in_progress;
138 /* At the moment, we care only about the NAWS (window size) negotiation,
139 * and that requires just a 5-character buffer (RFC 1073):
140 * <NAWS char> <16-bit width> <16-bit height> */
141#define TELNET_NAWS_SB_LEN 5
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200142 /*! sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200143 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200144 /*! How many subnegotiation characters have we received?
Harald Welte7acb30c2011-08-17 17:13:48 +0200145 *
146 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200147 size_t sb_len;
148
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200149 /*! Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200150 int width;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151 /*! Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200152 int height;
153
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200154 /*! Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200155 int lines;
156
157 int monitor;
158
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200159 /*! In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200160 int config;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200161
162 /*! List of parent nodes, last item is the outermost parent. */
163 struct llist_head parent_nodes;
164
165 /*! When reading from a config file, these are the indenting characters expected for children of
166 * the current VTY node. */
167 char *indent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200168};
169
170/* Small macro to determine newline is newline only or linefeed needed. */
171#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
172
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +0200173static inline const char *vty_newline(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200174{
175 return VTY_NEWLINE;
176}
177
Harald Welte7acb30c2011-08-17 17:13:48 +0200178/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200179struct vty_app_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180 /*! name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800181 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200182 /*! version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800183 const char *version;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200184 /*! copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800185 const char *copyright;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200186 /*! \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200187 void *tall_ctx;
Neels Hofmeyrd31de232019-10-31 16:09:23 +0100188 /*! Call-back for taking actions upon exiting a node.
189 * The return value is ignored, and changes to vty->node and vty->index made in this callback are ignored.
190 * Implicit parent node tracking always sets the correct parent node and vty->index after this callback exits,
191 * so this callback can handle only those nodes that should take specific actions upon node exit, or can be left
192 * NULL entirely. */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000193 int (*go_parent_cb)(struct vty *vty);
Neels Hofmeyrd31de232019-10-31 16:09:23 +0100194 /*! OBSOLETED: Implicit parent node tracking has replaced the use of this callback. This callback is no longer
195 * called, ever, and can be left NULL. */
Neels Hofmeyr70ce8712019-11-24 19:52:44 +0100196 int (*is_config_node)(struct vty *vty, int node)
197 OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is"
198 " no longer called, ever, and can be left NULL.");
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200199 /*! Check if the config is consistent before write */
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +0100200 int (*config_is_consistent)(struct vty *vty);
Vadim Yanitskiy7f1ecd92020-08-15 22:16:30 +0700201 /*! Description of the application specific VTY attributes (optional). */
Vadim Yanitskiye566bdd2020-10-06 17:41:22 +0700202 const char * usr_attr_desc[VTY_CMD_USR_ATTR_NUM];
Vadim Yanitskiy7f1ecd92020-08-15 22:16:30 +0700203 /*! Flag letters of the application specific VTY attributes (optional). */
Vadim Yanitskiye566bdd2020-10-06 17:41:22 +0700204 char usr_attr_letters[VTY_CMD_USR_ATTR_NUM];
Harald Welte237f6242010-05-25 23:00:45 +0200205};
206
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200207/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200208void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200209int vty_read_config_file(const char *file_name, void *priv);
210void vty_init_vtysh (void);
211void vty_reset (void);
212struct vty *vty_new (void);
213struct vty *vty_create (int vty_sock, void *priv);
Neels Hofmeyr63053002019-04-10 02:41:53 +0200214bool vty_is_active(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200215int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
Neels Hofmeyrc1aa1782019-02-01 05:38:44 +0100216int vty_out_va(struct vty *vty, const char *format, va_list ap);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200217int vty_out_newline(struct vty *);
218int vty_read(struct vty *vty);
219//void vty_time_print (struct vty *, int);
220void vty_close (struct vty *);
221char *vty_get_cwd (void);
222void vty_log (const char *level, const char *proto, const char *fmt, va_list);
223int vty_config_lock (struct vty *);
224int vty_config_unlock (struct vty *);
225int vty_shell (struct vty *);
226int vty_shell_serv (struct vty *);
227void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800228void *vty_current_index(struct vty *);
229int vty_current_node(struct vty *vty);
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000230int vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200231
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100232/* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
233const char *vty_get_bind_addr(void);
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000234/** Returns configured port passed to the 'line vty'/'bind' command or default_port. */
235int vty_get_bind_port(int default_port);
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100236
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800237extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200238
Harald Welted61d5172011-09-04 22:56:10 +0200239extern struct cmd_element cfg_description_cmd;
240extern struct cmd_element cfg_no_description_cmd;
241
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200242
243/**
244 * signal handling
245 */
246enum signal_vty {
247 S_VTY_EVENT,
248};
249
250struct vty_signal_data {
251 enum event event;
252 int sock;
253 struct vty *vty;
254};
255
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200256/*! @} */