1
0

net.c 4.7 KB

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