1
0

Control.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #define Control_RPATH_QUERY_be Endian_hostToBigEndian16(9)
  127. #define Control_RPATH_QUERY_MAGIC Endian_hostToBigEndian32(0x736e6f71) // rpaq
  128. #define Control_RPATH_REPLY_be Endian_hostToBigEndian16(10)
  129. #define Control_RPATH_REPLY_MAGIC Endian_hostToBigEndian32(0x736e6f72) // rpar
  130. #define Control_RPath_HEADER_SIZE 16
  131. struct Control_RPath
  132. {
  133. // Control_RPATH_QUERY_MAGIC for queries
  134. // Control_RPATH_REPLY_MAGIC for replies
  135. uint32_t magic;
  136. // Version of the node sending the query or the reply
  137. uint32_t version_be;
  138. // The reverse path back to the node who sent the query
  139. uint8_t rpath_be[8];
  140. };
  141. static inline char* Control_typeString(uint16_t type_be)
  142. {
  143. if (type_be == Control_ERROR_be) {
  144. return "ERROR";
  145. } else if (type_be == Control_PING_be) {
  146. return "PING";
  147. } else if (type_be == Control_PONG_be) {
  148. return "PONG";
  149. } else if (type_be == Control_KEYPING_be) {
  150. return "KEYPING";
  151. } else if (type_be == Control_KEYPONG_be) {
  152. return "KEYPONG";
  153. } else if (type_be == Control_GETSNODE_QUERY_be) {
  154. return "GETSNODE_QUERY";
  155. } else if (type_be == Control_GETSNODE_REPLY_be) {
  156. return "GETSNODE_REPLY";
  157. } else if (type_be == Control_RPATH_QUERY_be) {
  158. return "RPATH_QUERY";
  159. } else if (type_be == Control_RPATH_REPLY_be) {
  160. return "RPATH_REPLY";
  161. } else {
  162. return "UNKNOWN";
  163. }
  164. }
  165. struct Control_Header
  166. {
  167. /**
  168. * This should be the one's complement checksum
  169. * of the control packet with 0'd checksum field.
  170. */
  171. uint16_t checksum_be;
  172. /** The type of control message, eg: Control_ERROR. */
  173. uint16_t type_be;
  174. };
  175. #define Control_Header_SIZE 4
  176. Assert_compileTime(sizeof(struct Control_Header) == Control_Header_SIZE);
  177. /**
  178. * A return message which is treated specially by switches.
  179. *
  180. * 1 2 3
  181. * 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
  182. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  183. * 0 | Checksum | Type |
  184. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  185. * 4 | |
  186. * + First <Length> Bytes of Packet Which Caused The Error +
  187. * 8 | |
  188. *
  189. */
  190. struct Control
  191. {
  192. struct Control_Header header;
  193. union {
  194. struct Control_Error error;
  195. struct Control_Ping ping;
  196. struct Control_Ping pong;
  197. struct Control_KeyPing keyPing;
  198. struct Control_Ping keyPong;
  199. struct Control_GetSnode getSnode;
  200. struct Control_RPath rpath;
  201. /** The control packet content. */
  202. uint8_t bytes[4];
  203. } content;
  204. };
  205. // Control_KeyPing is the largest structure and thus defines the length of the "content" union.
  206. Assert_compileTime(
  207. sizeof(struct Control) == (Control_Header_SIZE + Control_GetSnode_HEADER_SIZE)
  208. );
  209. #endif