Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6af597d5 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN Committed by Gerrit Code Review
Browse files

Merge "Add hidden utils and constants to NetworkStack"

parents 3c361560 abeaaf74
Loading
Loading
Loading
Loading
+24 −6
Original line number Diff line number Diff line
@@ -26,11 +26,6 @@ import static android.system.OsConstants.IPPROTO_ICMPV6;
import static android.system.OsConstants.IPPROTO_UDP;
import static android.system.OsConstants.SOCK_RAW;

import static com.android.internal.util.BitUtils.bytesToBEInt;
import static com.android.internal.util.BitUtils.getUint16;
import static com.android.internal.util.BitUtils.getUint32;
import static com.android.internal.util.BitUtils.getUint8;
import static com.android.internal.util.BitUtils.uint32;
import static com.android.server.util.NetworkStackConstants.ICMPV6_ECHO_REQUEST_TYPE;
import static com.android.server.util.NetworkStackConstants.ICMPV6_NEIGHBOR_ADVERTISEMENT;
import static com.android.server.util.NetworkStackConstants.ICMPV6_ROUTER_ADVERTISEMENT;
@@ -1586,6 +1581,29 @@ public class ApfFilter {
    // TODO: move to android.net.NetworkUtils
    @VisibleForTesting
    public static int ipv4BroadcastAddress(byte[] addrBytes, int prefixLength) {
        return bytesToBEInt(addrBytes) | (int) (uint32(-1) >>> prefixLength);
        return bytesToBEInt(addrBytes) | (int) (Integer.toUnsignedLong(-1) >>> prefixLength);
    }

    private static int uint8(byte b) {
        return b & 0xff;
    }

    private static int getUint16(ByteBuffer buffer, int position) {
        return buffer.getShort(position) & 0xffff;
    }

    private static long getUint32(ByteBuffer buffer, int position) {
        return Integer.toUnsignedLong(buffer.getInt(position));
    }

    private static int getUint8(ByteBuffer buffer, int position) {
        return uint8(buffer.get(position));
    }

    private static int bytesToBEInt(byte[] bytes) {
        return (uint8(bytes[0]) << 24)
                + (uint8(bytes[1]) << 16)
                + (uint8(bytes[2]) << 8)
                + (uint8(bytes[3]));
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ import static android.system.OsConstants.SO_BROADCAST;
import static android.system.OsConstants.SO_RCVBUF;
import static android.system.OsConstants.SO_REUSEADDR;

import static com.android.server.util.NetworkStackConstants.IPV4_ADDR_ANY;

import android.content.Context;
import android.net.DhcpResults;
import android.net.NetworkUtils;
@@ -328,7 +330,7 @@ public class DhcpClient extends StateMachine {
            Os.setsockoptInt(mUdpSock, SOL_SOCKET, SO_REUSEADDR, 1);
            Os.setsockoptInt(mUdpSock, SOL_SOCKET, SO_BROADCAST, 1);
            Os.setsockoptInt(mUdpSock, SOL_SOCKET, SO_RCVBUF, 0);
            Os.bind(mUdpSock, Inet4Address.ANY, DhcpPacket.DHCP_CLIENT);
            Os.bind(mUdpSock, IPV4_ADDR_ANY, DhcpPacket.DHCP_CLIENT);
        } catch(SocketException|ErrnoException e) {
            Log.e(TAG, "Error creating UDP socket", e);
            return false;
+2 −1
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.net.shared.Inet4AddressUtils.inet4AddressToIntHTH;
import static android.net.shared.Inet4AddressUtils.intToInet4AddressHTH;
import static android.net.shared.Inet4AddressUtils.prefixLengthToV4NetmaskIntHTH;

import static com.android.server.util.NetworkStackConstants.IPV4_ADDR_ANY;
import static com.android.server.util.NetworkStackConstants.IPV4_ADDR_BITS;

import static java.lang.Math.min;
@@ -201,7 +202,7 @@ class DhcpLeaseRepository {

    private static boolean isIpAddrOutsidePrefix(@NonNull IpPrefix prefix,
            @Nullable Inet4Address addr) {
        return addr != null && !addr.equals(Inet4Address.ANY) && !prefix.contains(addr);
        return addr != null && !addr.equals(IPV4_ADDR_ANY) && !prefix.contains(addr);
    }

    @Nullable
+7 −4
Original line number Diff line number Diff line
package android.net.dhcp;

import static com.android.server.util.NetworkStackConstants.IPV4_ADDR_ALL;
import static com.android.server.util.NetworkStackConstants.IPV4_ADDR_ANY;

import android.annotation.Nullable;
import android.net.DhcpResults;
import android.net.LinkAddress;
@@ -43,8 +46,8 @@ public abstract class DhcpPacket {
    public static final int MINIMUM_LEASE = 60;
    public static final int INFINITE_LEASE = (int) 0xffffffff;

    public static final Inet4Address INADDR_ANY = (Inet4Address) Inet4Address.ANY;
    public static final Inet4Address INADDR_BROADCAST = (Inet4Address) Inet4Address.ALL;
    public static final Inet4Address INADDR_ANY = IPV4_ADDR_ANY;
    public static final Inet4Address INADDR_BROADCAST = IPV4_ADDR_ALL;
    public static final byte[] ETHER_BROADCAST = new byte[] {
            (byte) 0xff, (byte) 0xff, (byte) 0xff,
            (byte) 0xff, (byte) 0xff, (byte) 0xff,
@@ -1212,9 +1215,9 @@ public abstract class DhcpPacket {
     */
    public DhcpResults toDhcpResults() {
        Inet4Address ipAddress = mYourIp;
        if (ipAddress.equals(Inet4Address.ANY)) {
        if (ipAddress.equals(IPV4_ADDR_ANY)) {
            ipAddress = mClientIp;
            if (ipAddress.equals(Inet4Address.ANY)) {
            if (ipAddress.equals(IPV4_ADDR_ANY)) {
                return null;
            }
        }
+6 −4
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ import static android.system.OsConstants.SO_BROADCAST;
import static android.system.OsConstants.SO_REUSEADDR;

import static com.android.server.util.NetworkStackConstants.INFINITE_LEASE;
import static com.android.server.util.NetworkStackConstants.IPV4_ADDR_ALL;
import static com.android.server.util.NetworkStackConstants.IPV4_ADDR_ANY;
import static com.android.server.util.PermissionUtil.checkNetworkStackCallingPermission;

import static java.lang.Integer.toUnsignedLong;
@@ -434,7 +436,7 @@ public class DhcpServer extends IDhcpServer.Stub {
        if (!isEmpty(request.mRelayIp)) {
            return request.mRelayIp;
        } else if (broadcastFlag) {
            return (Inet4Address) Inet4Address.ALL;
            return IPV4_ADDR_ALL;
        } else if (!isEmpty(request.mClientIp)) {
            return request.mClientIp;
        } else {
@@ -517,7 +519,7 @@ public class DhcpServer extends IDhcpServer.Stub {
                request.mRelayIp, request.mClientMac, true /* broadcast */, message);

        final Inet4Address dst = isEmpty(request.mRelayIp)
                ? (Inet4Address) Inet4Address.ALL
                ? IPV4_ADDR_ALL
                : request.mRelayIp;
        return transmitPacket(nakPacket, DhcpNakPacket.class.getSimpleName(), dst);
    }
@@ -598,7 +600,7 @@ public class DhcpServer extends IDhcpServer.Stub {
    }

    private static boolean isEmpty(@Nullable Inet4Address address) {
        return address == null || Inet4Address.ANY.equals(address);
        return address == null || IPV4_ADDR_ANY.equals(address);
    }

    private class PacketListener extends DhcpPacketListener {
@@ -632,7 +634,7 @@ public class DhcpServer extends IDhcpServer.Stub {
                SocketUtils.bindSocketToInterface(mSocket, mIfName);
                Os.setsockoptInt(mSocket, SOL_SOCKET, SO_REUSEADDR, 1);
                Os.setsockoptInt(mSocket, SOL_SOCKET, SO_BROADCAST, 1);
                Os.bind(mSocket, Inet4Address.ANY, DHCP_SERVER);
                Os.bind(mSocket, IPV4_ADDR_ANY, DHCP_SERVER);

                return mSocket;
            } catch (IOException | ErrnoException e) {
Loading