Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1 | #ifndef _VTY_H |
| 2 | #define _VTY_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdarg.h> |
| 6 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 7 | /*! \defgroup vty VTY (Virtual TTY) interface |
| 8 | * @{ |
| 9 | */ |
| 10 | /*! \file vty.h */ |
| 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 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 29 | /*! \brief 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 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 50 | /*! Internal representation of a single VTY */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 51 | struct vty { |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 52 | /*! \brief underlying file (if any) */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 53 | FILE *file; |
| 54 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 55 | /*! \brief private data, specified by creator */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 56 | void *priv; |
| 57 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 58 | /*! \brief File descripter of this vty. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 59 | int fd; |
| 60 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 61 | /*! \brief Is this vty connect to file or not */ |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 62 | enum vty_type type; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 63 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 64 | /*! \brief Node status of this vty */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 65 | int node; |
| 66 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 67 | /*! \brief Failure count */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 68 | int fail; |
| 69 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 70 | /*! \brief Output buffer. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 71 | struct buffer *obuf; |
| 72 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 73 | /*! \brief Command input buffer */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 74 | char *buf; |
| 75 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 76 | /*! \brief Command cursor point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 77 | int cp; |
| 78 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 79 | /*! \brief Command length */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 80 | int length; |
| 81 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 82 | /*! \brief Command max length. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 83 | int max; |
| 84 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 85 | /*! \brief Histry of command */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 86 | char *hist[VTY_MAXHIST]; |
| 87 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 88 | /*! \brief History lookup current point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 89 | int hp; |
| 90 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 91 | /*! \brief History insert end point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 92 | int hindex; |
| 93 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 94 | /*! \brief For current referencing point of interface, route-map, |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 95 | access-list etc... */ |
| 96 | void *index; |
| 97 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 98 | /*! \brief For multiple level index treatment such as key chain and key. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 99 | void *index_sub; |
| 100 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 101 | /*! \brief For escape character. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 102 | unsigned char escape; |
| 103 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 104 | /*! \brief Current vty status. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 105 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; |
| 106 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 107 | /*! \brief IAC handling |
| 108 | * |
| 109 | * IAC handling: was the last character received the IAC |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 110 | * (interpret-as-command) escape character (and therefore the next |
| 111 | * character will be the command code)? Refer to Telnet RFC 854. */ |
| 112 | unsigned char iac; |
| 113 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 114 | /*! \brief IAC SB (option subnegotiation) handling */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 115 | unsigned char iac_sb_in_progress; |
| 116 | /* At the moment, we care only about the NAWS (window size) negotiation, |
| 117 | * and that requires just a 5-character buffer (RFC 1073): |
| 118 | * <NAWS char> <16-bit width> <16-bit height> */ |
| 119 | #define TELNET_NAWS_SB_LEN 5 |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 120 | /*! \brief sub-negotiation buffer */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 121 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 122 | /*! \brief How many subnegotiation characters have we received? |
| 123 | * |
| 124 | * We just drop those that do not fit in the buffer. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 125 | size_t sb_len; |
| 126 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 127 | /*! \brief Window width */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 128 | int width; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 129 | /*! \brief Widnow height */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 130 | int height; |
| 131 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 132 | /*! \brief Configure lines. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 133 | int lines; |
| 134 | |
| 135 | int monitor; |
| 136 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 137 | /*! \brief In configure mode. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 138 | int config; |
| 139 | }; |
| 140 | |
| 141 | /* Small macro to determine newline is newline only or linefeed needed. */ |
| 142 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") |
| 143 | |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 144 | static inline const char *vty_newline(struct vty *vty) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 145 | { |
| 146 | return VTY_NEWLINE; |
| 147 | } |
| 148 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 149 | /*! Information an application registers with the VTY */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 150 | struct vty_app_info { |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 151 | /*! \brief name of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 152 | const char *name; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 153 | /*! \brief version string of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 154 | const char *version; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 155 | /*! \brief copyright string of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 156 | const char *copyright; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 157 | /*! \brief \ref talloc context */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 158 | void *tall_ctx; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 159 | /*! \brief call-back for returning to parent n ode */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 160 | enum node_type (*go_parent_cb)(struct vty *vty); |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 161 | /*! \brief call-back to determine if node is config node */ |
Holger Hans Peter Freyther | 8f09f01 | 2010-08-25 17:34:56 +0800 | [diff] [blame] | 162 | int (*is_config_node)(struct vty *vty, int node); |
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); |
Andreas.Eversberg | f948dbc | 2011-11-10 23:09:35 +0100 | [diff] [blame] | 186 | enum node_type vty_go_parent(struct vty *vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 187 | |
Holger Hans Peter Freyther | 4aaccd7 | 2010-08-31 17:09:44 +0800 | [diff] [blame] | 188 | extern void *tall_vty_ctx; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 189 | |
Harald Welte | d61d517 | 2011-09-04 22:56:10 +0200 | [diff] [blame] | 190 | extern struct cmd_element cfg_description_cmd; |
| 191 | extern struct cmd_element cfg_no_description_cmd; |
| 192 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 193 | /*! @} */ |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 194 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 195 | #endif |