-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
EndPointHelper.cs
303 lines (270 loc) · 10.1 KB
/
EndPointHelper.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Copyright 2013-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
namespace MongoDB.Driver.Core.Misc
{
/// <summary>
/// Represents helper methods for EndPoints.
/// </summary>
public static class EndPointHelper
{
// static fields
private static IEqualityComparer<EndPoint> __endPointEqualityComparer = new EndPointEqualityComparerImpl();
// static properties
/// <summary>
/// Gets an end point equality comparer.
/// </summary>
/// <value>
/// An end point equality comparer.
/// </value>
public static IEqualityComparer<EndPoint> EndPointEqualityComparer
{
get { return __endPointEqualityComparer; }
}
// static methods
/// <summary>
/// Determines whether a list of end points contains a specific end point.
/// </summary>
/// <param name="endPoints">The list of end points.</param>
/// <param name="endPoint">The specific end point to search for.</param>
/// <returns>True if the list of end points contains the specific end point.</returns>
public static bool Contains(IEnumerable<EndPoint> endPoints, EndPoint endPoint)
{
return endPoints.Contains(endPoint, __endPointEqualityComparer);
}
/// <summary>
/// Compares two end points.
/// </summary>
/// <param name="a">The first end point.</param>
/// <param name="b">The second end point.</param>
/// <returns>True if both end points are equal, or if both are null.</returns>
public static bool Equals(EndPoint a, EndPoint b)
{
return __endPointEqualityComparer.Equals(a, b);
}
/// <summary>
/// Creates an end point from object data saved during serialization.
/// </summary>
/// <param name="info">The object data.</param>
/// <returns>An end point.</returns>
public static EndPoint FromObjectData(List<object> info)
{
if (info == null)
{
return null;
}
var type = (string)info[0];
switch (type)
{
case "DnsEndPoint": return new DnsEndPoint((string)info[1], (int)info[2], (AddressFamily)(int)info[3]);
case "IPEndPoint": return new IPEndPoint((IPAddress)info[1], (int)info[2]);
default: throw new MongoInternalException("Unexpected EndPoint type.");
}
}
/// <summary>
/// Gets the object data required to serialize an end point.
/// </summary>
/// <param name="value">The end point.</param>
/// <returns>The object data.</returns>
public static List<object> GetObjectData(EndPoint value)
{
var dnsEndPoint = value as DnsEndPoint;
if (dnsEndPoint != null)
{
return new List<object>
{
"DnsEndPoint",
dnsEndPoint.Host,
dnsEndPoint.Port,
(int)dnsEndPoint.AddressFamily
};
}
var ipEndPoint = value as IPEndPoint;
if (ipEndPoint != null)
{
return new List<object>
{
"IPEndPoint",
ipEndPoint.Address,
ipEndPoint.Port
};
}
return null;
}
/// <summary>
/// Compares two sequences of end points.
/// </summary>
/// <param name="a">The first sequence of end points.</param>
/// <param name="b">The second sequence of end points.</param>
/// <returns>True if both sequences contain the same end points in the same order, or if both sequences are null.</returns>
public static bool SequenceEquals(IEnumerable<EndPoint> a, IEnumerable<EndPoint> b)
{
if (a == null && b == null)
{
return true;
}
if (a == null || b == null)
{
return false;
}
return a.SequenceEqual(b, __endPointEqualityComparer);
}
/// <summary>
/// Parses the string representation of an end point.
/// </summary>
/// <param name="value">The value to parse.</param>
/// <returns>An end point.</returns>
public static EndPoint Parse(string value)
{
Ensure.IsNotNull(value, nameof(value));
EndPoint endPoint;
if (!TryParse(value, out endPoint))
{
var message = string.Format("'{0}' is not a valid end point.", value);
throw new ArgumentException(message, "value");
}
return endPoint;
}
internal static (string Host, int Port) GetHostAndPort(this EndPoint endPoint) =>
endPoint switch
{
DnsEndPoint dnsEndPoint => (dnsEndPoint.Host, dnsEndPoint.Port),
IPEndPoint ipEndPoint => (ipEndPoint.Address.ToString(), ipEndPoint.Port),
_ => default
};
/// <summary>
/// Returns a <see cref="System.String" /> that represents the end point.
/// </summary>
/// <param name="endPoint">The end point.</param>
/// <returns>
/// A <see cref="System.String" /> that represents the end point.
/// </returns>
public static string ToString(EndPoint endPoint)
{
var dnsEndPoint = endPoint as DnsEndPoint;
if (dnsEndPoint != null)
{
return string.Format("{0}:{1}", dnsEndPoint.Host, dnsEndPoint.Port);
}
return endPoint.ToString();
}
/// <summary>
/// Tries to parse the string representation of an end point.
/// </summary>
/// <param name="value">The value to parse.</param>
/// <param name="result">The result.</param>
/// <returns>True if the string representation was parsed successfully.</returns>
public static bool TryParse(string value, out EndPoint result)
{
return TryParse(value, 27017, out result);
}
internal static bool TryParse(string value, int defaultPort, out EndPoint result)
{
result = null;
if (value != null)
{
value = value.ToLowerInvariant();
var match = Regex.Match(value, @"^(?<address>\[[^]]+\])(:(?<port>\d+))?$");
if (match.Success)
{
var addressString = match.Groups["address"].Value;
var portString = match.Groups["port"].Value;
var port = defaultPort;
if (portString.Length != 0 && !int.TryParse(portString, out port))
{
return false;
}
if (!IsValidPort(port))
{
return false;
}
IPAddress address;
if (IPAddress.TryParse(addressString, out address))
{
result = new IPEndPoint(address, port);
return true;
}
return false;
}
match = Regex.Match(value, @"^(?<host>[^:]+)(:(?<port>\d+))?$");
if (match.Success)
{
var host = match.Groups["host"].Value;
var portString = match.Groups["port"].Value;
var port = defaultPort;
if (portString.Length != 0 && !int.TryParse(portString, out port))
{
return false;
}
if (!IsValidPort(port))
{
return false;
}
IPAddress address;
if (IPAddress.TryParse(host, out address))
{
result = new IPEndPoint(address, port);
return true;
}
try
{
result = new DnsEndPoint(host, port);
return true;
}
catch (ArgumentException)
{
return false;
}
}
}
return false;
}
private static bool IsValidPort(int port)
{
return port > 0 && port <= ushort.MaxValue;
}
// nested classes
private class EndPointEqualityComparerImpl : IEqualityComparer<EndPoint>
{
public bool Equals(EndPoint x, EndPoint y)
{
if (x == null && y == null)
{
return true;
}
else if (x == null || y == null)
{
return false;
}
// mono has a bug in DnsEndPoint.Equals, so if the types aren't
// equal, it will throw a null reference exception.
if (x.GetType() != y.GetType())
{
return false;
}
return x.Equals(y);
}
public int GetHashCode(EndPoint obj)
{
return obj.GetHashCode();
}
}
}
}