PacketRouter.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @file PacketRouter.h
  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. * @section DESCRIPTION
  30. *
  31. * Object which simplifies routing packets to {@link RouteBuffer}'s from a
  32. * {@link PacketRecvInterface} input.
  33. */
  34. #ifndef BADVPN_FLOW_PACKETROUTER_H
  35. #define BADVPN_FLOW_PACKETROUTER_H
  36. #include <base/DebugObject.h>
  37. #include <base/BPending.h>
  38. #include <flow/PacketRecvInterface.h>
  39. #include <flow/RouteBuffer.h>
  40. /**
  41. * Handler called when a packet is received, allowing the user to route it
  42. * to one or more buffers using {@link PacketRouter_Route}.
  43. *
  44. * @param user as in {@link PacketRouter_Init}
  45. * @param buf the buffer for the packet. May be modified by the user.
  46. * Will have space for mtu bytes. Only valid in the job context of
  47. * this handler, until {@link PacketRouter_Route} is called successfully.
  48. * @param recv_len length of the input packet (located at recv_offset bytes offset)
  49. */
  50. typedef void (*PacketRouter_handler) (void *user, uint8_t *buf, int recv_len);
  51. /**
  52. * Object which simplifies routing packets to {@link RouteBuffer}'s from a
  53. * {@link PacketRecvInterface} input.
  54. *
  55. * Packets are routed by calling {@link PacketRouter_Route} (possibly multiple times)
  56. * from the job context of the {@link PacketRouter_handler} handler.
  57. */
  58. typedef struct {
  59. int mtu;
  60. int recv_offset;
  61. PacketRecvInterface *input;
  62. PacketRouter_handler handler;
  63. void *user;
  64. RouteBufferSource rbs;
  65. BPending next_job;
  66. DebugObject d_obj;
  67. } PacketRouter;
  68. /**
  69. * Initializes the object.
  70. *
  71. * @param o the object
  72. * @param mtu maximum packet size. Must be >=0. It will only be possible to route packets to
  73. * {@link RouteBuffer}'s with the same MTU.
  74. * @param recv_offset offset from the beginning for receiving input packets.
  75. * Must be >=0 and <=mtu. The leading space should be initialized
  76. * by the user before routing a packet.
  77. * @param input input interface. Its MTU must be <= mtu - recv_offset.
  78. * @param handler handler called when a packet is received to allow the user to route it
  79. * @param user value passed to handler
  80. * @param pg pending group
  81. * @return 1 on success, 0 on failure
  82. */
  83. int PacketRouter_Init (PacketRouter *o, int mtu, int recv_offset, PacketRecvInterface *input, PacketRouter_handler handler, void *user, BPendingGroup *pg) WARN_UNUSED;
  84. /**
  85. * Frees the object.
  86. *
  87. * @param o the object
  88. */
  89. void PacketRouter_Free (PacketRouter *o);
  90. /**
  91. * Routes the current packet to the given buffer.
  92. * Must be called from the job context of the {@link PacketRouter_handler} handler.
  93. * On success, copies part of the current packet to next one (regardless if next_buf
  94. * is provided or not; if not, copies before receiving another packet).
  95. *
  96. * @param o the object
  97. * @param len total packet length (e.g. recv_offset + (recv_len from handler)).
  98. * Must be >=0 and <=mtu.
  99. * @param output buffer to route to. Its MTU must be the same as of this object.
  100. * @param next_buf if not NULL, on success, will be set to the address of a new current
  101. * packet that can be routed. The pointer will be valid in the job context of
  102. * the calling handler, until this function is called successfully again
  103. * (as for the original pointer provided by the handler).
  104. * @param copy_offset Offset from the beginning for copying to the next packet.
  105. * Must be >=0 and <=mtu.
  106. * @param copy_len Number of bytes to copy from the old current
  107. * packet to the next one. Must be >=0 and <= mtu - copy_offset.
  108. * @return 1 on success, 0 on failure (buffer full)
  109. */
  110. int PacketRouter_Route (PacketRouter *o, int len, RouteBuffer *output, uint8_t **next_buf, int copy_offset, int copy_len);
  111. /**
  112. * Asserts that {@link PacketRouter_Route} can be called.
  113. *
  114. * @param o the object
  115. */
  116. void PacketRouter_AssertRoute (PacketRouter *o);
  117. #endif