net.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <errno.h>
  7. #include <assert.h>
  8. #include <sys/socket.h>
  9. #include <string.h>
  10. #include <sys/un.h>
  11. #include "konf/buf.h"
  12. #include "konf/query.h"
  13. #include "lub/string.h"
  14. #include "private.h"
  15. /* Socket name in filesystem */
  16. #ifndef UNIX_PATH_MAX
  17. #define UNIX_PATH_MAX 108
  18. #endif
  19. /*--------------------------------------------------------- */
  20. konf_client_t *konf_client_new(const char *path)
  21. {
  22. konf_client_t *this;
  23. if (!path)
  24. return NULL;
  25. if (!(this = malloc(sizeof(*this))))
  26. return NULL;
  27. this->sock = -1; /* socket is not created yet */
  28. this->path = lub_string_dup(path);
  29. return this;
  30. }
  31. /*--------------------------------------------------------- */
  32. void konf_client_free(konf_client_t *this)
  33. {
  34. if (!this)
  35. return;
  36. if (this->sock != -1)
  37. konf_client_disconnect(this);
  38. lub_string_free(this->path);
  39. free(this);
  40. }
  41. /*--------------------------------------------------------- */
  42. int konf_client_connect(konf_client_t *this)
  43. {
  44. struct sockaddr_un raddr;
  45. if (this->sock >= 0)
  46. return this->sock;
  47. if ((this->sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
  48. return this->sock;
  49. raddr.sun_family = AF_UNIX;
  50. strncpy(raddr.sun_path, this->path, UNIX_PATH_MAX);
  51. raddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
  52. if (connect(this->sock, (struct sockaddr *)&raddr, sizeof(raddr))) {
  53. close(this->sock);
  54. this->sock = -1;
  55. }
  56. return this->sock;
  57. }
  58. /*--------------------------------------------------------- */
  59. void konf_client_disconnect(konf_client_t *this)
  60. {
  61. if (this->sock >= 0) {
  62. close(this->sock);
  63. this->sock = -1;
  64. }
  65. }
  66. /*--------------------------------------------------------- */
  67. int konf_client_reconnect(konf_client_t *this)
  68. {
  69. konf_client_disconnect(this);
  70. return konf_client_connect(this);
  71. }
  72. /*--------------------------------------------------------- */
  73. int konf_client_send(konf_client_t *this, char *command)
  74. {
  75. if (this->sock < 0)
  76. return this->sock;
  77. return send(this->sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  78. }
  79. /*--------------------------------------------------------- */
  80. int konf_client__get_sock(konf_client_t *this)
  81. {
  82. return this->sock;
  83. }
  84. /*--------------------------------------------------------- */
  85. konf_buf_t * konf_client_recv_data(konf_client_t * this, konf_buf_t *buf)
  86. {
  87. int processed = 0;
  88. konf_buf_t *data;
  89. char *str;
  90. /* Check if socked is connected */
  91. if ((konf_client_connect(this) < 0))
  92. return NULL;
  93. data = konf_buf_new(konf_client__get_sock(this));
  94. do {
  95. while ((str = konf_buf_parse(buf))) {
  96. konf_buf_add(data, str, strlen(str) + 1);
  97. if (strlen(str) == 0) {
  98. processed = 1;
  99. lub_string_free(str);
  100. break;
  101. }
  102. lub_string_free(str);
  103. }
  104. } while ((!processed) && (konf_buf_read(buf)) > 0);
  105. if (!processed) {
  106. konf_buf_delete(data);
  107. return NULL;
  108. }
  109. return data;
  110. }
  111. /*--------------------------------------------------------- */
  112. static int process_answer(konf_client_t * this, char *str, konf_buf_t *buf, konf_buf_t **data)
  113. {
  114. int res;
  115. konf_query_t *query;
  116. /* Parse query */
  117. query = konf_query_new();
  118. res = konf_query_parse_str(query, str);
  119. if (res < 0) {
  120. konf_query_free(query);
  121. #ifdef DEBUG
  122. fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
  123. #endif
  124. return -1;
  125. }
  126. #ifdef DEBUG
  127. fprintf(stderr, "ANSWER: %s\n", str);
  128. /* konf_query_dump(query);
  129. */
  130. #endif
  131. switch (konf_query__get_op(query)) {
  132. case KONF_QUERY_OP_OK:
  133. res = 0;
  134. break;
  135. case KONF_QUERY_OP_ERROR:
  136. res = -1;
  137. break;
  138. case KONF_QUERY_OP_STREAM:
  139. if (!(*data = konf_client_recv_data(this, buf)))
  140. res = -1;
  141. else
  142. res = 1; /* wait for another answer */
  143. break;
  144. default:
  145. res = -1;
  146. break;
  147. }
  148. /* Free resources */
  149. konf_query_free(query);
  150. return res;
  151. }
  152. /*--------------------------------------------------------- */
  153. int konf_client_recv_answer(konf_client_t * this, konf_buf_t **data)
  154. {
  155. konf_buf_t *buf;
  156. int nbytes;
  157. char *str;
  158. int retval = 0;
  159. int processed = 0;
  160. if ((konf_client_connect(this) < 0))
  161. return -1;
  162. buf = konf_buf_new(konf_client__get_sock(this));
  163. while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
  164. while ((str = konf_buf_parse(buf))) {
  165. konf_buf_t *tmpdata = NULL;
  166. retval = process_answer(this, str, buf, &tmpdata);
  167. lub_string_free(str);
  168. if (retval < 0) {
  169. konf_buf_delete(buf);
  170. return retval;
  171. }
  172. if (retval == 0)
  173. processed = 1;
  174. if (tmpdata) {
  175. if (*data)
  176. konf_buf_delete(*data);
  177. *data = tmpdata;
  178. }
  179. }
  180. }
  181. konf_buf_delete(buf);
  182. return retval;
  183. }