BConnectionGeneric.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @file BConnectionGeneric.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_BCONNECTION_GENERIC_H
  30. #define BADVPN_BCONNECTION_GENERIC_H
  31. #include <misc/debug.h>
  32. #include <base/BLog.h>
  33. #include <system/BConnection.h>
  34. #define BCONNECTION_ADDR_TYPE_BADDR 1
  35. #define BCONNECTION_ADDR_TYPE_UNIX 2
  36. struct BConnection_addr {
  37. int type;
  38. union {
  39. BAddr baddr;
  40. const char *unix_socket_path;
  41. } u;
  42. };
  43. static struct BConnection_addr BConnection_addr_baddr (BAddr baddr)
  44. {
  45. struct BConnection_addr addr;
  46. addr.type = BCONNECTION_ADDR_TYPE_BADDR;
  47. addr.u.baddr = baddr;
  48. return addr;
  49. }
  50. static struct BConnection_addr BConnection_addr_unix (const char *unix_socket_path)
  51. {
  52. struct BConnection_addr addr;
  53. addr.type = BCONNECTION_ADDR_TYPE_UNIX;
  54. addr.u.unix_socket_path = unix_socket_path;
  55. return addr;
  56. }
  57. static int BListener_InitGeneric (BListener *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  58. BListener_handler handler) WARN_UNUSED;
  59. static int BConnector_InitGeneric (BConnector *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  60. BConnector_handler handler) WARN_UNUSED;
  61. static int BListener_InitGeneric (BListener *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  62. BListener_handler handler)
  63. {
  64. ASSERT(handler)
  65. BNetwork_Assert();
  66. switch (addr.type) {
  67. case BCONNECTION_ADDR_TYPE_BADDR:
  68. return BListener_Init(o, addr.u.baddr, reactor, user, handler);
  69. case BCONNECTION_ADDR_TYPE_UNIX:
  70. #ifdef BADVPN_USE_WINAPI
  71. BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "unix sockets not supported");
  72. return 0;
  73. #else
  74. return BListener_InitUnix(o, addr.u.unix_socket_path, reactor, user, handler);
  75. #endif
  76. default:
  77. ASSERT(0);
  78. }
  79. }
  80. static int BConnector_InitGeneric (BConnector *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  81. BConnector_handler handler)
  82. {
  83. ASSERT(handler)
  84. BNetwork_Assert();
  85. switch (addr.type) {
  86. case BCONNECTION_ADDR_TYPE_BADDR:
  87. return BConnector_Init(o, addr.u.baddr, reactor, user, handler);
  88. case BCONNECTION_ADDR_TYPE_UNIX:
  89. #ifdef BADVPN_USE_WINAPI
  90. BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "unix sockets not supported");
  91. return 0;
  92. #else
  93. return BConnector_InitUnix(o, addr.u.unix_socket_path, reactor, user, handler);
  94. #endif
  95. default:
  96. ASSERT(0);
  97. }
  98. }
  99. #endif