Listener.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. DebugObject_Access(&o->d_obj);
  31. // schedule accept job (to accept and close the connection in case the handler doesn't)
  32. BPending_Set(&o->accept_job);
  33. // call handler
  34. o->handler(o->user);
  35. return;
  36. }
  37. static void accept_job_handler (Listener *o)
  38. {
  39. DebugObject_Access(&o->d_obj);
  40. // accept and discard the connection
  41. if (BSocket_Accept(o->sock, NULL, NULL) < 0) {
  42. BLog(BLOG_ERROR, "BSocket_Accept failed (%d)", BSocket_GetError(o->sock));
  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. // set not existing
  53. o->existing = 0;
  54. // create socket
  55. if (BSocket_Init(&o->our_sock, o->reactor, addr.type, BSOCKET_TYPE_STREAM) < 0) {
  56. BLog(BLOG_ERROR, "BSocket_Init failed");
  57. goto fail0;
  58. }
  59. // set socket
  60. o->sock = &o->our_sock;
  61. // bind socket
  62. if (BSocket_Bind(o->sock, &addr) < 0) {
  63. BLog(BLOG_ERROR, "BSocket_Bind failed (%d)", BSocket_GetError(o->sock));
  64. goto fail1;
  65. }
  66. // listen socket
  67. if (BSocket_Listen(o->sock, -1) < 0) {
  68. BLog(BLOG_ERROR, "BSocket_Listen failed (%d)", BSocket_GetError(o->sock));
  69. goto fail1;
  70. }
  71. // register socket event handler
  72. BSocket_AddEventHandler(o->sock, BSOCKET_ACCEPT, (BSocket_handler)socket_handler, o);
  73. BSocket_EnableEvent(o->sock, BSOCKET_ACCEPT);
  74. // init accept job
  75. BPending_Init(&o->accept_job, BReactor_PendingGroup(o->reactor), (BPending_handler)accept_job_handler, o);
  76. DebugObject_Init(&o->d_obj);
  77. return 1;
  78. fail1:
  79. BSocket_Free(&o->our_sock);
  80. fail0:
  81. return 0;
  82. }
  83. void Listener_InitExisting (Listener *o, BReactor *reactor, BSocket *sock, Listener_handler handler, void *user)
  84. {
  85. // init arguments
  86. o->reactor = reactor;
  87. o->handler = handler;
  88. o->user = user;
  89. o->sock = sock;
  90. // set existing
  91. o->existing = 1;
  92. // register socket event handler
  93. BSocket_AddEventHandler(o->sock, BSOCKET_ACCEPT, (BSocket_handler)socket_handler, o);
  94. BSocket_EnableEvent(o->sock, BSOCKET_ACCEPT);
  95. // init accept job
  96. BPending_Init(&o->accept_job, BReactor_PendingGroup(o->reactor), (BPending_handler)accept_job_handler, o);
  97. DebugObject_Init(&o->d_obj);
  98. }
  99. void Listener_Free (Listener *o)
  100. {
  101. DebugObject_Free(&o->d_obj);
  102. // free accept job
  103. BPending_Free(&o->accept_job);
  104. // remove socket event handler
  105. BSocket_RemoveEventHandler(o->sock, BSOCKET_ACCEPT);
  106. if (!o->existing) {
  107. // free socket
  108. BSocket_Free(&o->our_sock);
  109. }
  110. }
  111. int Listener_Accept (Listener *o, BSocket *sockout, BAddr *addrout)
  112. {
  113. ASSERT(sockout)
  114. ASSERT(BPending_IsSet(&o->accept_job))
  115. DebugObject_Access(&o->d_obj);
  116. // unset accept job
  117. BPending_Unset(&o->accept_job);
  118. if (BSocket_Accept(o->sock, sockout, addrout) < 0) {
  119. BLog(BLOG_ERROR, "BSocket_Accept failed (%d)", BSocket_GetError(o->sock));
  120. return 0;
  121. }
  122. return 1;
  123. }