ipc_server.c 3.7 KB

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