add init from TransportAddress to multiaddress

This commit is contained in:
Dmitriy Ryajov 2019-10-09 06:01:50 +09:00
parent ae7b354819
commit 0b77c8c6e3
3 changed files with 19 additions and 18 deletions

View File

@ -632,6 +632,23 @@ proc validate*(ma: MultiAddress): bool =
discard discard
result = true result = true
proc init*(mtype: typedesc[MultiAddress], address: TransportAddress): MultiAddress =
## Returns a multiaddr of ``address``.
case address.family
of AddressFamily.IPv4:
var a = IpAddress(
family: IpAddressFamily.IPv4,
address_v4: address.address_v4
)
result = MultiAddress.init(a, Protocol.IPPROTO_TCP, address.port)
of AddressFamily.IPv6:
var a = IpAddress(family: IpAddressFamily.IPv6,
address_v6: address.address_v6)
result = MultiAddress.init(a, Protocol.IPPROTO_TCP, address.port)
else:
raise newException(TransportAddressError,
"Invalid address for transport!")
proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec,
value: openarray[byte]): MultiAddress = value: openarray[byte]): MultiAddress =
## Initialize MultiAddress object from protocol id ``protocol`` and array ## Initialize MultiAddress object from protocol id ``protocol`` and array

View File

@ -28,7 +28,7 @@ proc connHandler*(t: Transport,
Future[Connection] {.async, gcsafe.} = Future[Connection] {.async, gcsafe.} =
trace "handling connection for", address = $client.remoteAddress trace "handling connection for", address = $client.remoteAddress
let conn: Connection = newConnection(newChronosStream(server, client)) let conn: Connection = newConnection(newChronosStream(server, client))
conn.observedAddrs = client.remoteAddress.toMultiAddr() conn.observedAddrs = MultiAddress.init(client.remoteAddress)
if not initiator: if not initiator:
let handlerFut = if t.handler == nil: nil else: t.handler(conn) let handlerFut = if t.handler == nil: nil else: t.handler(conn)
let connHolder: ConnHolder = ConnHolder(connection: conn, let connHolder: ConnHolder = ConnHolder(connection: conn,
@ -65,7 +65,7 @@ method listen*(t: TcpTransport,
t.server.start() t.server.start()
# always get the resolved address in case we're bound to 0.0.0.0:0 # always get the resolved address in case we're bound to 0.0.0.0:0
t.ma = t.server.sock.getLocalAddress().toMultiAddr() t.ma = MultiAddress.init(t.server.sock.getLocalAddress())
result = t.server.join() result = t.server.join()
debug "started node on", address = t.ma debug "started node on", address = t.ma

View File

@ -140,19 +140,3 @@ proc getLocalAddress*(sock: AsyncFD): TransportAddress =
if getsockname(SocketHandle(sock), cast[ptr SockAddr](addr saddr), if getsockname(SocketHandle(sock), cast[ptr SockAddr](addr saddr),
addr slen) == 0: addr slen) == 0:
fromSAddr(addr saddr, slen, result) fromSAddr(addr saddr, slen, result)
proc toMultiAddr*(address: TransportAddress): MultiAddress =
## Returns a multiaddr of ``address``.
case address.family
of AddressFamily.IPv4:
var a = IpAddress(
family: IpAddressFamily.IPv4,
address_v4: address.address_v4
)
result = MultiAddress.init(a, Protocol.IPPROTO_TCP, address.port)
of AddressFamily.IPv6:
var a = IpAddress(family: IpAddressFamily.IPv6,
address_v6: address.address_v6)
result = MultiAddress.init(a, Protocol.IPPROTO_TCP, address.port)
else:
raise newException(TransportAddressError, "Invalid address for transport!")