blob: 8d2e1b3641d6d8aed104aa5058c9feda1b99482b [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
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +02006#include <osmocom/core/linuxlist.h>
7
Harald Welte7acb30c2011-08-17 17:13:48 +02008/*! \defgroup vty VTY (Virtual TTY) interface
9 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020010 * \file vty.h */
Harald Welte7acb30c2011-08-17 17:13:48 +020011
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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020029/*! 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
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020043enum vty_type {
44 VTY_TERM,
45 VTY_FILE,
46 VTY_SHELL,
47 VTY_SHELL_SERV
48};
49
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +020050struct vty_parent_node {
51 struct llist_head entry;
52
53 /*! private data, specified by creator */
54 void *priv;
55
56 /*! Node status of this vty */
57 int node;
58
59 /*! When reading from a config file, these are the indenting characters expected for children of
60 * this VTY node. */
61 char *indent;
62};
63
Harald Welte7acb30c2011-08-17 17:13:48 +020064/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020065struct vty {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066 /*! underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020067 FILE *file;
68
Neels Hofmeyr87e45502017-06-20 00:17:59 +020069 /*! private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020070 void *priv;
71
Neels Hofmeyr87e45502017-06-20 00:17:59 +020072 /*! File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020073 int fd;
74
Neels Hofmeyr87e45502017-06-20 00:17:59 +020075 /*! Is this vty connect to file or not */
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020076 enum vty_type type;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020077
Neels Hofmeyr87e45502017-06-20 00:17:59 +020078 /*! Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020079 int node;
80
Neels Hofmeyr87e45502017-06-20 00:17:59 +020081 /*! Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020082 int fail;
83
Neels Hofmeyr87e45502017-06-20 00:17:59 +020084 /*! Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020085 struct buffer *obuf;
86
Neels Hofmeyr87e45502017-06-20 00:17:59 +020087 /*! Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020088 char *buf;
89
Neels Hofmeyr87e45502017-06-20 00:17:59 +020090 /*! Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020091 int cp;
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093 /*! Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020094 int length;
95
Neels Hofmeyr87e45502017-06-20 00:17:59 +020096 /*! Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020097 int max;
98
Neels Hofmeyr87e45502017-06-20 00:17:59 +020099 /*! Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200100 char *hist[VTY_MAXHIST];
101
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200102 /*! History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200103 int hp;
104
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200105 /*! History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200106 int hindex;
107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108 /*! For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200109 access-list etc... */
110 void *index;
111
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200112 /*! For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200113 void *index_sub;
114
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200115 /*! For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200116 unsigned char escape;
117
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200118 /*! Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200119 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
120
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200121 /*! IAC handling
Harald Welte7acb30c2011-08-17 17:13:48 +0200122 *
123 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200124 * (interpret-as-command) escape character (and therefore the next
125 * character will be the command code)? Refer to Telnet RFC 854. */
126 unsigned char iac;
127
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200128 /*! IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200129 unsigned char iac_sb_in_progress;
130 /* At the moment, we care only about the NAWS (window size) negotiation,
131 * and that requires just a 5-character buffer (RFC 1073):
132 * <NAWS char> <16-bit width> <16-bit height> */
133#define TELNET_NAWS_SB_LEN 5
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200134 /*! sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200135 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200136 /*! How many subnegotiation characters have we received?
Harald Welte7acb30c2011-08-17 17:13:48 +0200137 *
138 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200139 size_t sb_len;
140
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200141 /*! Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200142 int width;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200143 /*! Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200144 int height;
145
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200146 /*! Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200147 int lines;
148
149 int monitor;
150
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151 /*! In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200152 int config;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200153
154 /*! List of parent nodes, last item is the outermost parent. */
155 struct llist_head parent_nodes;
156
157 /*! When reading from a config file, these are the indenting characters expected for children of
158 * the current VTY node. */
159 char *indent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200160};
161
162/* Small macro to determine newline is newline only or linefeed needed. */
163#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
164
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +0200165static inline const char *vty_newline(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200166{
167 return VTY_NEWLINE;
168}
169
Harald Welte7acb30c2011-08-17 17:13:48 +0200170/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200171struct vty_app_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200172 /*! name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800173 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200174 /*! version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800175 const char *version;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200176 /*! copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800177 const char *copyright;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200178 /*! \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200179 void *tall_ctx;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180 /*! call-back for returning to parent n ode */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000181 int (*go_parent_cb)(struct vty *vty);
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200182 /*! call-back to determine if node is config node */
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800183 int (*is_config_node)(struct vty *vty, int node);
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200184 /*! Check if the config is consistent before write */
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +0100185 int (*config_is_consistent)(struct vty *vty);
Harald Welte237f6242010-05-25 23:00:45 +0200186};
187
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200188/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200189void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200190int vty_read_config_file(const char *file_name, void *priv);
191void vty_init_vtysh (void);
192void vty_reset (void);
193struct vty *vty_new (void);
194struct vty *vty_create (int vty_sock, void *priv);
195int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
Neels Hofmeyrc1aa1782019-02-01 05:38:44 +0100196int vty_out_va(struct vty *vty, const char *format, va_list ap);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200197int vty_out_newline(struct vty *);
198int vty_read(struct vty *vty);
199//void vty_time_print (struct vty *, int);
200void vty_close (struct vty *);
201char *vty_get_cwd (void);
202void vty_log (const char *level, const char *proto, const char *fmt, va_list);
203int vty_config_lock (struct vty *);
204int vty_config_unlock (struct vty *);
205int vty_shell (struct vty *);
206int vty_shell_serv (struct vty *);
207void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800208void *vty_current_index(struct vty *);
209int vty_current_node(struct vty *vty);
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000210int vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200211
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100212/* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
213const char *vty_get_bind_addr(void);
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000214/** Returns configured port passed to the 'line vty'/'bind' command or default_port. */
215int vty_get_bind_port(int default_port);
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100216
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800217extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200218
Harald Welted61d5172011-09-04 22:56:10 +0200219extern struct cmd_element cfg_description_cmd;
220extern struct cmd_element cfg_no_description_cmd;
221
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200222
223/**
224 * signal handling
225 */
226enum signal_vty {
227 S_VTY_EVENT,
228};
229
230struct vty_signal_data {
231 enum event event;
232 int sock;
233 struct vty *vty;
234};
235
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200236/*! @} */