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 
100  char *hist[VTY_MAXHIST];
101 
103  int hp;
104 
106  int hindex;
107 
110  void *index;
111 
113  void *index_sub;
114 
116  unsigned char escape;
117 
119  enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
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 
void vty_init_vtysh(void)
Definition: vty.c:1764
Definition: vty.h:33
int vty_shell_serv(struct vty *)
Definition: vty.c:1759
int length
Command length.
Definition: vty.h:94
Definition: vty.h:47
void * tall_ctx
talloc context
Definition: vty.h:179
int node
Node status of this vty.
Definition: vty.h:57
struct cmd_element cfg_description_cmd
char * indent
When reading from a config file, these are the indenting characters expected for children of the curr...
Definition: vty.h:159
const char * name
name of the application
Definition: vty.h:173
int lines
Configure lines.
Definition: vty.h:147
FILE * file
underlying file (if any)
Definition: vty.h:67
size_t sb_len
How many subnegotiation characters have we received?
Definition: vty.h:139
int vty_read(struct vty *vty)
Read data via vty socket.
Definition: vty.c:1278
int vty_config_unlock(struct vty *)
Unlock the configuration from a given VTY.
Definition: vty.c:348
int vty_config_lock(struct vty *)
Lock the configuration to a given VTY.
Definition: vty.c:335
int hindex
History insert end point.
Definition: vty.h:106
Internal representation of a single VTY.
Definition: vty.h:65
Definition: vty.h:227
int height
Widnow height.
Definition: vty.h:144
void vty_log(const char *level, const char *proto, const char *fmt, va_list)
Definition: vty.h:224
char * indent
When reading from a config file, these are the indenting characters expected for children of this VTY...
Definition: vty.h:61
void vty_close(struct vty *)
Close a given vty interface.
Definition: vty.c:202
int max
Command max length.
Definition: vty.h:97
int width
Window width.
Definition: vty.h:142
Definition: vty.h:32
const char * copyright
copyright string of the application
Definition: vty.h:177
char * buf
Command input buffer.
Definition: vty.h:88
int vty_out(struct vty *, const char *,...) VTY_PRINTF_ATTRIBUTE(2
unsigned char escape
For escape character.
Definition: vty.h:116
#define VTY_NEWLINE
Definition: vty.h:163
#define VTY_PRINTF_ATTRIBUTE(a, b)
Definition: vty.h:16
Definition: vty.h:46
int sock
Definition: vty.h:229
int fail
Failure count.
Definition: vty.h:82
void * vty_current_index(struct vty *)
return the current index of a given VTY
Definition: vty.c:318
int node
Node status of this vty.
Definition: vty.h:79
int config
In configure mode.
Definition: vty.h:152
void * priv
private data, specified by creator
Definition: vty.h:54
void vty_reset(void)
Reset all VTY status.
Definition: vty.c:1714
int vty_read_config_file(const char *file_name, void *priv)
Read the configuration file using the VTY code.
Definition: vty.c:1811
unsigned char iac
IAC handling.
Definition: vty.h:126
const char * version
version string of the application
Definition: vty.h:175
int fd
File descripter of this vty.
Definition: vty.h:73
static const char * vty_newline(struct vty *vty)
Definition: vty.h:165
void * tall_vty_ctx
Definition: vty.c:99
int vty_current_node(struct vty *vty)
return the current node of a given VTY
Definition: vty.c:324
void * priv
private data, specified by creator
Definition: vty.h:70
struct cmd_element cfg_no_description_cmd
struct buffer * obuf
Output buffer.
Definition: vty.h:85
struct llist_head entry
Definition: vty.h:51
int vty_shell(struct vty *)
Return if this VTY is a shell or not.
Definition: vty.c:243
Structure of a command element.
Definition: command.h:141
int int vty_out_newline(struct vty *)
print a newline on the given VTY
Definition: vty.c:310
void vty_init(struct vty_app_info *app_info)
Initialize VTY layer.
Definition: vty.c:1775
Definition: vty.h:35
#define TELNET_NAWS_SB_LEN
Definition: vty.h:133
Definition: vty.h:50
Definition: buffer.c:39
struct vty * vty
Definition: vty.h:230
event
VTY events.
Definition: vty.h:30
Definition: vty.h:44
Definition: vty.h:45
Definition: vty.h:119
int hp
History lookup current point.
Definition: vty.h:103
int monitor
Definition: vty.h:149
struct vty * vty_create(int vty_sock, void *priv)
Create new vty structure.
Definition: vty.c:1505
char * vty_get_cwd(void)
Definition: vty.c:1754
void * index_sub
For multiple level index treatment such as key chain and key.
Definition: vty.h:113
int cp
Command cursor point.
Definition: vty.h:91
void vty_hello(struct vty *)
Definition: vty.c:358
signal_vty
signal handling
Definition: vty.h:223
Definition: vty.h:31
unsigned char iac_sb_in_progress
IAC SB (option subnegotiation) handling.
Definition: vty.h:129
struct vty * vty_new(void)
Allocate a new vty interface structure.
Definition: vty.c:107
Information an application registers with the VTY.
Definition: vty.h:171
vty_type
Definition: vty.h:43
#define VTY_MAXHIST
Definition: vty.h:27
Definition: vty.h:34
int vty_go_parent(struct vty *vty)
Definition: command.c:2142
const char * vty_get_bind_addr(void)
Definition: vty.c:1622
void * index
For current referencing point of interface, route-map, access-list etc...
Definition: vty.h:110