1
0

net.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifndef UNIX_PATH_MAX
  17. #define UNIX_PATH_MAX 108
  18. #endif
  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. raddr.sun_family = AF_UNIX;
  56. strncpy(raddr.sun_path, this->path, UNIX_PATH_MAX);
  57. raddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
  58. if (connect(this->sock, (struct sockaddr *)&raddr, sizeof(raddr))) {
  59. close(this->sock);
  60. this->sock = -1;
  61. }
  62. return this->sock;
  63. }
  64. /*--------------------------------------------------------- */
  65. void konf_client_disconnect(konf_client_t *this)
  66. {
  67. if (this->sock >= 0) {
  68. close(this->sock);
  69. this->sock = -1;
  70. }
  71. }
  72. /*--------------------------------------------------------- */
  73. int konf_client_reconnect(konf_client_t *this)
  74. {
  75. konf_client_disconnect(this);
  76. return konf_client_connect(this);
  77. }
  78. /*--------------------------------------------------------- */
  79. int konf_client_send(konf_client_t *this, char *command)
  80. {
  81. if (this->sock < 0)
  82. return this->sock;
  83. return send(this->sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  84. }
  85. /*--------------------------------------------------------- */
  86. int konf_client__get_sock(konf_client_t *this)
  87. {
  88. return this->sock;
  89. }
  90. /*--------------------------------------------------------- */
  91. konf_buf_t * konf_client_recv_data(konf_client_t * this, konf_buf_t *buf)
  92. {
  93. int processed = 0;
  94. konf_buf_t *data;
  95. char *str;
  96. /* Check if socked is connected */
  97. if ((konf_client_connect(this) < 0))
  98. return NULL;
  99. data = konf_buf_new(konf_client__get_sock(this));
  100. do {
  101. while ((str = konf_buf_parse(buf))) {
  102. konf_buf_add(data, str, strlen(str) + 1);
  103. if (strlen(str) == 0) {
  104. processed = 1;
  105. free(str);
  106. break;
  107. }
  108. free(str);
  109. }
  110. } while ((!processed) && (konf_buf_read(buf)) > 0);
  111. if (!processed) {
  112. konf_buf_delete(data);
  113. return NULL;
  114. }
  115. return data;
  116. }
  117. /*--------------------------------------------------------- */
  118. static int process_answer(konf_client_t * this, char *str, konf_buf_t *buf, konf_buf_t **data)
  119. {
  120. int res;
  121. konf_query_t *query;
  122. /* Parse query */
  123. query = konf_query_new();
  124. res = konf_query_parse_str(query, str);
  125. if (res < 0) {
  126. konf_query_free(query);
  127. #ifdef DEBUG
  128. fprintf(stderr, "CONFIG error: Cannot parse answer string.\n");
  129. #endif
  130. return -1;
  131. }
  132. #ifdef DEBUG
  133. fprintf(stderr, "ANSWER: %s\n", str);
  134. /* konf_query_dump(query);
  135. */
  136. #endif
  137. switch (konf_query__get_op(query)) {
  138. case KONF_QUERY_OP_OK:
  139. res = 0;
  140. break;
  141. case KONF_QUERY_OP_ERROR:
  142. res = -1;
  143. break;
  144. case KONF_QUERY_OP_STREAM:
  145. if (!(*data = konf_client_recv_data(this, buf)))
  146. res = -1;
  147. else
  148. res = 1; /* wait for another answer */
  149. break;
  150. default:
  151. res = -1;
  152. break;
  153. }
  154. /* Free resources */
  155. konf_query_free(query);
  156. return res;
  157. }
  158. /*--------------------------------------------------------- */
  159. int konf_client_recv_answer(konf_client_t * this, konf_buf_t **data)
  160. {
  161. konf_buf_t *buf;
  162. int nbytes;
  163. char *str;
  164. int retval = 0;
  165. int processed = 0;
  166. if ((konf_client_connect(this) < 0))
  167. return -1;
  168. buf = konf_buf_new(konf_client__get_sock(this));
  169. while ((!processed) && (nbytes = konf_buf_read(buf)) > 0) {
  170. while ((str = konf_buf_parse(buf))) {
  171. konf_buf_t *tmpdata = NULL;
  172. retval = process_answer(this, str, buf, &tmpdata);
  173. free(str);
  174. if (retval < 0) {
  175. konf_buf_delete(buf);
  176. return retval;
  177. }
  178. if (retval == 0)
  179. processed = 1;
  180. if (tmpdata) {
  181. if (*data)
  182. konf_buf_delete(*data);
  183. *data = tmpdata;
  184. }
  185. }
  186. }
  187. konf_buf_delete(buf);
  188. return retval;
  189. }