libosmovty  1.3.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 
32 enum event {
38 #ifdef VTYSH
39  VTYSH_SERV,
40  VTYSH_READ,
41  VTYSH_WRITE
42 #endif /* VTYSH */
43 };
44 
45 enum vty_type {
50 };
51 
53  struct llist_head entry;
54 
56  void *priv;
57 
59  int node;
60 
63  char *indent;
64 };
65 
67 struct vty {
69  FILE *file;
70 
72  void *priv;
73 
75  int fd;
76 
78  enum vty_type type;
79 
81  int node;
82 
84  int fail;
85 
87  struct buffer *obuf;
88 
90  char *buf;
91 
93  int cp;
94 
96  int length;
97 
99  int max;
100 
102  char *hist[VTY_MAXHIST];
103 
105  int hp;
106 
108  int hindex;
109 
112  void *index;
113 
115  void *index_sub;
116 
118  unsigned char escape;
119 
121  enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
122 
128  unsigned char iac;
129 
131  unsigned char iac_sb_in_progress;
132  /* At the moment, we care only about the NAWS (window size) negotiation,
133  * and that requires just a 5-character buffer (RFC 1073):
134  * <NAWS char> <16-bit width> <16-bit height> */
135 #define TELNET_NAWS_SB_LEN 5
136 
137  unsigned char sb_buf[TELNET_NAWS_SB_LEN];
141  size_t sb_len;
142 
144  int width;
146  int height;
147 
149  int lines;
150 
151  int monitor;
152 
154  int config;
155 
157  struct llist_head parent_nodes;
158 
161  char *indent;
162 };
163 
164 /* Small macro to determine newline is newline only or linefeed needed. */
165 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
166 
167 static inline const char *vty_newline(struct vty *vty)
168 {
169  return VTY_NEWLINE;
170 }
171 
173 struct vty_app_info {
175  const char *name;
177  const char *version;
179  const char *copyright;
181  void *tall_ctx;
187  int (*go_parent_cb)(struct vty *vty);
190  int (*is_config_node)(struct vty *vty, int node)
191  OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is"
192  " no longer called, ever, and can be left NULL.");
194  int (*config_is_consistent)(struct vty *vty);
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 void vty_init_vtysh (void);
201 void vty_reset (void);
202 struct vty *vty_new (void);
203 struct vty *vty_create (int vty_sock, void *priv);
204 bool vty_is_active(struct vty *vty);
205 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
206 int vty_out_va(struct vty *vty, const char *format, va_list ap);
207 int vty_out_newline(struct vty *);
208 int vty_read(struct vty *vty);
209 //void vty_time_print (struct vty *, int);
210 void vty_close (struct vty *);
211 char *vty_get_cwd (void);
212 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
213 int vty_config_lock (struct vty *);
214 int vty_config_unlock (struct vty *);
215 int vty_shell (struct vty *);
216 int vty_shell_serv (struct vty *);
217 void vty_hello (struct vty *);
218 void *vty_current_index(struct vty *);
219 int vty_current_node(struct vty *vty);
220 int vty_go_parent(struct vty *vty);
221 
222 /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
223 const char *vty_get_bind_addr(void);
225 int vty_get_bind_port(int default_port);
226 
227 extern void *tall_vty_ctx;
228 
229 extern struct cmd_element cfg_description_cmd;
230 extern struct cmd_element cfg_no_description_cmd;
231 
232 
238 };
239 
241  enum event event;
242  int sock;
243  struct vty *vty;
244 };
245 
void vty_init_vtysh(void)
Definition: vty.c:1786
Definition: vty.h:35
int vty_shell_serv(struct vty *)
Definition: vty.c:1781
int length
Command length.
Definition: vty.h:96
#define OSMO_DEPRECATED(text)
Definition: vty.h:49
void * tall_ctx
talloc context
Definition: vty.h:181
int node
Node status of this vty.
Definition: vty.h:59
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:161
const char * name
name of the application
Definition: vty.h:175
int lines
Configure lines.
Definition: vty.h:149
FILE * file
underlying file (if any)
Definition: vty.h:69
size_t sb_len
How many subnegotiation characters have we received?
Definition: vty.h:141
int vty_read(struct vty *vty)
Read data via vty socket.
Definition: vty.c:1277
int vty_config_unlock(struct vty *)
Unlock the configuration from a given VTY.
Definition: vty.c:365
int vty_config_lock(struct vty *)
Lock the configuration to a given VTY.
Definition: vty.c:352
int hindex
History insert end point.
Definition: vty.h:108
Internal representation of a single VTY.
Definition: vty.h:67
int int vty_out_va(struct vty *vty, const char *format, va_list ap)
Definition: vty.c:258
Definition: vty.h:240
int height
Widnow height.
Definition: vty.h:146
void vty_log(const char *level, const char *proto, const char *fmt, va_list)
Definition: vty.h:237
char * indent
When reading from a config file, these are the indenting characters expected for children of this VTY...
Definition: vty.h:63
void vty_close(struct vty *)
Close a given vty interface.
Definition: vty.c:210
int max
Command max length.
Definition: vty.h:99
int width
Window width.
Definition: vty.h:144
bool vty_is_active(struct vty *vty)
Definition: telnet_interface.c:212
Definition: vty.h:34
const char * copyright
copyright string of the application
Definition: vty.h:179
char * buf
Command input buffer.
Definition: vty.h:90
int vty_out(struct vty *, const char *,...) VTY_PRINTF_ATTRIBUTE(2
unsigned char escape
For escape character.
Definition: vty.h:118
#define VTY_NEWLINE
Definition: vty.h:165
#define VTY_PRINTF_ATTRIBUTE(a, b)
Definition: vty.h:18
Definition: vty.h:48
int sock
Definition: vty.h:242
int fail
Failure count.
Definition: vty.h:84
void * vty_current_index(struct vty *)
return the current index of a given VTY
Definition: vty.c:335
int node
Node status of this vty.
Definition: vty.h:81
int config
In configure mode.
Definition: vty.h:154
void * priv
private data, specified by creator
Definition: vty.h:56
void vty_reset(void)
Reset all VTY status.
Definition: vty.c:1736
int vty_read_config_file(const char *file_name, void *priv)
Read the configuration file using the VTY code.
Definition: vty.c:1831
unsigned char iac
IAC handling.
Definition: vty.h:128
const char * version
version string of the application
Definition: vty.h:177
int fd
File descripter of this vty.
Definition: vty.h:75
static const char * vty_newline(struct vty *vty)
Definition: vty.h:167
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:1636
void * tall_vty_ctx
Definition: vty.c:106
int vty_current_node(struct vty *vty)
return the current node of a given VTY
Definition: vty.c:341
void * priv
private data, specified by creator
Definition: vty.h:72
struct cmd_element cfg_no_description_cmd
struct buffer * obuf
Output buffer.
Definition: vty.h:87
struct llist_head entry
Definition: vty.h:53
int vty_shell(struct vty *)
Return if this VTY is a shell or not.
Definition: vty.c:253
Structure of a command element.
Definition: command.h:142
void vty_init(struct vty_app_info *app_info)
Initialize VTY layer.
Definition: vty.c:1795
Definition: vty.h:37
#define TELNET_NAWS_SB_LEN
Definition: vty.h:135
Definition: vty.h:52
Definition: buffer.c:39
struct vty * vty
Definition: vty.h:243
event
VTY events.
Definition: vty.h:32
Definition: vty.h:46
Definition: vty.h:47
uint8_t type
Definition: vty.h:121
int vty_out_newline(struct vty *)
print a newline on the given VTY
Definition: vty.c:327
int hp
History lookup current point.
Definition: vty.h:105
int monitor
Definition: vty.h:151
struct vty * vty_create(int vty_sock, void *priv)
Create new vty structure.
Definition: vty.c:1510
char * vty_get_cwd(void)
Definition: vty.c:1776
void * index_sub
For multiple level index treatment such as key chain and key.
Definition: vty.h:115
int cp
Command cursor point.
Definition: vty.h:93
void vty_hello(struct vty *)
Definition: vty.c:375
signal_vty
signal handling
Definition: vty.h:236
Definition: vty.h:33
unsigned char iac_sb_in_progress
IAC SB (option subnegotiation) handling.
Definition: vty.h:131
struct vty * vty_new(void)
Allocate a new vty interface structure.
Definition: vty.c:114
Information an application registers with the VTY.
Definition: vty.h:173
vty_type
Definition: vty.h:45
#define VTY_MAXHIST
Definition: vty.h:29
Definition: vty.h:36
int vty_go_parent(struct vty *vty)
Definition: command.c:2163
const char * vty_get_bind_addr(void)
Definition: vty.c:1629
void * index
For current referencing point of interface, route-map, access-list etc...
Definition: vty.h:112