libosmovty  1.9.0.21-11a41.202311082026
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 vty {
62  FILE *file;
63 
65  void *priv;
66 
68  int fd;
69 
71  enum vty_type type;
72 
74  int node;
75 
77  int fail;
78 
80  struct buffer *obuf;
81 
83  char *buf;
84 
86  int cp;
87 
89  int length;
90 
92  int max;
93 
95  char *hist[VTY_MAXHIST];
96 
98  int hp;
99 
101  int hindex;
102 
105  void *index;
106 
108  void *index_sub;
109 
111  unsigned char escape;
112 
115 
121  unsigned char iac;
122 
124  unsigned char iac_sb_in_progress;
125  /* At the moment, we care only about the NAWS (window size) negotiation,
126  * and that requires just a 5-character buffer (RFC 1073):
127  * <NAWS char> <16-bit width> <16-bit height> */
128 #define TELNET_NAWS_SB_LEN 5
129 
130  unsigned char sb_buf[TELNET_NAWS_SB_LEN];
134  size_t sb_len;
135 
137  int width;
139  int height;
140 
142  int lines;
143 
144  int monitor;
145 
147  int config;
148 
151 
154  char *indent;
155 
158 };
159 
160 /* Small macro to determine newline is newline only or linefeed needed. */
161 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
162 
163 static inline const char *vty_newline(struct vty *vty)
164 {
165  return VTY_NEWLINE;
166 }
167 
169 struct vty_app_info {
171  const char *name;
173  const char *version;
175  const char *copyright;
177  void *tall_ctx;
183  int (*go_parent_cb)(struct vty *vty);
186  int (*is_config_node)(struct vty *vty, int node)
187  OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is"
188  " no longer called, ever, and can be left NULL.");
190  int (*config_is_consistent)(struct vty *vty);
192  const char * usr_attr_desc[VTY_CMD_USR_ATTR_NUM];
194  char usr_attr_letters[VTY_CMD_USR_ATTR_NUM];
195 };
196 
197 /* Prototypes. */
198 void vty_init(struct vty_app_info *app_info);
199 int vty_read_config_file(const char *file_name, void *priv);
200 int vty_read_config_filep(FILE *confp, void *priv);
201 void vty_init_vtysh (void);
202 void vty_reset (void);
203 struct vty *vty_new (void);
204 struct vty *vty_create (int vty_sock, void *priv);
205 bool vty_is_active(struct vty *vty);
206 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
207 int vty_out_va(struct vty *vty, const char *format, va_list ap);
208 int vty_out_newline(struct vty *);
209 int vty_out_uptime(struct vty *vty, const struct timespec *starttime);
210 int vty_read(struct vty *vty);
211 //void vty_time_print (struct vty *, int);
212 void vty_close (struct vty *);
213 void vty_flush(struct vty *vty);
214 char *vty_get_cwd (void);
215 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
216 int vty_config_lock (struct vty *);
217 int vty_config_unlock (struct vty *);
218 int vty_shell (struct vty *);
219 int vty_shell_serv (struct vty *);
220 void vty_hello (struct vty *);
221 void *vty_current_index(struct vty *);
222 int vty_current_node(struct vty *vty);
223 int vty_go_parent(struct vty *vty);
224 
225 /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
226 const char *vty_get_bind_addr(void);
228 int vty_get_bind_port(int default_port);
229 
230 extern void *tall_vty_ctx;
231 
232 extern struct cmd_element cfg_description_cmd;
233 extern struct cmd_element cfg_no_description_cmd;
234 
235 
241 };
242 
244  enum event event;
245  int sock;
246  struct vty *vty;
247 };
248 
void vty_init_vtysh(void)
Definition: vty.c:1834
Definition: vty.h:42
int vty_shell_serv(struct vty *)
Definition: vty.c:1829
int length
Command length.
Definition: vty.h:89
#define OSMO_DEPRECATED(text)
Definition: vty.h:56
void * tall_ctx
talloc context
Definition: vty.h:177
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:154
const char * name
name of the application
Definition: vty.h:171
int lines
Configure lines.
Definition: vty.h:142
FILE * file
underlying file (if any)
Definition: vty.h:62
size_t sb_len
How many subnegotiation characters have we received?
Definition: vty.h:134
int vty_read_config_filep(FILE *confp, void *priv)
Read up VTY configuration from a file stream.
Definition: vty.c:1514
int vty_read(struct vty *vty)
Read data via vty socket.
Definition: vty.c:1318
unsigned char sb_buf[TELNET_NAWS_SB_LEN]
sub-negotiation buffer
Definition: vty.h:130
int vty_config_unlock(struct vty *)
Unlock the configuration from a given VTY.
Definition: vty.c:392
int vty_config_lock(struct vty *)
Lock the configuration to a given VTY.
Definition: vty.c:379
int hindex
History insert end point.
Definition: vty.h:101
Internal representation of a single VTY.
Definition: vty.h:60
int int vty_out_va(struct vty *vty, const char *format, va_list ap)
Definition: vty.c:266
Definition: vty.h:243
int height
Widnow height.
Definition: vty.h:139
void vty_log(const char *level, const char *proto, const char *fmt, va_list)
Definition: vty.h:240
void vty_close(struct vty *)
Close a given vty interface.
Definition: vty.c:218
#define VTY_CMD_USR_ATTR_NUM
Definition: vty.h:33
int max
Command max length.
Definition: vty.h:92
int width
Window width.
Definition: vty.h:137
enum vty_type type
Is this vty connect to file or not.
Definition: vty.h:71
bool vty_is_active(struct vty *vty)
Definition: telnet_interface.c:219
Definition: vty.h:41
bool expert_mode
Whether the expert mode is enabled.
Definition: vty.h:157
const char * copyright
copyright string of the application
Definition: vty.h:175
char * hist[VTY_MAXHIST]
Histry of command.
Definition: vty.h:95
char * buf
Command input buffer.
Definition: vty.h:83
int vty_out(struct vty *, const char *,...) VTY_PRINTF_ATTRIBUTE(2
enum vty::@2 status
Current vty status.
unsigned char escape
For escape character.
Definition: vty.h:111
static struct timespec starttime
Definition: command.c:71
#define VTY_NEWLINE
Definition: vty.h:161
#define VTY_PRINTF_ATTRIBUTE(a, b)
Definition: vty.h:19
Definition: vty.h:114
Definition: vty.h:55
int sock
Definition: vty.h:245
int fail
Failure count.
Definition: vty.h:77
void * vty_current_index(struct vty *)
return the current index of a given VTY
Definition: vty.c:362
int node
Node status of this vty.
Definition: vty.h:74
int config
In configure mode.
Definition: vty.h:147
Definition: vty.h:114
void vty_reset(void)
Reset all VTY status.
Definition: vty.c:1784
int vty_read_config_file(const char *file_name, void *priv)
Read the configuration file using the VTY code.
Definition: vty.c:1911
unsigned char iac
IAC handling.
Definition: vty.h:121
const char * version
version string of the application
Definition: vty.h:173
int fd
File descripter of this vty.
Definition: vty.h:68
static const char * vty_newline(struct vty *vty)
Definition: vty.h:163
int vty_get_bind_port(int default_port)
Returns configured port passed to the &#39;line vty&#39;/&#39;bind&#39; command or default_port.
Definition: vty.c:1684
void * tall_vty_ctx
Definition: vty.c:108
int vty_current_node(struct vty *vty)
return the current node of a given VTY
Definition: vty.c:368
void * priv
private data, specified by creator
Definition: vty.h:65
struct cmd_element cfg_no_description_cmd
struct buffer * obuf
Output buffer.
Definition: vty.h:80
int vty_shell(struct vty *)
Return if this VTY is a shell or not.
Definition: vty.c:261
Structure of a command element.
Definition: command.h:175
void vty_init(struct vty_app_info *app_info)
Initialize VTY layer.
Definition: vty.c:1843
Definition: vty.h:44
#define TELNET_NAWS_SB_LEN
Definition: vty.h:128
Definition: buffer.c:39
struct vty * vty
Definition: vty.h:246
event
VTY events.
Definition: vty.h:39
Definition: vty.h:53
Definition: vty.h:54
Definition: vty.h:114
int vty_out_newline(struct vty *)
print a newline on the given VTY
Definition: vty.c:335
int hp
History lookup current point.
Definition: vty.h:98
void vty_flush(struct vty *vty)
Definition: vty.c:211
int monitor
Definition: vty.h:144
struct vty * vty_create(int vty_sock, void *priv)
Create new vty structure.
Definition: vty.c:1558
char * vty_get_cwd(void)
Definition: vty.c:1824
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
void * index_sub
For multiple level index treatment such as key chain and key.
Definition: vty.h:108
int cp
Command cursor point.
Definition: vty.h:86
void vty_hello(struct vty *)
Definition: vty.c:402
signal_vty
signal handling
Definition: vty.h:239
Definition: vty.h:40
unsigned char iac_sb_in_progress
IAC SB (option subnegotiation) handling.
Definition: vty.h:124
struct vty * vty_new(void)
Allocate a new vty interface structure.
Definition: vty.c:116
struct llist_head parent_nodes
List of parent nodes, last item is the outermost parent.
Definition: vty.h:150
Information an application registers with the VTY.
Definition: vty.h:169
vty_type
Definition: vty.h:52
#define VTY_MAXHIST
Definition: vty.h:30
Definition: vty.h:114
Definition: vty.h:43
int vty_go_parent(struct vty *vty)
Definition: command.c:2484
const char * vty_get_bind_addr(void)
Definition: vty.c:1677
void * index
For current referencing point of interface, route-map, access-list etc...
Definition: vty.h:105