diff options
Diffstat (limited to 'testing/hstox/binary_decode.c')
-rw-r--r-- | testing/hstox/binary_decode.c | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/testing/hstox/binary_decode.c b/testing/hstox/binary_decode.c new file mode 100644 index 00000000..aa4d7935 --- /dev/null +++ b/testing/hstox/binary_decode.c | |||
@@ -0,0 +1,224 @@ | |||
1 | #include "methods.h" | ||
2 | |||
3 | #include "byteswap.h" | ||
4 | #include "packet_kinds.h" | ||
5 | |||
6 | #include <DHT.h> | ||
7 | |||
8 | METHOD(bin, Binary_decode, CipherText) | ||
9 | { | ||
10 | uint64_t length; | ||
11 | uint64_t tmp; | ||
12 | |||
13 | SUCCESS { | ||
14 | memcpy(&tmp, args.ptr, sizeof(uint64_t)); | ||
15 | length = be64toh(tmp); | ||
16 | |||
17 | if (args.size >= sizeof(uint64_t) && args.size == length + sizeof(uint64_t)) | ||
18 | { | ||
19 | msgpack_pack_bin(res, args.size - sizeof(uint64_t)); | ||
20 | msgpack_pack_bin_body(res, args.ptr + sizeof(uint64_t), args.size - sizeof(uint64_t)); | ||
21 | } else { | ||
22 | msgpack_pack_nil(res); | ||
23 | } | ||
24 | } | ||
25 | return 0; | ||
26 | } | ||
27 | |||
28 | METHOD(bin, Binary_decode, DhtPacket) | ||
29 | { | ||
30 | return pending; | ||
31 | } | ||
32 | |||
33 | METHOD(bin, Binary_decode, HostAddress) | ||
34 | { | ||
35 | return pending; | ||
36 | } | ||
37 | |||
38 | METHOD(bin, Binary_decode, Word64) | ||
39 | { | ||
40 | return pending; | ||
41 | } | ||
42 | |||
43 | METHOD(bin, Binary_decode, Key_PublicKey) | ||
44 | { | ||
45 | return pending; | ||
46 | } | ||
47 | |||
48 | METHOD(bin, Binary_decode, KeyPair) | ||
49 | { | ||
50 | SUCCESS { | ||
51 | if (args.size != 64) | ||
52 | { | ||
53 | msgpack_pack_nil(res); | ||
54 | } else { | ||
55 | msgpack_pack_array(res, 2); | ||
56 | msgpack_pack_bin(res, 32); | ||
57 | msgpack_pack_bin_body(res, args.ptr, 32); | ||
58 | msgpack_pack_bin(res, 32); | ||
59 | msgpack_pack_bin_body(res, args.ptr + 32, 32); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | return 0; | ||
64 | } | ||
65 | |||
66 | METHOD(bin, Binary_decode, NodeInfo) | ||
67 | { | ||
68 | uint16_t data_processed; | ||
69 | Node_format node; | ||
70 | int len = unpack_nodes(&node, 1, &data_processed, (uint8_t const *)args.ptr, args.size, 1); | ||
71 | |||
72 | bool ip6_node = node.ip_port.ip.family == AF_INET6 || node.ip_port.ip.family == TCP_INET6; | ||
73 | bool tcp = node.ip_port.ip.family == TCP_INET || node.ip_port.ip.family == TCP_INET6; | ||
74 | |||
75 | uint16_t port = ntohs(node.ip_port.port); | ||
76 | uint32_t ip4 = ntohl(node.ip_port.ip.ip4.uint32); | ||
77 | uint32_t ip6_0 = ntohl(node.ip_port.ip.ip6.uint32[0]); | ||
78 | uint32_t ip6_1 = ntohl(node.ip_port.ip.ip6.uint32[1]); | ||
79 | uint32_t ip6_2 = ntohl(node.ip_port.ip.ip6.uint32[2]); | ||
80 | uint32_t ip6_3 = ntohl(node.ip_port.ip.ip6.uint32[3]); | ||
81 | |||
82 | SUCCESS { | ||
83 | if (len > 0 && data_processed > 0 && data_processed == args.size) | ||
84 | { | ||
85 | msgpack_pack_array(res, 3); | ||
86 | msgpack_pack_uint8(res, tcp); | ||
87 | msgpack_pack_array(res, 2); | ||
88 | msgpack_pack_array(res, 2); | ||
89 | msgpack_pack_uint8(res, ip6_node); | ||
90 | |||
91 | if (ip6_node) { | ||
92 | msgpack_pack_array(res, 4); | ||
93 | msgpack_pack_uint32(res, ip6_0); | ||
94 | msgpack_pack_uint32(res, ip6_1); | ||
95 | msgpack_pack_uint32(res, ip6_2); | ||
96 | msgpack_pack_uint32(res, ip6_3); | ||
97 | } else { | ||
98 | msgpack_pack_uint32(res, ip4); | ||
99 | } | ||
100 | |||
101 | msgpack_pack_uint16(res, port); | ||
102 | msgpack_pack_bin(res, crypto_box_PUBLICKEYBYTES); | ||
103 | msgpack_pack_bin_body(res, &node.public_key, crypto_box_PUBLICKEYBYTES); | ||
104 | } else { | ||
105 | msgpack_pack_nil(res); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | return 0; | ||
110 | } | ||
111 | |||
112 | METHOD(bin, Binary_decode, NodesRequest) | ||
113 | { | ||
114 | return pending; | ||
115 | } | ||
116 | |||
117 | METHOD(bin, Binary_decode, NodesResponse) | ||
118 | { | ||
119 | return pending; | ||
120 | } | ||
121 | |||
122 | METHOD(bin, Binary_decode, Packet_Word64) | ||
123 | { | ||
124 | return pending; | ||
125 | } | ||
126 | |||
127 | METHOD(bin, Binary_decode, PacketKind) | ||
128 | { | ||
129 | SUCCESS { | ||
130 | if (args.size != 1) | ||
131 | { | ||
132 | msgpack_pack_nil(res); | ||
133 | } else { | ||
134 | uint8_t kind = args.ptr[0]; | ||
135 | size_t i; | ||
136 | |||
137 | for (i = 0; i < sizeof packet_kinds / sizeof *packet_kinds; i++) | ||
138 | { | ||
139 | if (packet_kinds[i] == kind) { | ||
140 | msgpack_pack_fix_uint8(res, i); | ||
141 | return 0; | ||
142 | } | ||
143 | } | ||
144 | |||
145 | // Packet kind not found => error. | ||
146 | msgpack_pack_nil(res); | ||
147 | } | ||
148 | } | ||
149 | return 0; | ||
150 | } | ||
151 | |||
152 | METHOD(bin, Binary_decode, PingPacket) | ||
153 | { | ||
154 | return pending; | ||
155 | } | ||
156 | |||
157 | METHOD(bin, Binary_decode, PlainText) | ||
158 | { | ||
159 | return pending; | ||
160 | } | ||
161 | |||
162 | METHOD(bin, Binary_decode, PortNumber) | ||
163 | { | ||
164 | SUCCESS { | ||
165 | if (args.size == 2) | ||
166 | { | ||
167 | uint16_t tmp; | ||
168 | memcpy(&tmp, args.ptr, 2); | ||
169 | uint16_t port = ntohs(tmp); | ||
170 | msgpack_pack_uint16(res, port); | ||
171 | } else { | ||
172 | msgpack_pack_nil(res); | ||
173 | } | ||
174 | } | ||
175 | |||
176 | return 0; | ||
177 | } | ||
178 | |||
179 | METHOD(bin, Binary_decode, RpcPacket_Word64) | ||
180 | { | ||
181 | return pending; | ||
182 | } | ||
183 | |||
184 | METHOD(bin, Binary_decode, SocketAddress) | ||
185 | { | ||
186 | return pending; | ||
187 | } | ||
188 | |||
189 | METHOD(bin, Binary_decode, TransportProtocol) | ||
190 | { | ||
191 | return pending; | ||
192 | } | ||
193 | |||
194 | METHOD(array, Binary, decode) | ||
195 | { | ||
196 | CHECK_SIZE(args, 2); | ||
197 | CHECK_TYPE(args.ptr[0], MSGPACK_OBJECT_STR); | ||
198 | CHECK_TYPE(args.ptr[1], MSGPACK_OBJECT_BIN); | ||
199 | |||
200 | msgpack_object_str type = args.ptr[0].via.str; | ||
201 | #define DISPATCH(TYPE) \ | ||
202 | if (type.size == sizeof #TYPE - 1 && method_cmp(type.ptr, #TYPE, type.size) == 0) \ | ||
203 | return Binary_decode_##TYPE(args.ptr[1].via.bin, res) | ||
204 | DISPATCH(CipherText); | ||
205 | DISPATCH(DhtPacket); | ||
206 | DISPATCH(HostAddress); | ||
207 | DISPATCH(Word64); | ||
208 | DISPATCH(Key_PublicKey); | ||
209 | DISPATCH(KeyPair); | ||
210 | DISPATCH(NodeInfo); | ||
211 | DISPATCH(NodesRequest); | ||
212 | DISPATCH(NodesResponse); | ||
213 | DISPATCH(Packet_Word64); | ||
214 | DISPATCH(PacketKind); | ||
215 | DISPATCH(PingPacket); | ||
216 | DISPATCH(PlainText); | ||
217 | DISPATCH(PortNumber); | ||
218 | DISPATCH(RpcPacket_Word64); | ||
219 | DISPATCH(SocketAddress); | ||
220 | DISPATCH(TransportProtocol); | ||
221 | #undef DISPATCH | ||
222 | |||
223 | return unimplemented; | ||
224 | } | ||