Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdarg.h> |
Neels Hofmeyr | 6305300 | 2019-04-10 02:41:53 +0200 | [diff] [blame] | 5 | #include <stdbool.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 6 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 7 | #include <osmocom/core/linuxlist.h> |
| 8 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 9 | /*! \defgroup vty VTY (Virtual TTY) interface |
| 10 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 11 | * \file vty.h */ |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 12 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 13 | /* 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 30 | /*! VTY events */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 31 | enum 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 Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 44 | enum vty_type { |
| 45 | VTY_TERM, |
| 46 | VTY_FILE, |
| 47 | VTY_SHELL, |
| 48 | VTY_SHELL_SERV |
| 49 | }; |
| 50 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 51 | struct 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 Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 65 | /*! Internal representation of a single VTY */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 66 | struct vty { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 67 | /*! underlying file (if any) */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 68 | FILE *file; |
| 69 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 70 | /*! private data, specified by creator */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 71 | void *priv; |
| 72 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 73 | /*! File descripter of this vty. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 74 | int fd; |
| 75 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 76 | /*! Is this vty connect to file or not */ |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 77 | enum vty_type type; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 78 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 79 | /*! Node status of this vty */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 80 | int node; |
| 81 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 82 | /*! Failure count */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 83 | int fail; |
| 84 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 85 | /*! Output buffer. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 86 | struct buffer *obuf; |
| 87 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 88 | /*! Command input buffer */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 89 | char *buf; |
| 90 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 91 | /*! Command cursor point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 92 | int cp; |
| 93 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 94 | /*! Command length */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 95 | int length; |
| 96 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 97 | /*! Command max length. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 98 | int max; |
| 99 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 100 | /*! Histry of command */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 101 | char *hist[VTY_MAXHIST]; |
| 102 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 103 | /*! History lookup current point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 104 | int hp; |
| 105 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 106 | /*! History insert end point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 107 | int hindex; |
| 108 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 109 | /*! For current referencing point of interface, route-map, |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 110 | access-list etc... */ |
| 111 | void *index; |
| 112 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 113 | /*! For multiple level index treatment such as key chain and key. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 114 | void *index_sub; |
| 115 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 116 | /*! For escape character. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 117 | unsigned char escape; |
| 118 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 119 | /*! Current vty status. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 120 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; |
| 121 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 122 | /*! IAC handling |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 123 | * |
| 124 | * IAC handling: was the last character received the IAC |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 125 | * (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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 129 | /*! IAC SB (option subnegotiation) handling */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 130 | 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 Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 135 | /*! sub-negotiation buffer */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 136 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 137 | /*! How many subnegotiation characters have we received? |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 138 | * |
| 139 | * We just drop those that do not fit in the buffer. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 140 | size_t sb_len; |
| 141 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 142 | /*! Window width */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 143 | int width; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 144 | /*! Widnow height */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 145 | int height; |
| 146 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 147 | /*! Configure lines. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 148 | int lines; |
| 149 | |
| 150 | int monitor; |
| 151 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 152 | /*! In configure mode. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 153 | int config; |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 154 | |
| 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 Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 161 | }; |
| 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 Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 166 | static inline const char *vty_newline(struct vty *vty) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 167 | { |
| 168 | return VTY_NEWLINE; |
| 169 | } |
| 170 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 171 | /*! Information an application registers with the VTY */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 172 | struct vty_app_info { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 173 | /*! name of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 174 | const char *name; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 175 | /*! version string of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 176 | const char *version; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 177 | /*! copyright string of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 178 | const char *copyright; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 179 | /*! \ref talloc context */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 180 | void *tall_ctx; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 181 | /*! call-back for returning to parent n ode */ |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 182 | int (*go_parent_cb)(struct vty *vty); |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 183 | /*! call-back to determine if node is config node */ |
Holger Hans Peter Freyther | 8f09f01 | 2010-08-25 17:34:56 +0800 | [diff] [blame] | 184 | int (*is_config_node)(struct vty *vty, int node); |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 185 | /*! Check if the config is consistent before write */ |
Holger Hans Peter Freyther | 9f0f978 | 2014-11-21 10:40:07 +0100 | [diff] [blame] | 186 | int (*config_is_consistent)(struct vty *vty); |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 187 | }; |
| 188 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 189 | /* Prototypes. */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 190 | void vty_init(struct vty_app_info *app_info); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 191 | int vty_read_config_file(const char *file_name, void *priv); |
| 192 | void vty_init_vtysh (void); |
| 193 | void vty_reset (void); |
| 194 | struct vty *vty_new (void); |
| 195 | struct vty *vty_create (int vty_sock, void *priv); |
Neels Hofmeyr | 6305300 | 2019-04-10 02:41:53 +0200 | [diff] [blame] | 196 | bool vty_is_active(struct vty *vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 197 | int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3); |
Neels Hofmeyr | c1aa178 | 2019-02-01 05:38:44 +0100 | [diff] [blame] | 198 | int vty_out_va(struct vty *vty, const char *format, va_list ap); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 199 | int vty_out_newline(struct vty *); |
| 200 | int vty_read(struct vty *vty); |
| 201 | //void vty_time_print (struct vty *, int); |
| 202 | void vty_close (struct vty *); |
| 203 | char *vty_get_cwd (void); |
| 204 | void vty_log (const char *level, const char *proto, const char *fmt, va_list); |
| 205 | int vty_config_lock (struct vty *); |
| 206 | int vty_config_unlock (struct vty *); |
| 207 | int vty_shell (struct vty *); |
| 208 | int vty_shell_serv (struct vty *); |
| 209 | void vty_hello (struct vty *); |
Holger Hans Peter Freyther | 08aaded | 2010-09-14 02:24:03 +0800 | [diff] [blame] | 210 | void *vty_current_index(struct vty *); |
| 211 | int vty_current_node(struct vty *vty); |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 212 | int vty_go_parent(struct vty *vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 213 | |
Neels Hofmeyr | 96172f0 | 2016-02-23 14:01:41 +0100 | [diff] [blame] | 214 | /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */ |
| 215 | const char *vty_get_bind_addr(void); |
Holger Hans Peter Freyther | 99ae401 | 2018-12-15 17:36:41 +0000 | [diff] [blame] | 216 | /** Returns configured port passed to the 'line vty'/'bind' command or default_port. */ |
| 217 | int vty_get_bind_port(int default_port); |
Neels Hofmeyr | 96172f0 | 2016-02-23 14:01:41 +0100 | [diff] [blame] | 218 | |
Holger Hans Peter Freyther | 4aaccd7 | 2010-08-31 17:09:44 +0800 | [diff] [blame] | 219 | extern void *tall_vty_ctx; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 220 | |
Harald Welte | d61d517 | 2011-09-04 22:56:10 +0200 | [diff] [blame] | 221 | extern struct cmd_element cfg_description_cmd; |
| 222 | extern struct cmd_element cfg_no_description_cmd; |
| 223 | |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * signal handling |
| 227 | */ |
| 228 | enum signal_vty { |
| 229 | S_VTY_EVENT, |
| 230 | }; |
| 231 | |
| 232 | struct vty_signal_data { |
| 233 | enum event event; |
| 234 | int sock; |
| 235 | struct vty *vty; |
| 236 | }; |
| 237 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 238 | /*! @} */ |