libosmovty  0.12.0
Osmocom VTY library
vty.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdio.h>
4 #include <stdarg.h>
5 
6 #include <osmocom/core/linuxlist.h>
7 
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 
30 enum event {
36 #ifdef VTYSH
37  VTYSH_SERV,
38  VTYSH_READ,
39  VTYSH_WRITE
40 #endif /* VTYSH */
41 };
42 
43 enum vty_type {
48 };
49 
51  struct llist_head entry;
52 
54  void *priv;
55 
57  int node;
58 
61  char *indent;
62 };
63 
65 struct vty {
67  FILE *file;
68 
70  void *priv;
71 
73  int fd;
74 
76  enum vty_type type;
77 
79  int node;
80 
82  int fail;
83 
85  struct buffer *obuf;
86 
88  char *buf;
89 
91  int cp;
92 
94  int length;
95 
97  int max;
98 
101 
103  int hp;
104 
106  int hindex;
107 
110  void *index;
111 
113  void *index_sub;
114 
116  unsigned char escape;
117 
120 
126  unsigned char iac;
127 
129  unsigned char iac_sb_in_progress;
130  /* At the moment, we care only about the NAWS (window size) negotiation,
131  * and that requires just a 5-character buffer (RFC 1073):
132  * <NAWS char> <16-bit width> <16-bit height> */
133 #define TELNET_NAWS_SB_LEN 5
134 
135  unsigned char sb_buf[TELNET_NAWS_SB_LEN];
139  size_t sb_len;
140 
142  int width;
144  int height;
145 
147  int lines;
148 
149  int monitor;
150 
152  int config;
153 
155  struct llist_head parent_nodes;
156 
159  char *indent;
160 };
161 
162 /* Small macro to determine newline is newline only or linefeed needed. */
163 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
164 
165 static inline const char *vty_newline(struct vty *vty)
166 {
167  return VTY_NEWLINE;
168 }
169 
171 struct vty_app_info {
173  const char *name;
175  const char *version;
177  const char *copyright;
179  void *tall_ctx;
181  int (*go_parent_cb)(struct vty *vty);
183  int (*is_config_node)(struct vty *vty, int node);
185  int (*config_is_consistent)(struct vty *vty);
186 };
187 
188 /* Prototypes. */
189 void vty_init(struct vty_app_info *app_info);
190 int vty_read_config_file(const char *file_name, void *priv);
191 void vty_init_vtysh (void);
192 void vty_reset (void);
193 struct vty *vty_new (void);
194 struct vty *vty_create (int vty_sock, void *priv);
195 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
196 int vty_out_newline(struct vty *);
197 int vty_read(struct vty *vty);
198 //void vty_time_print (struct vty *, int);
199 void vty_close (struct vty *);
200 char *vty_get_cwd (void);
201 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
202 int vty_config_lock (struct vty *);
203 int vty_config_unlock (struct vty *);
204 int vty_shell (struct vty *);
205 int vty_shell_serv (struct vty *);
206 void vty_hello (struct vty *);
207 void *vty_current_index(struct vty *);
208 int vty_current_node(struct vty *vty);
209 int vty_go_parent(struct vty *vty);
210 
211 /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
212 const char *vty_get_bind_addr(void);
213 
214 extern void *tall_vty_ctx;
215 
216 extern struct cmd_element cfg_description_cmd;
217 extern struct cmd_element cfg_no_description_cmd;
218 
219 
225 };
226 
228  enum event event;
229  int sock;
230  struct vty *vty;
231 };
232 
vty_go_parent
int vty_go_parent(struct vty *vty)
Definition: command.c:2147
vty::index
void * index
For current referencing point of interface, route-map, access-list etc...
Definition: vty.h:110
vty::sb_len
size_t sb_len
How many subnegotiation characters have we received?
Definition: vty.h:139
vty_shell
int vty_shell(struct vty *)
Return if this VTY is a shell or not.
Definition: vty.c:248
vty_app_info::tall_ctx
void * tall_ctx
talloc context
Definition: vty.h:179
vty::length
int length
Command length.
Definition: vty.h:94
vty::indent
char * indent
When reading from a config file, these are the indenting characters expected for children of the curr...
Definition: vty.h:159
vty_app_info::name
const char * name
name of the application
Definition: vty.h:173
VTY_CLOSED
@ VTY_CLOSED
Definition: vty.h:34
vty::hist
char * hist[VTY_MAXHIST]
Histry of command.
Definition: vty.h:100
vty_signal_data::event
enum event event
Definition: vty.h:228
vty::iac_sb_in_progress
unsigned char iac_sb_in_progress
IAC SB (option subnegotiation) handling.
Definition: vty.h:129
signal_vty
signal_vty
signal handling
Definition: vty.h:223
VTY_NEWLINE
#define VTY_NEWLINE
Definition: vty.h:163
cmd_element
Structure of a command element.
Definition: command.h:141
vty::cp
int cp
Command cursor point.
Definition: vty.h:91
vty_get_bind_addr
const char * vty_get_bind_addr(void)
Definition: vty.c:1627
vty_config_unlock
int vty_config_unlock(struct vty *)
Unlock the configuration from a given VTY.
Definition: vty.c:353
vty_new
struct vty * vty_new(void)
Allocate a new vty interface structure.
Definition: vty.c:112
vty::lines
int lines
Configure lines.
Definition: vty.h:147
vty_type
vty_type
Definition: vty.h:43
vty_init_vtysh
void vty_init_vtysh(void)
Definition: vty.c:1769
VTY_WRITE
@ VTY_WRITE
Definition: vty.h:33
vty::fd
int fd
File descripter of this vty.
Definition: vty.h:73
VTY_FILE
@ VTY_FILE
Definition: vty.h:45
vty::priv
void * priv
private data, specified by creator
Definition: vty.h:70
vty::max
int max
Command max length.
Definition: vty.h:97
cfg_no_description_cmd
struct cmd_element cfg_no_description_cmd
vty_parent_node::node
int node
Node status of this vty.
Definition: vty.h:57
vty_log
void vty_log(const char *level, const char *proto, const char *fmt, va_list)
vty_app_info::version
const char * version
version string of the application
Definition: vty.h:175
vty_parent_node::priv
void * priv
private data, specified by creator
Definition: vty.h:54
vty_parent_node::entry
struct llist_head entry
Definition: vty.h:51
vty_signal_data
Definition: vty.h:227
vty::width
int width
Window width.
Definition: vty.h:142
vty_close
void vty_close(struct vty *)
Close a given vty interface.
Definition: vty.c:207
VTY_SERV
@ VTY_SERV
Definition: vty.h:31
vty::sb_buf
unsigned char sb_buf[TELNET_NAWS_SB_LEN]
sub-negotiation buffer
Definition: vty.h:135
vty
Internal representation of a single VTY.
Definition: vty.h:65
vty::type
enum vty_type type
Is this vty connect to file or not.
Definition: vty.h:76
vty::hindex
int hindex
History insert end point.
Definition: vty.h:106
VTY_PRINTF_ATTRIBUTE
#define VTY_PRINTF_ATTRIBUTE(a, b)
Definition: vty.h:16
TELNET_NAWS_SB_LEN
#define TELNET_NAWS_SB_LEN
Definition: vty.h:133
vty_current_index
void * vty_current_index(struct vty *)
return the current index of a given VTY
Definition: vty.c:323
VTY_TIMEOUT_RESET
@ VTY_TIMEOUT_RESET
Definition: vty.h:35
vty_reset
void vty_reset(void)
Reset all VTY status.
Definition: vty.c:1719
vty::buf
char * buf
Command input buffer.
Definition: vty.h:88
vty_app_info::is_config_node
int(* is_config_node)(struct vty *vty, int node)
call-back to determine if node is config node
Definition: vty.h:183
vty::node
int node
Node status of this vty.
Definition: vty.h:79
S_VTY_EVENT
@ S_VTY_EVENT
Definition: vty.h:224
vty_out
int vty_out(struct vty *, const char *,...) VTY_PRINTF_ATTRIBUTE(2
vty_app_info::go_parent_cb
int(* go_parent_cb)(struct vty *vty)
call-back for returning to parent n ode
Definition: vty.h:181
vty_signal_data::sock
int sock
Definition: vty.h:229
buffer
Definition: buffer.c:39
vty_app_info::config_is_consistent
int(* config_is_consistent)(struct vty *vty)
Check if the config is consistent before write.
Definition: vty.h:185
vty_read_config_file
int vty_read_config_file(const char *file_name, void *priv)
Read the configuration file using the VTY code.
Definition: vty.c:1816
vty::parent_nodes
struct llist_head parent_nodes
List of parent nodes, last item is the outermost parent.
Definition: vty.h:155
vty::fail
int fail
Failure count.
Definition: vty.h:82
vty_parent_node::indent
char * indent
When reading from a config file, these are the indenting characters expected for children of this VTY...
Definition: vty.h:61
vty_newline
static const char * vty_newline(struct vty *vty)
Definition: vty.h:165
node
struct cmd_node * node
Definition: command.c:2944
vty_hello
void vty_hello(struct vty *)
Definition: vty.c:363
vty::status
enum vty::@1 status
Current vty status.
vty::VTY_MORE
@ VTY_MORE
Definition: vty.h:119
vty_current_node
int vty_current_node(struct vty *vty)
return the current node of a given VTY
Definition: vty.c:329
vty::escape
unsigned char escape
For escape character.
Definition: vty.h:116
vty_signal_data::vty
struct vty * vty
Definition: vty.h:230
vty::monitor
int monitor
Definition: vty.h:149
vty_parent_node
Definition: vty.h:50
VTY_SHELL
@ VTY_SHELL
Definition: vty.h:46
vty::obuf
struct buffer * obuf
Output buffer.
Definition: vty.h:85
vty::index_sub
void * index_sub
For multiple level index treatment such as key chain and key.
Definition: vty.h:113
VTY_SHELL_SERV
@ VTY_SHELL_SERV
Definition: vty.h:47
vty_create
struct vty * vty_create(int vty_sock, void *priv)
Create new vty structure.
Definition: vty.c:1510
vty::config
int config
In configure mode.
Definition: vty.h:152
cfg_description_cmd
struct cmd_element cfg_description_cmd
tall_vty_ctx
void * tall_vty_ctx
Definition: vty.c:104
VTY_READ
@ VTY_READ
Definition: vty.h:32
vty::iac
unsigned char iac
IAC handling.
Definition: vty.h:126
vty::file
FILE * file
underlying file (if any)
Definition: vty.h:67
VTY_MAXHIST
#define VTY_MAXHIST
Definition: vty.h:27
vty_config_lock
int vty_config_lock(struct vty *)
Lock the configuration to a given VTY.
Definition: vty.c:340
vty::hp
int hp
History lookup current point.
Definition: vty.h:103
vty::VTY_NORMAL
@ VTY_NORMAL
Definition: vty.h:119
vty_get_cwd
char * vty_get_cwd(void)
Definition: vty.c:1759
vty::height
int height
Widnow height.
Definition: vty.h:144
vty::VTY_MORELINE
@ VTY_MORELINE
Definition: vty.h:119
vty_shell_serv
int vty_shell_serv(struct vty *)
Definition: vty.c:1764
vty::VTY_CLOSE
@ VTY_CLOSE
Definition: vty.h:119
vty_read
int vty_read(struct vty *vty)
Read data via vty socket.
Definition: vty.c:1283
VTY_TERM
@ VTY_TERM
Definition: vty.h:44
vty_out_newline
int int vty_out_newline(struct vty *)
print a newline on the given VTY
Definition: vty.c:315
vty_init
void vty_init(struct vty_app_info *app_info)
Initialize VTY layer.
Definition: vty.c:1780
vty_app_info::copyright
const char * copyright
copyright string of the application
Definition: vty.h:177
vty_app_info
Information an application registers with the VTY.
Definition: vty.h:171