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 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 6 | /*! \defgroup vty VTY (Virtual TTY) interface |
| 7 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 8 | * \file vty.h */ |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 9 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 10 | /* GCC have printf type attribute check. */ |
| 11 | #ifdef __GNUC__ |
| 12 | #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) |
| 13 | #else |
| 14 | #define VTY_PRINTF_ATTRIBUTE(a,b) |
| 15 | #endif /* __GNUC__ */ |
| 16 | |
| 17 | /* Does the I/O error indicate that the operation should be retried later? */ |
| 18 | #define ERRNO_IO_RETRY(EN) \ |
| 19 | (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR)) |
| 20 | |
| 21 | /* Vty read buffer size. */ |
| 22 | #define VTY_READ_BUFSIZ 512 |
| 23 | |
| 24 | #define VTY_BUFSIZ 512 |
| 25 | #define VTY_MAXHIST 20 |
| 26 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 27 | /*! VTY events */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 28 | enum event { |
| 29 | VTY_SERV, |
| 30 | VTY_READ, |
| 31 | VTY_WRITE, |
| 32 | VTY_CLOSED, |
| 33 | VTY_TIMEOUT_RESET, |
| 34 | #ifdef VTYSH |
| 35 | VTYSH_SERV, |
| 36 | VTYSH_READ, |
| 37 | VTYSH_WRITE |
| 38 | #endif /* VTYSH */ |
| 39 | }; |
| 40 | |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 41 | enum vty_type { |
| 42 | VTY_TERM, |
| 43 | VTY_FILE, |
| 44 | VTY_SHELL, |
| 45 | VTY_SHELL_SERV |
| 46 | }; |
| 47 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 48 | /*! Internal representation of a single VTY */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 49 | struct vty { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 50 | /*! underlying file (if any) */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 51 | FILE *file; |
| 52 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 53 | /*! private data, specified by creator */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 54 | void *priv; |
| 55 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 56 | /*! File descripter of this vty. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 57 | int fd; |
| 58 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 59 | /*! Is this vty connect to file or not */ |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 60 | enum vty_type type; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 61 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 62 | /*! Node status of this vty */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 63 | int node; |
| 64 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 65 | /*! Failure count */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 66 | int fail; |
| 67 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 68 | /*! Output buffer. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 69 | struct buffer *obuf; |
| 70 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 71 | /*! Command input buffer */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 72 | char *buf; |
| 73 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 74 | /*! Command cursor point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 75 | int cp; |
| 76 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 77 | /*! Command length */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 78 | int length; |
| 79 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 80 | /*! Command max length. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 81 | int max; |
| 82 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 83 | /*! Histry of command */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 84 | char *hist[VTY_MAXHIST]; |
| 85 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 86 | /*! History lookup current point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 87 | int hp; |
| 88 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 89 | /*! History insert end point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 90 | int hindex; |
| 91 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 92 | /*! For current referencing point of interface, route-map, |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 93 | access-list etc... */ |
| 94 | void *index; |
| 95 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 96 | /*! For multiple level index treatment such as key chain and key. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 97 | void *index_sub; |
| 98 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 99 | /*! For escape character. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 100 | unsigned char escape; |
| 101 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 102 | /*! Current vty status. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 103 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; |
| 104 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 105 | /*! IAC handling |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 106 | * |
| 107 | * IAC handling: was the last character received the IAC |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 108 | * (interpret-as-command) escape character (and therefore the next |
| 109 | * character will be the command code)? Refer to Telnet RFC 854. */ |
| 110 | unsigned char iac; |
| 111 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 112 | /*! IAC SB (option subnegotiation) handling */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 113 | unsigned char iac_sb_in_progress; |
| 114 | /* At the moment, we care only about the NAWS (window size) negotiation, |
| 115 | * and that requires just a 5-character buffer (RFC 1073): |
| 116 | * <NAWS char> <16-bit width> <16-bit height> */ |
| 117 | #define TELNET_NAWS_SB_LEN 5 |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 118 | /*! sub-negotiation buffer */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 119 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 120 | /*! How many subnegotiation characters have we received? |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 121 | * |
| 122 | * We just drop those that do not fit in the buffer. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 123 | size_t sb_len; |
| 124 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 125 | /*! Window width */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 126 | int width; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 127 | /*! Widnow height */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 128 | int height; |
| 129 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 130 | /*! Configure lines. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 131 | int lines; |
| 132 | |
| 133 | int monitor; |
| 134 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 135 | /*! In configure mode. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 136 | int config; |
| 137 | }; |
| 138 | |
| 139 | /* Small macro to determine newline is newline only or linefeed needed. */ |
| 140 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") |
| 141 | |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 142 | static inline const char *vty_newline(struct vty *vty) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 143 | { |
| 144 | return VTY_NEWLINE; |
| 145 | } |
| 146 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 147 | /*! Information an application registers with the VTY */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 148 | struct vty_app_info { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 149 | /*! name of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 150 | const char *name; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 151 | /*! version string of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 152 | const char *version; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 153 | /*! copyright string of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 154 | const char *copyright; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 155 | /*! \ref talloc context */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 156 | void *tall_ctx; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 157 | /*! call-back for returning to parent n ode */ |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 158 | int (*go_parent_cb)(struct vty *vty); |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 159 | /*! call-back to determine if node is config node */ |
Holger Hans Peter Freyther | 8f09f01 | 2010-08-25 17:34:56 +0800 | [diff] [blame] | 160 | int (*is_config_node)(struct vty *vty, int node); |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 161 | /*! Check if the config is consistent before write */ |
Holger Hans Peter Freyther | 9f0f978 | 2014-11-21 10:40:07 +0100 | [diff] [blame] | 162 | int (*config_is_consistent)(struct vty *vty); |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 163 | }; |
| 164 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 165 | /* Prototypes. */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 166 | void vty_init(struct vty_app_info *app_info); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 167 | int vty_read_config_file(const char *file_name, void *priv); |
| 168 | void vty_init_vtysh (void); |
| 169 | void vty_reset (void); |
| 170 | struct vty *vty_new (void); |
| 171 | struct vty *vty_create (int vty_sock, void *priv); |
| 172 | int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3); |
| 173 | int vty_out_newline(struct vty *); |
| 174 | int vty_read(struct vty *vty); |
| 175 | //void vty_time_print (struct vty *, int); |
| 176 | void vty_close (struct vty *); |
| 177 | char *vty_get_cwd (void); |
| 178 | void vty_log (const char *level, const char *proto, const char *fmt, va_list); |
| 179 | int vty_config_lock (struct vty *); |
| 180 | int vty_config_unlock (struct vty *); |
| 181 | int vty_shell (struct vty *); |
| 182 | int vty_shell_serv (struct vty *); |
| 183 | void vty_hello (struct vty *); |
Holger Hans Peter Freyther | 08aaded | 2010-09-14 02:24:03 +0800 | [diff] [blame] | 184 | void *vty_current_index(struct vty *); |
| 185 | int vty_current_node(struct vty *vty); |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 186 | int vty_go_parent(struct vty *vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 187 | |
Neels Hofmeyr | 96172f0 | 2016-02-23 14:01:41 +0100 | [diff] [blame] | 188 | /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */ |
| 189 | const char *vty_get_bind_addr(void); |
| 190 | |
Holger Hans Peter Freyther | 4aaccd7 | 2010-08-31 17:09:44 +0800 | [diff] [blame] | 191 | extern void *tall_vty_ctx; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 192 | |
Harald Welte | d61d517 | 2011-09-04 22:56:10 +0200 | [diff] [blame] | 193 | extern struct cmd_element cfg_description_cmd; |
| 194 | extern struct cmd_element cfg_no_description_cmd; |
| 195 | |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * signal handling |
| 199 | */ |
| 200 | enum signal_vty { |
| 201 | S_VTY_EVENT, |
| 202 | }; |
| 203 | |
| 204 | struct vty_signal_data { |
| 205 | enum event event; |
| 206 | int sock; |
| 207 | struct vty *vty; |
| 208 | }; |
| 209 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 210 | /*! @} */ |