aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAutumn <git@autumnfo.rest>2026-05-17 01:05:50 +0100
committerAutumn <git@autumnfo.rest>2026-05-17 01:05:50 +0100
commitcdf851275189c9cbe053c1717955bfe73497b6de (patch)
treea0168fa5cc479f1d19f4ab727086147a7057be17
parenta1a0d46c8865f4e3fc28cc8aab41dc7b5f4a31d4 (diff)
[fetch] added pretty output for service look-ups
-rw-r--r--fetch.lua101
-rw-r--r--src/pawsd.lua18
-rw-r--r--src/utils.lua12
3 files changed, 127 insertions, 4 deletions
diff --git a/fetch.lua b/fetch.lua
index 59c8e3c..43e268f 100644
--- a/fetch.lua
+++ b/fetch.lua
@@ -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()
diff --git a/src/pawsd.lua b/src/pawsd.lua
index 8efae57..4c7f82f 100644
--- a/src/pawsd.lua
+++ b/src/pawsd.lua
@@ -9,7 +9,11 @@ local verbs = {
}
-- metadata
-local magic = "5061775271757374" -- PawRqust
+local magic = {
+ REQUEST = "5061775271757374", -- PawRqust
+ RESPONSE = "5061775273706E73" -- PawRspns
+}
+
local version = "0001"
-- supported key algorithms
@@ -17,9 +21,21 @@ local keyalg = {
ED25519 = "63af"
}
+-- response codes
+local responses = {
+ OK = "0200",
+ CLIENT_ERROR = "0400",
+ NOT_FOUND = "0404",
+ SERVER_ERROR = "0500",
+ NOT_IMPLEMENTED = "0501",
+ UNAVAILABLE = "0503",
+ UNKNOWN = "0505"
+}
+
-- return
return {
Verbs = verbs,
+ Responses = responses,
Magic = magic,
Version = version,
KeyAlg = keyalg
diff --git a/src/utils.lua b/src/utils.lua
index af6e91f..5bcbc8c 100644
--- a/src/utils.lua
+++ b/src/utils.lua
@@ -15,3 +15,15 @@ function string.fromhex(str)
return string.char(tonumber(cc, 16))
end))
end
+
+-- get information from the pawsd response
+function extractfromresponse(response, start, last)
+
+ local result = {}
+
+ table.move(response, start, last, 1, result)
+ result = string.tohex(table.concat(result))
+
+ return result
+
+end