Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-roon_discovery.c
blob8ae2158ee7f6a1c40357d6b1655a14eb9c2e62be
1 /* packet-roon_discovery.c
2 * Routines for Roon Discovery dissection
3 * Copyright 2022, Aaron Turner <synfinatic@gmail.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
13 * Roon Discovery is used by devices running "Roon" from roonlabs.com
14 * to discover streaming endpoints and the "Roon Core". Reverse engineered
15 * as no public documentation exists.
18 #include <config.h>
20 #include <stdlib.h> /* For bsearch() */
22 #include <epan/packet.h> /* Should be first Wireshark include (other than config.h) */
23 #include <wsutil/array.h>
25 /* Prototypes */
26 /* (Required to prevent [-Wmissing-prototypes] warnings */
27 void proto_reg_handoff_roon_discover(void);
28 void proto_register_roon_discover(void);
30 static dissector_handle_t roon_discover_handle;
32 /* Initialize the protocol and registered fields */
33 static int proto_roon_discover;
34 static int hf_roon_disco_config_version;
35 static int hf_roon_disco_device_type;
36 static int hf_roon_disco_device_class;
37 static int hf_roon_disco_display_version;
38 static int hf_roon_disco_http_port;
39 static int hf_roon_disco_https_port;
40 static int hf_roon_disco_is_dev;
41 static int hf_roon_disco_machine_id;
42 static int hf_roon_disco_machine_name;
43 static int hf_roon_disco_marker;
44 static int hf_roon_disco_name;
45 static int hf_roon_disco_os_version;
46 static int hf_roon_disco_protocol_version;
47 static int hf_roon_disco_protocol_hash;
48 static int hf_roon_disco_query_service_id;
49 static int hf_roon_disco_raat_version;
50 static int hf_roon_disco_service_id;
51 static int hf_roon_disco_tcp_port;
52 static int hf_roon_disco_tid;
53 static int hf_roon_disco_type;
54 static int hf_roon_disco_unique_id;
55 static int hf_roon_disco_user_id;
58 #define ROON_DISCOVERY_ID "SOOD"
59 #define ROON_QUERY 0x0251 // Q(uery)
60 #define ROON_REPLY 0x0252 // R(eply)
61 #define ROON_DISCOVERY_UDP_PORT 9003 /* Not IANA-assigned */
63 /* Initialize the subtree pointers */
64 static int ett_roon_discover;
66 #define ROON_DISCOVERY_MIN_LENGTH 98 // empirically defined
68 typedef struct {
69 char *key;
70 char *name;
71 int *value;
72 } roon_map;
74 // table to map field keys to our protocol tree entry. The order of entries
75 // must be sorted by they key field.
76 static const roon_map roon_disco_string_fields[] = {
77 { "_tid" , "TransactionID" , &hf_roon_disco_tid } ,
78 { "config_version" , "Config Version" , &hf_roon_disco_config_version } ,
79 { "device_class" , "Device Class" , &hf_roon_disco_device_class } ,
80 { "device_type" , "Device Type" , &hf_roon_disco_device_type } ,
81 { "display_version" , "Display Version" , &hf_roon_disco_display_version } ,
82 { "http_port" , "HTTP Port" , &hf_roon_disco_http_port } ,
83 { "https_port" , "HTTPS Port" , &hf_roon_disco_https_port } ,
84 { "machine_id" , "MachineID" , &hf_roon_disco_machine_id } ,
85 { "machine_name" , "Machine Name" , &hf_roon_disco_machine_name } ,
86 { "marker" , "Discovery Marker" , &hf_roon_disco_marker } ,
87 { "name" , "Host Name" , &hf_roon_disco_name } ,
88 { "os_version" , "OS Version" , &hf_roon_disco_os_version } ,
89 { "protocol_hash" , "Protocol Hash" , &hf_roon_disco_protocol_hash } ,
90 { "protocol_version" , "Protocol Version" , &hf_roon_disco_protocol_version } ,
91 { "query_service_id" , "Query ServiceID" , &hf_roon_disco_query_service_id } ,
92 { "raat_version" , "RAAT Version" , &hf_roon_disco_raat_version } ,
93 { "service_id" , "ServiceID" , &hf_roon_disco_service_id } ,
94 { "tcp_port" , "TCP Port" , &hf_roon_disco_tcp_port } ,
95 { "type" , "Message Type" , &hf_roon_disco_type } ,
96 { "unique_id" , "UniqueID" , &hf_roon_disco_unique_id } ,
97 { "user_id" , "UserID" , &hf_roon_disco_user_id } ,
98 { NULL , NULL , NULL } ,
101 static const roon_map roon_disco_bool_fields[] = {
102 { "is_dev" , "Devel Version" , &hf_roon_disco_is_dev } ,
103 { NULL , NULL , NULL } ,
106 static int
107 compare_keys(const void *va, const void *vb) {
108 const roon_map *a = va, *b = vb;
109 return strcmp(a->key, b->key);
112 static size_t
113 roon_map_length(const roon_map rm[]) {
114 size_t len = 0;
115 while (rm[len].key != NULL) {
116 len++;
118 return len;
121 // returns the value of key from the roon_map or NULL
122 static int *
123 roon_map_value(char *key, const roon_map rm[]) {
124 size_t len = roon_map_length(rm);
125 roon_map map[1] = {{ key, NULL, NULL }};
126 roon_map *pair = bsearch(map, rm, len, sizeof(roon_map), compare_keys);
127 return pair ? pair->value : NULL;
130 // returns the name of key from the roon_map or NULL
131 static char *
132 roon_map_name(char *key, const roon_map rm[]) {
133 size_t len = roon_map_length(rm);
134 roon_map map[1] = {{ key, NULL, NULL }};
135 roon_map *pair = bsearch(map, rm, len, sizeof(roon_map), compare_keys);
136 return pair ? pair->name : NULL;
139 /* Code to actually dissect the packets
141 * The protocol is basically a static prefix of "SOOD" followed by a two byte
142 * type indicating a query or reply. The rest of the fields are an odd TLV-like
143 * format where the type is an ASCII encoded string with a length prefix, followed
144 * by a NULL byte terminator and then an ASCII encoded value also with a length
145 * prefix, but no NULL terminator.
147 static int
148 dissect_roon_discover(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
149 void *data _U_)
151 bool is_reply = false;
152 proto_item *ti;
153 proto_tree *roon_discover_tree;
155 // verify this is actually a Roon Discovery packet we can process at a basic level
156 if ((tvb_reported_length(tvb) < ROON_DISCOVERY_MIN_LENGTH || (tvb_captured_length(tvb) < 6)))
157 return 0;
159 // Must start with SOOD
160 char *marker = tvb_get_string_enc(pinfo->pool, tvb, 0, 4, ENC_ASCII);
161 if (strcmp(ROON_DISCOVERY_ID, marker) != 0)
162 return 0;
164 // query or reply are the next two bytes.
165 switch (tvb_get_int16(tvb, 4, ENC_BIG_ENDIAN)) {
166 case ROON_REPLY:
167 is_reply = true;
168 break;
169 case ROON_QUERY:
170 break;
171 default:
172 // dunno what we are
173 return 0;
176 /* Set the Protocol column to the constant string of roon_discover */
177 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RoonDisco");
178 col_clear(pinfo->cinfo, COL_INFO);
180 /* create display subtree for the protocol */
181 ti = proto_tree_add_item(tree, proto_roon_discover, tvb, 0, -1, ENC_NA);
182 roon_discover_tree = proto_item_add_subtree(ti, ett_roon_discover);
183 proto_tree_add_string(roon_discover_tree, hf_roon_disco_marker, tvb, 0, 4, ROON_DISCOVERY_ID);
185 if (is_reply) {
186 col_set_str(pinfo->cinfo, COL_INFO, "Roon Discovery Reply");
187 proto_tree_add_string(roon_discover_tree, hf_roon_disco_type, tvb, 4, 2, "Reply");
188 } else {
189 col_set_str(pinfo->cinfo, COL_INFO, "Roon Discovery Query");
190 proto_tree_add_string(roon_discover_tree, hf_roon_disco_type, tvb, 4, 2, "Query");
193 int next;
194 // iterate over the rest of our message bytes
195 for (unsigned i = 6; i < tvb_reported_length(tvb) ; i += next) {
196 uint8_t key_len, value_len;
197 unsigned offset;
198 char *key, *value;
200 key_len = tvb_get_uint8(tvb, i);
201 offset = i + 1;
202 key = tvb_get_string_enc(pinfo->pool, tvb, offset, key_len, ENC_ASCII);
204 offset += key_len + 1;
205 value_len = tvb_get_uint8(tvb, offset);
206 offset += 1;
207 value = tvb_get_string_enc(pinfo->pool, tvb, offset, value_len, ENC_ASCII);
209 next = key_len + value_len + 3;
211 // Is our value a string?
212 char *treeName = roon_map_name(key, roon_disco_string_fields);
213 int *treeValue;
214 if (treeName != NULL) {
215 treeValue = roon_map_value(key, roon_disco_string_fields);
216 proto_tree_add_string(roon_discover_tree, *treeValue, tvb, i, next, value);
217 continue;
220 // Is our value a boolean?
221 treeName = roon_map_name(key, roon_disco_bool_fields);
222 if (treeName != NULL) {
223 treeValue = roon_map_value(key, roon_disco_bool_fields);
224 int val = strcmp(value, "0") == 0 ? 0 : 1;
225 proto_tree_add_boolean(roon_discover_tree, *treeValue, tvb, i, next, val);
226 continue;
229 // If we reach here, unsupported field
230 // fprintf(stderr, "no match for %s\n", key);
233 return tvb_captured_length(tvb);
236 /* Register the protocol with Wireshark. */
237 void
238 proto_register_roon_discover(void)
240 static hf_register_info hf[] = {
241 { &hf_roon_disco_config_version,
242 { "Config Version", "roon_disco.config_version",
243 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
245 { &hf_roon_disco_display_version,
246 { "Display Version", "roon_disco.display_version",
247 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
249 { &hf_roon_disco_device_type,
250 { "Device Type", "roon_disco.device_type",
251 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
253 { &hf_roon_disco_device_class,
254 { "Device Class", "roon_disco.device_class",
255 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
257 { &hf_roon_disco_http_port,
258 { "HTTP Port", "roon_disco.http_port",
259 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
261 { &hf_roon_disco_https_port,
262 { "HTTPS Port", "roon_disco.https_port",
263 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
265 { &hf_roon_disco_is_dev,
266 { "Development Version", "roon_disco.is_dev",
267 FT_BOOLEAN, BASE_NONE, NULL, 0, NULL, HFILL } },
269 { &hf_roon_disco_machine_id,
270 { "MachineID", "roon_disco.machine_id",
271 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
273 { &hf_roon_disco_machine_name,
274 { "Machine Name", "roon_disco.machine_name",
275 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
277 { &hf_roon_disco_marker,
278 { "Protocol Marker", "roon_disco.marker",
279 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
281 { &hf_roon_disco_name,
282 { "Device Name", "roon_disco.name",
283 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
285 { &hf_roon_disco_os_version,
286 { "OS Version", "roon_disco.os_version",
287 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
289 { &hf_roon_disco_protocol_hash,
290 { "Protocol Hash", "roon_disco.protocol_hash",
291 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
293 { &hf_roon_disco_protocol_version,
294 { "Protocol Version", "roon_disco.protocol_version",
295 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
297 { &hf_roon_disco_query_service_id,
298 { "Query ServiceID", "roon_disco.query_service_id",
299 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
301 { &hf_roon_disco_raat_version,
302 { "RAAT Version", "roon_disco.raat_version",
303 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
305 { &hf_roon_disco_service_id,
306 { "ServiceId", "roon_disco.service_id",
307 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
309 { &hf_roon_disco_tcp_port,
310 { "TCP PORT", "roon_disco.tcp_port",
311 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
313 { &hf_roon_disco_tid,
314 { "TID", "roon_disco.tid",
315 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
317 { &hf_roon_disco_type,
318 { "Message Type", "roon_disco.type",
319 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
321 { &hf_roon_disco_user_id,
322 { "UserID", "roon_disco.user_id",
323 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
325 { &hf_roon_disco_unique_id,
326 { "UniqueID", "roon_disco.unique_id",
327 FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL } },
330 /* Setup protocol subtree array */
331 static int *ett[] = {
332 &ett_roon_discover
335 /* Register the protocol name and description */
336 proto_roon_discover = proto_register_protocol("Roon Discovery", "RoonDisco", "roon_disco");
338 /* Required function calls to register the header fields and subtrees */
339 proto_register_field_array(proto_roon_discover, hf, array_length(hf));
340 proto_register_subtree_array(ett, array_length(ett));
342 roon_discover_handle = register_dissector("roon_disco", dissect_roon_discover, proto_roon_discover);
345 void
346 proto_reg_handoff_roon_discover(void)
348 dissector_add_uint_with_preference("udp.port", ROON_DISCOVERY_UDP_PORT, roon_discover_handle);
352 * Editor modelines - https://www.wireshark.org/tools/modelines.html
354 * Local variables:
355 * c-basic-offset: 4
356 * tab-width: 8
357 * indent-tabs-mode: nil
358 * End:
360 * vi: set shiftwidth=4 tabstop=8 expandtab:
361 * :indentSize=4:tabSize=8:noTabs=true: