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

Commit 87d8e82e authored by Chalard Jean's avatar Chalard Jean Committed by android-build-merger
Browse files

Merge changes I09167532,I7df77a99,I67797b3f,Ic27e706f am: 796c9446

am: 18e166a1

Change-Id: I174716764b2d6e441547d4fbad4043cdbf0f8ad9
parents a78fb89c 18e166a1
Loading
Loading
Loading
Loading
+49 −0
Original line number Original line Diff line number Diff line
@@ -28,6 +28,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
import java.util.Objects;
import java.util.Objects;
import java.util.StringJoiner;


/**
/**
 * A POD object to represent attributes of a single L2 network entry.
 * A POD object to represent attributes of a single L2 network entry.
@@ -207,4 +208,52 @@ public class NetworkAttributes {
    public int hashCode() {
    public int hashCode() {
        return Objects.hash(assignedV4Address, groupHint, dnsAddresses, mtu);
        return Objects.hash(assignedV4Address, groupHint, dnsAddresses, mtu);
    }
    }

    /** Pretty print */
    @Override
    public String toString() {
        final StringJoiner resultJoiner = new StringJoiner(" ", "{", "}");
        final ArrayList<String> nullFields = new ArrayList<>();

        if (null != assignedV4Address) {
            resultJoiner.add("assignedV4Addr :");
            resultJoiner.add(assignedV4Address.toString());
        } else {
            nullFields.add("assignedV4Addr");
        }

        if (null != groupHint) {
            resultJoiner.add("groupHint :");
            resultJoiner.add(groupHint);
        } else {
            nullFields.add("groupHint");
        }

        if (null != dnsAddresses) {
            resultJoiner.add("dnsAddr : [");
            for (final InetAddress addr : dnsAddresses) {
                resultJoiner.add(addr.getHostAddress());
            }
            resultJoiner.add("]");
        } else {
            nullFields.add("dnsAddr");
        }

        if (null != mtu) {
            resultJoiner.add("mtu :");
            resultJoiner.add(mtu.toString());
        } else {
            nullFields.add("mtu");
        }

        if (!nullFields.isEmpty()) {
            resultJoiner.add("; Null fields : [");
            for (final String field : nullFields) {
                resultJoiner.add(field);
            }
            resultJoiner.add("]");
        }

        return resultJoiner.toString();
    }
}
}
+15 −0
Original line number Original line Diff line number Diff line
@@ -128,4 +128,19 @@ public class SameL3NetworkResponse {
    public int hashCode() {
    public int hashCode() {
        return Objects.hash(l2Key1, l2Key2, confidence);
        return Objects.hash(l2Key1, l2Key2, confidence);
    }
    }

    @Override
    /** Pretty print */
    public String toString() {
        switch (getNetworkSameness()) {
            case NETWORK_SAME:
                return "\"" + l2Key1 + "\" same L3 network as \"" + l2Key2 + "\"";
            case NETWORK_DIFFERENT:
                return "\"" + l2Key1 + "\" different L3 network from \"" + l2Key2 + "\"";
            case NETWORK_NEVER_CONNECTED:
                return "\"" + l2Key1 + "\" can't be tested against \"" + l2Key2 + "\"";
            default:
                return "Buggy sameness value ? \"" + l2Key1 + "\", \"" + l2Key2 + "\"";
        }
    }
}
}
+12 −0
Original line number Original line Diff line number Diff line
@@ -26,6 +26,8 @@ import android.annotation.NonNull;
public class Status {
public class Status {
    public static final int SUCCESS = 0;
    public static final int SUCCESS = 0;


    public static final int ERROR_DATABASE_CANNOT_BE_OPENED = -1;

    public final int resultCode;
    public final int resultCode;


