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