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 Control_H
16 #define Control_H
17
18 #include "wire/SwitchHeader.h"
19 #include "util/Endian.h"
20 #include "util/Assert.h"
21
22 /**
23 * Type two, error.
24 */
25 #define Control_ERROR 2
26 #define Control_ERROR_be Endian_hostToBigEndian16(Control_ERROR)
27 #define Control_Error_HEADER_SIZE 4
28 #define Control_Error_MIN_SIZE (Control_Error_HEADER_SIZE + SwitchHeader_SIZE + 4)
29 #define Control_Error_MAX_SIZE 256
30 struct Control_Error
31 {
32 /** The type of error, see Error.h */
33 uint32_t errorType_be;
34
35 /** The header of the packet which caused the error. */
36 struct SwitchHeader cause;
37
38 /** The handle which sits below the SwitchHeader. */
39 uint32_t causeHandle;
40 };
41 Assert_compileTime(sizeof(struct Control_Error) == Control_Error_MIN_SIZE);
42
43 /**
44 * Type three, ping.
45 */
46 #define Control_PING_be Endian_hostToBigEndian16(3)
47 #define Control_Ping_HEADER_SIZE 8
48 #define Control_Ping_MIN_SIZE 8
49 #define Control_Ping_MAX_SIZE 256
50 #define Control_Ping_MAGIC Endian_hostToBigEndian32(0x09f91102)
51 struct Control_Ping
52 {
53 /** Magic: equal to Control_Ping_MAGIC in a ping and Control_Pong_MAGIC in a pong. */
54 uint32_t magic;
55
56 /** The version of the sending node. */
57 uint32_t version_be;
58
59 /**
60 * Between 0 and 256 bytes of opaque data.
61 * Since a ping is inherently a message to one's self,
62 * the format is only of interest to the sender and thus undefined.
63 */
64 uint8_t data[4];
65 };
66 Assert_compileTime(sizeof(struct Control_Ping) == Control_Ping_MIN_SIZE + 4);
67
68 /**
69 * Type four, pong.
70 * A pong is identical to a ping.
71 */
72 #define Control_PONG_be Endian_hostToBigEndian16(4)
73 #define Control_Pong_HEADER_SIZE Control_Ping_HEADER_SIZE
74 #define Control_Pong_MIN_SIZE Control_Ping_MIN_SIZE
75 #define Control_Pong_MAX_SIZE Control_Ping_MAX_SIZE
76 #define Control_Pong_MAGIC Endian_hostToBigEndian32(0x9d74e35b)
77
78
79 /**
80 * Type five, key request/response.
81 * Request a node's public key, for use in debugging.
82 *
83 * Any data (up to 64 bytes) following the end of the KeyPing structure
84 * is the cookie which must be reflected.
85 */
86 #define Control_KEYPING_be Endian_hostToBigEndian16(5)
87 #define Control_KeyPing_HEADER_SIZE 40
88 #define Control_KeyPing_MAX_SIZE (Control_KeyPing_HEADER_SIZE + 64)
89 #define Control_KeyPing_MAGIC Endian_hostToBigEndian32(0x01234567)
90 struct Control_KeyPing
91 {
92 /** Magic: equal to Control_KeyPing_MAGIC in a ping and Control_KeyPong_MAGIC in a pong. */
93 uint32_t magic;
94
95 /** The version of the sending node. */
96 uint32_t version_be;
97
98 /** The permanent public key. */
99 uint8_t key[32];
100 };
101 Assert_compileTime(sizeof(struct Control_KeyPing) == Control_KeyPing_HEADER_SIZE);
102
103 #define Control_KEYPONG_be Endian_hostToBigEndian16(6)
104 #define Control_KeyPong_HEADER_SIZE Control_KeyPing_HEADER_SIZE
105 #define Control_KeyPong_MAX_SIZE Control_KeyPing_MAX_SIZE
106 #define Control_KeyPong_MAGIC Endian_hostToBigEndian32(0x89abcdef)
107
108
109 #define Control_GETSNODE_QUERY_be Endian_hostToBigEndian16(7)
110 #define Control_GETSNODE_QUERY_MAGIC Endian_hostToBigEndian32(0x736e6f71) // snoq
111 #define Control_GETSNODE_REPLY_be Endian_hostToBigEndian16(8)
112 #define Control_GETSNODE_REPLY_MAGIC Endian_hostToBigEndian32(0x736e6f72) // snor
113 #define Control_GetSnode_HEADER_SIZE 56
114 struct Control_GetSnode
115 {
116 // Control_SNODE_QUERY_MAGIC for queries
117 // Control_SNODE_REPLY_MAGIC for replies
118 uint32_t magic;
119
120 // version of the node sending the packet
121 uint32_t version_be;
122
123 // version of the supernode belonging to the node sending the packet
124 // 0 if unknown
125 uint32_t snodeVersion_be;
126
127 // If the highest bit is set then this number is per configuration and no more than this
128 // should be sent by the peer, otherwise it is an estimate of the link speed.
129 // Nodes should send no more than min(minimumConfigured, avg(nodeAVal, nodeBVal)
130 // so if A configures 1000Mb, B estimates 100Mb -> use avg(100, 1000)
131 // but if A configures or estimates 1000Mb, B configures 100Mb -> use 100Mb
132 //
133 // Nodes MUST check if this message comes from a direct peer before using this value.
134 // 0 if unknown
135 uint32_t kbps_be;
136
137 // Key of the supernode belonging to the node sending, zero if no supernode is known or
138 // configured.
139 uint8_t snodeKey[32];
140
141 // Path from sender to sender's supernode, "corrected" so that recipient can splice to it
142 // without knowing sender's encoding scheme. 0 if unknown.
143 uint8_t pathToSnode_be[8];
144 };
145 Assert_compileTime(sizeof(struct Control_GetSnode) == Control_GetSnode_HEADER_SIZE);
146
147 #define Control_RPATH_QUERY_be Endian_hostToBigEndian16(9)
148 #define Control_RPATH_QUERY_MAGIC Endian_hostToBigEndian32(0x736e6f71) // rpaq
149 #define Control_RPATH_REPLY_be Endian_hostToBigEndian16(10)
150 #define Control_RPATH_REPLY_MAGIC Endian_hostToBigEndian32(0x736e6f72) // rpar
151 #define Control_RPath_HEADER_SIZE 16
152 struct Control_RPath
153 {
154 // Control_RPATH_QUERY_MAGIC for queries
155 // Control_RPATH_REPLY_MAGIC for replies
156 uint32_t magic;
157
158 // Version of the node sending the query or the reply
159 uint32_t version_be;
160
161 // The reverse path back to the node who sent the query
162 uint8_t rpath_be[8];
163 };
164
Control_typeString(uint16_t type_be)165 static inline char* Control_typeString(uint16_t type_be)
166 {
167 if (type_be == Control_ERROR_be) {
168 return "ERROR";
169 } else if (type_be == Control_PING_be) {
170 return "PING";
171 } else if (type_be == Control_PONG_be) {
172 return "PONG";
173 } else if (type_be == Control_KEYPING_be) {
174 return "KEYPING";
175 } else if (type_be == Control_KEYPONG_be) {
176 return "KEYPONG";
177 } else if (type_be == Control_GETSNODE_QUERY_be) {
178 return "GETSNODE_QUERY";
179 } else if (type_be == Control_GETSNODE_REPLY_be) {
180 return "GETSNODE_REPLY";
181 } else if (type_be == Control_RPATH_QUERY_be) {
182 return "RPATH_QUERY";
183 } else if (type_be == Control_RPATH_REPLY_be) {
184 return "RPATH_REPLY";
185 } else {
186 return "UNKNOWN";
187 }
188 }
189
190 struct Control_Header
191 {
192 /**
193 * This should be the one's complement checksum
194 * of the control packet with 0'd checksum field.
195 */
196 uint16_t checksum_be;
197
198 /** The type of control message, eg: Control_ERROR. */
199 uint16_t type_be;
200 };
201 #define Control_Header_SIZE 4
202 Assert_compileTime(sizeof(struct Control_Header) == Control_Header_SIZE);
203
204 /**
205 * A return message which is treated specially by switches.
206 *
207 * 1 2 3
208 * 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
209 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
210 * 0 | Checksum | Type |
211 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212 * 4 | |
213 * + First <Length> Bytes of Packet Which Caused The Error +
214 * 8 | |
215 *
216 */
217 struct Control
218 {
219 struct Control_Header header;
220
221 union {
222 struct Control_Error error;
223 struct Control_Ping ping;
224 struct Control_Ping pong;
225 struct Control_KeyPing keyPing;
226 struct Control_Ping keyPong;
227 struct Control_GetSnode getSnode;
228 struct Control_RPath rpath;
229
230 /** The control packet content. */
231 uint8_t bytes[4];
232 } content;
233 };
234 // Control_KeyPing is the largest structure and thus defines the length of the "content" union.
235 Assert_compileTime(
236 sizeof(struct Control) == (Control_Header_SIZE + Control_GetSnode_HEADER_SIZE)
237 );
238
239 #endif
240