ncd-request.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * @file ncd-request.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <misc/string_begins_with.h>
  34. #include <base/BLog.h>
  35. #include <base/DebugObject.h>
  36. #include <system/BNetwork.h>
  37. #include <system/BReactor.h>
  38. #include <system/BAddr.h>
  39. #include <ncd/NCDValParser.h>
  40. #include <ncd/NCDValGenerator.h>
  41. #include <ncd/extra/NCDRequestClient.h>
  42. #include <generated/blog_channel_ncd_request.h>
  43. static void client_handler_error (void *user);
  44. static void client_handler_connected (void *user);
  45. static void request_handler_sent (void *user);
  46. static void request_handler_reply (void *user, NCDValMem reply_mem, NCDValRef reply_value);
  47. static void request_handler_finished (void *user, int is_error);
  48. static int write_all (int fd, const uint8_t *data, size_t len);
  49. static int make_connect_addr (const char *str, struct BConnection_addr *out_addr);
  50. NCDStringIndex string_index;
  51. NCDValMem request_mem;
  52. NCDValRef request_value;
  53. BReactor reactor;
  54. NCDRequestClient client;
  55. NCDRequestClientRequest request;
  56. int have_request;
  57. int main (int argc, char *argv[])
  58. {
  59. int res = 1;
  60. if (argc != 3) {
  61. fprintf(stderr, "Usage: %s < unix:<socket_path> / tcp:<address>:<port> > <request_payload>\n", (argc > 0 ? argv[0] : ""));
  62. goto fail0;
  63. }
  64. char *connect_address = argv[1];
  65. char *request_payload_string = argv[2];
  66. BLog_InitStderr();
  67. BTime_Init();
  68. if (!NCDStringIndex_Init(&string_index)) {
  69. BLog(BLOG_ERROR, "NCDStringIndex_Init failed");
  70. goto fail01;
  71. }
  72. NCDValMem_Init(&request_mem, &string_index);
  73. if (!NCDValParser_Parse(MemRef_MakeCstr(request_payload_string), &request_mem, &request_value)) {
  74. BLog(BLOG_ERROR, "BReactor_Init failed");
  75. goto fail1;
  76. }
  77. if (!BNetwork_GlobalInit()) {
  78. BLog(BLOG_ERROR, "BNetwork_Init failed");
  79. goto fail1;
  80. }
  81. if (!BReactor_Init(&reactor)) {
  82. BLog(BLOG_ERROR, "BReactor_Init failed");
  83. goto fail1;
  84. }
  85. struct BConnection_addr addr;
  86. if (!make_connect_addr(connect_address, &addr)) {
  87. goto fail2;
  88. }
  89. if (!NCDRequestClient_Init(&client, addr, &reactor, &string_index, NULL, client_handler_error, client_handler_connected)) {
  90. BLog(BLOG_ERROR, "NCDRequestClient_Init failed");
  91. goto fail2;
  92. }
  93. have_request = 0;
  94. res = BReactor_Exec(&reactor);
  95. if (have_request) {
  96. NCDRequestClientRequest_Free(&request);
  97. }
  98. NCDRequestClient_Free(&client);
  99. fail2:
  100. BReactor_Free(&reactor);
  101. fail1:
  102. NCDValMem_Free(&request_mem);
  103. NCDStringIndex_Free(&string_index);
  104. fail01:
  105. BLog_Free();
  106. fail0:
  107. DebugObjectGlobal_Finish();
  108. return res;
  109. }
  110. static int make_connect_addr (const char *str, struct BConnection_addr *out_addr)
  111. {
  112. size_t i;
  113. if (i = string_begins_with(str, "unix:")) {
  114. *out_addr = BConnection_addr_unix(MemRef_MakeCstr(str + i));
  115. }
  116. else if (i = string_begins_with(str, "tcp:")) {
  117. BAddr baddr;
  118. if (!BAddr_Parse2(&baddr, (char *)str + i, NULL, 0, 1)) {
  119. BLog(BLOG_ERROR, "failed to parse tcp address");
  120. return 0;
  121. }
  122. *out_addr = BConnection_addr_baddr(baddr);
  123. }
  124. else {
  125. BLog(BLOG_ERROR, "address must start with unix: or tcp:");
  126. return 0;
  127. }
  128. return 1;
  129. }
  130. static void client_handler_error (void *user)
  131. {
  132. BLog(BLOG_ERROR, "client error");
  133. BReactor_Quit(&reactor, 1);
  134. }
  135. static void client_handler_connected (void *user)
  136. {
  137. ASSERT(!have_request)
  138. if (!NCDRequestClientRequest_Init(&request, &client, request_value, NULL, request_handler_sent, request_handler_reply, request_handler_finished)) {
  139. BLog(BLOG_ERROR, "NCDRequestClientRequest_Init failed");
  140. BReactor_Quit(&reactor, 1);
  141. return;
  142. }
  143. have_request = 1;
  144. }
  145. static void request_handler_sent (void *user)
  146. {
  147. ASSERT(have_request)
  148. }
  149. static void request_handler_reply (void *user, NCDValMem reply_mem, NCDValRef reply_value)
  150. {
  151. ASSERT(have_request)
  152. char *str = NCDValGenerator_Generate(reply_value);
  153. if (!str) {
  154. BLog(BLOG_ERROR, "NCDValGenerator_Generate failed");
  155. goto fail0;
  156. }
  157. if (!write_all(1, (uint8_t *)str, strlen(str))) {
  158. goto fail1;
  159. }
  160. if (!write_all(1, (const uint8_t *)"\n", 1)) {
  161. goto fail1;
  162. }
  163. free(str);
  164. NCDValMem_Free(&reply_mem);
  165. return;
  166. fail1:
  167. free(str);
  168. fail0:
  169. NCDValMem_Free(&reply_mem);
  170. BReactor_Quit(&reactor, 1);
  171. }
  172. static void request_handler_finished (void *user, int is_error)
  173. {
  174. if (is_error) {
  175. BLog(BLOG_ERROR, "request error");
  176. BReactor_Quit(&reactor, 1);
  177. return;
  178. }
  179. BReactor_Quit(&reactor, 0);
  180. }
  181. static int write_all (int fd, const uint8_t *data, size_t len)
  182. {
  183. while (len > 0) {
  184. ssize_t res = write(fd, data, len);
  185. if (res <= 0) {
  186. BLog(BLOG_ERROR, "write failed");
  187. return 0;
  188. }
  189. data += res;
  190. len -= res;
  191. }
  192. return 1;
  193. }