    public Status(final int resultCode) {
    public Status(final int resultCode) {
@@ -47,4 +49,14 @@ public class Status {
    public boolean isSuccess() {
    public boolean isSuccess() {
        return SUCCESS == resultCode;
        return SUCCESS == resultCode;
    }
    }

    /** Pretty print */
    @Override
    public String toString() {
        switch (resultCode) {
            case SUCCESS: return "SUCCESS";
            case ERROR_DATABASE_CANNOT_BE_OPENED: return "DATABASE CANNOT BE OPENED";
            default: return "Unknown value ?!";
        }
    }
}
}
+44 −0
Original line number Original line Diff line number Diff line
/*
 * 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.ipmemorystore;

import android.annotation.NonNull;

/** {@hide} */
public class Utils {
    /** Pretty print */
    public static String blobToString(final Blob blob) {
        final StringBuilder sb = new StringBuilder("Blob : [");
        if (blob.data.length <= 24) {
            appendByteArray(sb, blob.data, 0, blob.data.length);
        } else {
            appendByteArray(sb, blob.data, 0, 16);
            sb.append("...");
            appendByteArray(sb, blob.data, blob.data.length - 8, blob.data.length);
        }
        sb.append("]");
        return sb.toString();
    }

    // Adds the hex representation of the array between the specified indices (inclusive, exclusive)
    private static void appendByteArray(@NonNull final StringBuilder sb, @NonNull final byte[] ar,
            final int from, final int to) {
        for (int i = from; i < to; ++i) {
            sb.append(String.format("%02X", ar[i]));
        }
    }
}
+143 −0
Original line number Original line Diff line number Diff line
/*
 * 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 com.android.server.net.ipmemorystore;

import android.annotation.NonNull;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Encapsulating class for using the SQLite database backing the memory store.
 *
 * This class groups together the contracts and the SQLite helper used to
 * use the database.
 *
 * @hide
 */
public class IpMemoryStoreDatabase {
    /**
     * Contract class for the Network Attributes table.
     */
    public static class NetworkAttributesContract {
        public static final String TABLENAME = "NetworkAttributes";

        public static final String COLNAME_L2KEY = "l2Key";
        public static final String COLTYPE_L2KEY = "TEXT NOT NULL";

        public static final String COLNAME_EXPIRYDATE = "expiryDate";
        // Milliseconds since the Epoch, in true Java style
        public static final String COLTYPE_EXPIRYDATE = "BIGINT";

        public static final String COLNAME_ASSIGNEDV4ADDRESS = "assignedV4Address";
        public static final String COLTYPE_ASSIGNEDV4ADDRESS = "INTEGER";

        // Please note that the group hint is only a *hint*, hence its name. The client can offer
        // this information to nudge the grouping in the decision it thinks is right, but it can't
        // decide for the memory store what is the same L3 network.
        public static final String COLNAME_GROUPHINT = "groupHint";
        public static final String COLTYPE_GROUPHINT = "TEXT";

        public static final String COLNAME_DNSADDRESSES = "dnsAddresses";
        // Stored in marshalled form as is
        public static final String COLTYPE_DNSADDRESSES = "BLOB";

        public static final String COLNAME_MTU = "mtu";
        public static final String COLTYPE_MTU = "INTEGER";

        public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
                + TABLENAME                 + " ("
                + COLNAME_L2KEY             + " " + COLTYPE_L2KEY + " PRIMARY KEY NOT NULL, "
                + COLNAME_EXPIRYDATE        + " " + COLTYPE_EXPIRYDATE        + ", "
                + COLNAME_ASSIGNEDV4ADDRESS + " " + COLTYPE_ASSIGNEDV4ADDRESS + ", "
                + COLNAME_GROUPHINT         + " " + COLTYPE_GROUPHINT         + ", "
                + COLNAME_DNSADDRESSES      + " " + COLTYPE_DNSADDRESSES      + ", "
                + COLNAME_MTU               + " " + COLTYPE_MTU               + ")";
        public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLENAME;
    }

    /**
     * Contract class for the Private Data table.
     */
    public static class PrivateDataContract {
        public static final String TABLENAME = "PrivateData";

        public static final String COLNAME_L2KEY = "l2Key";
        public static final String COLTYPE_L2KEY = "TEXT NOT NULL";

        public static final String COLNAME_CLIENT = "client";
        public static final String COLTYPE_CLIENT = "TEXT NOT NULL";

        public static final String COLNAME_DATANAME = "dataName";
        public static final String COLTYPE_DATANAME = "TEXT NOT NULL";

        public static final String COLNAME_DATA = "data";
        public static final String COLTYPE_DATA = "BLOB NOT NULL";

        public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
                + TABLENAME        + " ("
                + COLNAME_L2KEY    + " " + COLTYPE_L2KEY    + ", "
                + COLNAME_CLIENT   + " " + COLTYPE_CLIENT   + ", "
                + COLNAME_DATANAME + " " + COLTYPE_DATANAME + ", "
                + COLNAME_DATA     + " " + COLTYPE_DATA     + ", "
                + "PRIMARY KEY ("
                + COLNAME_L2KEY    + ", "
                + COLNAME_CLIENT   + ", "
                + COLNAME_DATANAME + "))";
        public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLENAME;
    }

    // To save memory when the DB is not used, close it after 30s of inactivity. This is
    // determined manually based on what feels right.
    private static final long IDLE_CONNECTION_TIMEOUT_MS = 30_000;

    /** The SQLite DB helper */
    public static class DbHelper extends SQLiteOpenHelper {
        // Update this whenever changing the schema.
        private static final int SCHEMA_VERSION = 1;
        private static final String DATABASE_FILENAME = "IpMemoryStore.db";

        public DbHelper(@NonNull final Context context) {
            super(context, DATABASE_FILENAME, null, SCHEMA_VERSION);
            setIdleConnectionTimeout(IDLE_CONNECTION_TIMEOUT_MS);
        }

        /** Called when the database is created */
        public void onCreate(@NonNull final SQLiteDatabase db) {
            db.execSQL(NetworkAttributesContract.CREATE_TABLE);
            db.execSQL(PrivateDataContract.CREATE_TABLE);
        }

        /** Called when the database is upgraded */
        public void onUpgrade(@NonNull final SQLiteDatabase db, final int oldVersion,
                final int newVersion) {
            // No upgrade supported yet.
            db.execSQL(NetworkAttributesContract.DROP_TABLE);
            db.execSQL(PrivateDataContract.DROP_TABLE);
            onCreate(db);
        }

        /** Called when the database is downgraded */
        public void onDowngrade(@NonNull final SQLiteDatabase db, final int oldVersion,
                final int newVersion) {
            // Downgrades always nuke all data and recreate an empty table.
            db.execSQL(NetworkAttributesContract.DROP_TABLE);
            db.execSQL(PrivateDataContract.DROP_TABLE);
            onCreate(db);
        }
    }
}
Loading