summaryrefslogtreecommitdiff
path: root/Assembly_Firstpass/Steamworks/SteamIPAddress_t.cs
blob: 5bc8fc0e6127f408668516f776e6679911151f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace Steamworks;

[Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SteamIPAddress_t
{
	private long m_ip0;

	private long m_ip1;

	private ESteamIPType m_eType;

	public SteamIPAddress_t(IPAddress iPAddress)
	{
		byte[] addressBytes = iPAddress.GetAddressBytes();
		switch (iPAddress.AddressFamily)
		{
		case AddressFamily.InterNetwork:
			if (addressBytes.Length != 4)
			{
				throw new TypeInitializationException("SteamIPAddress_t: Unexpected byte length for Ipv4." + addressBytes.Length, null);
			}
			m_ip0 = (addressBytes[0] << 24) | (addressBytes[1] << 16) | (addressBytes[2] << 8) | addressBytes[3];
			m_ip1 = 0L;
			m_eType = ESteamIPType.k_ESteamIPTypeIPv4;
			break;
		case AddressFamily.InterNetworkV6:
			if (addressBytes.Length != 16)
			{
				throw new TypeInitializationException("SteamIPAddress_t: Unexpected byte length for Ipv6: " + addressBytes.Length, null);
			}
			m_ip0 = (addressBytes[1] << 24) | (addressBytes[0] << 16) | (addressBytes[3] << 8) | addressBytes[2] | (addressBytes[5] << 24) | (addressBytes[4] << 16) | (addressBytes[7] << 8) | addressBytes[6];
			m_ip1 = (addressBytes[9] << 24) | (addressBytes[8] << 16) | (addressBytes[11] << 8) | addressBytes[10] | (addressBytes[13] << 24) | (addressBytes[12] << 16) | (addressBytes[15] << 8) | addressBytes[14];
			m_eType = ESteamIPType.k_ESteamIPTypeIPv6;
			break;
		default:
			throw new TypeInitializationException("SteamIPAddress_t: Unexpected address family " + iPAddress.AddressFamily, null);
		}
	}

	public IPAddress ToIPAddress()
	{
		if (m_eType == ESteamIPType.k_ESteamIPTypeIPv4)
		{
			byte[] bytes = BitConverter.GetBytes(m_ip0);
			return new IPAddress(new byte[4]
			{
				bytes[3],
				bytes[2],
				bytes[1],
				bytes[0]
			});
		}
		byte[] array = new byte[16];
		BitConverter.GetBytes(m_ip0).CopyTo(array, 0);
		BitConverter.GetBytes(m_ip1).CopyTo(array, 8);
		return new IPAddress(array);
	}

	public override string ToString()
	{
		return ToIPAddress().ToString();
	}

	public ESteamIPType GetIPType()
	{
		return m_eType;
	}

	public bool IsSet()
	{
		if (m_ip0 == 0L)
		{
			return m_ip1 != 0;
		}
		return true;
	}
}