Control.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 two, 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 three, 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 four, 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. /**
  67. * Type five, key request/response.
  68. * Request a node's public key, for use in debugging.
  69. *
  70. * Any data (up to 64 bytes) following the end of the KeyPing structure
  71. * is the cookie which must be reflected.
  72. */
  73. #define Control_KEYPING_be Endian_hostToBigEndian16(5)
  74. #define Control_KeyPing_MIN_SIZE 40
  75. #define Control_KeyPing_MAGIC Endian_hostToBigEndian32(0x01234567)
  76. struct Control_KeyPing
  77. {
  78. /** Magic: equal to Control_KeyPing_MAGIC in a ping and Control_KeyPong_MAGIC in a pong. */
  79. uint32_t magic;
  80. /** The version of the sending node. */
  81. uint32_t version_be;
  82. /** The permanent public key. */
  83. uint8_t key[32];
  84. };
  85. Assert_compileTime(sizeof(struct Control_KeyPing) == Control_KeyPing_MIN_SIZE);
  86. #define Control_KEYPONG_be Endian_hostToBigEndian16(6)
  87. #define Control_KeyPong_MIN_SIZE Control_KeyPing_MIN_SIZE
  88. #define Control_KeyPong_MAGIC Endian_hostToBigEndian32(0x89abcdef)
  89. static inline char* Control_typeString(uint16_t type_be)
  90. {
  91. if (type_be == Control_ERROR_be) {
  92. return "ERROR";
  93. } else if (type_be == Control_PING_be) {
  94. return "PING";
  95. } else if (type_be == Control_PONG_be) {
  96. return "PONG";
  97. } else if (type_be == Control_KEYPING_be) {
  98. return "KEYPING";
  99. } else if (type_be == Control_KEYPONG_be) {
  100. return "KEYPONG";
  101. } else {
  102. return "UNKNOWN";
  103. }
  104. }
  105. /**
  106. * A return message which is treated specially by switches.
  107. *
  108. * 1 2 3
  109. * 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
  110. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  111. * 0 | Checksum | Type |
  112. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  113. * 4 | |
  114. * + First <Length> Bytes of Packet Which Caused The Error +
  115. * 8 | |
  116. *
  117. */
  118. #define Control_HEADER_SIZE 4
  119. struct Control
  120. {
  121. /**
  122. * This should be the one's complement checksum
  123. * of the control packet with 0'd checksum field.
  124. */
  125. uint16_t checksum_be;
  126. /** The type of control message, eg: Control_ERROR. */
  127. uint16_t type_be;
  128. union {
  129. struct Control_Error error;
  130. struct Control_Ping ping;
  131. struct Control_Ping pong;
  132. struct Control_KeyPing keyPing;
  133. struct Control_Ping keyPong;
  134. /** The control packet content. */
  135. uint8_t bytes[4];
  136. } content;
  137. };
  138. // Control_KeyPing is the largest structure and thus defines the length of the "content" union.
  139. Assert_compileTime(sizeof(struct Control) == Control_HEADER_SIZE + Control_KeyPing_MIN_SIZE);
  140. #endif