San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 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 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 20 | import android.os.Parcel; |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 21 | import android.os.Parcelable; |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 22 | import android.text.TextUtils; |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 23 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 24 | import com.google.android.collect.Sets; |
| 25 | |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 26 | import java.net.InetAddress; |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 27 | import java.util.HashSet; |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 28 | |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 29 | /** |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 30 | * Configuration details for a network interface. |
| 31 | * |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 32 | * @hide |
| 33 | */ |
| 34 | public class InterfaceConfiguration implements Parcelable { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 35 | private String mHwAddr; |
| 36 | private LinkAddress mAddr; |
| 37 | private HashSet<String> mFlags = Sets.newHashSet(); |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 38 | |
Remi NGUYEN VAN | f9a8c2e | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 39 | // Must be kept in sync with constant in INetd.aidl |
| 40 | private static final String FLAG_UP = "up"; |
| 41 | private static final String FLAG_DOWN = "down"; |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 42 | |
| 43 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 44 | |
| 45 | @Override |
| 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(); |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 54 | @UnsupportedAppUsage |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 55 | public Iterable<String> getFlags() { |
| 56 | return mFlags; |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public boolean hasFlag(String flag) { |
| 60 | validateFlag(flag); |
| 61 | return mFlags.contains(flag); |
| 62 | } |
| 63 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 64 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 65 | public void clearFlag(String flag) { |
| 66 | validateFlag(flag); |
| 67 | mFlags.remove(flag); |
| 68 | } |
| 69 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 70 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 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 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 79 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 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 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 88 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 89 | public void setInterfaceDown() { |
| 90 | mFlags.remove(FLAG_UP); |
| 91 | mFlags.add(FLAG_DOWN); |
| 92 | } |
| 93 | |
Christopher Wiley | ce5d913 | 2016-09-13 12:07:58 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Set flags so that no changes will be made to the up/down status. |
| 96 | */ |
| 97 | public void ignoreInterfaceUpDownStatus() { |
| 98 | mFlags.remove(FLAG_UP); |
| 99 | mFlags.remove(FLAG_DOWN); |
| 100 | } |
| 101 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 102 | public LinkAddress getLinkAddress() { |
| 103 | return mAddr; |
| 104 | } |
| 105 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 106 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 107 | public void setLinkAddress(LinkAddress addr) { |
| 108 | mAddr = addr; |
| 109 | } |
| 110 | |
| 111 | public String getHardwareAddress() { |
| 112 | return mHwAddr; |
| 113 | } |
| 114 | |
| 115 | public void setHardwareAddress(String hwAddr) { |
| 116 | mHwAddr = hwAddr; |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Irfan Sheriff | 2955209 | 2011-01-17 12:38:30 -0800 | [diff] [blame] | 119 | /** |
Luke Huang | 14f7544 | 2018-08-15 19:22:54 +0800 | [diff] [blame] | 120 | * Construct InterfaceConfiguration from InterfaceConfigurationParcel. |
| 121 | */ |
| 122 | public static InterfaceConfiguration fromParcel(InterfaceConfigurationParcel p) { |
| 123 | InterfaceConfiguration cfg = new InterfaceConfiguration(); |
| 124 | cfg.setHardwareAddress(p.hwAddr); |
| 125 | |
| 126 | final InetAddress addr = NetworkUtils.numericToInetAddress(p.ipv4Addr); |
| 127 | cfg.setLinkAddress(new LinkAddress(addr, p.prefixLength)); |
| 128 | for (String flag : p.flags) { |
| 129 | cfg.setFlag(flag); |
| 130 | } |
| 131 | |
| 132 | return cfg; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Convert InterfaceConfiguration to InterfaceConfigurationParcel with given ifname. |
| 137 | */ |
| 138 | public InterfaceConfigurationParcel toParcel(String iface) { |
| 139 | InterfaceConfigurationParcel cfgParcel = new InterfaceConfigurationParcel(); |
| 140 | cfgParcel.ifName = iface; |
| 141 | if (!TextUtils.isEmpty(mHwAddr)) { |
| 142 | cfgParcel.hwAddr = mHwAddr; |
| 143 | } else { |
| 144 | cfgParcel.hwAddr = ""; |
| 145 | } |
| 146 | cfgParcel.ipv4Addr = mAddr.getAddress().getHostAddress(); |
| 147 | cfgParcel.prefixLength = mAddr.getPrefixLength(); |
| 148 | cfgParcel.flags = mFlags.toArray(EMPTY_STRING_ARRAY); |
| 149 | |
| 150 | return cfgParcel; |
| 151 | } |
| 152 | |
| 153 | /** |
Irfan Sheriff | 2955209 | 2011-01-17 12:38:30 -0800 | [diff] [blame] | 154 | * This function determines if the interface is up and has a valid IP |
| 155 | * configuration (IP address has a non zero octet). |
| 156 | * |
| 157 | * Note: It is supposed to be quick and hence should not initiate |
| 158 | * any network activity |
| 159 | */ |
| 160 | public boolean isActive() { |
| 161 | try { |
Christopher Wiley | 87100cc | 2016-10-17 10:30:28 -0700 | [diff] [blame] | 162 | if (isUp()) { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 163 | for (byte b : mAddr.getAddress().getAddress()) { |
Irfan Sheriff | 2955209 | 2011-01-17 12:38:30 -0800 | [diff] [blame] | 164 | if (b != 0) return true; |
| 165 | } |
| 166 | } |
| 167 | } catch (NullPointerException e) { |
| 168 | return false; |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
Christopher Wiley | 08395dc | 2016-10-10 09:24:03 -0700 | [diff] [blame] | 173 | public boolean isUp() { |
| 174 | return hasFlag(FLAG_UP); |
| 175 | } |
| 176 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 177 | /** {@inheritDoc} */ |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 178 | public int describeContents() { |
| 179 | return 0; |
| 180 | } |
| 181 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 182 | /** {@inheritDoc} */ |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 183 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 184 | dest.writeString(mHwAddr); |
| 185 | if (mAddr != null) { |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 186 | dest.writeByte((byte)1); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 187 | dest.writeParcelable(mAddr, flags); |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 188 | } else { |
| 189 | dest.writeByte((byte)0); |
| 190 | } |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 191 | dest.writeInt(mFlags.size()); |
| 192 | for (String flag : mFlags) { |
| 193 | dest.writeString(flag); |
| 194 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 197 | public static final @android.annotation.NonNull Creator<InterfaceConfiguration> CREATOR = new Creator< |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 198 | InterfaceConfiguration>() { |
| 199 | public InterfaceConfiguration createFromParcel(Parcel in) { |
| 200 | InterfaceConfiguration info = new InterfaceConfiguration(); |
| 201 | info.mHwAddr = in.readString(); |
| 202 | if (in.readByte() == 1) { |
| 203 | info.mAddr = in.readParcelable(null); |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 204 | } |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 205 | final int size = in.readInt(); |
| 206 | for (int i = 0; i < size; i++) { |
| 207 | info.mFlags.add(in.readString()); |
| 208 | } |
| 209 | return info; |
| 210 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 211 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 212 | public InterfaceConfiguration[] newArray(int size) { |
| 213 | return new InterfaceConfiguration[size]; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | private static void validateFlag(String flag) { |
| 218 | if (flag.indexOf(' ') >= 0) { |
| 219 | throw new IllegalArgumentException("flag contains space: " + flag); |
| 220 | } |
| 221 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 222 | } |