2023-01-06 10:14:38 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2023-01-06 10:14:38 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2023-01-06 10:14:38 +00:00
|
|
|
|
|
|
|
import stew/[results, objects]
|
|
|
|
import chronos, chronicles
|
|
|
|
import ../../../multiaddress,
|
|
|
|
../../../peerid,
|
|
|
|
../../../errors
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "libp2p autonat"
|
|
|
|
|
|
|
|
const
|
|
|
|
AutonatCodec* = "/libp2p/autonat/1.0.0"
|
|
|
|
AddressLimit* = 8
|
|
|
|
|
|
|
|
type
|
|
|
|
AutonatError* = object of LPError
|
|
|
|
AutonatUnreachableError* = object of LPError
|
|
|
|
|
|
|
|
MsgType* = enum
|
|
|
|
Dial = 0
|
|
|
|
DialResponse = 1
|
|
|
|
|
|
|
|
ResponseStatus* = enum
|
|
|
|
Ok = 0
|
|
|
|
DialError = 100
|
|
|
|
DialRefused = 101
|
|
|
|
BadRequest = 200
|
|
|
|
InternalError = 300
|
|
|
|
|
|
|
|
AutonatPeerInfo* = object
|
2023-06-28 14:44:58 +00:00
|
|
|
id*: Opt[PeerId]
|
2023-01-06 10:14:38 +00:00
|
|
|
addrs*: seq[MultiAddress]
|
|
|
|
|
|
|
|
AutonatDial* = object
|
2023-06-28 14:44:58 +00:00
|
|
|
peerInfo*: Opt[AutonatPeerInfo]
|
2023-01-06 10:14:38 +00:00
|
|
|
|
|
|
|
AutonatDialResponse* = object
|
|
|
|
status*: ResponseStatus
|
2023-06-28 14:44:58 +00:00
|
|
|
text*: Opt[string]
|
|
|
|
ma*: Opt[MultiAddress]
|
2023-01-06 10:14:38 +00:00
|
|
|
|
|
|
|
AutonatMsg* = object
|
|
|
|
msgType*: MsgType
|
2023-06-28 14:44:58 +00:00
|
|
|
dial*: Opt[AutonatDial]
|
|
|
|
response*: Opt[AutonatDialResponse]
|
2023-01-06 10:14:38 +00:00
|
|
|
|
2023-04-06 13:23:35 +00:00
|
|
|
NetworkReachability* {.pure.} = enum
|
|
|
|
Unknown, NotReachable, Reachable
|
|
|
|
|
2023-01-06 10:14:38 +00:00
|
|
|
proc encode(p: AutonatPeerInfo): ProtoBuffer =
|
|
|
|
result = initProtoBuffer()
|
2023-06-28 14:44:58 +00:00
|
|
|
p.id.withValue(id):
|
|
|
|
result.write(1, id)
|
2023-01-06 10:14:38 +00:00
|
|
|
for ma in p.addrs:
|
|
|
|
result.write(2, ma.data.buffer)
|
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc encode*(d: AutonatDial): ProtoBuffer =
|
|
|
|
result = initProtoBuffer()
|
|
|
|
result.write(1, MsgType.Dial.uint)
|
|
|
|
var dial = initProtoBuffer()
|
2023-06-28 14:44:58 +00:00
|
|
|
d.peerInfo.withValue(pinfo):
|
|
|
|
dial.write(1, encode(pinfo))
|
2023-01-06 10:14:38 +00:00
|
|
|
dial.finish()
|
|
|
|
result.write(2, dial.buffer)
|
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc encode*(r: AutonatDialResponse): ProtoBuffer =
|
|
|
|
result = initProtoBuffer()
|
|
|
|
result.write(1, MsgType.DialResponse.uint)
|
|
|
|
var bufferResponse = initProtoBuffer()
|
|
|
|
bufferResponse.write(1, r.status.uint)
|
2023-06-28 14:44:58 +00:00
|
|
|
r.text.withValue(text):
|
|
|
|
bufferResponse.write(2, text)
|
|
|
|
r.ma.withValue(ma):
|
|
|
|
bufferResponse.write(3, ma)
|
2023-01-06 10:14:38 +00:00
|
|
|
bufferResponse.finish()
|
|
|
|
result.write(3, bufferResponse.buffer)
|
|
|
|
result.finish()
|
|
|
|
|
|
|
|
proc encode*(msg: AutonatMsg): ProtoBuffer =
|
2023-06-28 14:44:58 +00:00
|
|
|
msg.dial.withValue(dial):
|
|
|
|
return encode(dial)
|
|
|
|
msg.response.withValue(res):
|
|
|
|
return encode(res)
|
2023-01-06 10:14:38 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
proc decode*(_: typedesc[AutonatMsg], buf: seq[byte]): Opt[AutonatMsg] =
|
2023-01-06 10:14:38 +00:00
|
|
|
var
|
|
|
|
msgTypeOrd: uint32
|
|
|
|
pbDial: ProtoBuffer
|
|
|
|
pbResponse: ProtoBuffer
|
|
|
|
msg: AutonatMsg
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
let pb = initProtoBuffer(buf)
|
2023-01-06 10:14:38 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
if ? pb.getField(1, msgTypeOrd).toOpt() and not checkedEnumAssign(msg.msgType, msgTypeOrd):
|
|
|
|
return Opt.none(AutonatMsg)
|
|
|
|
if ? pb.getField(2, pbDial).toOpt():
|
2023-01-06 10:14:38 +00:00
|
|
|
var
|
|
|
|
pbPeerInfo: ProtoBuffer
|
|
|
|
dial: AutonatDial
|
2023-06-28 14:44:58 +00:00
|
|
|
let r4 = ? pbDial.getField(1, pbPeerInfo).toOpt()
|
2023-01-06 10:14:38 +00:00
|
|
|
|
|
|
|
var peerInfo: AutonatPeerInfo
|
2023-06-28 14:44:58 +00:00
|
|
|
if r4:
|
2023-01-06 10:14:38 +00:00
|
|
|
var pid: PeerId
|
|
|
|
let
|
2023-06-28 14:44:58 +00:00
|
|
|
r5 = ? pbPeerInfo.getField(1, pid).toOpt()
|
|
|
|
r6 = ? pbPeerInfo.getRepeatedField(2, peerInfo.addrs).toOpt()
|
|
|
|
if r5: peerInfo.id = Opt.some(pid)
|
|
|
|
dial.peerInfo = Opt.some(peerInfo)
|
|
|
|
msg.dial = Opt.some(dial)
|
|
|
|
|
|
|
|
if ? pb.getField(3, pbResponse).toOpt():
|
2023-01-06 10:14:38 +00:00
|
|
|
var
|
|
|
|
statusOrd: uint
|
|
|
|
text: string
|
|
|
|
ma: MultiAddress
|
|
|
|
response: AutonatDialResponse
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
if ? pbResponse.getField(1, statusOrd).optValue():
|
|
|
|
if not checkedEnumAssign(response.status, statusOrd):
|
|
|
|
return Opt.none(AutonatMsg)
|
|
|
|
if ? pbResponse.getField(2, text).optValue():
|
|
|
|
response.text = Opt.some(text)
|
|
|
|
if ? pbResponse.getField(3, ma).optValue():
|
|
|
|
response.ma = Opt.some(ma)
|
|
|
|
msg.response = Opt.some(response)
|
|
|
|
return Opt.some(msg)
|