| | 1 | | using System.Diagnostics.CodeAnalysis; |
| | 2 | | using System.Net; |
| | 3 | | using System.Text; |
| | 4 | | using NLightning.Domain.Protocol.Constants; |
| | 5 | | using NLightning.Domain.Protocol.Interfaces; |
| | 6 | | using NLightning.Domain.Protocol.Tlv; |
| | 7 | | using NLightning.Infrastructure.Converters; |
| | 8 | |
|
| | 9 | | namespace NLightning.Infrastructure.Protocol.Tlv.Converters; |
| | 10 | |
|
| | 11 | | public class RemoteAddressTlvConverter : ITlvConverter<RemoteAddressTlv> |
| | 12 | | { |
| | 13 | | public BaseTlv ConvertToBase(RemoteAddressTlv tlv) |
| | 14 | | { |
| 0 | 15 | | var tlvValue = new byte[tlv.Length]; |
| 0 | 16 | | tlvValue[0] = tlv.AddressType; |
| | 17 | |
|
| 0 | 18 | | switch (tlv.AddressType) |
| | 19 | | { |
| | 20 | | // IPv4 |
| | 21 | | case 1: |
| | 22 | | { |
| 0 | 23 | | var ipAddressBytes = IPAddress.Parse(tlv.Address).GetAddressBytes(); |
| 0 | 24 | | if (ipAddressBytes.Length != 4) |
| 0 | 25 | | throw new InvalidOperationException("Invalid IPv4 address"); |
| | 26 | |
|
| 0 | 27 | | ipAddressBytes.CopyTo(tlvValue, 1); |
| 0 | 28 | | EndianBitConverter.GetBytesBigEndian(tlv.Port).CopyTo(tlvValue, 5); |
| 0 | 29 | | break; |
| | 30 | | } |
| | 31 | | // IPv6 |
| | 32 | | case 2: |
| | 33 | | { |
| 0 | 34 | | var ipAddressBytes = IPAddress.Parse(tlv.Address).GetAddressBytes(); |
| 0 | 35 | | if (ipAddressBytes.Length != 16) |
| 0 | 36 | | throw new InvalidOperationException("Invalid IPv6 address"); |
| | 37 | |
|
| 0 | 38 | | ipAddressBytes.CopyTo(tlvValue, 1); |
| 0 | 39 | | EndianBitConverter.GetBytesBigEndian(tlv.Port).CopyTo(tlvValue, 17); |
| 0 | 40 | | break; |
| | 41 | | } |
| | 42 | | // Tor v3 |
| | 43 | | case 4: |
| | 44 | | { |
| 0 | 45 | | var torV3Bytes = Convert.FromHexString(tlv.Address); |
| 0 | 46 | | torV3Bytes.CopyTo(tlvValue, 1); |
| 0 | 47 | | EndianBitConverter.GetBytesBigEndian(tlv.Port).CopyTo(tlvValue, 36); |
| 0 | 48 | | break; |
| | 49 | | } |
| | 50 | | // Custom address type |
| | 51 | | case 5: |
| | 52 | | { |
| 0 | 53 | | var customAddressBytes = Encoding.ASCII.GetBytes(tlv.Address); |
| 0 | 54 | | customAddressBytes[1] = (byte)customAddressBytes.Length; |
| 0 | 55 | | customAddressBytes.CopyTo(tlvValue, 2); |
| 0 | 56 | | EndianBitConverter.GetBytesBigEndian(tlv.Port).CopyTo(tlvValue, customAddressBytes.Length + 2); |
| 0 | 57 | | break; |
| | 58 | | } |
| | 59 | | default: |
| 0 | 60 | | throw new InvalidOperationException("Invalid address type"); |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | return new BaseTlv(tlv.Type, tlvValue); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public RemoteAddressTlv ConvertFromBase(BaseTlv baseTlv) |
| | 67 | | { |
| 0 | 68 | | if (baseTlv.Type != TlvConstants.RemoteAddress) |
| 0 | 69 | | throw new InvalidCastException("Invalid TLV type"); |
| | 70 | |
|
| 0 | 71 | | var addressType = baseTlv.Value[0]; |
| | 72 | | string address; |
| | 73 | | ushort port; |
| | 74 | |
|
| | 75 | | switch (addressType) |
| | 76 | | { |
| | 77 | | // IPv4 |
| | 78 | | case 1: |
| 0 | 79 | | if (baseTlv.Length != 7) |
| 0 | 80 | | throw new InvalidCastException("Invalid length for IPv4 address"); |
| 0 | 81 | | address = new IPAddress(baseTlv.Value[1..5]).ToString(); |
| 0 | 82 | | port = EndianBitConverter.ToUInt16BigEndian(baseTlv.Value.AsSpan()[5..]); |
| 0 | 83 | | break; |
| | 84 | | // IPv6 |
| | 85 | | case 2: |
| 0 | 86 | | if (baseTlv.Length != 19) |
| 0 | 87 | | throw new InvalidCastException("Invalid length for IPv6 address"); |
| 0 | 88 | | address = new IPAddress(baseTlv.Value[1..17]).ToString(); |
| 0 | 89 | | port = EndianBitConverter.ToUInt16BigEndian(baseTlv.Value.AsSpan()[17..]); |
| 0 | 90 | | break; |
| | 91 | | // Tor v3 |
| | 92 | | case 4: |
| 0 | 93 | | if (baseTlv.Length != 38) |
| 0 | 94 | | throw new InvalidCastException("Invalid length for Tor v3 address"); |
| 0 | 95 | | address = Convert.ToHexString(baseTlv.Value[1..37]); |
| 0 | 96 | | port = EndianBitConverter.ToUInt16BigEndian(baseTlv.Value.AsSpan()[37..]); |
| 0 | 97 | | break; |
| | 98 | | // Custom address type |
| | 99 | | case 5: |
| 0 | 100 | | if (baseTlv.Length < 3 || baseTlv.Length > 258) |
| 0 | 101 | | throw new InvalidCastException("Invalid length for custom address"); |
| 0 | 102 | | var customAddressLength = baseTlv.Value[1]; |
| 0 | 103 | | if (customAddressLength + 3 != baseTlv.Length) |
| 0 | 104 | | throw new InvalidCastException("Custom address length mismatch"); |
| 0 | 105 | | address = Encoding.ASCII.GetString(baseTlv.Value[2..(2 + customAddressLength)]); |
| 0 | 106 | | port = EndianBitConverter.ToUInt16BigEndian(baseTlv.Value.AsSpan()[(2 + customAddressLength)..]); |
| 0 | 107 | | break; |
| | 108 | | default: |
| 0 | 109 | | throw new InvalidOperationException("Invalid address type"); |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | return new RemoteAddressTlv(addressType, address, port); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | [ExcludeFromCodeCoverage] |
| | 116 | | BaseTlv ITlvConverter.ConvertFromBase(BaseTlv tlv) |
| | 117 | | { |
| | 118 | | return ConvertFromBase(tlv); |
| | 119 | | } |
| | 120 | |
|
| | 121 | | [ExcludeFromCodeCoverage] |
| | 122 | | BaseTlv ITlvConverter.ConvertToBase(BaseTlv tlv) |
| | 123 | | { |
| | 124 | | return ConvertToBase(tlv as RemoteAddressTlv |
| | 125 | | ?? throw new InvalidCastException($"Error converting BaseTlv to {nameof(RemoteAddressTlv)}")); |
| | 126 | | } |
| | 127 | | } |