BConnection.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file BConnection.h
  3. * @author Ambroz Bizjak <[email protected]>
  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. #ifndef BADVPN_SYSTEM_BCONNECTION
  23. #define BADVPN_SYSTEM_BCONNECTION
  24. #include <misc/debug.h>
  25. #include <flow/StreamPassInterface.h>
  26. #include <flow/StreamRecvInterface.h>
  27. #include <system/BAddr.h>
  28. #include <system/BReactor.h>
  29. #include <system/BNetwork.h>
  30. int BConnection_AddressSupported (BAddr addr);
  31. struct BListener_s;
  32. typedef struct BListener_s BListener;
  33. typedef void (*BListener_handler) (void *user);
  34. int BListener_Init (BListener *o, BAddr addr, BReactor *reactor, void *user,
  35. BListener_handler handler) WARN_UNUSED;
  36. void BListener_Free (BListener *o);
  37. struct BConnector_s;
  38. typedef struct BConnector_s BConnector;
  39. typedef void (*BConnector_handler) (void *user, int is_error);
  40. int BConnector_Init (BConnector *o, BAddr addr, BReactor *reactor, void *user,
  41. BConnector_handler handler) WARN_UNUSED;
  42. void BConnector_Free (BConnector *o);
  43. #define BCONNECTION_SOURCE_TYPE_LISTENER 1
  44. #define BCONNECTION_SOURCE_TYPE_CONNECTOR 2
  45. #define BCONNECTION_SOURCE_TYPE_PIPE 3
  46. struct BConnection_source {
  47. int type;
  48. union {
  49. struct {
  50. BListener *listener;
  51. BAddr *out_addr;
  52. } listener;
  53. struct {
  54. BConnector *connector;
  55. } connector;
  56. #ifndef BADVPN_USE_WINAPI
  57. struct {
  58. int pipefd;
  59. } pipe;
  60. #endif
  61. } u;
  62. };
  63. #define BCONNECTION_SOURCE_LISTENER(_listener, _out_addr) \
  64. ((struct BConnection_source){ \
  65. .type = BCONNECTION_SOURCE_TYPE_LISTENER, \
  66. .u.listener.listener = (_listener), \
  67. .u.listener.out_addr = (_out_addr) \
  68. })
  69. #define BCONNECTION_SOURCE_CONNECTOR(_connector) \
  70. ((struct BConnection_source){ \
  71. .type = BCONNECTION_SOURCE_TYPE_CONNECTOR, \
  72. .u.connector.connector = (_connector) \
  73. })
  74. #ifndef BADVPN_USE_WINAPI
  75. #define BCONNECTION_SOURCE_PIPE(_pipefd) \
  76. ((struct BConnection_source){ \
  77. .type = BCONNECTION_SOURCE_TYPE_PIPE, \
  78. .u.pipe.pipefd = (_pipefd) \
  79. })
  80. #endif
  81. struct BConnection_s;
  82. typedef struct BConnection_s BConnection;
  83. #define BCONNECTION_EVENT_ERROR 1
  84. #define BCONNECTION_EVENT_RECVCLOSED 2
  85. typedef void (*BConnection_handler) (void *user, int event);
  86. int BConnection_Init (BConnection *o, struct BConnection_source source, BReactor *reactor, void *user,
  87. BConnection_handler handler) WARN_UNUSED;
  88. void BConnection_Free (BConnection *o);
  89. void BConnection_SetHandlers (BConnection *o, void *user, BConnection_handler handler);
  90. int BConnection_SetSendBuffer (BConnection *o, int buf_size);
  91. void BConnection_SendAsync_Init (BConnection *o);
  92. void BConnection_SendAsync_Free (BConnection *o);
  93. StreamPassInterface * BConnection_SendAsync_GetIf (BConnection *o);
  94. void BConnection_RecvAsync_Init (BConnection *o);
  95. void BConnection_RecvAsync_Free (BConnection *o);
  96. StreamRecvInterface * BConnection_RecvAsync_GetIf (BConnection *o);
  97. #ifdef BADVPN_USE_WINAPI
  98. #include "BConnection_win.h"
  99. #else
  100. #include "BConnection_unix.h"
  101. #endif
  102. #endif