net.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "clish/private.h"
  12. #include "lub/string.h"
  13. #include "private.h"
  14. #ifndef UNIX_PATH_MAX
  15. #define UNIX_PATH_MAX 108
  16. #endif
  17. /*--------------------------------------------------------- */
  18. konf_client_t *konf_client_new(const char *path)
  19. {
  20. konf_client_t *client;
  21. if (!path)
  22. return NULL;
  23. if (!(client = malloc(sizeof(*client))))
  24. return NULL;
  25. client->sock = -1; /* socket is not created yet */
  26. client->path = lub_string_dup(path);
  27. return client;
  28. }
  29. /*--------------------------------------------------------- */
  30. void konf_client_free(konf_client_t *client)
  31. {
  32. if (!client)
  33. return;
  34. if (client->sock != -1)
  35. konf_client_disconnect(client);
  36. lub_string_free(client->path);
  37. free(client);
  38. }
  39. /*--------------------------------------------------------- */
  40. int konf_client_connect(konf_client_t *client)
  41. {
  42. struct sockaddr_un raddr;
  43. if (client->sock >= 0)
  44. return client->sock;
  45. if ((client->sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
  46. return client->sock;
  47. raddr.sun_family = AF_UNIX;
  48. strncpy(raddr.sun_path, client->path, UNIX_PATH_MAX);
  49. raddr.sun_path[UNIX_PATH_MAX - 1] = '\0';
  50. if (connect(client->sock, (struct sockaddr *)&raddr, sizeof(raddr))) {
  51. close(client->sock);
  52. client->sock = -1;
  53. }
  54. return client->sock;
  55. }
  56. /*--------------------------------------------------------- */
  57. void konf_client_disconnect(konf_client_t *client)
  58. {
  59. if (client->sock >= 0) {
  60. close(client->sock);
  61. client->sock = -1;
  62. }
  63. }
  64. /*--------------------------------------------------------- */
  65. int konf_client_reconnect(konf_client_t *client)
  66. {
  67. konf_client_disconnect(client);
  68. return konf_client_connect(client);
  69. }
  70. /*--------------------------------------------------------- */
  71. int konf_client_send(konf_client_t *client, char *command)
  72. {
  73. if (client->sock < 0)
  74. return client->sock;
  75. return send(client->sock, command, strlen(command) + 1, MSG_NOSIGNAL);
  76. }
  77. /*--------------------------------------------------------- */
  78. int konf_client__get_sock(konf_client_t *client)
  79. {
  80. return client->sock;
  81. }
  82. /*--------------------------------------------------------- */
  83. konf_buf_t * konf_client_recv_data(konf_client_t * this, konf_buf_t *buf)
  84. {
  85. int processed = 0;
  86. konf_buf_t *data;
  87. char *str;
  88. /* Check if socked is connected */
  89. if ((konf_client_connect(this) < 0))
  90. return NULL;
  91. data = konf_buf_new(konf_client__get_sock(this));
  92. do {
  93. while ((str = konf_buf_parse(buf))) {
  94. konf_buf_add(data, str, strlen(str) + 1);
  95. if (strlen(str) == 0) {
  96. processed = 1;
  97. lub_string_free(str);
  98. break;
  99. }
  100. lub_string_free(str);
  101. }
  102. } while ((!processed) && (konf_buf_read(buf)) > 0);
  103. if (!processed) {
  104. konf_buf_delete(data);
  105. return NULL;
  106. }
  107. return data;
  108. }