PacketRouter.h 4.5 KB

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