Skip to content

Commit 98674ee

Browse files
authored
Fix IPAddress byte constructor (#217)
1 parent 6fa8bbd commit 98674ee

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

nanoFramework.System.Net/IPAddress.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class IPAddress
2121
/// This field is read-only.
2222
/// </summary>
2323
public static readonly IPAddress Any = new IPAddress(0x0000000000000000);
24-
24+
2525
/// <summary>
2626
/// Provides the IP loopback address. This field is read-only.
2727
/// </summary>
@@ -75,13 +75,23 @@ public IPAddress(long newAddress)
7575
/// <param name="address"></param>
7676
public IPAddress(byte[] address)
7777
{
78-
if (address[0] == (byte)AddressFamily.InterNetwork)
78+
if (address == null)
79+
{
80+
throw new ArgumentNullException();
81+
}
82+
83+
if (address.Length != IPv4AddressBytes && address.Length != IPv6AddressBytes)
84+
{
85+
// unsupported address family
86+
throw new NotSupportedException();
87+
}
88+
89+
if (address.Length == IPv4AddressBytes)
7990
{
8091
_family = AddressFamily.InterNetwork;
81-
// need to offset address by 4 (1st are family, 2nd are port
82-
Address = ((address[3 + 4] << 24 | address[2 + 4] << 16 | address[1 + 4] << 8 | address[0 + 4]) & 0x0FFFFFFFF);
92+
Address = ((address[3] << 24 | address[2] << 16 | address[1] << 8 | address[0]) & 0x0FFFFFFFF);
8393
}
84-
else if (address[0] == (byte)AddressFamily.InterNetworkV6)
94+
else
8595
{
8696
_family = AddressFamily.InterNetworkV6;
8797

@@ -90,11 +100,6 @@ public IPAddress(byte[] address)
90100
_numbers[i] = (ushort)(address[i * 2] * 256 + address[i * 2 + 1]);
91101
}
92102
}
93-
else
94-
{
95-
// unsupported address family
96-
throw new NotSupportedException();
97-
}
98103
}
99104

100105
/// <summary>
@@ -188,8 +193,8 @@ internal IPAddress Snapshot()
188193
case AddressFamily.InterNetwork:
189194
return new IPAddress(Address);
190195

191-
//case AddressFamily.InterNetworkV6:
192-
// return new IPAddress(m_Numbers, (uint)m_ScopeId);
196+
//case AddressFamily.InterNetworkV6:
197+
// return new IPAddress(m_Numbers, (uint)m_ScopeId);
193198
}
194199

195200
throw new NotSupportedException();

0 commit comments

Comments
 (0)