1
0

Control.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 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 2
  24. #define Control_ERROR_be Endian_hostToBigEndian16(Control_ERROR)
  25. #define Control_Error_HEADER_SIZE 4
  26. #define Control_Error_MIN_SIZE (Control_Error_HEADER_SIZE + SwitchHeader_SIZE + 4)
  27. #define Control_Error_MAX_SIZE 256
  28. struct Control_Error
  29. {
  30. /** The type of error, see Error.h */
  31. uint32_t errorType_be;
  32. /** The header of the packet which caused the error. */
  33. struct SwitchHeader cause;
  34. /** The handle which sits below the SwitchHeader. */
  35. uint32_t causeHandle;
  36. };
  37. Assert_compileTime(sizeof(struct Control_Error) == Control_Error_MIN_SIZE);
  38. /**
  39. * Type three, ping.
  40. */
  41. #define Control_PING_be Endian_hostToBigEndian16(3)
  42. #define Control_Ping_HEADER_SIZE 8
  43. #define Control_Ping_MIN_SIZE 8
  44. #define Control_Ping_MAX_SIZE 256
  45. #define Control_Ping_MAGIC Endian_hostToBigEndian32(0x09f91102)
  46. struct Control_Ping
  47. {
  48. /** Magic: equal to Control_Ping_MAGIC in a ping and Control_Pong_MAGIC in a pong. */
  49. uint32_t magic;
  50. /** The version of the sending node. */
  51. uint32_t version_be;
  52. /**
  53. * Between 0 and 256 bytes of opaque data.
  54. * Since a ping is inherently a message to one's self,
  55. * the format is only of interest to the sender and thus undefined.
  56. */
  57. uint8_t data[4];
  58. };
  59. Assert_compileTime(sizeof(struct Control_Ping) == Control_Ping_MIN_SIZE + 4);
  60. /**
  61. * Type four, pong.
  62. * A pong is identical to a ping.
  63. */
  64. #define Control_PONG_be Endian_hostToBigEndian16(4)
  65. #define Control_Pong_HEADER_SIZE Control_Ping_HEADER_SIZE
  66. #define Control_Pong_MIN_SIZE Control_Ping_MIN_SIZE
  67. #define Control_Pong_MAX_SIZE Control_Ping_MAX_SIZE
  68. #define Control_Pong_MAGIC Endian_hostToBigEndian32(0x9d74e35b)
  69. /**
  70. * Type five, key request/response.
  71. * Request a node's public key, for use in debugging.
  72. *
  73. * Any data (up to 64 bytes) following the end of the KeyPing structure
  74. * is the cookie which must be reflected.
  75. */
  76. #define Control_KEYPING_be Endian_hostToBigEndian16(5)
  77. #define Control_KeyPing_HEADER_SIZE 40
  78. #define Control_KeyPing_MAX_SIZE (Control_KeyPing_HEADER_SIZE + 64)
  79. #define Control_KeyPing_MAGIC Endian_hostToBigEndian32(0x01234567)
  80. struct Control_KeyPing
  81. {
  82. /** Magic: equal to Control_KeyPing_MAGIC in a ping and Control_KeyPong_MAGIC in a pong. */
  83. uint32_t magic;
  84. /** The version of the sending node. */
  85. uint32_t version_be;
  86. /** The permanent public key. */
  87. uint8_t key[32];
  88. };
  89. Assert_compileTime(sizeof(struct Control_KeyPing) == Control_KeyPing_HEADER_SIZE);
  90. #define Control_KEYPONG_be Endian_hostToBigEndian16(6)
  91. #define Control_KeyPong_HEADER_SIZE Control_KeyPing_HEADER_SIZE
  92. #define Control_KeyPong_MAX_SIZE Control_KeyPing_MAX_SIZE
  93. #define Control_KeyPong_MAGIC Endian_hostToBigEndian32(0x89abcdef)
  94. #define Control_GETSNODE_QUERY_be Endian_hostToBigEndian16(7)
  95. #define Control_GETSNODE_QUERY_MAGIC Endian_hostToBigEndian32(0x736e6f71) // snoq
  96. #define Control_GETSNODE_REPLY_be Endian_hostToBigEndian16(8)
  97. #define Control_GETSNODE_REPLY_MAGIC Endian_hostToBigEndian32(0x736e6f72) // snor
  98. #define Control_GetSnode_HEADER_SIZE 56
  99. struct Control_GetSnode
  100. {
  101. // Control_SNODE_QUERY_MAGIC for queries
  102. // Control_SNODE_REPLY_MAGIC for replies
  103. uint32_t magic;
  104. // version of the node sending the packet
  105. uint32_t version_be;
  106. // version of the supernode belonging to the node sending the packet
  107. // 0 if unknown
  108. uint32_t snodeVersion_be;
  109. // If the highest bit is set then this number is per configuration and no more than this
  110. // should be sent by the peer, otherwise it is an estimate of the link speed.
  111. // Nodes should send no more than min(minimumConfigured, avg(nodeAVal, nodeBVal)
  112. // so if A configures 1000Mb, B estimates 100Mb -> use avg(100, 1000)
  113. // but if A configures or estimates 1000Mb, B configures 100Mb -> use 100Mb
  114. //
  115. // Nodes MUST check if this message comes from a direct peer before using this value.
  116. // 0 if unknown
  117. uint32_t kbps_be;
  118. // Key of the supernode belonging to the node sending, zero if no supernode is known or
  119. // configured.
  120. uint8_t snodeKey[32];
  121. // Path from sender to sender's supernode, "corrected" so that recipient can splice to it
  122. // without knowing sender's encoding scheme. 0 if unknown.
  123. uint8_t pathToSnode_be[8];
  124. };
  125. Assert_compileTime(sizeof(struct Control_GetSnode) == Control_GetSnode_HEADER_SIZE);
  126. static inline char* Control_typeString(uint16_t type_be)
  127. {
  128. if (type_be == Control_ERROR_be) {
  129. return "ERROR";
  130. } else if (type_be == Control_PING_be) {
  131. return "PING";
  132. } else if (type_be == Control_PONG_be) {
  133. return "PONG";
  134. } else if (type_be == Control_KEYPING_be) {
  135. return "KEYPING";
  136. } else if (type_be == Control_KEYPONG_be) {
  137. return "KEYPONG";
  138. } else if (type_be == Control_GETSNODE_QUERY_be) {
  139. return "GETSNODE_QUERY";
  140. } else if (type_be == Control_GETSNODE_REPLY_be) {
  141. return "GETSNODE_REPLY";
  142. } else {
  143. return "UNKNOWN";
  144. }
  145. }
  146. struct Control_Header
  147. {
  148. /**
  149. * This should be the one's complement checksum
  150. * of the control packet with 0'd checksum field.
  151. */
  152. uint16_t checksum_be;
  153. /** The type of control message, eg: Control_ERROR. */
  154. uint16_t type_be;
  155. };
  156. #define Control_Header_SIZE 4
  157. Assert_compileTime(sizeof(struct Control_Header) == Control_Header_SIZE);
  158. /**
  159. * A return message which is treated specially by switches.
  160. *
  161. * 1 2 3
  162. * 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
  163. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  164. * 0 | Checksum | Type |
  165. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  166. * 4 | |
  167. * + First <Length> Bytes of Packet Which Caused The Error +
  168. * 8 | |
  169. *
  170. */
  171. struct Control
  172. {
  173. struct Control_Header header;
  174. union {
  175. struct Control_Error error;
  176. struct Control_Ping ping;
  177. struct Control_Ping pong;
  178. struct Control_KeyPing keyPing;
  179. struct Control_Ping keyPong;
  180. struct Control_GetSnode getSnode;
  181. /** The control packet content. */
  182. uint8_t bytes[4];
  183. } content;
  184. };
  185. // Control_KeyPing is the largest structure and thus defines the length of the "content" union.
  186. Assert_compileTime(
  187. sizeof(struct Control) == (Control_Header_SIZE + Control_GetSnode_HEADER_SIZE)
  188. );
  189. #endif