ncd-request.c 6.2 KB

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