Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 1 | #ifndef _VTY_H |
| 2 | #define _VTY_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdarg.h> |
| 6 | |
| 7 | /* GCC have printf type attribute check. */ |
| 8 | #ifdef __GNUC__ |
Holger Hans Peter Freyther | d2aba27 | 2009-08-10 07:52:50 +0200 | [diff] [blame] | 9 | #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 10 | #else |
Holger Hans Peter Freyther | d2aba27 | 2009-08-10 07:52:50 +0200 | [diff] [blame] | 11 | #define VTY_PRINTF_ATTRIBUTE(a,b) |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 12 | #endif /* __GNUC__ */ |
| 13 | |
| 14 | /* Does the I/O error indicate that the operation should be retried later? */ |
| 15 | #define ERRNO_IO_RETRY(EN) \ |
| 16 | (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR)) |
| 17 | |
| 18 | /* Vty read buffer size. */ |
| 19 | #define VTY_READ_BUFSIZ 512 |
| 20 | |
| 21 | #define VTY_BUFSIZ 512 |
| 22 | #define VTY_MAXHIST 20 |
| 23 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 24 | /* Vty events */ |
| 25 | enum event { |
| 26 | VTY_SERV, |
| 27 | VTY_READ, |
| 28 | VTY_WRITE, |
Harald Welte | 2477d93 | 2009-08-07 00:32:41 +0200 | [diff] [blame] | 29 | VTY_CLOSED, |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 30 | VTY_TIMEOUT_RESET, |
| 31 | #ifdef VTYSH |
| 32 | VTYSH_SERV, |
| 33 | VTYSH_READ, |
| 34 | VTYSH_WRITE |
| 35 | #endif /* VTYSH */ |
| 36 | }; |
| 37 | |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 38 | struct vty { |
| 39 | FILE *file; |
| 40 | |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 41 | /* private data, specified by creator */ |
| 42 | void *priv; |
| 43 | |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 44 | /* File descripter of this vty. */ |
| 45 | int fd; |
| 46 | |
| 47 | /* Is this vty connect to file or not */ |
| 48 | enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type; |
| 49 | |
| 50 | /* Node status of this vty */ |
| 51 | int node; |
| 52 | |
| 53 | /* Failure count */ |
| 54 | int fail; |
| 55 | |
| 56 | /* Output buffer. */ |
| 57 | struct buffer *obuf; |
| 58 | |
| 59 | /* Command input buffer */ |
| 60 | char *buf; |
| 61 | |
| 62 | /* Command cursor point */ |
| 63 | int cp; |
| 64 | |
| 65 | /* Command length */ |
| 66 | int length; |
| 67 | |
| 68 | /* Command max length. */ |
| 69 | int max; |
| 70 | |
| 71 | /* Histry of command */ |
| 72 | char *hist[VTY_MAXHIST]; |
| 73 | |
| 74 | /* History lookup current point */ |
| 75 | int hp; |
| 76 | |
| 77 | /* History insert end point */ |
| 78 | int hindex; |
| 79 | |
| 80 | /* For current referencing point of interface, route-map, |
| 81 | access-list etc... */ |
| 82 | void *index; |
| 83 | |
| 84 | /* For multiple level index treatment such as key chain and key. */ |
| 85 | void *index_sub; |
| 86 | |
| 87 | /* For escape character. */ |
| 88 | unsigned char escape; |
| 89 | |
| 90 | /* Current vty status. */ |
| 91 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; |
| 92 | |
| 93 | /* IAC handling: was the last character received the IAC |
| 94 | * (interpret-as-command) escape character (and therefore the next |
| 95 | * character will be the command code)? Refer to Telnet RFC 854. */ |
| 96 | unsigned char iac; |
| 97 | |
| 98 | /* IAC SB (option subnegotiation) handling */ |
| 99 | unsigned char iac_sb_in_progress; |
| 100 | /* At the moment, we care only about the NAWS (window size) negotiation, |
| 101 | * and that requires just a 5-character buffer (RFC 1073): |
| 102 | * <NAWS char> <16-bit width> <16-bit height> */ |
| 103 | #define TELNET_NAWS_SB_LEN 5 |
| 104 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
| 105 | /* How many subnegotiation characters have we received? We just drop |
| 106 | * those that do not fit in the buffer. */ |
| 107 | size_t sb_len; |
| 108 | |
| 109 | /* Window width/height. */ |
| 110 | int width; |
| 111 | int height; |
| 112 | |
| 113 | /* Configure lines. */ |
| 114 | int lines; |
| 115 | |
| 116 | int monitor; |
| 117 | |
| 118 | /* In configure mode. */ |
| 119 | int config; |
| 120 | }; |
| 121 | |
| 122 | /* Small macro to determine newline is newline only or linefeed needed. */ |
| 123 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") |
| 124 | |
| 125 | static inline char *vty_newline(struct vty *vty) |
| 126 | { |
| 127 | return VTY_NEWLINE; |
| 128 | } |
| 129 | |
| 130 | /* Prototypes. */ |
| 131 | void vty_init (void); |
Harald Welte | c7c1982 | 2009-08-07 13:28:08 +0200 | [diff] [blame] | 132 | int vty_read_config_file(const char *file_name); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 133 | void vty_init_vtysh (void); |
| 134 | void vty_reset (void); |
| 135 | struct vty *vty_new (void); |
Harald Welte | c63e51d | 2009-03-10 19:46:16 +0000 | [diff] [blame] | 136 | struct vty *vty_create (int vty_sock, void *priv); |
Holger Hans Peter Freyther | d2aba27 | 2009-08-10 07:52:50 +0200 | [diff] [blame] | 137 | int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 138 | int vty_out_newline(struct vty *); |
| 139 | int vty_read(struct vty *vty); |
Harald Welte | c7c1982 | 2009-08-07 13:28:08 +0200 | [diff] [blame] | 140 | //void vty_time_print (struct vty *, int); |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 141 | void vty_close (struct vty *); |
| 142 | char *vty_get_cwd (void); |
| 143 | void vty_log (const char *level, const char *proto, const char *fmt, va_list); |
| 144 | int vty_config_lock (struct vty *); |
| 145 | int vty_config_unlock (struct vty *); |
| 146 | int vty_shell (struct vty *); |
| 147 | int vty_shell_serv (struct vty *); |
| 148 | void vty_hello (struct vty *); |
| 149 | |
Harald Welte | 0224e4d | 2009-08-07 13:25:41 +0200 | [diff] [blame] | 150 | void *tall_vty_ctx; |
Harald Welte | 955049f | 2009-03-10 12:16:51 +0000 | [diff] [blame] | 151 | #endif |