libosmovty  1.1.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 #include <stdbool.h>
6 
8 
13 /* GCC have printf type attribute check. */
14 #ifdef __GNUC__
15 #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
16 #else
17 #define VTY_PRINTF_ATTRIBUTE(a,b)
18 #endif /* __GNUC__ */
19 
20 /* Does the I/O error indicate that the operation should be retried later? */
21 #define ERRNO_IO_RETRY(EN) \
22  (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
23 
24 /* Vty read buffer size. */
25 #define VTY_READ_BUFSIZ 512
26 
27 #define VTY_BUFSIZ 512
28 #define VTY_MAXHIST 20
29 
31 enum event {
37 #ifdef VTYSH
38  VTYSH_SERV,
39  VTYSH_READ,
40  VTYSH_WRITE
41 #endif /* VTYSH */
42 };
43 
44 enum vty_type {
49 };
50 
52  struct llist_head entry;
53 
55  void *priv;
56 
58  int node;
59 
62  char *indent;
63 };
64 
66 struct vty {
68  FILE *file;
69 
71  void *priv;
72 
74  int fd;
75 
77  enum vty_type type;
78 
80  int node;
81 
83  int fail;
84 
86  struct buffer *obuf;
87 
89  char *buf;
90 
92  int cp;
93 
95  int length;
96 
98  int max;
99 
101  char *hist[VTY_MAXHIST];
102 
104  int hp;
105 
107  int hindex;
108 
111  void *index;
112 
114  void *index_sub;
115 
117  unsigned char escape;
118 
120  enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
121 
127  unsigned char iac;
128 
130  unsigned char iac_sb_in_progress;
131  /* At the moment, we care only about the NAWS (window size) negotiation,
132  * and that requires just a 5-character buffer (RFC 1073):
133  * <NAWS char> <16-bit width> <16-bit height> */
134 #define TELNET_NAWS_SB_LEN 5
135 
136  unsigned char sb_buf[TELNET_NAWS_SB_LEN];
140  size_t sb_len;
141 
143  int width;
145  int height;
146 
148  int lines;
149 
150  int monitor;
151 
153  int config;
154 
156  struct llist_head parent_nodes;
157 
160  char *indent;
161 };
162 
163 /* Small macro to determine newline is newline only or linefeed needed. */
164 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
165 
166 static inline const char *vty_newline(struct vty *vty)
167 {
168  return VTY_NEWLINE;
169 }
170 
172 struct vty_app_info {
174  const char *name;
176  const char *version;
178  const char *copyright;
180  void *tall_ctx;
182  int (*go_parent_cb)(struct vty *vty);
184  int (*is_config_node)(struct vty *vty, int node);
186  int (*config_is_consistent)(struct vty *vty);
187 };
188 
189 /* Prototypes. */
190 void vty_init(struct vty_app_info *app_info);
191 int vty_read_config_file(const char *file_name, void *priv);
192 void vty_init_vtysh (void);
193 void vty_reset (void);
194 struct vty *vty_new (void);
195 struct vty *vty_create (int vty_sock, void *priv);
196 bool vty_is_active(struct vty *vty);
197 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
198 int vty_out_va(struct vty *vty, const char *format, va_list ap);
199 int vty_out_newline(struct vty *);
200 int vty_read(struct vty *vty);
201 //void vty_time_print (struct vty *, int);
202 void vty_close (struct vty *);
203 char *vty_get_cwd (void);
204 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
205 int vty_config_lock (struct vty *);
206 int vty_config_unlock (struct vty *);
207 int vty_shell (struct vty *);
208 int vty_shell_serv (struct vty *);
209 void vty_hello (struct vty *);
210 void *vty_current_index(struct vty *);
211 int vty_current_node(struct vty *vty);
212 int vty_go_parent(struct vty *vty);
213 
214 /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */
215 const char *vty_get_bind_addr(void);
217 int vty_get_bind_port(int default_port);
218 
219 extern void *tall_vty_ctx;
220 
221 extern struct cmd_element cfg_description_cmd;
222 extern struct cmd_element cfg_no_description_cmd;
223 
224 
230 };
231 
233  enum event event;
234  int sock;
235  struct vty *vty;
236 };
237 
void vty_init_vtysh(void)
Definition: vty.c:1774
Definition: vty.h:34
int vty_shell_serv(struct vty *)
Definition: vty.c:1769
int length
Command length.
Definition: vty.h:95
Definition: vty.h:48
void * tall_ctx
talloc context
Definition: vty.h:180
int node
Node status of this vty.
Definition: vty.h:58
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:160
const char * name
name of the application
Definition: vty.h:174
int lines
Configure lines.
Definition: vty.h:148
FILE * file
underlying file (if any)
Definition: vty.h:68
size_t sb_len
How many subnegotiation characters have we received?
Definition: vty.h:140
int vty_read(struct vty *vty)
Read data via vty socket.
Definition: vty.c:1271
int vty_config_unlock(struct vty *)
Unlock the configuration from a given VTY.
Definition: vty.c:359
int vty_config_lock(struct vty *)
Lock the configuration to a given VTY.
Definition: vty.c:346
int hindex
History insert end point.
Definition: vty.h:107
Internal representation of a single VTY.
Definition: vty.h:66
int int vty_out_va(struct vty *vty, const char *format, va_list ap)
Definition: vty.c:252
Definition: vty.h:232
int height
Widnow height.
Definition: vty.h:145
void vty_log(const char *level, const char *proto, const char *fmt, va_list)
Definition: vty.h:229
char * indent
When reading from a config file, these are the indenting characters expected for children of this VTY...
Definition: vty.h:62
void vty_close(struct vty *)
Close a given vty interface.
Definition: vty.c:204
int max
Command max length.
Definition: vty.h:98
int width
Window width.
Definition: vty.h:143
bool vty_is_active(struct vty *vty)
Definition: telnet_interface.c:200
Definition: vty.h:33
const char * copyright
copyright string of the application
Definition: vty.h:178
char * buf
Command input buffer.
Definition: vty.h:89
int vty_out(struct vty *, const char *,...) VTY_PRINTF_ATTRIBUTE(2
unsigned char escape
For escape character.
Definition: vty.h:117
#define VTY_NEWLINE
Definition: vty.h:164
#define VTY_PRINTF_ATTRIBUTE(a, b)
Definition: vty.h:17
Definition: vty.h:47
int sock
Definition: vty.h:234
int fail
Failure count.
Definition: vty.h:83
void * vty_current_index(struct vty *)
return the current index of a given VTY
Definition: vty.c:329
int node
Node status of this vty.
Definition: vty.h:80
int config
In configure mode.
Definition: vty.h:153
void * priv
private data, specified by creator
Definition: vty.h:55
void vty_reset(void)
Reset all VTY status.
Definition: vty.c:1724
int vty_read_config_file(const char *file_name, void *priv)
Read the configuration file using the VTY code.
Definition: vty.c:1819
unsigned char iac
IAC handling.
Definition: vty.h:127
const char * version
version string of the application
Definition: vty.h:176
int fd
File descripter of this vty.
Definition: vty.h:74
static const char * vty_newline(struct vty *vty)
Definition: vty.h:166
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:1624
void * tall_vty_ctx
Definition: vty.c:101
int vty_current_node(struct vty *vty)
return the current node of a given VTY
Definition: vty.c:335
void * priv
private data, specified by creator
Definition: vty.h:71
struct cmd_element cfg_no_description_cmd
struct buffer * obuf
Output buffer.
Definition: vty.h:86
struct llist_head entry
Definition: vty.h:52
int vty_shell(struct vty *)
Return if this VTY is a shell or not.
Definition: vty.c:247
Structure of a command element.
Definition: command.h:142
void vty_init(struct vty_app_info *app_info)
Initialize VTY layer.
Definition: vty.c:1783
Definition: vty.h:36
#define TELNET_NAWS_SB_LEN
Definition: vty.h:134
Definition: vty.h:51
Definition: buffer.c:39
struct vty * vty
Definition: vty.h:235
event
VTY events.
Definition: vty.h:31
Definition: vty.h:45
Definition: vty.h:46
uint8_t type
Definition: vty.h:120
int vty_out_newline(struct vty *)
print a newline on the given VTY
Definition: vty.c:321
int hp
History lookup current point.
Definition: vty.h:104
int monitor
Definition: vty.h:150
struct vty * vty_create(int vty_sock, void *priv)
Create new vty structure.
Definition: vty.c:1498
char * vty_get_cwd(void)
Definition: vty.c:1764
void * index_sub
For multiple level index treatment such as key chain and key.
Definition: vty.h:114
int cp
Command cursor point.
Definition: vty.h:92
void vty_hello(struct vty *)
Definition: vty.c:369
signal_vty
signal handling
Definition: vty.h:228
Definition: vty.h:32
unsigned char iac_sb_in_progress
IAC SB (option subnegotiation) handling.
Definition: vty.h:130
struct vty * vty_new(void)
Allocate a new vty interface structure.
Definition: vty.c:109
Information an application registers with the VTY.
Definition: vty.h:172
vty_type
Definition: vty.h:44
#define VTY_MAXHIST
Definition: vty.h:28
Definition: vty.h:35
int vty_go_parent(struct vty *vty)
Definition: command.c:2169
const char * vty_get_bind_addr(void)
Definition: vty.c:1617
void * index
For current referencing point of interface, route-map, access-list etc...
Definition: vty.h:111