libosmovty  0.9.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 
11 /* GCC have printf type attribute check. */
12 #ifdef __GNUC__
13 #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
14 #else
15 #define VTY_PRINTF_ATTRIBUTE(a,b)
16 #endif /* __GNUC__ */
17 
18 /* Does the I/O error indicate that the operation should be retried later? */
19 #define ERRNO_IO_RETRY(EN) \
20  (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
21 
22 /* Vty read buffer size. */
23 #define VTY_READ_BUFSIZ 512
24 
25 #define VTY_BUFSIZ 512
26 #define VTY_MAXHIST 20
27 
29 enum event {
30  VTY_SERV,
31  VTY_READ,
32  VTY_WRITE,
33  VTY_CLOSED,
34  VTY_TIMEOUT_RESET,
35 #ifdef VTYSH
36  VTYSH_SERV,
37  VTYSH_READ,
38  VTYSH_WRITE
39 #endif /* VTYSH */
40 };
41 
42 enum vty_type {
43  VTY_TERM,
44  VTY_FILE,
45  VTY_SHELL,
46  VTY_SHELL_SERV
47 };
48 
50 struct vty {
52  FILE *file;
53 
55  void *priv;
56 
58  int fd;
59 
61  enum vty_type type;
62 
64  int node;
65 
67  int fail;
68 
70  struct buffer *obuf;
71 
73  char *buf;
74 
76  int cp;
77 
79  int length;
80 
82  int max;
83 
85  char *hist[VTY_MAXHIST];
86 
88  int hp;
89 
91  int hindex;
92 
95  void *index;
96 
98  void *index_sub;
99 
101  unsigned char escape;
102 
104  enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
105 
111  unsigned char iac;
112 
114  unsigned char iac_sb_in_progress;
115  /* At the moment, we care only about the NAWS (window size) negotiation,
116  * and that requires just a 5-character buffer (RFC 1073):
117  * <NAWS char> <16-bit width> <16-bit height> */
118 #define TELNET_NAWS_SB_LEN 5
119 
120  unsigned char sb_buf[TELNET_NAWS_SB_LEN];
124  size_t sb_len;
125 
127  int width;
129  int height;
130 
132  int lines;
133 
134  int monitor;
135 
137  int config;
138 };
139 
140 /* Small macro to determine newline is newline only or linefeed needed. */
141 #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
142 
143 static inline const char *vty_newline(struct vty *vty)
144 {
145  return VTY_NEWLINE;
146 }
147 
149 struct vty_app_info {
151  const char *name;
153  const char *version;
155  const char *copyright;
157  void *tall_ctx;
159  int (*go_parent_cb)(struct vty *vty);
161  int (*is_config_node)(struct vty *vty, int node);
163  int (*config_is_consistent)(struct vty *vty);
164 };
165 
166 /* Prototypes. */
167 void vty_init(struct vty_app_info *app_info);
168 int vty_read_config_file(const char *file_name, void *priv);
169 void vty_init_vtysh (void);
170 void vty_reset (void);
171 struct vty *vty_new (void);
172 struct vty *vty_create (int vty_sock, void *priv);
173 int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3);
174 int vty_out_newline(struct vty *);
175 int vty_read(struct vty *vty);
176 //void vty_time_print (struct vty *, int);
177 void vty_close (struct vty *);
178 char *vty_get_cwd (void);
179 void vty_log (const char *level, const char *proto, const char *fmt, va_list);
180 int vty_config_lock (struct vty *);
181 int vty_config_unlock (struct vty *);
182 int vty_shell (struct vty *);
183 int vty_shell_serv (struct vty *);
184 void vty_hello (struct vty *);
185 void *vty_current_index(struct vty *);
186 int vty_current_node(struct vty *vty);
187 int vty_go_parent(struct vty *vty);
188 
189 extern void *tall_vty_ctx;
190 
191 extern struct cmd_element cfg_description_cmd;
192 extern struct cmd_element cfg_no_description_cmd;
193 
194 
199  S_VTY_EVENT,
200 };
201 
203  enum event event;
204  int sock;
205  struct vty *vty;
206 };
207 
int length
Command length.
Definition: vty.h:79
void * tall_ctx
talloc context
Definition: vty.h:157
const char * name
name of the application
Definition: vty.h:151
int lines
Configure lines.
Definition: vty.h:132
FILE * file
underlying file (if any)
Definition: vty.h:52
size_t sb_len
How many subnegotiation characters have we received?
Definition: vty.h:124
int vty_read(struct vty *vty)
Read data via vty socket.
Definition: vty.c:1260
unsigned char sb_buf[TELNET_NAWS_SB_LEN]
sub-negotiation buffer
Definition: vty.h:120
int vty_config_unlock(struct vty *)
Unlock the configuration from a given VTY.
Definition: vty.c:330
int vty_config_lock(struct vty *)
Lock the configuration to a given VTY.
Definition: vty.c:317
int hindex
History insert end point.
Definition: vty.h:91
Definition: vty.h:50
Definition: vty.h:202
int height
Widnow height.
Definition: vty.h:129
void vty_close(struct vty *)
Close a given vty interface.
Definition: vty.c:184
int max
Command max length.
Definition: vty.h:82
int width
Window width.
Definition: vty.h:127
enum vty_type type
Is this vty connect to file or not.
Definition: vty.h:61
const char * copyright
copyright string of the application
Definition: vty.h:155
char * hist[VTY_MAXHIST]
Histry of command.
Definition: vty.h:85
char * buf
Command input buffer.
Definition: vty.h:73
unsigned char escape
For escape character.
Definition: vty.h:101
int vty_out(struct vty *vty, const char *format,...)
VTY standard output function.
Definition: vty.c:235
int fail
Failure count.
Definition: vty.h:67
void * vty_current_index(struct vty *)
return the current index of a given VTY
Definition: vty.c:300
int node
Node status of this vty.
Definition: vty.h:64
int config
In configure mode.
Definition: vty.h:137
void vty_reset(void)
Reset all VTY status.
Definition: vty.c:1669
int vty_read_config_file(const char *file_name, void *priv)
Read the configuration file using the VTY code.
Definition: vty.c:1766
unsigned char iac
IAC handling.
Definition: vty.h:111
const char * version
version string of the application
Definition: vty.h:153
int fd
File descripter of this vty.
Definition: vty.h:58
int vty_current_node(struct vty *vty)
return the current node of a given VTY
Definition: vty.c:306
void * priv
private data, specified by creator
Definition: vty.h:55
enum vty::@1 status
Current vty status.
struct buffer * obuf
Output buffer.
Definition: vty.h:70
int vty_shell(struct vty *)
Return if this VTY is a shell or not.
Definition: vty.c:225
Structure of a command element.
Definition: command.h:126
int int vty_out_newline(struct vty *)
print a newline on the given VTY
Definition: vty.c:292
void vty_init(struct vty_app_info *app_info)
Initialize VTY layer.
Definition: vty.c:1730
Definition: buffer.c:36
event
VTY events.
Definition: vty.h:29
int hp
History lookup current point.
Definition: vty.h:88
struct vty * vty_create(int vty_sock, void *priv)
Create new vty structure.
Definition: vty.c:1481
void * index_sub
For multiple level index treatment such as key chain and key.
Definition: vty.h:98
int cp
Command cursor point.
Definition: vty.h:76
signal_vty
Definition: vty.h:198
unsigned char iac_sb_in_progress
IAC SB (option subnegotiation) handling.
Definition: vty.h:114
struct vty * vty_new(void)
Allocate a new vty interface structure.
Definition: vty.c:91
Definition: vty.h:149
void * index
For current referencing point of interface, route-map, access-list etc...
Definition: vty.h:95