PacketProtoDecoder.h 2.6 KB

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