PacketProtoDecoder.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @file PacketProtoDecoder.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 decodes a stream according to PacketProto.
  32. */
  33. #ifndef BADVPN_FLOW_PACKETPROTODECODER_H
  34. #define BADVPN_FLOW_PACKETPROTODECODER_H
  35. #include <stdint.h>
  36. #include <protocol/packetproto.h>
  37. #include <misc/debug.h>
  38. #include <base/DebugObject.h>
  39. #include <flow/StreamRecvInterface.h>
  40. #include <flow/PacketPassInterface.h>
  41. /**
  42. * Handler called when a protocol error occurs.
  43. * When an error occurs, the decoder is reset to the initial state.
  44. *
  45. * @param user as in {@link PacketProtoDecoder_Init}
  46. */
  47. typedef void (*PacketProtoDecoder_handler_error) (void *user);
  48. typedef struct {
  49. StreamRecvInterface *input;
  50. PacketPassInterface *output;
  51. void *user;
  52. PacketProtoDecoder_handler_error handler_error;
  53. int output_mtu;
  54. int buf_size;
  55. int buf_start;
  56. int buf_used;
  57. uint8_t *buf;
  58. DebugObject d_obj;
  59. } PacketProtoDecoder;
  60. /**
  61. * Initializes the object.
  62. *
  63. * @param enc the object
  64. * @param input input interface. The decoder will accept packets with payload size up to its MTU
  65. * (but the payload can never be more than PACKETPROTO_MAXPAYLOAD).
  66. * @param output output interface
  67. * @param pg pending group
  68. * @param user argument to handlers
  69. * @param handler_error error handler
  70. * @return 1 on success, 0 on failure
  71. */
  72. int PacketProtoDecoder_Init (PacketProtoDecoder *enc, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg, void *user, PacketProtoDecoder_handler_error handler_error) WARN_UNUSED;
  73. /**
  74. * Frees the object.
  75. *
  76. * @param enc the object
  77. */
  78. void PacketProtoDecoder_Free (PacketProtoDecoder *enc);
  79. /**
  80. * Clears the internal buffer.
  81. * The next data received from the input will be treated as a new
  82. * PacketProto stream.
  83. *
  84. * @param enc the object
  85. */
  86. void PacketProtoDecoder_Reset (PacketProtoDecoder *enc);
  87. #endif