PacketRouter.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @file PacketRouter.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <flow/PacketRouter.h>
  30. static void input_handler_done (PacketRouter *o, int data_len)
  31. {
  32. ASSERT(data_len >= 0)
  33. ASSERT(data_len <= o->mtu - o->recv_offset)
  34. ASSERT(!BPending_IsSet(&o->next_job))
  35. DebugObject_Access(&o->d_obj);
  36. // set next job
  37. BPending_Set(&o->next_job);
  38. // call handler
  39. o->handler(o->user, RouteBufferSource_Pointer(&o->rbs), data_len);
  40. return;
  41. }
  42. static void next_job_handler (PacketRouter *o)
  43. {
  44. DebugObject_Access(&o->d_obj);
  45. // receive
  46. PacketRecvInterface_Receiver_Recv(o->input, RouteBufferSource_Pointer(&o->rbs) + o->recv_offset);
  47. }
  48. int PacketRouter_Init (PacketRouter *o, int mtu, int recv_offset, PacketRecvInterface *input, PacketRouter_handler handler, void *user, BPendingGroup *pg)
  49. {
  50. ASSERT(mtu >= 0)
  51. ASSERT(recv_offset >= 0)
  52. ASSERT(recv_offset <= mtu)
  53. ASSERT(PacketRecvInterface_GetMTU(input) <= mtu - recv_offset)
  54. // init arguments
  55. o->mtu = mtu;
  56. o->recv_offset = recv_offset;
  57. o->input = input;
  58. o->handler = handler;
  59. o->user = user;
  60. // init input
  61. PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
  62. // init RouteBufferSource
  63. if (!RouteBufferSource_Init(&o->rbs, mtu)) {
  64. goto fail0;
  65. }
  66. // init next job
  67. BPending_Init(&o->next_job, pg, (BPending_handler)next_job_handler, o);
  68. // receive
  69. PacketRecvInterface_Receiver_Recv(o->input, RouteBufferSource_Pointer(&o->rbs) + o->recv_offset);
  70. DebugObject_Init(&o->d_obj);
  71. return 1;
  72. fail0:
  73. return 0;
  74. }
  75. void PacketRouter_Free (PacketRouter *o)
  76. {
  77. DebugObject_Free(&o->d_obj);
  78. // free next job
  79. BPending_Free(&o->next_job);
  80. // free RouteBufferSource
  81. RouteBufferSource_Free(&o->rbs);
  82. }
  83. int PacketRouter_Route (PacketRouter *o, int len, RouteBuffer *output, uint8_t **next_buf, int copy_offset, int copy_len)
  84. {
  85. ASSERT(len >= 0)
  86. ASSERT(len <= o->mtu)
  87. ASSERT(RouteBuffer_GetMTU(output) == o->mtu)
  88. ASSERT(copy_offset >= 0)
  89. ASSERT(copy_offset <= o->mtu)
  90. ASSERT(copy_len >= 0)
  91. ASSERT(copy_len <= o->mtu - copy_offset)
  92. ASSERT(BPending_IsSet(&o->next_job))
  93. DebugObject_Access(&o->d_obj);
  94. if (!RouteBufferSource_Route(&o->rbs, len, output, copy_offset, copy_len)) {
  95. return 0;
  96. }
  97. if (next_buf) {
  98. *next_buf = RouteBufferSource_Pointer(&o->rbs);
  99. }
  100. return 1;
  101. }
  102. void PacketRouter_AssertRoute (PacketRouter *o)
  103. {
  104. ASSERT(BPending_IsSet(&o->next_job))
  105. DebugObject_Access(&o->d_obj);
  106. }