diff options
| author | Autumn <git@autumnfo.rest> | 2026-05-17 01:05:50 +0100 |
|---|---|---|
| committer | Autumn <git@autumnfo.rest> | 2026-05-17 01:05:50 +0100 |
| commit | cdf851275189c9cbe053c1717955bfe73497b6de (patch) | |
| tree | a0168fa5cc479f1d19f4ab727086147a7057be17 /fetch.lua | |
| parent | a1a0d46c8865f4e3fc28cc8aab41dc7b5f4a31d4 (diff) | |
[fetch] added pretty output for service look-ups
Diffstat (limited to 'fetch.lua')
| -rw-r--r-- | fetch.lua | 101 |
1 files changed, 98 insertions, 3 deletions
@@ -27,16 +27,111 @@ end client:connect(server.host, server.port) -- send request -local request = string.fromhex(PawSD.Magic .. PawSD.Version .. PawSD.Verbs.FETCH_SERVICE .. server.keyalg .. "0020" .. server.key .. index) +local request = string.fromhex(PawSD.Magic.REQUEST .. PawSD.Version .. PawSD.Verbs.FETCH_SERVICE .. server.keyalg .. "0020" .. server.key .. index) client:send(request) -- print response +local response = {} + while true do - local response, status = client:receive(1) + local packet, status = client:receive(1) - if response then print(response) end + -- if packet then print(response) end + table.insert(response, packet) if status == "closed" then break end end +-- check that the response is correct +local responseMagic = extractfromresponse(response, 1, 8) +if not responseMagic == PawSD.Magic.RESPONSE then + error("invalid response") +end + +-- check that the status is okay +local responseStatus = extractfromresponse(response, 11, 12) +if not responseStatus == PawSD.Responses.OK then + error("unsuccessful request") +end + +-- get signature information +local responseSignatureLength = tonumber(extractfromresponse(response, 13, 14), 16) +local responseSignatureEnd = 14 + responseSignatureLength +local responseSignature = extractfromresponse(response, 15, responseSignatureEnd) + +print("Signature: " .. responseSignature) +-- todo: check this? + +-- get service index +local responseIndexStart = responseSignatureEnd + 1 +local responseIndex = extractfromresponse(response, responseIndexStart, responseIndexStart + 1) + +-- get service flags +local responseFlagsStart = responseIndexStart + 2 +local responseFlags = extractfromresponse(response, responseFlagsStart, responseFlagsStart + 3) + +-- get number of records +local responseRecordCountStart = responseFlagsStart + 4 +local responseRecordCount = tonumber(extractfromresponse(response, responseRecordCountStart, responseRecordCountStart + 1), 16) + +-- store offset +local recordOffset = responseRecordCountStart + 2 + +-- loop through each record +for recordNumber = 1, responseRecordCount, 1 do + + print("\n--- Record Number " .. recordNumber .. " ---") + + -- get number of tags + local responseTagCountStart = recordOffset + local responseTagCount = tonumber(extractfromresponse(response, responseTagCountStart, responseTagCountStart + 1), 16) + + -- store offset + local tagOffset = recordOffset + 2 + + -- loop through each tag + for tagNumber = 1, responseTagCount, 1 do + + print("\n - Tag Number " .. tagNumber .. " -\n") + + -- high tag + local responseTagHighStart = tagOffset + local responseTagHigh = extractfromresponse(response, responseTagHighStart, responseTagHighStart + 7) + print(" High Tag: " .. responseTagHigh) + + -- low tag + local responseTagLowStart = responseTagHighStart + 8 + local responseTagLow = extractfromresponse(response, responseTagLowStart, responseTagLowStart + 7) + print(" Low Tag: " .. responseTagLow) + + -- get value length + local responseTagValueLength = {} + local responseTagValueLengthStart = responseTagLowStart + 8 + local responseTagValueLength = tonumber(extractfromresponse(response, responseTagValueLengthStart, responseTagValueLengthStart + 3), 16) + + -- only check if value actually exists + if responseTagValueLength > 0 then + + -- get value + local responseTagValueStart = responseTagValueLengthStart + 4 + local responseTagValueEnd = responseTagValueStart + responseTagValueLength + local responseTagValue = string.fromhex(extractfromresponse(response, responseTagValueStart, responseTagValueEnd)) + + print(" Value: " .. responseTagValue) + + -- adjust offset + tagOffset = responseTagValueEnd + + else + + -- adjust offset + tagOffset = responseTagValueLengthStart + 4 + + end + end + + -- adjust offset + recordOffset = tagOffset +end + -- close connection client:close() |
