|
| 1 | +using System.Net; |
| 2 | +using OpenPort.Net.Models; |
| 3 | + |
| 4 | +namespace OpenPort.Net.Internal; |
| 5 | + |
| 6 | +internal static class NatPmpMessage |
| 7 | +{ |
| 8 | + public const byte Version = 0; |
| 9 | + public const byte ExternalAddressOpcode = 0; |
| 10 | + public const byte UdpMapOpcode = 1; |
| 11 | + public const byte TcpMapOpcode = 2; |
| 12 | + public const byte ResponseOffset = 128; |
| 13 | + |
| 14 | + public static byte[] CreateExternalAddressRequest() => [Version, ExternalAddressOpcode]; |
| 15 | + |
| 16 | + public static byte[] CreateMapRequest(PortProtocol protocol, int internalPort, int externalPort, uint lifetimeSeconds) |
| 17 | + { |
| 18 | + var request = new byte[12]; |
| 19 | + request[0] = Version; |
| 20 | + request[1] = ToMapOpcode(protocol); |
| 21 | + NetworkUtils.WriteUInt16BigEndian(request, 4, internalPort); |
| 22 | + NetworkUtils.WriteUInt16BigEndian(request, 6, externalPort); |
| 23 | + NetworkUtils.WriteUInt32BigEndian(request, 8, lifetimeSeconds); |
| 24 | + return request; |
| 25 | + } |
| 26 | + |
| 27 | + public static bool TryParseExternalAddressResponse( |
| 28 | + byte[] response, |
| 29 | + out ushort resultCode, |
| 30 | + out IPAddress? externalAddress) |
| 31 | + { |
| 32 | + resultCode = 0; |
| 33 | + externalAddress = null; |
| 34 | + |
| 35 | + if (response.Length < 8 || response[0] != Version || response[1] != ExternalAddressOpcode + ResponseOffset) |
| 36 | + { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + resultCode = NetworkUtils.ReadUInt16BigEndian(response, 2); |
| 41 | + if (resultCode != 0) |
| 42 | + { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + if (response.Length < 12) |
| 47 | + { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + externalAddress = new IPAddress(response.Skip(8).Take(4).ToArray()); |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + public static bool TryParseMapResponse( |
| 56 | + byte[] response, |
| 57 | + PortProtocol protocol, |
| 58 | + out NatPmpMapResponse mapResponse) |
| 59 | + { |
| 60 | + mapResponse = default; |
| 61 | + var opcode = ToMapOpcode(protocol); |
| 62 | + |
| 63 | + if (response.Length < 8 || response[0] != Version || response[1] != opcode + ResponseOffset) |
| 64 | + { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + var resultCode = NetworkUtils.ReadUInt16BigEndian(response, 2); |
| 69 | + if (resultCode != 0) |
| 70 | + { |
| 71 | + mapResponse = new NatPmpMapResponse(resultCode, 0, 0, 0); |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + if (response.Length < 16) |
| 76 | + { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + mapResponse = new NatPmpMapResponse( |
| 81 | + resultCode, |
| 82 | + NetworkUtils.ReadUInt16BigEndian(response, 8), |
| 83 | + NetworkUtils.ReadUInt16BigEndian(response, 10), |
| 84 | + NetworkUtils.ReadUInt32BigEndian(response, 12)); |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + public static OpenPortStatus MapResultCode(ushort code) => |
| 89 | + code switch |
| 90 | + { |
| 91 | + 0 => OpenPortStatus.Success, |
| 92 | + 1 => OpenPortStatus.NotSupported, |
| 93 | + 2 => OpenPortStatus.Unauthorized, |
| 94 | + 3 => OpenPortStatus.Failed, |
| 95 | + 4 => OpenPortStatus.NoResources, |
| 96 | + 5 => OpenPortStatus.NotSupported, |
| 97 | + _ => OpenPortStatus.Failed |
| 98 | + }; |
| 99 | + |
| 100 | + public static string GetResultName(ushort code) => |
| 101 | + code switch |
| 102 | + { |
| 103 | + 0 => "Success", |
| 104 | + 1 => "UnsupportedVersion", |
| 105 | + 2 => "NotAuthorized", |
| 106 | + 3 => "NetworkFailure", |
| 107 | + 4 => "OutOfResources", |
| 108 | + 5 => "UnsupportedOpcode", |
| 109 | + _ => "Unknown" |
| 110 | + }; |
| 111 | + |
| 112 | + private static byte ToMapOpcode(PortProtocol protocol) => |
| 113 | + protocol == PortProtocol.Udp ? UdpMapOpcode : TcpMapOpcode; |
| 114 | +} |
| 115 | + |
| 116 | +internal readonly struct NatPmpMapResponse |
| 117 | +{ |
| 118 | + public NatPmpMapResponse(ushort resultCode, int internalPort, int externalPort, uint lifetimeSeconds) |
| 119 | + { |
| 120 | + ResultCode = resultCode; |
| 121 | + InternalPort = internalPort; |
| 122 | + ExternalPort = externalPort; |
| 123 | + LifetimeSeconds = lifetimeSeconds; |
| 124 | + } |
| 125 | + |
| 126 | + public ushort ResultCode { get; } |
| 127 | + public int InternalPort { get; } |
| 128 | + public int ExternalPort { get; } |
| 129 | + public uint LifetimeSeconds { get; } |
| 130 | +} |
0 commit comments