ipc_server.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 void 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(&reactor, signal_handler, NULL)) {
  69. DEBUG("BSignal_Init failed");
  70. goto fail2;
  71. }
  72. if (!BIPCServer_Init(&server, path, server_handler, NULL, &reactor)) {
  73. DEBUG("BIPCServer_Init failed");
  74. goto fail3;
  75. }
  76. LinkedList2_Init(&clients);
  77. int ret = BReactor_Exec(&reactor);
  78. BReactor_Free(&reactor);
  79. BLog_Free();
  80. DebugObjectGlobal_Finish();
  81. return ret;
  82. fail3:
  83. BSignal_Finish();
  84. fail2:
  85. BReactor_Free(&reactor);
  86. fail1:
  87. BLog_Free();
  88. DebugObjectGlobal_Finish();
  89. return 1;
  90. }
  91. void signal_handler (void *user)
  92. {
  93. DEBUG("termination requested");
  94. LinkedList2Node *node;
  95. while (node = LinkedList2_GetFirst(&clients)) {
  96. struct client *client = UPPER_OBJECT(node, struct client, list_node);
  97. remove_client(client);
  98. }
  99. BIPCServer_Free(&server);
  100. BSignal_Finish();
  101. BReactor_Quit(&reactor, 1);
  102. }
  103. void server_handler (void *user)
  104. {
  105. struct client *client = malloc(sizeof(*client));
  106. if (!client) {
  107. DEBUG("failed to allocate client structure");
  108. goto fail0;
  109. }
  110. DEAD_INIT(client->dead);
  111. PacketPassInterface_Init(&client->recv_if, RECV_MTU, (PacketPassInterface_handler_send)client_recv_handler_send, client, BReactor_PendingGroup(&reactor));
  112. if (!BIPC_InitAccept(&client->ipc, &server, 0, &client->recv_if, (BIPC_handler)client_ipc_handler, client, &reactor)) {
  113. DEBUG("BIPC_InitAccept failed");
  114. goto fail1;
  115. }
  116. client->send_if = BIPC_GetSendInterface(&client->ipc);
  117. PacketPassInterface_Sender_Init(client->send_if, (PacketPassInterface_handler_done)client_send_handler_done, client);
  118. LinkedList2_Append(&clients, &client->list_node);
  119. DEBUG("client connected");
  120. return;
  121. fail1:
  122. PacketPassInterface_Free(&client->recv_if);
  123. free(client);
  124. fail0:
  125. ;
  126. }
  127. void remove_client (struct client *client)
  128. {
  129. DEBUG("removing client");
  130. LinkedList2_Remove(&clients, &client->list_node);
  131. BIPC_Free(&client->ipc);
  132. PacketPassInterface_Free(&client->recv_if);
  133. DEAD_KILL(client->dead);
  134. free(client);
  135. }
  136. void client_ipc_handler (struct client *client)
  137. {
  138. remove_client(client);
  139. }
  140. void client_recv_handler_send (struct client *client, uint8_t *data, int data_len)
  141. {
  142. // print message
  143. uint8_t buf[data_len + 1];
  144. memcpy(buf, data, data_len);
  145. buf[data_len] = '\0';
  146. printf("received: '%s'\n", buf);
  147. // send reply
  148. PacketPassInterface_Sender_Send(client->send_if, NULL, 0);
  149. }
  150. void client_send_handler_done (struct client *client)
  151. {
  152. // allow next message
  153. PacketPassInterface_Done(&client->recv_if);
  154. }