nim-libp2p/libp2p/protocols/identify.nim

125 lines
3.7 KiB
Nim
Raw Normal View History

2019-08-28 02:30:53 +00:00
## Nim-LibP2P
## Copyright (c) 2018 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
2019-09-08 07:43:33 +00:00
import options, strformat
2019-09-09 17:33:32 +00:00
import chronos, chronicles
2019-09-08 07:43:33 +00:00
import ../protobuf/minprotobuf,
../peerinfo,
../connection,
../peer,
../crypto/crypto,
../multiaddress,
2019-09-09 17:33:32 +00:00
../protocols/protocol
2019-08-28 02:30:53 +00:00
2019-09-10 02:15:52 +00:00
logScope:
topic = "identify"
2019-08-30 05:16:30 +00:00
const IdentifyCodec* = "/ipfs/id/1.0.0"
const IdentifyPushCodec* = "/ipfs/id/push/1.0.0"
const ProtoVersion* = "ipfs/0.1.0"
const AgentVersion* = "nim-libp2p/0.0.1"
2019-08-28 02:30:53 +00:00
2019-08-30 15:28:07 +00:00
#TODO: implment push identify, leaving out for now as it is not essential
2019-08-28 02:30:53 +00:00
type
IdentityNoMatchError* = object of CatchableError
IdentityInvalidMsgError* = object of CatchableError
2019-08-28 02:30:53 +00:00
2019-08-30 05:16:30 +00:00
IdentifyInfo* = object
pubKey*: PublicKey
addrs*: seq[MultiAddress]
observedAddr*: MultiAddress
protoVersion*: string
agentVersion*: string
protos*: seq[string]
2019-08-28 02:30:53 +00:00
2019-08-30 05:16:30 +00:00
Identify* = ref object of LPProtocol
2019-09-03 20:39:28 +00:00
peerInfo*: PeerInfo
2019-08-28 02:30:53 +00:00
2019-08-30 05:16:30 +00:00
proc encodeMsg*(peerInfo: PeerInfo, observedAddrs: Multiaddress): ProtoBuffer =
result = initProtoBuffer()
2019-08-30 05:16:30 +00:00
result.write(initProtoField(1, peerInfo.peerId.publicKey.getBytes()))
for ma in peerInfo.addrs:
result.write(initProtoField(2, ma.data.buffer))
for proto in peerInfo.protocols:
result.write(initProtoField(3, proto))
result.write(initProtoField(4, observedAddrs.data.buffer))
let protoVersion = ProtoVersion
result.write(initProtoField(5, protoVersion))
let agentVersion = AgentVersion
result.write(initProtoField(6, agentVersion))
result.finish()
2019-08-30 05:16:30 +00:00
proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
var pb = initProtoBuffer(buf)
discard pb.getValue(1, result.pubKey)
2019-08-30 05:16:30 +00:00
result.addrs = newSeq[MultiAddress]()
var address = newSeq[byte]()
while pb.getBytes(2, address) != -1:
if len(address) != 0:
var copyaddr = address
result.addrs.add(MultiAddress.init(copyaddr))
address.setLen(0)
var proto = ""
while pb.getString(3, proto) > 0:
result.protos.add(proto)
2019-09-01 21:52:02 +00:00
proto = ""
2019-08-30 05:16:30 +00:00
var observableAddr = newSeq[byte]()
if pb.getBytes(4, observableAddr) > 0: # attempt to read the observed addr
result.observedAddr = MultiAddress.init(observableAddr)
2019-08-31 19:12:49 +00:00
discard pb.getString(5, result.protoVersion)
discard pb.getString(6, result.agentVersion)
2019-09-03 20:39:28 +00:00
proc newIdentify*(peerInfo: PeerInfo): Identify =
new result
result.peerInfo = peerInfo
result.init()
2019-08-30 23:45:57 +00:00
method init*(p: Identify) =
2019-09-02 20:45:52 +00:00
proc handle(conn: Connection, proto: string) {.async, gcsafe, closure.} =
2019-08-30 23:45:57 +00:00
var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs())
await conn.writeLp(pb.buffer)
p.handler = handle
p.codec = IdentifyCodec
2019-08-30 23:45:57 +00:00
proc identify*(p: Identify,
conn: Connection,
remotePeerInfo: Option[PeerInfo] = none(PeerInfo)):
Future[IdentifyInfo] {.async.} =
var message = await conn.readLp()
if len(message) == 0:
2019-09-08 07:43:33 +00:00
debug "identify: Invalid or empty message received!"
raise newException(IdentityInvalidMsgError,
"Invalid or empty message received!")
2019-08-30 05:16:30 +00:00
result = decodeMsg(message)
2019-09-09 17:33:32 +00:00
debug "identify: Identify for remote peer succeded"
if remotePeerInfo.isSome and
remotePeerInfo.get().peerId.publicKey != result.pubKey:
2019-09-08 07:43:33 +00:00
debug "identify: Peer's remote public key doesn't match"
raise newException(IdentityNoMatchError,
"Peer's remote public key doesn't match")
2019-08-30 05:16:30 +00:00
proc push*(p: Identify, conn: Connection) {.async.} =
await conn.write(IdentifyPushCodec)
var pb = encodeMsg(p.peerInfo, await conn.getObservedAddrs())
let length = pb.getLen()
await conn.writeLp(pb.buffer)