DataHeader.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef DataHeader_H
  16. #define DataHeader_H
  17. #include "util/Assert.h"
  18. #include "util/Endian.h"
  19. #include "wire/ContentType.h"
  20. /**
  21. * 1 2 3
  22. * 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
  23. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  24. * 0 | ver | unusd | unused | Content Type |
  25. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  26. *
  27. * The DataHeader is protected from the switches by the l2 encryption layer.
  28. * It's primary use is to tell the endpoint the protocol of the content.
  29. */
  30. struct DataHeader
  31. {
  32. /** Version is set to DataHeader_CURRENT_VERSION version. */
  33. uint8_t versionAndFlags;
  34. uint8_t unused;
  35. uint16_t contentType_be;
  36. };
  37. #define DataHeader_SIZE 4
  38. Assert_compileTime(sizeof(struct DataHeader) == DataHeader_SIZE);
  39. #define DataHeader_CURRENT_VERSION 1
  40. static inline enum ContentType DataHeader_getContentType(struct DataHeader* hdr)
  41. {
  42. return Endian_bigEndianToHost16(hdr->contentType_be);
  43. }
  44. static inline void DataHeader_setContentType(struct DataHeader* hdr, enum ContentType type)
  45. {
  46. Assert_true(type <= 0xffff);
  47. hdr->contentType_be = Endian_hostToBigEndian16(type);
  48. }
  49. static inline void DataHeader_setVersion(struct DataHeader* hdr, uint8_t version)
  50. {
  51. hdr->versionAndFlags = (hdr->versionAndFlags & 0x0f) | (version << 4);
  52. }
  53. static inline uint8_t DataHeader_getVersion(struct DataHeader* hdr)
  54. {
  55. return hdr->versionAndFlags >> 4;
  56. }
  57. #endif