Announce.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. };
  30. struct Announce_Version
  31. {
  32. // Announce_Version_SIZE
  33. uint8_t length;
  34. // Announce_Type_VERSION
  35. uint8_t type;
  36. uint16_t version_be;
  37. };
  38. #define Announce_Version_SIZE 4
  39. Assert_compileTime(sizeof(struct Announce_Version) == Announce_Version_SIZE);
  40. static void Announce_Version_init(struct Announce_Version* v)
  41. {
  42. v->length = Announce_Version_SIZE;
  43. v->type = Announce_Type_VERSION;
  44. v->version_be = Endian_hostToBigEndian16(Version_CURRENT_PROTOCOL);
  45. }
  46. struct Announce_EncodingScheme
  47. {
  48. // Length of `scheme` + 2
  49. uint8_t length;
  50. // Announce_Type_ENCODING_SCHEME
  51. uint8_t type;
  52. // real length is `length` - 2
  53. uint8_t scheme[2];
  54. };
  55. static inline void Announce_EncodingScheme_push(struct Message* pushTo, String* compressedScheme)
  56. {
  57. Assert_true(compressedScheme->len + 2 < 256);
  58. Message_push(pushTo, compressedScheme->bytes, compressedScheme->len, NULL);
  59. Message_push8(pushTo, Announce_Type_ENCODING_SCHEME, NULL);
  60. Message_push8(pushTo, compressedScheme->len + 2, NULL);
  61. while ((uintptr_t)pushTo->bytes % 4) {
  62. Message_push8(pushTo, 1, NULL);
  63. }
  64. }
  65. /**
  66. * 1 2 3
  67. * 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
  68. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  69. * 0 | length | type | encodingForm | flags |
  70. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  71. * 4 | MTU (8 byte units) | drops |
  72. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  73. * 8 | latency | penalty |
  74. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  75. * 12 | |
  76. * + +
  77. * 16 | |
  78. * + Peer IPv6 +
  79. * 20 | |
  80. * + +
  81. * 24 | |
  82. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  83. * 28 | label |
  84. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  85. */
  86. struct Announce_Peer
  87. {
  88. // Announce_Peer_SIZE
  89. uint8_t length;
  90. // Announce_Type_PEER
  91. uint8_t type;
  92. // The number of the encoding form needed for getting to this node via the peer.
  93. uint8_t encodingFormNum;
  94. // no flags yet but maybe in the future...
  95. uint8_t flags;
  96. // MTU of the link in 8 byte units.
  97. // 0 is unknown
  98. // 0xffff = MTU of 542280 bytes
  99. uint16_t mtu8_be;
  100. // Fraction of packets dropped in previous time-window (out of 65k)
  101. // 0xffff is unknown
  102. uint16_t drops_be;
  103. // Average latency of packets in previous time-window (milliseconds)
  104. // 0xffff is unknown
  105. uint16_t latency_be;
  106. // Penalty which would be applied to a packet (with current penalty 0)
  107. // if it passes through this link.
  108. // 0xffff is unknown
  109. uint16_t penalty_be;
  110. // Ipv6 of a node from which this node is reachable
  111. uint8_t ipv6[16];
  112. // Label for getting to this node from the given node
  113. // 0 means withdraw the link.
  114. uint32_t label_be;
  115. };
  116. #define Announce_Peer_SIZE 32
  117. Assert_compileTime(sizeof(struct Announce_Peer) == Announce_Peer_SIZE);
  118. static inline void Announce_Peer_init(struct Announce_Peer* peer)
  119. {
  120. Bits_memset(peer, 0, Announce_Peer_SIZE);
  121. peer->length = Announce_Peer_SIZE;
  122. peer->type = Announce_Type_PEER;
  123. }
  124. struct Announce_ItemHeader
  125. {
  126. uint8_t length;
  127. uint8_t type;
  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 | |
  134. * + +
  135. * 4 | |
  136. * + +
  137. * 8 | |
  138. * + +
  139. * 12 | |
  140. * + +
  141. * 16 | |
  142. * + +
  143. * 20 | |
  144. * + +
  145. * 24 | |
  146. * + +
  147. * 28 | |
  148. * + Signature +
  149. * 32 | |
  150. * + +
  151. * 36 | |
  152. * + +
  153. * 40 | |
  154. * + +
  155. * 44 | |
  156. * + +
  157. * 48 | |
  158. * + +
  159. * 52 | |
  160. * + +
  161. * 56 | |
  162. * + +
  163. * 60 | |
  164. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  165. * 64 | |
  166. * + +
  167. * 68 | |
  168. * + +
  169. * 72 | |
  170. * + +
  171. * 76 | |
  172. * + Public Signing Key +
  173. * 80 | |
  174. * + +
  175. * 84 | |
  176. * + +
  177. * 88 | |
  178. * + +
  179. * 92 | |
  180. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  181. * 96 | |
  182. * + +
  183. * 100 | |
  184. * + SuperNode IP +
  185. * 104 | |
  186. * + +
  187. * 108 | |
  188. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  189. * 112 | |
  190. * + Timestamp +-+-+-+-+
  191. * 116 | |R| ver |
  192. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  193. */
  194. struct Announce_Header
  195. {
  196. // Signature of the header concatinated with the SHA-512 of the set of
  197. uint8_t signature[64];
  198. // Public signing key (can be derived to crypto key, see Sign.h)
  199. uint8_t pubSigningKey[32];
  200. // This is the IPv6 of the supernode which we are announcing to.
  201. // Including this allows supernodes to replicate only messages which:
  202. // 1. Indicate that a subnode has changed its supernode
  203. // 2. Update a link state which affects best paths between clusters of nodes that
  204. // are controlled by a given supernode.
  205. uint8_t snodeIp[16];
  206. // Milliseconds since the epoch when this message was crafted and reset flag
  207. uint64_t timeStampVersionFlags_be;
  208. };
  209. #define Announce_Header_SIZE 120
  210. Assert_compileTime(sizeof(struct Announce_Header) == Announce_Header_SIZE);
  211. static inline int64_t Announce_Header_getTimestamp(struct Announce_Header* hdr)
  212. {
  213. return Endian_bigEndianToHost64(hdr->timeStampVersionFlags_be) >> 4;
  214. }
  215. static inline void Announce_Header_setTimestamp(struct Announce_Header* hdr,
  216. int64_t timestamp)
  217. {
  218. uint64_t uTime = (uint64_t) timestamp;
  219. // This will fail on January 1, 20892770.
  220. // It will also fail for negative timestamps.
  221. Assert_true(!(uTime >> 60));
  222. hdr->timeStampVersionFlags_be =
  223. (hdr->timeStampVersionFlags_be & Endian_hostToBigEndian64(0x0f)) |
  224. Endian_hostToBigEndian64(uTime << 4);
  225. }
  226. static inline bool Announce_Header_isReset(struct Announce_Header* hdr)
  227. {
  228. return (Endian_bigEndianToHost64(hdr->timeStampVersionFlags_be) >> 3) & 1;
  229. }
  230. static inline void Announce_Header_setReset(struct Announce_Header* hdr, bool isReset)
  231. {
  232. if (isReset) {
  233. hdr->timeStampVersionFlags_be |= Endian_hostToBigEndian64(1<<3);
  234. } else {
  235. hdr->timeStampVersionFlags_be &= ~Endian_hostToBigEndian64(1<<3);
  236. }
  237. }
  238. static inline int Announce_Header_getVersion(struct Announce_Header* hdr)
  239. {
  240. return Endian_bigEndianToHost64(hdr->timeStampVersionFlags_be) & 0x07;
  241. }
  242. #define Announce_Header_CURRENT_VERSION 1
  243. static inline void Announce_Header_setVersion(struct Announce_Header* hdr, int version)
  244. {
  245. hdr->timeStampVersionFlags_be =
  246. (hdr->timeStampVersionFlags_be & ~Endian_hostToBigEndian64(0x07)) |
  247. Endian_hostToBigEndian64(version & 0x07);
  248. }
  249. static inline struct Announce_ItemHeader* Announce_ItemHeader_next(struct Message* msg, void* last)
  250. {
  251. struct Announce_ItemHeader* ih = (struct Announce_ItemHeader*) last;
  252. if (ih) {
  253. Assert_true((uint8_t*)ih > &msg->bytes[-msg->padding]);
  254. Assert_true((uint8_t*)ih < &msg->bytes[msg->length]);
  255. ih = (struct Announce_ItemHeader*) ( &((uint8_t*) ih)[ih->length] );
  256. } else {
  257. ih = (struct Announce_ItemHeader*) &msg->bytes[Announce_Header_SIZE];
  258. }
  259. while ((uint8_t*)ih < &msg->bytes[msg->length]) {
  260. if (!ih->length) { return NULL; } // invalid message
  261. if (ih->length > 1) {
  262. if ( &((uint8_t*) ih)[ih->length] > &msg->bytes[msg->length] ) {
  263. // invalid message, overflow...
  264. return NULL;
  265. }
  266. return ih;
  267. }
  268. ih = (struct Announce_ItemHeader*) ( &((uint8_t*) ih)[ih->length] );
  269. }
  270. return NULL;
  271. }
  272. static inline bool Announce_isValid(struct Message* msg)
  273. {
  274. struct Announce_ItemHeader* ih = NULL;
  275. for (;;) {
  276. ih = Announce_ItemHeader_next(msg, ih);
  277. if (!ih) { return false; }
  278. if ((uint8_t*)ih == &msg->bytes[msg->length - ih->length]) { return true; }
  279. }
  280. }
  281. static inline struct Announce_Peer* Announce_Peer_next(struct Message* msg, void* last)
  282. {
  283. struct Announce_ItemHeader* ih = (struct Announce_ItemHeader*) last;
  284. do {
  285. ih = Announce_ItemHeader_next(msg, ih);
  286. } while (ih && ih->type != Announce_Type_PEER);
  287. return (struct Announce_Peer*) ih;
  288. }
  289. #endif