blob: 9acaa7d2ca928f72c5f0a200ae75021de3b4d8c3 [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>
8
Harald Welte7acb30c2011-08-17 17:13:48 +02009/*! \defgroup vty VTY (Virtual TTY) interface
10 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020011 * \file vty.h */
Harald Welte7acb30c2011-08-17 17:13:48 +020012
Harald Welte3fb0b6f2010-05-19 19:02:52 +020013/* GCC have printf type attribute check. */
14#ifdef __GNUC__
15#define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
16#else
17#define VTY_PRINTF_ATTRIBUTE(a,b)
18#endif /* __GNUC__ */
19
20/* Does the I/O error indicate that the operation should be retried later? */
21#define ERRNO_IO_RETRY(EN) \
22 (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
23
24/* Vty read buffer size. */
25#define VTY_READ_BUFSIZ 512
26
27#define VTY_BUFSIZ 512
28#define VTY_MAXHIST 20
29
Neels Hofmeyr87e45502017-06-20 00:17:59 +020030/*! VTY events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020031enum event {
32 VTY_SERV,
33 VTY_READ,
34 VTY_WRITE,
35 VTY_CLOSED,
36 VTY_TIMEOUT_RESET,
37#ifdef VTYSH
38 VTYSH_SERV,
39 VTYSH_READ,
40 VTYSH_WRITE
41#endif /* VTYSH */
42};
43
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020044enum vty_type {
45 VTY_TERM,
46 VTY_FILE,
47 VTY_SHELL,
48 VTY_SHELL_SERV
49};
50
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +020051struct vty_parent_node {
52 struct llist_head entry;
53
54 /*! private data, specified by creator */
55 void *priv;
56
57 /*! Node status of this vty */
58 int node;
59
60 /*! When reading from a config file, these are the indenting characters expected for children of
61 * this VTY node. */
62 char *indent;
63};
64
Harald Welte7acb30c2011-08-17 17:13:48 +020065/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020066struct vty {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020067 /*! underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020068 FILE *file;
69
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070 /*! private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020071 void *priv;
72
Neels Hofmeyr87e45502017-06-20 00:17:59 +020073 /*! File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020074 int fd;
75
Neels Hofmeyr87e45502017-06-20 00:17:59 +020076 /*! Is this vty connect to file or not */
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020077 enum vty_type type;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020078
Neels Hofmeyr87e45502017-06-20 00:17:59 +020079 /*! Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020080 int node;
81
Neels Hofmeyr87e45502017-06-20 00:17:59 +020082 /*! Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020083 int fail;
84
Neels Hofmeyr87e45502017-06-20 00:17:59 +020085 /*! Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020086 struct buffer *obuf;
87
Neels Hofmeyr87e45502017-06-20 00:17:59 +020088 /*! Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020089 char *buf;
90
Neels Hofmeyr87e45502017-06-20 00:17:59 +020091 /*! Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020092 int cp;
93
Neels Hofmeyr87e45502017-06-20 00:17:59 +020094 /*! Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095 int length;
96
Neels Hofmeyr87e45502017-06-20 00:17:59 +020097 /*! Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020098 int max;
99
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200100 /*! Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200101 char *hist[VTY_MAXHIST];
102
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200103 /*! History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200104 int hp;
105
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200106 /*! History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200107 int hindex;
108
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200109 /*! For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200110 access-list etc... */
111 void *index;
112
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200113 /*! For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200114 void *index_sub;
115
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200116 /*! For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200117 unsigned char escape;
118
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200119 /*! Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200120 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
121
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200122 /*! IAC handling
Harald Welte7acb30c2011-08-17 17:13:48 +0200123 *
124 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200125 * (interpret-as-command) escape character (and therefore the next
126 * character will be the command code)? Refer to Telnet RFC 854. */
127 unsigned char iac;
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129 /*! IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200130 unsigned char iac_sb_in_progress;
131 /* At the moment, we care only about the NAWS (window size) negotiation,
132 * and that requires just a 5-character buffer (RFC 1073):
133 * <NAWS char> <16-bit width> <16-bit height> */
134#define TELNET_NAWS_SB_LEN 5
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200135 /*! sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200136 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200137 /*! How many subnegotiation characters have we received?
Harald Welte7acb30c2011-08-17 17:13:48 +0200138 *
139 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200140 size_t sb_len;
141
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200142 /*! Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200143 int width;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200144 /*! Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200145 int height;
146
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200147 /*! Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200148 int lines;
149
150 int monitor;
151
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200152 /*! In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200153 int config;
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200154
155 /*! List of parent nodes, last item is the outermost parent. */
156 struct llist_head parent_nodes;
157
158 /*! When reading from a config file, these are the indenting characters expected for children of
159 * the current VTY node. */
160 char *indent;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200161};
162
163/* Small macro to determine newline is newline only or linefeed needed. */
164#define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
165
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +0200166static inline const char *vty_newline(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200167{
168 return VTY_NEWLINE;
169}
170
Harald Welte7acb30c2011-08-17 17:13:48 +0200171/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200172struct vty_app_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200173 /*! name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800174 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200175 /*! version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800176 const char *version;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200177 /*! copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800178 const char *copyright;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200179 /*! \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200180 void *tall_ctx;
Neels Hofmeyrd31de232019-10-31 16:09:23 +0100181 /*! Call-back for taking actions upon exiting a node.
182 * The return value is ignored, and changes to vty->node and vty->index made in this callback are ignored.
183 * Implicit parent node tracking always sets the correct parent node and vty->index after this callback exits,
184 * so this callback can handle only those nodes that should take specific actions upon node exit, or can be left
185 * NULL entirely. */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000186 int (*go_parent_cb)(struct vty *vty);
Neels Hofmeyrd31de232019-10-31 16:09:23 +0100187 /*! OBSOLETED: Implicit parent node tracking has replaced the use of this callback. This callback is no longer
188 * called, ever, and can be left NULL. */
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800189 int (*is_config_node)(struct vty *vty, int node);
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200190 /*! Check if the config is consistent before write */
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +0100191 int (*config_is_consistent)(struct vty *vty);
Harald Welte237f6242010-05-25 23:00:45 +0200192};
193
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200194/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200195void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200196int vty_read_config_file(const char *file_name, void *priv);
197void vty_init_vtysh (void);
198void vty_reset (void);
199struct vty *vty_new (void);
200struct vty *vty_create (int vty_sock, void *priv);
Neels Hofmeyr63053002019-04-10 02:41:53 +0200201bool vty_is_active(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200202int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
Neels Hofmeyrc1aa1782019-02-01 05:38:44 +0100203int vty_out_va(struct vty *vty, const char *format, va_list ap);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200204int vty_out_newline(struct vty *);
205int vty_read(struct vty *vty);
206//void vty_time_print (struct vty *, int);
207void vty_close (struct vty *);
208char *vty_get_cwd (void);
209void vty_log (const char *level, const char *proto, const char *fmt, va_list);
210int vty_config_lock (struct vty *);
211int vty_config_unlock (struct vty *);
212int vty_shell (struct vty *);
213int vty_shell_serv (struct vty *);
214void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800215void *vty_current_index(struct vty *);
216int vty_current_node(struct vty *vty);
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000217int vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200218
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100219/* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
220const char *vty_get_bind_addr(void);
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000221/** Returns configured port passed to the 'line vty'/'bind' command or default_port. */
222int vty_get_bind_port(int default_port);
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100223
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800224extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200225
Harald Welted61d5172011-09-04 22:56:10 +0200226extern struct cmd_element cfg_description_cmd;
227extern struct cmd_element cfg_no_description_cmd;
228
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200229
230/**
231 * signal handling
232 */
233enum signal_vty {
234 S_VTY_EVENT,
235};
236
237struct vty_signal_data {
238 enum event event;
239 int sock;
240 struct vty *vty;
241};
242
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200243/*! @} */