BIPC.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file BIPC.h
  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. * @section DESCRIPTION
  23. *
  24. * An abstraction of a reliable, sequenced, message-oriented two-way IPC.
  25. * Uses unix sockets on *nix systems, and named pipes on Windows.
  26. */
  27. #ifndef BADVPN_IPC_BIPC_H
  28. #define BADVPN_IPC_BIPC_H
  29. #include <protocol/packetproto.h>
  30. #include <misc/debug.h>
  31. #include <misc/dead.h>
  32. #include <system/BSocket.h>
  33. #include <system/DebugObject.h>
  34. #include <flow/StreamSocketSink.h>
  35. #include <flow/StreamSocketSource.h>
  36. #include <flow/PacketProtoEncoder.h>
  37. #include <flow/PacketProtoDecoder.h>
  38. #include <flow/SinglePacketBuffer.h>
  39. #include <flow/PacketCopier.h>
  40. #include <flow/PacketStreamSender.h>
  41. #include <ipc/BIPCServer.h>
  42. /**
  43. * Handler function called when an error occurs.
  44. * The object must be freed from within this handler.
  45. *
  46. * @param user as in {@link BIPC_InitConnect}
  47. */
  48. typedef void (*BIPC_handler) (void *user);
  49. /**
  50. * An abstraction of a reliable, sequenced, message-oriented two-way IPC.
  51. */
  52. typedef struct {
  53. dead_t dead;
  54. BSocket sock;
  55. FlowErrorDomain domain;
  56. BIPC_handler handler;
  57. void *user;
  58. // sending
  59. PacketCopier send_copier;
  60. PacketProtoEncoder send_encoder;
  61. SinglePacketBuffer send_buf;
  62. PacketStreamSender send_pss;
  63. StreamSocketSink send_sink;
  64. // receiving
  65. StreamSocketSource recv_source;
  66. PacketProtoDecoder recv_decoder;
  67. PacketCopier recv_copier;
  68. DebugObject d_obj;
  69. } BIPC;
  70. /**
  71. * Initializes the object by connecting to an IPC server.
  72. *
  73. * @param o the object
  74. * @param path path of the IPC object. On *nix path of the unix socket, on Windows
  75. * path of the named pipe.
  76. * @param send_mtu maximum packet size for sending. Must be >=0 and <=PACKETPROTO_MAXPAYLOAD.
  77. * @param recv_mtu maximum packet size for receiving. Must be >=0 and <=PACKETPROTO_MAXPAYLOAD.
  78. * @param handler handler function called when an error occurs
  79. * @param user value to pass to handler function
  80. * @param reactor reactor we live in
  81. * @return 1 on success, 0 on failure
  82. */
  83. int BIPC_InitConnect (BIPC *o, const char *path, int send_mtu, int recv_mtu, BIPC_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  84. /**
  85. * Initializes the object by acception a connection on an IPC server.
  86. *
  87. * @param o the object
  88. * @param server IPC server to accept a connection on
  89. * @param send_mtu maximum packet size for sending. Must be >=0.
  90. * @param recv_mtu maximum packet size for receiving. Must be >=0.
  91. * @param handler handler function called when an error occurs
  92. * @param user value to pass to handler function
  93. * @param reactor reactor we live in
  94. * @return 1 on success, 0 on failure
  95. */
  96. int BIPC_InitAccept (BIPC *o, BIPCServer *server, int send_mtu, int recv_mtu, BIPC_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  97. /**
  98. * Frees the object.
  99. *
  100. * @param o the object
  101. */
  102. void BIPC_Free (BIPC *o);
  103. /**
  104. * Returns the interface for sending.
  105. * The MTU of the interface will be as send_mtu in {@link BIPC_InitConnect}.
  106. *
  107. * @param o the object
  108. * @return interface for sending
  109. */
  110. PacketPassInterface * BIPC_GetSendInterface (BIPC *o);
  111. /**
  112. * Returns the interface for receiving.
  113. * The MTU of the interface will be as recv_mtu in {@link BIPC_InitConnect}.
  114. *
  115. * @param o the object
  116. * @return interface for receiving
  117. */
  118. PacketRecvInterface * BIPC_GetRecvInterface (BIPC *o);
  119. #endif