libosmovty  1.5.1
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 #include <stdbool.h>
6 
8 #include <osmocom/core/defs.h>
9 
14 /* GCC have printf type attribute check. */
15 #ifdef __GNUC__
16 #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
17 #else
18 #define VTY_PRINTF_ATTRIBUTE(a,b)
19 #endif /* __GNUC__ */
20 
21 /* Does the I/O error indicate that the operation should be retried later? */
22 #define ERRNO_IO_RETRY(EN) \
23  (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
24 
25 /* Vty read buffer size. */
26 #define VTY_READ_BUFSIZ 512
27 
28 #define VTY_BUFSIZ 512
29 #define VTY_MAXHIST 20
30 
31 /* Number of application / library specific VTY attributes */
32 #define VTY_CMD_USR_ATTR_NUM 32
33 /* Flag characters reserved for global VTY attributes */
34 #define VTY_CMD_ATTR_FLAGS_RESERVED \
35  { '.', '!', '@', '^' }
36 
38 enum event {
44 #ifdef VTYSH
45  VTYSH_SERV,
46  VTYSH_READ,
47  VTYSH_WRITE
48 #endif /* VTYSH */
49 };
50 
51 enum vty_type {
56 };
57 
59  struct llist_head entry;
60 
62  void *priv;
63 
65  int node;
66 
69  char *indent;
70 };
71 
73 struct vty {
75  FILE *file;
76 
78  void *priv;
79 
81  int fd;
82 
84  enum vty_type type;
85 
87  int node;
88 
90  int fail;
91 
93  struct buffer *obuf;
94 
96  char *buf;
97 
99  int cp;
100 
102  int length;
103 
105  int max;
106 
109 
111  int hp;
112 
114  int hindex;
115 
118  void *index;
119 
121  void *index_sub;
122 
124  unsigned char escape;
125 
128 
134  unsigned char iac;
135 
137  unsigned char iac_sb_in_progress;
138  /* At the moment, we care only about the NAWS (window size) negotiation,
139  * and that requires just a 5-character buffer (RFC 1073):
140  * <NAWS char> <16-bit width> <16-bit height> */
141 #define TELNET_NAWS_SB_LEN 5
143  unsigned char sb_buf[TELNET_NAWS_SB_LEN];
147  size_t sb_len;
148 
150  int width;
152  int height;
153 
155  int lines;
156 
157  int monitor;
158 
160  int config;
161 
163  struct llist_head parent_nodes;
164 
167  char *indent;
168 
171 };
172 
173 /* Small macro to determine newline is newline only or linefeed needed. */
174 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
175 
176 static inline const char *vty_newline(struct vty *vty)
177 {
178  return VTY_NEWLINE;
179 }
180 
182 struct vty_app_info {
184  const char *name;
186  const char *version;
188  const char *copyright;
190  void *tall_ctx;
196  int (*go_parent_cb)(struct vty *vty);
199  int (*is_config_node)(struct vty *vty, int node)
200  OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is"
201  " no longer called, ever, and can be left NULL.");
203  int (*config_is_consistent)(struct vty *vty);
208 };
209 
210 /* Prototypes. */
211 void vty_init(struct vty_app_info *app_info);
212 int vty_read_config_file(const char *file_name, void *priv);
213 void vty_init_vtysh (void);
214 void vty_reset (void);
215 struct vty *vty_new (void);
216 struct vty *vty_create (int vty_sock, void *priv);
217 bool vty_is_active(struct vty *vty);
218 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
219 int vty_out_va(struct vty *vty, const char *format, va_list ap);
220 int vty_out_newline(struct vty *);
221 int vty_read(struct vty *vty);
222 //void vty_time_print (struct vty *, int);
223 void vty_close (struct vty *);
224 char *vty_get_cwd (void);
225 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
226 int vty_config_lock (struct vty *);
227 int vty_config_unlock (struct vty *);
228 int vty_shell (struct vty *);
229 int vty_shell_serv (struct vty *);
230 void vty_hello (struct vty *);
231 void *vty_current_index(struct vty *);
232 int vty_current_node(struct vty *vty);
233 int vty_go_parent(struct vty *vty);
234 
235 /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
236 const char *vty_get_bind_addr(void);
238 int vty_get_bind_port(int default_port);
239 
240 extern void *tall_vty_ctx;
241 
242 extern struct cmd_element cfg_description_cmd;
243 extern struct cmd_element cfg_no_description_cmd;
244 
245 
251 };
252 
254  enum event event;
255  int sock;
256  struct vty *vty;
257 };
258 
vty
Definition: tdef_vty.c:299
uint16_t node
#define OSMO_DEPRECATED(text)
int vty_go_parent(struct vty *vty)
Definition: command.c:2416
int vty_config_lock(struct vty *)
Lock the configuration to a given VTY.
Definition: vty.c:353
vty_type
Definition: vty.h:51
struct vty * vty_create(int vty_sock, void *priv)
Create new vty structure.
Definition: vty.c:1511
void vty_reset(void)
Reset all VTY status.
Definition: vty.c:1737
#define VTY_MAXHIST
Definition: vty.h:29
char * vty_get_cwd(void)
Definition: vty.c:1777
int vty_shell_serv(struct vty *)
Definition: vty.c:1782
#define VTY_NEWLINE
Definition: vty.h:174
#define VTY_CMD_USR_ATTR_NUM
Definition: vty.h:32
const char * vty_get_bind_addr(void)
Definition: vty.c:1630
#define VTY_PRINTF_ATTRIBUTE(a, b)
Definition: vty.h:18
struct vty * vty_new(void)
Allocate a new vty interface structure.
Definition: vty.c:115
struct cmd_element cfg_description_cmd
void vty_init_vtysh(void)
Definition: vty.c:1787
int vty_read(struct vty *vty)
Read data via vty socket.
Definition: vty.c:1278
int vty_read_config_file(const char *file_name, void *priv)
Read the configuration file using the VTY code.
Definition: vty.c:1864
void vty_log(const char *level, const char *proto, const char *fmt, va_list)
int vty_current_node(struct vty *vty)
return the current node of a given VTY
Definition: vty.c:342
int vty_shell(struct vty *)
Return if this VTY is a shell or not.
Definition: vty.c:254
bool vty_is_active(struct vty *vty)
Definition: telnet_interface.c:212
void vty_close(struct vty *)
Close a given vty interface.
Definition: vty.c:211
void vty_hello(struct vty *)
Definition: vty.c:376
int vty_get_bind_port(int default_port)
Returns configured port passed to the 'line vty'/'bind' command or default_port.
Definition: vty.c:1637
int vty_config_unlock(struct vty *)
Unlock the configuration from a given VTY.
Definition: vty.c:366
int vty_out_newline(struct vty *)
print a newline on the given VTY
Definition: vty.c:328
struct cmd_element cfg_no_description_cmd
int int vty_out_va(struct vty *vty, const char *format, va_list ap)
Definition: vty.c:259
static const char * vty_newline(struct vty *vty)
Definition: vty.h:176
void * vty_current_index(struct vty *)
return the current index of a given VTY
Definition: vty.c:336
int vty_out(struct vty *, const char *,...) VTY_PRINTF_ATTRIBUTE(2
void vty_init(struct vty_app_info *app_info)
Initialize VTY layer.
Definition: vty.c:1796
signal_vty
signal handling
Definition: vty.h:249
void * tall_vty_ctx
Definition: vty.c:107
@ VTY_FILE
Definition: vty.h:53
@ VTY_SHELL
Definition: vty.h:54
@ VTY_SHELL_SERV
Definition: vty.h:55
@ VTY_TERM
Definition: vty.h:52
@ VTY_WRITE
Definition: vty.h:41
@ VTY_SERV
Definition: vty.h:39
@ VTY_READ
Definition: vty.h:40
@ VTY_CLOSED
Definition: vty.h:42
@ VTY_TIMEOUT_RESET
Definition: vty.h:43
@ S_VTY_EVENT
Definition: vty.h:250
Definition: buffer.c:39
Structure of a command element.
Definition: command.h:168
Information an application registers with the VTY.
Definition: vty.h:182
int(* config_is_consistent)(struct vty *vty)
Check if the config is consistent before write.
Definition: vty.h:203
const char * version
version string of the application
Definition: vty.h:186
const char * name
name of the application
Definition: vty.h:184
char usr_attr_letters[VTY_CMD_USR_ATTR_NUM]
Flag letters of the application specific VTY attributes (optional).
Definition: vty.h:207
const char * copyright
copyright string of the application
Definition: vty.h:188
const char * usr_attr_desc[VTY_CMD_USR_ATTR_NUM]
Description of the application specific VTY attributes (optional).
Definition: vty.h:205
int(* go_parent_cb)(struct vty *vty)
Call-back for taking actions upon exiting a node.
Definition: vty.h:196
void * tall_ctx
talloc context
Definition: vty.h:190
int(* is_config_node)(struct vty *vty, int node) OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is" " no longer called
OBSOLETED: Implicit parent node tracking has replaced the use of this callback.
Definition: vty.h:199
Definition: vty.h:58
char * indent
When reading from a config file, these are the indenting characters expected for children of this VTY...
Definition: vty.h:69
struct llist_head entry
Definition: vty.h:59
int node
Node status of this vty.
Definition: vty.h:65
void * priv
private data, specified by creator
Definition: vty.h:62
Definition: vty.h:253
enum event event
Definition: vty.h:254
struct vty * vty
Definition: vty.h:256
int sock
Definition: vty.h:255
Internal representation of a single VTY.
Definition: vty.h:73
struct buffer * obuf
Output buffer.
Definition: vty.h:93
int fail
Failure count.
Definition: vty.h:90
int config
In configure mode.
Definition: vty.h:160
void * priv
private data, specified by creator
Definition: vty.h:78
int length
Command length.
Definition: vty.h:102
void * index_sub
For multiple level index treatment such as key chain and key.
Definition: vty.h:121
enum vty::@2 status
Current vty status.
char * hist[VTY_MAXHIST]
Histry of command.
Definition: vty.h:108
int width
Window width.
Definition: vty.h:150
unsigned char escape
For escape character.
Definition: vty.h:124
struct llist_head parent_nodes
List of parent nodes, last item is the outermost parent.
Definition: vty.h:163
char * indent
When reading from a config file, these are the indenting characters expected for children of the curr...
Definition: vty.h:167
int hp
History lookup current point.
Definition: vty.h:111
FILE * file
underlying file (if any)
Definition: vty.h:75
unsigned char iac
IAC handling.
Definition: vty.h:134
int height
Widnow height.
Definition: vty.h:152
size_t sb_len
How many subnegotiation characters have we received?
Definition: vty.h:147
void * index
For current referencing point of interface, route-map, access-list etc...
Definition: vty.h:118
unsigned char sb_buf[TELNET_NAWS_SB_LEN]
sub-negotiation buffer
Definition: vty.h:143
int node
Node status of this vty.
Definition: vty.h:87
char * buf
Command input buffer.
Definition: vty.h:96
bool expert_mode
Whether the expert mode is enabled.
Definition: vty.h:170
int cp
Command cursor point.
Definition: vty.h:99
@ VTY_MORELINE
Definition: vty.h:127
@ VTY_NORMAL
Definition: vty.h:127
@ VTY_MORE
Definition: vty.h:127
@ VTY_CLOSE
Definition: vty.h:127
int monitor
Definition: vty.h:157
int max
Command max length.
Definition: vty.h:105
enum vty_type type
Is this vty connect to file or not.
Definition: vty.h:84
int fd
File descripter of this vty.
Definition: vty.h:81
int hindex
History insert end point.
Definition: vty.h:114
int lines
Configure lines.
Definition: vty.h:155
unsigned char iac_sb_in_progress
IAC SB (option subnegotiation) handling.
Definition: vty.h:137
#define TELNET_NAWS_SB_LEN
Definition: vty.h:141