Skip to content

Commit

Permalink
Fixed IN6_ADDR.ToString and words property capturing and giving incor…
Browse files Browse the repository at this point in the history
…rect values (#388)
  • Loading branch information
dahall committed Apr 8, 2023
1 parent 44d9c2a commit 3663faf
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions PInvoke/Ws2_32/WinSock2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,32 +1398,28 @@ public ushort[] words
{
get
{
unsafe
{
var v6addr = new ushort[IN6_ADDR_SIZE / 2];
fixed (ushort* usp = &v6addr[0])
{
var ulp2 = (ulong*)usp;
ulp2[0] = lower;
ulp2[1] = upper;
}
return v6addr;
}
var v6addr = new ushort[IN6_ADDR_SIZE / 2];
var b = bytes;
for (int i = 0; i < v6addr.Length; i++)
v6addr[i] = (ushort)(b[i * 2] * 256 + b[i * 2 + 1]);
return v6addr;
}
set
{
unsafe
if (value == null)
{
if (value == null) value = new ushort[IN6_ADDR_SIZE / 2];
if (value.Length != IN6_ADDR_SIZE / 2)
throw new ArgumentException("UInt16 array must have 8 items.", nameof(value));
fixed (ushort* bp = &value[0])
{
var ulp = (ulong*)bp;
lower = ulp[0];
upper = ulp[1];
}
lower = upper = 0;
return;
}
if (value.Length != IN6_ADDR_SIZE / 2)
throw new ArgumentException("UInt16 array must have 8 items.", nameof(value));
byte[] b = new byte[IN6_ADDR_SIZE];
for (int i = 0, j = 0; i < value.Length; i++)
{
b[j++] = (byte)((value[i] >> 8) & 0xFF);
b[j++] = (byte)((value[i]) & 0xFF);
}
bytes = b;
}
}

Expand Down Expand Up @@ -1462,11 +1458,10 @@ public ushort[] words
/// <returns>A <see cref="string"/> that represents this instance.</returns>
public override string ToString()
{
const string numberFormat = "{0:x4}:{1:x4}:{2:x4}:{3:x4}:{4:x4}:{5:x4}:{6}.{7}.{8}.{9}";
var m_Numbers = words;
return string.Format(System.Globalization.CultureInfo.InvariantCulture, numberFormat,
m_Numbers[0], m_Numbers[1], m_Numbers[2], m_Numbers[3], m_Numbers[4], m_Numbers[5],
(m_Numbers[6] >> 8) & 0xFF, m_Numbers[6] & 0xFF, (m_Numbers[7] >> 8) & 0xFF, m_Numbers[7] & 0xFF);
System.Text.StringBuilder sb = new(64);
var p = InetNtopW(ADDRESS_FAMILY.AF_INET6, this, sb, sb.Capacity);
if (p.IsNull) throw WSAGetLastError().GetException();
return sb.ToString();
}

/// <summary>Determines whether the specified <paramref name="other"/> value is equal to this instance.</summary>
Expand Down

0 comments on commit 3663faf

Please sign in to comment.