PacketProtoDecoder.h 3.0 KB

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