SeqPacketSocketSink.c 3.7 KB

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