ncd-request.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. NCDValMem request_mem;
  51. NCDValRef request_value;
  52. BReactor reactor;
  53. NCDRequestClient client;
  54. NCDRequestClientRequest request;
  55. int have_request;
  56. int main (int argc, char *argv[])
  57. {
  58. int res = 1;
  59. if (argc != 3) {
  60. fprintf(stderr, "Usage: %s < unix:<socket_path> / tcp:<address>:<port> > <request_payload>\n", (argc > 0 ? argv[0] : ""));
  61. goto fail0;
  62. }
  63. char *connect_address = argv[1];
  64. char *request_payload_string = argv[2];
  65. BLog_InitStderr();
  66. BTime_Init();
  67. NCDValMem_Init(&request_mem);
  68. if (!NCDValParser_Parse(MemRef_MakeCstr(request_payload_string), &request_mem, &request_value)) {
  69. BLog(BLOG_ERROR, "BReactor_Init failed");
  70. goto fail1;
  71. }
  72. if (!BNetwork_GlobalInit()) {
  73. BLog(BLOG_ERROR, "BNetwork_Init failed");
  74. goto fail1;
  75. }
  76. if (!BReactor_Init(&reactor)) {
  77. BLog(BLOG_ERROR, "BReactor_Init failed");
  78. goto fail1;
  79. }
  80. struct BConnection_addr addr;
  81. if (!make_connect_addr(connect_address, &addr)) {
  82. goto fail2;
  83. }
  84. if (!NCDRequestClient_Init(&client, addr, &reactor, NULL, client_handler_error, client_handler_connected)) {
  85. BLog(BLOG_ERROR, "NCDRequestClient_Init failed");
  86. goto fail2;
  87. }
  88. have_request = 0;
  89. res = BReactor_Exec(&reactor);
  90. if (have_request) {
  91. NCDRequestClientRequest_Free(&request);
  92. }
  93. NCDRequestClient_Free(&client);
  94. fail2:
  95. BReactor_Free(&reactor);
  96. fail1:
  97. NCDValMem_Free(&request_mem);
  98. BLog_Free();
  99. fail0:
  100. DebugObjectGlobal_Finish();
  101. return res;
  102. }
  103. static int make_connect_addr (const char *str, struct BConnection_addr *out_addr)
  104. {
  105. size_t i;
  106. if (i = string_begins_with(str, "unix:")) {
  107. *out_addr = BConnection_addr_unix(MemRef_MakeCstr(str + i));
  108. }
  109. else if (i = string_begins_with(str, "tcp:")) {
  110. BAddr baddr;
  111. if (!BAddr_Parse2(&baddr, (char *)str + i, NULL, 0, 1)) {
  112. BLog(BLOG_ERROR, "failed to parse tcp address");
  113. return 0;
  114. }
  115. *out_addr = BConnection_addr_baddr(baddr);
  116. }
  117. else {
  118. BLog(BLOG_ERROR, "address must start with unix: or tcp:");
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. static void client_handler_error (void *user)
  124. {
  125. BLog(BLOG_ERROR, "client error");
  126. BReactor_Quit(&reactor, 1);
  127. }
  128. static void client_handler_connected (void *user)
  129. {
  130. ASSERT(!have_request)
  131. if (!NCDRequestClientRequest_Init(&request, &client, request_value, NULL, request_handler_sent, request_handler_reply, request_handler_finished)) {
  132. BLog(BLOG_ERROR, "NCDRequestClientRequest_Init failed");
  133. BReactor_Quit(&reactor, 1);
  134. return;
  135. }
  136. have_request = 1;
  137. }
  138. static void request_handler_sent (void *user)
  139. {
  140. ASSERT(have_request)
  141. }
  142. static void request_handler_reply (void *user, NCDValMem reply_mem, NCDValRef reply_value)
  143. {
  144. ASSERT(have_request)
  145. char *str = NCDValGenerator_Generate(reply_value);
  146. if (!str) {
  147. BLog(BLOG_ERROR, "NCDValGenerator_Generate failed");
  148. goto fail0;
  149. }
  150. if (!write_all(1, (uint8_t *)str, strlen(str))) {
  151. goto fail1;
  152. }
  153. if (!write_all(1, (const uint8_t *)"\n", 1)) {
  154. goto fail1;
  155. }
  156. free(str);
  157. NCDValMem_Free(&reply_mem);
  158. return;
  159. fail1:
  160. free(str);
  161. fail0:
  162. NCDValMem_Free(&reply_mem);
  163. BReactor_Quit(&reactor, 1);
  164. }
  165. static void request_handler_finished (void *user, int is_error)
  166. {
  167. if (is_error) {
  168. BLog(BLOG_ERROR, "request error");
  169. BReactor_Quit(&reactor, 1);
  170. return;
  171. }
  172. BReactor_Quit(&reactor, 0);
  173. }
  174. static int write_all (int fd, const uint8_t *data, size_t len)
  175. {
  176. while (len > 0) {
  177. ssize_t res = write(fd, data, len);
  178. if (res <= 0) {
  179. BLog(BLOG_ERROR, "write failed");
  180. return 0;
  181. }
  182. data += res;
  183. len -= res;
  184. }
  185. return 1;
  186. }