BIPC.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/debugerror.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. BSocket sock;
  54. FlowErrorDomain domain;
  55. BIPC_handler handler;
  56. void *user;
  57. // sending
  58. PacketCopier send_copier;
  59. PacketProtoEncoder send_encoder;
  60. SinglePacketBuffer send_buf;
  61. PacketStreamSender send_pss;
  62. StreamSocketSink send_sink;
  63. // receiving
  64. StreamSocketSource recv_source;
  65. PacketProtoDecoder recv_decoder;
  66. DebugObject d_obj;
  67. DebugError d_err;
  68. } BIPC;
  69. /**
  70. * Initializes the object by connecting to an IPC server.
  71. *
  72. * @param o the object
  73. * @param path path of the IPC object. On *nix path of the unix socket, on Windows
  74. * path of the named pipe.
  75. * @param send_mtu maximum packet size for sending. Must be >=0 and <=PACKETPROTO_MAXPAYLOAD.
  76. * @param recv_mtu maximum packet size for receiving. Must be >=0 and <=PACKETPROTO_MAXPAYLOAD.
  77. * @param handler handler function called when an error occurs
  78. * @param user value to pass to handler function
  79. * @param reactor reactor we live in
  80. * @return 1 on success, 0 on failure
  81. */
  82. int BIPC_InitConnect (BIPC *o, const char *path, int send_mtu, PacketPassInterface *recv_if, BIPC_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  83. /**
  84. * Initializes the object by acception a connection on an IPC server.
  85. *
  86. * @param o the object
  87. * @param server IPC server to accept a connection on
  88. * @param send_mtu maximum packet size for sending. Must be >=0.
  89. * @param recv_mtu maximum packet size for receiving. Must be >=0.
  90. * @param handler handler function called when an error occurs
  91. * @param user value to pass to handler function
  92. * @param reactor reactor we live in
  93. * @return 1 on success, 0 on failure
  94. */
  95. int BIPC_InitAccept (BIPC *o, BIPCServer *server, int send_mtu, PacketPassInterface *recv_if, BIPC_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
  96. /**
  97. * Frees the object.
  98. *
  99. * @param o the object
  100. */
  101. void BIPC_Free (BIPC *o);
  102. /**
  103. * Returns the interface for sending.
  104. * The MTU of the interface will be as send_mtu in {@link BIPC_InitConnect}.
  105. *
  106. * @param o the object
  107. * @return interface for sending
  108. */
  109. PacketPassInterface * BIPC_GetSendInterface (BIPC *o);
  110. #endif