ipc_client.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <ipc/BIPC.h>
  27. #define SEND_MTU 100
  28. #define MAX_PACKETS 4096
  29. char *packets[MAX_PACKETS];
  30. int num_packets;
  31. int current_packet;
  32. int waiting;
  33. BReactor reactor;
  34. BIPC ipc;
  35. PacketPassInterface recv_if;
  36. PacketPassInterface *send_if;
  37. static void terminate (int ret);
  38. static void ipc_handler (void *user);
  39. static void send_packets (void);
  40. static void ipc_send_handler_done (void *user);
  41. static void ipc_recv_handler_send (void *user, uint8_t *data, int data_len);
  42. int main (int argc, char **argv)
  43. {
  44. if (argc <= 0) {
  45. return 1;
  46. }
  47. int ret = 1;
  48. if (argc < 2) {
  49. printf("Usage: %s <path> [message] ...\n", argv[0]);
  50. goto fail0;
  51. }
  52. char *path = argv[1];
  53. num_packets = 0;
  54. for (int i = 2; i < argc; i++) {
  55. if (num_packets == MAX_PACKETS) {
  56. DEBUG("too many packets");
  57. goto fail0;
  58. }
  59. packets[num_packets] = argv[i];
  60. num_packets++;
  61. }
  62. current_packet = 0;
  63. waiting = 0;
  64. BLog_InitStdout();
  65. if (!BReactor_Init(&reactor)) {
  66. DEBUG("BReactor_Init failed");
  67. goto fail1;
  68. }
  69. PacketPassInterface_Init(&recv_if, 0, ipc_recv_handler_send, NULL, BReactor_PendingGroup(&reactor));
  70. if (!BIPC_InitConnect(&ipc, path, SEND_MTU, &recv_if, ipc_handler, NULL, &reactor)) {
  71. DEBUG("BIPC_InitConnect failed");
  72. goto fail3;
  73. }
  74. send_if = BIPC_GetSendInterface(&ipc);
  75. PacketPassInterface_Sender_Init(send_if, ipc_send_handler_done, NULL);
  76. send_packets();
  77. ret = BReactor_Exec(&reactor);
  78. BIPC_Free(&ipc);
  79. fail3:
  80. PacketPassInterface_Free(&recv_if);
  81. fail2:
  82. BReactor_Free(&reactor);
  83. fail1:
  84. BLog_Free();
  85. fail0:
  86. DebugObjectGlobal_Finish();
  87. return ret;
  88. }
  89. void terminate (int ret)
  90. {
  91. BReactor_Quit(&reactor, ret);
  92. }
  93. void ipc_handler (void *user)
  94. {
  95. DEBUG("IPC broken");
  96. terminate(1);
  97. }
  98. void send_packets (void)
  99. {
  100. ASSERT(current_packet >= 0)
  101. ASSERT(current_packet <= num_packets)
  102. ASSERT(!waiting)
  103. if (current_packet < num_packets) {
  104. PacketPassInterface_Sender_Send(send_if, (uint8_t *)packets[current_packet], strlen(packets[current_packet]));
  105. } else {
  106. terminate(0);
  107. }
  108. }
  109. void ipc_send_handler_done (void *user)
  110. {
  111. ASSERT(current_packet >= 0)
  112. ASSERT(current_packet < num_packets)
  113. ASSERT(!waiting)
  114. // wait for confirmation
  115. waiting = 1;
  116. }
  117. void ipc_recv_handler_send (void *user, uint8_t *data, int data_len)
  118. {
  119. ASSERT(current_packet >= 0)
  120. ASSERT(current_packet <= num_packets)
  121. ASSERT(!waiting || current_packet < num_packets)
  122. if (!waiting) {
  123. DEBUG("not waiting!");
  124. terminate(1);
  125. return;
  126. }
  127. if (data_len != 0) {
  128. DEBUG("reply not empty!");
  129. terminate(1);
  130. return;
  131. }
  132. current_packet++;
  133. waiting = 0;
  134. // accept received packet
  135. PacketPassInterface_Done(&recv_if);
  136. // send more packets
  137. send_packets();
  138. }