[TCPKeepalive] Fill correct TOS and TTL value
Fill correct TOS/TTL value by fetching them from kernel with
getsockopt.
bug: 123967966
Test: -build, flash, boot
-atest FrameworksNetTests
Change-Id: I75b1be51040b4a381163958b4cddd27dbb22bac1
This commit is contained in:
@@ -23,7 +23,10 @@ import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_ERROR;
|
|||||||
import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_INPUT;
|
import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_INPUT;
|
||||||
import static android.system.OsConstants.ENOPROTOOPT;
|
import static android.system.OsConstants.ENOPROTOOPT;
|
||||||
import static android.system.OsConstants.FIONREAD;
|
import static android.system.OsConstants.FIONREAD;
|
||||||
|
import static android.system.OsConstants.IPPROTO_IP;
|
||||||
import static android.system.OsConstants.IPPROTO_TCP;
|
import static android.system.OsConstants.IPPROTO_TCP;
|
||||||
|
import static android.system.OsConstants.IP_TOS;
|
||||||
|
import static android.system.OsConstants.IP_TTL;
|
||||||
import static android.system.OsConstants.TIOCOUTQ;
|
import static android.system.OsConstants.TIOCOUTQ;
|
||||||
|
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
@@ -193,6 +196,12 @@ public class TcpKeepaliveController {
|
|||||||
trw = NetworkUtils.getTcpRepairWindow(fd);
|
trw = NetworkUtils.getTcpRepairWindow(fd);
|
||||||
tcpDetails.rcvWnd = trw.rcvWnd;
|
tcpDetails.rcvWnd = trw.rcvWnd;
|
||||||
tcpDetails.rcvWndScale = trw.rcvWndScale;
|
tcpDetails.rcvWndScale = trw.rcvWndScale;
|
||||||
|
if (tcpDetails.srcAddress.length == 4 /* V4 address length */) {
|
||||||
|
// Query TOS.
|
||||||
|
tcpDetails.tos = Os.getsockoptInt(fd, IPPROTO_IP, IP_TOS);
|
||||||
|
// Query TTL.
|
||||||
|
tcpDetails.ttl = Os.getsockoptInt(fd, IPPROTO_IP, IP_TTL);
|
||||||
|
}
|
||||||
} catch (ErrnoException e) {
|
} catch (ErrnoException e) {
|
||||||
Log.e(TAG, "Exception reading TCP state from socket", e);
|
Log.e(TAG, "Exception reading TCP state from socket", e);
|
||||||
if (e.errno == ENOPROTOOPT) {
|
if (e.errno == ENOPROTOOPT) {
|
||||||
|
|||||||
@@ -48,6 +48,8 @@ public final class TcpKeepalivePacketDataTest {
|
|||||||
final int ack = 0x22222222;
|
final int ack = 0x22222222;
|
||||||
final int wnd = 8000;
|
final int wnd = 8000;
|
||||||
final int wndScale = 2;
|
final int wndScale = 2;
|
||||||
|
final int tos = 4;
|
||||||
|
final int ttl = 64;
|
||||||
TcpKeepalivePacketData resultData = null;
|
TcpKeepalivePacketData resultData = null;
|
||||||
final TcpKeepalivePacketDataParcelable testInfo = new TcpKeepalivePacketDataParcelable();
|
final TcpKeepalivePacketDataParcelable testInfo = new TcpKeepalivePacketDataParcelable();
|
||||||
testInfo.srcAddress = IPV4_KEEPALIVE_SRC_ADDR;
|
testInfo.srcAddress = IPV4_KEEPALIVE_SRC_ADDR;
|
||||||
@@ -58,6 +60,8 @@ public final class TcpKeepalivePacketDataTest {
|
|||||||
testInfo.ack = ack;
|
testInfo.ack = ack;
|
||||||
testInfo.rcvWnd = wnd;
|
testInfo.rcvWnd = wnd;
|
||||||
testInfo.rcvWndScale = wndScale;
|
testInfo.rcvWndScale = wndScale;
|
||||||
|
testInfo.tos = tos;
|
||||||
|
testInfo.ttl = ttl;
|
||||||
try {
|
try {
|
||||||
resultData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo);
|
resultData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo);
|
||||||
} catch (InvalidPacketException e) {
|
} catch (InvalidPacketException e) {
|
||||||
@@ -72,16 +76,21 @@ public final class TcpKeepalivePacketDataTest {
|
|||||||
assertEquals(testInfo.ack, resultData.tcpAck);
|
assertEquals(testInfo.ack, resultData.tcpAck);
|
||||||
assertEquals(testInfo.rcvWnd, resultData.tcpWnd);
|
assertEquals(testInfo.rcvWnd, resultData.tcpWnd);
|
||||||
assertEquals(testInfo.rcvWndScale, resultData.tcpWndScale);
|
assertEquals(testInfo.rcvWndScale, resultData.tcpWndScale);
|
||||||
|
assertEquals(testInfo.tos, resultData.ipTos);
|
||||||
|
assertEquals(testInfo.ttl, resultData.ipTtl);
|
||||||
|
|
||||||
TestUtils.assertParcelingIsLossless(resultData, TcpKeepalivePacketData.CREATOR);
|
TestUtils.assertParcelingIsLossless(resultData, TcpKeepalivePacketData.CREATOR);
|
||||||
|
|
||||||
final byte[] packet = resultData.getPacket();
|
final byte[] packet = resultData.getPacket();
|
||||||
// IP version and TOS.
|
// IP version and IHL
|
||||||
ByteBuffer buf = ByteBuffer.wrap(packet);
|
assertEquals(packet[0], 0x45);
|
||||||
assertEquals(buf.getShort(), 0x4500);
|
// TOS
|
||||||
|
assertEquals(packet[1], tos);
|
||||||
|
// TTL
|
||||||
|
assertEquals(packet[8], ttl);
|
||||||
// Source IP address.
|
// Source IP address.
|
||||||
byte[] ip = new byte[4];
|
byte[] ip = new byte[4];
|
||||||
buf = ByteBuffer.wrap(packet, 12, 4);
|
ByteBuffer buf = ByteBuffer.wrap(packet, 12, 4);
|
||||||
buf.get(ip);
|
buf.get(ip);
|
||||||
assertArrayEquals(ip, IPV4_KEEPALIVE_SRC_ADDR);
|
assertArrayEquals(ip, IPV4_KEEPALIVE_SRC_ADDR);
|
||||||
// Destination IP address.
|
// Destination IP address.
|
||||||
@@ -113,6 +122,8 @@ public final class TcpKeepalivePacketDataTest {
|
|||||||
final int ack = 0x22222222;
|
final int ack = 0x22222222;
|
||||||
final int wnd = 48_000;
|
final int wnd = 48_000;
|
||||||
final int wndScale = 2;
|
final int wndScale = 2;
|
||||||
|
final int tos = 4;
|
||||||
|
final int ttl = 64;
|
||||||
final TcpKeepalivePacketDataParcelable testInfo = new TcpKeepalivePacketDataParcelable();
|
final TcpKeepalivePacketDataParcelable testInfo = new TcpKeepalivePacketDataParcelable();
|
||||||
testInfo.srcAddress = IPV4_KEEPALIVE_SRC_ADDR;
|
testInfo.srcAddress = IPV4_KEEPALIVE_SRC_ADDR;
|
||||||
testInfo.srcPort = srcPort;
|
testInfo.srcPort = srcPort;
|
||||||
@@ -122,6 +133,8 @@ public final class TcpKeepalivePacketDataTest {
|
|||||||
testInfo.ack = ack;
|
testInfo.ack = ack;
|
||||||
testInfo.rcvWnd = wnd;
|
testInfo.rcvWnd = wnd;
|
||||||
testInfo.rcvWndScale = wndScale;
|
testInfo.rcvWndScale = wndScale;
|
||||||
|
testInfo.tos = tos;
|
||||||
|
testInfo.ttl = ttl;
|
||||||
TcpKeepalivePacketData testData = null;
|
TcpKeepalivePacketData testData = null;
|
||||||
TcpKeepalivePacketDataParcelable resultData = null;
|
TcpKeepalivePacketDataParcelable resultData = null;
|
||||||
testData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo);
|
testData = TcpKeepalivePacketData.tcpKeepalivePacket(testInfo);
|
||||||
@@ -134,5 +147,7 @@ public final class TcpKeepalivePacketDataTest {
|
|||||||
assertEquals(resultData.ack, ack);
|
assertEquals(resultData.ack, ack);
|
||||||
assertEquals(resultData.rcvWnd, wnd);
|
assertEquals(resultData.rcvWnd, wnd);
|
||||||
assertEquals(resultData.rcvWndScale, wndScale);
|
assertEquals(resultData.rcvWndScale, wndScale);
|
||||||
|
assertEquals(resultData.tos, tos);
|
||||||
|
assertEquals(resultData.ttl, ttl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user