vty.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_BUFSIZ 512
  22. #define VTY_MAXHIST 20
  23. /* VTY struct. */
  24. struct vty
  25. {
  26. /* File descripter of this vty. */
  27. int fd;
  28. /* Is this vty connect to file or not */
  29. enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;
  30. /* Node status of this vty */
  31. int node;
  32. /* Failure count */
  33. int fail;
  34. /* Output buffer. */
  35. struct buffer *obuf;
  36. /* Command input buffer */
  37. char *buf;
  38. /* Command cursor point */
  39. int cp;
  40. /* Command length */
  41. int length;
  42. /* Command max length. */
  43. int max;
  44. /* Histry of command */
  45. char *hist[VTY_MAXHIST];
  46. /* History lookup current point */
  47. int hp;
  48. /* History insert end point */
  49. int hindex;
  50. /* For current referencing point of interface, route-map,
  51. access-list etc... */
  52. void *index;
  53. /* For multiple level index treatment such as key chain and key. */
  54. void *index_sub;
  55. /* For escape character. */
  56. unsigned char escape;
  57. /* Current vty status. */
  58. enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;
  59. /* IAC handling: was the last character received the
  60. IAC (interpret-as-command) escape character (and therefore the next
  61. character will be the command code)? Refer to Telnet RFC 854. */
  62. unsigned char iac;
  63. /* IAC SB (option subnegotiation) handling */
  64. unsigned char iac_sb_in_progress;
  65. /* At the moment, we care only about the NAWS (window size) negotiation,
  66. and that requires just a 5-character buffer (RFC 1073):
  67. <NAWS char> <16-bit width> <16-bit height> */
  68. #define TELNET_NAWS_SB_LEN 5
  69. unsigned char sb_buf[TELNET_NAWS_SB_LEN];
  70. /* How many subnegotiation characters have we received? We just drop
  71. those that do not fit in the buffer. */
  72. size_t sb_len;
  73. /* Window width/height. */
  74. int width;
  75. int height;
  76. /* Configure lines. */
  77. int lines;
  78. /* Terminal monitor. */
  79. int monitor;
  80. /* In configure mode. */
  81. int config;
  82. /* Read and write thread. */
  83. struct thread *t_read;
  84. struct thread *t_write;
  85. /* Timeout seconds and thread. */
  86. unsigned long v_timeout;
  87. struct thread *t_timeout;
  88. /* What address is this vty comming from. */
  89. char address[SU_ADDRSTRLEN];
  90. };
  91. /* Integrated configuration file. */
  92. #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
  93. /* Small macro to determine newline is newline only or linefeed needed. */
  94. #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
  95. /* Default time out value */
  96. #define VTY_TIMEOUT_DEFAULT 600
  97. /* Vty read buffer size. */
  98. #define VTY_READ_BUFSIZ 512
  99. /* Directory separator. */
  100. #ifndef DIRECTORY_SEP
  101. #define DIRECTORY_SEP '/'
  102. #endif /* DIRECTORY_SEP */
  103. #ifndef IS_DIRECTORY_SEP
  104. #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
  105. #endif
  106. /* GCC have printf type attribute check. */
  107. #ifdef __GNUC__
  108. #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
  109. #else
  110. #define PRINTF_ATTRIBUTE(a,b)
  111. #endif /* __GNUC__ */
  112. /* Utility macros to convert VTY argument to unsigned long */
  113. #define VTY_GET_ULONG(NAME,V,STR) \
  114. do { \
  115. char *endptr = NULL; \
  116. errno = 0; \
  117. (V) = strtoul ((STR), &endptr, 10); \
  118. if (*(STR) == '-' || *endptr != '\0' || errno) \
  119. { \
  120. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  121. return CMD_WARNING; \
  122. } \
  123. } while (0)
  124. /*
  125. * The logic below ((TMPL) <= ((MIN) && (TMPL) != (MIN)) is
  126. * done to circumvent the compiler complaining about
  127. * comparing unsigned numbers against zero, if MIN is zero.
  128. * NB: The compiler isn't smart enough to supress the warning
  129. * if you write (MIN) != 0 && tmpl < (MIN).
  130. */
  131. #define VTY_GET_INTEGER_RANGE_HEART(NAME,TMPL,STR,MIN,MAX) \
  132. do { \
  133. VTY_GET_ULONG(NAME, (TMPL), STR); \
  134. if ( ((TMPL) <= (MIN) && (TMPL) != (MIN)) || (TMPL) > (MAX) ) \
  135. { \
  136. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE);\
  137. return CMD_WARNING; \
  138. } \
  139. } while (0)
  140. #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
  141. do { \
  142. unsigned long tmpl; \
  143. VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
  144. (V) = tmpl; \
  145. } while (0)
  146. #define VTY_CHECK_INTEGER_RANGE(NAME,STR,MIN,MAX) \
  147. do { \
  148. unsigned long tmpl; \
  149. VTY_GET_INTEGER_RANGE_HEART(NAME,tmpl,STR,MIN,MAX); \
  150. } while (0)
  151. #define VTY_GET_INTEGER(NAME,V,STR) \
  152. VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
  153. #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
  154. do { \
  155. int retv; \
  156. retv = inet_aton ((STR), &(V)); \
  157. if (!retv) \
  158. { \
  159. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  160. return CMD_WARNING; \
  161. } \
  162. } while (0)
  163. #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
  164. do { \
  165. int retv; \
  166. retv = str2prefix_ipv4 ((STR), &(V)); \
  167. if (retv <= 0) \
  168. { \
  169. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  170. return CMD_WARNING; \
  171. } \
  172. } while (0)
  173. /* Exported variables */
  174. extern char integrate_default[];
  175. /* Prototypes. */
  176. extern void vty_init (struct thread_master *);
  177. extern void vty_init_vtysh (void);
  178. extern void vty_terminate (void);
  179. extern void vty_reset (void);
  180. extern struct vty *vty_new (void);
  181. extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
  182. extern void vty_read_config (char *, char *);
  183. extern void vty_time_print (struct vty *, int);
  184. extern void vty_serv_sock (const char *, unsigned short, const char *);
  185. extern void vty_close (struct vty *);
  186. extern char *vty_get_cwd (void);
  187. extern void vty_log (const char *level, const char *proto,
  188. const char *fmt, struct timestamp_control *, va_list);
  189. extern int vty_config_lock (struct vty *);
  190. extern int vty_config_unlock (struct vty *);
  191. extern int vty_shell (struct vty *);
  192. extern int vty_shell_serv (struct vty *);
  193. extern void vty_hello (struct vty *);
  194. /* Send a fixed-size message to all vty terminal monitors; this should be
  195. an async-signal-safe function. */
  196. extern void vty_log_fixed (char *buf, size_t len);
  197. #endif /* _ZEBRA_VTY_H */