PacketProtoDecoder.h 2.9 KB

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