Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big-endian fixes: networking stack #48398

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/libraries/Native/Unix/System.Native/pal_networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,14 @@ int32_t SystemNative_GetSockOpt(
{
if (socketOptionName == SocketOptionName_SO_IP_DONTFRAGMENT)
{
*optionValue = *optionValue == IP_PMTUDISC_DO ? 1 : 0;
if (optLen >= (socklen_t)sizeof(int))
{
*(int*)optionValue = *(int*)optionValue == IP_PMTUDISC_DO ? 1 : 0;
}
else
{
*optionValue = *optionValue == IP_PMTUDISC_DO ? 1 : 0;
}
}
}
#endif
Expand Down Expand Up @@ -2139,7 +2146,14 @@ SystemNative_SetSockOpt(intptr_t socket, int32_t socketOptionLevel, int32_t sock
{
if (socketOptionName == SocketOptionName_SO_IP_DONTFRAGMENT)
{
*optionValue = *optionValue != 0 ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
if ((socklen_t)optionLen >= (socklen_t)sizeof(int))
{
*(int*)optionValue = *(int*)optionValue != 0 ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
}
else
{
*optionValue = *optionValue != 0 ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
}
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ private static IPAddress ParseIPv6HexString(string hexAddress, bool isNetworkOrd
{
Debug.Assert(hexAddress.Length == 32);
byte[] addressBytes = new byte[16];
if (isNetworkOrder)
if (isNetworkOrder || !BitConverter.IsLittleEndian)
scalablecory marked this conversation as resolved.
Show resolved Hide resolved
{
for (int i = 0; i < 16; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,44 @@ public class AddressParsingTests : FileCleanupTestBase
[Fact]
public void HexIPAddressParsing()
{
Assert.Equal(IPAddress.Parse("10.105.128.1"), StringParsingHelpers.ParseHexIPAddress("0180690A"));
Assert.Equal(IPAddress.Parse("103.69.35.1"), StringParsingHelpers.ParseHexIPAddress("01234567"));
Assert.Equal(IPAddress.Parse("152.186.220.254"), StringParsingHelpers.ParseHexIPAddress("FEDCBA98"));
if (BitConverter.IsLittleEndian)
{
Assert.Equal(IPAddress.Parse("10.105.128.1"), StringParsingHelpers.ParseHexIPAddress("0180690A"));
Assert.Equal(IPAddress.Parse("103.69.35.1"), StringParsingHelpers.ParseHexIPAddress("01234567"));
Assert.Equal(IPAddress.Parse("152.186.220.254"), StringParsingHelpers.ParseHexIPAddress("FEDCBA98"));

Assert.Equal(IPAddress.Parse("::"), StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"));
Assert.Equal(IPAddress.Parse("::1"), StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"));
Assert.Equal(IPAddress.Parse("fec0::1"), StringParsingHelpers.ParseHexIPAddress("0000C0FE000000000000000001000000"));
Assert.Equal(IPAddress.Parse("fe80::222:222"), StringParsingHelpers.ParseHexIPAddress("000080FE000000000000000022022202"));
Assert.Equal(IPAddress.Parse("fe80::215:5dff:fe00:402"), StringParsingHelpers.ParseHexIPAddress("000080FE00000000FF5D1502020400FE"));
Assert.Equal(IPAddress.Parse("::"), StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"));
Assert.Equal(IPAddress.Parse("::1"), StringParsingHelpers.ParseHexIPAddress("00000000000000000000000001000000"));
Assert.Equal(IPAddress.Parse("fec0::1"), StringParsingHelpers.ParseHexIPAddress("0000C0FE000000000000000001000000"));
Assert.Equal(IPAddress.Parse("fe80::222:222"), StringParsingHelpers.ParseHexIPAddress("000080FE000000000000000022022202"));
Assert.Equal(IPAddress.Parse("fe80::215:5dff:fe00:402"), StringParsingHelpers.ParseHexIPAddress("000080FE00000000FF5D1502020400FE"));
}
else
{
Assert.Equal(IPAddress.Parse("10.105.128.1"), StringParsingHelpers.ParseHexIPAddress("0A698001"));
Assert.Equal(IPAddress.Parse("103.69.35.1"), StringParsingHelpers.ParseHexIPAddress("67452301"));
Assert.Equal(IPAddress.Parse("152.186.220.254"), StringParsingHelpers.ParseHexIPAddress("98BADCFE"));

Assert.Equal(IPAddress.Parse("::"), StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000000"));
Assert.Equal(IPAddress.Parse("::1"), StringParsingHelpers.ParseHexIPAddress("00000000000000000000000000000001"));
Assert.Equal(IPAddress.Parse("fec0::1"), StringParsingHelpers.ParseHexIPAddress("FEC00000000000000000000000000001"));
Assert.Equal(IPAddress.Parse("fe80::222:222"), StringParsingHelpers.ParseHexIPAddress("FE800000000000000000000002220222"));
Assert.Equal(IPAddress.Parse("fe80::215:5dff:fe00:402"), StringParsingHelpers.ParseHexIPAddress("FE8000000000000002155DFFFE000402"));
}
}

[Fact]
public void IPv4GatewayAddressParsing()
{
string fileName = GetTestFilePath();
FileUtil.NormalizeLineEndings("NetworkFiles/route", fileName);
if (BitConverter.IsLittleEndian)
{
FileUtil.NormalizeLineEndings("NetworkFiles/route", fileName);
}
else
{
FileUtil.NormalizeLineEndings("NetworkFiles/route-be", fileName);
}
List<GatewayIPAddressInformation> gatewayAddresses = new List<GatewayIPAddressInformation>();
StringParsingHelpers.ParseIPv4GatewayAddressesFromRouteFile(gatewayAddresses, File.ReadAllLines(fileName), "wlan0");
Assert.Equal(3, gatewayAddresses.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,48 @@ public void ActiveTcpConnectionsParsing()
{
string tcpFile = GetTestFilePath();
string tcp6File = GetTestFilePath();
FileUtil.NormalizeLineEndings("NetworkFiles/tcp", tcpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/tcp6", tcp6File);
if (BitConverter.IsLittleEndian)
{
FileUtil.NormalizeLineEndings("NetworkFiles/tcp", tcpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/tcp6", tcp6File);
}
else
{
FileUtil.NormalizeLineEndings("NetworkFiles/tcp-be", tcpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/tcp6-be", tcp6File);
}

TcpConnectionInformation[] infos = StringParsingHelpers.ParseActiveTcpConnectionsFromFiles(tcpFile, tcp6File);
Assert.Equal(11, infos.Length);
ValidateInfo(infos[0], new IPEndPoint(0xFFFFFF01L, 0x01BD), new IPEndPoint(0L, 0), TcpState.Established);
ValidateInfo(infos[1], new IPEndPoint(0x12345678L, 0x008B), new IPEndPoint(0L, 0), TcpState.SynSent);
ValidateInfo(infos[2], new IPEndPoint(0x0101007FL, 0x0035), new IPEndPoint(0L, 0), TcpState.SynReceived);
ValidateInfo(infos[3], new IPEndPoint(0x0100007FL, 0x0277), new IPEndPoint(0L, 0), TcpState.FinWait1);
ValidateInfo(infos[4], new IPEndPoint(0x0100007FL, 0x0277), new IPEndPoint(0x00000001L, 0), TcpState.SynReceived);

ValidateInfo(
infos[0],
new IPEndPoint(IPAddress.Parse("1.255.255.255"), 0x01BD),
new IPEndPoint(0L, 0),
TcpState.Established);

ValidateInfo(
infos[1],
new IPEndPoint(IPAddress.Parse("120.86.52.18"), 0x008B),
new IPEndPoint(0L, 0),
TcpState.SynSent);

ValidateInfo(
infos[2],
new IPEndPoint(IPAddress.Parse("127.0.1.1"), 0x0035),
new IPEndPoint(0L, 0),
TcpState.SynReceived);

ValidateInfo(
infos[3],
new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0x0277),
new IPEndPoint(0L, 0),
TcpState.FinWait1);

ValidateInfo(infos[4],
new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0x0277),
new IPEndPoint(IPAddress.Parse("1.0.0.0"), 0),
TcpState.SynReceived);

ValidateInfo(
infos[5],
Expand Down Expand Up @@ -87,8 +119,16 @@ public void TcpListenersParsing()
{
string tcpFile = GetTestFilePath();
string tcp6File = GetTestFilePath();
FileUtil.NormalizeLineEndings("NetworkFiles/tcp", tcpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/tcp6", tcp6File);
if (BitConverter.IsLittleEndian)
{
FileUtil.NormalizeLineEndings("NetworkFiles/tcp", tcpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/tcp6", tcp6File);
}
else
{
FileUtil.NormalizeLineEndings("NetworkFiles/tcp-be", tcpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/tcp6-be", tcp6File);
}

IPEndPoint[] listeners = StringParsingHelpers.ParseActiveTcpListenersFromFiles(tcpFile, tcp6File);
// There is only one socket in Listening state
Expand All @@ -102,24 +142,32 @@ public void UdpListenersParsing()
{
string udpFile = GetTestFilePath();
string udp6File = GetTestFilePath();
FileUtil.NormalizeLineEndings("NetworkFiles/udp", udpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/udp6", udp6File);
if (BitConverter.IsLittleEndian)
{
FileUtil.NormalizeLineEndings("NetworkFiles/udp", udpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/udp6", udp6File);
}
else
{
FileUtil.NormalizeLineEndings("NetworkFiles/udp-be", udpFile);
FileUtil.NormalizeLineEndings("NetworkFiles/udp6-be", udp6File);
}

IPEndPoint[] listeners = StringParsingHelpers.ParseActiveUdpListenersFromFiles(udpFile, udp6File);
Assert.Equal(17, listeners.Length);

Assert.Equal(listeners[0], new IPEndPoint(0x00000000, 0x8E15));
Assert.Equal(listeners[1], new IPEndPoint(0x00000000, 0x14E9));
Assert.Equal(listeners[2], new IPEndPoint(0x00000000, 0xB50F));
Assert.Equal(listeners[3], new IPEndPoint(0x0101007F, 0x0035));
Assert.Equal(listeners[4], new IPEndPoint(0x00000000, 0x0044));
Assert.Equal(listeners[5], new IPEndPoint(0xFF83690A, 0x0089));
Assert.Equal(listeners[6], new IPEndPoint(0x3B80690A, 0x0089));
Assert.Equal(listeners[7], new IPEndPoint(0x00000000, 0x0089));
Assert.Equal(listeners[8], new IPEndPoint(0xFF83690A, 0x008A));
Assert.Equal(listeners[9], new IPEndPoint(0x3B80690A, 0x008A));
Assert.Equal(listeners[10], new IPEndPoint(0x00000000, 0x008A));
Assert.Equal(listeners[11], new IPEndPoint(0x00000000, 0x0277));
Assert.Equal(listeners[0], new IPEndPoint(0, 0x8E15));
Assert.Equal(listeners[1], new IPEndPoint(0, 0x14E9));
Assert.Equal(listeners[2], new IPEndPoint(0, 0xB50F));
Assert.Equal(listeners[3], new IPEndPoint(IPAddress.Parse("127.0.1.1"), 0x0035));
Assert.Equal(listeners[4], new IPEndPoint(0, 0x0044));
Assert.Equal(listeners[5], new IPEndPoint(IPAddress.Parse("10.105.131.255"), 0x0089));
Assert.Equal(listeners[6], new IPEndPoint(IPAddress.Parse("10.105.128.59"), 0x0089));
Assert.Equal(listeners[7], new IPEndPoint(0, 0x0089));
Assert.Equal(listeners[8], new IPEndPoint(IPAddress.Parse("10.105.131.255"), 0x008A));
Assert.Equal(listeners[9], new IPEndPoint(IPAddress.Parse("10.105.128.59"), 0x008A));
Assert.Equal(listeners[10], new IPEndPoint(0, 0x008A));
Assert.Equal(listeners[11], new IPEndPoint(0, 0x0277));

Assert.Equal(listeners[12], new IPEndPoint(IPAddress.Parse("::"), 0x14E9));
Assert.Equal(listeners[13], new IPEndPoint(IPAddress.Parse("::"), 0x96D3));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
wlan0 00000000 0A698001 0003 0 0 400 00000000 0 0 0
wlan0 0A698000 67452301 0001 0 0 400 FFFFFC00 0 0 0
wlan0 A9FE0000 98BADCFE 0001 0 0 1000 FFFF0000 0 0 0
eth0 00000000 11111111 0001 0 0 1000 FFFF0000 0 0 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 01FFFFFF:01BD 00000000:0000 01 00000000:00000000 00:00000000 00000000 0 0 23053 1 0000000000000000 100 0 0 10 0
1: 78563412:008B 00000000:0000 02 00000000:00000000 00:00000000 00000000 0 0 23054 1 0000000000000000 100 0 0 10 0
2: 7F000101:0035 00000000:0000 03 00000000:00000000 00:00000000 00000000 0 0 17216 1 0000000000000000 100 0 0 10 0
3: 7F000001:0277 00000000:0000 04 00000000:00000000 00:00000000 00000000 0 0 16089 1 0000000000000000 100 0 0 10 0
4: 7F000001:0277 01000000:0000 0C 00000000:00000000 00:00000000 00000000 0 0 16089 1 0000000000000000 100 0 0 10 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 00000000000000000000000000000000:01BD 00000000000000000000000000000000:0000 05 00000000:00000000 00:00000000 00000000 0 0 23051 1 0000000000000000 100 0 0 10 0
1: 00000000000000000000000000000000:008B 00000000000000000000000000000000:0000 06 00000000:00000000 00:00000000 00000000 0 0 23052 1 0000000000000000 100 0 0 10 0
2: 00000000000000000000000000000001:0277 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 16088 1 0000000000000000 100 0 0 10 0
3: 00000000000000000000000000000001:A696 00000000000000000000000000000001:0277 08 00000000:00000001 00:00000000 00000000 0 0 17035 1 0000000000000000 20 4 24 10 -1
4: 00000000000000000000000000000001:A69B 00000000000000000000000000000001:0277 09 00000000:00000001 00:00000000 00000000 1000 0 23248 1 0000000000000000 20 4 16 10 -1
5: 00000000000000000000000000000001:A697 00000000000000000000000000000001:0277 0A 00000000:00000001 00:00000000 00000000 0 0 12176 1 0000000000000000 20 4 24 10 -1
6: FEC0000000000000AA64000000000001:007B 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000 109 0 59682 2 0000000000000000 100 0 0 10 0
7: 00000000000000000000000000000001:007C FE8000000000000002155DFFFE000402:007B 01 00000000:00000000 00:00000000 00000000 0 0 24746 2 0000000000000000 100 0 0 10 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops
572: 00000000:8E15 00000000:0000 07 00000000:00000000 00:00000000 00000000 107 0 11207 2 0000000000000000 0
2320: 00000000:14E9 00000000:0000 07 00000000:00000000 00:00000000 00000000 107 0 11205 2 0000000000000000 0
2358: 00000000:B50F 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 21943 2 0000000000000000 0
5212: 7F000101:0035 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 17215 2 0000000000000000 0
5227: 00000000:0044 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 17211 2 0000000000000000 0
5296: 0A6983FF:0089 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 20035 2 0000000000000000 0
5296: 0A69803B:0089 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 20034 2 0000000000000000 0
5296: 00000000:0089 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 20031 2 0000000000000000 0
5297: 0A6983FF:008A 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 20037 2 0000000000000000 0
5297: 0A69803B:008A 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 20036 2 0000000000000000 0
5297: 00000000:008A 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 20032 2 0000000000000000 0
5790: 00000000:0277 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 16165 2 0000000000000000 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sl local_address remote_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops
2320: 00000000000000000000000000000000:14E9 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 107 0 11206 2 0000000000000000 0
2810: 00000000000000000000000000000000:96D3 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 21944 2 0000000000000000 0
8063: 00000000000000000000000000000000:8B58 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 107 0 11208 2 0000000000000000 0
9960: FEC0000000000000AA64000000000001:007B 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 109 0 59682 2 0000000000000000 0
9961: FE8000000000000002155DFFFE000402:007B 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 24746 2 0000000000000000 0
Loading