ipc_server.c 4.6 KB

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