PacketProtoDecoder.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/dead.h>
  32. #include <misc/balign.h>
  33. #include <system/DebugObject.h>
  34. #include <flow/StreamRecvInterface.h>
  35. #include <flow/PacketPassInterface.h>
  36. #include <flow/error.h>
  37. #define PACKETPROTODECODER_ERROR_TOOLONG 1
  38. /**
  39. * Object which decodes a stream according to PacketProto.
  40. *
  41. * Input is with {@link StreamRecvInterface}.
  42. * Output is with {@link PacketPassInterface}.
  43. *
  44. * Errors are reported through {@link FlowErrorDomain}. All errors
  45. * are fatal and the object must be freed from the error handler.
  46. * Error code is an int which is one of the following:
  47. * - PACKETPROTODECODER_ERROR_TOOLONG: the packet header contains
  48. * a packet length value which is too big,
  49. */
  50. typedef struct {
  51. FlowErrorReporter rep;
  52. StreamRecvInterface *input;
  53. PacketPassInterface *output;
  54. int output_mtu;
  55. int buf_size;
  56. int buf_start;
  57. int buf_used;
  58. uint8_t *buf;
  59. DebugObject d_obj;
  60. #ifndef NDEBUG
  61. dead_t d_dead;
  62. #endif
  63. } PacketProtoDecoder;
  64. /**
  65. * Initializes the object.
  66. *
  67. * @param enc the object
  68. * @param rep error reporting data
  69. * @param input input interface. The decoder will accept packets with payload size up to its MTU
  70. * (but the payload can never be more than PACKETPROTO_MAXPAYLOAD).
  71. * @param output output interface
  72. * @param pg pending group
  73. * @return 1 on success, 0 on failure
  74. */
  75. int PacketProtoDecoder_Init (PacketProtoDecoder *enc, FlowErrorReporter rep, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg) WARN_UNUSED;
  76. /**
  77. * Frees the object.
  78. *
  79. * @param enc the object
  80. */
  81. void PacketProtoDecoder_Free (PacketProtoDecoder *enc);
  82. /**
  83. * Clears the internal buffer.
  84. * The next data received from the input will be treated as a new
  85. * PacketProto stream.
  86. *
  87. * @param enc the object
  88. */
  89. void PacketProtoDecoder_Reset (PacketProtoDecoder *enc);
  90. #endif