vty.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #define VTY_BUFSIZ 512
  21. #define VTY_MAXHIST 20
  22. /* VTY struct. */
  23. struct vty
  24. {
  25. /* File descripter of this vty. */
  26. int fd;
  27. /* Is this vty connect to file or not */
  28. enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;
  29. /* Node status of this vty */
  30. int node;
  31. /* What address is this vty comming from. */
  32. char *address;
  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. };
  90. /* Integrated configuration file. */
  91. #define INTEGRATE_DEFAULT_CONFIG "Quagga.conf"
  92. /* Small macro to determine newline is newline only or linefeed needed. */
  93. #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n")
  94. /* Default time out value */
  95. #define VTY_TIMEOUT_DEFAULT 600
  96. /* Vty read buffer size. */
  97. #define VTY_READ_BUFSIZ 512
  98. /* Directory separator. */
  99. #ifndef DIRECTORY_SEP
  100. #define DIRECTORY_SEP '/'
  101. #endif /* DIRECTORY_SEP */
  102. #ifndef IS_DIRECTORY_SEP
  103. #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
  104. #endif
  105. /* GCC have printf type attribute check. */
  106. #ifdef __GNUC__
  107. #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
  108. #else
  109. #define PRINTF_ATTRIBUTE(a,b)
  110. #endif /* __GNUC__ */
  111. /* Utility macros to convert VTY argument to unsigned long or integer. */
  112. #define VTY_GET_LONG(NAME,V,STR) \
  113. do { \
  114. char *endptr = NULL; \
  115. (V) = strtoul ((STR), &endptr, 10); \
  116. if (*endptr != '\0' || (V) == ULONG_MAX) \
  117. { \
  118. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  119. return CMD_WARNING; \
  120. } \
  121. } while (0)
  122. #define VTY_GET_INTEGER_RANGE(NAME,V,STR,MIN,MAX) \
  123. do { \
  124. unsigned long tmpl; \
  125. VTY_GET_LONG(NAME, tmpl, STR); \
  126. if ( (tmpl < (MIN)) || (tmpl > (MAX))) \
  127. { \
  128. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  129. return CMD_WARNING; \
  130. } \
  131. (V) = tmpl; \
  132. } while (0)
  133. #define VTY_GET_INTEGER(NAME,V,STR) \
  134. VTY_GET_INTEGER_RANGE(NAME,V,STR,0U,UINT32_MAX)
  135. #define VTY_GET_IPV4_ADDRESS(NAME,V,STR) \
  136. do { \
  137. int retv; \
  138. retv = inet_aton ((STR), &(V)); \
  139. if (!retv) \
  140. { \
  141. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  142. return CMD_WARNING; \
  143. } \
  144. } while (0)
  145. #define VTY_GET_IPV4_PREFIX(NAME,V,STR) \
  146. do { \
  147. int retv; \
  148. retv = str2prefix_ipv4 ((STR), &(V)); \
  149. if (retv <= 0) \
  150. { \
  151. vty_out (vty, "%% Invalid %s value%s", NAME, VTY_NEWLINE); \
  152. return CMD_WARNING; \
  153. } \
  154. } while (0)
  155. /* Exported variables */
  156. extern char integrate_default[];
  157. /* Prototypes. */
  158. extern void vty_init (struct thread_master *);
  159. extern void vty_init_vtysh (void);
  160. extern void vty_terminate (void);
  161. extern void vty_reset (void);
  162. extern struct vty *vty_new (void);
  163. extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
  164. extern void vty_read_config (char *, char *);
  165. extern void vty_time_print (struct vty *, int);
  166. extern void vty_serv_sock (const char *, unsigned short, const char *);
  167. extern void vty_close (struct vty *);
  168. extern char *vty_get_cwd (void);
  169. extern void vty_log (const char *level, const char *proto,
  170. const char *fmt, struct timestamp_control *, va_list);
  171. extern int vty_config_lock (struct vty *);
  172. extern int vty_config_unlock (struct vty *);
  173. extern int vty_shell (struct vty *);
  174. extern int vty_shell_serv (struct vty *);
  175. extern void vty_hello (struct vty *);
  176. /* Send a fixed-size message to all vty terminal monitors; this should be
  177. an async-signal-safe function. */
  178. extern void vty_log_fixed (const char *buf, size_t len);
  179. #endif /* _ZEBRA_VTY_H */