1 /* 2 * Copyright (C) 2008 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 android.net; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 23 import java.util.HashSet; 24 25 /** 26 * Configuration details for a network interface. 27 * 28 * @hide 29 */ 30 public class InterfaceConfiguration implements Parcelable { 31 private String mHwAddr; 32 private LinkAddress mAddr; 33 private HashSet<String> mFlags = new HashSet<>(); 34 35 // Must be kept in sync with constant in INetd.aidl 36 private static final String FLAG_UP = "up"; 37 private static final String FLAG_DOWN = "down"; 38 39 private static final String[] EMPTY_STRING_ARRAY = new String[0]; 40 41 @UnsupportedAppUsage InterfaceConfiguration()42 public InterfaceConfiguration() { 43 } 44 45 @Override toString()46 public String toString() { 47 final StringBuilder builder = new StringBuilder(); 48 builder.append("mHwAddr=").append(mHwAddr); 49 builder.append(" mAddr=").append(String.valueOf(mAddr)); 50 builder.append(" mFlags=").append(getFlags()); 51 return builder.toString(); 52 } 53 54 @UnsupportedAppUsage getFlags()55 public Iterable<String> getFlags() { 56 return mFlags; 57 } 58 hasFlag(String flag)59 public boolean hasFlag(String flag) { 60 validateFlag(flag); 61 return mFlags.contains(flag); 62 } 63 64 @UnsupportedAppUsage clearFlag(String flag)65 public void clearFlag(String flag) { 66 validateFlag(flag); 67 mFlags.remove(flag); 68 } 69 70 @UnsupportedAppUsage setFlag(String flag)71 public void setFlag(String flag) { 72 validateFlag(flag); 73 mFlags.add(flag); 74 } 75 76 /** 77 * Set flags to mark interface as up. 78 */ 79 @UnsupportedAppUsage setInterfaceUp()80 public void setInterfaceUp() { 81 mFlags.remove(FLAG_DOWN); 82 mFlags.add(FLAG_UP); 83 } 84 85 /** 86 * Set flags to mark interface as down. 87 */ 88 @UnsupportedAppUsage setInterfaceDown()89 public void setInterfaceDown() { 90 mFlags.remove(FLAG_UP); 91 mFlags.add(FLAG_DOWN); 92 } 93 94 /** 95 * Set flags so that no changes will be made to the up/down status. 96 */ ignoreInterfaceUpDownStatus()97 public void ignoreInterfaceUpDownStatus() { 98 mFlags.remove(FLAG_UP); 99 mFlags.remove(FLAG_DOWN); 100 } 101 getLinkAddress()102 public LinkAddress getLinkAddress() { 103 return mAddr; 104 } 105 106 @UnsupportedAppUsage setLinkAddress(LinkAddress addr)107 public void setLinkAddress(LinkAddress addr) { 108 mAddr = addr; 109 } 110 getHardwareAddress()111 public String getHardwareAddress() { 112 return mHwAddr; 113 } 114 setHardwareAddress(String hwAddr)115 public void setHardwareAddress(String hwAddr) { 116 mHwAddr = hwAddr; 117 } 118 119 /** 120 * This function determines if the interface is up and has a valid IP 121 * configuration (IP address has a non zero octet). 122 * 123 * Note: It is supposed to be quick and hence should not initiate 124 * any network activity 125 */ isActive()126 public boolean isActive() { 127 try { 128 if (isUp()) { 129 for (byte b : mAddr.getAddress().getAddress()) { 130 if (b != 0) return true; 131 } 132 } 133 } catch (NullPointerException e) { 134 return false; 135 } 136 return false; 137 } 138 isUp()139 public boolean isUp() { 140 return hasFlag(FLAG_UP); 141 } 142 143 /** {@inheritDoc} */ describeContents()144 public int describeContents() { 145 return 0; 146 } 147 148 /** {@inheritDoc} */ writeToParcel(Parcel dest, int flags)149 public void writeToParcel(Parcel dest, int flags) { 150 dest.writeString(mHwAddr); 151 if (mAddr != null) { 152 dest.writeByte((byte)1); 153 dest.writeParcelable(mAddr, flags); 154 } else { 155 dest.writeByte((byte)0); 156 } 157 dest.writeInt(mFlags.size()); 158 for (String flag : mFlags) { 159 dest.writeString(flag); 160 } 161 } 162 163 public static final @android.annotation.NonNull Creator<InterfaceConfiguration> CREATOR = new Creator< 164 InterfaceConfiguration>() { 165 public InterfaceConfiguration createFromParcel(Parcel in) { 166 InterfaceConfiguration info = new InterfaceConfiguration(); 167 info.mHwAddr = in.readString(); 168 if (in.readByte() == 1) { 169 info.mAddr = in.readParcelable(null); 170 } 171 final int size = in.readInt(); 172 for (int i = 0; i < size; i++) { 173 info.mFlags.add(in.readString()); 174 } 175 return info; 176 } 177 178 public InterfaceConfiguration[] newArray(int size) { 179 return new InterfaceConfiguration[size]; 180 } 181 }; 182 validateFlag(String flag)183 private static void validateFlag(String flag) { 184 if (flag.indexOf(' ') >= 0) { 185 throw new IllegalArgumentException("flag contains space: " + flag); 186 } 187 } 188 } 189