Headers.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Headers_H
  16. #define Headers_H
  17. #include "util/Assert.h"
  18. #include "util/Endian.h"
  19. #include <stdint.h>
  20. struct Headers_IP6Header
  21. {
  22. uint16_t versionClassAndFlowLabel;
  23. /** Big Endian. */
  24. uint16_t flowLabelLow_be;
  25. /** Big Endian. */
  26. uint16_t payloadLength_be;
  27. uint8_t nextHeader;
  28. uint8_t hopLimit;
  29. uint8_t sourceAddr[16];
  30. uint8_t destinationAddr[16];
  31. };
  32. #define Headers_IP6Header_SIZE 40
  33. Assert_compileTime(sizeof(struct Headers_IP6Header) == Headers_IP6Header_SIZE);
  34. struct Headers_IP6Fragment
  35. {
  36. uint8_t nextHeader;
  37. uint8_t zero;
  38. uint16_t fragmentOffsetAndMoreFragments_be;
  39. uint32_t identifier;
  40. };
  41. #define Headers_IP6Fragment_SIZE 8
  42. Assert_compileTime(sizeof(struct Headers_IP6Fragment) == Headers_IP6Fragment_SIZE);
  43. #define Headers_IP6Fragment_TYPE 44
  44. static inline uint32_t Headers_IP6Fragment_getOffset(struct Headers_IP6Fragment* frag)
  45. {
  46. return Endian_bigEndianToHost16(frag->fragmentOffsetAndMoreFragments_be) >> 3;
  47. }
  48. static inline void Headers_IP6Fragment_setOffset(struct Headers_IP6Fragment* frag, uint16_t offset)
  49. {
  50. frag->fragmentOffsetAndMoreFragments_be &= Endian_hostToBigEndian16(7);
  51. frag->fragmentOffsetAndMoreFragments_be |= Endian_hostToBigEndian16(offset << 3);
  52. }
  53. static inline int Headers_IP6Fragment_hasMoreFragments(struct Headers_IP6Fragment* frag)
  54. {
  55. return frag->fragmentOffsetAndMoreFragments_be & Endian_hostToBigEndian16(1);
  56. }
  57. static inline void Headers_IP6Fragment_setMoreFragments(struct Headers_IP6Fragment* frag, int more)
  58. {
  59. if (more) {
  60. frag->fragmentOffsetAndMoreFragments_be |= Endian_hostToBigEndian16(1);
  61. } else {
  62. frag->fragmentOffsetAndMoreFragments_be &= Endian_hostToBigEndian16(0xFFFF << 1);
  63. }
  64. }
  65. struct Headers_IP4Header
  66. {
  67. uint8_t versionAndHeaderLength;
  68. uint8_t differentiatedServices;
  69. uint16_t totalLength_be;
  70. uint16_t identification_be;
  71. uint16_t flagsAndFragmentOffset;
  72. uint8_t ttl;
  73. uint8_t protocol;
  74. uint16_t checksum_be;
  75. uint8_t sourceAddr[4];
  76. uint8_t destAddr[4];
  77. };
  78. #define Headers_IP4Header_SIZE 20
  79. Assert_compileTime(sizeof(struct Headers_IP4Header) == Headers_IP4Header_SIZE);
  80. static inline int Headers_getIpVersion(void* header)
  81. {
  82. return (((uint8_t*) header)[0] & 0xF0) >> 4;
  83. }
  84. #define Headers_setIpVersion(header) \
  85. (((uint8_t*) header)[0] |= ( \
  86. (sizeof(*header) == Headers_IP4Header_SIZE) ? 4 : 6 \
  87. ) << 4)
  88. struct Headers_UDPHeader {
  89. uint16_t srcPort_be;
  90. uint16_t destPort_be;
  91. uint16_t length_be;
  92. uint16_t checksum_be;
  93. };
  94. #define Headers_UDPHeader_SIZE 8
  95. Assert_compileTime(sizeof(struct Headers_UDPHeader) == Headers_UDPHeader_SIZE);
  96. struct Headers_ICMP6Header {
  97. uint8_t type;
  98. uint8_t code;
  99. uint16_t checksum;
  100. uint32_t additional;
  101. };
  102. #define Headers_ICMP6Header_SIZE 8
  103. Assert_compileTime(sizeof(struct Headers_ICMP6Header) == Headers_ICMP6Header_SIZE);
  104. /**
  105. * A message which is broadcast to signal to other nodes in the local network that they can connect.
  106. */
  107. #define Headers_Beacon_PASSWORD_LEN 20
  108. struct Headers_Beacon {
  109. uint32_t version_be;
  110. uint8_t password[Headers_Beacon_PASSWORD_LEN];
  111. uint8_t publicKey[32];
  112. };
  113. #define Headers_Beacon_SIZE 56
  114. Assert_compileTime(sizeof(struct Headers_Beacon) == Headers_Beacon_SIZE);
  115. #endif