Proper fix for init(ValidIpAddress).

This commit is contained in:
cheatfate 2020-10-30 17:52:38 +02:00
parent 3d9948a65e
commit ff48d0b1a2
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
1 changed files with 14 additions and 20 deletions

View File

@ -838,33 +838,27 @@ proc init*(mtype: typedesc[MultiAddress]): MultiAddress =
## Initialize empty MultiAddress. ## Initialize empty MultiAddress.
result.data = initVBuffer() result.data = initVBuffer()
proc init*(mtype: typedesc[MultiAddress], proc init*(mtype: typedesc[MultiAddress], address: ValidIpAddress,
address: ValidIpAddress, protocol: IpTransportProtocol, port: Port): MultiAddress =
protocol: IpTransportProtocol, var res: MultiAddress
port: Port): MaResult[MultiAddress] = res.data = initVBuffer()
let let
familyProto = case address.family networkProto = case address.family
of IpAddressFamily.IPv4: getProtocol("ip4") of IpAddressFamily.IPv4: getProtocol("ip4")
of IpAddressFamily.IPv6: getProtocol("ip6") of IpAddressFamily.IPv6: getProtocol("ip6")
protoProto = case protocol transportProto = case protocol
of tcpProtocol: getProtocol("tcp") of tcpProtocol: getProtocol("tcp")
of udpProtocol: getProtocol("udp") of udpProtocol: getProtocol("udp")
var data = initVBuffer() res.data.write(networkProto.mcodec)
data.write(familyProto.mcodec) case address.family
var written = familyProto.coder.stringToBuffer($address, data) of IpAddressFamily.IPv4: res.data.writeArray(address.address_v4)
if not written: of IpAddressFamily.IPv6: res.data.writeArray(address.address_v6)
return err("multiaddress: Merely writing a string to a buffer should always be possible, address") res.data.write(transportProto.mcodec)
res.data.write(toBytesBE(uint16(port)))
data.write(protoProto.mcodec) res.data.finish()
written = protoProto.coder.stringToBuffer($port, data) ok(res)
if not written:
return err("multiaddress: Merely writing a string to a buffer should always be possible, port")
data.finish()
ok(MultiAddress(data: data))
proc init*(mtype: typedesc[MultiAddress], address: TransportAddress, proc init*(mtype: typedesc[MultiAddress], address: TransportAddress,
protocol = IPPROTO_TCP): MaResult[MultiAddress] = protocol = IPPROTO_TCP): MaResult[MultiAddress] =