PacketRouter.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @file PacketRouter.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 <flow/PacketRouter.h>
  23. static void input_handler_done (PacketRouter *o, int data_len)
  24. {
  25. ASSERT(data_len >= 0)
  26. ASSERT(data_len <= o->mtu - o->recv_offset)
  27. ASSERT(!BPending_IsSet(&o->next_job))
  28. DebugObject_Access(&o->d_obj);
  29. // set next job
  30. BPending_Set(&o->next_job);
  31. // call handler
  32. o->handler(o->user, RouteBufferSource_Pointer(&o->rbs), data_len);
  33. return;
  34. }
  35. static void next_job_handler (PacketRouter *o)
  36. {
  37. DebugObject_Access(&o->d_obj);
  38. // receive
  39. PacketRecvInterface_Receiver_Recv(o->input, RouteBufferSource_Pointer(&o->rbs) + o->recv_offset);
  40. }
  41. int PacketRouter_Init (PacketRouter *o, int mtu, int recv_offset, PacketRecvInterface *input, PacketRouter_handler handler, void *user, BPendingGroup *pg)
  42. {
  43. ASSERT(mtu >= 0)
  44. ASSERT(recv_offset >= 0)
  45. ASSERT(recv_offset <= mtu)
  46. ASSERT(PacketRecvInterface_GetMTU(input) <= mtu - recv_offset)
  47. // init arguments
  48. o->mtu = mtu;
  49. o->recv_offset = recv_offset;
  50. o->input = input;
  51. o->handler = handler;
  52. o->user = user;
  53. // init input
  54. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  55. // init RouteBufferSource
  56. if (!RouteBufferSource_Init(&o->rbs, mtu)) {
  57. goto fail0;
  58. }
  59. // init next job
  60. BPending_Init(&o->next_job, pg, (BPending_handler)next_job_handler, o);
  61. // receive
  62. PacketRecvInterface_Receiver_Recv(o->input, RouteBufferSource_Pointer(&o->rbs) + o->recv_offset);
  63. DebugObject_Init(&o->d_obj);
  64. return 1;
  65. fail0:
  66. return 0;
  67. }
  68. void PacketRouter_Free (PacketRouter *o)
  69. {
  70. DebugObject_Free(&o->d_obj);
  71. // free next job
  72. BPending_Free(&o->next_job);
  73. // free RouteBufferSource
  74. RouteBufferSource_Free(&o->rbs);
  75. }
  76. int PacketRouter_Route (PacketRouter *o, int len, RouteBuffer *output, uint8_t **next_buf, int copy_offset, int copy_len)
  77. {
  78. ASSERT(len >= 0)
  79. ASSERT(len <= o->mtu)
  80. ASSERT(RouteBuffer_GetMTU(output) == o->mtu)
  81. ASSERT(copy_offset >= 0)
  82. ASSERT(copy_offset <= o->mtu)
  83. ASSERT(copy_len >= 0)
  84. ASSERT(copy_len <= o->mtu - copy_offset)
  85. ASSERT(BPending_IsSet(&o->next_job))
  86. DebugObject_Access(&o->d_obj);
  87. if (!RouteBufferSource_Route(&o->rbs, len, output, copy_offset, copy_len)) {
  88. return 0;
  89. }
  90. if (next_buf) {
  91. *next_buf = RouteBufferSource_Pointer(&o->rbs);
  92. }
  93. return 1;
  94. }
  95. void PacketRouter_AssertRoute (PacketRouter *o)
  96. {
  97. ASSERT(BPending_IsSet(&o->next_job))
  98. DebugObject_Access(&o->d_obj);
  99. }