Listener.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file Listener.c
  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. #include <stddef.h>
  23. #include <misc/debug.h>
  24. #include <system/BLog.h>
  25. #include <system/Listener.h>
  26. #include <generated/blog_channel_Listener.h>
  27. static void socket_handler (Listener *o, int event)
  28. {
  29. ASSERT(event == BSOCKET_ACCEPT)
  30. o->accepted = 0;
  31. DebugIn_GoIn(&o->d_in_handler);
  32. DEAD_ENTER(o->dead)
  33. o->handler(o->user);
  34. if (DEAD_LEAVE(o->dead)) {
  35. return;
  36. }
  37. DebugIn_GoOut(&o->d_in_handler);
  38. // if there was no attempt to accept, do it now, discarding the client
  39. if (!o->accepted) {
  40. if (BSocket_Accept(o->sock, NULL, NULL) < 0) {
  41. BLog(BLOG_ERROR, "BSocket_Accept failed (%d)", BSocket_GetError(o->sock));
  42. }
  43. }
  44. }
  45. int Listener_Init (Listener *o, BReactor *reactor, BAddr addr, Listener_handler handler, void *user)
  46. {
  47. ASSERT(!BAddr_IsInvalid(&addr))
  48. // init arguments
  49. o->reactor = reactor;
  50. o->handler = handler;
  51. o->user = user;
  52. // init dead var
  53. DEAD_INIT(o->dead);
  54. // set not existing
  55. o->existing = 0;
  56. // create socket
  57. if (BSocket_Init(&o->our_sock, o->reactor, addr.type, BSOCKET_TYPE_STREAM) < 0) {
  58. BLog(BLOG_ERROR, "BSocket_Init failed");
  59. goto fail0;
  60. }
  61. // set socket
  62. o->sock = &o->our_sock;
  63. // bind socket
  64. if (BSocket_Bind(o->sock, &addr) < 0) {
  65. BLog(BLOG_ERROR, "BSocket_Bind failed (%d)", BSocket_GetError(o->sock));
  66. goto fail1;
  67. }
  68. // listen socket
  69. if (BSocket_Listen(o->sock, -1) < 0) {
  70. BLog(BLOG_ERROR, "BSocket_Listen failed (%d)", BSocket_GetError(o->sock));
  71. goto fail1;
  72. }
  73. // register socket event handler
  74. BSocket_AddEventHandler(o->sock, BSOCKET_ACCEPT, (BSocket_handler)socket_handler, o);
  75. BSocket_EnableEvent(o->sock, BSOCKET_ACCEPT);
  76. // init debug in handler
  77. DebugIn_Init(&o->d_in_handler);
  78. // init debug object
  79. DebugObject_Init(&o->d_obj);
  80. return 1;
  81. fail1:
  82. BSocket_Free(&o->our_sock);
  83. fail0:
  84. return 0;
  85. }
  86. void Listener_InitExisting (Listener *o, BReactor *reactor, BSocket *sock, Listener_handler handler, void *user)
  87. {
  88. // init arguments
  89. o->reactor = reactor;
  90. o->handler = handler;
  91. o->user = user;
  92. o->sock = sock;
  93. // init dead var
  94. DEAD_INIT(o->dead);
  95. // set existing
  96. o->existing = 1;
  97. // register socket event handler
  98. BSocket_AddEventHandler(o->sock, BSOCKET_ACCEPT, (BSocket_handler)socket_handler, o);
  99. BSocket_EnableEvent(o->sock, BSOCKET_ACCEPT);
  100. // init debug in handler
  101. DebugIn_Init(&o->d_in_handler);
  102. // init debug object
  103. DebugObject_Init(&o->d_obj);
  104. }
  105. void Listener_Free (Listener *o)
  106. {
  107. // free debug object
  108. DebugObject_Free(&o->d_obj);
  109. if (!o->existing) {
  110. // free socket
  111. BSocket_Free(&o->our_sock);
  112. }
  113. // free dead var
  114. DEAD_KILL(o->dead);
  115. }
  116. int Listener_Accept (Listener *o, BSocket *sockout, BAddr *addrout)
  117. {
  118. ASSERT(sockout)
  119. ASSERT(DebugIn_In(&o->d_in_handler))
  120. o->accepted = 1;
  121. if (BSocket_Accept(o->sock, sockout, addrout) < 0) {
  122. BLog(BLOG_ERROR, "BSocket_Accept failed (%d)", BSocket_GetError(o->sock));
  123. return 0;
  124. }
  125. return 1;
  126. }