clish_config_callback.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. static int receive_answer(konf_client_t * client, konf_buf_t **data);
  21. static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data);
  22. /*--------------------------------------------------------- */
  23. bool_t
  24. clish_config_callback(const clish_shell_t * this,
  25. const clish_command_t * cmd, clish_pargv_t * pargv)
  26. {
  27. unsigned i;
  28. char *command = NULL;
  29. konf_client_t *client;
  30. konf_buf_t *buf = NULL;
  31. const char *viewid = NULL;
  32. char *str = NULL;
  33. char tmp[PATH_MAX + 100];
  34. clish_config_operation_t op;
  35. if (!this)
  36. return BOOL_TRUE;
  37. viewid = clish_shell__get_viewid(this);
  38. op = clish_command__get_cfg_op(cmd);
  39. switch (op) {
  40. case CLISH_CONFIG_NONE:
  41. return BOOL_TRUE;
  42. case CLISH_CONFIG_SET:
  43. /* Add set operation */
  44. lub_string_cat(&command, "-s");
  45. /* Add entered line */
  46. str = clish_variable__get_line(cmd, pargv);
  47. lub_string_cat(&command, " -l \"");
  48. lub_string_cat(&command, str);
  49. lub_string_cat(&command, "\"");
  50. lub_string_free(str);
  51. /* Add splitter */
  52. if (clish_command__get_splitter(cmd) == BOOL_FALSE)
  53. lub_string_cat(&command, " -i");
  54. /* Add unique */
  55. if (clish_command__get_unique(cmd) == BOOL_FALSE)
  56. lub_string_cat(&command, " -n");
  57. break;
  58. case CLISH_CONFIG_UNSET:
  59. /* Add unset operation */
  60. lub_string_cat(&command, "-u");
  61. break;
  62. case CLISH_CONFIG_DUMP:
  63. /* Add dump operation */
  64. lub_string_cat(&command, "-d");
  65. /* Add filename */
  66. str = clish_command__get_file(cmd, viewid, pargv);
  67. if (str) {
  68. lub_string_cat(&command, " -f \"");
  69. if (str[0] != '\0')
  70. lub_string_cat(&command, str);
  71. else
  72. lub_string_cat(&command, "/tmp/running-config");
  73. lub_string_cat(&command, "\"");
  74. lub_string_free(str);
  75. }
  76. break;
  77. default:
  78. return BOOL_FALSE;
  79. };
  80. /* Add pattern */
  81. if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
  82. str = clish_command__get_pattern(cmd, viewid, pargv);
  83. if (!str) {
  84. lub_string_free(command);
  85. return BOOL_FALSE;
  86. }
  87. lub_string_cat(&command, " -r \"");
  88. lub_string_cat(&command, str);
  89. lub_string_cat(&command, "\"");
  90. lub_string_free(str);
  91. }
  92. /* Add priority */
  93. if (clish_command__get_priority(cmd) != 0) {
  94. snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
  95. clish_command__get_priority(cmd));
  96. tmp[sizeof(tmp) - 1] = '\0';
  97. lub_string_cat(&command, tmp);
  98. }
  99. /* Add sequence */
  100. if (clish_command__is_seq(cmd)) {
  101. snprintf(tmp, sizeof(tmp) - 1, " -q %u",
  102. clish_command__get_seq(cmd,
  103. viewid, pargv));
  104. tmp[sizeof(tmp) - 1] = '\0';
  105. lub_string_cat(&command, tmp);
  106. }
  107. /* Add pwd */
  108. str = clish_shell__get_pwd_full(this,
  109. clish_command__get_cfg_depth(cmd, viewid, pargv));
  110. if (str) {
  111. lub_string_cat(&command, " ");
  112. lub_string_cat(&command, str);
  113. lub_string_free(str);
  114. }
  115. client = clish_shell__get_client(this);
  116. #ifdef DEBUG
  117. fprintf(stderr, "CONFIG request: %s\n", command);
  118. #endif
  119. if (send_request(client, command) < 0) {
  120. fprintf(stderr, "Cannot write to the running-config.\n");
  121. }
  122. if (receive_answer(client, &buf) < 0) {
  123. fprintf(stderr, "Cannot get answer from config daemon.\n");
  124. }
  125. lub_string_free(command);
  126. /* Postprocessing. Get data from daemon etc. */
  127. switch (op) {
  128. case CLISH_CONFIG_DUMP:
  129. if (buf) {
  130. konf_buf_lseek(buf, 0);
  131. while ((str = konf_buf_preparse(buf))) {
  132. if (strlen(str) == 0) {
  133. lub_string_free(str);
  134. break;
  135. }
  136. fprintf(clish_shell__get_ostream(this),
  137. "%s\n", str);
  138. lub_string_free(str);
  139. }
  140. konf_buf_delete(buf);
  141. }
  142. break;
  143. default:
  144. break;
  145. };
  146. return BOOL_TRUE;
  147. }
  148. /*--------------------------------------------------------- */
  149. static int send_request(konf_client_t * client, char *command)
  150. {
  151. if ((konf_client_connect(client) < 0))
  152. return -1;
  153. if (konf_client_send(client, command) < 0) {
  154. if (konf_client_reconnect(client) < 0)
  155. return -1;
  156. if (konf_client_send(client, command) < 0)
  157. return -1;
  158. }
  159. return 0;
  160. }
  161. static int receive_answer(konf_client_t * client, konf_buf_t **data)
  162. {
  163. konf_buf_t *buf;
  164. int nbytes;
  165. char *str;
  166. int retval = 0;
  167. int processed = 0;
  168. if ((konf_client_connect(client) < 0))
  169. return -1;
  170. buf = konf_buf_new(konf_client__get_sock(client));
  171. while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
  172. while ((str = konf_buf_parse(buf))) {
  173. konf_buf_t *tmpdata = NULL;
  174. retval = process_answer(client, str, buf, &tmpdata);
  175. lub_string_free(str);
  176. if (retval < 0) {
  177. konf_buf_delete(buf);
  178. return retval;
  179. }
  180. if (retval == 0)
  181. processed = 1;
  182. if (tmpdata) {
  183. if (*data)
  184. konf_buf_delete(*data);
  185. *data = tmpdata;
  186. }
  187. }
  188. }
  189. konf_buf_delete(buf);
  190. return retval;
  191. }
  192. static int process_answer(konf_client_t * client, char *str, konf_buf_t *buf, konf_buf_t **data)
  193. {
  194. int res;
  195. konf_query_t *query;
  196. /* Parse query */
  197. query = konf_query_new();
  198. res = konf_query_parse_str(query, str);
  199. if (res < 0) {
  200. konf_query_free(query);
  201. #ifdef DEBUG
  202. fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
  203. #endif
  204. return -1;
  205. }
  206. #ifdef DEBUG
  207. fprintf(stderr, "CONFIG answer: %s\n", str);
  208. // konf_query_dump(query);
  209. #endif
  210. switch (konf_query__get_op(query)) {
  211. case KONF_QUERY_OP_OK:
  212. res = 0;
  213. break;
  214. case KONF_QUERY_OP_ERROR:
  215. res = -1;
  216. break;
  217. case KONF_QUERY_OP_STREAM:
  218. if (!(*data = konf_client_recv_data(client, buf)))
  219. res = -1;
  220. else
  221. res = 1; /* wait for another answer */
  222. break;
  223. default:
  224. res = -1;
  225. break;
  226. }
  227. /* Free resources */
  228. konf_query_free(query);
  229. return res;
  230. }