DatagramSocketSource.c 4.0 KB

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