No hard limit for auth msg, rlpx encoding corrected, cosmetics.

This commit is contained in:
Yuriy Glukhov 2018-05-16 11:12:12 +03:00
parent 74f0b705f0
commit 98cf6376c8
1 changed files with 17 additions and 16 deletions

View File

@ -213,6 +213,9 @@ proc dispatchMsg(peer: Peer, msgId: int, msgData: var Rlp) =
thunk(peer, msgData) thunk(peer, msgData)
proc send(p: Peer, data: BytesRange) {.async.} = proc send(p: Peer, data: BytesRange) {.async.} =
# var rlp = rlpFromBytes(data)
# echo "sending: ", rlp.read(int)
# echo "payload: ", rlp.inspect
var cipherText = encryptMsg(data, p.secretsState) var cipherText = encryptMsg(data, p.secretsState)
await p.socket.send(addr cipherText[0], cipherText.len) await p.socket.send(addr cipherText[0], cipherText.len)
@ -419,8 +422,10 @@ macro rlpxProtocol*(protoIdentifier: untyped,
newEmptyNode(), newEmptyNode(),
msgTypeFields) msgTypeFields)
var paramCount = 0
# implement sending proc # implement sending proc
for param, paramType in n.typedParams(skip = 1): for param, paramType in n.typedParams(skip = 1):
inc paramCount
appendParams.add quote do: appendParams.add quote do:
`append`(`rlpWriter`, `param`) `append`(`rlpWriter`, `param`)
@ -450,9 +455,11 @@ macro rlpxProtocol*(protoIdentifier: untyped,
else: else:
quote: `append`(`rlpWriter`, `nextId`) quote: `append`(`rlpWriter`, `nextId`)
let paramCountNode = newLit(paramCount)
n.body = quote do: n.body = quote do:
var `rlpWriter` = `initRlpWriter`() var `rlpWriter` = `initRlpWriter`()
`writeMsgId` `writeMsgId`
`rlpWriter`.startList(`paramCountNode`)
`appendParams` `appendParams`
return `send`(`peer`, `finish`(`rlpWriter`)) return `send`(`peer`, `finish`(`rlpWriter`))
@ -536,20 +543,17 @@ proc rlpxConnect*(myKeys: KeyPair, listenPort: Port, remote: Node): Future[Peer]
result.remote = remote result.remote = remote
await result.socket.connect($remote.node.address.ip, remote.node.address.tcpPort) await result.socket.connect($remote.node.address.ip, remote.node.address.tcpPort)
const encryptionEnabled = true
var handshake = newHandshake({Initiator}) var handshake = newHandshake({Initiator})
handshake.host = myKeys handshake.host = myKeys
var authMsg: array[AuthMessageMaxEIP8, byte] var authMsg: array[AuthMessageMaxEIP8, byte]
var authMsgLen = 0 var authMsgLen = 0
check authMessage(handshake, remote.node.pubkey, authMsg, authMsgLen, check authMessage(handshake, remote.node.pubkey, authMsg, authMsgLen)
encrypt = encryptionEnabled)
await result.socket.send(addr authMsg[0], authMsgLen) await result.socket.send(addr authMsg[0], authMsgLen)
var ackMsg: array[AckMessageMaxEIP8, byte] var ackMsg: array[AckMessageMaxEIP8, byte]
let ackMsgLen = handshake.ackSize(encrypt = encryptionEnabled) let ackMsgLen = handshake.ackSize()
await result.socket.fullRecvInto(addr ackMsg, ackMsgLen) await result.socket.fullRecvInto(addr ackMsg, ackMsgLen)
check handshake.decodeAckMessage(^ackMsg) check handshake.decodeAckMessage(^ackMsg)
@ -574,18 +578,15 @@ proc rlpxConnectIncoming*(myKeys: KeyPair, listenPort: Port, address: IpAddress,
var handshake = newHandshake({Responder}) var handshake = newHandshake({Responder})
handshake.host = myKeys handshake.host = myKeys
var authMsg: array[4086, byte] var authMsg = newSeqOfCap[byte](1024)
var authMsgLen = AuthMessageV4Length authMsg.setLen(AuthMessageV4Length)
await s.fullRecvInto(addr authMsg[0], authMsgLen) await s.fullRecvInto(authMsg)
var ret = handshake.decodeAuthMessage(^authMsg) var ret = handshake.decodeAuthMessage(authMsg)
if ret == AuthStatus.IncompleteError: # Eip8 auth message is likely if ret == AuthStatus.IncompleteError: # Eip8 auth message is likely
let expectedLen = expectedAuthMsgLenEip8(authMsg).int + 2 authMsg.setLen(expectedAuthMsgLenEip8(authMsg).int + 2)
if expectedLen > authMsg.len: await s.fullRecvInto(addr authMsg[AuthMessageV4Length], authMsg.len - AuthMessageV4Length)
raise newException(Exception, "Auth message too big: " & $authMsgLen) ret = handshake.decodeAuthMessage(authMsg)
await s.fullRecvInto(addr authMsg[authMsgLen], expectedLen - authMsgLen)
authMsgLen = expectedLen
ret = handshake.decodeAuthMessage(^authMsg)
check ret check ret
@ -594,7 +595,7 @@ proc rlpxConnectIncoming*(myKeys: KeyPair, listenPort: Port, address: IpAddress,
check handshake.ackMessage(ackMsg, ackMsgLen) check handshake.ackMessage(ackMsg, ackMsgLen)
await s.send(addr ackMsg[0], ackMsgLen) await s.send(addr ackMsg[0], ackMsgLen)
initSecretState(handshake, ^authMsg, ^ackMsg, result) initSecretState(handshake, authMsg, ^ackMsg, result)
var response = await result.nextMsg(p2p.hello, discardOthers = true) var response = await result.nextMsg(p2p.hello, discardOthers = true)
discard result.hello(baseProtocolVersion, clienId, discard result.hello(baseProtocolVersion, clienId,