1
0

net.c 4.6 KB

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