packetproto.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @file packetproto.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. * Definitions for PacketProto, a protocol that allows sending of packets
  25. * over a reliable stream connection.
  26. *
  27. * All multi-byte integers in structs are little-endian, unless stated otherwise.
  28. *
  29. * Packets are encoded into a stream by representing each packet with:
  30. * - a 16-bit little-endian unsigned integer representing the length
  31. * of the payload
  32. * - that many bytes of payload
  33. */
  34. #ifndef BADVPN_PROTOCOL_PACKETPROTO_H
  35. #define BADVPN_PROTOCOL_PACKETPROTO_H
  36. #include <stdint.h>
  37. #include <limits.h>
  38. #include <misc/balign.h>
  39. /**
  40. * PacketProto packet header.
  41. * Wraps a single uint16_t in a packed struct for easy access.
  42. */
  43. struct packetproto_header
  44. {
  45. /**
  46. * Length of the packet payload that follows.
  47. */
  48. uint16_t len;
  49. } __attribute__((packed));
  50. #define PACKETPROTO_ENCLEN(_len) (sizeof(struct packetproto_header) + (_len))
  51. #define PACKETPROTO_MAXPAYLOAD UINT16_MAX
  52. #endif