DatagramSocketSource.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file DatagramSocketSource.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/DatagramSocketSource.h>
  24. static void report_error (DatagramSocketSource *s, int error)
  25. {
  26. FlowErrorReporter_ReportError(&s->rep, &error);
  27. return;
  28. }
  29. static void try_recv (DatagramSocketSource *s)
  30. {
  31. ASSERT(s->out_have)
  32. int res = BSocket_RecvFromTo(s->bsock, s->out, s->mtu, &s->last_addr, &s->last_local_addr);
  33. if (res < 0 && BSocket_GetError(s->bsock) == BSOCKET_ERROR_LATER) {
  34. // wait for socket in socket_handler
  35. BSocket_EnableEvent(s->bsock, BSOCKET_READ);
  36. return;
  37. }
  38. if (res < 0) {
  39. // schedule retry
  40. BPending_Set(&s->retry_job);
  41. // report error
  42. report_error(s, DATAGRAMSOCKETSOURCE_ERROR_BSOCKET);
  43. return;
  44. }
  45. #ifndef NDEBUG
  46. s->have_last_addr = 1;
  47. #endif
  48. // finish packet
  49. s->out_have = 0;
  50. PacketRecvInterface_Done(&s->output, res);
  51. }
  52. static void output_handler_recv (DatagramSocketSource *s, uint8_t *data)
  53. {
  54. ASSERT(!s->out_have)
  55. DebugObject_Access(&s->d_obj);
  56. // set packet
  57. s->out_have = 1;
  58. s->out = data;
  59. try_recv(s);
  60. return;
  61. }
  62. static void socket_handler (DatagramSocketSource *s, int event)
  63. {
  64. ASSERT(s->out_have)
  65. ASSERT(event == BSOCKET_READ)
  66. DebugObject_Access(&s->d_obj);
  67. BSocket_DisableEvent(s->bsock, BSOCKET_READ);
  68. try_recv(s);
  69. return;
  70. }
  71. static void retry_job_handler (DatagramSocketSource *s)
  72. {
  73. ASSERT(s->out_have)
  74. DebugObject_Access(&s->d_obj);
  75. try_recv(s);
  76. return;
  77. }
  78. void DatagramSocketSource_Init (DatagramSocketSource *s, FlowErrorReporter rep, BSocket *bsock, int mtu, BPendingGroup *pg)
  79. {
  80. ASSERT(mtu >= 0)
  81. // init arguments
  82. s->rep = rep;
  83. s->bsock = bsock;
  84. s->mtu = mtu;
  85. // add socket event handler
  86. BSocket_AddEventHandler(s->bsock, BSOCKET_READ, (BSocket_handler)socket_handler, s);
  87. // init output
  88. PacketRecvInterface_Init(&s->output, mtu, (PacketRecvInterface_handler_recv)output_handler_recv, s, pg);
  89. // have no output packet
  90. s->out_have = 0;
  91. // init retry job
  92. BPending_Init(&s->retry_job, pg, (BPending_handler)retry_job_handler, s);
  93. DebugObject_Init(&s->d_obj);
  94. #ifndef NDEBUG
  95. s->have_last_addr = 0;
  96. #endif
  97. }
  98. void DatagramSocketSource_Free (DatagramSocketSource *s)
  99. {
  100. DebugObject_Free(&s->d_obj);
  101. // free retry job
  102. BPending_Free(&s->retry_job);
  103. // free output
  104. PacketRecvInterface_Free(&s->output);
  105. // remove socket event handler
  106. BSocket_RemoveEventHandler(s->bsock, BSOCKET_READ);
  107. }
  108. PacketRecvInterface * DatagramSocketSource_GetOutput (DatagramSocketSource *s)
  109. {
  110. DebugObject_Access(&s->d_obj);
  111. return &s->output;
  112. }
  113. void DatagramSocketSource_GetLastAddresses (DatagramSocketSource *s, BAddr *addr, BIPAddr *local_addr)
  114. {
  115. ASSERT(s->have_last_addr)
  116. DebugObject_Access(&s->d_obj);
  117. if (addr) {
  118. *addr = s->last_addr;
  119. }
  120. if (local_addr) {
  121. *local_addr = s->last_local_addr;
  122. }
  123. }