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