blob: c9a999cfdf171a5ffd1afcd336c5e241e4b0ccf6 [file] [log] [blame]
San Mehat353ced72010-01-22 12:22:54 -08001/*
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
17package android.net;
18
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010019import android.annotation.UnsupportedAppUsage;
San Mehat353ced72010-01-22 12:22:54 -080020import android.os.Parcel;
Jeff Sharkeyddba1062011-11-29 18:37:04 -080021import android.os.Parcelable;
San Mehat353ced72010-01-22 12:22:54 -080022
Jeff Sharkeyddba1062011-11-29 18:37:04 -080023import com.google.android.collect.Sets;
24
25import java.util.HashSet;
Robert Greenwalt04808c22010-12-13 17:01:41 -080026
San Mehat353ced72010-01-22 12:22:54 -080027/**
Jeff Sharkeyddba1062011-11-29 18:37:04 -080028 * Configuration details for a network interface.
29 *
San Mehat353ced72010-01-22 12:22:54 -080030 * @hide
31 */
32public class InterfaceConfiguration implements Parcelable {
Jeff Sharkeyddba1062011-11-29 18:37:04 -080033 private String mHwAddr;
34 private LinkAddress mAddr;
35 private HashSet<String> mFlags = Sets.newHashSet();
San Mehat353ced72010-01-22 12:22:54 -080036
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +090037 // 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 Huang14f75442018-08-15 19:22:54 +080040
41 private static final String[] EMPTY_STRING_ARRAY = new String[0];
Jeff Sharkeyddba1062011-11-29 18:37:04 -080042
Artur Satayev751e5512019-11-15 19:12:49 +000043 @UnsupportedAppUsage
44 public InterfaceConfiguration() {
45 }
46
Jeff Sharkeyddba1062011-11-29 18:37:04 -080047 @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 Mehat353ced72010-01-22 12:22:54 -080054 }
55
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010056 @UnsupportedAppUsage
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080057 public Iterable<String> getFlags() {
58 return mFlags;
Jeff Sharkeyddba1062011-11-29 18:37:04 -080059 }
60
61 public boolean hasFlag(String flag) {
62 validateFlag(flag);
63 return mFlags.contains(flag);
64 }
65
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010066 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080067 public void clearFlag(String flag) {
68 validateFlag(flag);
69 mFlags.remove(flag);
70 }
71
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010072 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080073 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 Inwoodfa3a7462018-08-08 14:52:47 +010081 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080082 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 Inwoodfa3a7462018-08-08 14:52:47 +010090 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080091 public void setInterfaceDown() {
92 mFlags.remove(FLAG_UP);
93 mFlags.add(FLAG_DOWN);
94 }
95
Christopher Wileyce5d9132016-09-13 12:07:58 -070096 /**
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 Sharkeyddba1062011-11-29 18:37:04 -0800104 public LinkAddress getLinkAddress() {
105 return mAddr;
106 }
107
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100108 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800109 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 Mehat353ced72010-01-22 12:22:54 -0800119 }
120
Irfan Sheriff29552092011-01-17 12:38:30 -0800121 /**
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 Wiley87100cc2016-10-17 10:30:28 -0700130 if (isUp()) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800131 for (byte b : mAddr.getAddress().getAddress()) {
Irfan Sheriff29552092011-01-17 12:38:30 -0800132 if (b != 0) return true;
133 }
134 }
135 } catch (NullPointerException e) {
136 return false;
137 }
138 return false;
139 }
140
Christopher Wiley08395dc2016-10-10 09:24:03 -0700141 public boolean isUp() {
142 return hasFlag(FLAG_UP);
143 }
144
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800145 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800146 public int describeContents() {
147 return 0;
148 }
149
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800150 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800151 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800152 dest.writeString(mHwAddr);
153 if (mAddr != null) {
Robert Greenwalt04808c22010-12-13 17:01:41 -0800154 dest.writeByte((byte)1);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800155 dest.writeParcelable(mAddr, flags);
Robert Greenwalt04808c22010-12-13 17:01:41 -0800156 } else {
157 dest.writeByte((byte)0);
158 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800159 dest.writeInt(mFlags.size());
160 for (String flag : mFlags) {
161 dest.writeString(flag);
162 }
San Mehat353ced72010-01-22 12:22:54 -0800163 }
164
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700165 public static final @android.annotation.NonNull Creator<InterfaceConfiguration> CREATOR = new Creator<
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800166 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 Mehat353ced72010-01-22 12:22:54 -0800172 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800173 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 Mehat353ced72010-01-22 12:22:54 -0800179
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800180 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 Mehat353ced72010-01-22 12:22:54 -0800190}