1/* Virtual terminal [aka TeletYpe] interface routine
2   Copyright (C) 1997 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING.  If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA.  */
20
21#ifndef _ZEBRA_VTY_H
22#define _ZEBRA_VTY_H
23
24#define VTY_BUFSIZ 512
25#define VTY_MAXHIST 20
26
27/* VTY struct. */
28struct vty
29{
30  /* File descripter of this vty. */
31  int fd;
32
33  /* Is this vty connect to file or not */
34  enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;
35
36  /* Node status of this vty */
37  int node;
38
39  /* What address is this vty comming from. */
40  char *address;
41
42  /* Privilege level of this vty. */
43  int privilege;
44
45  /* Failure count */
46  int fail;
47
48  /* Output buffer. */
49  struct buffer *obuf;
50
51  /* Command input buffer */
52  char *buf;
53
54  /* Command cursor point */
55  int cp;
56
57  /* Command length */
58  int length;
59
60  /* Command max length. */
61  int max;
62
63  /* Histry of command */
64  char *hist[VTY_MAXHIST];
65
66  /* History lookup current point */
67  int hp;
68
69  /* History insert end point */
70  int hindex;
71
72  /* For current referencing point of interface, route-map,
73     access-list etc... */
74  void *index;
75
76  /* For multiple level index treatment such as key chain and key. */
77  void *index_sub;
78
79  /* For escape character. */
80  unsigned char escape;
81
82  /* Current vty status. */
83  enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_START, VTY_CONTINUE} status;
84
85  /* IAC handling */
86  unsigned char iac;
87
88  /* IAC SB handling */
89  unsigned char iac_sb_in_progress;
90  struct buffer *sb_buffer;
91
92  /* Window width/height. */
93  int width;
94  int height;
95
96  int scroll_one;
97
98  /* Configure lines. */
99  int lines;
100
101  /* Current executing function pointer. */
102  int (*func) (struct vty *, void *arg);
103
104  /* Terminal monitor. */
105  int monitor;
106
107  /* In configure mode. */
108  int config;
109
110  /* Read and write thread. */
111  struct thread *t_read;
112  struct thread *t_write;
113
114  /* Timeout seconds and thread. */
115  unsigned long v_timeout;
116  struct thread *t_timeout;
117
118  /* Thread output function. */
119  struct thread *t_output;
120
121  /* Output data pointer. */
122  int (*output_func) (struct vty *, int);
123  void (*output_clean) (struct vty *);
124  void *output_rn;
125  unsigned long output_count;
126  int output_type;
127  void *output_arg;
128};
129
130/* Integrated configuration file. */
131#define INTEGRATE_DEFAULT_CONFIG "Zebra.conf"
132
133/* Small macro to determine newline is newline only or linefeed needed. */
134#define VTY_NEWLINE  ((vty->type == VTY_TERM) ? "\r\n" : "\n")
135
136/* Default time out value */
137#define VTY_TIMEOUT_DEFAULT 600
138
139/* Vty read buffer size. */
140#define VTY_READ_BUFSIZ 512
141
142/* Directory separator. */
143#ifndef DIRECTORY_SEP
144#define DIRECTORY_SEP '/'
145#endif /* DIRECTORY_SEP */
146
147#ifndef IS_DIRECTORY_SEP
148#define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
149#endif
150
151/* GCC have printf type attribute check.  */
152#ifdef __GNUC__
153#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
154#else
155#define PRINTF_ATTRIBUTE(a,b)
156#endif /* __GNUC__ */
157
158/* Utility macro to convert VTY argument to unsigned integer.  */
159#define VTY_GET_INTEGER(NAME,V,STR)                              \
160{                                                                \
161  char *endptr = NULL;                                           \
162  (V) = strtoul ((STR), &endptr, 10);                            \
163  if ((V) == ULONG_MAX || *endptr != '\0')                       \
164    {                                                            \
165      vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
166      return CMD_WARNING;                                        \
167    }                                                            \
168}
169
170#define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX)                \
171{                                                                \
172  char *endptr = NULL;                                           \
173  (V) = strtoul ((STR), &endptr, 10);                            \
174  if ((V) == ULONG_MAX || *endptr != '\0'                        \
175      || (V) < (MIN) || (V) > (MAX))                             \
176    {                                                            \
177      vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
178      return CMD_WARNING;                                        \
179    }                                                            \
180}
181
182/* Exported variables */
183extern char integrate_default[];
184
185/* Prototypes. */
186void vty_init (void);
187void vty_init_vtysh (void);
188void vty_reset (void);
189void vty_finish (void);
190struct vty *vty_new (void);
191int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
192void vty_read_config (char *, char *, char *);
193void vty_time_print (struct vty *, int);
194void vty_serv_sock (const char *, unsigned short, char *);
195void vty_close (struct vty *);
196char *vty_get_cwd (void);
197void vty_log (const char *, const char *, va_list);
198int vty_config_lock (struct vty *);
199int vty_config_unlock (struct vty *);
200int vty_shell (struct vty *);
201int vty_shell_serv (struct vty *);
202void vty_hello (struct vty *);
203
204#endif /* _ZEBRA_VTY_H */
205