1
0

Announce.h 17 KB

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