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__ |
| 9 | #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) |
| 10 | #else |
| 11 | #define PRINTF_ATTRIBUTE(a,b) |
| 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 | |
| 24 | struct vty { |
| 25 | FILE *file; |
| 26 | |
| 27 | /* File descripter of this vty. */ |
| 28 | int fd; |
| 29 | |
| 30 | /* Is this vty connect to file or not */ |
| 31 | enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type; |
| 32 | |
| 33 | /* Node status of this vty */ |
| 34 | int node; |
| 35 | |
| 36 | /* Failure count */ |
| 37 | int fail; |
| 38 | |
| 39 | /* Output buffer. */ |
| 40 | struct buffer *obuf; |
| 41 | |
| 42 | /* Command input buffer */ |
| 43 | char *buf; |
| 44 | |
| 45 | /* Command cursor point */ |
| 46 | int cp; |
| 47 | |
| 48 | /* Command length */ |
| 49 | int length; |
| 50 | |
| 51 | /* Command max length. */ |
| 52 | int max; |
| 53 | |
| 54 | /* Histry of command */ |
| 55 | char *hist[VTY_MAXHIST]; |
| 56 | |
| 57 | /* History lookup current point */ |
| 58 | int hp; |
| 59 | |
| 60 | /* History insert end point */ |
| 61 | int hindex; |
| 62 | |
| 63 | /* For current referencing point of interface, route-map, |
| 64 | access-list etc... */ |
| 65 | void *index; |
| 66 | |
| 67 | /* For multiple level index treatment such as key chain and key. */ |
| 68 | void *index_sub; |
| 69 | |
| 70 | /* For escape character. */ |
| 71 | unsigned char escape; |
| 72 | |
| 73 | /* Current vty status. */ |
| 74 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; |
| 75 | |
| 76 | /* IAC handling: was the last character received the IAC |
| 77 | * (interpret-as-command) escape character (and therefore the next |
| 78 | * character will be the command code)? Refer to Telnet RFC 854. */ |
| 79 | unsigned char iac; |
| 80 | |
| 81 | /* IAC SB (option subnegotiation) handling */ |
| 82 | unsigned char iac_sb_in_progress; |
| 83 | /* At the moment, we care only about the NAWS (window size) negotiation, |
| 84 | * and that requires just a 5-character buffer (RFC 1073): |
| 85 | * <NAWS char> <16-bit width> <16-bit height> */ |
| 86 | #define TELNET_NAWS_SB_LEN 5 |
| 87 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
| 88 | /* How many subnegotiation characters have we received? We just drop |
| 89 | * those that do not fit in the buffer. */ |
| 90 | size_t sb_len; |
| 91 | |
| 92 | /* Window width/height. */ |
| 93 | int width; |
| 94 | int height; |
| 95 | |
| 96 | /* Configure lines. */ |
| 97 | int lines; |
| 98 | |
| 99 | int monitor; |
| 100 | |
| 101 | /* In configure mode. */ |
| 102 | int config; |
| 103 | }; |
| 104 | |
| 105 | /* Small macro to determine newline is newline only or linefeed needed. */ |
| 106 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") |
| 107 | |
| 108 | static inline char *vty_newline(struct vty *vty) |
| 109 | { |
| 110 | return VTY_NEWLINE; |
| 111 | } |
| 112 | |
| 113 | /* Prototypes. */ |
| 114 | void vty_init (void); |
| 115 | void vty_init_vtysh (void); |
| 116 | void vty_reset (void); |
| 117 | struct vty *vty_new (void); |
| 118 | struct vty *vty_create (int vty_sock); |
| 119 | int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); |
| 120 | int vty_out_newline(struct vty *); |
| 121 | int vty_read(struct vty *vty); |
| 122 | void vty_read_config (char *, char *); |
| 123 | void vty_time_print (struct vty *, int); |
| 124 | void vty_close (struct vty *); |
| 125 | char *vty_get_cwd (void); |
| 126 | void vty_log (const char *level, const char *proto, const char *fmt, va_list); |
| 127 | int vty_config_lock (struct vty *); |
| 128 | int vty_config_unlock (struct vty *); |
| 129 | int vty_shell (struct vty *); |
| 130 | int vty_shell_serv (struct vty *); |
| 131 | void vty_hello (struct vty *); |
| 132 | |
| 133 | |
| 134 | #endif |