1
0

Control.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/SwitchHeader.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 + SwitchHeader_SIZE + 4)
  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 SwitchHeader cause;
  33. /** The handle which sits below the SwitchHeader. */
  34. uint32_t causeHandle;
  35. };
  36. Assert_compileTime(sizeof(struct Control_Error) == Control_Error_MIN_SIZE);
  37. /**
  38. * Type three, ping.
  39. */
  40. #define Control_PING_be Endian_hostToBigEndian16(3)
  41. #define Control_Ping_HEADER_SIZE 8
  42. #define Control_Ping_MIN_SIZE 8
  43. #define Control_Ping_MAX_SIZE 256
  44. #define Control_Ping_MAGIC Endian_hostToBigEndian32(0x09f91102)
  45. struct Control_Ping
  46. {
  47. /** Magic: equal to Control_Ping_MAGIC in a ping and Control_Pong_MAGIC in a pong. */
  48. uint32_t magic;
  49. /** The version of the sending node. */
  50. uint32_t version_be;
  51. /**
  52. * Between 0 and 256 bytes of opaque data.
  53. * Since a ping is inherently a message to one's self,
  54. * the format is only of interest to the sender and thus undefined.
  55. */
  56. uint8_t data[4];
  57. };
  58. Assert_compileTime(sizeof(struct Control_Ping) == Control_Ping_MIN_SIZE + 4);
  59. /**
  60. * Type four, pong.
  61. * A pong is identical to a ping.
  62. */
  63. #define Control_PONG_be Endian_hostToBigEndian16(4)
  64. #define Control_Pong_HEADER_SIZE Control_Ping_HEADER_SIZE
  65. #define Control_Pong_MIN_SIZE Control_Ping_MIN_SIZE
  66. #define Control_Pong_MAX_SIZE Control_Ping_MAX_SIZE
  67. #define Control_Pong_MAGIC Endian_hostToBigEndian32(0x9d74e35b)
  68. /**
  69. * Type five, key request/response.
  70. * Request a node's public key, for use in debugging.
  71. *
  72. * Any data (up to 64 bytes) following the end of the KeyPing structure
  73. * is the cookie which must be reflected.
  74. */
  75. #define Control_KEYPING_be Endian_hostToBigEndian16(5)
  76. #define Control_KeyPing_HEADER_SIZE 40
  77. #define Control_KeyPing_MAX_SIZE (Control_KeyPing_HEADER_SIZE + 64)
  78. #define Control_KeyPing_MAGIC Endian_hostToBigEndian32(0x01234567)
  79. struct Control_KeyPing
  80. {
  81. /** Magic: equal to Control_KeyPing_MAGIC in a ping and Control_KeyPong_MAGIC in a pong. */
  82. uint32_t magic;
  83. /** The version of the sending node. */
  84. uint32_t version_be;
  85. /** The permanent public key. */
  86. uint8_t key[32];
  87. };
  88. Assert_compileTime(sizeof(struct Control_KeyPing) == Control_KeyPing_HEADER_SIZE);
  89. #define Control_KEYPONG_be Endian_hostToBigEndian16(6)
  90. #define Control_KeyPong_HEADER_SIZE Control_KeyPing_HEADER_SIZE
  91. #define Control_KeyPong_MAX_SIZE Control_KeyPing_MAX_SIZE
  92. #define Control_KeyPong_MAGIC Endian_hostToBigEndian32(0x89abcdef)
  93. static inline char* Control_typeString(uint16_t type_be)
  94. {
  95. if (type_be == Control_ERROR_be) {
  96. return "ERROR";
  97. } else if (type_be == Control_PING_be) {
  98. return "PING";
  99. } else if (type_be == Control_PONG_be) {
  100. return "PONG";
  101. } else if (type_be == Control_KEYPING_be) {
  102. return "KEYPING";
  103. } else if (type_be == Control_KEYPONG_be) {
  104. return "KEYPONG";
  105. } else {
  106. return "UNKNOWN";
  107. }
  108. }
  109. struct Control_Header
  110. {
  111. /**
  112. * This should be the one's complement checksum
  113. * of the control packet with 0'd checksum field.
  114. */
  115. uint16_t checksum_be;
  116. /** The type of control message, eg: Control_ERROR. */
  117. uint16_t type_be;
  118. };
  119. #define Control_Header_SIZE 4
  120. Assert_compileTime(sizeof(struct Control_Header) == Control_Header_SIZE);
  121. /**
  122. * A return message which is treated specially by switches.
  123. *
  124. * 1 2 3
  125. * 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
  126. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  127. * 0 | Checksum | Type |
  128. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  129. * 4 | |
  130. * + First <Length> Bytes of Packet Which Caused The Error +
  131. * 8 | |
  132. *
  133. */
  134. struct Control
  135. {
  136. struct Control_Header header;
  137. union {
  138. struct Control_Error error;
  139. struct Control_Ping ping;
  140. struct Control_Ping pong;
  141. struct Control_KeyPing keyPing;
  142. struct Control_Ping keyPong;
  143. /** The control packet content. */
  144. uint8_t bytes[4];
  145. } content;
  146. };
  147. // Control_KeyPing is the largest structure and thus defines the length of the "content" union.
  148. Assert_compileTime(sizeof(struct Control) == Control_Header_SIZE + Control_KeyPing_HEADER_SIZE);
  149. #endif