Announce.h 17 KB

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