[vm/io] add endian.host to RawSocketOption.fromInt

This is the reland of 127489 with fix on Mac. IPv6 and IPv4 behave differently in setsockopt(). IPv6 requires an index instead of an address.
https://dart-review.googlesource.com/c/sdk/+/127489

Bug: https://github.com/dart-lang/sdk/issues/39691
Change-Id: Idc0449e88be47663851f2de90cdbbf3f13466221
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128367
Reviewed-by: Siva Annamalai <asiva@google.com>
Commit-Queue: Zichang Guo <zichangguo@google.com>
This commit is contained in:
Zichang Guo 2019-12-30 09:34:18 +00:00 committed by commit-bot@chromium.org
parent f1084cdf0b
commit d3b40e59c6
3 changed files with 23 additions and 8 deletions

View file

@ -452,7 +452,7 @@ class RawSocketOption {
}
final Uint8List list = Uint8List(4);
final buffer = ByteData.view(list.buffer, list.offsetInBytes);
buffer.setInt32(0, value);
buffer.setInt32(0, value, Endian.host);
return RawSocketOption(level, option, list);
}

View file

@ -451,7 +451,7 @@ class RawSocketOption {
}
final Uint8List list = Uint8List(4);
final buffer = ByteData.view(list.buffer, list.offsetInBytes);
buffer.setInt32(0, value);
buffer.setInt32(0, value, Endian.host);
return RawSocketOption(level, option, list);
}

View file

@ -121,14 +121,23 @@ testDatagramSocketMulticastIf() {
asyncStart();
final socket = await RawDatagramSocket.bind(address, 0);
RawSocketOption option;
int idx;
if (address.type == InternetAddressType.IPv4) {
option = RawSocketOption(RawSocketOption.levelIPv4,
RawSocketOption.IPv4MulticastInterface, address.rawAddress);
} else {
// We'll need a Uint8List(4) for this option, since it will be an 4 byte
// word value sent into get/setsockopt.
option = RawSocketOption(RawSocketOption.levelIPv6,
RawSocketOption.IPv6MulticastInterface, Uint8List(4));
if (!NetworkInterface.listSupported) {
asyncEnd();
return;
}
var interface = await NetworkInterface.list();
if (interface.length == 0) {
asyncEnd();
return;
}
idx = interface[0].index;
option = RawSocketOption.fromInt(RawSocketOption.levelIPv6,
RawSocketOption.IPv6MulticastInterface, idx);
}
socket.setRawOption(option);
@ -137,12 +146,18 @@ testDatagramSocketMulticastIf() {
if (address.type == InternetAddressType.IPv4) {
Expect.listEquals(getResult, address.rawAddress);
} else {
Expect.listEquals(getResult, [0, 0, 0, 0]);
// RawSocketOption.fromInt() will create a Uint8List(4).
Expect.equals(
getResult.buffer.asByteData().getUint32(0, Endian.host), idx);
}
asyncSuccess(socket);
asyncEnd();
}
test(InternetAddress.loopbackIPv4);
test(InternetAddress.anyIPv4);
test(InternetAddress.loopbackIPv6);
test(InternetAddress.anyIPv6);
}
testBroadcast() {