BIPC.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <system/BIPC.h>
  23. #define COMPONENT_SOURCE 1
  24. #define COMPONENT_SINK 2
  25. static void error_handler (BIPC *o, int component, const void *data)
  26. {
  27. ASSERT(component == COMPONENT_SOURCE || component == COMPONENT_SINK)
  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. int BIPC_InitConnect (BIPC *o, const char *path, int send_mtu, int recv_mtu, BIPC_handler handler, void *user, BReactor *reactor)
  39. {
  40. ASSERT(send_mtu >= 0)
  41. ASSERT(recv_mtu >= 0)
  42. // init arguments
  43. o->handler = handler;
  44. o->user = user;
  45. // init dead var
  46. DEAD_INIT(o->dead);
  47. // init socket
  48. if (BSocket_Init(&o->sock, reactor, BADDR_TYPE_UNIX, BSOCKET_TYPE_SEQPACKET) < 0) {
  49. DEBUG("BSocket_Init failed");
  50. goto fail0;
  51. }
  52. // connect socket
  53. if (BSocket_ConnectUnix(&o->sock, path) < 0) {
  54. DEBUG("BSocket_ConnectUnix failed (%d)", BSocket_GetError(&o->sock));
  55. goto fail1;
  56. }
  57. // init error domain
  58. FlowErrorDomain_Init(&o->domain, (FlowErrorDomain_handler)error_handler, o);
  59. // init sink
  60. SeqPacketSocketSink_Init(&o->sink, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), &o->sock, send_mtu);
  61. // init source
  62. SeqPacketSocketSource_Init(&o->source, FlowErrorReporter_Create(&o->domain, COMPONENT_SOURCE), &o->sock, recv_mtu);
  63. DebugObject_Init(&o->d_obj);
  64. return 1;
  65. fail1:
  66. BSocket_Free(&o->sock);
  67. fail0:
  68. return 0;
  69. }
  70. int BIPC_InitAccept (BIPC *o, BIPCServer *server, int send_mtu, int recv_mtu, BIPC_handler handler, void *user)
  71. {
  72. ASSERT(send_mtu >= 0)
  73. ASSERT(recv_mtu >= 0)
  74. // init arguments
  75. o->handler = handler;
  76. o->user = user;
  77. // init dead var
  78. DEAD_INIT(o->dead);
  79. // accept socket
  80. if (Listener_Accept(&server->listener, &o->sock, NULL) < 0) {
  81. DEBUG("Listener_Accept failed");
  82. goto fail0;
  83. }
  84. // init error domain
  85. FlowErrorDomain_Init(&o->domain, (FlowErrorDomain_handler)error_handler, o);
  86. // init sink
  87. SeqPacketSocketSink_Init(&o->sink, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), &o->sock, send_mtu);
  88. // init source
  89. SeqPacketSocketSource_Init(&o->source, FlowErrorReporter_Create(&o->domain, COMPONENT_SOURCE), &o->sock, recv_mtu);
  90. DebugObject_Init(&o->d_obj);
  91. return 1;
  92. fail0:
  93. return 0;
  94. }
  95. void BIPC_Free (BIPC *o)
  96. {
  97. DebugObject_Free(&o->d_obj);
  98. // free source
  99. SeqPacketSocketSource_Free(&o->source);
  100. // free sink
  101. SeqPacketSocketSink_Free(&o->sink);
  102. // free socket
  103. BSocket_Free(&o->sock);
  104. // free dead var
  105. DEAD_KILL(o->dead);
  106. }
  107. PacketPassInterface * BIPC_GetSendInterface (BIPC *o)
  108. {
  109. DebugObject_Access(&o->d_obj);
  110. return SeqPacketSocketSink_GetInput(&o->sink);
  111. }
  112. PacketRecvInterface * BIPC_GetRecvInterface (BIPC *o)
  113. {
  114. DebugObject_Access(&o->d_obj);
  115. return SeqPacketSocketSource_GetOutput(&o->source);
  116. }