Make the MultiAddress.init function used in NBC non-failing
This commit is contained in:
parent
5960d42c50
commit
828a80ec8f
|
@ -12,7 +12,7 @@
|
||||||
{.push raises: [Defect].}
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import nativesockets
|
import nativesockets
|
||||||
import tables, strutils, net
|
import tables, strutils, stew/shims/net
|
||||||
import chronos
|
import chronos
|
||||||
import multicodec, multihash, multibase, transcoder, vbuffer
|
import multicodec, multihash, multibase, transcoder, vbuffer
|
||||||
import stew/[base58, base32, endians2, results]
|
import stew/[base58, base32, endians2, results]
|
||||||
|
@ -46,6 +46,10 @@ type
|
||||||
|
|
||||||
MaResult*[T] = Result[T, string]
|
MaResult*[T] = Result[T, string]
|
||||||
|
|
||||||
|
IpTransportProtocol* = enum
|
||||||
|
tcpProtocol
|
||||||
|
udpProtocol
|
||||||
|
|
||||||
const
|
const
|
||||||
# These are needed in order to avoid an ambiguity error stemming from
|
# These are needed in order to avoid an ambiguity error stemming from
|
||||||
# some cint constants with the same name defined in the posix modules
|
# some cint constants with the same name defined in the posix modules
|
||||||
|
@ -816,29 +820,26 @@ proc init*(mtype: typedesc[MultiAddress]): MultiAddress =
|
||||||
result.data = initVBuffer()
|
result.data = initVBuffer()
|
||||||
|
|
||||||
proc init*(mtype: typedesc[MultiAddress],
|
proc init*(mtype: typedesc[MultiAddress],
|
||||||
address: IpAddress, protocol: Protocol, port: Port): MaResult[MultiAddress] =
|
address: ValidIpAddress,
|
||||||
## Initialize MultiAddress using stdlib's net.IpAddress (IPv4/IPv6) and
|
protocol: IpTransportProtocol,
|
||||||
## net.Protocol (UDP/TCP) information.
|
port: Port): MultiAddress =
|
||||||
var res: MultiAddress
|
let
|
||||||
res.data = initVBuffer()
|
familyProto = case address.family
|
||||||
let familyProto = case address.family
|
of IpAddressFamily.IPv4: getProtocol("ip4")
|
||||||
of IpAddressFamily.IPv4: getProtocol("ip4")
|
of IpAddressFamily.IPv6: getProtocol("ip6")
|
||||||
of IpAddressFamily.IPv6: getProtocol("ip6")
|
|
||||||
let protoProto = case protocol
|
|
||||||
of IPPROTO_TCP: getProtocol("tcp")
|
|
||||||
of IPPROTO_UDP: getProtocol("udp")
|
|
||||||
else: return err("multiaddress: protocol should be either TCP or UDP")
|
|
||||||
|
|
||||||
res.data.write(familyProto.mcodec)
|
protoProto = case protocol
|
||||||
if not familyProto.coder.stringToBuffer($address, res.data):
|
of tcpProtocol: getProtocol("tcp")
|
||||||
err("multiaddress: Error encoding IPv4/IPv6 address")
|
of udpProtocol: getProtocol("udp")
|
||||||
else:
|
|
||||||
res.data.write(protoProto.mcodec)
|
var data = initVBuffer()
|
||||||
if not protoProto.coder.stringToBuffer($port, res.data):
|
data.write(familyProto.mcodec)
|
||||||
err("multiaddress: Error encoding port number")
|
doAssert familyProto.coder.stringToBuffer($address, data)
|
||||||
else:
|
data.write(protoProto.mcodec)
|
||||||
res.data.finish()
|
doAssert protoProto.coder.stringToBuffer($port, data)
|
||||||
ok(res)
|
data.finish()
|
||||||
|
|
||||||
|
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] =
|
||||||
|
|
Loading…
Reference in New Issue