Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cemu udp proto
--基于UDP协议
local udp_table = DissectorTable.get("udp.port")
local main_proto = Proto("udp-sample", "udp sample protocol", "udp sample protocol")
--协议端口号
local my_port = 26760
--定义协议字段内容
local stringHeadField = ProtoField.string("Head", "Head")
local versionField = ProtoField.uint16("Version", "Version", base.DEC)
local packagelengthField = ProtoField.uint16("Length", "Package Length", base.DEC)
local CRCField = ProtoField.uint32("CRC", "CRC", base.HEX)
local ServerIdField = ProtoField.uint32("Id", "Id", base.HEX)
local TypeField = ProtoField.uint32("Type", "Type", base.HEX)
local PadIdField = ProtoField.uint8("PadId", "PadId", base.HEX)
local StateField = ProtoField.uint8("State", "State", base.HEX)
local ModelField = ProtoField.uint8("Model", "Model", base.HEX)
local ConnectTypeField = ProtoField.uint8("Connection-type", "Connection-type", base.HEX)
local MacField = ProtoField.bytes("MAC", "MAC")
local BatteryField = ProtoField.uint8("Battery", "Battery", base.HEX)
local ActiveField = ProtoField.uint8("Active", "Active", base.HEX)
local PackageNumberField = ProtoField.uint32("Package-Number", "Package-Number", base.DEC_HEX)
local PackageTimeField = ProtoField.uint32("Time", "Time", base.DEC_HEX)
local SensorGyroX = ProtoField.float("Gyro-X")
local SensorGyroY = ProtoField.float("Gyro-Y")
local SensorGyroZ = ProtoField.float("Gyro-Z")
local SensorTemp = ProtoField.float("Temperature")
local SensorAccuX = ProtoField.float("Accu-X")
local SensorAccuY = ProtoField.float("Accu-Y")
local SensorAccuZ = ProtoField.float("Accu-Z")
local unusedUint32Field = ProtoField.uint32("Unused-Number", "Unused-Number", base.DEC_HEX)
local extField = ProtoField.string("Raw", "Raw")
-- table
main_proto.fields = {
stringHeadField,
packagelengthField,
versionField,
CRCField,
ServerIdField,
TypeField,
PadIdField,
StateField,
ModelField,
ConnectTypeField,
MacField,
BatteryField,
ActiveField,
PackageNumberField,
PackageTimeField,
SensorGyroX,
SensorGyroY,
SensorGyroZ,
SensorTemp,
SensorAccuX,
SensorAccuY,
SensorAccuZ,
unusedUint32Field,
extField}
-- type
DSUC_VersionReq = 0x100000;
DSUS_PortInfo = 0x100001;
DSUC_PadDataReq = 0x100002;
--协议分析器
function main_proto.dissector(buffer, pinfo, tree)
pinfo.cols.protocol:set("udp-sample")
local len = buffer:len()
local offset = main_dissector(buffer, pinfo, tree)
-- print (string.format("%x -> %x", DSUS_PortInfo_LE, buffer(offset - 4, 4):le_uint()))
if(buffer(0, 4):string() == "DSUS") then
if buffer(offset - 4, 4):le_uint() == DSUS_PortInfo then
DSUS_PortInfo_dissector(buffer(offset, len - offset), pinfo, tree)
elseif buffer(offset - 4, 4):le_uint() == DSUC_PadDataReq then
offset = offset + DSUS_PortInfo_dissector(buffer(offset, len - offset), pinfo, tree)
DSUS_PortData_dissector(buffer(offset, len - offset), pinfo, tree)
else
default_dissector(buffer, pinfo, tree)
end
else
default_dissector(buffer, pinfo, tree)
end
end
function main_dissector(buffer, pinfo, tree)
local subtreeitem = tree:add(main_proto, buffer(0, len), "Cemu Head")
local offset = 0
subtreeitem:add_le(stringHeadField, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(versionField, buffer(offset, 2))
offset = offset + 2
subtreeitem:add_le(packagelengthField, buffer(offset, 2))
offset = offset + 2
subtreeitem:add_le(CRCField, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(ServerIdField, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(TypeField, buffer(offset, 4))
offset = offset + 4
return offset
end
function default_dissector(buffer, pinfo, tree)
local subtreeitem = tree:add(main_proto, "ext Data")
subtreeitem:add(extField, buffer(offset, buffer:len()))
end
function DSUS_PortInfo_dissector(buffer, pinfo, tree)
local subtreeitem = tree:add(main_proto, "Info")
local offset = 0
subtreeitem:add_le(PadIdField, buffer(offset, 1))
offset = offset + 1
subtreeitem:add_le(StateField, buffer(offset, 1))
offset = offset + 1
subtreeitem:add_le(ModelField, buffer(offset, 1))
offset = offset + 1
subtreeitem:add_le(ConnectTypeField, buffer(offset, 1))
offset = offset + 1
subtreeitem:add_le(MacField, buffer(offset, 6))
offset = offset + 6
subtreeitem:add_le(BatteryField, buffer(offset, 1))
offset = offset + 1
subtreeitem:add_le(ActiveField, buffer(offset, 1))
offset = offset + 1
return offset
end
function DSUS_PortData_dissector(buffer, pinfo, tree)
local subtreeitem = tree:add(main_proto, "Data")
local offset = 0
subtreeitem:add_le(PackageNumberField, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(PackageTimeField, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(unusedUint32Field, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(SensorAccuX, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(SensorAccuY, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(SensorAccuZ, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(SensorGyroX, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(SensorGyroY, buffer(offset, 4))
offset = offset + 4
subtreeitem:add_le(SensorGyroZ, buffer(offset, 4))
offset = offset + 4
return offset
end
--增加协议到Wireshark中
udp_table:add(my_port, main_proto)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment