SeqPacketSocketSource.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file SeqPacketSocketSource.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 <stdlib.h>
  23. #include <misc/debug.h>
  24. #include <flow/SeqPacketSocketSource.h>
  25. static void report_error (SeqPacketSocketSource *s, int error)
  26. {
  27. #ifndef NDEBUG
  28. s->in_error = 1;
  29. DEAD_ENTER(s->dead)
  30. #endif
  31. FlowErrorReporter_ReportError(&s->rep, &error);
  32. #ifndef NDEBUG
  33. ASSERT(DEAD_KILLED)
  34. DEAD_LEAVE(s->dead);
  35. #endif
  36. }
  37. static int output_handler_recv (SeqPacketSocketSource *s, uint8_t *data, int *data_len)
  38. {
  39. ASSERT(!s->out_have)
  40. ASSERT(!s->in_error)
  41. DebugObject_Access(&s->d_obj);
  42. int res = BSocket_Recv(s->bsock, data, s->mtu);
  43. if (res < 0) {
  44. int error = BSocket_GetError(s->bsock);
  45. if (error == BSOCKET_ERROR_LATER) {
  46. s->out_have = 1;
  47. s->out = data;
  48. BSocket_EnableEvent(s->bsock, BSOCKET_READ);
  49. return 0;
  50. }
  51. report_error(s, SEQPACKETSOCKETSOURCE_ERROR_BSOCKET);
  52. return -1;
  53. }
  54. if (res == 0) {
  55. report_error(s, SEQPACKETSOCKETSOURCE_ERROR_CLOSED);
  56. return -1;
  57. }
  58. *data_len = res;
  59. return 1;
  60. }
  61. static void socket_handler (SeqPacketSocketSource *s, int event)
  62. {
  63. ASSERT(s->out_have)
  64. ASSERT(event == BSOCKET_READ)
  65. ASSERT(!s->in_error)
  66. DebugObject_Access(&s->d_obj);
  67. int res = BSocket_Recv(s->bsock, s->out, s->mtu);
  68. if (res < 0) {
  69. int error = BSocket_GetError(s->bsock);
  70. if (error == BSOCKET_ERROR_LATER) {
  71. // nothing to receive, continue in socket_handler
  72. return;
  73. }
  74. report_error(s, SEQPACKETSOCKETSOURCE_ERROR_BSOCKET);
  75. return;
  76. }
  77. if (res == 0) {
  78. report_error(s, SEQPACKETSOCKETSOURCE_ERROR_CLOSED);
  79. return;
  80. }
  81. BSocket_DisableEvent(s->bsock, BSOCKET_READ);
  82. s->out_have = 0;
  83. PacketRecvInterface_Done(&s->output, res);
  84. return;
  85. }
  86. void SeqPacketSocketSource_Init (SeqPacketSocketSource *s, FlowErrorReporter rep, BSocket *bsock, int mtu)
  87. {
  88. ASSERT(mtu >= 0)
  89. // init arguments
  90. s->rep = rep;
  91. s->bsock = bsock;
  92. s->mtu = mtu;
  93. // init dead var
  94. DEAD_INIT(s->dead);
  95. // add socket event handler
  96. BSocket_AddEventHandler(s->bsock, BSOCKET_READ, (BSocket_handler)socket_handler, s);
  97. // init output
  98. PacketRecvInterface_Init(&s->output, mtu, (PacketRecvInterface_handler_recv)output_handler_recv, s);
  99. // have no output packet
  100. s->out_have = 0;
  101. // init debugging
  102. #ifndef NDEBUG
  103. s->in_error = 0;
  104. #endif
  105. DebugObject_Init(&s->d_obj);
  106. }
  107. void SeqPacketSocketSource_Free (SeqPacketSocketSource *s)
  108. {
  109. DebugObject_Free(&s->d_obj);
  110. // free output
  111. PacketRecvInterface_Free(&s->output);
  112. // remove socket event handler
  113. BSocket_RemoveEventHandler(s->bsock, BSOCKET_READ);
  114. // free dead var
  115. DEAD_KILL(s->dead);
  116. }
  117. PacketRecvInterface * SeqPacketSocketSource_GetOutput (SeqPacketSocketSource *s)
  118. {
  119. DebugObject_Access(&s->d_obj);
  120. return &s->output;
  121. }