Skip to content

Commit

Permalink
Merge pull request #231 from joemphilips/add_dns_address
Browse files Browse the repository at this point in the history
Add DnsHostname type to NetAddress
  • Loading branch information
joemphilips authored Aug 25, 2022
2 parents 2ff8912 + a961899 commit df8b07b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 55 deletions.
82 changes: 37 additions & 45 deletions src/DotNetLightning.Core/Serialization/Msgs/Msgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,20 +1212,25 @@ type NetAddress =
| IPv6 of IPv4Or6Data
| OnionV2 of OnionV2EndPoint
| OnionV3 of OnionV3EndPoint
| DnsHostName of DnsHostName

member this.GetId() =
match this with
| IPv4 _ -> 1uy
| IPv6 _ -> 2uy
| OnionV2 _ -> 3uy
| OnionV3 _ -> 4uy
| DnsHostName _ -> 5uy

member this.Length =
match this with
| IPv4 _ -> 6us
| IPv6 _ -> 18us
| OnionV2 _ -> 12us
| OnionV3 _ -> 37us
| DnsHostName {
HostName = name
} -> name.Length |> uint16

member this.WriteTo(ls: LightningWriterStream) =
ls.Write(this.GetId())
Expand All @@ -1245,10 +1250,14 @@ type NetAddress =
ls.Write(d.CheckSum, false)
ls.Write(d.Version)
ls.Write(d.Port, false)
| DnsHostName d ->
ls.WriteByte(d.HostName.Length |> uint8)
ls.Write(d.HostName)
ls.Write(d.Port, false)

static member ReadFrom
(ls: LightningReaderStream)
: NetAdddrSerilizationResult =
: NetAddrSerializationResult =
let id = ls.ReadUInt8()

match id with
Expand Down Expand Up @@ -1296,6 +1305,17 @@ type NetAddress =
Port = port
}
|> Ok
| 5uy ->
let hostnameLength = ls.ReadByte()
let hostname = ls.ReadBytes(int hostnameLength)
let port = ls.ReadUInt16(false)

DnsHostName
{
HostName = hostname
Port = port
}
|> Ok
| unknown -> Result.Error(unknown)

/// Pair of Address and port.
Expand Down Expand Up @@ -1324,10 +1344,17 @@ and OnionV3EndPoint =
Port: uint16
}

and NetAdddrSerilizationResult = Result<NetAddress, UnknownNetAddr>
and DnsHostName =
{
HostName: byte []
Port: uint16
}

and NetAddrSerializationResult = Result<NetAddress, UnknownNetAddr>
and UnknownNetAddr = byte



/// `node_announcement` in [bolt07](https://github.com/lightning/bolts/blob/master/07-routing-gossip.md)
/// without signatures.
[<CLIMutable>]
Expand Down Expand Up @@ -1360,49 +1387,14 @@ type UnsignedNodeAnnouncementMsg =
while addrReadPos < addrLen && (not foundUnknown) do
let addr = NetAddress.ReadFrom ls

ignore
<| match addr with
| Ok(IPv4 _) ->
if addresses.Length > 0 then
raise
<| FormatException(
sprintf "Extra Address per type %A" addresses
)
| Ok(IPv6 _) ->
if addresses.Length > 1
|| (addresses.Length = 1
&& addresses.[0].GetId() <> 1uy) then
raise
<| FormatException(
sprintf "Extra Address per type %A" addresses
)
| Ok(OnionV2 _) ->
if addresses.Length > 2
|| (addresses.Length > 0
&& addresses.[0].GetId() > 2uy) then
raise
<| FormatException(
sprintf "Extra Address per type %A" addresses
)
| Ok(OnionV3 _) ->
if addresses.Length > 3
|| (addresses.Length > 0
&& addresses.[0].GetId() > 3uy) then
raise
<| FormatException(
sprintf "Extra Address per type %A" addresses
)
| Result.Error v ->
excessAddressDataByte <- v
foundUnknown <- true
addrReadPos <- addrReadPos + 1us

if (not foundUnknown) then
match addr with
| Ok addr ->
addrReadPos <- addrReadPos + (1us + addr.Length)
addresses <- addr :: addresses
| Result.Error _ -> failwith "Unreachable"
match addr with
| Error v ->
excessAddressDataByte <- v
foundUnknown <- true
addrReadPos <- addrReadPos + 1us
| Ok addr ->
addrReadPos <- addrReadPos + (1us + addr.Length)
addresses <- addr :: addresses

addresses |> List.rev |> Array.ofList

Expand Down
39 changes: 30 additions & 9 deletions tests/DNLTestBase/Generators/Msgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -525,16 +525,33 @@ let private onionV3AddressGen =
}
}


let private netAddressesGen =
let private dnsHostnameGen =
gen {
let! ipv4 = Gen.optionOf(ipV4AddressGen)
let! ipv6 = Gen.optionOf(ipV6AddressGen)
let! onionv2 = Gen.optionOf(onionV2AddressGen)
let! onionv3 = Gen.optionOf(onionV3AddressGen)
return [| ipv4; ipv6; onionv2; onionv3 |]
let! name =
Arb.generate<NonEmptyArray<byte>>
|> Gen.filter(fun b -> b.Get.Length < 255)

let! port = Arb.generate<uint16>

return
NetAddress.DnsHostName
{
HostName = name.Get
Port = port
}
}

let private netAddressesGen =
Gen.sequence
[
ipV4AddressGen
ipV6AddressGen
onionV2AddressGen
onionV3AddressGen
dnsHostnameGen
]
|> Gen.map(Array.ofList)

let unsignedNodeAnnouncementGen =
gen {
let! f = featuresGen
Expand All @@ -554,13 +571,17 @@ let unsignedNodeAnnouncementGen =
<*> Arb.generate<uint8>

let! a = uint256Gen
let! addrs = netAddressesGen |> Gen.map(Array.choose id)
let! addrs = netAddressesGen

let! eAddrs =
bytesGen
|> Gen.filter(fun b ->
b.Length = 0
|| (b.[0] <> 1uy && b.[0] <> 2uy && b.[0] <> 3uy && b.[0] <> 4uy)
|| (b[0] <> 1uy
&& b[0] <> 2uy
&& b[0] <> 3uy
&& b[0] <> 4uy
&& b[0] <> 5uy)
)

let! ed = bytesGen
Expand Down
2 changes: 1 addition & 1 deletion tests/DotNetLightning.Core.Tests/Serialization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module SerializationTest =
)
|]
ExcessAddressData =
[| 5uy; 121uy; 62uy; 96uy; 44uy; 34uy |]
[| 6uy; 121uy; 62uy; 96uy; 44uy; 34uy |]
ExcessData = [||]
}
}
Expand Down

0 comments on commit df8b07b

Please sign in to comment.