SeqPacketSocketSource.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. *data_len = res;
  55. return 1;
  56. }
  57. static void socket_handler (SeqPacketSocketSource *s, int event)
  58. {
  59. ASSERT(s->out_have)
  60. ASSERT(event == BSOCKET_READ)
  61. ASSERT(!s->in_error)
  62. DebugObject_Access(&s->d_obj);
  63. int res = BSocket_Recv(s->bsock, s->out, s->mtu);
  64. if (res < 0) {
  65. int error = BSocket_GetError(s->bsock);
  66. if (error == BSOCKET_ERROR_LATER) {
  67. // nothing to receive, continue in socket_handler
  68. return;
  69. }
  70. report_error(s, SEQPACKETSOCKETSOURCE_ERROR_BSOCKET);
  71. return;
  72. }
  73. BSocket_DisableEvent(s->bsock, BSOCKET_READ);
  74. s->out_have = 0;
  75. PacketRecvInterface_Done(&s->output, res);
  76. return;
  77. }
  78. void SeqPacketSocketSource_Init (SeqPacketSocketSource *s, FlowErrorReporter rep, BSocket *bsock, int mtu)
  79. {
  80. ASSERT(mtu >= 0)
  81. // init arguments
  82. s->rep = rep;
  83. s->bsock = bsock;
  84. s->mtu = mtu;
  85. // init dead var
  86. DEAD_INIT(s->dead);
  87. // add socket event handler
  88. BSocket_AddEventHandler(s->bsock, BSOCKET_READ, (BSocket_handler)socket_handler, s);
  89. // init output
  90. PacketRecvInterface_Init(&s->output, mtu, (PacketRecvInterface_handler_recv)output_handler_recv, s);
  91. // have no output packet
  92. s->out_have = 0;
  93. // init debugging
  94. #ifndef NDEBUG
  95. s->in_error = 0;
  96. #endif
  97. DebugObject_Init(&s->d_obj);
  98. }
  99. void SeqPacketSocketSource_Free (SeqPacketSocketSource *s)
  100. {
  101. DebugObject_Free(&s->d_obj);
  102. // free output
  103. PacketRecvInterface_Free(&s->output);
  104. // remove socket event handler
  105. BSocket_RemoveEventHandler(s->bsock, BSOCKET_READ);
  106. // free dead var
  107. DEAD_KILL(s->dead);
  108. }
  109. PacketRecvInterface * SeqPacketSocketSource_GetOutput (SeqPacketSocketSource *s)
  110. {
  111. DebugObject_Access(&s->d_obj);
  112. return &s->output;
  113. }