Pass packet as decoding result

This commit is contained in:
kdeme 2020-04-24 16:52:41 +02:00
parent 5dec5c5a9b
commit 09a127ef1b
No known key found for this signature in database
GPG Key ID: 4E8DD21420AF43F5
2 changed files with 19 additions and 24 deletions

View File

@ -190,19 +190,20 @@ proc decryptGCM*(key: AesKey, nonce, ct, authData: openarray[byte]):
return some(res) return some(res)
proc decodePacketBody(typ: byte, proc decodePacketBody(body: openarray[byte]):
body: openarray[byte], DecodeResult[Packet] {.raises:[Defect].} =
res: var Packet): if body.len < 1:
DecodeResult[void] {.raises:[Defect].} =
if typ < PacketKind.low.byte or typ > PacketKind.high.byte:
return err(PacketError) return err(PacketError)
let kind = cast[PacketKind](typ) if body[0] < PacketKind.low.byte or body[0] > PacketKind.high.byte:
res = Packet(kind: kind) return err(PacketError)
var rlp = rlpFromBytes(body)
let kind = cast[PacketKind](body[0])
var packet = Packet(kind: kind)
var rlp = rlpFromBytes(body.toOpenArray(1, body.high))
if rlp.enterList: if rlp.enterList:
try: try:
res.reqId = rlp.read(RequestId) packet.reqId = rlp.read(RequestId)
except RlpError: except RlpError:
return err(PacketError) return err(PacketError)
@ -214,17 +215,17 @@ proc decodePacketBody(typ: byte,
try: try:
case kind case kind
of unused: return err(PacketError) of unused: return err(PacketError)
of ping: rlp.decode(res.ping) of ping: rlp.decode(packet.ping)
of pong: rlp.decode(res.pong) of pong: rlp.decode(packet.pong)
of findNode: rlp.decode(res.findNode) of findNode: rlp.decode(packet.findNode)
of nodes: rlp.decode(res.nodes) of nodes: rlp.decode(packet.nodes)
of regtopic, ticket, regconfirmation, topicquery: of regtopic, ticket, regconfirmation, topicquery:
# TODO: Implement support for topic advertisement # TODO: Implement support for topic advertisement
return err(UnsupportedPacketType) return err(UnsupportedPacketType)
except RlpError, ValueError: except RlpError, ValueError:
return err(PacketError) return err(PacketError)
ok() ok(packet)
else: else:
err(PacketError) err(PacketError)
@ -270,8 +271,7 @@ proc decodeEncrypted*(c: var Codec,
fromAddr: Address, fromAddr: Address,
input: openArray[byte], input: openArray[byte],
authTag: var AuthTag, authTag: var AuthTag,
newNode: var Node, newNode: var Node): DecodeResult[Packet] =
packet: var Packet): DecodeResult[void] =
var r = rlpFromBytes(input.toOpenArray(tagSize, input.high)) var r = rlpFromBytes(input.toOpenArray(tagSize, input.high))
var auth: AuthHeader var auth: AuthHeader
@ -335,12 +335,7 @@ proc decodeEncrypted*(c: var Codec,
discard c.db.deleteKeys(fromId, fromAddr) discard c.db.deleteKeys(fromId, fromAddr)
return err(DecryptError) return err(DecryptError)
let packetData = body.get() decodePacketBody(body.get())
if packetData.len > 1:
decodePacketBody(packetData[0],
packetData.toOpenArray(1, packetData.high), packet)
else:
err(PacketError)
proc newRequestId*(): Result[RequestId, cstring] {.raises:[].} = proc newRequestId*(): Result[RequestId, cstring] {.raises:[].} =
var id: RequestId var id: RequestId

View File

@ -288,9 +288,9 @@ proc receive*(d: Protocol, a: Address, msg: openArray[byte]) {.gcsafe,
var authTag: AuthTag var authTag: AuthTag
var node: Node var node: Node
var packet: Packet let decoded = d.codec.decodeEncrypted(sender, a, msg, authTag, node)
let decoded = d.codec.decodeEncrypted(sender, a, msg, authTag, node, packet)
if decoded.isOk: if decoded.isOk:
let packet = decoded[]
if not node.isNil: if not node.isNil:
# Not filling table with nodes without correct IP in the ENR # Not filling table with nodes without correct IP in the ENR
if a.ip == node.address.ip: if a.ip == node.address.ip: