Control.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. typedef struct Control_LlAddr_Payload {
  142. uint8_t type;
  143. uint8_t len;
  144. } Control_LlAddr_Payload_t;
  145. Assert_compileTime(sizeof(Control_LlAddr_Payload_t) == 2);
  146. static const uint8_t Control_LlAddr_Udp4_TYPE = 1;
  147. typedef struct Control_LlAddr_Udp4 {
  148. uint8_t type; // Control_LlAddr_Udp4_TYPE
  149. uint8_t len; // 8
  150. uint16_t port_be;
  151. uint8_t addr[4];
  152. } Control_LlAddr_Udp4_t;
  153. Assert_compileTime(sizeof(Control_LlAddr_Udp4_t) == 8);
  154. static const uint8_t Control_LlAddr_Udp6_TYPE = 2;
  155. typedef struct Control_LlAddr_Udp6 {
  156. uint8_t type; // Control_LlAddr_Udp6_TYPE
  157. uint8_t len; // 20
  158. uint16_t port_be;
  159. uint8_t addr[16];
  160. } Control_LlAddr_Udp6_t;
  161. Assert_compileTime(sizeof(Control_LlAddr_Udp6_t) == 20);
  162. static const uint8_t Control_LlAddr_Other_TYPE = 3;
  163. typedef struct Control_LlAddr_Other {
  164. uint8_t type; // Control_LlAddr_Other_TYPE
  165. uint8_t len; // 32
  166. uint8_t sockaddrHeader[30];
  167. } Control_LlAddr_Other_t;
  168. Assert_compileTime(sizeof(Control_LlAddr_Other_t) == 32);
  169. #define Control_LlAddr_QUERY_be Endian_hostToBigEndian16(11)
  170. #define Control_LlAddr_QUERY_MAGIC Endian_hostToBigEndian32(0x6c6c6171) // llaq
  171. #define Control_LlAddr_REPLY_be Endian_hostToBigEndian16(12)
  172. static const uint32_t Control_LlAddr_REPLY_MAGIC = Endian_hostToBigEndian32(0x6c6c6172); // llar
  173. #define Control_LlAddr_HEADER_SIZE 40
  174. // Following the LlAddr message, there is additional opaque data that is reflected back.
  175. typedef struct Control_LlAddr
  176. {
  177. // Control_LlAddr_QUERY_MAGIC for queries
  178. // Control_LlAddr_REPLY_MAGIC for replies
  179. uint32_t magic;
  180. // Version of the node sending the query or the reply
  181. uint32_t version_be;
  182. // The address of the sender
  183. union {
  184. Control_LlAddr_Payload_t payload;
  185. Control_LlAddr_Udp4_t udp4;
  186. Control_LlAddr_Udp6_t udp6;
  187. Control_LlAddr_Other_t other;
  188. } addr;
  189. } Control_LlAddr_t;
  190. Assert_compileTime(sizeof(Control_LlAddr_t) == Control_LlAddr_HEADER_SIZE);
  191. static inline char* Control_typeString(uint16_t type_be)
  192. {
  193. if (type_be == Control_ERROR_be) {
  194. return "ERROR";
  195. } else if (type_be == Control_PING_be) {
  196. return "PING";
  197. } else if (type_be == Control_PONG_be) {
  198. return "PONG";
  199. } else if (type_be == Control_KEYPING_be) {
  200. return "KEYPING";
  201. } else if (type_be == Control_KEYPONG_be) {
  202. return "KEYPONG";
  203. } else if (type_be == Control_GETSNODE_QUERY_be) {
  204. return "GETSNODE_QUERY";
  205. } else if (type_be == Control_GETSNODE_REPLY_be) {
  206. return "GETSNODE_REPLY";
  207. } else if (type_be == Control_RPATH_QUERY_be) {
  208. return "RPATH_QUERY";
  209. } else if (type_be == Control_RPATH_REPLY_be) {
  210. return "RPATH_REPLY";
  211. } else if (type_be == Control_LlAddr_QUERY_be) {
  212. return "LLADDR_QUERY";
  213. } else if (type_be == Control_LlAddr_REPLY_be) {
  214. return "LLADDR_REPLY";
  215. } else {
  216. return "UNKNOWN";
  217. }
  218. }
  219. struct Control_Header
  220. {
  221. /**
  222. * This should be the one's complement checksum
  223. * of the control packet with 0'd checksum field.
  224. */
  225. uint16_t checksum_be;
  226. /** The type of control message, eg: Control_ERROR. */
  227. uint16_t type_be;
  228. };
  229. #define Control_Header_SIZE 4
  230. Assert_compileTime(sizeof(struct Control_Header) == Control_Header_SIZE);
  231. /**
  232. * A return message which is treated specially by switches.
  233. *
  234. * 1 2 3
  235. * 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
  236. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  237. * 0 | Checksum | Type |
  238. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  239. * 4 | |
  240. * + First <Length> Bytes of Packet Which Caused The Error +
  241. * 8 | |
  242. *
  243. */
  244. struct Control
  245. {
  246. struct Control_Header header;
  247. union {
  248. struct Control_Error error;
  249. struct Control_Ping ping;
  250. struct Control_Ping pong;
  251. struct Control_KeyPing keyPing;
  252. struct Control_Ping keyPong;
  253. struct Control_GetSnode getSnode;
  254. struct Control_RPath rpath;
  255. struct Control_LlAddr lladdr;
  256. /** The control packet content. */
  257. uint8_t bytes[4];
  258. } content;
  259. };
  260. // Control_KeyPing is the largest structure and thus defines the length of the "content" union.
  261. Assert_compileTime(
  262. sizeof(struct Control) == (Control_Header_SIZE + Control_GetSnode_HEADER_SIZE)
  263. );
  264. #endif