-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdplus.lua
More file actions
255 lines (215 loc) · 9.51 KB
/
dplus.lua
File metadata and controls
255 lines (215 loc) · 9.51 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
-- dplus.lua - DPlus Protocol
local proto_shortname = "dplus"
local proto_longname = "DPlus Protocol"
p_dplus = Proto ( proto_shortname, proto_longname)
local ascp_type = {
[0]= "Control Item",
[1]= "Current Control Item",
[2]= "Control Item Range",
[3]= "Data ACK",
[4]= "D-Star Voice Trunk data",
[5]= "Data Item 1",
[6]= "Query",
[7]= "Data Item 3",
}
local ctl_types = {
[0x0018]= "State transition",
}
local query_types = {
[0x0003]= "Version",
[0x0004]= "Authentication",
[0x0005]= "Linked Repeaters List",
[0x0105]= "Linked Repeaters List (Reply)",
[0x0006]= "Connected Users List",
[0x0007]= "Last Heard List",
[0x0008]= "Date",
}
local state_trans = {
[0]= "Idle",
[1]= "Active",
}
local auth_result = {
["OKRO"]= "Success (read-only)",
["OKRW"]= "Success",
["BUSY"]= "No available slot",
["FAIL"]= "Failure",
}
local client_type = {
["A"]="DVAP",
["D"]="Dongle",
["H"]="Hotspot",
}
-- Fields
local pf_dplus_ascp_type = ProtoField.uint16 ( proto_shortname .. ".ascp.type" , "ASCP Type", base.DEC, ascp_type, 0xE000)
local pf_dplus_ascp_length = ProtoField.uint16 ( proto_shortname .. ".ascp.length" , "Payload Length", base.DEC, nil, 0x1FFF)
local pf_dplus_keepalive = ProtoField.uint8 ( proto_shortname .. ".keepalive" , "Keepalive", base.DEC)
local pf_dplus_ctl_code = ProtoField.uint16 ( proto_shortname .. ".ctl" , "Control Code", base.HEX)
local pf_dplus_ctl_state = ProtoField.uint8 ( proto_shortname .. ".state" , "New state", base.HEX, state_trans)
local pf_dplus_query_type = ProtoField.uint16 ( proto_shortname .. ".query.type" , "Type", base.HEX, query_types)
local pf_dplus_query_entries = ProtoField.uint16 ( proto_shortname .. ".query.entries" , "Number of entries", base.DEC)
local pf_dplus_auth = ProtoField.string ( proto_shortname .. ".auth" , "Authentication Result", base.ASCII, auth_result)
local pf_dplus_dongle_serial = ProtoField.string ( proto_shortname .. ".dongle.sn" , "Dongle Serial Number", base.ASCII)
local pf_dplus_epoch = ProtoField.uint32 ( proto_shortname .. ".epoch" , "*NIX Epoch", base.ASCII)
p_dplus.fields = {
pf_dplus_ascp_type, pf_dplus_ascp_length, pf_dplus_keepalive, pf_dplus_ctl_code, pf_dplus_ctl_state,
pf_dplus_query_type, pf_dplus_query_entries, pf_dplus_auth, pf_dplus_dongle_serial, pf_dplus_epoch,
}
-- Ternary operator
local function fif(condition, if_true, if_false)
if condition then return if_true else return if_false end
end
-- Enumerate Repeaters/Clients
local function dplus_enumerate_peers( buffer, tree, nb_results, is_repeater)
local peer_str = fif( is_repeater == true, "Repeater", "Client")
local i = 0
while ( i < nb_results ) do
local modl = buffer(8+(20*i),1)
local mod_name = fif( modl:string() == " ", "(Unmapped)", modl:string())
local qrz = buffer(8+(20*i+1),9)
local qrz_str = qrz:string():gsub( '^%s*(.-)%s*$', '%1')
local ctype = buffer(8+(20*i+10),2)
local ctype_val = buffer(8+(20*i+10),1):string()
local ts = buffer(8+(20*i+12),4)
local entry = tree:add( p_dplus, buffer(8+(20*i),20), string.format( "%s %s on module %s", peer_str, qrz_str, mod_name))
entry:add( modl, "Mapped module: " .. mod_name)
entry:add( qrz, "Callsign: " .. qrz_str)
if ( is_repeater ~= true ) then
if ( client_type[ctype_val] ~= nil ) then
entry:add( ctype, string.format("%s Type: %s ('%s')", peer_str, client_type[ctype_val], ctype_val))
else
local unkent = entry:add( ctype, string.format( "%s Type: Unknown ('%s')", peer_str, ctype_val))
unkent:add_expert_info( PI_UNDECODED, PI_WARN, string.format( "Unknown %s type", string.lower(peer_str)))
end
else
entry:add( ctype, "Reserved")
end
entry:add_le ( ts, "Connected Since (epoch): " .. ts:le_uint())
i = i + 1
end
end
function p_dplus.dissector ( buffer, pinfo, tree)
-- Validate packet length
if ( buffer:len() < 3 ) then return end
local len = buffer:len()
local inner_len = bit.band( buffer( 0, 2):le_uint(), 0x1FFF)
local ascp_header = buffer( 0, 2)
local ascp_type = bit.rshift( bit.band( buffer( 1,1):uint(), 0xE0), 5)
-- Validate length
if ( len ~= inner_len ) then return end;
-- Set protocol name
pinfo.cols.protocol = "DPLUS"
-- Subtree
local subtree = tree:add( p_dplus, buffer(), "DPlus Protocol")
local ascp_tree = subtree:add_le ( p_dplus, ascp_header, "Amateur Station Control Protocol Header" )
ascp_tree:add_le( pf_dplus_ascp_type, ascp_header)
ascp_tree:add_le( pf_dplus_ascp_length, ascp_header)
local callsign = nil
local serial = nil
if ( ascp_type == 4 ) then
-- Pass the information to the DSVT dissector
Dissector.get("dsvt"):call( buffer(2):tvb(), pinfo, tree)
elseif ( ascp_type == 3 and len == 3 ) then
-- Keepalive
pinfo.cols.info = "Keepalive"
subtree:add( pf_dplus_keepalive, buffer(2,1))
return
elseif ( ascp_type == 0 and len >= 4 ) then
-- Commands
local command = buffer(2,2):le_uint()
subtree:add_le( pf_dplus_ctl_code, buffer( 2, 2))
if ( command == 0x0018 and len == 5 ) then
-- Repeater state transition
local state = buffer( 4,1):uint()
subtree:add( pf_dplus_ctl_state, buffer( 4, 1))
pinfo.cols.info = "New state: " .. state_trans[state]
else
local uncomm = subtree:add( buffer(2,1), "Unknown command")
uncomm:add_expert_info( PI_UNDECODED, PI_WARN, "Unknown command")
end
elseif ( ascp_type == 6 ) then
-- Queries
local qtype = buffer( 2, 2):le_uint()
subtree:add_le( pf_dplus_query_type, buffer( 2, 2))
if ( qtype == 0x0004 and len == 8 ) then
-- Authentication result
local auth_tree = subtree:add( pf_dplus_auth, buffer(4))
local result = buffer(4):string()
pinfo.cols.info = "Auth result: " .. auth_result[result]
auth_tree.text = string.format( "%s %s (%s)", auth_tree.text:sub( 1, -6), auth_result[result], result)
elseif ( qtype == 0x0004 and len == 28 ) then
-- Authentication request
callsign = buffer( 4, 8):string()
serial = buffer( 20, 8):string()
subtree:add( buffer( 4, 8), "Callsign: " .. callsign )
local serial_tree = subtree:add( buffer( 20, 8), "Dongle serial: " .. serial )
if ( buffer( 20, 8):string() == "DV019999" ) then
serial_tree:add_expert_info( PI_PROTOCOL, PI_COMMENT, "Not an actual dongle")
end
pinfo.cols.info = "Auth request from: " .. callsign
elseif ( qtype == 0x0003 and len > 5 ) then
-- Software version
version = buffer( 4):string()
subtree:add( buffer(4), "Software version: " .. version)
pinfo.cols.info = "Software version: " .. version
elseif ( qtype == 0x0008 and len == 34 ) then
-- Date reply
local time = buffer( 8):string()
subtree:add_le( pf_dplus_epoch, buffer( 4, 4))
local time_tree = subtree:add( buffer(8), "Current Date: " .. time)
if ( buffer(28,5):string() == " " ) then
time_tree:add_expert_info( PI_PROTOCOL, PI_COMMENT, "Implied time zone: UTC")
end
pinfo.cols.info = "Current Date: " .. time
elseif ( qtype == 0x0105 and math.fmod( len-8, 20) == 0 ) then
-- Connected Repeaters List
local nb_results = buffer( 6, 2):le_uint()
subtree:add_le( pf_dplus_query_entries, buffer( 6, 2))
local cntd_tree = subtree:add( p_dplus, buffer(8), "Connected Repeaters List")
dplus_enumerate_peers( buffer, cntd_tree, nb_results, true)
pinfo.cols.info = string.format( "Connected Repeaters List, %d entr%s", nb_results, fif( nb_results ~= 1, "ies", "y"))
elseif ( qtype == 0x0006 and math.fmod( len-8, 20) == 0 ) then
-- Connected Users List
local nb_results = buffer( 6, 2):le_uint()
subtree:add_le( pf_dplus_query_entries, buffer( 6, 2))
local cntd_tree = subtree:add( p_dplus, buffer(8), "Connected Users List")
dplus_enumerate_peers( buffer, cntd_tree, nb_results, false)
pinfo.cols.info = string.format( "Connected Users List, %d entr%s", nb_results, fif( nb_results ~= 1, "ies", "y"))
elseif ( qtype == 0x0007 and math.fmod( len-10, 24) == 0 ) then
-- Last Heard List
local nb_results = buffer( 4, 2):le_uint()
local ts = buffer(6,4)
subtree:add_le( pf_dplus_query_entries, buffer( 4, 2))
local cntd_tree = subtree:add( p_dplus, buffer(10), "Last Heard List")
cntd_tree:add_le ( ts, "Generated at (epoch): " .. ts:le_uint())
local i = 0
while ( i < nb_results ) do
local qrz = buffer(10+(24*i),8)
local qrz_str = qrz:string():gsub( '^%s*(.-)%s*$', '%1')
local via = buffer(18+(24*i),7)
local via_str = via:string():gsub( '^%s*(.-)%s*$', '%1')
local modl = buffer(25+(24*i),1)
local modl_name = fif( modl:string() ~= " ", modl:string(), "(Unlinked)")
local rx_ts = buffer(26+(24*i),4)
local resv = buffer(30+(24*i),4)
local entry = subtree:add( p_dplus, buffer(10+(24*i),24), string.format( "User %s via %s on module %s", qrz_str, via_str, modl_name))
entry:add( qrz, "Callsign: " .. qrz_str)
entry:add( via, "Via: " .. via_str)
entry:add( modl, "Module: " .. modl_name)
entry:add_le ( rx_ts, "Last heard on (epoch): " .. rx_ts:le_uint())
entry:add( resv, "Reserved" )
i = i + 1
end
pinfo.cols.info = string.format( "Last Heard List, %d entr%s", nb_results, fif( nb_results ~= 1, "ies", "y"))
elseif ( query_types[qtype] ~= nil and len == 4 ) then
pinfo.cols.info = query_types[qtype] .. " Request"
else
local unkquery = subtree:add( buffer( 4), "Unknown Query")
unkquery:add_expert_info( PI_UNDECODED, PI_WARN, "Undocumented data")
end
else
local undoc = subtree:add( buffer(2), "Undocumented data")
undoc:add_expert_info( PI_UNDECODED, PI_WARN, "Undocumented data")
end
end
local udp_dissector_table = DissectorTable.get("udp.port")
udp_dissector_table:add( 20001, p_dplus)