diff options
author | Remi NGUYEN VAN <reminv@google.com> | 2019-11-21 23:15:29 -0800 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-11-21 23:15:29 -0800 |
commit | 8a72e3d3b401b9330e26ff4b1ac3a587a77b0316 (patch) | |
tree | 1c081cba99352edbecf57f7b3ee4ef29f3c5f3bc /services/net | |
parent | dca2d45630db4905e7b353fff30f8b7dad76a10b (diff) | |
parent | 27253f29ad997d8a1896e94e06cdc11cefe42dc4 (diff) |
Merge "Move shared packages to NetworkStack project"
am: 27253f29ad
Change-Id: I17068de9e9b4388b8ec025ce3e1dc722e471ccbb
Diffstat (limited to 'services/net')
27 files changed, 2 insertions, 3433 deletions
diff --git a/services/net/Android.bp b/services/net/Android.bp index c56ecd6e19e7..45430eab9bf4 100644 --- a/services/net/Android.bp +++ b/services/net/Android.bp @@ -1,38 +1,26 @@ java_library_static { name: "services.net", srcs: [ + ":net-module-utils-srcs", ":tethering-servicesnet-srcs", "java/**/*.java", ], static_libs: [ "dnsresolver_aidl_interface-V2-java", "netd_aidl_interface-unstable-java", + "netlink-client", "networkstack-client", "tethering-client", ], } filegroup { - name: "services-networkstack-shared-srcs", - srcs: [ - "java/android/net/ip/InterfaceController.java", // TODO: move to NetworkStack with tethering - "java/android/net/util/InterfaceParams.java", // TODO: move to NetworkStack with IpServer - "java/android/net/shared/*.java", - "java/android/net/netlink/*.java", - ], -} - -filegroup { name: "services-tethering-shared-srcs", srcs: [ ":framework-annotations", "java/android/net/ConnectivityModuleConnector.java", "java/android/net/NetworkStackClient.java", - "java/android/net/ip/InterfaceController.java", - "java/android/net/netlink/*.java", - "java/android/net/util/InterfaceParams.java", "java/android/net/util/NetdService.java", "java/android/net/util/NetworkConstants.java", - "java/android/net/util/SharedLog.java" ], } diff --git a/services/net/java/android/net/ip/InterfaceController.java b/services/net/java/android/net/ip/InterfaceController.java deleted file mode 100644 index 970bc9cf667b..000000000000 --- a/services/net/java/android/net/ip/InterfaceController.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.ip; - -import android.net.INetd; -import android.net.InterfaceConfigurationParcel; -import android.net.LinkAddress; -import android.net.util.SharedLog; -import android.os.RemoteException; -import android.os.ServiceSpecificException; -import android.system.OsConstants; - -import java.net.Inet4Address; -import java.net.InetAddress; - - -/** - * Encapsulates the multiple IP configuration operations performed on an interface. - * - * TODO: refactor/eliminate the redundant ways to set and clear addresses. - * - * @hide - */ -public class InterfaceController { - private final static boolean DBG = false; - - private final String mIfName; - private final INetd mNetd; - private final SharedLog mLog; - - public InterfaceController(String ifname, INetd netd, SharedLog log) { - mIfName = ifname; - mNetd = netd; - mLog = log; - } - - private boolean setInterfaceAddress(LinkAddress addr) { - final InterfaceConfigurationParcel ifConfig = new InterfaceConfigurationParcel(); - ifConfig.ifName = mIfName; - ifConfig.ipv4Addr = addr.getAddress().getHostAddress(); - ifConfig.prefixLength = addr.getPrefixLength(); - ifConfig.hwAddr = ""; - ifConfig.flags = new String[0]; - try { - mNetd.interfaceSetCfg(ifConfig); - } catch (RemoteException | ServiceSpecificException e) { - logError("Setting IPv4 address to %s/%d failed: %s", - ifConfig.ipv4Addr, ifConfig.prefixLength, e); - return false; - } - return true; - } - - /** - * Set the IPv4 address of the interface. - */ - public boolean setIPv4Address(LinkAddress address) { - if (!(address.getAddress() instanceof Inet4Address)) { - return false; - } - return setInterfaceAddress(address); - } - - /** - * Clear the IPv4Address of the interface. - */ - public boolean clearIPv4Address() { - return setInterfaceAddress(new LinkAddress("0.0.0.0/0")); - } - - private boolean setEnableIPv6(boolean enabled) { - try { - mNetd.interfaceSetEnableIPv6(mIfName, enabled); - } catch (RemoteException | ServiceSpecificException e) { - logError("%s IPv6 failed: %s", (enabled ? "enabling" : "disabling"), e); - return false; - } - return true; - } - - /** - * Enable IPv6 on the interface. - */ - public boolean enableIPv6() { - return setEnableIPv6(true); - } - - /** - * Disable IPv6 on the interface. - */ - public boolean disableIPv6() { - return setEnableIPv6(false); - } - - /** - * Enable or disable IPv6 privacy extensions on the interface. - * @param enabled Whether the extensions should be enabled. - */ - public boolean setIPv6PrivacyExtensions(boolean enabled) { - try { - mNetd.interfaceSetIPv6PrivacyExtensions(mIfName, enabled); - } catch (RemoteException | ServiceSpecificException e) { - logError("error %s IPv6 privacy extensions: %s", - (enabled ? "enabling" : "disabling"), e); - return false; - } - return true; - } - - /** - * Set IPv6 address generation mode on the interface. - * - * <p>IPv6 should be disabled before changing the mode. - */ - public boolean setIPv6AddrGenModeIfSupported(int mode) { - try { - mNetd.setIPv6AddrGenMode(mIfName, mode); - } catch (RemoteException e) { - logError("Unable to set IPv6 addrgen mode: %s", e); - return false; - } catch (ServiceSpecificException e) { - if (e.errorCode != OsConstants.EOPNOTSUPP) { - logError("Unable to set IPv6 addrgen mode: %s", e); - return false; - } - } - return true; - } - - /** - * Add an address to the interface. - */ - public boolean addAddress(LinkAddress addr) { - return addAddress(addr.getAddress(), addr.getPrefixLength()); - } - - /** - * Add an address to the interface. - */ - public boolean addAddress(InetAddress ip, int prefixLen) { - try { - mNetd.interfaceAddAddress(mIfName, ip.getHostAddress(), prefixLen); - } catch (ServiceSpecificException | RemoteException e) { - logError("failed to add %s/%d: %s", ip, prefixLen, e); - return false; - } - return true; - } - - /** - * Remove an address from the interface. - */ - public boolean removeAddress(InetAddress ip, int prefixLen) { - try { - mNetd.interfaceDelAddress(mIfName, ip.getHostAddress(), prefixLen); - } catch (ServiceSpecificException | RemoteException e) { - logError("failed to remove %s/%d: %s", ip, prefixLen, e); - return false; - } - return true; - } - - /** - * Remove all addresses from the interface. - */ - public boolean clearAllAddresses() { - try { - mNetd.interfaceClearAddrs(mIfName); - } catch (Exception e) { - logError("Failed to clear addresses: %s", e); - return false; - } - return true; - } - - private void logError(String fmt, Object... args) { - mLog.e(String.format(fmt, args)); - } -} diff --git a/services/net/java/android/net/netlink/ConntrackMessage.java b/services/net/java/android/net/netlink/ConntrackMessage.java deleted file mode 100644 index 6978739886c3..000000000000 --- a/services/net/java/android/net/netlink/ConntrackMessage.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import static android.net.netlink.StructNlMsgHdr.NLM_F_ACK; -import static android.net.netlink.StructNlMsgHdr.NLM_F_REPLACE; -import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST; - -import static java.nio.ByteOrder.BIG_ENDIAN; - -import android.system.OsConstants; - -import java.net.Inet4Address; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - - -/** - * A NetlinkMessage subclass for netlink conntrack messages. - * - * see also: <linux_src>/include/uapi/linux/netfilter/nfnetlink_conntrack.h - * - * @hide - */ -public class ConntrackMessage extends NetlinkMessage { - public static final int STRUCT_SIZE = StructNlMsgHdr.STRUCT_SIZE + StructNfGenMsg.STRUCT_SIZE; - - public static final short NFNL_SUBSYS_CTNETLINK = 1; - public static final short IPCTNL_MSG_CT_NEW = 0; - - // enum ctattr_type - public static final short CTA_TUPLE_ORIG = 1; - public static final short CTA_TUPLE_REPLY = 2; - public static final short CTA_TIMEOUT = 7; - - // enum ctattr_tuple - public static final short CTA_TUPLE_IP = 1; - public static final short CTA_TUPLE_PROTO = 2; - - // enum ctattr_ip - public static final short CTA_IP_V4_SRC = 1; - public static final short CTA_IP_V4_DST = 2; - - // enum ctattr_l4proto - public static final short CTA_PROTO_NUM = 1; - public static final short CTA_PROTO_SRC_PORT = 2; - public static final short CTA_PROTO_DST_PORT = 3; - - public static byte[] newIPv4TimeoutUpdateRequest( - int proto, Inet4Address src, int sport, Inet4Address dst, int dport, int timeoutSec) { - // *** STYLE WARNING *** - // - // Code below this point uses extra block indentation to highlight the - // packing of nested tuple netlink attribute types. - final StructNlAttr ctaTupleOrig = new StructNlAttr(CTA_TUPLE_ORIG, - new StructNlAttr(CTA_TUPLE_IP, - new StructNlAttr(CTA_IP_V4_SRC, src), - new StructNlAttr(CTA_IP_V4_DST, dst)), - new StructNlAttr(CTA_TUPLE_PROTO, - new StructNlAttr(CTA_PROTO_NUM, (byte) proto), - new StructNlAttr(CTA_PROTO_SRC_PORT, (short) sport, BIG_ENDIAN), - new StructNlAttr(CTA_PROTO_DST_PORT, (short) dport, BIG_ENDIAN))); - - final StructNlAttr ctaTimeout = new StructNlAttr(CTA_TIMEOUT, timeoutSec, BIG_ENDIAN); - - final int payloadLength = ctaTupleOrig.getAlignedLength() + ctaTimeout.getAlignedLength(); - final byte[] bytes = new byte[STRUCT_SIZE + payloadLength]; - final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - byteBuffer.order(ByteOrder.nativeOrder()); - - final ConntrackMessage ctmsg = new ConntrackMessage(); - ctmsg.mHeader.nlmsg_len = bytes.length; - ctmsg.mHeader.nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_NEW; - ctmsg.mHeader.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE; - ctmsg.mHeader.nlmsg_seq = 1; - ctmsg.pack(byteBuffer); - - ctaTupleOrig.pack(byteBuffer); - ctaTimeout.pack(byteBuffer); - - return bytes; - } - - protected StructNfGenMsg mNfGenMsg; - - private ConntrackMessage() { - super(new StructNlMsgHdr()); - mNfGenMsg = new StructNfGenMsg((byte) OsConstants.AF_INET); - } - - public void pack(ByteBuffer byteBuffer) { - mHeader.pack(byteBuffer); - mNfGenMsg.pack(byteBuffer); - } -} diff --git a/services/net/java/android/net/netlink/InetDiagMessage.java b/services/net/java/android/net/netlink/InetDiagMessage.java deleted file mode 100644 index ca07630d3e63..000000000000 --- a/services/net/java/android/net/netlink/InetDiagMessage.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import static android.net.netlink.NetlinkConstants.SOCK_DIAG_BY_FAMILY; -import static android.net.netlink.NetlinkSocket.DEFAULT_RECV_BUFSIZE; -import static android.net.netlink.StructNlMsgHdr.NLM_F_DUMP; -import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST; -import static android.os.Process.INVALID_UID; -import static android.system.OsConstants.AF_INET; -import static android.system.OsConstants.AF_INET6; -import static android.system.OsConstants.IPPROTO_UDP; -import static android.system.OsConstants.NETLINK_INET_DIAG; - -import android.annotation.Nullable; -import android.net.util.SocketUtils; -import android.system.ErrnoException; -import android.util.Log; - -import java.io.FileDescriptor; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.net.Inet4Address; -import java.net.Inet6Address; -import java.net.InetSocketAddress; -import java.net.SocketException; -import java.net.UnknownHostException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -/** - * A NetlinkMessage subclass for netlink inet_diag messages. - * - * see also: <linux_src>/include/uapi/linux/inet_diag.h - * - * @hide - */ -public class InetDiagMessage extends NetlinkMessage { - public static final String TAG = "InetDiagMessage"; - private static final int TIMEOUT_MS = 500; - - public static byte[] InetDiagReqV2(int protocol, InetSocketAddress local, - InetSocketAddress remote, int family, short flags) { - return InetDiagReqV2(protocol, local, remote, family, flags, 0 /* pad */, - 0 /* idiagExt */, StructInetDiagReqV2.INET_DIAG_REQ_V2_ALL_STATES); - } - - /** - * Construct an inet_diag_req_v2 message. This method will throw {@code NullPointerException} - * if local and remote are not both null or both non-null. - * - * @param protocol the request protocol type. This should be set to one of IPPROTO_TCP, - * IPPROTO_UDP, or IPPROTO_UDPLITE. - * @param local local socket address of the target socket. This will be packed into a - * {@Code StructInetDiagSockId}. Request to diagnose for all sockets if both of - * local or remote address is null. - * @param remote remote socket address of the target socket. This will be packed into a - * {@Code StructInetDiagSockId}. Request to diagnose for all sockets if both of - * local or remote address is null. - * @param family the ip family of the request message. This should be set to either AF_INET or - * AF_INET6 for IPv4 or IPv6 sockets respectively. - * @param flags message flags. See <linux_src>/include/uapi/linux/netlink.h. - * @param pad for raw socket protocol specification. - * @param idiagExt a set of flags defining what kind of extended information to report. - * @param state a bit mask that defines a filter of socket states. - * - * @return bytes array representation of the message - **/ - public static byte[] InetDiagReqV2(int protocol, @Nullable InetSocketAddress local, - @Nullable InetSocketAddress remote, int family, short flags, int pad, int idiagExt, - int state) throws NullPointerException { - final byte[] bytes = new byte[StructNlMsgHdr.STRUCT_SIZE + StructInetDiagReqV2.STRUCT_SIZE]; - final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - byteBuffer.order(ByteOrder.nativeOrder()); - - final StructNlMsgHdr nlMsgHdr = new StructNlMsgHdr(); - nlMsgHdr.nlmsg_len = bytes.length; - nlMsgHdr.nlmsg_type = SOCK_DIAG_BY_FAMILY; - nlMsgHdr.nlmsg_flags = flags; - nlMsgHdr.pack(byteBuffer); - final StructInetDiagReqV2 inetDiagReqV2 = - new StructInetDiagReqV2(protocol, local, remote, family, pad, idiagExt, state); - - inetDiagReqV2.pack(byteBuffer); - return bytes; - } - - public StructInetDiagMsg mStructInetDiagMsg; - - private InetDiagMessage(StructNlMsgHdr header) { - super(header); - mStructInetDiagMsg = new StructInetDiagMsg(); - } - - public static InetDiagMessage parse(StructNlMsgHdr header, ByteBuffer byteBuffer) { - final InetDiagMessage msg = new InetDiagMessage(header); - msg.mStructInetDiagMsg = StructInetDiagMsg.parse(byteBuffer); - return msg; - } - - private static int lookupUidByFamily(int protocol, InetSocketAddress local, - InetSocketAddress remote, int family, short flags, - FileDescriptor fd) - throws ErrnoException, InterruptedIOException { - byte[] msg = InetDiagReqV2(protocol, local, remote, family, flags); - NetlinkSocket.sendMessage(fd, msg, 0, msg.length, TIMEOUT_MS); - ByteBuffer response = NetlinkSocket.recvMessage(fd, DEFAULT_RECV_BUFSIZE, TIMEOUT_MS); - - final NetlinkMessage nlMsg = NetlinkMessage.parse(response); - final StructNlMsgHdr hdr = nlMsg.getHeader(); - if (hdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) { - return INVALID_UID; - } - if (nlMsg instanceof InetDiagMessage) { - return ((InetDiagMessage) nlMsg).mStructInetDiagMsg.idiag_uid; - } - return INVALID_UID; - } - - private static final int FAMILY[] = {AF_INET6, AF_INET}; - - private static int lookupUid(int protocol, InetSocketAddress local, - InetSocketAddress remote, FileDescriptor fd) - throws ErrnoException, InterruptedIOException { - int uid; - - for (int family : FAMILY) { - /** - * For exact match lookup, swap local and remote for UDP lookups due to kernel - * bug which will not be fixed. See aosp/755889 and - * https://www.mail-archive.com/netdev@vger.kernel.org/msg248638.html - */ - if (protocol == IPPROTO_UDP) { - uid = lookupUidByFamily(protocol, remote, local, family, NLM_F_REQUEST, fd); - } else { - uid = lookupUidByFamily(protocol, local, remote, family, NLM_F_REQUEST, fd); - } - if (uid != INVALID_UID) { - return uid; - } - } - - /** - * For UDP it's possible for a socket to send packets to arbitrary destinations, even if the - * socket is not connected (and even if the socket is connected to a different destination). - * If we want this API to work for such packets, then on miss we need to do a second lookup - * with only the local address and port filled in. - * Always use flags == NLM_F_REQUEST | NLM_F_DUMP for wildcard. - */ - if (protocol == IPPROTO_UDP) { - try { - InetSocketAddress wildcard = new InetSocketAddress( - Inet6Address.getByName("::"), 0); - uid = lookupUidByFamily(protocol, local, wildcard, AF_INET6, - (short) (NLM_F_REQUEST | NLM_F_DUMP), fd); - if (uid != INVALID_UID) { - return uid; - } - wildcard = new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), 0); - uid = lookupUidByFamily(protocol, local, wildcard, AF_INET, - (short) (NLM_F_REQUEST | NLM_F_DUMP), fd); - if (uid != INVALID_UID) { - return uid; - } - } catch (UnknownHostException e) { - Log.e(TAG, e.toString()); - } - } - return INVALID_UID; - } - - /** - * Use an inet_diag socket to look up the UID associated with the input local and remote - * address/port and protocol of a connection. - */ - public static int getConnectionOwnerUid(int protocol, InetSocketAddress local, - InetSocketAddress remote) { - int uid = INVALID_UID; - FileDescriptor fd = null; - try { - fd = NetlinkSocket.forProto(NETLINK_INET_DIAG); - NetlinkSocket.connectToKernel(fd); - uid = lookupUid(protocol, local, remote, fd); - } catch (ErrnoException | SocketException | IllegalArgumentException - | InterruptedIOException e) { - Log.e(TAG, e.toString()); - } finally { - if (fd != null) { - try { - SocketUtils.closeSocket(fd); - } catch (IOException e) { - Log.e(TAG, e.toString()); - } - } - } - return uid; - } - - @Override - public String toString() { - return "InetDiagMessage{ " - + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString()) + "}, " - + "inet_diag_msg{" - + (mStructInetDiagMsg == null ? "" : mStructInetDiagMsg.toString()) + "} " - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/NetlinkConstants.java b/services/net/java/android/net/netlink/NetlinkConstants.java deleted file mode 100644 index fc1551c87b6b..000000000000 --- a/services/net/java/android/net/netlink/NetlinkConstants.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.system.OsConstants; -import com.android.internal.util.HexDump; - -import java.nio.ByteBuffer; - - -/** - * Various constants and static helper methods for netlink communications. - * - * Values taken from: - * - * <linux_src>/include/uapi/linux/netlink.h - * <linux_src>/include/uapi/linux/rtnetlink.h - * - * @hide - */ -public class NetlinkConstants { - private NetlinkConstants() {} - - public static final int NLA_ALIGNTO = 4; - - public static final int alignedLengthOf(short length) { - final int intLength = (int) length & 0xffff; - return alignedLengthOf(intLength); - } - - public static final int alignedLengthOf(int length) { - if (length <= 0) { return 0; } - return (((length + NLA_ALIGNTO - 1) / NLA_ALIGNTO) * NLA_ALIGNTO); - } - - public static String stringForAddressFamily(int family) { - if (family == OsConstants.AF_INET) { return "AF_INET"; } - if (family == OsConstants.AF_INET6) { return "AF_INET6"; } - if (family == OsConstants.AF_NETLINK) { return "AF_NETLINK"; } - return String.valueOf(family); - } - - public static String stringForProtocol(int protocol) { - if (protocol == OsConstants.IPPROTO_TCP) { return "IPPROTO_TCP"; } - if (protocol == OsConstants.IPPROTO_UDP) { return "IPPROTO_UDP"; } - return String.valueOf(protocol); - } - - public static String hexify(byte[] bytes) { - if (bytes == null) { return "(null)"; } - return HexDump.toHexString(bytes); - } - - public static String hexify(ByteBuffer buffer) { - if (buffer == null) { return "(null)"; } - return HexDump.toHexString( - buffer.array(), buffer.position(), buffer.remaining()); - } - - // Known values for struct nlmsghdr nlm_type. - public static final short NLMSG_NOOP = 1; // Nothing - public static final short NLMSG_ERROR = 2; // Error - public static final short NLMSG_DONE = 3; // End of a dump - public static final short NLMSG_OVERRUN = 4; // Data lost - public static final short NLMSG_MAX_RESERVED = 15; // Max reserved value - - public static final short RTM_NEWLINK = 16; - public static final short RTM_DELLINK = 17; - public static final short RTM_GETLINK = 18; - public static final short RTM_SETLINK = 19; - public static final short RTM_NEWADDR = 20; - public static final short RTM_DELADDR = 21; - public static final short RTM_GETADDR = 22; - public static final short RTM_NEWROUTE = 24; - public static final short RTM_DELROUTE = 25; - public static final short RTM_GETROUTE = 26; - public static final short RTM_NEWNEIGH = 28; - public static final short RTM_DELNEIGH = 29; - public static final short RTM_GETNEIGH = 30; - public static final short RTM_NEWRULE = 32; - public static final short RTM_DELRULE = 33; - public static final short RTM_GETRULE = 34; - public static final short RTM_NEWNDUSEROPT = 68; - - /* see <linux_src>/include/uapi/linux/sock_diag.h */ - public static final short SOCK_DIAG_BY_FAMILY = 20; - - public static String stringForNlMsgType(short nlm_type) { - switch (nlm_type) { - case NLMSG_NOOP: return "NLMSG_NOOP"; - case NLMSG_ERROR: return "NLMSG_ERROR"; - case NLMSG_DONE: return "NLMSG_DONE"; - case NLMSG_OVERRUN: return "NLMSG_OVERRUN"; - case RTM_NEWLINK: return "RTM_NEWLINK"; - case RTM_DELLINK: return "RTM_DELLINK"; - case RTM_GETLINK: return "RTM_GETLINK"; - case RTM_SETLINK: return "RTM_SETLINK"; - case RTM_NEWADDR: return "RTM_NEWADDR"; - case RTM_DELADDR: return "RTM_DELADDR"; - case RTM_GETADDR: return "RTM_GETADDR"; - case RTM_NEWROUTE: return "RTM_NEWROUTE"; - case RTM_DELROUTE: return "RTM_DELROUTE"; - case RTM_GETROUTE: return "RTM_GETROUTE"; - case RTM_NEWNEIGH: return "RTM_NEWNEIGH"; - case RTM_DELNEIGH: return "RTM_DELNEIGH"; - case RTM_GETNEIGH: return "RTM_GETNEIGH"; - case RTM_NEWRULE: return "RTM_NEWRULE"; - case RTM_DELRULE: return "RTM_DELRULE"; - case RTM_GETRULE: return "RTM_GETRULE"; - case RTM_NEWNDUSEROPT: return "RTM_NEWNDUSEROPT"; - default: - return "unknown RTM type: " + String.valueOf(nlm_type); - } - } -} diff --git a/services/net/java/android/net/netlink/NetlinkErrorMessage.java b/services/net/java/android/net/netlink/NetlinkErrorMessage.java deleted file mode 100644 index e2755740453a..000000000000 --- a/services/net/java/android/net/netlink/NetlinkErrorMessage.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.net.netlink.StructNlMsgHdr; -import android.net.netlink.NetlinkMessage; - -import java.nio.ByteBuffer; - - -/** - * A NetlinkMessage subclass for netlink error messages. - * - * @hide - */ -public class NetlinkErrorMessage extends NetlinkMessage { - - public static NetlinkErrorMessage parse(StructNlMsgHdr header, ByteBuffer byteBuffer) { - final NetlinkErrorMessage errorMsg = new NetlinkErrorMessage(header); - - errorMsg.mNlMsgErr = StructNlMsgErr.parse(byteBuffer); - if (errorMsg.mNlMsgErr == null) { - return null; - } - - return errorMsg; - } - - private StructNlMsgErr mNlMsgErr; - - NetlinkErrorMessage(StructNlMsgHdr header) { - super(header); - mNlMsgErr = null; - } - - public StructNlMsgErr getNlMsgError() { - return mNlMsgErr; - } - - @Override - public String toString() { - return "NetlinkErrorMessage{ " - + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString()) + "}, " - + "nlmsgerr{" + (mNlMsgErr == null ? "" : mNlMsgErr.toString()) + "} " - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/NetlinkMessage.java b/services/net/java/android/net/netlink/NetlinkMessage.java deleted file mode 100644 index a325db800813..000000000000 --- a/services/net/java/android/net/netlink/NetlinkMessage.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.net.netlink.NetlinkConstants; -import android.net.netlink.NetlinkErrorMessage; -import android.net.netlink.RtNetlinkNeighborMessage; -import android.net.netlink.StructNlAttr; -import android.net.netlink.StructNlMsgHdr; -import android.util.Log; - -import java.nio.ByteBuffer; - - -/** - * NetlinkMessage base class for other, more specific netlink message types. - * - * Classes that extend NetlinkMessage should: - * - implement a public static parse(StructNlMsgHdr, ByteBuffer) method - * - returning either null (parse errors) or a new object of the subclass - * type (cast-able to NetlinkMessage) - * - * NetlinkMessage.parse() should be updated to know which nlmsg_type values - * correspond with which message subclasses. - * - * @hide - */ -public class NetlinkMessage { - private final static String TAG = "NetlinkMessage"; - - public static NetlinkMessage parse(ByteBuffer byteBuffer) { - final int startPosition = (byteBuffer != null) ? byteBuffer.position() : -1; - final StructNlMsgHdr nlmsghdr = StructNlMsgHdr.parse(byteBuffer); - if (nlmsghdr == null) { - return null; - } - - int payloadLength = NetlinkConstants.alignedLengthOf(nlmsghdr.nlmsg_len); - payloadLength -= StructNlMsgHdr.STRUCT_SIZE; - if (payloadLength < 0 || payloadLength > byteBuffer.remaining()) { - // Malformed message or runt buffer. Pretend the buffer was consumed. - byteBuffer.position(byteBuffer.limit()); - return null; - } - - switch (nlmsghdr.nlmsg_type) { - //case NetlinkConstants.NLMSG_NOOP: - case NetlinkConstants.NLMSG_ERROR: - return (NetlinkMessage) NetlinkErrorMessage.parse(nlmsghdr, byteBuffer); - case NetlinkConstants.NLMSG_DONE: - byteBuffer.position(byteBuffer.position() + payloadLength); - return new NetlinkMessage(nlmsghdr); - //case NetlinkConstants.NLMSG_OVERRUN: - case NetlinkConstants.RTM_NEWNEIGH: - case NetlinkConstants.RTM_DELNEIGH: - case NetlinkConstants.RTM_GETNEIGH: - return (NetlinkMessage) RtNetlinkNeighborMessage.parse(nlmsghdr, byteBuffer); - case NetlinkConstants.SOCK_DIAG_BY_FAMILY: - return (NetlinkMessage) InetDiagMessage.parse(nlmsghdr, byteBuffer); - default: - if (nlmsghdr.nlmsg_type <= NetlinkConstants.NLMSG_MAX_RESERVED) { - // Netlink control message. Just parse the header for now, - // pretending the whole message was consumed. - byteBuffer.position(byteBuffer.position() + payloadLength); - return new NetlinkMessage(nlmsghdr); - } - return null; - } - } - - protected StructNlMsgHdr mHeader; - - public NetlinkMessage(StructNlMsgHdr nlmsghdr) { - mHeader = nlmsghdr; - } - - public StructNlMsgHdr getHeader() { - return mHeader; - } - - @Override - public String toString() { - return "NetlinkMessage{" + (mHeader == null ? "" : mHeader.toString()) + "}"; - } -} diff --git a/services/net/java/android/net/netlink/NetlinkSocket.java b/services/net/java/android/net/netlink/NetlinkSocket.java deleted file mode 100644 index 7311fc537205..000000000000 --- a/services/net/java/android/net/netlink/NetlinkSocket.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import static android.net.util.SocketUtils.makeNetlinkSocketAddress; -import static android.system.OsConstants.AF_NETLINK; -import static android.system.OsConstants.EIO; -import static android.system.OsConstants.EPROTO; -import static android.system.OsConstants.ETIMEDOUT; -import static android.system.OsConstants.SOCK_DGRAM; -import static android.system.OsConstants.SOL_SOCKET; -import static android.system.OsConstants.SO_RCVBUF; -import static android.system.OsConstants.SO_RCVTIMEO; -import static android.system.OsConstants.SO_SNDTIMEO; - -import android.net.util.SocketUtils; -import android.system.ErrnoException; -import android.system.Os; -import android.system.StructTimeval; -import android.util.Log; - -import java.io.FileDescriptor; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.net.SocketException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - - -/** - * NetlinkSocket - * - * A small static class to assist with AF_NETLINK socket operations. - * - * @hide - */ -public class NetlinkSocket { - private static final String TAG = "NetlinkSocket"; - - public static final int DEFAULT_RECV_BUFSIZE = 8 * 1024; - public static final int SOCKET_RECV_BUFSIZE = 64 * 1024; - - public static void sendOneShotKernelMessage(int nlProto, byte[] msg) throws ErrnoException { - final String errPrefix = "Error in NetlinkSocket.sendOneShotKernelMessage"; - final long IO_TIMEOUT = 300L; - - final FileDescriptor fd = forProto(nlProto); - - try { - connectToKernel(fd); - sendMessage(fd, msg, 0, msg.length, IO_TIMEOUT); - final ByteBuffer bytes = recvMessage(fd, DEFAULT_RECV_BUFSIZE, IO_TIMEOUT); - // recvMessage() guaranteed to not return null if it did not throw. - final NetlinkMessage response = NetlinkMessage.parse(bytes); - if (response != null && response instanceof NetlinkErrorMessage && - (((NetlinkErrorMessage) response).getNlMsgError() != null)) { - final int errno = ((NetlinkErrorMessage) response).getNlMsgError().error; - if (errno != 0) { - // TODO: consider ignoring EINVAL (-22), which appears to be - // normal when probing a neighbor for which the kernel does - // not already have / no longer has a link layer address. - Log.e(TAG, errPrefix + ", errmsg=" + response.toString()); - // Note: convert kernel errnos (negative) into userspace errnos (positive). - throw new ErrnoException(response.toString(), Math.abs(errno)); - } - } else { - final String errmsg; - if (response == null) { - bytes.position(0); - errmsg = "raw bytes: " + NetlinkConstants.hexify(bytes); - } else { - errmsg = response.toString(); - } - Log.e(TAG, errPrefix + ", errmsg=" + errmsg); - throw new ErrnoException(errmsg, EPROTO); - } - } catch (InterruptedIOException e) { - Log.e(TAG, errPrefix, e); - throw new ErrnoException(errPrefix, ETIMEDOUT, e); - } catch (SocketException e) { - Log.e(TAG, errPrefix, e); - throw new ErrnoException(errPrefix, EIO, e); - } finally { - try { - SocketUtils.closeSocket(fd); - } catch (IOException e) { - // Nothing we can do here - } - } - } - - public static FileDescriptor forProto(int nlProto) throws ErrnoException { - final FileDescriptor fd = Os.socket(AF_NETLINK, SOCK_DGRAM, nlProto); - Os.setsockoptInt(fd, SOL_SOCKET, SO_RCVBUF, SOCKET_RECV_BUFSIZE); - return fd; - } - - public static void connectToKernel(FileDescriptor fd) throws ErrnoException, SocketException { - Os.connect(fd, makeNetlinkSocketAddress(0, 0)); - } - - private static void checkTimeout(long timeoutMs) { - if (timeoutMs < 0) { - throw new IllegalArgumentException("Negative timeouts not permitted"); - } - } - - /** - * Wait up to |timeoutMs| (or until underlying socket error) for a - * netlink message of at most |bufsize| size. - * - * Multi-threaded calls with different timeouts will cause unexpected results. - */ - public static ByteBuffer recvMessage(FileDescriptor fd, int bufsize, long timeoutMs) - throws ErrnoException, IllegalArgumentException, InterruptedIOException { - checkTimeout(timeoutMs); - - Os.setsockoptTimeval(fd, SOL_SOCKET, SO_RCVTIMEO, StructTimeval.fromMillis(timeoutMs)); - - ByteBuffer byteBuffer = ByteBuffer.allocate(bufsize); - int length = Os.read(fd, byteBuffer); - if (length == bufsize) { - Log.w(TAG, "maximum read"); - } - byteBuffer.position(0); - byteBuffer.limit(length); - byteBuffer.order(ByteOrder.nativeOrder()); - return byteBuffer; - } - - /** - * Send a message to a peer to which this socket has previously connected, - * waiting at most |timeoutMs| milliseconds for the send to complete. - * - * Multi-threaded calls with different timeouts will cause unexpected results. - */ - public static int sendMessage( - FileDescriptor fd, byte[] bytes, int offset, int count, long timeoutMs) - throws ErrnoException, IllegalArgumentException, InterruptedIOException { - checkTimeout(timeoutMs); - Os.setsockoptTimeval(fd, SOL_SOCKET, SO_SNDTIMEO, StructTimeval.fromMillis(timeoutMs)); - return Os.write(fd, bytes, offset, count); - } -} diff --git a/services/net/java/android/net/netlink/RtNetlinkNeighborMessage.java b/services/net/java/android/net/netlink/RtNetlinkNeighborMessage.java deleted file mode 100644 index e784fbb5e0dc..000000000000 --- a/services/net/java/android/net/netlink/RtNetlinkNeighborMessage.java +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import static android.net.netlink.StructNlMsgHdr.NLM_F_ACK; -import static android.net.netlink.StructNlMsgHdr.NLM_F_DUMP; -import static android.net.netlink.StructNlMsgHdr.NLM_F_REPLACE; -import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST; - -import android.net.netlink.StructNdaCacheInfo; -import android.net.netlink.StructNdMsg; -import android.net.netlink.StructNlAttr; -import android.net.netlink.StructNlMsgHdr; -import android.net.netlink.NetlinkMessage; -import android.system.OsConstants; -import android.util.Log; - -import java.net.InetAddress; -import java.net.Inet6Address; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - - -/** - * A NetlinkMessage subclass for rtnetlink neighbor messages. - * - * see also: <linux_src>/include/uapi/linux/neighbour.h - * - * @hide - */ -public class RtNetlinkNeighborMessage extends NetlinkMessage { - public static final short NDA_UNSPEC = 0; - public static final short NDA_DST = 1; - public static final short NDA_LLADDR = 2; - public static final short NDA_CACHEINFO = 3; - public static final short NDA_PROBES = 4; - public static final short NDA_VLAN = 5; - public static final short NDA_PORT = 6; - public static final short NDA_VNI = 7; - public static final short NDA_IFINDEX = 8; - public static final short NDA_MASTER = 9; - - private static StructNlAttr findNextAttrOfType(short attrType, ByteBuffer byteBuffer) { - while (byteBuffer != null && byteBuffer.remaining() > 0) { - final StructNlAttr nlAttr = StructNlAttr.peek(byteBuffer); - if (nlAttr == null) { - break; - } - if (nlAttr.nla_type == attrType) { - return StructNlAttr.parse(byteBuffer); - } - if (byteBuffer.remaining() < nlAttr.getAlignedLength()) { - break; - } - byteBuffer.position(byteBuffer.position() + nlAttr.getAlignedLength()); - } - return null; - } - - public static RtNetlinkNeighborMessage parse(StructNlMsgHdr header, ByteBuffer byteBuffer) { - final RtNetlinkNeighborMessage neighMsg = new RtNetlinkNeighborMessage(header); - - neighMsg.mNdmsg = StructNdMsg.parse(byteBuffer); - if (neighMsg.mNdmsg == null) { - return null; - } - - // Some of these are message-type dependent, and not always present. - final int baseOffset = byteBuffer.position(); - StructNlAttr nlAttr = findNextAttrOfType(NDA_DST, byteBuffer); - if (nlAttr != null) { - neighMsg.mDestination = nlAttr.getValueAsInetAddress(); - } - - byteBuffer.position(baseOffset); - nlAttr = findNextAttrOfType(NDA_LLADDR, byteBuffer); - if (nlAttr != null) { - neighMsg.mLinkLayerAddr = nlAttr.nla_value; - } - - byteBuffer.position(baseOffset); - nlAttr = findNextAttrOfType(NDA_PROBES, byteBuffer); - if (nlAttr != null) { - neighMsg.mNumProbes = nlAttr.getValueAsInt(0); - } - - byteBuffer.position(baseOffset); - nlAttr = findNextAttrOfType(NDA_CACHEINFO, byteBuffer); - if (nlAttr != null) { - neighMsg.mCacheInfo = StructNdaCacheInfo.parse(nlAttr.getValueAsByteBuffer()); - } - - final int kMinConsumed = StructNlMsgHdr.STRUCT_SIZE + StructNdMsg.STRUCT_SIZE; - final int kAdditionalSpace = NetlinkConstants.alignedLengthOf( - neighMsg.mHeader.nlmsg_len - kMinConsumed); - if (byteBuffer.remaining() < kAdditionalSpace) { - byteBuffer.position(byteBuffer.limit()); - } else { - byteBuffer.position(baseOffset + kAdditionalSpace); - } - - return neighMsg; - } - - /** - * A convenience method to create an RTM_GETNEIGH request message. - */ - public static byte[] newGetNeighborsRequest(int seqNo) { - final int length = StructNlMsgHdr.STRUCT_SIZE + StructNdMsg.STRUCT_SIZE; - final byte[] bytes = new byte[length]; - final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - byteBuffer.order(ByteOrder.nativeOrder()); - - final StructNlMsgHdr nlmsghdr = new StructNlMsgHdr(); - nlmsghdr.nlmsg_len = length; - nlmsghdr.nlmsg_type = NetlinkConstants.RTM_GETNEIGH; - nlmsghdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; - nlmsghdr.nlmsg_seq = seqNo; - nlmsghdr.pack(byteBuffer); - - final StructNdMsg ndmsg = new StructNdMsg(); - ndmsg.pack(byteBuffer); - - return bytes; - } - - /** - * A convenience method to create an RTM_NEWNEIGH message, to modify - * the kernel's state information for a specific neighbor. - */ - public static byte[] newNewNeighborMessage( - int seqNo, InetAddress ip, short nudState, int ifIndex, byte[] llAddr) { - final StructNlMsgHdr nlmsghdr = new StructNlMsgHdr(); - nlmsghdr.nlmsg_type = NetlinkConstants.RTM_NEWNEIGH; - nlmsghdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE; - nlmsghdr.nlmsg_seq = seqNo; - - final RtNetlinkNeighborMessage msg = new RtNetlinkNeighborMessage(nlmsghdr); - msg.mNdmsg = new StructNdMsg(); - msg.mNdmsg.ndm_family = - (byte) ((ip instanceof Inet6Address) ? OsConstants.AF_INET6 : OsConstants.AF_INET); - msg.mNdmsg.ndm_ifindex = ifIndex; - msg.mNdmsg.ndm_state = nudState; - msg.mDestination = ip; - msg.mLinkLayerAddr = llAddr; // might be null - - final byte[] bytes = new byte[msg.getRequiredSpace()]; - nlmsghdr.nlmsg_len = bytes.length; - final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); - byteBuffer.order(ByteOrder.nativeOrder()); - msg.pack(byteBuffer); - return bytes; - } - - private StructNdMsg mNdmsg; - private InetAddress mDestination; - private byte[] mLinkLayerAddr; - private int mNumProbes; - private StructNdaCacheInfo mCacheInfo; - - private RtNetlinkNeighborMessage(StructNlMsgHdr header) { - super(header); - mNdmsg = null; - mDestination = null; - mLinkLayerAddr = null; - mNumProbes = 0; - mCacheInfo = null; - } - - public StructNdMsg getNdHeader() { - return mNdmsg; - } - - public InetAddress getDestination() { - return mDestination; - } - - public byte[] getLinkLayerAddress() { - return mLinkLayerAddr; - } - - public int getProbes() { - return mNumProbes; - } - - public StructNdaCacheInfo getCacheInfo() { - return mCacheInfo; - } - - public int getRequiredSpace() { - int spaceRequired = StructNlMsgHdr.STRUCT_SIZE + StructNdMsg.STRUCT_SIZE; - if (mDestination != null) { - spaceRequired += NetlinkConstants.alignedLengthOf( - StructNlAttr.NLA_HEADERLEN + mDestination.getAddress().length); - } - if (mLinkLayerAddr != null) { - spaceRequired += NetlinkConstants.alignedLengthOf( - StructNlAttr.NLA_HEADERLEN + mLinkLayerAddr.length); - } - // Currently we don't write messages with NDA_PROBES nor NDA_CACHEINFO - // attributes appended. Fix later, if necessary. - return spaceRequired; - } - - private static void packNlAttr(short nlType, byte[] nlValue, ByteBuffer byteBuffer) { - final StructNlAttr nlAttr = new StructNlAttr(); - nlAttr.nla_type = nlType; - nlAttr.nla_value = nlValue; - nlAttr.nla_len = (short) (StructNlAttr.NLA_HEADERLEN + nlAttr.nla_value.length); - nlAttr.pack(byteBuffer); - } - - public void pack(ByteBuffer byteBuffer) { - getHeader().pack(byteBuffer) ; - mNdmsg.pack(byteBuffer); - - if (mDestination != null) { - packNlAttr(NDA_DST, mDestination.getAddress(), byteBuffer); - } - if (mLinkLayerAddr != null) { - packNlAttr(NDA_LLADDR, mLinkLayerAddr, byteBuffer); - } - } - - @Override - public String toString() { - final String ipLiteral = (mDestination == null) ? "" : mDestination.getHostAddress(); - return "RtNetlinkNeighborMessage{ " - + "nlmsghdr{" + (mHeader == null ? "" : mHeader.toString()) + "}, " - + "ndmsg{" + (mNdmsg == null ? "" : mNdmsg.toString()) + "}, " - + "destination{" + ipLiteral + "} " - + "linklayeraddr{" + NetlinkConstants.hexify(mLinkLayerAddr) + "} " - + "probes{" + mNumProbes + "} " - + "cacheinfo{" + (mCacheInfo == null ? "" : mCacheInfo.toString()) + "} " - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/StructInetDiagMsg.java b/services/net/java/android/net/netlink/StructInetDiagMsg.java deleted file mode 100644 index da824ad01efa..000000000000 --- a/services/net/java/android/net/netlink/StructInetDiagMsg.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import static java.nio.ByteOrder.BIG_ENDIAN; -import static android.system.OsConstants.AF_INET; -import static android.system.OsConstants.AF_INET6; - -import java.net.Inet4Address; -import java.net.InetSocketAddress; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import android.util.Log; - -/** - * struct inet_diag_msg - * - * see <linux_src>/include/uapi/linux/inet_diag.h - * - * struct inet_diag_msg { - * __u8 idiag_family; - * __u8 idiag_state; - * __u8 idiag_timer; - * __u8 idiag_retrans; - * struct inet_diag_sockid id; - * __u32 idiag_expires; - * __u32 idiag_rqueue; - * __u32 idiag_wqueue; - * __u32 idiag_uid; - * __u32 idiag_inode; - * }; - * - * @hide - */ -public class StructInetDiagMsg { - public static final int STRUCT_SIZE = 4 + StructInetDiagSockId.STRUCT_SIZE + 20; - private static final int IDIAG_UID_OFFSET = StructNlMsgHdr.STRUCT_SIZE + 4 + - StructInetDiagSockId.STRUCT_SIZE + 12; - public int idiag_uid; - - public static StructInetDiagMsg parse(ByteBuffer byteBuffer) { - StructInetDiagMsg struct = new StructInetDiagMsg(); - struct.idiag_uid = byteBuffer.getInt(IDIAG_UID_OFFSET); - return struct; - } - - @Override - public String toString() { - return "StructInetDiagMsg{ " - + "idiag_uid{" + idiag_uid + "}, " - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/StructInetDiagReqV2.java b/services/net/java/android/net/netlink/StructInetDiagReqV2.java deleted file mode 100644 index 2268113044d5..000000000000 --- a/services/net/java/android/net/netlink/StructInetDiagReqV2.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.annotation.Nullable; - -import java.net.InetSocketAddress; -import java.nio.ByteBuffer; - -/** - * struct inet_diag_req_v2 - * - * see <linux_src>/include/uapi/linux/inet_diag.h - * - * struct inet_diag_req_v2 { - * __u8 sdiag_family; - * __u8 sdiag_protocol; - * __u8 idiag_ext; - * __u8 pad; - * __u32 idiag_states; - * struct inet_diag_sockid id; - * }; - * - * @hide - */ -public class StructInetDiagReqV2 { - public static final int STRUCT_SIZE = 8 + StructInetDiagSockId.STRUCT_SIZE; - - private final byte mSdiagFamily; - private final byte mSdiagProtocol; - private final byte mIdiagExt; - private final byte mPad; - private final StructInetDiagSockId mId; - private final int mState; - public static final int INET_DIAG_REQ_V2_ALL_STATES = (int) 0xffffffff; - - public StructInetDiagReqV2(int protocol, InetSocketAddress local, InetSocketAddress remote, - int family) { - this(protocol, local, remote, family, 0 /* pad */, 0 /* extension */, - INET_DIAG_REQ_V2_ALL_STATES); - } - - public StructInetDiagReqV2(int protocol, @Nullable InetSocketAddress local, - @Nullable InetSocketAddress remote, int family, int pad, int extension, int state) - throws NullPointerException { - mSdiagFamily = (byte) family; - mSdiagProtocol = (byte) protocol; - // Request for all sockets if no specific socket is requested. Specify the local and remote - // socket address information for target request socket. - if ((local == null) != (remote == null)) { - throw new NullPointerException("Local and remote must be both null or both non-null"); - } - mId = ((local != null && remote != null) ? new StructInetDiagSockId(local, remote) : null); - mPad = (byte) pad; - mIdiagExt = (byte) extension; - mState = state; - } - - public void pack(ByteBuffer byteBuffer) { - // The ByteOrder must have already been set by the caller. - byteBuffer.put((byte) mSdiagFamily); - byteBuffer.put((byte) mSdiagProtocol); - byteBuffer.put((byte) mIdiagExt); - byteBuffer.put((byte) mPad); - byteBuffer.putInt(mState); - if (mId != null) mId.pack(byteBuffer); - } - - @Override - public String toString() { - final String familyStr = NetlinkConstants.stringForAddressFamily(mSdiagFamily); - final String protocolStr = NetlinkConstants.stringForAddressFamily(mSdiagProtocol); - - return "StructInetDiagReqV2{ " - + "sdiag_family{" + familyStr + "}, " - + "sdiag_protocol{" + protocolStr + "}, " - + "idiag_ext{" + mIdiagExt + ")}, " - + "pad{" + mPad + "}, " - + "idiag_states{" + Integer.toHexString(mState) + "}, " - + ((mId != null) ? mId.toString() : "inet_diag_sockid=null") - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/StructInetDiagSockId.java b/services/net/java/android/net/netlink/StructInetDiagSockId.java deleted file mode 100644 index 2e9fa253463d..000000000000 --- a/services/net/java/android/net/netlink/StructInetDiagSockId.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import static java.nio.ByteOrder.BIG_ENDIAN; - -import java.net.Inet4Address; -import java.net.InetSocketAddress; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -/** - * struct inet_diag_req_v2 - * - * see <linux_src>/include/uapi/linux/inet_diag.h - * - * struct inet_diag_sockid { - * __be16 idiag_sport; - * __be16 idiag_dport; - * __be32 idiag_src[4]; - * __be32 idiag_dst[4]; - * __u32 idiag_if; - * __u32 idiag_cookie[2]; - * #define INET_DIAG_NOCOOKIE (~0U) - * }; - * - * @hide - */ -public class StructInetDiagSockId { - public static final int STRUCT_SIZE = 48; - - private final InetSocketAddress mLocSocketAddress; - private final InetSocketAddress mRemSocketAddress; - private final byte[] INET_DIAG_NOCOOKIE = new byte[]{ - (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, - (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; - private final byte[] IPV4_PADDING = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - public StructInetDiagSockId(InetSocketAddress loc, InetSocketAddress rem) { - mLocSocketAddress = loc; - mRemSocketAddress = rem; - } - - public void pack(ByteBuffer byteBuffer) { - byteBuffer.order(BIG_ENDIAN); - byteBuffer.putShort((short) mLocSocketAddress.getPort()); - byteBuffer.putShort((short) mRemSocketAddress.getPort()); - byteBuffer.put(mLocSocketAddress.getAddress().getAddress()); - if (mLocSocketAddress.getAddress() instanceof Inet4Address) { - byteBuffer.put(IPV4_PADDING); - } - byteBuffer.put(mRemSocketAddress.getAddress().getAddress()); - if (mRemSocketAddress.getAddress() instanceof Inet4Address) { - byteBuffer.put(IPV4_PADDING); - } - byteBuffer.order(ByteOrder.nativeOrder()); - byteBuffer.putInt(0); - byteBuffer.put(INET_DIAG_NOCOOKIE); - } - - @Override - public String toString() { - return "StructInetDiagSockId{ " - + "idiag_sport{" + mLocSocketAddress.getPort() + "}, " - + "idiag_dport{" + mRemSocketAddress.getPort() + "}, " - + "idiag_src{" + mLocSocketAddress.getAddress().getHostAddress() + "}, " - + "idiag_dst{" + mRemSocketAddress.getAddress().getHostAddress() + "}, " - + "idiag_if{" + 0 + "} " - + "idiag_cookie{INET_DIAG_NOCOOKIE}" - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/StructNdMsg.java b/services/net/java/android/net/netlink/StructNdMsg.java deleted file mode 100644 index e34ec39ab99b..000000000000 --- a/services/net/java/android/net/netlink/StructNdMsg.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.net.netlink.NetlinkConstants; -import android.system.OsConstants; -import java.nio.ByteBuffer; - - -/** - * struct ndmsg - * - * see: <linux_src>/include/uapi/linux/neighbour.h - * - * @hide - */ -public class StructNdMsg { - // Already aligned. - public static final int STRUCT_SIZE = 12; - - // Neighbor Cache Entry States - public static final short NUD_NONE = 0x00; - public static final short NUD_INCOMPLETE = 0x01; - public static final short NUD_REACHABLE = 0x02; - public static final short NUD_STALE = 0x04; - public static final short NUD_DELAY = 0x08; - public static final short NUD_PROBE = 0x10; - public static final short NUD_FAILED = 0x20; - public static final short NUD_NOARP = 0x40; - public static final short NUD_PERMANENT = 0x80; - - public static String stringForNudState(short nudState) { - switch (nudState) { - case NUD_NONE: return "NUD_NONE"; - case NUD_INCOMPLETE: return "NUD_INCOMPLETE"; - case NUD_REACHABLE: return "NUD_REACHABLE"; - case NUD_STALE: return "NUD_STALE"; - case NUD_DELAY: return "NUD_DELAY"; - case NUD_PROBE: return "NUD_PROBE"; - case NUD_FAILED: return "NUD_FAILED"; - case NUD_NOARP: return "NUD_NOARP"; - case NUD_PERMANENT: return "NUD_PERMANENT"; - default: - return "unknown NUD state: " + String.valueOf(nudState); - } - } - - public static boolean isNudStateConnected(short nudState) { - return ((nudState & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)) != 0); - } - - public static boolean isNudStateValid(short nudState) { - return (isNudStateConnected(nudState) || - ((nudState & (NUD_PROBE|NUD_STALE|NUD_DELAY)) != 0)); - } - - // Neighbor Cache Entry Flags - public static byte NTF_USE = (byte) 0x01; - public static byte NTF_SELF = (byte) 0x02; - public static byte NTF_MASTER = (byte) 0x04; - public static byte NTF_PROXY = (byte) 0x08; - public static byte NTF_ROUTER = (byte) 0x80; - - public static String stringForNudFlags(byte flags) { - final StringBuilder sb = new StringBuilder(); - if ((flags & NTF_USE) != 0) { - sb.append("NTF_USE"); - } - if ((flags & NTF_SELF) != 0) { - if (sb.length() > 0) { sb.append("|"); } - sb.append("NTF_SELF"); - } - if ((flags & NTF_MASTER) != 0) { - if (sb.length() > 0) { sb.append("|"); } - sb.append("NTF_MASTER"); - } - if ((flags & NTF_PROXY) != 0) { - if (sb.length() > 0) { sb.append("|"); - } - sb.append("NTF_PROXY"); } - if ((flags & NTF_ROUTER) != 0) { - if (sb.length() > 0) { sb.append("|"); } - sb.append("NTF_ROUTER"); - } - return sb.toString(); - } - - private static boolean hasAvailableSpace(ByteBuffer byteBuffer) { - return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE; - } - - public static StructNdMsg parse(ByteBuffer byteBuffer) { - if (!hasAvailableSpace(byteBuffer)) { return null; } - - // The ByteOrder must have already been set by the caller. In most - // cases ByteOrder.nativeOrder() is correct, with the possible - // exception of usage within unittests. - final StructNdMsg struct = new StructNdMsg(); - struct.ndm_family = byteBuffer.get(); - final byte pad1 = byteBuffer.get(); - final short pad2 = byteBuffer.getShort(); - struct.ndm_ifindex = byteBuffer.getInt(); - struct.ndm_state = byteBuffer.getShort(); - struct.ndm_flags = byteBuffer.get(); - struct.ndm_type = byteBuffer.get(); - return struct; - } - - public byte ndm_family; - public int ndm_ifindex; - public short ndm_state; - public byte ndm_flags; - public byte ndm_type; - - public StructNdMsg() { - ndm_family = (byte) OsConstants.AF_UNSPEC; - } - - public void pack(ByteBuffer byteBuffer) { - // The ByteOrder must have already been set by the caller. In most - // cases ByteOrder.nativeOrder() is correct, with the exception - // of usage within unittests. - byteBuffer.put(ndm_family); - byteBuffer.put((byte) 0); // pad1 - byteBuffer.putShort((short) 0); // pad2 - byteBuffer.putInt(ndm_ifindex); - byteBuffer.putShort(ndm_state); - byteBuffer.put(ndm_flags); - byteBuffer.put(ndm_type); - } - - public boolean nudConnected() { - return isNudStateConnected(ndm_state); - } - - public boolean nudValid() { - return isNudStateValid(ndm_state); - } - - @Override - public String toString() { - final String stateStr = "" + ndm_state + " (" + stringForNudState(ndm_state) + ")"; - final String flagsStr = "" + ndm_flags + " (" + stringForNudFlags(ndm_flags) + ")"; - return "StructNdMsg{ " - + "family{" + NetlinkConstants.stringForAddressFamily((int) ndm_family) + "}, " - + "ifindex{" + ndm_ifindex + "}, " - + "state{" + stateStr + "}, " - + "flags{" + flagsStr + "}, " - + "type{" + ndm_type + "} " - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/StructNdaCacheInfo.java b/services/net/java/android/net/netlink/StructNdaCacheInfo.java deleted file mode 100644 index 16cf56385eb8..000000000000 --- a/services/net/java/android/net/netlink/StructNdaCacheInfo.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.system.Os; -import android.system.OsConstants; - -import java.nio.ByteBuffer; - - -/** - * struct nda_cacheinfo - * - * see: <linux_src>/include/uapi/linux/neighbour.h - * - * @hide - */ -public class StructNdaCacheInfo { - // Already aligned. - public static final int STRUCT_SIZE = 16; - - private static boolean hasAvailableSpace(ByteBuffer byteBuffer) { - return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE; - } - - public static StructNdaCacheInfo parse(ByteBuffer byteBuffer) { - if (!hasAvailableSpace(byteBuffer)) { return null; } - - // The ByteOrder must have already been set by the caller. In most - // cases ByteOrder.nativeOrder() is correct, with the possible - // exception of usage within unittests. - final StructNdaCacheInfo struct = new StructNdaCacheInfo(); - struct.ndm_used = byteBuffer.getInt(); - struct.ndm_confirmed = byteBuffer.getInt(); - struct.ndm_updated = byteBuffer.getInt(); - struct.ndm_refcnt = byteBuffer.getInt(); - return struct; - } - - // TODO: investigate whether this can change during device runtime and - // decide what (if anything) should be done about that. - private static final long CLOCK_TICKS_PER_SECOND = Os.sysconf(OsConstants._SC_CLK_TCK); - - private static long ticksToMilliSeconds(int intClockTicks) { - final long longClockTicks = (long) intClockTicks & 0xffffffff; - return (longClockTicks * 1000) / CLOCK_TICKS_PER_SECOND; - } - - /** - * Explanatory notes, for reference. - * - * Before being returned to user space, the neighbor entry times are - * converted to clock_t's like so: - * - * ndm_used = jiffies_to_clock_t(now - neigh->used); - * ndm_confirmed = jiffies_to_clock_t(now - neigh->confirmed); - * ndm_updated = jiffies_to_clock_t(now - neigh->updated); - * - * meaning that these values are expressed as "clock ticks ago". To - * convert these clock ticks to seconds divide by sysconf(_SC_CLK_TCK). - * When _SC_CLK_TCK is 100, for example, the ndm_* times are expressed - * in centiseconds. - * - * These values are unsigned, but fortunately being expressed as "some - * clock ticks ago", these values are typically very small (and - * 2^31 centiseconds = 248 days). - * - * By observation, it appears that: - * ndm_used: the last time ARP/ND took place for this neighbor - * ndm_confirmed: the last time ARP/ND succeeded for this neighbor OR - * higher layer confirmation (TCP or MSG_CONFIRM) - * was received - * ndm_updated: the time when the current NUD state was entered - */ - public int ndm_used; - public int ndm_confirmed; - public int ndm_updated; - public int ndm_refcnt; - - public StructNdaCacheInfo() {} - - public long lastUsed() { - return ticksToMilliSeconds(ndm_used); - } - - public long lastConfirmed() { - return ticksToMilliSeconds(ndm_confirmed); - } - - public long lastUpdated() { - return ticksToMilliSeconds(ndm_updated); - } - - @Override - public String toString() { - return "NdaCacheInfo{ " - + "ndm_used{" + lastUsed() + "}, " - + "ndm_confirmed{" + lastConfirmed() + "}, " - + "ndm_updated{" + lastUpdated() + "}, " - + "ndm_refcnt{" + ndm_refcnt + "} " - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/StructNfGenMsg.java b/services/net/java/android/net/netlink/StructNfGenMsg.java deleted file mode 100644 index 8155977b6e6a..000000000000 --- a/services/net/java/android/net/netlink/StructNfGenMsg.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import java.nio.ByteBuffer; - - -/** - * struct nfgenmsg - * - * see <linux_src>/include/uapi/linux/netfilter/nfnetlink.h - * - * @hide - */ -public class StructNfGenMsg { - public static final int STRUCT_SIZE = 2 + Short.BYTES; - - public static final int NFNETLINK_V0 = 0; - - final public byte nfgen_family; - final public byte version; - final public short res_id; // N.B.: this is big endian in the kernel - - public StructNfGenMsg(byte family) { - nfgen_family = family; - version = (byte) NFNETLINK_V0; - res_id = (short) 0; - } - - public void pack(ByteBuffer byteBuffer) { - byteBuffer.put(nfgen_family); - byteBuffer.put(version); - byteBuffer.putShort(res_id); - } -} diff --git a/services/net/java/android/net/netlink/StructNlAttr.java b/services/net/java/android/net/netlink/StructNlAttr.java deleted file mode 100644 index 28a4e883a316..000000000000 --- a/services/net/java/android/net/netlink/StructNlAttr.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.net.netlink.NetlinkConstants; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.nio.ByteOrder; -import java.nio.ByteBuffer; - - -/** - * struct nlattr - * - * see: <linux_src>/include/uapi/linux/netlink.h - * - * @hide - */ -public class StructNlAttr { - // Already aligned. - public static final int NLA_HEADERLEN = 4; - public static final int NLA_F_NESTED = (1 << 15); - - public static short makeNestedType(short type) { - return (short) (type | NLA_F_NESTED); - } - - // Return a (length, type) object only, without consuming any bytes in - // |byteBuffer| and without copying or interpreting any value bytes. - // This is used for scanning over a packed set of struct nlattr's, - // looking for instances of a particular type. - public static StructNlAttr peek(ByteBuffer byteBuffer) { - if (byteBuffer == null || byteBuffer.remaining() < NLA_HEADERLEN) { - return null; - } - final int baseOffset = byteBuffer.position(); - - // Assume the byte order of the buffer is the expected byte order of the value. - final StructNlAttr struct = new StructNlAttr(byteBuffer.order()); - // The byte order of nla_len and nla_type is always native. - final ByteOrder originalOrder = byteBuffer.order(); - byteBuffer.order(ByteOrder.nativeOrder()); - try { - struct.nla_len = byteBuffer.getShort(); - struct.nla_type = byteBuffer.getShort(); - } finally { - byteBuffer.order(originalOrder); - } - - byteBuffer.position(baseOffset); - if (struct.nla_len < NLA_HEADERLEN) { - // Malformed. - return null; - } - return struct; - } - - public static StructNlAttr parse(ByteBuffer byteBuffer) { - final StructNlAttr struct = peek(byteBuffer); - if (struct == null || byteBuffer.remaining() < struct.getAlignedLength()) { - return null; - } - - final int baseOffset = byteBuffer.position(); - byteBuffer.position(baseOffset + NLA_HEADERLEN); - - int valueLen = ((int) struct.nla_len) & 0xffff; - valueLen -= NLA_HEADERLEN; - if (valueLen > 0) { - struct.nla_value = new byte[valueLen]; - byteBuffer.get(struct.nla_value, 0, valueLen); - byteBuffer.position(baseOffset + struct.getAlignedLength()); - } - return struct; - } - - public short nla_len = (short) NLA_HEADERLEN; - public short nla_type; - public byte[] nla_value; - - // The byte order used to read/write the value member. Netlink length and - // type members are always read/written in native order. - private ByteOrder mByteOrder = ByteOrder.nativeOrder(); - - public StructNlAttr() {} - - public StructNlAttr(ByteOrder byteOrder) { - mByteOrder = byteOrder; - } - - public StructNlAttr(short type, byte value) { - nla_type = type; - setValue(new byte[1]); - nla_value[0] = value; - } - - public StructNlAttr(short type, short value) { - this(type, value, ByteOrder.nativeOrder()); - } - - public StructNlAttr(short type, short value, ByteOrder order) { - this(order); - nla_type = type; - setValue(new byte[Short.BYTES]); - getValueAsByteBuffer().putShort(value); - } - - public StructNlAttr(short type, int value) { - this(type, value, ByteOrder.nativeOrder()); - } - - public StructNlAttr(short type, int value, ByteOrder order) { - this(order); - nla_type = type; - setValue(new byte[Integer.BYTES]); - getValueAsByteBuffer().putInt(value); - } - - public StructNlAttr(short type, InetAddress ip) { - nla_type = type; - setValue(ip.getAddress()); - } - - public StructNlAttr(short type, StructNlAttr... nested) { - this(); - nla_type = makeNestedType(type); - - int payloadLength = 0; - for (StructNlAttr nla : nested) payloadLength += nla.getAlignedLength(); - setValue(new byte[payloadLength]); - - final ByteBuffer buf = getValueAsByteBuffer(); - for (StructNlAttr nla : nested) { - nla.pack(buf); - } - } - - public int getAlignedLength() { - return NetlinkConstants.alignedLengthOf(nla_len); - } - - public ByteBuffer getValueAsByteBuffer() { - if (nla_value == null) { return null; } - final ByteBuffer byteBuffer = ByteBuffer.wrap(nla_value); - byteBuffer.order(mByteOrder); - return byteBuffer; - } - - public int getValueAsInt(int defaultValue) { - final ByteBuffer byteBuffer = getValueAsByteBuffer(); - if (byteBuffer == null || byteBuffer.remaining() != Integer.BYTES) { - return defaultValue; - } - return getValueAsByteBuffer().getInt(); - } - - public InetAddress getValueAsInetAddress() { - if (nla_value == null) { return null; } - - try { - return InetAddress.getByAddress(nla_value); - } catch (UnknownHostException ignored) { - return null; - } - } - - public void pack(ByteBuffer byteBuffer) { - final ByteOrder originalOrder = byteBuffer.order(); - final int originalPosition = byteBuffer.position(); - - byteBuffer.order(ByteOrder.nativeOrder()); - try { - byteBuffer.putShort(nla_len); - byteBuffer.putShort(nla_type); - if (nla_value != null) byteBuffer.put(nla_value); - } finally { - byteBuffer.order(originalOrder); - } - byteBuffer.position(originalPosition + getAlignedLength()); - } - - private void setValue(byte[] value) { - nla_value = value; - nla_len = (short) (NLA_HEADERLEN + ((nla_value != null) ? nla_value.length : 0)); - } - - @Override - public String toString() { - return "StructNlAttr{ " - + "nla_len{" + nla_len + "}, " - + "nla_type{" + nla_type + "}, " - + "nla_value{" + NetlinkConstants.hexify(nla_value) + "}, " - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/StructNlMsgErr.java b/services/net/java/android/net/netlink/StructNlMsgErr.java deleted file mode 100644 index 6fcc6e69da5b..000000000000 --- a/services/net/java/android/net/netlink/StructNlMsgErr.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.net.netlink.NetlinkConstants; -import android.net.netlink.StructNlMsgHdr; - -import java.nio.ByteBuffer; - - -/** - * struct nlmsgerr - * - * see <linux_src>/include/uapi/linux/netlink.h - * - * @hide - */ -public class StructNlMsgErr { - public static final int STRUCT_SIZE = Integer.BYTES + StructNlMsgHdr.STRUCT_SIZE; - - public static boolean hasAvailableSpace(ByteBuffer byteBuffer) { - return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE; - } - - public static StructNlMsgErr parse(ByteBuffer byteBuffer) { - if (!hasAvailableSpace(byteBuffer)) { return null; } - - // The ByteOrder must have already been set by the caller. In most - // cases ByteOrder.nativeOrder() is correct, with the exception - // of usage within unittests. - final StructNlMsgErr struct = new StructNlMsgErr(); - struct.error = byteBuffer.getInt(); - struct.msg = StructNlMsgHdr.parse(byteBuffer); - return struct; - } - - public int error; - public StructNlMsgHdr msg; - - public void pack(ByteBuffer byteBuffer) { - // The ByteOrder must have already been set by the caller. In most - // cases ByteOrder.nativeOrder() is correct, with the possible - // exception of usage within unittests. - byteBuffer.putInt(error); - if (msg != null) { - msg.pack(byteBuffer); - } - } - - @Override - public String toString() { - return "StructNlMsgErr{ " - + "error{" + error + "}, " - + "msg{" + (msg == null ? "" : msg.toString()) + "} " - + "}"; - } -} diff --git a/services/net/java/android/net/netlink/StructNlMsgHdr.java b/services/net/java/android/net/netlink/StructNlMsgHdr.java deleted file mode 100644 index 98ab5e719d69..000000000000 --- a/services/net/java/android/net/netlink/StructNlMsgHdr.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.netlink; - -import android.net.netlink.NetlinkConstants; -import java.nio.ByteBuffer; - - -/** - * struct nlmsghdr - * - * see <linux_src>/include/uapi/linux/netlink.h - * - * @hide - */ -public class StructNlMsgHdr { - // Already aligned. - public static final int STRUCT_SIZE = 16; - - public static final short NLM_F_REQUEST = 0x0001; - public static final short NLM_F_MULTI = 0x0002; - public static final short NLM_F_ACK = 0x0004; - public static final short NLM_F_ECHO = 0x0008; - // Flags for a GET request. - public static final short NLM_F_ROOT = 0x0100; - public static final short NLM_F_MATCH = 0x0200; - public static final short NLM_F_DUMP = NLM_F_ROOT|NLM_F_MATCH; - // Flags for a NEW request. - public static final short NLM_F_REPLACE = 0x100; - public static final short NLM_F_EXCL = 0x200; - public static final short NLM_F_CREATE = 0x400; - public static final short NLM_F_APPEND = 0x800; - - - public static String stringForNlMsgFlags(short flags) { - final StringBuilder sb = new StringBuilder(); - if ((flags & NLM_F_REQUEST) != 0) { - sb.append("NLM_F_REQUEST"); - } - if ((flags & NLM_F_MULTI) != 0) { - if (sb.length() > 0) { sb.append("|"); } - sb.append("NLM_F_MULTI"); - } - if ((flags & NLM_F_ACK) != 0) { - if (sb.length() > 0) { sb.append("|"); } - sb.append("NLM_F_ACK"); - } - if ((flags & NLM_F_ECHO) != 0) { - if (sb.length() > 0) { sb.append("|"); } - sb.append("NLM_F_ECHO"); - } - if ((flags & NLM_F_ROOT) != 0) { - if (sb.length() > 0) { sb.append("|"); } - sb.append("NLM_F_ROOT"); - } - if ((flags & NLM_F_MATCH) != 0) { - if (sb.length() > 0) { sb.append("|"); } - sb.append("NLM_F_MATCH"); - } - return sb.toString(); - } - - public static boolean hasAvailableSpace(ByteBuffer byteBuffer) { - return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE; - } - - public static StructNlMsgHdr parse(ByteBuffer byteBuffer) { - if (!hasAvailableSpace(byteBuffer)) { return null; } - - // The ByteOrder must have already been set by the caller. In most - // cases ByteOrder.nativeOrder() is correct, with the exception - // of usage within unittests. - final StructNlMsgHdr struct = new StructNlMsgHdr(); - struct.nlmsg_len = byteBuffer.getInt(); - struct.nlmsg_type = byteBuffer.getShort(); - struct.nlmsg_flags = byteBuffer.getShort(); - struct.nlmsg_seq = byteBuffer.getInt(); - struct.nlmsg_pid = byteBuffer.getInt(); - - if (struct.nlmsg_len < STRUCT_SIZE) { - // Malformed. - return null; - } - return struct; - } - - public int nlmsg_len; - public short nlmsg_type; - public short nlmsg_flags; - public int nlmsg_seq; - public int nlmsg_pid; - - public StructNlMsgHdr() { - nlmsg_len = 0; - nlmsg_type = 0; - nlmsg_flags = 0; - nlmsg_seq = 0; - nlmsg_pid = 0; - } - - public void pack(ByteBuffer byteBuffer) { - // The ByteOrder must have already been set by the caller. In most - // cases ByteOrder.nativeOrder() is correct, with the possible - // exception of usage within unittests. - byteBuffer.putInt(nlmsg_len); - byteBuffer.putShort(nlmsg_type); - byteBuffer.putShort(nlmsg_flags); - byteBuffer.putInt(nlmsg_seq); - byteBuffer.putInt(nlmsg_pid); - } - - @Override - public String toString() { - final String typeStr = "" + nlmsg_type - + "(" + NetlinkConstants.stringForNlMsgType(nlmsg_type) + ")"; - final String flagsStr = "" + nlmsg_flags - + "(" + stringForNlMsgFlags(nlmsg_flags) + ")"; - return "StructNlMsgHdr{ " - + "nlmsg_len{" + nlmsg_len + "}, " - + "nlmsg_type{" + typeStr + "}, " - + "nlmsg_flags{" + flagsStr + ")}, " - + "nlmsg_seq{" + nlmsg_seq + "}, " - + "nlmsg_pid{" + nlmsg_pid + "} " - + "}"; - } -} diff --git a/services/net/java/android/net/shared/InitialConfiguration.java b/services/net/java/android/net/shared/InitialConfiguration.java deleted file mode 100644 index 007c8ca93d5a..000000000000 --- a/services/net/java/android/net/shared/InitialConfiguration.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.shared; - -import static android.net.shared.ParcelableUtil.fromParcelableArray; -import static android.net.shared.ParcelableUtil.toParcelableArray; -import static android.text.TextUtils.join; - -import android.net.InetAddresses; -import android.net.InitialConfigurationParcelable; -import android.net.IpPrefix; -import android.net.LinkAddress; -import android.net.RouteInfo; - -import java.net.Inet4Address; -import java.net.InetAddress; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.function.Predicate; - -/** @hide */ -public class InitialConfiguration { - public final Set<LinkAddress> ipAddresses = new HashSet<>(); - public final Set<IpPrefix> directlyConnectedRoutes = new HashSet<>(); - public final Set<InetAddress> dnsServers = new HashSet<>(); - - private static final int RFC6177_MIN_PREFIX_LENGTH = 48; - private static final int RFC7421_PREFIX_LENGTH = 64; - - public static final InetAddress INET6_ANY = InetAddresses.parseNumericAddress("::"); - - /** - * Create a InitialConfiguration that is a copy of the specified configuration. - */ - public static InitialConfiguration copy(InitialConfiguration config) { - if (config == null) { - return null; - } - InitialConfiguration configCopy = new InitialConfiguration(); - configCopy.ipAddresses.addAll(config.ipAddresses); - configCopy.directlyConnectedRoutes.addAll(config.directlyConnectedRoutes); - configCopy.dnsServers.addAll(config.dnsServers); - return configCopy; - } - - @Override - public String toString() { - return String.format( - "InitialConfiguration(IPs: {%s}, prefixes: {%s}, DNS: {%s})", - join(", ", ipAddresses), join(", ", directlyConnectedRoutes), - join(", ", dnsServers)); - } - - /** - * Tests whether the contents of this IpConfiguration represent a valid configuration. - */ - public boolean isValid() { - if (ipAddresses.isEmpty()) { - return false; - } - - // For every IP address, there must be at least one prefix containing that address. - for (LinkAddress addr : ipAddresses) { - if (!any(directlyConnectedRoutes, (p) -> p.contains(addr.getAddress()))) { - return false; - } - } - // For every dns server, there must be at least one prefix containing that address. - for (InetAddress addr : dnsServers) { - if (!any(directlyConnectedRoutes, (p) -> p.contains(addr))) { - return false; - } - } - // All IPv6 LinkAddresses have an RFC7421-suitable prefix length - // (read: compliant with RFC4291#section2.5.4). - if (any(ipAddresses, not(InitialConfiguration::isPrefixLengthCompliant))) { - return false; - } - // If directlyConnectedRoutes contains an IPv6 default route - // then ipAddresses MUST contain at least one non-ULA GUA. - if (any(directlyConnectedRoutes, InitialConfiguration::isIPv6DefaultRoute) - && all(ipAddresses, not(InitialConfiguration::isIPv6GUA))) { - return false; - } - // The prefix length of routes in directlyConnectedRoutes be within reasonable - // bounds for IPv6: /48-/64 just as we’d accept in RIOs. - if (any(directlyConnectedRoutes, not(InitialConfiguration::isPrefixLengthCompliant))) { - return false; - } - // There no more than one IPv4 address - if (ipAddresses.stream().filter(InitialConfiguration::isIPv4).count() > 1) { - return false; - } - - return true; - } - - /** - * @return true if the given list of addressess and routes satisfies provisioning for this - * InitialConfiguration. LinkAddresses and RouteInfo objects are not compared with equality - * because addresses and routes seen by Netlink will contain additional fields like flags, - * interfaces, and so on. If this InitialConfiguration has no IP address specified, the - * provisioning check always fails. - * - * If the given list of routes is null, only addresses are taken into considerations. - */ - public boolean isProvisionedBy(List<LinkAddress> addresses, List<RouteInfo> routes) { - if (ipAddresses.isEmpty()) { - return false; - } - - for (LinkAddress addr : ipAddresses) { - if (!any(addresses, (addrSeen) -> addr.isSameAddressAs(addrSeen))) { - return false; - } - } - - if (routes != null) { - for (IpPrefix prefix : directlyConnectedRoutes) { - if (!any(routes, (routeSeen) -> isDirectlyConnectedRoute(routeSeen, prefix))) { - return false; - } - } - } - - return true; - } - - /** - * Convert this configuration to a {@link InitialConfigurationParcelable}. - */ - public InitialConfigurationParcelable toStableParcelable() { - final InitialConfigurationParcelable p = new InitialConfigurationParcelable(); - p.ipAddresses = ipAddresses.toArray(new LinkAddress[0]); - p.directlyConnectedRoutes = directlyConnectedRoutes.toArray(new IpPrefix[0]); - p.dnsServers = toParcelableArray( - dnsServers, IpConfigurationParcelableUtil::parcelAddress, String.class); - return p; - } - - /** - * Create an instance of {@link InitialConfiguration} based on the contents of the specified - * {@link InitialConfigurationParcelable}. - */ - public static InitialConfiguration fromStableParcelable(InitialConfigurationParcelable p) { - if (p == null) return null; - final InitialConfiguration config = new InitialConfiguration(); - config.ipAddresses.addAll(Arrays.asList(p.ipAddresses)); - config.directlyConnectedRoutes.addAll(Arrays.asList(p.directlyConnectedRoutes)); - config.dnsServers.addAll( - fromParcelableArray(p.dnsServers, IpConfigurationParcelableUtil::unparcelAddress)); - return config; - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof InitialConfiguration)) return false; - final InitialConfiguration other = (InitialConfiguration) obj; - return ipAddresses.equals(other.ipAddresses) - && directlyConnectedRoutes.equals(other.directlyConnectedRoutes) - && dnsServers.equals(other.dnsServers); - } - - private static boolean isDirectlyConnectedRoute(RouteInfo route, IpPrefix prefix) { - return !route.hasGateway() && prefix.equals(route.getDestination()); - } - - private static boolean isPrefixLengthCompliant(LinkAddress addr) { - return isIPv4(addr) || isCompliantIPv6PrefixLength(addr.getPrefixLength()); - } - - private static boolean isPrefixLengthCompliant(IpPrefix prefix) { - return isIPv4(prefix) || isCompliantIPv6PrefixLength(prefix.getPrefixLength()); - } - - private static boolean isCompliantIPv6PrefixLength(int prefixLength) { - return (RFC6177_MIN_PREFIX_LENGTH <= prefixLength) - && (prefixLength <= RFC7421_PREFIX_LENGTH); - } - - private static boolean isIPv4(IpPrefix prefix) { - return prefix.getAddress() instanceof Inet4Address; - } - - private static boolean isIPv4(LinkAddress addr) { - return addr.getAddress() instanceof Inet4Address; - } - - private static boolean isIPv6DefaultRoute(IpPrefix prefix) { - return prefix.getAddress().equals(INET6_ANY); - } - - private static boolean isIPv6GUA(LinkAddress addr) { - return addr.isIpv6() && addr.isGlobalPreferred(); - } - - // TODO: extract out into CollectionUtils. - - /** - * Indicate whether any element of the specified iterable verifies the specified predicate. - */ - public static <T> boolean any(Iterable<T> coll, Predicate<T> fn) { - for (T t : coll) { - if (fn.test(t)) { - return true; - } - } - return false; - } - - /** - * Indicate whether all elements of the specified iterable verifies the specified predicate. - */ - public static <T> boolean all(Iterable<T> coll, Predicate<T> fn) { - return !any(coll, not(fn)); - } - - /** - * Create a predicate that returns the opposite value of the specified predicate. - */ - public static <T> Predicate<T> not(Predicate<T> fn) { - return (t) -> !fn.test(t); - } -} diff --git a/services/net/java/android/net/shared/IpConfigurationParcelableUtil.java b/services/net/java/android/net/shared/IpConfigurationParcelableUtil.java deleted file mode 100644 index 172dc24df6b0..000000000000 --- a/services/net/java/android/net/shared/IpConfigurationParcelableUtil.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.shared; - -import android.annotation.Nullable; -import android.net.DhcpResults; -import android.net.DhcpResultsParcelable; -import android.net.InetAddresses; - -import java.net.Inet4Address; -import java.net.InetAddress; - -/** - * Collection of utility methods to convert to and from stable AIDL parcelables for IpClient - * configuration classes. - * @hide - */ -public final class IpConfigurationParcelableUtil { - /** - * Convert DhcpResults to a DhcpResultsParcelable. - */ - public static DhcpResultsParcelable toStableParcelable(@Nullable DhcpResults results) { - if (results == null) return null; - final DhcpResultsParcelable p = new DhcpResultsParcelable(); - p.baseConfiguration = results.toStaticIpConfiguration(); - p.leaseDuration = results.leaseDuration; - p.mtu = results.mtu; - p.serverAddress = parcelAddress(results.serverAddress); - p.vendorInfo = results.vendorInfo; - p.serverHostName = results.serverHostName; - return p; - } - - /** - * Convert a DhcpResultsParcelable to DhcpResults. - */ - public static DhcpResults fromStableParcelable(@Nullable DhcpResultsParcelable p) { - if (p == null) return null; - final DhcpResults results = new DhcpResults(p.baseConfiguration); - results.leaseDuration = p.leaseDuration; - results.mtu = p.mtu; - results.serverAddress = (Inet4Address) unparcelAddress(p.serverAddress); - results.vendorInfo = p.vendorInfo; - results.serverHostName = p.serverHostName; - return results; - } - - /** - * Convert InetAddress to String. - * TODO: have an InetAddressParcelable - */ - public static String parcelAddress(@Nullable InetAddress addr) { - if (addr == null) return null; - return addr.getHostAddress(); - } - - /** - * Convert String to InetAddress. - * TODO: have an InetAddressParcelable - */ - public static InetAddress unparcelAddress(@Nullable String addr) { - if (addr == null) return null; - return InetAddresses.parseNumericAddress(addr); - } -} diff --git a/services/net/java/android/net/shared/LinkPropertiesParcelableUtil.java b/services/net/java/android/net/shared/LinkPropertiesParcelableUtil.java deleted file mode 100644 index 1729da6eb141..000000000000 --- a/services/net/java/android/net/shared/LinkPropertiesParcelableUtil.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.shared; - -import android.annotation.Nullable; -import android.net.LinkProperties; -import android.net.ProxyInfo; - -/** - * Collection of utility methods to convert to and from stable AIDL parcelables for LinkProperties - * and its attributes. - * @hide - */ -public final class LinkPropertiesParcelableUtil { - // Temporary methods to facilitate migrating clients away from LinkPropertiesParcelable - // TODO: remove the following methods after migrating clients. - - /** - * @deprecated conversion to stable parcelable is no longer necessary. - */ - @Deprecated - public static LinkProperties toStableParcelable(@Nullable LinkProperties lp) { - return lp; - } - - /** - * @deprecated conversion to stable parcelable is no longer necessary. - */ - @Deprecated - public static ProxyInfo toStableParcelable(@Nullable ProxyInfo info) { - return info; - } -} diff --git a/services/net/java/android/net/shared/NetworkMonitorUtils.java b/services/net/java/android/net/shared/NetworkMonitorUtils.java deleted file mode 100644 index 46e9c7373f97..000000000000 --- a/services/net/java/android/net/shared/NetworkMonitorUtils.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.shared; - -import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; -import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; -import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; -import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED; - -import android.net.NetworkCapabilities; - -/** @hide */ -public class NetworkMonitorUtils { - - // Network conditions broadcast constants - public static final String ACTION_NETWORK_CONDITIONS_MEASURED = - "android.net.conn.NETWORK_CONDITIONS_MEASURED"; - public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type"; - public static final String EXTRA_NETWORK_TYPE = "extra_network_type"; - public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received"; - public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal"; - public static final String EXTRA_CELL_ID = "extra_cellid"; - public static final String EXTRA_SSID = "extra_ssid"; - public static final String EXTRA_BSSID = "extra_bssid"; - /** real time since boot */ - public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms"; - public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms"; - public static final String PERMISSION_ACCESS_NETWORK_CONDITIONS = - "android.permission.ACCESS_NETWORK_CONDITIONS"; - - /** - * Return whether validation is required for private DNS in strict mode. - * @param nc Network capabilities of the network to test. - */ - public static boolean isPrivateDnsValidationRequired(NetworkCapabilities nc) { - // TODO: Consider requiring validation for DUN networks. - return nc != null - && nc.hasCapability(NET_CAPABILITY_INTERNET) - && nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) - && nc.hasCapability(NET_CAPABILITY_TRUSTED); - } - - /** - * Return whether validation is required for a network. - * @param nc Network capabilities of the network to test. - */ - public static boolean isValidationRequired(NetworkCapabilities nc) { - // TODO: Consider requiring validation for DUN networks. - return isPrivateDnsValidationRequired(nc) && nc.hasCapability(NET_CAPABILITY_NOT_VPN); - } -} diff --git a/services/net/java/android/net/shared/ParcelableUtil.java b/services/net/java/android/net/shared/ParcelableUtil.java deleted file mode 100644 index 3f4030047938..000000000000 --- a/services/net/java/android/net/shared/ParcelableUtil.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.shared; - -import android.annotation.NonNull; - -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.Collection; -import java.util.function.Function; - -/** - * Utility methods to help convert to/from stable parcelables. - * @hide - */ -public final class ParcelableUtil { - // Below methods could be implemented easily with streams, but streams are frowned upon in - // frameworks code. - - /** - * Convert a list of BaseType items to an array of ParcelableType items using the specified - * converter function. - */ - public static <ParcelableType, BaseType> ParcelableType[] toParcelableArray( - @NonNull Collection<BaseType> base, - @NonNull Function<BaseType, ParcelableType> conv, - @NonNull Class<ParcelableType> parcelClass) { - final ParcelableType[] out = (ParcelableType[]) Array.newInstance(parcelClass, base.size()); - int i = 0; - for (BaseType b : base) { - out[i] = conv.apply(b); - i++; - } - return out; - } - - /** - * Convert an array of ParcelableType items to a list of BaseType items using the specified - * converter function. - */ - public static <ParcelableType, BaseType> ArrayList<BaseType> fromParcelableArray( - @NonNull ParcelableType[] parceled, @NonNull Function<ParcelableType, BaseType> conv) { - final ArrayList<BaseType> out = new ArrayList<>(parceled.length); - for (ParcelableType t : parceled) { - out.add(conv.apply(t)); - } - return out; - } -} diff --git a/services/net/java/android/net/shared/PrivateDnsConfig.java b/services/net/java/android/net/shared/PrivateDnsConfig.java deleted file mode 100644 index c7dc5306b51b..000000000000 --- a/services/net/java/android/net/shared/PrivateDnsConfig.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.shared; - -import static android.net.shared.ParcelableUtil.fromParcelableArray; -import static android.net.shared.ParcelableUtil.toParcelableArray; - -import android.net.PrivateDnsConfigParcel; -import android.text.TextUtils; - -import java.net.InetAddress; -import java.util.Arrays; - -/** @hide */ -public class PrivateDnsConfig { - public final boolean useTls; - public final String hostname; - public final InetAddress[] ips; - - public PrivateDnsConfig() { - this(false); - } - - public PrivateDnsConfig(boolean useTls) { - this.useTls = useTls; - this.hostname = ""; - this.ips = new InetAddress[0]; - } - - public PrivateDnsConfig(String hostname, InetAddress[] ips) { - this.useTls = !TextUtils.isEmpty(hostname); - this.hostname = useTls ? hostname : ""; - this.ips = (ips != null) ? ips : new InetAddress[0]; - } - - public PrivateDnsConfig(PrivateDnsConfig cfg) { - useTls = cfg.useTls; - hostname = cfg.hostname; - ips = cfg.ips; - } - - /** - * Indicates whether this is a strict mode private DNS configuration. - */ - public boolean inStrictMode() { - return useTls && !TextUtils.isEmpty(hostname); - } - - @Override - public String toString() { - return PrivateDnsConfig.class.getSimpleName() - + "{" + useTls + ":" + hostname + "/" + Arrays.toString(ips) + "}"; - } - - /** - * Create a stable AIDL-compatible parcel from the current instance. - */ - public PrivateDnsConfigParcel toParcel() { - final PrivateDnsConfigParcel parcel = new PrivateDnsConfigParcel(); - parcel.hostname = hostname; - parcel.ips = toParcelableArray( - Arrays.asList(ips), IpConfigurationParcelableUtil::parcelAddress, String.class); - - return parcel; - } - - /** - * Build a configuration from a stable AIDL-compatible parcel. - */ - public static PrivateDnsConfig fromParcel(PrivateDnsConfigParcel parcel) { - InetAddress[] ips = new InetAddress[parcel.ips.length]; - ips = fromParcelableArray(parcel.ips, IpConfigurationParcelableUtil::unparcelAddress) - .toArray(ips); - return new PrivateDnsConfig(parcel.hostname, ips); - } -} diff --git a/services/net/java/android/net/shared/ProvisioningConfiguration.java b/services/net/java/android/net/shared/ProvisioningConfiguration.java deleted file mode 100644 index 6f9c2949d864..000000000000 --- a/services/net/java/android/net/shared/ProvisioningConfiguration.java +++ /dev/null @@ -1,312 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.shared; - -import android.annotation.Nullable; -import android.net.INetd; -import android.net.Network; -import android.net.ProvisioningConfigurationParcelable; -import android.net.StaticIpConfiguration; -import android.net.apf.ApfCapabilities; -import android.net.ip.IIpClient; - -import java.util.Objects; -import java.util.StringJoiner; - -/** - * This class encapsulates parameters to be passed to - * IpClient#startProvisioning(). A defensive copy is made by IpClient - * and the values specified herein are in force until IpClient#stop() - * is called. - * - * Example use: - * - * final ProvisioningConfiguration config = - * new ProvisioningConfiguration.Builder() - * .withPreDhcpAction() - * .withProvisioningTimeoutMs(36 * 1000) - * .build(); - * mIpClient.startProvisioning(config.toStableParcelable()); - * ... - * mIpClient.stop(); - * - * The specified provisioning configuration will only be active until - * IIpClient#stop() is called. Future calls to IIpClient#startProvisioning() - * must specify the configuration again. - * @hide - */ -public class ProvisioningConfiguration { - // TODO: Delete this default timeout once those callers that care are - // fixed to pass in their preferred timeout. - // - // We pick 36 seconds so we can send DHCP requests at - // - // t=0, t=2, t=6, t=14, t=30 - // - // allowing for 10% jitter. - private static final int DEFAULT_TIMEOUT_MS = 36 * 1000; - - /** - * Builder to create a {@link ProvisioningConfiguration}. - */ - public static class Builder { - protected ProvisioningConfiguration mConfig = new ProvisioningConfiguration(); - - /** - * Specify that the configuration should not enable IPv4. It is enabled by default. - */ - public Builder withoutIPv4() { - mConfig.mEnableIPv4 = false; - return this; - } - - /** - * Specify that the configuration should not enable IPv6. It is enabled by default. - */ - public Builder withoutIPv6() { - mConfig.mEnableIPv6 = false; - return this; - } - - /** - * Specify that the configuration should not use a MultinetworkPolicyTracker. It is used - * by default. - */ - public Builder withoutMultinetworkPolicyTracker() { - mConfig.mUsingMultinetworkPolicyTracker = false; - return this; - } - - /** - * Specify that the configuration should not use a IpReachabilityMonitor. It is used by - * default. - */ - public Builder withoutIpReachabilityMonitor() { - mConfig.mUsingIpReachabilityMonitor = false; - return this; - } - - /** - * Identical to {@link #withPreDhcpAction(int)}, using a default timeout. - * @see #withPreDhcpAction(int) - */ - public Builder withPreDhcpAction() { - mConfig.mRequestedPreDhcpActionMs = DEFAULT_TIMEOUT_MS; - return this; - } - - /** - * Specify that {@link IpClientCallbacks#onPreDhcpAction()} should be called. Clients must - * call {@link IIpClient#completedPreDhcpAction()} when the callback called. This behavior - * is disabled by default. - * @param dhcpActionTimeoutMs Timeout for clients to call completedPreDhcpAction(). - */ - public Builder withPreDhcpAction(int dhcpActionTimeoutMs) { - mConfig.mRequestedPreDhcpActionMs = dhcpActionTimeoutMs; - return this; - } - - /** - * Specify the initial provisioning configuration. - */ - public Builder withInitialConfiguration(InitialConfiguration initialConfig) { - mConfig.mInitialConfig = initialConfig; - return this; - } - - /** - * Specify a static configuration for provisioning. - */ - public Builder withStaticConfiguration(StaticIpConfiguration staticConfig) { - mConfig.mStaticIpConfig = staticConfig; - return this; - } - - /** - * Specify ApfCapabilities. - */ - public Builder withApfCapabilities(ApfCapabilities apfCapabilities) { - mConfig.mApfCapabilities = apfCapabilities; - return this; - } - - /** - * Specify the timeout to use for provisioning. - */ - public Builder withProvisioningTimeoutMs(int timeoutMs) { - mConfig.mProvisioningTimeoutMs = timeoutMs; - return this; - } - - /** - * Specify that IPv6 address generation should use a random MAC address. - */ - public Builder withRandomMacAddress() { - mConfig.mIPv6AddrGenMode = INetd.IPV6_ADDR_GEN_MODE_EUI64; - return this; - } - - /** - * Specify that IPv6 address generation should use a stable MAC address. - */ - public Builder withStableMacAddress() { - mConfig.mIPv6AddrGenMode = INetd.IPV6_ADDR_GEN_MODE_STABLE_PRIVACY; - return this; - } - - /** - * Specify the network to use for provisioning. - */ - public Builder withNetwork(Network network) { - mConfig.mNetwork = network; - return this; - } - - /** - * Specify the display name that the IpClient should use. - */ - public Builder withDisplayName(String displayName) { - mConfig.mDisplayName = displayName; - return this; - } - - /** - * Build the configuration using previously specified parameters. - */ - public ProvisioningConfiguration build() { - return new ProvisioningConfiguration(mConfig); - } - } - - public boolean mEnableIPv4 = true; - public boolean mEnableIPv6 = true; - public boolean mUsingMultinetworkPolicyTracker = true; - public boolean mUsingIpReachabilityMonitor = true; - public int mRequestedPreDhcpActionMs; - public InitialConfiguration mInitialConfig; - public StaticIpConfiguration mStaticIpConfig; - public ApfCapabilities mApfCapabilities; - public int mProvisioningTimeoutMs = DEFAULT_TIMEOUT_MS; - public int mIPv6AddrGenMode = INetd.IPV6_ADDR_GEN_MODE_STABLE_PRIVACY; - public Network mNetwork = null; - public String mDisplayName = null; - - public ProvisioningConfiguration() {} // used by Builder - - public ProvisioningConfiguration(ProvisioningConfiguration other) { - mEnableIPv4 = other.mEnableIPv4; - mEnableIPv6 = other.mEnableIPv6; - mUsingMultinetworkPolicyTracker = other.mUsingMultinetworkPolicyTracker; - mUsingIpReachabilityMonitor = other.mUsingIpReachabilityMonitor; - mRequestedPreDhcpActionMs = other.mRequestedPreDhcpActionMs; - mInitialConfig = InitialConfiguration.copy(other.mInitialConfig); - mStaticIpConfig = other.mStaticIpConfig == null - ? null - : new StaticIpConfiguration(other.mStaticIpConfig); - mApfCapabilities = other.mApfCapabilities; - mProvisioningTimeoutMs = other.mProvisioningTimeoutMs; - mIPv6AddrGenMode = other.mIPv6AddrGenMode; - mNetwork = other.mNetwork; - mDisplayName = other.mDisplayName; - } - - /** - * Create a ProvisioningConfigurationParcelable from this ProvisioningConfiguration. - */ - public ProvisioningConfigurationParcelable toStableParcelable() { - final ProvisioningConfigurationParcelable p = new ProvisioningConfigurationParcelable(); - p.enableIPv4 = mEnableIPv4; - p.enableIPv6 = mEnableIPv6; - p.usingMultinetworkPolicyTracker = mUsingMultinetworkPolicyTracker; - p.usingIpReachabilityMonitor = mUsingIpReachabilityMonitor; - p.requestedPreDhcpActionMs = mRequestedPreDhcpActionMs; - p.initialConfig = mInitialConfig == null ? null : mInitialConfig.toStableParcelable(); - p.staticIpConfig = mStaticIpConfig == null - ? null - : new StaticIpConfiguration(mStaticIpConfig); - p.apfCapabilities = mApfCapabilities; // ApfCapabilities is immutable - p.provisioningTimeoutMs = mProvisioningTimeoutMs; - p.ipv6AddrGenMode = mIPv6AddrGenMode; - p.network = mNetwork; - p.displayName = mDisplayName; - return p; - } - - /** - * Create a ProvisioningConfiguration from a ProvisioningConfigurationParcelable. - */ - public static ProvisioningConfiguration fromStableParcelable( - @Nullable ProvisioningConfigurationParcelable p) { - if (p == null) return null; - final ProvisioningConfiguration config = new ProvisioningConfiguration(); - config.mEnableIPv4 = p.enableIPv4; - config.mEnableIPv6 = p.enableIPv6; - config.mUsingMultinetworkPolicyTracker = p.usingMultinetworkPolicyTracker; - config.mUsingIpReachabilityMonitor = p.usingIpReachabilityMonitor; - config.mRequestedPreDhcpActionMs = p.requestedPreDhcpActionMs; - config.mInitialConfig = InitialConfiguration.fromStableParcelable(p.initialConfig); - config.mStaticIpConfig = p.staticIpConfig == null - ? null - : new StaticIpConfiguration(p.staticIpConfig); - config.mApfCapabilities = p.apfCapabilities; // ApfCapabilities is immutable - config.mProvisioningTimeoutMs = p.provisioningTimeoutMs; - config.mIPv6AddrGenMode = p.ipv6AddrGenMode; - config.mNetwork = p.network; - config.mDisplayName = p.displayName; - return config; - } - - @Override - public String toString() { - return new StringJoiner(", ", getClass().getSimpleName() + "{", "}") - .add("mEnableIPv4: " + mEnableIPv4) - .add("mEnableIPv6: " + mEnableIPv6) - .add("mUsingMultinetworkPolicyTracker: " + mUsingMultinetworkPolicyTracker) - .add("mUsingIpReachabilityMonitor: " + mUsingIpReachabilityMonitor) - .add("mRequestedPreDhcpActionMs: " + mRequestedPreDhcpActionMs) - .add("mInitialConfig: " + mInitialConfig) - .add("mStaticIpConfig: " + mStaticIpConfig) - .add("mApfCapabilities: " + mApfCapabilities) - .add("mProvisioningTimeoutMs: " + mProvisioningTimeoutMs) - .add("mIPv6AddrGenMode: " + mIPv6AddrGenMode) - .add("mNetwork: " + mNetwork) - .add("mDisplayName: " + mDisplayName) - .toString(); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof ProvisioningConfiguration)) return false; - final ProvisioningConfiguration other = (ProvisioningConfiguration) obj; - return mEnableIPv4 == other.mEnableIPv4 - && mEnableIPv6 == other.mEnableIPv6 - && mUsingMultinetworkPolicyTracker == other.mUsingMultinetworkPolicyTracker - && mUsingIpReachabilityMonitor == other.mUsingIpReachabilityMonitor - && mRequestedPreDhcpActionMs == other.mRequestedPreDhcpActionMs - && Objects.equals(mInitialConfig, other.mInitialConfig) - && Objects.equals(mStaticIpConfig, other.mStaticIpConfig) - && Objects.equals(mApfCapabilities, other.mApfCapabilities) - && mProvisioningTimeoutMs == other.mProvisioningTimeoutMs - && mIPv6AddrGenMode == other.mIPv6AddrGenMode - && Objects.equals(mNetwork, other.mNetwork) - && Objects.equals(mDisplayName, other.mDisplayName); - } - - public boolean isValid() { - return (mInitialConfig == null) || mInitialConfig.isValid(); - } -} diff --git a/services/net/java/android/net/util/InterfaceParams.java b/services/net/java/android/net/util/InterfaceParams.java deleted file mode 100644 index f6bb87369cad..000000000000 --- a/services/net/java/android/net/util/InterfaceParams.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.util; - -import static com.android.internal.util.Preconditions.checkArgument; - -import android.net.MacAddress; -import android.text.TextUtils; - -import java.net.NetworkInterface; -import java.net.SocketException; - - -/** - * Encapsulate the interface parameters common to IpClient/IpServer components. - * - * Basically all java.net.NetworkInterface methods throw Exceptions. IpClient - * and IpServer (sub)components need most or all of this information at some - * point during their lifecycles, so pass only this simplified object around - * which can be created once when IpClient/IpServer are told to start. - * - * @hide - */ -public class InterfaceParams { - public final String name; - public final int index; - public final MacAddress macAddr; - public final int defaultMtu; - - // TODO: move the below to NetworkStackConstants when this class is moved to the NetworkStack. - private static final int ETHER_MTU = 1500; - private static final int IPV6_MIN_MTU = 1280; - - - public static InterfaceParams getByName(String name) { - final NetworkInterface netif = getNetworkInterfaceByName(name); - if (netif == null) return null; - - // Not all interfaces have MAC addresses, e.g. rmnet_data0. - final MacAddress macAddr = getMacAddress(netif); - - try { - return new InterfaceParams(name, netif.getIndex(), macAddr, netif.getMTU()); - } catch (IllegalArgumentException|SocketException e) { - return null; - } - } - - public InterfaceParams(String name, int index, MacAddress macAddr) { - this(name, index, macAddr, ETHER_MTU); - } - - public InterfaceParams(String name, int index, MacAddress macAddr, int defaultMtu) { - checkArgument((!TextUtils.isEmpty(name)), "impossible interface name"); - checkArgument((index > 0), "invalid interface index"); - this.name = name; - this.index = index; - this.macAddr = (macAddr != null) ? macAddr : MacAddress.fromBytes(new byte[] { - 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 }); - this.defaultMtu = (defaultMtu > IPV6_MIN_MTU) ? defaultMtu : IPV6_MIN_MTU; - } - - @Override - public String toString() { - return String.format("%s/%d/%s/%d", name, index, macAddr, defaultMtu); - } - - private static NetworkInterface getNetworkInterfaceByName(String name) { - try { - return NetworkInterface.getByName(name); - } catch (NullPointerException|SocketException e) { - return null; - } - } - - private static MacAddress getMacAddress(NetworkInterface netif) { - try { - return MacAddress.fromBytes(netif.getHardwareAddress()); - } catch (IllegalArgumentException|NullPointerException|SocketException e) { - return null; - } - } -} diff --git a/services/net/java/android/net/util/SharedLog.java b/services/net/java/android/net/util/SharedLog.java deleted file mode 100644 index 2cdb2b04f8f7..000000000000 --- a/services/net/java/android/net/util/SharedLog.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.net.util; - -import android.annotation.NonNull; -import android.annotation.Nullable; -import android.text.TextUtils; -import android.util.LocalLog; -import android.util.Log; - -import java.io.FileDescriptor; -import java.io.PrintWriter; -import java.util.StringJoiner; - - -/** - * Class to centralize logging functionality for tethering. - * - * All access to class methods other than dump() must be on the same thread. - * - * TODO: this is a copy of SharedLog in the NetworkStack. Remove after Tethering is migrated. - * @hide - */ -public class SharedLog { - private static final int DEFAULT_MAX_RECORDS = 500; - private static final String COMPONENT_DELIMITER = "."; - - private enum Category { - NONE, - ERROR, - MARK, - WARN, - }; - - private final LocalLog mLocalLog; - // The tag to use for output to the system log. This is not output to the - // LocalLog because that would be redundant. - private final String mTag; - // The component (or subcomponent) of a system that is sharing this log. - // This can grow in depth if components call forSubComponent() to obtain - // their SharedLog instance. The tag is not included in the component for - // brevity. - private final String mComponent; - - public SharedLog(String tag) { - this(DEFAULT_MAX_RECORDS, tag); - } - - public SharedLog(int maxRecords, String tag) { - this(new LocalLog(maxRecords), tag, tag); - } - - private SharedLog(LocalLog localLog, String tag, String component) { - mLocalLog = localLog; - mTag = tag; - mComponent = component; - } - - public String getTag() { - return mTag; - } - - /** - * Create a SharedLog based on this log with an additional component prefix on each logged line. - */ - public SharedLog forSubComponent(String component) { - if (!isRootLogInstance()) { - component = mComponent + COMPONENT_DELIMITER + component; - } - return new SharedLog(mLocalLog, mTag, component); - } - - /** - * Dump the contents of this log. - * - * <p>This method may be called on any thread. - */ - public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { - mLocalLog.readOnlyLocalLog().dump(fd, writer, args); - } - - ////// - // Methods that both log an entry and emit it to the system log. - ////// - - /** - * Log an error due to an exception. This does not include the exception stacktrace. - * - * <p>The log entry will be also added to the system log. - * @see #e(String, Throwable) - */ - public void e(Exception e) { - Log.e(mTag, record(Category.ERROR, e.toString())); - } - - /** - * Log an error message. - * - * <p>The log entry will be also added to the system log. - */ - public void e(String msg) { - Log.e(mTag, record(Category.ERROR, msg)); - } - - /** - * Log an error due to an exception, with the exception stacktrace if provided. - * - * <p>The error and exception message appear in the shared log, but the stacktrace is only - * logged in general log output (logcat). The log entry will be also added to the system log. - */ - public void e(@NonNull String msg, @Nullable Throwable exception) { - if (exception == null) { - e(msg); - return; - } - Log.e(mTag, record(Category.ERROR, msg + ": " + exception.getMessage()), exception); - } - - /** - * Log an informational message. - * - * <p>The log entry will be also added to the system log. - */ - public void i(String msg) { - Log.i(mTag, record(Category.NONE, msg)); - } - - /** - * Log a warning message. - * - * <p>The log entry will be also added to the system log. - */ - public void w(String msg) { - Log.w(mTag, record(Category.WARN, msg)); - } - - ////// - // Methods that only log an entry (and do NOT emit to the system log). - ////// - - /** - * Log a general message to be only included in the in-memory log. - * - * <p>The log entry will *not* be added to the system log. - */ - public void log(String msg) { - record(Category.NONE, msg); - } - - /** - * Log a general, formatted message to be only included in the in-memory log. - * - * <p>The log entry will *not* be added to the system log. - * @see String#format(String, Object...) - */ - public void logf(String fmt, Object... args) { - log(String.format(fmt, args)); - } - - /** - * Log a message with MARK level. - * - * <p>The log entry will *not* be added to the system log. - */ - public void mark(String msg) { - record(Category.MARK, msg); - } - - private String record(Category category, String msg) { - final String entry = logLine(category, msg); - mLocalLog.log(entry); - return entry; - } - - private String logLine(Category category, String msg) { - final StringJoiner sj = new StringJoiner(" "); - if (!isRootLogInstance()) sj.add("[" + mComponent + "]"); - if (category != Category.NONE) sj.add(category.toString()); - return sj.add(msg).toString(); - } - - // Check whether this SharedLog instance is nominally the top level in - // a potential hierarchy of shared logs (the root of a tree), - // or is a subcomponent within the hierarchy. - private boolean isRootLogInstance() { - return TextUtils.isEmpty(mComponent) || mComponent.equals(mTag); - } -} |