PFChan.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. typedef struct PFChan_Pathfinder_ConnectPeer {
  86. // If ::ffff:XXXX:XXXX then is 4 in 6
  87. uint8_t ip[16];
  88. // Public key of the peer
  89. uint8_t pubkey[32];
  90. // Null terminated, e.g. "AP_LOGIN: 65535\0"
  91. uint8_t login[16];
  92. // Null terminated, e.g. "AP_PASS: 4eqnmtOq/Mo\0"
  93. uint8_t password[24];
  94. // Protocol version of the peer
  95. uint32_t version;
  96. // Port number from the address
  97. uint16_t port;
  98. // Padding for length
  99. uint16_t _pad;
  100. } PFChan_Pathfinder_ConnectPeer_t;
  101. Assert_compileTime(sizeof(PFChan_Pathfinder_ConnectPeer_t) == 96);
  102. enum PFChan_Pathfinder
  103. {
  104. /** Below the lowest valid value. */
  105. PFChan_Pathfinder__TOO_LOW = 511,
  106. /**
  107. * Must be emitted before any other messages.
  108. * (Received by: EventEmitter.c)
  109. */
  110. PFChan_Pathfinder_CONNECT = 512,
  111. /**
  112. * See PFChan_Pathfinder_Superiority for more information about this event.
  113. * (Received by: EventEmitter.c)
  114. */
  115. PFChan_Pathfinder_SUPERIORITY = 513,
  116. /**
  117. * Emit to indicate the discovery of a node or a new best path to the node.
  118. * To reduce traffic load, first request all sessions and then only emit for nodes for which
  119. * there are active sessions.
  120. * (Received by: SessionManager.c)
  121. */
  122. PFChan_Pathfinder_NODE = 514,
  123. /**
  124. * Send a DHT message to another node.
  125. * (Received by: UpperDistributor.c)
  126. */
  127. PFChan_Pathfinder_SENDMSG = 515,
  128. /**
  129. * PFChan_Pathfinder_PING will elicit an PFChan_Core_PONG
  130. * (Received by: EventEmitter.c)
  131. */
  132. PFChan_Pathfinder_PING = 516,
  133. /**
  134. * PFChan_Pathfinder_PONG must be sent if core sends a PFChan_Core_PING
  135. * (Received by: EventEmitter.c)
  136. */
  137. PFChan_Pathfinder_PONG = 517,
  138. // The following events have no content.
  139. /**
  140. * Get all sessions.
  141. * (Received by: SessionManager.c)
  142. */
  143. PFChan_Pathfinder_SESSIONS = 518,
  144. /**
  145. * Get all peers.
  146. * (Received by: InterfaceController.c)
  147. */
  148. PFChan_Pathfinder_PEERS = 519,
  149. /**
  150. * Get all registered pathfinders
  151. * (Received by: EventEmitter.c)
  152. */
  153. PFChan_Pathfinder_PATHFINDERS = 520,
  154. /**
  155. * Send from the Pathfinder in order to send off a CTRL message.
  156. * Send under this, a RouteHeader and the control frame after it.
  157. * (Received by: UpperDistributor.c)
  158. */
  159. PFChan_Pathfinder_CTRL_SENDMSG = 521,
  160. /**
  161. * You must send this whenever you have adopted a supernode.
  162. * Send with address set to all zeros if you lost your supernode.
  163. * (Received by: ControlHandler.c)
  164. */
  165. PFChan_Pathfinder_SNODE = 522,
  166. /**
  167. * Send a peer setup request.
  168. * (Received by: InterfaceController.c)
  169. */
  170. PFChan_Pathfinder_CONNECT_PEER = 523,
  171. PFChan_Pathfinder__TOO_HIGH = 524,
  172. };
  173. typedef struct PFChan_FromPathfinder
  174. {
  175. enum PFChan_Pathfinder event_be;
  176. /* Number of the Pathfinder which sent this event, added by EventEmitter.c */
  177. uint8_t target_be;
  178. union {
  179. struct PFChan_Pathfinder_Connect connect;
  180. struct PFChan_Pathfinder_Superiority superiority;
  181. struct PFChan_Node node;
  182. struct PFChan_Msg sendmsg;
  183. struct PFChan_Ping ping;
  184. struct PFChan_Ping pong;
  185. PFChan_Pathfinder_ConnectPeer_t cp;
  186. uint8_t bytes[1];
  187. } content;
  188. } PFChan_FromPathfinder_t;
  189. //// ------------------------- Core Events ------------------------- ////
  190. enum PFChan_Core
  191. {
  192. /** This is below the lowest valid value */
  193. PFChan_Core__TOO_LOW = 1023,
  194. /**
  195. * This message is sent in response to an PFChan_Pathfinder_CONNECT message and is
  196. * guaranteed to be sent before any other message.
  197. * (emitted by: EventEmitter.c)
  198. */
  199. PFChan_Core_CONNECT = 1024,
  200. /**
  201. * Emitted when a pathfinder connects or if PFChan_Pathfinder_PATHFINDERS is sent.
  202. * (emitted by: EventEmitter.c)
  203. */
  204. PFChan_Core_PATHFINDER = 1025,
  205. /**
  206. * Emitted when a pathfinder disconnects from the core
  207. * (emitted by: EventEmitter.c)
  208. */
  209. PFChan_Core_PATHFINDER_GONE = 1026,
  210. /**
  211. * Emitted if a switch error is received, no matter what type of packet causes it.
  212. * (emitted by: ControlHandler.c)
  213. */
  214. PFChan_Core_SWITCH_ERR = 1027,
  215. /**
  216. * Emitted if the core wants the pathfinder to begin searching for a node.
  217. * (emitted by: SessionManager.c)
  218. */
  219. PFChan_Core_SEARCH_REQ = 1028,
  220. /**
  221. * Emitted when a peer connects (becomes state ESTABLISHED) or
  222. * emitted for every peer if PFChan_Pathfinder_PEERS is sent.
  223. * (emitted by: InterfaceController.c)
  224. */
  225. PFChan_Core_PEER = 1029,
  226. /**
  227. * Emitted when a peer disconnects (or becomes state UNRESPONSIVE)
  228. * (emitted by: InterfaceController.c)
  229. */
  230. PFChan_Core_PEER_GONE = 1030,
  231. /**
  232. * Emitted if a new session begins, also emitted for every active session of
  233. * PFChan_Pathfinder_SESSIONS is sent.
  234. * (emitted by: SessionManager.c)
  235. */
  236. PFChan_Core_SESSION = 1031,
  237. /**
  238. * Emitted when a session ends.
  239. * (emitted by: SessionManager.c)
  240. */
  241. PFChan_Core_SESSION_ENDED = 1032,
  242. /**
  243. * Emitted when SessionManager sees an incoming packet with a new path.
  244. * (emitted by: SessionManager.c)
  245. */
  246. PFChan_Core_DISCOVERED_PATH = 1033,
  247. /**
  248. * Emitted for each incoming DHT message.
  249. * (emitted by: UpperDistributor.c)
  250. */
  251. PFChan_Core_MSG = 1034,
  252. /**
  253. * Emitted from time to time in order to verify the pathfinder is alive.
  254. * Must be responded to by an PFChan_Pathfinder_PONG.
  255. * (emitted by: EventEmitter.c)
  256. */
  257. PFChan_Core_PING = 1035,
  258. /**
  259. * Will be emitted if the pathfinder emits an PFChan_Pathfinder_PING.
  260. * (emitted by: EventEmitter.c)
  261. */
  262. PFChan_Core_PONG = 1036,
  263. /**
  264. * Will be emitted by the core when a control message (response) is incoming.
  265. * TODO(cjd): This doesn't cover all control message types yet.
  266. */
  267. PFChan_Core_CTRL_MSG = 1037,
  268. /**
  269. * Will be emitted when the core has a path to a node but the session is not setup.
  270. * Structure is a PFChan_Node
  271. * (emitted by: SessionManager.c)
  272. */
  273. PFChan_Core_UNSETUP_SESSION = 1038,
  274. /**
  275. * Will be emitted once every 3 seconds to inform pathfinders of the link state of
  276. * the peering links, contains an array of PFChan_LinkState_Entry.
  277. * (emitted by: InterfaceController.c)
  278. */
  279. PFChan_Core_LINK_STATE = 1039,
  280. PFChan_Core__TOO_HIGH = 1040,
  281. };
  282. // All values are in host order
  283. struct PFChan_LinkState_Entry {
  284. uint64_t peerLabel;
  285. uint64_t sumOfPackets;
  286. uint64_t sumOfDrops;
  287. uint64_t sumOfKb;
  288. };
  289. #define PFChan_LinkState_Entry_SIZE 32
  290. Assert_compileTime(sizeof(struct PFChan_LinkState_Entry) == PFChan_LinkState_Entry_SIZE);
  291. struct PFChan_Core_SearchReq
  292. {
  293. uint8_t ipv6[16];
  294. uint32_t pad;
  295. uint32_t version_be;
  296. };
  297. #define PFChan_Core_SearchReq_SIZE 24
  298. Assert_compileTime(sizeof(struct PFChan_Core_SearchReq) == PFChan_Core_SearchReq_SIZE);
  299. struct PFChan_Core_Pathfinder
  300. {
  301. /** See struct PFChan_Pathfinder_Superiority for more information */
  302. uint32_t superiority_be;
  303. /** The number of this pathfinder. */
  304. uint32_t pathfinderId_be;
  305. /** Description of the pathfinder in ASCII text. */
  306. uint8_t userAgent[64];
  307. };
  308. #define PFChan_Core_Pathfinder_SIZE 72
  309. Assert_compileTime(sizeof(struct PFChan_Core_Pathfinder) == PFChan_Core_Pathfinder_SIZE);
  310. struct PFChan_Core_Connect
  311. {
  312. /** The core's version (Version.h). */
  313. uint32_t version_be;
  314. /** This pathfinder's ID. */
  315. uint32_t pathfinderId_be;
  316. /** The public key of this cjdns node. */
  317. uint8_t publicKey[32];
  318. };
  319. #define PFChan_Core_Connect_SIZE 40
  320. Assert_compileTime(sizeof(struct PFChan_Core_Connect) == PFChan_Core_Connect_SIZE);
  321. struct PFChan_Core_SwitchErr
  322. {
  323. struct SwitchHeader sh;
  324. struct Control_Header ctrlHeader;
  325. struct Control_Error ctrlErr;
  326. struct SwitchHeader shAtErrorHop;
  327. };
  328. #pragma GCC poison PFChan_Core_SwitchErr_SIZE
  329. #define PFChan_Core_SwitchErr_MIN_SIZE \
  330. (SwitchHeader_SIZE + Control_Header_SIZE + Control_Error_MIN_SIZE + SwitchHeader_SIZE)
  331. Assert_compileTime(sizeof(struct PFChan_Core_SwitchErr) == PFChan_Core_SwitchErr_MIN_SIZE);
  332. typedef struct PFChan_FromCore
  333. {
  334. enum PFChan_Core event_be;
  335. /* Number of the Pathfinder to send this event to, 0xffffffff sends to all. */
  336. uint8_t target_be;
  337. union {
  338. struct PFChan_Core_Connect connect;
  339. struct PFChan_Core_Pathfinder pathfinder;
  340. struct PFChan_Core_Pathfinder pathfinderGone;
  341. struct PFChan_Core_SwitchErr switchErr;
  342. struct PFChan_Core_SearchReq searchReq;
  343. struct PFChan_Node peer;
  344. struct PFChan_Node peerGone;
  345. struct PFChan_Node session;
  346. struct PFChan_Node sessionEnded;
  347. struct PFChan_Node discoveredPath;
  348. struct PFChan_Msg msg;
  349. struct PFChan_Ping ping;
  350. struct PFChan_Ping pong;
  351. struct PFChan_LinkState_Entry linkState;
  352. uint8_t bytes[4];
  353. } content;
  354. } PFChan_FromCore_t;
  355. // PFChan_FromCore contains a union so it's size is not useful.
  356. #pragma GCC poison PFChan_FromCore_SIZE
  357. #endif