Announce.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 Announce_H
  16. #define Announce_H
  17. #include "util/version/Version.h"
  18. #include "util/Assert.h"
  19. #include "util/Endian.h"
  20. #include "util/Bits.h"
  21. #include "util/Gcc.h"
  22. // NOTE: Length of 0 in a Announce message is invalid.
  23. // Length of 1 is by definition a pad byte.
  24. // Length field allows parsers to skip over entries which they do not understand.
  25. enum Announce_Type {
  26. Announce_Type_ENCODING_SCHEME,
  27. Announce_Type_PEER,
  28. Announce_Type_VERSION,
  29. Announce_Type_LINK_STATE
  30. };
  31. struct Announce_Version
  32. {
  33. // Announce_Version_SIZE
  34. uint8_t length;
  35. // Announce_Type_VERSION
  36. uint8_t type;
  37. uint16_t version_be;
  38. };
  39. #define Announce_Version_SIZE 4
  40. Assert_compileTime(sizeof(struct Announce_Version) == Announce_Version_SIZE);
  41. static inline void Announce_Version_init(struct Announce_Version* v)
  42. {
  43. v->length = Announce_Version_SIZE;
  44. v->type = Announce_Type_VERSION;
  45. v->version_be = Endian_hostToBigEndian16(Version_CURRENT_PROTOCOL);
  46. }
  47. struct Announce_EncodingScheme
  48. {
  49. // Length of `scheme` + 2
  50. uint8_t length;
  51. // Announce_Type_ENCODING_SCHEME
  52. uint8_t type;
  53. // real length is `length` - 2
  54. uint8_t scheme[2];
  55. };
  56. static inline void Announce_EncodingScheme_push(struct Message* pushTo, String* compressedScheme)
  57. {
  58. Assert_true(compressedScheme->len + 2 < 256);
  59. Message_push(pushTo, compressedScheme->bytes, compressedScheme->len, NULL);
  60. Message_push8(pushTo, Announce_Type_ENCODING_SCHEME, NULL);
  61. Message_push8(pushTo, compressedScheme->len + 2, NULL);
  62. while ((uintptr_t)pushTo->bytes % 4) {
  63. Message_push8(pushTo, 1, NULL);
  64. }
  65. }
  66. /**
  67. * 1 2 3
  68. * 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
  69. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  70. * 0 | length | type | encodingForm | flags |
  71. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  72. * 4 | MTU (8 byte units) | peer number |
  73. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  74. * 8 | Unused |
  75. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  76. * 12 | |
  77. * + +
  78. * 16 | |
  79. * + Peer IPv6 +
  80. * 20 | |
  81. * + +
  82. * 24 | |
  83. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  84. * 28 | label |
  85. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  86. */
  87. struct Announce_Peer
  88. {
  89. // Announce_Peer_SIZE
  90. uint8_t length;
  91. // Announce_Type_PEER
  92. uint8_t type;
  93. // The number of the encoding form needed for getting to this node via the peer.
  94. uint8_t encodingFormNum;
  95. // no flags yet but maybe in the future...
  96. uint8_t flags;
  97. // MTU of the link in 8 byte units.
  98. // 0 is unknown
  99. // 0xffff = MTU of 542280 bytes
  100. uint16_t mtu8_be;
  101. // Number of the peer in the list, used for referencing in LinkState
  102. // 0xffff is unknown
  103. uint16_t peerNum_be;
  104. // 0xffffffff
  105. uint32_t unused;
  106. // Ipv6 of a node from which this node is reachable
  107. uint8_t ipv6[16];
  108. // Label for getting to this node from the given node
  109. // 0 means withdraw the link.
  110. uint32_t label_be;
  111. };
  112. #define Announce_Peer_SIZE 32
  113. Assert_compileTime(sizeof(struct Announce_Peer) == Announce_Peer_SIZE);
  114. static inline void Announce_Peer_init(struct Announce_Peer* peer)
  115. {
  116. Bits_memset(peer, 0, Announce_Peer_SIZE);
  117. peer->length = Announce_Peer_SIZE;
  118. peer->type = Announce_Type_PEER;
  119. peer->unused = 0xffffffff;
  120. peer->peerNum_be = 0xffff;
  121. }
  122. /**
  123. * 1 2 3
  124. * 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
  125. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  126. * 0 | length | type | padding | |
  127. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
  128. * 4 | Compressed Link State..... |
  129. * + +
  130. */
  131. struct Announce_LinkState
  132. {
  133. // Length of linkState + 3
  134. uint8_t length;
  135. // Announce_Type_LINK_STATE
  136. uint8_t type;
  137. // number of zero bytes before beginning of packed numbers
  138. uint8_t padding;
  139. // linkState
  140. uint8_t linkState[1];
  141. };
  142. static inline void Announce_LinkState_applyHeader(struct Message* pushTo)
  143. {
  144. Assert_failure("todo implement");
  145. }
  146. struct Announce_ItemHeader
  147. {
  148. uint8_t length;
  149. uint8_t type;
  150. };
  151. /**
  152. * 1 2 3
  153. * 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
  154. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  155. * 0 | |
  156. * + +
  157. * 4 | |
  158. * + +
  159. * 8 | |
  160. * + +
  161. * 12 | |
  162. * + +
  163. * 16 | |
  164. * + +
  165. * 20 | |
  166. * + +
  167. * 24 | |
  168. * + +
  169. * 28 | |
  170. * + Signature +
  171. * 32 | |
  172. * + +
  173. * 36 | |
  174. * + +
  175. * 40 | |
  176. * + +
  177. * 44 | |
  178. * + +
  179. * 48 | |
  180. * + +
  181. * 52 | |
  182. * + +
  183. * 56 | |
  184. * + +
  185. * 60 | |
  186. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  187. * 64 | |
  188. * + +
  189. * 68 | |
  190. * + +
  191. * 72 | |
  192. * + +
  193. * 76 | |
  194. * + Public Signing Key +
  195. * 80 | |
  196. * + +
  197. * 84 | |
  198. * + +
  199. * 88 | |
  200. * + +
  201. * 92 | |
  202. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  203. * 96 | |
  204. * + +
  205. * 100 | |
  206. * + SuperNode IP +
  207. * 104 | |
  208. * + +
  209. * 108 | |
  210. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  211. * 112 | |
  212. * + Timestamp +-+-+-+-+
  213. * 116 | |R| ver |
  214. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  215. */
  216. struct Announce_Header
  217. {
  218. // Signature of the header concatinated with the SHA-512 of the set of
  219. uint8_t signature[64];
  220. // Public signing key (can be derived to crypto key, see Sign.h)
  221. uint8_t pubSigningKey[32];
  222. // This is the IPv6 of the supernode which we are announcing to.
  223. // Including this allows supernodes to replicate only messages which:
  224. // 1. Indicate that a subnode has changed its supernode
  225. // 2. Update a link state which affects best paths between clusters of nodes that
  226. // are controlled by a given supernode.
  227. uint8_t snodeIp[16];
  228. // Milliseconds since the epoch when this message was crafted and reset flag
  229. uint8_t timeStampVersionFlags_be[8];
  230. };
  231. #define Announce_Header_SIZE 120
  232. Assert_compileTime(sizeof(struct Announce_Header) == Announce_Header_SIZE);
  233. static inline int64_t Announce_Header_getTimestamp(struct Announce_Header* hdr)
  234. {
  235. uint64_t ts_be;
  236. Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
  237. return Endian_bigEndianToHost64(ts_be) >> 4;
  238. }
  239. static inline void Announce_Header_setTimestamp(struct Announce_Header* hdr,
  240. int64_t timestamp)
  241. {
  242. uint64_t uTime = (uint64_t) timestamp;
  243. // This will fail on January 1, 20892770.
  244. // It will also fail for negative timestamps.
  245. Assert_true(!(uTime >> 60));
  246. uint64_t ts_be;
  247. Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
  248. ts_be = (ts_be & Endian_hostToBigEndian64(0x0f)) | Endian_hostToBigEndian64(uTime << 4);
  249. Bits_memcpy(hdr->timeStampVersionFlags_be, &ts_be, sizeof(uint64_t));
  250. }
  251. static inline bool Announce_Header_isReset(struct Announce_Header* hdr)
  252. {
  253. uint64_t ts_be;
  254. Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
  255. return (Endian_bigEndianToHost64(ts_be) >> 3) & 1;
  256. }
  257. static inline void Announce_Header_setReset(struct Announce_Header* hdr, bool isReset)
  258. {
  259. uint64_t ts_be;
  260. Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
  261. if (isReset) {
  262. ts_be |= Endian_hostToBigEndian64(1<<3);
  263. } else {
  264. ts_be &= ~Endian_hostToBigEndian64(1<<3);
  265. }
  266. Bits_memcpy(hdr->timeStampVersionFlags_be, &ts_be, sizeof(uint64_t));
  267. }
  268. static inline int Announce_Header_getVersion(struct Announce_Header* hdr)
  269. {
  270. uint64_t ts_be;
  271. Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
  272. return Endian_bigEndianToHost64(ts_be) & 0x07;
  273. }
  274. #define Announce_Header_CURRENT_VERSION 1
  275. static inline void Announce_Header_setVersion(struct Announce_Header* hdr, int version)
  276. {
  277. uint64_t ts_be;
  278. Bits_memcpy(&ts_be, hdr->timeStampVersionFlags_be, sizeof(uint64_t));
  279. ts_be = (ts_be & ~Endian_hostToBigEndian64(0x07)) | Endian_hostToBigEndian64(version & 0x07);
  280. Bits_memcpy(hdr->timeStampVersionFlags_be, &ts_be, sizeof(uint64_t));
  281. }
  282. static inline struct Announce_ItemHeader* Announce_ItemHeader_next(struct Message* msg, void* last)
  283. {
  284. struct Announce_ItemHeader* ih = (struct Announce_ItemHeader*) last;
  285. if (ih) {
  286. Assert_true((uint8_t*)ih > &msg->bytes[-msg->padding]);
  287. Assert_true((uint8_t*)ih < &msg->bytes[msg->length]);
  288. ih = (struct Announce_ItemHeader*) ( &((uint8_t*) ih)[ih->length] );
  289. } else {
  290. ih = (struct Announce_ItemHeader*) &msg->bytes[Announce_Header_SIZE];
  291. }
  292. while ((uint8_t*)ih < &msg->bytes[msg->length]) {
  293. if (!ih->length) { return NULL; } // invalid message
  294. if (ih->length > 1) {
  295. if ( &((uint8_t*) ih)[ih->length] > &msg->bytes[msg->length] ) {
  296. // invalid message, overflow...
  297. return NULL;
  298. }
  299. return ih;
  300. }
  301. ih = (struct Announce_ItemHeader*) ( &((uint8_t*) ih)[ih->length] );
  302. }
  303. return NULL;
  304. }
  305. static inline bool Announce_isValid(struct Message* msg)
  306. {
  307. struct Announce_ItemHeader* ih = NULL;
  308. for (;;) {
  309. ih = Announce_ItemHeader_next(msg, ih);
  310. if (!ih) { return false; }
  311. if ((uint8_t*)ih == &msg->bytes[msg->length - ih->length]) { return true; }
  312. }
  313. }
  314. static inline struct Announce_Peer* Announce_Peer_next(struct Message* msg, void* last)
  315. {
  316. struct Announce_ItemHeader* ih = (struct Announce_ItemHeader*) last;
  317. do {
  318. ih = Announce_ItemHeader_next(msg, ih);
  319. } while (ih && ih->type != Announce_Type_PEER);
  320. return (struct Announce_Peer*) ih;
  321. }
  322. #endif