ipc_server.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <ipc/BIPCServer.h>
  32. #include <ipc/BIPC.h>
  33. #define RECV_MTU 100
  34. BReactor reactor;
  35. BIPCServer server;
  36. struct client {
  37. BIPC ipc;
  38. PacketPassInterface recv_if;
  39. PacketPassInterface *send_if;
  40. LinkedList2Node list_node;
  41. };
  42. static void server_handler (void *user);
  43. static void remove_client (struct client *client);
  44. static void client_ipc_handler (struct client *client);
  45. static void client_recv_handler_send (struct client *client, uint8_t *data, int data_len);
  46. static void client_send_handler_done (struct client *client);
  47. int main (int argc, char **argv)
  48. {
  49. if (argc <= 0) {
  50. return 1;
  51. }
  52. if (argc != 2) {
  53. printf("Usage: %s <path>\n", argv[0]);
  54. return 1;
  55. }
  56. char *path = argv[1];
  57. BLog_InitStdout();
  58. if (!BReactor_Init(&reactor)) {
  59. DEBUG("BReactor_Init failed");
  60. goto fail1;
  61. }
  62. if (!BIPCServer_Init(&server, path, server_handler, NULL, &reactor)) {
  63. DEBUG("BIPCServer_Init failed");
  64. goto fail2;
  65. }
  66. BReactor_Exec(&reactor);
  67. ASSERT(0)
  68. fail2:
  69. BReactor_Free(&reactor);
  70. fail1:
  71. BLog_Free();
  72. DebugObjectGlobal_Finish();
  73. return 1;
  74. }
  75. void server_handler (void *user)
  76. {
  77. struct client *client = malloc(sizeof(*client));
  78. if (!client) {
  79. DEBUG("failed to allocate client structure");
  80. goto fail0;
  81. }
  82. PacketPassInterface_Init(&client->recv_if, RECV_MTU, (PacketPassInterface_handler_send)client_recv_handler_send, client, BReactor_PendingGroup(&reactor));
  83. if (!BIPC_InitAccept(&client->ipc, &server, 0, &client->recv_if, (BIPC_handler)client_ipc_handler, client, &reactor)) {
  84. DEBUG("BIPC_InitAccept failed");
  85. goto fail1;
  86. }
  87. client->send_if = BIPC_GetSendInterface(&client->ipc);
  88. PacketPassInterface_Sender_Init(client->send_if, (PacketPassInterface_handler_done)client_send_handler_done, client);
  89. DEBUG("client connected");
  90. return;
  91. fail1:
  92. PacketPassInterface_Free(&client->recv_if);
  93. free(client);
  94. fail0:
  95. ;
  96. }
  97. void remove_client (struct client *client)
  98. {
  99. DEBUG("removing client");
  100. BIPC_Free(&client->ipc);
  101. PacketPassInterface_Free(&client->recv_if);
  102. free(client);
  103. }
  104. void client_ipc_handler (struct client *client)
  105. {
  106. remove_client(client);
  107. }
  108. void client_recv_handler_send (struct client *client, uint8_t *data, int data_len)
  109. {
  110. // print message
  111. uint8_t buf[data_len + 1];
  112. memcpy(buf, data, data_len);
  113. buf[data_len] = '\0';
  114. printf("received: '%s'\n", buf);
  115. // send reply
  116. PacketPassInterface_Sender_Send(client->send_if, NULL, 0);
  117. }
  118. void client_send_handler_done (struct client *client)
  119. {
  120. // allow next message
  121. PacketPassInterface_Done(&client->recv_if);
  122. }