BIPC.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @file BIPC.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 <ipc/BIPC.h>
  23. #define COMPONENT_SOURCE 1
  24. #define COMPONENT_SINK 2
  25. #define COMPONENT_DECODER 3
  26. static void error_handler (BIPC *o, int component, const void *data)
  27. {
  28. DebugObject_Access(&o->d_obj);
  29. #ifndef NDEBUG
  30. DEAD_ENTER(o->dead)
  31. #endif
  32. o->handler(o->user);
  33. #ifndef NDEBUG
  34. ASSERT(DEAD_KILLED)
  35. DEAD_LEAVE(o->dead);
  36. #endif
  37. }
  38. static int init_io (BIPC *o, int send_mtu, PacketPassInterface *recv_if, BReactor *reactor)
  39. {
  40. // init error domain
  41. FlowErrorDomain_Init(&o->domain, (FlowErrorDomain_handler)error_handler, o);
  42. // init sending
  43. StreamSocketSink_Init(&o->send_sink, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), &o->sock);
  44. PacketStreamSender_Init(&o->send_pss, StreamSocketSink_GetInput(&o->send_sink), PACKETPROTO_ENCLEN(send_mtu));
  45. PacketCopier_Init(&o->send_copier, send_mtu);
  46. PacketProtoEncoder_Init(&o->send_encoder, PacketCopier_GetOutput(&o->send_copier));
  47. if (!SinglePacketBuffer_Init(&o->send_buf, PacketProtoEncoder_GetOutput(&o->send_encoder), PacketStreamSender_GetInput(&o->send_pss), BReactor_PendingGroup(reactor))) {
  48. goto fail1;
  49. }
  50. // init receiving
  51. StreamSocketSource_Init(&o->recv_source, FlowErrorReporter_Create(&o->domain, COMPONENT_SOURCE), &o->sock);
  52. if (!PacketProtoDecoder_Init(&o->recv_decoder, FlowErrorReporter_Create(&o->domain, COMPONENT_DECODER), StreamSocketSource_GetOutput(&o->recv_source), recv_if, BReactor_PendingGroup(reactor))) {
  53. goto fail2;
  54. }
  55. return 1;
  56. fail2:
  57. StreamSocketSource_Free(&o->recv_source);
  58. SinglePacketBuffer_Free(&o->send_buf);
  59. fail1:
  60. PacketProtoEncoder_Free(&o->send_encoder);
  61. PacketCopier_Free(&o->send_copier);
  62. PacketStreamSender_Free(&o->send_pss);
  63. StreamSocketSink_Free(&o->send_sink);
  64. return 0;
  65. }
  66. static void free_io (BIPC *o)
  67. {
  68. // free receiving
  69. PacketProtoDecoder_Free(&o->recv_decoder);
  70. StreamSocketSource_Free(&o->recv_source);
  71. // free sending
  72. SinglePacketBuffer_Free(&o->send_buf);
  73. PacketProtoEncoder_Free(&o->send_encoder);
  74. PacketCopier_Free(&o->send_copier);
  75. PacketStreamSender_Free(&o->send_pss);
  76. StreamSocketSink_Free(&o->send_sink);
  77. }
  78. int BIPC_InitConnect (BIPC *o, const char *path, int send_mtu, PacketPassInterface *recv_if, BIPC_handler handler, void *user, BReactor *reactor)
  79. {
  80. ASSERT(send_mtu >= 0)
  81. ASSERT(send_mtu <= PACKETPROTO_MAXPAYLOAD)
  82. ASSERT(PacketPassInterface_GetMTU(recv_if) >= 0)
  83. ASSERT(PacketPassInterface_GetMTU(recv_if) <= PACKETPROTO_MAXPAYLOAD)
  84. // init arguments
  85. o->handler = handler;
  86. o->user = user;
  87. // init dead var
  88. DEAD_INIT(o->dead);
  89. // init socket
  90. if (BSocket_Init(&o->sock, reactor, BADDR_TYPE_UNIX, BSOCKET_TYPE_STREAM) < 0) {
  91. DEBUG("BSocket_Init failed");
  92. goto fail0;
  93. }
  94. // connect socket
  95. if (BSocket_ConnectUnix(&o->sock, path) < 0) {
  96. DEBUG("BSocket_ConnectUnix failed (%d)", BSocket_GetError(&o->sock));
  97. goto fail1;
  98. }
  99. // init I/O
  100. if (!init_io(o, send_mtu, recv_if, reactor)) {
  101. goto fail1;
  102. }
  103. DebugObject_Init(&o->d_obj);
  104. return 1;
  105. fail1:
  106. BSocket_Free(&o->sock);
  107. fail0:
  108. return 0;
  109. }
  110. int BIPC_InitAccept (BIPC *o, BIPCServer *server, int send_mtu, PacketPassInterface *recv_if, BIPC_handler handler, void *user, BReactor *reactor)
  111. {
  112. ASSERT(send_mtu >= 0)
  113. ASSERT(send_mtu <= PACKETPROTO_MAXPAYLOAD)
  114. ASSERT(PacketPassInterface_GetMTU(recv_if) >= 0)
  115. ASSERT(PacketPassInterface_GetMTU(recv_if) <= PACKETPROTO_MAXPAYLOAD)
  116. // init arguments
  117. o->handler = handler;
  118. o->user = user;
  119. // init dead var
  120. DEAD_INIT(o->dead);
  121. // accept socket
  122. if (!Listener_Accept(&server->listener, &o->sock, NULL)) {
  123. DEBUG("Listener_Accept failed");
  124. goto fail0;
  125. }
  126. // init I/O
  127. if (!init_io(o, send_mtu, recv_if, reactor)) {
  128. goto fail1;
  129. }
  130. DebugObject_Init(&o->d_obj);
  131. return 1;
  132. fail1:
  133. BSocket_Free(&o->sock);
  134. fail0:
  135. return 0;
  136. }
  137. void BIPC_Free (BIPC *o)
  138. {
  139. DebugObject_Free(&o->d_obj);
  140. // free I/O
  141. free_io(o);
  142. // free socket
  143. BSocket_Free(&o->sock);
  144. // free dead var
  145. DEAD_KILL(o->dead);
  146. }
  147. PacketPassInterface * BIPC_GetSendInterface (BIPC *o)
  148. {
  149. DebugObject_Access(&o->d_obj);
  150. return PacketCopier_GetInput(&o->send_copier);
  151. }