clish_config_callback.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "private.h"
  15. #include "konf/net.h"
  16. #include "konf/buf.h"
  17. #include "konf/query.h"
  18. #include "clish/variable.h"
  19. static int send_request(konf_client_t * client, char *command);
  20. /*--------------------------------------------------------- */
  21. bool_t clish_config_callback(const clish_shell_t * this,
  22. const clish_command_t * cmd, clish_pargv_t * pargv)
  23. {
  24. unsigned i;
  25. char *command = NULL;
  26. konf_client_t *client;
  27. konf_buf_t *buf = NULL;
  28. const char *viewid = NULL;
  29. char *str = NULL;
  30. char tmp[PATH_MAX + 100];
  31. clish_config_operation_t op;
  32. if (!this)
  33. return BOOL_TRUE;
  34. viewid = clish_shell__get_viewid(this);
  35. op = clish_command__get_cfg_op(cmd);
  36. switch (op) {
  37. case CLISH_CONFIG_NONE:
  38. return BOOL_TRUE;
  39. case CLISH_CONFIG_SET:
  40. /* Add set operation */
  41. lub_string_cat(&command, "-s");
  42. /* Add entered line */
  43. str = clish_variable__get_line(cmd, pargv);
  44. lub_string_cat(&command, " -l \"");
  45. lub_string_cat(&command, str);
  46. lub_string_cat(&command, "\"");
  47. lub_string_free(str);
  48. /* Add splitter */
  49. if (clish_command__get_splitter(cmd) == BOOL_FALSE)
  50. lub_string_cat(&command, " -i");
  51. /* Add unique */
  52. if (clish_command__get_unique(cmd) == BOOL_FALSE)
  53. lub_string_cat(&command, " -n");
  54. break;
  55. case CLISH_CONFIG_UNSET:
  56. /* Add unset operation */
  57. lub_string_cat(&command, "-u");
  58. break;
  59. case CLISH_CONFIG_DUMP:
  60. /* Add dump operation */
  61. lub_string_cat(&command, "-d");
  62. /* Add filename */
  63. str = clish_command__get_file(cmd, viewid, pargv);
  64. if (str) {
  65. lub_string_cat(&command, " -f \"");
  66. if (str[0] != '\0')
  67. lub_string_cat(&command, str);
  68. else
  69. lub_string_cat(&command, "/tmp/running-config");
  70. lub_string_cat(&command, "\"");
  71. lub_string_free(str);
  72. }
  73. break;
  74. default:
  75. return BOOL_FALSE;
  76. };
  77. /* Add pattern */
  78. if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
  79. str = clish_command__get_pattern(cmd, viewid, pargv);
  80. if (!str) {
  81. lub_string_free(command);
  82. return BOOL_FALSE;
  83. }
  84. lub_string_cat(&command, " -r \"");
  85. lub_string_cat(&command, str);
  86. lub_string_cat(&command, "\"");
  87. lub_string_free(str);
  88. }
  89. /* Add priority */
  90. if (clish_command__get_priority(cmd) != 0) {
  91. snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
  92. clish_command__get_priority(cmd));
  93. tmp[sizeof(tmp) - 1] = '\0';
  94. lub_string_cat(&command, tmp);
  95. }
  96. /* Add sequence */
  97. if (clish_command__is_seq(cmd)) {
  98. snprintf(tmp, sizeof(tmp) - 1, " -q %u",
  99. clish_command__get_seq(cmd,
  100. viewid, pargv));
  101. tmp[sizeof(tmp) - 1] = '\0';
  102. lub_string_cat(&command, tmp);
  103. }
  104. /* Add pwd */
  105. str = clish_shell__get_pwd_full(this,
  106. clish_command__get_cfg_depth(cmd, viewid, pargv));
  107. if (str) {
  108. lub_string_cat(&command, " ");
  109. lub_string_cat(&command, str);
  110. lub_string_free(str);
  111. }
  112. client = clish_shell__get_client(this);
  113. #ifdef DEBUG
  114. fprintf(stderr, "CONFIG request: %s\n", command);
  115. #endif
  116. if (send_request(client, command) < 0) {
  117. fprintf(stderr, "Cannot write to the running-config.\n");
  118. }
  119. if (konf_client_recv_answer(client, &buf) < 0) {
  120. fprintf(stderr, "The error while request to the config daemon.\n");
  121. }
  122. lub_string_free(command);
  123. /* Postprocessing. Get data from daemon etc. */
  124. switch (op) {
  125. case CLISH_CONFIG_DUMP:
  126. if (buf) {
  127. konf_buf_lseek(buf, 0);
  128. while ((str = konf_buf_preparse(buf))) {
  129. if (strlen(str) == 0) {
  130. lub_string_free(str);
  131. break;
  132. }
  133. fprintf(clish_shell__get_ostream(this),
  134. "%s\n", str);
  135. lub_string_free(str);
  136. }
  137. konf_buf_delete(buf);
  138. }
  139. break;
  140. default:
  141. break;
  142. };
  143. return BOOL_TRUE;
  144. }
  145. /*--------------------------------------------------------- */
  146. static int send_request(konf_client_t * client, char *command)
  147. {
  148. if ((konf_client_connect(client) < 0))
  149. return -1;
  150. if (konf_client_send(client, command) < 0) {
  151. if (konf_client_reconnect(client) < 0)
  152. return -1;
  153. if (konf_client_send(client, command) < 0)
  154. return -1;
  155. }
  156. return 0;
  157. }