-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
45 lines (36 loc) · 1.31 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace netnje
{
class Utils
{
/// <summary>
/// Converts a string containing either a host name or IPv4 address in dotted notation to a UInt32 for NJE usage
/// </summary>
/// <param name="ipstr"></param>
/// <returns>UInt32 representation of the supplied IP or hostname</returns>
public static UInt32 stringToNjeIP(string ipstr)
{
UInt32 result = 0x80000001 ; // default to localhost/127.0.0.1 in case we fail
if (ipstr.Count(f => f == '.') == 3)
{
IPAddress ipa = IPAddress.Parse(ipstr);
byte[] aBytes = ipa.GetAddressBytes();
if (BitConverter.IsLittleEndian)
Array.Reverse(aBytes);
result = BitConverter.ToUInt32(aBytes, 0);
return result;
}
IPHostEntry hostEntry = Dns.GetHostEntry(ipstr);
if (hostEntry.AddressList.Length > 0)
{
byte[] addressBytes = hostEntry.AddressList[0].GetAddressBytes();
result = BitConverter.ToUInt32(addressBytes, 0);
}
return result;
}
}
}