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