Listener.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <listener/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. // create socket
  55. if (BSocket_Init(&o->sock, o->reactor, addr.type, BSOCKET_TYPE_STREAM) < 0) {
  56. BLog(BLOG_ERROR, "BSocket_Init failed");
  57. goto fail0;
  58. }
  59. // bind socket
  60. if (BSocket_Bind(&o->sock, &addr) < 0) {
  61. BLog(BLOG_ERROR, "BSocket_Bind failed (%d)", BSocket_GetError(&o->sock));
  62. goto fail1;
  63. }
  64. // listen socket
  65. if (BSocket_Listen(&o->sock, -1) < 0) {
  66. BLog(BLOG_ERROR, "BSocket_Listen failed (%d)", BSocket_GetError(&o->sock));
  67. goto fail1;
  68. }
  69. // register socket event handler
  70. BSocket_AddEventHandler(&o->sock, BSOCKET_ACCEPT, (BSocket_handler)socket_handler, o);
  71. BSocket_EnableEvent(&o->sock, BSOCKET_ACCEPT);
  72. // init debug in handler
  73. DebugIn_Init(&o->d_in_handler);
  74. // init debug object
  75. DebugObject_Init(&o->d_obj);
  76. return 1;
  77. fail1:
  78. BSocket_Free(&o->sock);
  79. fail0:
  80. return 0;
  81. }
  82. void Listener_Free (Listener *o)
  83. {
  84. // free debug object
  85. DebugObject_Free(&o->d_obj);
  86. // free socket
  87. BSocket_Free(&o->sock);
  88. // free dead var
  89. DEAD_KILL(o->dead);
  90. }
  91. int Listener_Accept (Listener *o, BSocket *sockout, BAddr *addrout)
  92. {
  93. ASSERT(sockout)
  94. ASSERT(DebugIn_In(&o->d_in_handler))
  95. o->accepted = 1;
  96. if (BSocket_Accept(&o->sock, sockout, addrout) < 0) {
  97. BLog(BLOG_ERROR, "BSocket_Accept failed (%d)", BSocket_GetError(&o->sock));
  98. return 0;
  99. }
  100. return 1;
  101. }