blob: 544e1fa090653542dd0a4560c542cc34e4753b23 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welte3fb0b6f2010-05-19 19:02:52 +02002
3#include <stdio.h>
4#include <stdarg.h>
5
Harald Welte7acb30c2011-08-17 17:13:48 +02006/*! \defgroup vty VTY (Virtual TTY) interface
7 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02008 * \file vty.h */
Harald Welte7acb30c2011-08-17 17:13:48 +02009
Harald Welte3fb0b6f2010-05-19 19:02:52 +020010/* 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 Hofmeyr87e45502017-06-20 00:17:59 +020027/*! VTY events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020028enum 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 Eversberg3c6a2ce2012-07-12 09:22:56 +020041enum vty_type {
42 VTY_TERM,
43 VTY_FILE,
44 VTY_SHELL,
45 VTY_SHELL_SERV
46};
47
Harald Welte7acb30c2011-08-17 17:13:48 +020048/*! Internal representation of a single VTY */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020049struct vty {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 /*! underlying file (if any) */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020051 FILE *file;
52
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053 /*! private data, specified by creator */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020054 void *priv;
55
Neels Hofmeyr87e45502017-06-20 00:17:59 +020056 /*! File descripter of this vty. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020057 int fd;
58
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059 /*! Is this vty connect to file or not */
Andreas Eversberg3c6a2ce2012-07-12 09:22:56 +020060 enum vty_type type;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020061
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062 /*! Node status of this vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020063 int node;
64
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065 /*! Failure count */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020066 int fail;
67
Neels Hofmeyr87e45502017-06-20 00:17:59 +020068 /*! Output buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020069 struct buffer *obuf;
70
Neels Hofmeyr87e45502017-06-20 00:17:59 +020071 /*! Command input buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020072 char *buf;
73
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074 /*! Command cursor point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020075 int cp;
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077 /*! Command length */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020078 int length;
79
Neels Hofmeyr87e45502017-06-20 00:17:59 +020080 /*! Command max length. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020081 int max;
82
Neels Hofmeyr87e45502017-06-20 00:17:59 +020083 /*! Histry of command */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020084 char *hist[VTY_MAXHIST];
85
Neels Hofmeyr87e45502017-06-20 00:17:59 +020086 /*! History lookup current point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020087 int hp;
88
Neels Hofmeyr87e45502017-06-20 00:17:59 +020089 /*! History insert end point */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020090 int hindex;
91
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092 /*! For current referencing point of interface, route-map,
Harald Welte3fb0b6f2010-05-19 19:02:52 +020093 access-list etc... */
94 void *index;
95
Neels Hofmeyr87e45502017-06-20 00:17:59 +020096 /*! For multiple level index treatment such as key chain and key. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020097 void *index_sub;
98
Neels Hofmeyr87e45502017-06-20 00:17:59 +020099 /*! For escape character. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200100 unsigned char escape;
101
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200102 /*! Current vty status. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200103 enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
104
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200105 /*! IAC handling
Harald Welte7acb30c2011-08-17 17:13:48 +0200106 *
107 * IAC handling: was the last character received the IAC
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200108 * (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 Hofmeyr87e45502017-06-20 00:17:59 +0200112 /*! IAC SB (option subnegotiation) handling */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200113 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 Hofmeyr87e45502017-06-20 00:17:59 +0200118 /*! sub-negotiation buffer */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200119 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200120 /*! How many subnegotiation characters have we received?
Harald Welte7acb30c2011-08-17 17:13:48 +0200121 *
122 * We just drop those that do not fit in the buffer. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200123 size_t sb_len;
124
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200125 /*! Window width */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200126 int width;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200127 /*! Widnow height */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200128 int height;
129
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200130 /*! Configure lines. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200131 int lines;
132
133 int monitor;
134
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200135 /*! In configure mode. */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200136 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 Eversberg3c6a2ce2012-07-12 09:22:56 +0200142static inline const char *vty_newline(struct vty *vty)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200143{
144 return VTY_NEWLINE;
145}
146
Harald Welte7acb30c2011-08-17 17:13:48 +0200147/*! Information an application registers with the VTY */
Harald Welte237f6242010-05-25 23:00:45 +0200148struct vty_app_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200149 /*! name of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800150 const char *name;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151 /*! version string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800152 const char *version;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153 /*! copyright string of the application */
Holger Hans Peter Freytherdd195272010-06-08 16:12:58 +0800154 const char *copyright;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200155 /*! \ref talloc context */
Harald Welte237f6242010-05-25 23:00:45 +0200156 void *tall_ctx;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200157 /*! call-back for returning to parent n ode */
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000158 int (*go_parent_cb)(struct vty *vty);
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200159 /*! call-back to determine if node is config node */
Holger Hans Peter Freyther8f09f012010-08-25 17:34:56 +0800160 int (*is_config_node)(struct vty *vty, int node);
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161 /*! Check if the config is consistent before write */
Holger Hans Peter Freyther9f0f9782014-11-21 10:40:07 +0100162 int (*config_is_consistent)(struct vty *vty);
Harald Welte237f6242010-05-25 23:00:45 +0200163};
164
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200165/* Prototypes. */
Harald Welte237f6242010-05-25 23:00:45 +0200166void vty_init(struct vty_app_info *app_info);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200167int vty_read_config_file(const char *file_name, void *priv);
168void vty_init_vtysh (void);
169void vty_reset (void);
170struct vty *vty_new (void);
171struct vty *vty_create (int vty_sock, void *priv);
172int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
173int vty_out_newline(struct vty *);
174int vty_read(struct vty *vty);
175//void vty_time_print (struct vty *, int);
176void vty_close (struct vty *);
177char *vty_get_cwd (void);
178void vty_log (const char *level, const char *proto, const char *fmt, va_list);
179int vty_config_lock (struct vty *);
180int vty_config_unlock (struct vty *);
181int vty_shell (struct vty *);
182int vty_shell_serv (struct vty *);
183void vty_hello (struct vty *);
Holger Hans Peter Freyther08aaded2010-09-14 02:24:03 +0800184void *vty_current_index(struct vty *);
185int vty_current_node(struct vty *vty);
Holger Hans Peter Freythera9e52522015-08-02 02:14:07 +0000186int vty_go_parent(struct vty *vty);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200187
Neels Hofmeyr96172f02016-02-23 14:01:41 +0100188/* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
189const char *vty_get_bind_addr(void);
190
Holger Hans Peter Freyther4aaccd72010-08-31 17:09:44 +0800191extern void *tall_vty_ctx;
Harald Welte7acb30c2011-08-17 17:13:48 +0200192
Harald Welted61d5172011-09-04 22:56:10 +0200193extern struct cmd_element cfg_description_cmd;
194extern struct cmd_element cfg_no_description_cmd;
195
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200196
197/**
198 * signal handling
199 */
200enum signal_vty {
201 S_VTY_EVENT,
202};
203
204struct vty_signal_data {
205 enum event event;
206 int sock;
207 struct vty *vty;
208};
209
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200210/*! @} */