vty.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* Virtual terminal [aka TeletYpe] interface routine
  2. Copyright (C) 1997 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #ifndef _ZEBRA_VTY_H
  17. #define _ZEBRA_VTY_H
  18. #include "thread.h"
  19. #include "log.h"
  20. #include "sockunion.h"
  21. #define VTY_MAXHIST 20
  22. /* VTY struct. */
  23. struct vty
  24. {
  25. /* File descripter of this vty. */
  26. int fd;
  27. /* output FD, to support stdin/stdout combination */
  28. int wfd;
  29. /* Is this vty connect to file or not */
  30. enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;
  31. /* Node status of this vty */
  32. int node;
  33. /* Failure count */
  34. int fail;
  35. /* Output buffer. */
  36. struct buffer *obuf;
  37. /* Command input buffer */
  38. char *buf;
  39. /* Command cursor point */
  40. int cp;
  41. /* Command length */
  42. int length;
  43. /* Command max length. */
  44. int max;
  45. /* Histry of command */
  46. char *hist[VTY_MAXHIST];
  47. /* History lookup current point */
  48. int hp;
  49. /* History insert end point */
  50. int hindex;
  51. /* For current referencing point of interface, route-map,
  52. access-list etc... */
  53. void *index;
  54. /* For multiple level index treatment such as key chain and key. */
  55. void *index_sub;
  56. /* For escape character. */
  57. unsigned char escape;
  58. /* Current vty status. */
  59. enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;
  60. /* IAC handling: was the last character received the
  61. IAC (interpret-as-command) escape character (and therefore the next
  62. character will be the command code)? Refer to Telnet RFC 854. */
  63. unsigned char iac;
  64. /* IAC SB (option subnegotiation) handling */
  65. unsigned char iac_sb_in_progress;
  66. /* At the moment, we care only about the NAWS (window size) negotiation,
  67. and that requires just a 5-character buffer (RFC 1073):
  68. <NAWS char> <16-bit width> <16-bit height> */
  69. #define TELNET_NAWS_SB_LEN 5
  70. unsigned char sb_buf[TELNET_NAWS_SB_LEN];
  71. /* How many subnegotiation characters have we received? We just drop
  72. those that do not fit in the buffer. */
  73. size_t sb_len;
  74. /* Window width/height. */
  75. int width;
  76. int height;
  77. /* Configure lines. */
  78. int lines;
  79. /* Terminal monitor. */
  80. int monitor;
  81. /* In configure mode. */
  82. int config;
  83. /* Read and write thread. */
  84. struct thread *t_read;
  85. struct thread *t_write;
  86. /* Timeout seconds and thread. */
  87. unsigned long v_timeout;
  88. struct thread *t_timeout;
  89. /* What address is this vty comming from. */
  90. char address[SU_ADDRSTRLEN];
  91. };
  92. /* Integrated configuration file. */
  93. #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
  94. /* Small macro to determine newline is newline only or linefeed needed. */
  95. #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
  96. /* Default time out value */
  97. #define VTY_TIMEOUT_DEFAULT 600
  98. /* Vty read buffer size. */
  99. #define VTY_READ_BUFSIZ 512
  100. /* Directory separator. */
  101. #ifndef DIRECTORY_SEP
  102. #define DIRECTORY_SEP '/'
  103. #endif /* DIRECTORY_SEP */
  104. #ifndef IS_DIRECTORY_SEP
  105. #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
  106. #endif
  107. /* GCC have printf type attribute check. */
  108. #ifdef __GNUC__
  109. #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
  110. #else
  111. #define PRINTF_ATTRIBUTE(a,b)
  112. #endif /* __GNUC__ */
  113. /* Utility macros to convert VTY argument to unsigned long */
  114. #define VTY_GET_ULONG(NAME,V,STR) \
  115. do { \
  116. char *endptr = NULL; \
  117. errno = 0; \
  118. (V) = strtoul ((STR), &endptr, 10); \
  119. if (*(STR) == '-' || *endptr != '\0' || errno) \
  120. { \
  121. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  122. return CMD_WARNING; \
  123. } \
  124. } while (0)
  125. /*
  126. * The logic below ((TMPL) <= ((MIN) && (TMPL) != (MIN)) is
  127. * done to circumvent the compiler complaining about
  128. * comparing unsigned numbers against zero, if MIN is zero.
  129. * NB: The compiler isn't smart enough to suprress the warning
  130. * if you write (MIN) != 0 && tmpl < (MIN).
  131. */
  132. #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \
  133. do { \
  134. VTY_GET_ULONG(NAME, (TMPL), STR); \
  135. if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \
  136. { \
  137. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\
  138. return CMD_WARNING; \
  139. } \
  140. } while (0)
  141. #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
  142. do { \
  143. unsigned long tmpl; \
  144. VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
  145. (V) = tmpl; \
  146. } while (0)
  147. #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \
  148. do { \
  149. unsigned long tmpl; \
  150. VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
  151. } while (0)
  152. #define VTY_GET_INTEGER(NAME,V,STR) \
  153. VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
  154. #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
  155. do { \
  156. int retv; \
  157. retv = inet_aton ((STR), &(V)); \
  158. if (!retv) \
  159. { \
  160. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  161. return CMD_WARNING; \
  162. } \
  163. } while (0)
  164. #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
  165. do { \
  166. int retv; \
  167. retv = str2prefix_ipv4 ((STR), &(V)); \
  168. if (retv <= 0) \
  169. { \
  170. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  171. return CMD_WARNING; \
  172. } \
  173. } while (0)
  174. #define VTY_WARN_EXPERIMENTAL() \
  175. do { \
  176. vty_out (vty, "%% WARNING: this command is experimental. Both its name and" \
  177. " parameters may%s%% change in a future version of Quagga," \
  178. " possibly breaking your configuration!%s", \
  179. VTY_NEWLINE, VTY_NEWLINE); \
  180. } while (0)
  181. /* Exported variables */
  182. extern char integrate_default[];
  183. /* Prototypes. */
  184. extern void vty_init (struct thread_master *);
  185. extern void vty_init_vtysh (void);
  186. extern void vty_terminate (void);
  187. extern void vty_reset (void);
  188. extern struct vty *vty_new (void);
  189. extern struct vty *vty_stdio (void (*atclose)(void));
  190. extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
  191. extern void vty_read_config (char *, char *);
  192. extern void vty_time_print (struct vty *, int);
  193. extern void vty_serv_sock (const char *, unsigned short, const char *);
  194. extern void vty_close (struct vty *);
  195. extern char *vty_get_cwd (void);
  196. extern void vty_log (const char *level, const char *proto,
  197. const char *fmt, struct timestamp_control *, va_list);
  198. extern int vty_config_lock (struct vty *);
  199. extern int vty_config_unlock (struct vty *);
  200. extern int vty_shell (struct vty *);
  201. extern int vty_shell_serv (struct vty *);
  202. extern void vty_hello (struct vty *);
  203. /* Send a fixed-size message to all vty terminal monitors; this should be
  204. an async-signal-safe function. */
  205. extern void vty_log_fixed (char *buf, size_t len);
  206. #endif /* _ZEBRA_VTY_H */