callback_config.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * clish_config_callback.c
  3. *
  4. *
  5. * Callback hook to execute config operations.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/types.h>
  10. #include <assert.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. #include <limits.h>
  14. #include <string.h>
  15. #include "internal.h"
  16. #include "konf/net.h"
  17. #include "konf/buf.h"
  18. #include "konf/query.h"
  19. #include "lub/string.h"
  20. static int send_request(konf_client_t * client, char *command);
  21. static unsigned short str2ushort(const char *str)
  22. {
  23. unsigned short num = 0;
  24. if (str && (*str != '\0')) {
  25. long val = 0;
  26. char *endptr;
  27. val = strtol(str, &endptr, 0);
  28. if (endptr == str)
  29. num = 0;
  30. else if (val > 0xffff)
  31. num = 0xffff;
  32. else if (val < 0)
  33. num = 0;
  34. else
  35. num = (unsigned)val;
  36. }
  37. return num;
  38. }
  39. /*--------------------------------------------------------- */
  40. bool_t clish_config_callback(clish_context_t *context)
  41. {
  42. clish_shell_t *this = context->shell;
  43. const clish_command_t *cmd = context->cmd;
  44. clish_pargv_t *pargv = context->pargv;
  45. clish_config_t *config;
  46. char *command = NULL;
  47. konf_client_t *client;
  48. konf_buf_t *buf = NULL;
  49. const char *viewid = NULL;
  50. char *str = NULL;
  51. char tmp[PATH_MAX + 100];
  52. clish_config_op_t op;
  53. unsigned int num;
  54. if (!this)
  55. return BOOL_TRUE;
  56. client = clish_shell__get_client(this);
  57. if (!client)
  58. return BOOL_TRUE;
  59. viewid = clish_shell__get_viewid(this);
  60. config = clish_command__get_config(cmd);
  61. op = clish_config__get_op(config);
  62. switch (op) {
  63. case CLISH_CONFIG_NONE:
  64. return BOOL_TRUE;
  65. case CLISH_CONFIG_SET:
  66. /* Add set operation */
  67. lub_string_cat(&command, "-s");
  68. /* Add entered line */
  69. str = clish_shell__get_line(cmd, pargv);
  70. lub_string_cat(&command, " -l \"");
  71. lub_string_cat(&command, str);
  72. lub_string_cat(&command, "\"");
  73. lub_string_free(str);
  74. /* Add splitter */
  75. if (!clish_config__get_splitter(config))
  76. lub_string_cat(&command, " -i");
  77. /* Add unique */
  78. if (!clish_config__get_unique(config))
  79. lub_string_cat(&command, " -n");
  80. break;
  81. case CLISH_CONFIG_UNSET:
  82. /* Add unset operation */
  83. lub_string_cat(&command, "-u");
  84. break;
  85. case CLISH_CONFIG_DUMP:
  86. /* Add dump operation */
  87. lub_string_cat(&command, "-d");
  88. /* Add filename */
  89. str = clish_shell_expand(clish_config__get_file(config), context);
  90. if (str) {
  91. lub_string_cat(&command, " -f \"");
  92. if (str[0] != '\0')
  93. lub_string_cat(&command, str);
  94. else
  95. lub_string_cat(&command, "/tmp/running-config");
  96. lub_string_cat(&command, "\"");
  97. lub_string_free(str);
  98. }
  99. break;
  100. default:
  101. return BOOL_FALSE;
  102. };
  103. /* Add pattern */
  104. if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
  105. str = clish_shell_expand(clish_config__get_pattern(config), context);
  106. if (!str) {
  107. lub_string_free(command);
  108. return BOOL_FALSE;
  109. }
  110. lub_string_cat(&command, " -r \"");
  111. lub_string_cat(&command, str);
  112. lub_string_cat(&command, "\"");
  113. lub_string_free(str);
  114. }
  115. /* Add priority */
  116. if (clish_config__get_priority(config) != 0) {
  117. snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
  118. clish_config__get_priority(config));
  119. tmp[sizeof(tmp) - 1] = '\0';
  120. lub_string_cat(&command, tmp);
  121. }
  122. /* Add sequence */
  123. if (clish_config__get_seq(config)) {
  124. str = clish_shell_expand(clish_config__get_seq(config), context);
  125. snprintf(tmp, sizeof(tmp) - 1, " -q %u", str2ushort(str));
  126. tmp[sizeof(tmp) - 1] = '\0';
  127. lub_string_cat(&command, tmp);
  128. lub_string_free(str);
  129. }
  130. /* Add pwd */
  131. if (clish_config__get_depth(config)) {
  132. str = clish_shell_expand(clish_config__get_depth(config), context);
  133. num = str2ushort(str);
  134. lub_string_free(str);
  135. } else {
  136. num = clish_command__get_depth(cmd);
  137. }
  138. str = clish_shell__get_pwd_full(this, num);
  139. if (str) {
  140. lub_string_cat(&command, " ");
  141. lub_string_cat(&command, str);
  142. lub_string_free(str);
  143. }
  144. #ifdef DEBUG
  145. fprintf(stderr, "CONFIG request: %s\n", command);
  146. #endif
  147. if (send_request(client, command) < 0) {
  148. fprintf(stderr, "Cannot write to the running-config.\n");
  149. }
  150. if (konf_client_recv_answer(client, &buf) < 0) {
  151. fprintf(stderr, "The error while request to the config daemon.\n");
  152. }
  153. lub_string_free(command);
  154. /* Postprocessing. Get data from daemon etc. */
  155. switch (op) {
  156. case CLISH_CONFIG_DUMP:
  157. if (buf) {
  158. konf_buf_lseek(buf, 0);
  159. while ((str = konf_buf_preparse(buf))) {
  160. if (strlen(str) == 0) {
  161. lub_string_free(str);
  162. break;
  163. }
  164. fprintf(clish_shell__get_ostream(this),
  165. "%s\n", str);
  166. lub_string_free(str);
  167. }
  168. konf_buf_delete(buf);
  169. }
  170. break;
  171. default:
  172. break;
  173. };
  174. return BOOL_TRUE;
  175. }
  176. /*--------------------------------------------------------- */
  177. static int send_request(konf_client_t * client, char *command)
  178. {
  179. if ((konf_client_connect(client) < 0))
  180. return -1;
  181. if (konf_client_send(client, command) < 0) {
  182. if (konf_client_reconnect(client) < 0)
  183. return -1;
  184. if (konf_client_send(client, command) < 0)
  185. return -1;
  186. }
  187. return 0;
  188. }