1
0

Control.h 4.0 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 <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Control_H
  16. #define Control_H
  17. #include "wire/Headers.h"
  18. #include "util/Endian.h"
  19. #include "util/Assert.h"
  20. /**
  21. * Type one, error.
  22. */
  23. #define Control_ERROR_be Endian_hostToBigEndian16(2)
  24. #define Control_Error_HEADER_SIZE 4
  25. #define Control_Error_MIN_SIZE (Control_Error_HEADER_SIZE + Headers_SwitchHeader_SIZE)
  26. #define Control_Error_MAX_SIZE 256
  27. struct Control_Error
  28. {
  29. /** The type of error, see Error.h */
  30. uint32_t errorType_be;
  31. /** The header of the packet which caused the error. */
  32. struct Headers_SwitchHeader cause;
  33. };
  34. Assert_compileTime(sizeof(struct Control_Error) == Control_Error_MIN_SIZE);
  35. /**
  36. * Type two, ping.
  37. */
  38. #define Control_PING_be Endian_hostToBigEndian16(3)
  39. #define Control_Ping_HEADER_SIZE 8
  40. #define Control_Ping_MIN_SIZE 8
  41. #define Control_Ping_MAX_SIZE 256
  42. #define Control_Ping_MAGIC Endian_hostToBigEndian32(0x09f91102)
  43. struct Control_Ping
  44. {
  45. /** Magic: equal to Control_Ping_MAGIC in a ping and Control_Pong_MAGIC in a pong. */
  46. uint32_t magic;
  47. /** The version of the sending node. */
  48. uint32_t version_be;
  49. /**
  50. * Between 0 and 256 bytes of opaque data.
  51. * Since a ping is inherently a message to one's self,
  52. * the format is only of interest to the sender and thus undefined.
  53. */
  54. uint8_t data[4];
  55. };
  56. Assert_compileTime(sizeof(struct Control_Ping) == Control_Ping_MIN_SIZE + 4);
  57. /**
  58. * Type three, pong.
  59. * A pong is identical to a ping.
  60. */
  61. #define Control_PONG_be Endian_hostToBigEndian16(4)
  62. #define Control_Pong_HEADER_SIZE Control_Ping_HEADER_SIZE
  63. #define Control_Pong_MIN_SIZE Control_Ping_MIN_SIZE
  64. #define Control_Pong_MAX_SIZE Control_Ping_MAX_SIZE
  65. #define Control_Pong_MAGIC Endian_hostToBigEndian32(0x9d74e35b)
  66. static inline char* Control_typeString(uint16_t type_be)
  67. {
  68. if (type_be == Control_ERROR_be) {
  69. return "error";
  70. } else if (type_be == Control_PING_be) {
  71. return "ping";
  72. } else if (type_be == Control_PONG_be) {
  73. return "pong";
  74. } else {
  75. return "unknown";
  76. }
  77. }
  78. /**
  79. * A return message which is treated specially by switches.
  80. *
  81. * 1 2 3
  82. * 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
  83. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  84. * 0 | Checksum | Type |
  85. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  86. * 4 | |
  87. * + First <Length> Bytes of Packet Which Caused The Error +
  88. * 8 | |
  89. *
  90. */
  91. #define Control_HEADER_SIZE 4
  92. struct Control
  93. {
  94. /**
  95. * This should be the one's complement checksum
  96. * of the control packet with 0'd checksum field.
  97. * TODO...
  98. */
  99. uint16_t checksum_be;
  100. /** The type of control message, eg: Control_ERROR. */
  101. uint16_t type_be;
  102. union {
  103. /** Type one, error. */
  104. struct Control_Error error;
  105. /** Type two, ping. */
  106. struct Control_Ping ping;
  107. /** Type three, pong. */
  108. struct Control_Ping pong;
  109. /** The control packet content. */
  110. uint8_t bytes[4];
  111. } content;
  112. };
  113. // Control_Error is the largest structure and thus defines the length of the "content" union.
  114. Assert_compileTime(sizeof(struct Control) == Control_HEADER_SIZE + Control_Error_MIN_SIZE);
  115. #endif