1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* packet-lsd.c
* Local Service Discovery packet dissector
*
* From http://bittorrent.org/beps/bep_0014.html
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/strutil.h>
#include <wsutil/pint.h>
#include <wsutil/strtoi.h>
void proto_register_lsd(void);
void proto_reg_handoff_lsd(void);
#define LSD_MULTICAST_ADDRESS 0xEFC0988F /* 239.192.152.143 */
#define LSD_PORT 6771
static int proto_lsd;
static int hf_lsd_header;
static int hf_lsd_host;
static int hf_lsd_port;
static int hf_lsd_infohash;
static int hf_lsd_cookie;
static int ett_lsd;
static expert_field ei_lsd_field;
static bool
parse_string_field(proto_tree *tree, int hf, packet_info *pinfo, tvbuff_t *tvb, int offset, int* next_offset, int* linelen)
{
uint8_t *str;
header_field_info* hf_info = proto_registrar_get_nth(hf);
char **field_and_value;
proto_item* ti;
char *p;
*linelen = tvb_find_line_end(tvb, offset, -1, next_offset, false);
if (*linelen < 0)
return false;
str = tvb_get_string_enc(pinfo->pool, tvb, offset, *linelen, ENC_ASCII);
if (g_ascii_strncasecmp(str, hf_info->name, strlen(hf_info->name)) == 0)
{
field_and_value = wmem_strsplit(pinfo->pool, str, ":", 2);
p = field_and_value[1];
if (p) {
while(g_ascii_isspace(*p))
p++;
proto_tree_add_string(tree, hf, tvb, offset, *linelen, p);
return true;
}
}
ti = proto_tree_add_string_format(tree, hf, tvb, offset, *linelen, str, "%s", str);
expert_add_info_format(pinfo, ti, &ei_lsd_field, "%s field malformed", hf_info->name);
return true;
}
static int
dissect_lsd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
proto_item *ti = NULL;
proto_tree *lsd_tree;
int offset = 0, next_offset = 0, linelen;
uint8_t *str;
char **field_and_value;
uint16_t port;
bool valid;
linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, false);
if (linelen < 0)
return 0;
col_set_str(pinfo->cinfo, COL_PROTOCOL, "LSD");
col_clear(pinfo->cinfo, COL_INFO);
/* Create display subtree for the protocol */
ti = proto_tree_add_item(tree, proto_lsd, tvb, 0, -1, ENC_NA);
lsd_tree = proto_item_add_subtree(ti, ett_lsd);
proto_tree_add_item(lsd_tree, hf_lsd_header, tvb, offset, linelen, ENC_ASCII);
offset = next_offset;
if (!parse_string_field(lsd_tree, hf_lsd_host, pinfo, tvb, offset, &next_offset, &linelen))
return offset+linelen;
offset = next_offset;
linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, false);
if (linelen < 0)
return offset+linelen;
str = tvb_get_string_enc(pinfo->pool, tvb, offset, linelen, ENC_ASCII);
if (g_ascii_strncasecmp(str, "Port", strlen("Port")) == 0)
{
field_and_value = wmem_strsplit(pinfo->pool, str, ":", 2);
valid = ws_strtou16(field_and_value[1], NULL, &port);
ti = proto_tree_add_uint(lsd_tree, hf_lsd_port, tvb, offset, linelen, port);
if (!valid)
{
expert_add_info_format(pinfo, ti, &ei_lsd_field, "Port value malformed");
}
}
else
{
ti = proto_tree_add_uint(lsd_tree, hf_lsd_port, tvb, offset, 0, 0xFFFF);
expert_add_info_format(pinfo, ti, &ei_lsd_field, "Port field malformed");
}
proto_item_set_len(ti, linelen);
offset = next_offset;
if (!parse_string_field(lsd_tree, hf_lsd_infohash, pinfo, tvb, offset, &next_offset, &linelen))
return offset+linelen;
offset = next_offset;
linelen = tvb_find_line_end(tvb, offset, -1, &next_offset, false);
if (linelen < 0)
return offset+linelen;
/* Cookie is optional */
if (tvb_strncaseeql(tvb, offset, "cookie", strlen("cookie")) == 0)
{
if (!parse_string_field(lsd_tree, hf_lsd_cookie, pinfo, tvb, offset, &next_offset, &linelen))
return offset+linelen;
}
return tvb_captured_length(tvb);
}
static bool
dissect_lsd_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
if (pinfo->dst.type == AT_IPv4 && pntoh32(pinfo->dst.data) == LSD_MULTICAST_ADDRESS && pinfo->destport == LSD_PORT)
return (dissect_lsd(tvb, pinfo, tree, data) != 0);
if (pinfo->dst.type == AT_IPv6 && pinfo->destport == LSD_PORT)
return (dissect_lsd(tvb, pinfo, tree, data) != 0);
return false;
}
void
proto_register_lsd(void)
{
static hf_register_info hf[] = {
{ &hf_lsd_header,
{ "Header", "lsd.header",
FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }
},
{ &hf_lsd_host,
{ "Host", "lsd.host",
FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }
},
{ &hf_lsd_port,
{ "Port", "lsd.port",
FT_UINT32, BASE_DEC, NULL, 0, NULL, HFILL }
},
{ &hf_lsd_infohash,
{ "Infohash", "lsd.infohash",
FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }
},
{ &hf_lsd_cookie,
{ "cookie", "lsd.cookie",
FT_STRING, BASE_NONE, NULL, 0, NULL, HFILL }
},
};
static int *ett[] = {
&ett_lsd,
};
static ei_register_info ei[] = {
{ &ei_lsd_field, { "lsd.malformed_field", PI_MALFORMED, PI_ERROR, "Malformed LDS field", EXPFILL }},
};
expert_module_t* expert_lsd;
proto_lsd = proto_register_protocol("Local Service Discovery", "LSD", "lsd");
proto_register_field_array(proto_lsd, hf, array_length(hf));
proto_register_subtree_array(ett, array_length(ett));
expert_lsd = expert_register_protocol(proto_lsd);
expert_register_field_array(expert_lsd, ei, array_length(ei));
}
void
proto_reg_handoff_lsd(void)
{
heur_dissector_add( "udp", dissect_lsd_heur, "LSD over UDP", "lsd_udp", proto_lsd, HEURISTIC_ENABLE);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 2
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
*/
|