BConnectionGeneric.h 5.2 KB

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