| | 1 | | using System.Globalization; |
| | 2 | | using NLightning.Domain.Protocol.Constants; |
| | 3 | |
|
| | 4 | | namespace NLightning.Domain.Protocol.Tlv; |
| | 5 | |
|
| | 6 | | public class RemoteAddressTlv : BaseTlv |
| | 7 | | { |
| 0 | 8 | | public byte AddressType { get; } |
| 0 | 9 | | public string Address { get; } |
| 0 | 10 | | public ushort Port { get; } |
| | 11 | |
|
| 0 | 12 | | public RemoteAddressTlv(byte addressType, string address, ushort port) : base(TlvConstants.RemoteAddress) |
| | 13 | | { |
| 0 | 14 | | if (addressType is < 1 or > 5) |
| 0 | 15 | | throw new ArgumentException("Invalid address type", nameof(addressType)); |
| | 16 | |
|
| 0 | 17 | | if (addressType == 5) |
| 0 | 18 | | address = new IdnMapping().GetAscii(address); |
| | 19 | |
|
| 0 | 20 | | AddressType = addressType; |
| 0 | 21 | | Address = address; |
| 0 | 22 | | Port = port; |
| | 23 | |
|
| 0 | 24 | | Length = addressType switch |
| 0 | 25 | | { |
| 0 | 26 | | 1 => 7, |
| 0 | 27 | | 2 => 19, |
| 0 | 28 | | 3 => throw new ArgumentException("Tor v2 addresses are deprecated", nameof(addressType)), |
| 0 | 29 | | 4 => 38, |
| 0 | 30 | | 5 when address.Length > 255 => throw new ArgumentException("Maximum lenght for address is 255", |
| 0 | 31 | | nameof(address)), |
| 0 | 32 | | 5 => 3 + address.Length, |
| 0 | 33 | | _ => throw new ArgumentException("Invalid address type", nameof(addressType)) |
| 0 | 34 | | }; |
| | 35 | |
|
| 0 | 36 | | Value = new byte[Length]; |
| 0 | 37 | | } |
| | 38 | | } |