-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetAddress.cs
202 lines (174 loc) · 3.89 KB
/
NetAddress.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Hosts
{
public enum NetAddressType { None, IPv4, IPv6 };
public class NetAddress
{
private string IP;
public NetAddressType Type { get; protected set; }
/// <summary>
/// Normalize IPv6 address to compact format
/// </summary>
/// <param name="ip">IPv6 string</param>
/// <returns>Normalized IPv6 string</returns>
protected string NormalizeIPv6(string ip)
{
var hex = ip.Split(':');
var dec = new uint[8];
var gap = false;
// Parse "head"
for (var i = 0; i < hex.Length; i++)
{
if (String.IsNullOrEmpty(hex[i]))
{
gap = true;
break;
}
dec[i] = Convert.ToUInt32(hex[i], 16);
}
// Parse "tail"
if (gap) for (var i = 0; i < hex.Length; i++)
{
if (String.IsNullOrEmpty(hex[hex.Length - i - 1]))
{
break;
}
dec[dec.Length - i - 1] = Convert.ToUInt32(hex[hex.Length - i - 1], 16);
}
// Find longest zeroes part
var max_offset = -1;
var max_length = 0;
var offset = -1;
var length = 0;
for (var i = 0; i < dec.Length; i++)
{
if (dec[i] == 0)
{
if (offset == -1)
{
offset = i;
length = 1;
}
else
{
length++;
}
}
else if (offset > -1)
{
if (length > max_length)
{
max_offset = offset;
max_length = length;
}
offset = -1;
length = 0;
}
}
if (length > max_length)
{
max_offset = offset;
max_length = length;
}
// Ignore one zero
if (max_length == 1)
{
max_length = 0;
max_offset = -1;
}
// Format normalized IPv6 address
ip = "";
for (int i = 0; i < dec.Length; i++)
{
if (i >= max_offset && i < (max_offset + max_length))
{
if (i == max_offset)
{
ip += "::";
}
continue;
}
if (ip.Length > 0 && !ip.EndsWith(":"))
{
ip += ":";
}
ip += dec[i].ToString("x");
}
return ip.ToLower();
}
public NetAddress(string ip)
{
if (ip == null) throw new ArgumentNullException();
try
{
var parsed = IPAddress.Parse(ip);
switch (parsed.AddressFamily)
{
case AddressFamily.InterNetwork:
Type = NetAddressType.IPv4;
IP = parsed.ToString();
break;
case AddressFamily.InterNetworkV6:
Type = NetAddressType.IPv6;
IP = NormalizeIPv6(ip);
break;
default: throw new Exception();
}
}
catch
{
throw new FormatException(String.Format("Invalid IP address '{0}'", IP));
}
}
public static NetAddress TryCreate(string ip)
{
try { return new NetAddress(ip); }
catch { return null; }
}
public static implicit operator NetAddress(string ip)
{
return new NetAddress(ip);
}
public static implicit operator string(NetAddress ip)
{
return ip.ToString();
}
public override string ToString()
{
return IP;
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public bool Equals(NetAddress ip)
{
if ((object)ip == null) return false;
return ip.IP == this.IP;
}
public bool Equals(string ip)
{
return Equals(NetAddress.TryCreate(ip));
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj is string) return Equals((string)obj);
if (obj is NetAddress) return Equals((NetAddress)obj);
return false;
}
public static bool operator ==(NetAddress na1, NetAddress na2)
{
return Object.ReferenceEquals(na1, na2) || na1.Equals(na2);
}
public static bool operator !=(NetAddress na1, NetAddress na2)
{
return !(Object.ReferenceEquals(na1, na2) || na1.Equals(na2));
}
}
}