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 | |
| 43 | @Override |
| 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(); |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 52 | @UnsupportedAppUsage |
Jeff Sharkey | ba2896e | 2011-11-30 18:13:54 -0800 | [diff] [blame] | 53 | public Iterable<String> getFlags() { |
| 54 | return mFlags; |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | public boolean hasFlag(String flag) { |
| 58 | validateFlag(flag); |
| 59 | return mFlags.contains(flag); |
| 60 | } |
| 61 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 62 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 63 | public void clearFlag(String flag) { |
| 64 | validateFlag(flag); |
| 65 | mFlags.remove(flag); |
| 66 | } |
| 67 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 68 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 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 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 77 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 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 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 86 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 87 | public void setInterfaceDown() { |
| 88 | mFlags.remove(FLAG_UP); |
| 89 | mFlags.add(FLAG_DOWN); |
| 90 | } |
| 91 | |
Christopher Wiley | ce5d913 | 2016-09-13 12:07:58 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Set flags so that no changes will be made to the up/down status. |
| 94 | */ |
| 95 | public void ignoreInterfaceUpDownStatus() { |
| 96 | mFlags.remove(FLAG_UP); |
| 97 | mFlags.remove(FLAG_DOWN); |
| 98 | } |
| 99 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 100 | public LinkAddress getLinkAddress() { |
| 101 | return mAddr; |
| 102 | } |
| 103 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 104 | @UnsupportedAppUsage |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 105 | public void setLinkAddress(LinkAddress addr) { |
| 106 | mAddr = addr; |
| 107 | } |
| 108 | |
| 109 | public String getHardwareAddress() { |
| 110 | return mHwAddr; |
| 111 | } |
| 112 | |
| 113 | public void setHardwareAddress(String hwAddr) { |
| 114 | mHwAddr = hwAddr; |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Irfan Sheriff | 2955209 | 2011-01-17 12:38:30 -0800 | [diff] [blame] | 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 | */ |
| 124 | public boolean isActive() { |
| 125 | try { |
Christopher Wiley | 87100cc | 2016-10-17 10:30:28 -0700 | [diff] [blame] | 126 | if (isUp()) { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 127 | for (byte b : mAddr.getAddress().getAddress()) { |
Irfan Sheriff | 2955209 | 2011-01-17 12:38:30 -0800 | [diff] [blame] | 128 | if (b != 0) return true; |
| 129 | } |
| 130 | } |
| 131 | } catch (NullPointerException e) { |
| 132 | return false; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
Christopher Wiley | 08395dc | 2016-10-10 09:24:03 -0700 | [diff] [blame] | 137 | public boolean isUp() { |
| 138 | return hasFlag(FLAG_UP); |
| 139 | } |
| 140 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 141 | /** {@inheritDoc} */ |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 142 | public int describeContents() { |
| 143 | return 0; |
| 144 | } |
| 145 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 146 | /** {@inheritDoc} */ |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 147 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 148 | dest.writeString(mHwAddr); |
| 149 | if (mAddr != null) { |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 150 | dest.writeByte((byte)1); |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 151 | dest.writeParcelable(mAddr, flags); |
Robert Greenwalt | 04808c2 | 2010-12-13 17:01:41 -0800 | [diff] [blame] | 152 | } else { |
| 153 | dest.writeByte((byte)0); |
| 154 | } |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 155 | dest.writeInt(mFlags.size()); |
| 156 | for (String flag : mFlags) { |
| 157 | dest.writeString(flag); |
| 158 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 161 | public static final @android.annotation.NonNull Creator<InterfaceConfiguration> CREATOR = new Creator< |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 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); |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 168 | } |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 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 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 175 | |
Jeff Sharkey | ddba106 | 2011-11-29 18:37:04 -0800 | [diff] [blame] | 176 | public InterfaceConfiguration[] newArray(int size) { |
| 177 | return new InterfaceConfiguration[size]; |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | private static void validateFlag(String flag) { |
| 182 | if (flag.indexOf(' ') >= 0) { |
| 183 | throw new IllegalArgumentException("flag contains space: " + flag); |
| 184 | } |
| 185 | } |
San Mehat | 353ced7 | 2010-01-22 12:22:54 -0800 | [diff] [blame] | 186 | } |