blob: 155501e9ed1a60bd59bd295a83b254b051ff795f [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>
Alexander Couzens06929162021-09-05 23:08:59 +02006#include <time.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +02007
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02008#include <osmocom/core/linuxlist.h>
Neels Hofmeyr70ce8712019-11-24 19:52:44 +01009#include <osmocom/core/defs.h>
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +020010
Harald Welte7acb30c2011-08-17 17:13:48 +020011/*! \defgroup vty VTY (Virtual TTY) interface
12 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020013 * \file vty.h */
Harald Welte7acb30c2011-08-17 17:13:48 +020014
Harald Welte3fb0b6f2010-05-19 19:02:52 +020015/* GCC have printf type attribute check. */
16#ifdef __GNUC__
17#define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
18#else
19#define VTY_PRINTF_ATTRIBUTE(a,b)
20#endif /* __GNUC__ */
21
22/* Does the I/O error indicate that the operation should be retried later? */
23#define ERRNO_IO_RETRY(EN) \
24 (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
25
26/* Vty read buffer size. */
27#define VTY_READ_BUFSIZ 512
28
29#define VTY_BUFSIZ 512
30#define VTY_MAXHIST 20
31
Vadim Yanitskiye566bdd2020-10-06 17:41:22 +070032/* Number of application / library specific VTY attributes */
33#define VTY_CMD_USR_ATTR_NUM 32
Vadim Yanitskiyef4c5972020-10-07 13:44:31 +070034/* Flag characters reserved for global VTY attributes */
35#define VTY_CMD_ATTR_FLAGS_RESERVED \
Vadim Yanitskiy72b90882020-10-21 05:07:34 +070036 { '.', '!', '@', '^' }
Vadim Yanitskiye566bdd2020-10-06 17:41:22 +070037
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038/*! VTY events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020039enum event {
40 VTY_SERV,
41 VTY_READ,
42 VTY_WRITE,
43 VTY_CLOSED,
44 VTY_TIMEOUT_RESET,
45#ifdef VTYSH
46 VTYSH_SERV,
47 VTYSH_READ,
48 VTYSH_WRITE
49#endif /* VTYSH */
50};
51
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020052enum vty_type {
53 VTY_TERM,
54 VTY_FILE,
55 VTY_SHELL,
56 VTY_SHELL_SERV
57};
58
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +020059struct vty_parent_node {
60 struct llist_head entry;
61
62 /*! private data, specified by creator */
63 void *priv;
Neels Hofmeyr67d84d22023-05-01 02:59:01 +020064 void *index;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +020065
66 /*! Node status of this vty */
67 int node;
68
69 /*! When reading from a config file, these are the indenting characters expected for children of
70 * this VTY node. */
71 char *indent;
72};
73
Harald Welte7acb30c2011-08-17 17:13:48 +020074/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020075struct vty {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020076 /*! underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020077 FILE *file;
78
Neels Hofmeyr87e45502017-06-20 00:17:59 +020079 /*! private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020080 void *priv;
81
Neels Hofmeyr87e45502017-06-20 00:17:59 +020082 /*! File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020083 int fd;
84
Neels Hofmeyr87e45502017-06-20 00:17:59 +020085 /*! Is this vty connect to file or not */
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020086 enum vty_type type;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020087
Neels Hofmeyr87e45502017-06-20 00:17:59 +020088 /*! Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020089 int node;
90
Neels Hofmeyr87e45502017-06-20 00:17:59 +020091 /*! Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020092 int fail;
93
Neels Hofmeyr87e45502017-06-20 00:17:59 +020094 /*! Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095 struct buffer *obuf;
96
Neels Hofmeyr87e45502017-06-20 00:17:59 +020097 /*! Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020098 char *buf;
99
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200100 /*! Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200101 int cp;
102
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200103 /*! Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200104 int length;
105
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200106 /*! Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200107 int max;
108
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200109 /*! Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200110 char *hist[VTY_MAXHIST];
111
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200112 /*! History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200113 int hp;
114
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200115 /*! History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200116 int hindex;
117
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200118 /*! For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200119 access-list etc... */
120 void *index;
121
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200122 /*! For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200123 void *index_sub;
124
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200125 /*! For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126 unsigned char escape;
127
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200128 /*! Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200129 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131 /*! IAC handling
Harald Welte7acb30c2011-08-17 17:13:48 +0200132 *
133 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200134 * (interpret-as-command) escape character (and therefore the next
135 * character will be the command code)? Refer to Telnet RFC 854. */
136 unsigned char iac;
137
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200138 /*! IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200139 unsigned char iac_sb_in_progress;
140 /* At the moment, we care only about the NAWS (window size) negotiation,
141 * and that requires just a 5-character buffer (RFC 1073):
142 * <NAWS char> <16-bit width> <16-bit height> */
143#define TELNET_NAWS_SB_LEN 5
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200144 /*! sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200145 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Pau Espin Pedrol645aec82021-05-18 14:46:29 +0200146 /*! How many subnegotiation characters have we received?
Harald Welte7acb30c2011-08-17 17:13:48 +0200147 *
148 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200149 size_t sb_len;
150
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151 /*! Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200152 int width;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153 /*! Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200154 int height;
155
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200156 /*! Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200157 int lines;
158
159 int monitor;
160
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161 /*! In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200162 int config;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200163
164 /*! List of parent nodes, last item is the outermost parent. */
165 struct llist_head parent_nodes;
166
167 /*! When reading from a config file, these are the indenting characters expected for children of
168 * the current VTY node. */
169 char *indent;
Vadim Yanitskiy0a2d9bd2020-10-25 16:34:57 +0700170
171 /*! Whether the expert mode is enabled. */
172 bool expert_mode;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200173};
174
175/* Small macro to determine newline is newline only or linefeed needed. */
176#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
177
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +0200178static inline const char *vty_newline(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200179{
180 return VTY_NEWLINE;
181}
182
Harald Welte7acb30c2011-08-17 17:13:48 +0200183/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200184struct vty_app_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200185 /*! name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800186 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200187 /*! version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800188 const char *version;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200189 /*! copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800190 const char *copyright;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200191 /*! \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200192 void *tall_ctx;
Neels Hofmeyrd31de232019-10-31 16:09:23 +0100193 /*! Call-back for taking actions upon exiting a node.
194 * The return value is ignored, and changes to vty->node and vty->index made in this callback are ignored.
195 * Implicit parent node tracking always sets the correct parent node and vty->index after this callback exits,
196 * so this callback can handle only those nodes that should take specific actions upon node exit, or can be left
197 * NULL entirely. */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000198 int (*go_parent_cb)(struct vty *vty);
Neels Hofmeyrd31de232019-10-31 16:09:23 +0100199 /*! OBSOLETED: Implicit parent node tracking has replaced the use of this callback. This callback is no longer
200 * called, ever, and can be left NULL. */
Neels Hofmeyr70ce8712019-11-24 19:52:44 +0100201 int (*is_config_node)(struct vty *vty, int node)
202 OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is"
203 " no longer called, ever, and can be left NULL.");
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200204 /*! Check if the config is consistent before write */
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +0100205 int (*config_is_consistent)(struct vty *vty);
Vadim Yanitskiy7f1ecd92020-08-15 22:16:30 +0700206 /*! Description of the application specific VTY attributes (optional). */
Vadim Yanitskiye566bdd2020-10-06 17:41:22 +0700207 const char * usr_attr_desc[VTY_CMD_USR_ATTR_NUM];
Vadim Yanitskiy7f1ecd92020-08-15 22:16:30 +0700208 /*! Flag letters of the application specific VTY attributes (optional). */
Vadim Yanitskiye566bdd2020-10-06 17:41:22 +0700209 char usr_attr_letters[VTY_CMD_USR_ATTR_NUM];
Harald Welte237f6242010-05-25 23:00:45 +0200210};
211
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200212/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200213void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200214int vty_read_config_file(const char *file_name, void *priv);
Pau Espin Pedrol645aec82021-05-18 14:46:29 +0200215int vty_read_config_filep(FILE *confp, void *priv);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200216void vty_init_vtysh (void);
217void vty_reset (void);
218struct vty *vty_new (void);
219struct vty *vty_create (int vty_sock, void *priv);
Neels Hofmeyr63053002019-04-10 02:41:53 +0200220bool vty_is_active(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200221int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
Neels Hofmeyrc1aa1782019-02-01 05:38:44 +0100222int vty_out_va(struct vty *vty, const char *format, va_list ap);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200223int vty_out_newline(struct vty *);
Alexander Couzens06929162021-09-05 23:08:59 +0200224int vty_out_uptime(struct vty *vty, const struct timespec *starttime);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200225int vty_read(struct vty *vty);
226//void vty_time_print (struct vty *, int);
227void vty_close (struct vty *);
Eric11a58a12021-09-09 15:42:32 +0200228void vty_flush(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200229char *vty_get_cwd (void);
230void vty_log (const char *level, const char *proto, const char *fmt, va_list);
231int vty_config_lock (struct vty *);
232int vty_config_unlock (struct vty *);
233int vty_shell (struct vty *);
234int vty_shell_serv (struct vty *);
235void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800236void *vty_current_index(struct vty *);
237int vty_current_node(struct vty *vty);
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000238int vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200239
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100240/* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
241const char *vty_get_bind_addr(void);
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000242/** Returns configured port passed to the 'line vty'/'bind' command or default_port. */
243int vty_get_bind_port(int default_port);
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100244
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800245extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200246
Harald Welted61d5172011-09-04 22:56:10 +0200247extern struct cmd_element cfg_description_cmd;
248extern struct cmd_element cfg_no_description_cmd;
249
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200250
251/**
252 * signal handling
253 */
254enum signal_vty {
255 S_VTY_EVENT,
256};
257
258struct vty_signal_data {
259 enum event event;
260 int sock;
261 struct vty *vty;
262};
263
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200264/*! @} */