ipc_server.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @file ipc_server.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 <stdio.h>
  23. #include <stddef.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <misc/dead.h>
  27. #include <misc/debug.h>
  28. #include <misc/offset.h>
  29. #include <structure/LinkedList2.h>
  30. #include <system/DebugObject.h>
  31. #include <system/BLog.h>
  32. #include <system/BSignal.h>
  33. #include <flow/SinglePacketBuffer.h>
  34. #include <ipc/BIPCServer.h>
  35. #include <ipc/BIPC.h>
  36. #define RECV_MTU 100
  37. BReactor reactor;
  38. BIPCServer server;
  39. LinkedList2 clients;
  40. struct client {
  41. dead_t dead;
  42. BIPC ipc;
  43. PacketPassInterface recv_if;
  44. PacketPassInterface *send_if;
  45. LinkedList2Node list_node;
  46. };
  47. static void signal_handler (void *user);
  48. static void server_handler (void *user);
  49. static void remove_client (struct client *client);
  50. static void client_ipc_handler (struct client *client);
  51. static int client_recv_handler_send (struct client *client, uint8_t *data, int data_len);
  52. static void client_send_handler_done (struct client *client);
  53. int main (int argc, char **argv)
  54. {
  55. if (argc <= 0) {
  56. return 1;
  57. }
  58. if (argc != 2) {
  59. printf("Usage: %s <path>\n", argv[0]);
  60. return 1;
  61. }
  62. char *path = argv[1];
  63. BLog_InitStdout();
  64. if (!BReactor_Init(&reactor)) {
  65. DEBUG("BReactor_Init failed");
  66. goto fail1;
  67. }
  68. if (!BSignal_Init()) {
  69. DEBUG("BSignal_Init failed");
  70. goto fail2;
  71. }
  72. BSignal_Capture();
  73. if (!BSignal_SetHandler(&reactor, signal_handler, NULL)) {
  74. DEBUG("BSignal_SetHandler failed");
  75. goto fail2;
  76. }
  77. if (!BIPCServer_Init(&server, path, server_handler, NULL, &reactor)) {
  78. DEBUG("BIPCServer_Init failed");
  79. goto fail3;
  80. }
  81. LinkedList2_Init(&clients);
  82. int ret = BReactor_Exec(&reactor);
  83. BReactor_Free(&reactor);
  84. BLog_Free();
  85. DebugObjectGlobal_Finish();
  86. return ret;
  87. fail3:
  88. BSignal_RemoveHandler();
  89. fail2:
  90. BReactor_Free(&reactor);
  91. fail1:
  92. BLog_Free();
  93. DebugObjectGlobal_Finish();
  94. return 1;
  95. }
  96. void signal_handler (void *user)
  97. {
  98. DEBUG("termination requested");
  99. LinkedList2Node *node;
  100. while (node = LinkedList2_GetFirst(&clients)) {
  101. struct client *client = UPPER_OBJECT(node, struct client, list_node);
  102. remove_client(client);
  103. }
  104. BIPCServer_Free(&server);
  105. BSignal_RemoveHandler();
  106. BReactor_Quit(&reactor, 1);
  107. }
  108. void server_handler (void *user)
  109. {
  110. struct client *client = malloc(sizeof(*client));
  111. if (!client) {
  112. DEBUG("failed to allocate client structure");
  113. goto fail0;
  114. }
  115. DEAD_INIT(client->dead);
  116. PacketPassInterface_Init(&client->recv_if, RECV_MTU, (PacketPassInterface_handler_send)client_recv_handler_send, client);
  117. if (!BIPC_InitAccept(&client->ipc, &server, 0, &client->recv_if, (BIPC_handler)client_ipc_handler, client, &reactor)) {
  118. DEBUG("BIPC_InitAccept failed");
  119. goto fail1;
  120. }
  121. client->send_if = BIPC_GetSendInterface(&client->ipc);
  122. PacketPassInterface_Sender_Init(client->send_if, (PacketPassInterface_handler_done)client_send_handler_done, client);
  123. LinkedList2_Append(&clients, &client->list_node);
  124. DEBUG("client connected");
  125. return;
  126. fail1:
  127. PacketPassInterface_Free(&client->recv_if);
  128. free(client);
  129. fail0:
  130. ;
  131. }
  132. void remove_client (struct client *client)
  133. {
  134. DEBUG("removing client");
  135. LinkedList2_Remove(&clients, &client->list_node);
  136. BIPC_Free(&client->ipc);
  137. PacketPassInterface_Free(&client->recv_if);
  138. DEAD_KILL(client->dead);
  139. free(client);
  140. }
  141. void client_ipc_handler (struct client *client)
  142. {
  143. remove_client(client);
  144. }
  145. int client_recv_handler_send (struct client *client, uint8_t *data, int data_len)
  146. {
  147. // print message
  148. uint8_t buf[data_len + 1];
  149. memcpy(buf, data, data_len);
  150. buf[data_len] = '\0';
  151. printf("received: '%s'\n", buf);
  152. // send reply
  153. DEAD_ENTER(client->dead)
  154. int res = PacketPassInterface_Sender_Send(client->send_if, NULL, 0);
  155. if (DEAD_LEAVE(client->dead)) {
  156. return -1;
  157. }
  158. if (!res) {
  159. // wait for reply to be sent, then allow next message
  160. return 0;
  161. }
  162. return 1;
  163. }
  164. void client_send_handler_done (struct client *client)
  165. {
  166. // allow next message
  167. PacketPassInterface_Done(&client->recv_if);
  168. return;
  169. }