1
0

clish_config_callback.c 6.5 KB

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