PacketProtoDecoder.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <system/DebugObject.h>
  33. #include <flow/StreamRecvInterface.h>
  34. #include <flow/PacketPassInterface.h>
  35. #include <flow/FlowError.h>
  36. #define PACKETPROTODECODER_ERROR_TOOLONG 1
  37. /**
  38. * Object which decodes a stream according to PacketProto.
  39. *
  40. * Input is with {@link StreamRecvInterface}.
  41. * Output is with {@link PacketPassInterface}.
  42. *
  43. * Errors are reported through {@link FlowErrorDomain}.
  44. * On error, the decoder is reset to the initial state.
  45. * Error code is an int which is one of the following:
  46. * - PACKETPROTODECODER_ERROR_TOOLONG: the packet header contains
  47. * a packet length value which is too big.
  48. */
  49. typedef struct {
  50. FlowErrorReporter rep;
  51. StreamRecvInterface *input;
  52. PacketPassInterface *output;
  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 rep error reporting data
  65. * @param input input interface. The decoder will accept packets with payload size up to its MTU
  66. * (but the payload can never be more than PACKETPROTO_MAXPAYLOAD).
  67. * @param output output interface
  68. * @param pg pending group
  69. * @return 1 on success, 0 on failure
  70. */
  71. int PacketProtoDecoder_Init (PacketProtoDecoder *enc, FlowErrorReporter rep, StreamRecvInterface *input, PacketPassInterface *output, BPendingGroup *pg) WARN_UNUSED;
  72. /**
  73. * Frees the object.
  74. *
  75. * @param enc the object
  76. */
  77. void PacketProtoDecoder_Free (PacketProtoDecoder *enc);
  78. /**
  79. * Clears the internal buffer.
  80. * The next data received from the input will be treated as a new
  81. * PacketProto stream.
  82. *
  83. * @param enc the object
  84. */
  85. void PacketProtoDecoder_Reset (PacketProtoDecoder *enc);
  86. #endif