BConnectionGeneric.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <string.h>
  32. #include <misc/debug.h>
  33. #include <misc/strdup.h>
  34. #include <misc/memref.h>
  35. #include <base/BLog.h>
  36. #include <system/BConnection.h>
  37. #define BCONNECTION_ADDR_TYPE_BADDR 1
  38. #define BCONNECTION_ADDR_TYPE_UNIX 2
  39. struct BConnection_addr {
  40. int type;
  41. union {
  42. BAddr baddr;
  43. struct {
  44. const char *str;
  45. size_t len;
  46. } unix_socket_path;
  47. } u;
  48. };
  49. static struct BConnection_addr BConnection_addr_baddr (BAddr baddr)
  50. {
  51. struct BConnection_addr addr;
  52. addr.type = BCONNECTION_ADDR_TYPE_BADDR;
  53. addr.u.baddr = baddr;
  54. return addr;
  55. }
  56. static struct BConnection_addr BConnection_addr_unix (MemRef unix_socket_path)
  57. {
  58. struct BConnection_addr addr;
  59. addr.type = BCONNECTION_ADDR_TYPE_UNIX;
  60. addr.u.unix_socket_path.str = unix_socket_path.ptr;
  61. addr.u.unix_socket_path.len = unix_socket_path.len;
  62. return addr;
  63. }
  64. static int BListener_InitGeneric (BListener *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  65. BListener_handler handler) WARN_UNUSED;
  66. static int BConnector_InitGeneric (BConnector *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  67. BConnector_handler handler) WARN_UNUSED;
  68. static int BListener_InitGeneric (BListener *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  69. BListener_handler handler)
  70. {
  71. ASSERT(handler)
  72. BNetwork_Assert();
  73. ASSERT(addr.type != BCONNECTION_ADDR_TYPE_UNIX || !memchr(addr.u.unix_socket_path.str, '\0', addr.u.unix_socket_path.len))
  74. switch (addr.type) {
  75. case BCONNECTION_ADDR_TYPE_BADDR: {
  76. return BListener_Init(o, addr.u.baddr, reactor, user, handler);
  77. } break;
  78. case BCONNECTION_ADDR_TYPE_UNIX: {
  79. #ifdef BADVPN_USE_WINAPI
  80. BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "unix sockets not supported");
  81. return 0;
  82. #else
  83. char *str = b_strdup_bin(addr.u.unix_socket_path.str, addr.u.unix_socket_path.len);
  84. if (!str) {
  85. BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "b_strdup_bin failed");
  86. return 0;
  87. }
  88. int res = BListener_InitUnix(o, str, reactor, user, handler);
  89. free(str);
  90. return res;
  91. #endif
  92. } break;
  93. default:
  94. ASSERT(0);
  95. return 0;
  96. }
  97. }
  98. static int BConnector_InitGeneric (BConnector *o, struct BConnection_addr addr, BReactor *reactor, void *user,
  99. BConnector_handler handler)
  100. {
  101. ASSERT(handler)
  102. BNetwork_Assert();
  103. ASSERT(addr.type != BCONNECTION_ADDR_TYPE_UNIX || !memchr(addr.u.unix_socket_path.str, '\0', addr.u.unix_socket_path.len))
  104. switch (addr.type) {
  105. case BCONNECTION_ADDR_TYPE_BADDR: {
  106. return BConnector_Init(o, addr.u.baddr, reactor, user, handler);
  107. } break;
  108. case BCONNECTION_ADDR_TYPE_UNIX: {
  109. #ifdef BADVPN_USE_WINAPI
  110. BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "unix sockets not supported");
  111. return 0;
  112. #else
  113. char *str = b_strdup_bin(addr.u.unix_socket_path.str, addr.u.unix_socket_path.len);
  114. if (!str) {
  115. BLog_LogToChannel(BLOG_CHANNEL_BConnection, BLOG_ERROR, "b_strdup_bin failed");
  116. return 0;
  117. }
  118. int res = BConnector_InitUnix(o, str, reactor, user, handler);
  119. free(str);
  120. return res;
  121. #endif
  122. } break;
  123. default:
  124. ASSERT(0);
  125. return 0;
  126. }
  127. }
  128. #endif