1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.ethernet; 18 19 import android.annotation.Nullable; 20 import android.net.IpConfiguration; 21 import android.os.Environment; 22 import android.util.ArrayMap; 23 24 import com.android.server.net.IpConfigStore; 25 26 27 /** 28 * This class provides an API to store and manage Ethernet network configuration. 29 */ 30 public class EthernetConfigStore { 31 private static final String ipConfigFile = Environment.getDataDirectory() + 32 "/misc/ethernet/ipconfig.txt"; 33 34 private IpConfigStore mStore = new IpConfigStore(); 35 private ArrayMap<String, IpConfiguration> mIpConfigurations; 36 private IpConfiguration mIpConfigurationForDefaultInterface; 37 private final Object mSync = new Object(); 38 EthernetConfigStore()39 public EthernetConfigStore() { 40 mIpConfigurations = new ArrayMap<>(0); 41 } 42 read()43 public void read() { 44 synchronized (mSync) { 45 ArrayMap<String, IpConfiguration> configs = 46 IpConfigStore.readIpConfigurations(ipConfigFile); 47 48 // This configuration may exist in old file versions when there was only a single active 49 // Ethernet interface. 50 if (configs.containsKey("0")) { 51 mIpConfigurationForDefaultInterface = configs.remove("0"); 52 } 53 54 mIpConfigurations = configs; 55 } 56 } 57 write(String iface, IpConfiguration config)58 public void write(String iface, IpConfiguration config) { 59 boolean modified; 60 61 synchronized (mSync) { 62 if (config == null) { 63 modified = mIpConfigurations.remove(iface) != null; 64 } else { 65 IpConfiguration oldConfig = mIpConfigurations.put(iface, config); 66 modified = !config.equals(oldConfig); 67 } 68 69 if (modified) { 70 mStore.writeIpConfigurations(ipConfigFile, mIpConfigurations); 71 } 72 } 73 } 74 getIpConfigurations()75 public ArrayMap<String, IpConfiguration> getIpConfigurations() { 76 synchronized (mSync) { 77 return new ArrayMap<>(mIpConfigurations); 78 } 79 } 80 81 @Nullable getIpConfigurationForDefaultInterface()82 public IpConfiguration getIpConfigurationForDefaultInterface() { 83 synchronized (mSync) { 84 return mIpConfigurationForDefaultInterface == null 85 ? null : new IpConfiguration(mIpConfigurationForDefaultInterface); 86 } 87 } 88 } 89