PFChan.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 PFChan_H
  16. #define PFChan_H
  17. #include "util/Assert.h"
  18. #include "wire/RouteHeader.h"
  19. #include "wire/DataHeader.h"
  20. #include "wire/SwitchHeader.h"
  21. #include "wire/Control.h"
  22. struct PFChan_Node
  23. {
  24. uint8_t ip6[16];
  25. uint8_t publicKey[32];
  26. uint64_t path_be;
  27. /** Quality of path represented by switch label (0:best ffffffff:worst) */
  28. uint32_t metric_be;
  29. uint32_t version_be;
  30. };
  31. #define PFChan_Node_SIZE 64
  32. Assert_compileTime(sizeof(struct PFChan_Node) == PFChan_Node_SIZE);
  33. struct PFChan_Msg
  34. {
  35. struct RouteHeader route;
  36. struct DataHeader data;
  37. // ...content follows...
  38. };
  39. #define PFChan_Msg_MIN_SIZE (RouteHeader_SIZE + DataHeader_SIZE)
  40. Assert_compileTime(sizeof(struct PFChan_Msg) == PFChan_Msg_MIN_SIZE);
  41. #pragma GCC poison PFChan_Msg_SIZE
  42. struct PFChan_CtrlMsg
  43. {
  44. struct RouteHeader route;
  45. struct Control_Header ctrlHdr;
  46. };
  47. #define PFChan_CtrlMsg_MIN_SIZE (RouteHeader_SIZE + Control_Header_SIZE)
  48. Assert_compileTime(sizeof(struct PFChan_Msg) == PFChan_CtrlMsg_MIN_SIZE);
  49. #pragma GCC poison PFChan_CtrlMsg_SIZE
  50. struct PFChan_Ping
  51. {
  52. uint64_t cookie;
  53. };
  54. #define PFChan_Ping_SIZE 8
  55. Assert_compileTime(sizeof(struct PFChan_Ping) == PFChan_Ping_SIZE);
  56. struct PFChan_Pathfinder_Connect
  57. {
  58. /**
  59. * See PFChan_Pathfinder_Superiority for more information about this field.
  60. * It is recommended that you pass zero at first and then pass a higher number once your table
  61. * has populated. Use an PFChan_Pathfinder_SUPERIORITY event to alter it.
  62. */
  63. uint32_t superiority_be;
  64. /** Protocol version of the pathfinder. */
  65. uint32_t version_be;
  66. /** Description of the pathfinder in ASCII text. */
  67. uint8_t userAgent[64];
  68. };
  69. #define PFChan_Pathfinder_Connect_SIZE 72
  70. Assert_compileTime(sizeof(struct PFChan_Pathfinder_Connect) == PFChan_Pathfinder_Connect_SIZE);
  71. /**
  72. * Sending a PFChan_Pathfinder_SUPERIORITY event will cause PFChan_Core_PATHFINDER event to be sent
  73. * again for your pathfinder with the updated superiority. Superiority is a way for multiple
  74. * connected pathfinders to operate together by ones which have lower superiority switching
  75. * to an "idle" mode, relying on the responses to the DHT pings and searches sent by the higher
  76. * superiority pathfinder.
  77. */
  78. struct PFChan_Pathfinder_Superiority
  79. {
  80. uint32_t superiority_be;
  81. };
  82. #define PFChan_Pathfinder_Superiority_SIZE 4
  83. Assert_compileTime(
  84. sizeof(struct PFChan_Pathfinder_Superiority) == PFChan_Pathfinder_Superiority_SIZE);
  85. enum PFChan_Pathfinder
  86. {
  87. /** Below the lowest valid value. */
  88. PFChan_Pathfinder__TOO_LOW = 511,
  89. /**
  90. * Must be emitted before any other messages.
  91. * (Received by: EventEmitter.c)
  92. */
  93. PFChan_Pathfinder_CONNECT = 512,
  94. /**
  95. * See PFChan_Pathfinder_Superiority for more information about this event.
  96. * (Received by: EventEmitter.c)
  97. */
  98. PFChan_Pathfinder_SUPERIORITY = 513,
  99. /**
  100. * Emit to indicate the discovery of a node or a new best path to the node.
  101. * To reduce traffic load, first request all sessions and then only emit for nodes for which
  102. * there are active sessions.
  103. * (Received by: SessionManager.c)
  104. */
  105. PFChan_Pathfinder_NODE = 514,
  106. /**
  107. * Send a DHT message to another node.
  108. * (Received by: UpperDistributor.c)
  109. */
  110. PFChan_Pathfinder_SENDMSG = 515,
  111. /**
  112. * PFChan_Pathfinder_PING will elicit an PFChan_Core_PONG
  113. * (Received by: EventEmitter.c)
  114. */
  115. PFChan_Pathfinder_PING = 516,
  116. /**
  117. * PFChan_Pathfinder_PONG must be sent if core sends a PFChan_Core_PING
  118. * (Received by: EventEmitter.c)
  119. */
  120. PFChan_Pathfinder_PONG = 517,
  121. // The following events have no content.
  122. /**
  123. * Get all sessions.
  124. * (Received by: SessionManager.c)
  125. */
  126. PFChan_Pathfinder_SESSIONS = 518,
  127. /**
  128. * Get all peers.
  129. * (Received by: InterfaceController.c)
  130. */
  131. PFChan_Pathfinder_PEERS = 519,
  132. /**
  133. * Get all registered pathfinders
  134. * (Received by: EventEmitter.c)
  135. */
  136. PFChan_Pathfinder_PATHFINDERS = 520,
  137. /**
  138. * Send from the Pathfinder in order to send off a CTRL message.
  139. * Send under this, a RouteHeader and the control frame after it.
  140. * (Received by: UpperDistributor.c)
  141. */
  142. PFChan_Pathfinder_CTRL_SENDMSG = 521,
  143. /**
  144. * You must send this whenever you have adopted a supernode.
  145. * Send with address set to all zeros if you lost your supernode.
  146. * (Received by: ControlHandler.c)
  147. */
  148. PFChan_Pathfinder_SNODE = 522,
  149. PFChan_Pathfinder__TOO_HIGH = 523,
  150. };
  151. struct PFChan_FromPathfinder
  152. {
  153. enum PFChan_Pathfinder event_be;
  154. /* Number of the Pathfinder which sent this event, added by EventEmitter.c */
  155. uint8_t target_be;
  156. union {
  157. struct PFChan_Pathfinder_Connect connect;
  158. struct PFChan_Pathfinder_Superiority superiority;
  159. struct PFChan_Node node;
  160. struct PFChan_Msg sendmsg;
  161. struct PFChan_Ping ping;
  162. struct PFChan_Ping pong;
  163. uint8_t bytes[1];
  164. } content;
  165. };
  166. //// ------------------------- Core Events ------------------------- ////
  167. enum PFChan_Core
  168. {
  169. /** This is below the lowest valid value */
  170. PFChan_Core__TOO_LOW = 1023,
  171. /**
  172. * This message is sent in response to an PFChan_Pathfinder_CONNECT message and is
  173. * guaranteed to be sent before any other message.
  174. * (emitted by: EventEmitter.c)
  175. */
  176. PFChan_Core_CONNECT = 1024,
  177. /**
  178. * Emitted when a pathfinder connects or if PFChan_Pathfinder_PATHFINDERS is sent.
  179. * (emitted by: EventEmitter.c)
  180. */
  181. PFChan_Core_PATHFINDER = 1025,
  182. /**
  183. * Emitted when a pathfinder disconnects from the core
  184. * (emitted by: EventEmitter.c)
  185. */
  186. PFChan_Core_PATHFINDER_GONE = 1026,
  187. /**
  188. * Emitted if a switch error is received, no matter what type of packet causes it.
  189. * (emitted by: ControlHandler.c)
  190. */
  191. PFChan_Core_SWITCH_ERR = 1027,
  192. /**
  193. * Emitted if the core wants the pathfinder to begin searching for a node.
  194. * (emitted by: SessionManager.c)
  195. */
  196. PFChan_Core_SEARCH_REQ = 1028,
  197. /**
  198. * Emitted when a peer connects (becomes state ESTABLISHED) or
  199. * emitted for every peer if PFChan_Pathfinder_PEERS is sent.
  200. * (emitted by: InterfaceController.c)
  201. */
  202. PFChan_Core_PEER = 1029,
  203. /**
  204. * Emitted when a peer disconnects (or becomes state UNRESPONSIVE)
  205. * (emitted by: InterfaceController.c)
  206. */
  207. PFChan_Core_PEER_GONE = 1030,
  208. /**
  209. * Emitted if a new session begins, also emitted for every active session of
  210. * PFChan_Pathfinder_SESSIONS is sent.
  211. * (emitted by: SessionManager.c)
  212. */
  213. PFChan_Core_SESSION = 1031,
  214. /**
  215. * Emitted when a session ends.
  216. * (emitted by: SessionManager.c)
  217. */
  218. PFChan_Core_SESSION_ENDED = 1032,
  219. /**
  220. * Emitted when SessionManager sees an incoming packet with a new path.
  221. * (emitted by: SessionManager.c)
  222. */
  223. PFChan_Core_DISCOVERED_PATH = 1033,
  224. /**
  225. * Emitted for each incoming DHT message.
  226. * (emitted by: UpperDistributor.c)
  227. */
  228. PFChan_Core_MSG = 1034,
  229. /**
  230. * Emitted from time to time in order to verify the pathfinder is alive.
  231. * Must be responded to by an PFChan_Pathfinder_PONG.
  232. * (emitted by: EventEmitter.c)
  233. */
  234. PFChan_Core_PING = 1035,
  235. /**
  236. * Will be emitted if the pathfinder emits an PFChan_Pathfinder_PING.
  237. * (emitted by: EventEmitter.c)
  238. */
  239. PFChan_Core_PONG = 1036,
  240. /**
  241. * Will be emitted by the core when a control message (response) is incoming.
  242. * TODO(cjd): This doesn't cover all control message types yet.
  243. */
  244. PFChan_Core_CTRL_MSG = 1037,
  245. /**
  246. * Will be emitted when the core has a path to a node but the session is not setup.
  247. * Structure is a PFChan_Node
  248. * (emitted by: SessionManager.c)
  249. */
  250. PFChan_Core_UNSETUP_SESSION = 1038,
  251. /**
  252. * Will be emitted once every 3 seconds to inform pathfinders of the link state of
  253. * the peering links, contains an array of PFChan_LinkState_Entry.
  254. * (emitted by: InterfaceController.c)
  255. */
  256. PFChan_Core_LINK_STATE = 1039,
  257. PFChan_Core__TOO_HIGH = 1040,
  258. };
  259. // All values are in host order
  260. struct PFChan_LinkState_Entry {
  261. uint64_t peerLabel;
  262. uint64_t sumOfPackets;
  263. uint64_t sumOfDrops;
  264. uint64_t sumOfKb;
  265. };
  266. #define PFChan_LinkState_Entry_SIZE 32
  267. Assert_compileTime(sizeof(struct PFChan_LinkState_Entry) == PFChan_LinkState_Entry_SIZE);
  268. struct PFChan_Core_SearchReq
  269. {
  270. uint8_t ipv6[16];
  271. uint32_t pad;
  272. uint32_t version_be;
  273. };
  274. #define PFChan_Core_SearchReq_SIZE 24
  275. Assert_compileTime(sizeof(struct PFChan_Core_SearchReq) == PFChan_Core_SearchReq_SIZE);
  276. struct PFChan_Core_Pathfinder
  277. {
  278. /** See struct PFChan_Pathfinder_Superiority for more information */
  279. uint32_t superiority_be;
  280. /** The number of this pathfinder. */
  281. uint32_t pathfinderId_be;
  282. /** Description of the pathfinder in ASCII text. */
  283. uint8_t userAgent[64];
  284. };
  285. #define PFChan_Core_Pathfinder_SIZE 72
  286. Assert_compileTime(sizeof(struct PFChan_Core_Pathfinder) == PFChan_Core_Pathfinder_SIZE);
  287. struct PFChan_Core_Connect
  288. {
  289. /** The core's version (Version.h). */
  290. uint32_t version_be;
  291. /** This pathfinder's ID. */
  292. uint32_t pathfinderId_be;
  293. /** The public key of this cjdns node. */
  294. uint8_t publicKey[32];
  295. };
  296. #define PFChan_Core_Connect_SIZE 40
  297. Assert_compileTime(sizeof(struct PFChan_Core_Connect) == PFChan_Core_Connect_SIZE);
  298. struct PFChan_Core_SwitchErr
  299. {
  300. struct SwitchHeader sh;
  301. struct Control_Header ctrlHeader;
  302. struct Control_Error ctrlErr;
  303. struct SwitchHeader shAtErrorHop;
  304. };
  305. #pragma GCC poison PFChan_Core_SwitchErr_SIZE
  306. #define PFChan_Core_SwitchErr_MIN_SIZE \
  307. (SwitchHeader_SIZE + Control_Header_SIZE + Control_Error_MIN_SIZE + SwitchHeader_SIZE)
  308. Assert_compileTime(sizeof(struct PFChan_Core_SwitchErr) == PFChan_Core_SwitchErr_MIN_SIZE);
  309. struct PFChan_FromCore
  310. {
  311. enum PFChan_Core event_be;
  312. /* Number of the Pathfinder to send this event to, 0xffffffff sends to all. */
  313. uint8_t target_be;
  314. union {
  315. struct PFChan_Core_Connect connect;
  316. struct PFChan_Core_Pathfinder pathfinder;
  317. struct PFChan_Core_Pathfinder pathfinderGone;
  318. struct PFChan_Core_SwitchErr switchErr;
  319. struct PFChan_Core_SearchReq searchReq;
  320. struct PFChan_Node peer;
  321. struct PFChan_Node peerGone;
  322. struct PFChan_Node session;
  323. struct PFChan_Node sessionEnded;
  324. struct PFChan_Node discoveredPath;
  325. struct PFChan_Msg msg;
  326. struct PFChan_Ping ping;
  327. struct PFChan_Ping pong;
  328. struct PFChan_LinkState_Entry linkState;
  329. uint8_t bytes[4];
  330. } content;
  331. };
  332. // PFChan_FromCore contains a union so it's size is not useful.
  333. #pragma GCC poison PFChan_FromCore_SIZE
  334. #endif