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