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