libosmovty  1.6.0.113-b17c
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 #include <time.h>
7 
9 #include <osmocom/core/defs.h>
10 
15 /* GCC have printf type attribute check. */
16 #ifdef __GNUC__
17 #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
18 #else
19 #define VTY_PRINTF_ATTRIBUTE(a,b)
20 #endif /* __GNUC__ */
21 
22 /* Does the I/O error indicate that the operation should be retried later? */
23 #define ERRNO_IO_RETRY(EN) \
24  (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
25 
26 /* Vty read buffer size. */
27 #define VTY_READ_BUFSIZ 512
28 
29 #define VTY_BUFSIZ 512
30 #define VTY_MAXHIST 20
31 
32 /* Number of application / library specific VTY attributes */
33 #define VTY_CMD_USR_ATTR_NUM 32
34 /* Flag characters reserved for global VTY attributes */
35 #define VTY_CMD_ATTR_FLAGS_RESERVED \
36  { '.', '!', '@', '^' }
37 
39 enum event {
45 #ifdef VTYSH
46  VTYSH_SERV,
47  VTYSH_READ,
48  VTYSH_WRITE
49 #endif /* VTYSH */
50 };
51 
52 enum vty_type {
57 };
58 
60  struct llist_head entry;
61 
63  void *priv;
64 
66  int node;
67 
70  char *indent;
71 };
72 
74 struct vty {
76  FILE *file;
77 
79  void *priv;
80 
82  int fd;
83 
85  enum vty_type type;
86 
88  int node;
89 
91  int fail;
92 
94  struct buffer *obuf;
95 
97  char *buf;
98 
100  int cp;
101 
103  int length;
104 
106  int max;
107 
110 
112  int hp;
113 
115  int hindex;
116 
119  void *index;
120 
122  void *index_sub;
123 
125  unsigned char escape;
126 
129 
135  unsigned char iac;
136 
138  unsigned char iac_sb_in_progress;
139  /* At the moment, we care only about the NAWS (window size) negotiation,
140  * and that requires just a 5-character buffer (RFC 1073):
141  * <NAWS char> <16-bit width> <16-bit height> */
142 #define TELNET_NAWS_SB_LEN 5
144  unsigned char sb_buf[TELNET_NAWS_SB_LEN];
148  size_t sb_len;
149 
151  int width;
153  int height;
154 
156  int lines;
157 
158  int monitor;
159 
161  int config;
162 
164  struct llist_head parent_nodes;
165 
168  char *indent;
169 
172 };
173 
174 /* Small macro to determine newline is newline only or linefeed needed. */
175 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
176 
177 static inline const char *vty_newline(struct vty *vty)
178 {
179  return VTY_NEWLINE;
180 }
181 
183 struct vty_app_info {
185  const char *name;
187  const char *version;
189  const char *copyright;
191  void *tall_ctx;
197  int (*go_parent_cb)(struct vty *vty);
200  int (*is_config_node)(struct vty *vty, int node)
201  OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is"
202  " no longer called, ever, and can be left NULL.");
204  int (*config_is_consistent)(struct vty *vty);
209 };
210 
211 /* Prototypes. */
212 void vty_init(struct vty_app_info *app_info);
213 int vty_read_config_file(const char *file_name, void *priv);
214 int vty_read_config_filep(FILE *confp, void *priv);
215 void vty_init_vtysh (void);
216 void vty_reset (void);
217 struct vty *vty_new (void);
218 struct vty *vty_create (int vty_sock, void *priv);
219 bool vty_is_active(struct vty *vty);
220 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
221 int vty_out_va(struct vty *vty, const char *format, va_list ap);
222 int vty_out_newline(struct vty *);
223 int vty_out_uptime(struct vty *vty, const struct timespec *starttime);
224 int vty_read(struct vty *vty);
225 //void vty_time_print (struct vty *, int);
226 void vty_close (struct vty *);
227 void vty_flush(struct vty *vty);
228 char *vty_get_cwd (void);
229 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
230 int vty_config_lock (struct vty *);
231 int vty_config_unlock (struct vty *);
232 int vty_shell (struct vty *);
233 int vty_shell_serv (struct vty *);
234 void vty_hello (struct vty *);
235 void *vty_current_index(struct vty *);
236 int vty_current_node(struct vty *vty);
237 int vty_go_parent(struct vty *vty);
238 
239 /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
240 const char *vty_get_bind_addr(void);
242 int vty_get_bind_port(int default_port);
243 
244 extern void *tall_vty_ctx;
245 
246 extern struct cmd_element cfg_description_cmd;
247 extern struct cmd_element cfg_no_description_cmd;
248 
249 
255 };
256 
258  enum event event;
259  int sock;
260  struct vty *vty;
261 };
262 
vty
Definition: tdef_vty.c:296
static struct timespec starttime
Definition: command.c:71
uint16_t node
#define OSMO_DEPRECATED(text)
int vty_go_parent(struct vty *vty)
Definition: command.c:2418
int vty_config_lock(struct vty *)
Lock the configuration to a given VTY.
Definition: vty.c:379
vty_type
Definition: vty.h:52
struct vty * vty_create(int vty_sock, void *priv)
Create new vty structure.
Definition: vty.c:1558
void vty_reset(void)
Reset all VTY status.
Definition: vty.c:1784
#define VTY_MAXHIST
Definition: vty.h:30
char * vty_get_cwd(void)
Definition: vty.c:1824
int vty_shell_serv(struct vty *)
Definition: vty.c:1829
#define VTY_NEWLINE
Definition: vty.h:175
#define VTY_CMD_USR_ATTR_NUM
Definition: vty.h:33
int vty_read_config_filep(FILE *confp, void *priv)
Read up VTY configuration from a file stream.
Definition: vty.c:1514
const char * vty_get_bind_addr(void)
Definition: vty.c:1677
#define VTY_PRINTF_ATTRIBUTE(a, b)
Definition: vty.h:19
struct vty * vty_new(void)
Allocate a new vty interface structure.
Definition: vty.c:116
int vty_out_uptime(struct vty *vty, const struct timespec *starttime)
calculates the time difference of a give timespec to the current time and prints in a human readable ...
Definition: vty.c:345
struct cmd_element cfg_description_cmd
void vty_init_vtysh(void)
Definition: vty.c:1834
int vty_read(struct vty *vty)
Read data via vty socket.
Definition: vty.c:1318
int vty_read_config_file(const char *file_name, void *priv)
Read the configuration file using the VTY code.
Definition: vty.c:1911
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:368
int vty_shell(struct vty *)
Return if this VTY is a shell or not.
Definition: vty.c:261
bool vty_is_active(struct vty *vty)
Definition: telnet_interface.c:208
void vty_close(struct vty *)
Close a given vty interface.
Definition: vty.c:218
void vty_hello(struct vty *)
Definition: vty.c:402
int vty_get_bind_port(int default_port)
Returns configured port passed to the 'line vty'/'bind' command or default_port.
Definition: vty.c:1684
void vty_flush(struct vty *vty)
Definition: vty.c:211
int vty_config_unlock(struct vty *)
Unlock the configuration from a given VTY.
Definition: vty.c:392
int vty_out_newline(struct vty *)
print a newline on the given VTY
Definition: vty.c:335
struct cmd_element cfg_no_description_cmd
int int vty_out_va(struct vty *vty, const char *format, va_list ap)
Definition: vty.c:266
static const char * vty_newline(struct vty *vty)
Definition: vty.h:177
void * vty_current_index(struct vty *)
return the current index of a given VTY
Definition: vty.c:362
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:1843
signal_vty
signal handling
Definition: vty.h:253
void * tall_vty_ctx
Definition: vty.c:108
@ VTY_FILE
Definition: vty.h:54
@ VTY_SHELL
Definition: vty.h:55
@ VTY_SHELL_SERV
Definition: vty.h:56
@ VTY_TERM
Definition: vty.h:53
@ VTY_WRITE
Definition: vty.h:42
@ VTY_SERV
Definition: vty.h:40
@ VTY_READ
Definition: vty.h:41
@ VTY_CLOSED
Definition: vty.h:43
@ VTY_TIMEOUT_RESET
Definition: vty.h:44
@ S_VTY_EVENT
Definition: vty.h:254
Definition: buffer.c:39
Structure of a command element.
Definition: command.h:175
Information an application registers with the VTY.
Definition: vty.h:183
int(* config_is_consistent)(struct vty *vty)
Check if the config is consistent before write.
Definition: vty.h:204
const char * version
version string of the application
Definition: vty.h:187
const char * name
name of the application
Definition: vty.h:185
char usr_attr_letters[VTY_CMD_USR_ATTR_NUM]
Flag letters of the application specific VTY attributes (optional).
Definition: vty.h:208
const char * copyright
copyright string of the application
Definition: vty.h:189
const char * usr_attr_desc[VTY_CMD_USR_ATTR_NUM]
Description of the application specific VTY attributes (optional).
Definition: vty.h:206
int(* go_parent_cb)(struct vty *vty)
Call-back for taking actions upon exiting a node.
Definition: vty.h:197
void * tall_ctx
talloc context
Definition: vty.h:191
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:200
Definition: vty.h:59
char * indent
When reading from a config file, these are the indenting characters expected for children of this VTY...
Definition: vty.h:70
struct llist_head entry
Definition: vty.h:60
int node
Node status of this vty.
Definition: vty.h:66
void * priv
private data, specified by creator
Definition: vty.h:63
Definition: vty.h:257
enum event event
Definition: vty.h:258
struct vty * vty
Definition: vty.h:260
int sock
Definition: vty.h:259
Internal representation of a single VTY.
Definition: vty.h:74
struct buffer * obuf
Output buffer.
Definition: vty.h:94
int fail
Failure count.
Definition: vty.h:91
int config
In configure mode.
Definition: vty.h:161
void * priv
private data, specified by creator
Definition: vty.h:79
int length
Command length.
Definition: vty.h:103
void * index_sub
For multiple level index treatment such as key chain and key.
Definition: vty.h:122
enum vty::@2 status
Current vty status.
char * hist[VTY_MAXHIST]
Histry of command.
Definition: vty.h:109
int width
Window width.
Definition: vty.h:151
unsigned char escape
For escape character.
Definition: vty.h:125
struct llist_head parent_nodes
List of parent nodes, last item is the outermost parent.
Definition: vty.h:164
char * indent
When reading from a config file, these are the indenting characters expected for children of the curr...
Definition: vty.h:168
int hp
History lookup current point.
Definition: vty.h:112
FILE * file
underlying file (if any)
Definition: vty.h:76
unsigned char iac
IAC handling.
Definition: vty.h:135
int height
Widnow height.
Definition: vty.h:153
size_t sb_len
How many subnegotiation characters have we received?
Definition: vty.h:148
void * index
For current referencing point of interface, route-map, access-list etc...
Definition: vty.h:119
unsigned char sb_buf[TELNET_NAWS_SB_LEN]
sub-negotiation buffer
Definition: vty.h:144
int node
Node status of this vty.
Definition: vty.h:88
char * buf
Command input buffer.
Definition: vty.h:97
bool expert_mode
Whether the expert mode is enabled.
Definition: vty.h:171
int cp
Command cursor point.
Definition: vty.h:100
@ VTY_MORELINE
Definition: vty.h:128
@ VTY_NORMAL
Definition: vty.h:128
@ VTY_MORE
Definition: vty.h:128
@ VTY_CLOSE
Definition: vty.h:128
int monitor
Definition: vty.h:158
int max
Command max length.
Definition: vty.h:106
enum vty_type type
Is this vty connect to file or not.
Definition: vty.h:85
int fd
File descripter of this vty.
Definition: vty.h:82
int hindex
History insert end point.
Definition: vty.h:115
int lines
Configure lines.
Definition: vty.h:156
unsigned char iac_sb_in_progress
IAC SB (option subnegotiation) handling.
Definition: vty.h:138
#define TELNET_NAWS_SB_LEN
Definition: vty.h:142