PFChan.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <http://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_Ping
  43. {
  44. uint64_t cookie;
  45. };
  46. #define PFChan_Ping_SIZE 8
  47. Assert_compileTime(sizeof(struct PFChan_Ping) == PFChan_Ping_SIZE);
  48. struct PFChan_Pathfinder_Connect
  49. {
  50. /**
  51. * See PFChan_Pathfinder_Superiority for more information about this field.
  52. * It is recommended that you pass zero at first and then pass a higher number once your table
  53. * has populated. Use an PFChan_Pathfinder_SUPERIORITY event to alter it.
  54. */
  55. uint32_t superiority_be;
  56. /** Protocol version of the pathfinder. */
  57. uint32_t version_be;
  58. /** Description of the pathfinder in ASCII text. */
  59. uint8_t userAgent[64];
  60. };
  61. #define PFChan_Pathfinder_Connect_SIZE 72
  62. Assert_compileTime(sizeof(struct PFChan_Pathfinder_Connect) == PFChan_Pathfinder_Connect_SIZE);
  63. /**
  64. * Sending a PFChan_Pathfinder_SUPERIORITY event will cause PFChan_Core_PATHFINDER event to be sent
  65. * again for your pathfinder with the updated superiority. Superiority is a way for multiple
  66. * connected pathfinders to operate together by ones which have lower superiority switching
  67. * to an "idle" mode, relying on the responses to the DHT pings and searches sent by the higher
  68. * superiority pathfinder.
  69. */
  70. struct PFChan_Pathfinder_Superiority
  71. {
  72. uint32_t superiority_be;
  73. };
  74. #define PFChan_Pathfinder_Superiority_SIZE 4
  75. Assert_compileTime(
  76. sizeof(struct PFChan_Pathfinder_Superiority) == PFChan_Pathfinder_Superiority_SIZE);
  77. enum PFChan_Pathfinder
  78. {
  79. /** Below the lowest valid value. */
  80. PFChan_Pathfinder__TOO_LOW = 511,
  81. /**
  82. * Must be emitted before any other messages.
  83. * (Received by: EventEmitter.c)
  84. */
  85. PFChan_Pathfinder_CONNECT = 512,
  86. /**
  87. * See PFChan_Pathfinder_Superiority for more information about this event.
  88. * (Received by: EventEmitter.c)
  89. */
  90. PFChan_Pathfinder_SUPERIORITY = 513,
  91. /**
  92. * Emit to indicate the discovery of a node or a new best path to the node.
  93. * To reduce traffic load, first request all sessions and then only emit for nodes for which
  94. * there are active sessions.
  95. * (Received by: SessionManager.c)
  96. */
  97. PFChan_Pathfinder_NODE = 514,
  98. /**
  99. * Send a DHT message to another node.
  100. * (Received by: UpperDistributor.c)
  101. */
  102. PFChan_Pathfinder_SENDMSG = 515,
  103. /**
  104. * PFChan_Pathfinder_PING will elicit an PFChan_Core_PONG
  105. * (Received by: EventEmitter.c)
  106. */
  107. PFChan_Pathfinder_PING = 516,
  108. /**
  109. * PFChan_Pathfinder_PONG must be sent if core sends a PFChan_Core_PING
  110. * (Received by: EventEmitter.c)
  111. */
  112. PFChan_Pathfinder_PONG = 517,
  113. // The following events have no content.
  114. /**
  115. * Get all sessions.
  116. * (Received by: SessionManager.c)
  117. */
  118. PFChan_Pathfinder_SESSIONS = 518,
  119. /**
  120. * Get all peers.
  121. * (Received by: InterfaceController.c)
  122. */
  123. PFChan_Pathfinder_PEERS = 519,
  124. /**
  125. * Get all registered pathfinders
  126. * (Received by: EventEmitter.c)
  127. */
  128. PFChan_Pathfinder_PATHFINDERS = 520,
  129. PFChan_Pathfinder__TOO_HIGH = 521,
  130. };
  131. struct PFChan_FromPathfinder
  132. {
  133. enum PFChan_Pathfinder event_be;
  134. /* Number of the Pathfinder which sent this event, added by EventEmitter.c */
  135. uint8_t target_be;
  136. union {
  137. struct PFChan_Pathfinder_Connect connect;
  138. struct PFChan_Pathfinder_Superiority superiority;
  139. struct PFChan_Node node;
  140. struct PFChan_Msg sendmsg;
  141. struct PFChan_Ping ping;
  142. struct PFChan_Ping pong;
  143. uint8_t bytes[1];
  144. } content;
  145. };
  146. //// ------------------------- Core Events ------------------------- ////
  147. enum PFChan_Core
  148. {
  149. /** This is below the lowest valid value */
  150. PFChan_Core__TOO_LOW = 1023,
  151. /**
  152. * This message is sent in response to an PFChan_Pathfinder_CONNECT message and is
  153. * guaranteed to be sent before any other message.
  154. * (emitted by: EventEmitter.c)
  155. */
  156. PFChan_Core_CONNECT = 1024,
  157. /**
  158. * Emitted when a pathfinder connects or if PFChan_Pathfinder_PATHFINDERS is sent.
  159. * (emitted by: EventEmitter.c)
  160. */
  161. PFChan_Core_PATHFINDER = 1025,
  162. /**
  163. * Emitted when a pathfinder disconnects from the core
  164. * (emitted by: EventEmitter.c)
  165. */
  166. PFChan_Core_PATHFINDER_GONE = 1026,
  167. /**
  168. * Emitted if a switch error is received, no matter what type of packet causes it.
  169. * (emitted by: ControlHandler.c)
  170. */
  171. PFChan_Core_SWITCH_ERR = 1027,
  172. /**
  173. * Emitted if the core wants the pathfinder to begin searching for a node.
  174. * (emitted by: SessionManager.c)
  175. */
  176. PFChan_Core_SEARCH_REQ = 1028,
  177. /**
  178. * Emitted when a peer connects (becomes state ESTABLISHED) or
  179. * emitted for every peer if PFChan_Pathfinder_PEERS is sent.
  180. * (emitted by: InterfaceController.c)
  181. */
  182. PFChan_Core_PEER = 1029,
  183. /**
  184. * Emitted when a peer disconnects (or becomes state UNRESPONSIVE)
  185. * (emitted by: InterfaceController.c)
  186. */
  187. PFChan_Core_PEER_GONE = 1030,
  188. /**
  189. * Emitted if a new session begins, also emitted for every active session of
  190. * PFChan_Pathfinder_SESSIONS is sent.
  191. * (emitted by: SessionManager.c)
  192. */
  193. PFChan_Core_SESSION = 1031,
  194. /**
  195. * Emitted when a session ends.
  196. * (emitted by: SessionManager.c)
  197. */
  198. PFChan_Core_SESSION_ENDED = 1032,
  199. /**
  200. * Emitted when SessionManager sees an incoming packet with a new path.
  201. * (emitted by: SessionManager.c)
  202. */
  203. PFChan_Core_DISCOVERED_PATH = 1033,
  204. /**
  205. * Emitted for each incoming DHT message.
  206. * (emitted by: UpperDistributor.c)
  207. */
  208. PFChan_Core_MSG = 1034,
  209. /**
  210. * Emitted from time to time in order to verify the pathfinder is alive.
  211. * Must be responded to by an PFChan_Pathfinder_PONG.
  212. * (emitted by: EventEmitter.c)
  213. */
  214. PFChan_Core_PING = 1035,
  215. /**
  216. * Will be emitted if the pathfinder emits an PFChan_Pathfinder_PING.
  217. * (emitted by: EventEmitter.c)
  218. */
  219. PFChan_Core_PONG = 1036,
  220. PFChan_Core__TOO_HIGH = 1037,
  221. };
  222. struct PFChan_Core_SearchReq
  223. {
  224. uint8_t ipv6[16];
  225. };
  226. #define PFChan_Core_SearchReq_SIZE 16
  227. Assert_compileTime(sizeof(struct PFChan_Core_SearchReq) == PFChan_Core_SearchReq_SIZE);
  228. struct PFChan_Core_Pathfinder
  229. {
  230. /** See struct PFChan_Pathfinder_Superiority for more information */
  231. uint32_t superiority_be;
  232. /** The number of this pathfinder. */
  233. uint32_t pathfinderId_be;
  234. /** Description of the pathfinder in ASCII text. */
  235. uint8_t userAgent[64];
  236. };
  237. #define PFChan_Core_Pathfinder_SIZE 72
  238. Assert_compileTime(sizeof(struct PFChan_Core_Pathfinder) == PFChan_Core_Pathfinder_SIZE);
  239. struct PFChan_Core_Connect
  240. {
  241. /** The core's version (Version.h). */
  242. uint32_t version_be;
  243. /** This pathfinder's ID. */
  244. uint32_t pathfinderId_be;
  245. /** The public key of this cjdns node. */
  246. uint8_t publicKey[32];
  247. };
  248. #define PFChan_Core_Connect_SIZE 40
  249. Assert_compileTime(sizeof(struct PFChan_Core_Connect) == PFChan_Core_Connect_SIZE);
  250. struct PFChan_Core_SwitchErr
  251. {
  252. struct SwitchHeader sh;
  253. uint32_t ffffffff;
  254. struct Control_Header ctrlHeader;
  255. struct Control_Error ctrlErr;
  256. };
  257. #pragma GCC poison PFChan_Core_SwitchErr_SIZE
  258. #define PFChan_Core_SwitchErr_MIN_SIZE \
  259. (SwitchHeader_SIZE + 4 + Control_Header_SIZE + Control_Error_MIN_SIZE)
  260. Assert_compileTime(sizeof(struct PFChan_Core_SwitchErr) == PFChan_Core_SwitchErr_MIN_SIZE);
  261. struct PFChan_FromCore
  262. {
  263. enum PFChan_Core event_be;
  264. /* Number of the Pathfinder to send this event to, 0xffffffff sends to all. */
  265. uint8_t target_be;
  266. union {
  267. struct PFChan_Core_Connect connect;
  268. struct PFChan_Core_Pathfinder pathfinder;
  269. struct PFChan_Core_Pathfinder pathfinderGone;
  270. struct PFChan_Core_SwitchErr switchErr;
  271. struct PFChan_Core_SearchReq searchReq;
  272. struct PFChan_Node peer;
  273. struct PFChan_Node peerGone;
  274. struct PFChan_Node session;
  275. struct PFChan_Node sessionEnded;
  276. struct PFChan_Node discoveredPath;
  277. struct PFChan_Msg msg;
  278. struct PFChan_Ping ping;
  279. struct PFChan_Ping pong;
  280. uint8_t bytes[4];
  281. } content;
  282. };
  283. // PFChan_FromCore contains a union so it's size is not useful.
  284. #pragma GCC poison PFChan_FromCore_SIZE
  285. #endif