ipc_client.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @file ipc_client.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stddef.h>
  23. #include <misc/debug.h>
  24. #include <system/DebugObject.h>
  25. #include <system/BLog.h>
  26. #include <system/BSignal.h>
  27. #include <ipc/BIPC.h>
  28. #define SEND_MTU 100
  29. #define MAX_PACKETS 4096
  30. char *packets[MAX_PACKETS];
  31. int num_packets;
  32. int current_packet;
  33. int waiting;
  34. dead_t dead;
  35. BReactor reactor;
  36. BIPC ipc;
  37. PacketPassInterface recv_if;
  38. PacketPassInterface *send_if;
  39. static void terminate (int ret);
  40. static void signal_handler (void *user);
  41. static void ipc_handler (void *user);
  42. static void send_packets (void);
  43. static void ipc_send_handler_done (void *user);
  44. static void ipc_recv_handler_send (void *user, uint8_t *data, int data_len);
  45. int main (int argc, char **argv)
  46. {
  47. if (argc <= 0) {
  48. return 1;
  49. }
  50. if (argc < 2) {
  51. printf("Usage: %s <path> [message] ...\n", argv[0]);
  52. goto fail0;
  53. }
  54. char *path = argv[1];
  55. num_packets = 0;
  56. for (int i = 2; i < argc; i++) {
  57. if (num_packets == MAX_PACKETS) {
  58. DEBUG("too many packets");
  59. goto fail0;
  60. }
  61. packets[num_packets] = argv[i];
  62. num_packets++;
  63. }
  64. current_packet = 0;
  65. waiting = 0;
  66. DEAD_INIT(dead);
  67. BLog_InitStdout();
  68. if (!BReactor_Init(&reactor)) {
  69. DEBUG("BReactor_Init failed");
  70. goto fail1;
  71. }
  72. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  73. DEBUG("BSignal_Init failed");
  74. goto fail2;
  75. }
  76. PacketPassInterface_Init(&recv_if, 0, ipc_recv_handler_send, NULL, BReactor_PendingGroup(&reactor));
  77. if (!BIPC_InitConnect(&ipc, path, SEND_MTU, &recv_if, ipc_handler, NULL, &reactor)) {
  78. DEBUG("BIPC_InitConnect failed");
  79. goto fail3;
  80. }
  81. send_if = BIPC_GetSendInterface(&ipc);
  82. PacketPassInterface_Sender_Init(send_if, ipc_send_handler_done, NULL);
  83. send_packets();
  84. int ret = BReactor_Exec(&reactor);
  85. BReactor_Free(&reactor);
  86. BLog_Free();
  87. DebugObjectGlobal_Finish();
  88. return ret;
  89. fail3:
  90. PacketPassInterface_Free(&recv_if);
  91. BSignal_Finish();
  92. fail2:
  93. BReactor_Free(&reactor);
  94. fail1:
  95. BLog_Free();
  96. fail0:
  97. DebugObjectGlobal_Finish();
  98. return 1;
  99. }
  100. void terminate (int ret)
  101. {
  102. BIPC_Free(&ipc);
  103. PacketPassInterface_Free(&recv_if);
  104. BSignal_Finish();
  105. BReactor_Quit(&reactor, ret);
  106. DEAD_KILL(dead);
  107. }
  108. void signal_handler (void *user)
  109. {
  110. DEBUG("termination requested");
  111. terminate(1);
  112. }
  113. void ipc_handler (void *user)
  114. {
  115. DEBUG("IPC broken");
  116. terminate(1);
  117. }
  118. void send_packets (void)
  119. {
  120. ASSERT(current_packet >= 0)
  121. ASSERT(current_packet <= num_packets)
  122. ASSERT(!waiting)
  123. if (current_packet < num_packets) {
  124. PacketPassInterface_Sender_Send(send_if, (uint8_t *)packets[current_packet], strlen(packets[current_packet]));
  125. } else {
  126. terminate(0);
  127. }
  128. }
  129. void ipc_send_handler_done (void *user)
  130. {
  131. ASSERT(current_packet >= 0)
  132. ASSERT(current_packet < num_packets)
  133. ASSERT(!waiting)
  134. // wait for confirmation
  135. waiting = 1;
  136. }
  137. void ipc_recv_handler_send (void *user, uint8_t *data, int data_len)
  138. {
  139. ASSERT(current_packet >= 0)
  140. ASSERT(current_packet <= num_packets)
  141. ASSERT(!waiting || current_packet < num_packets)
  142. if (!waiting) {
  143. DEBUG("not waiting!");
  144. terminate(1);
  145. return;
  146. }
  147. if (data_len != 0) {
  148. DEBUG("reply not empty!");
  149. terminate(1);
  150. return;
  151. }
  152. current_packet++;
  153. waiting = 0;
  154. // accept received packet
  155. PacketPassInterface_Done(&recv_if);
  156. // send more packets
  157. send_packets();
  158. }