blob: b9d49c14f6c625f8cb17cea6fe7c171fb29f9f80 [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;
Luke Huang14f75442018-08-15 19:22:54 +080022import android.text.TextUtils;
San Mehat353ced72010-01-22 12:22:54 -080023
Jeff Sharkeyddba1062011-11-29 18:37:04 -080024import com.google.android.collect.Sets;
25
Luke Huang14f75442018-08-15 19:22:54 +080026import java.net.InetAddress;
Jeff Sharkeyddba1062011-11-29 18:37:04 -080027import java.util.HashSet;
Robert Greenwalt04808c22010-12-13 17:01:41 -080028
San Mehat353ced72010-01-22 12:22:54 -080029/**
Jeff Sharkeyddba1062011-11-29 18:37:04 -080030 * Configuration details for a network interface.
31 *
San Mehat353ced72010-01-22 12:22:54 -080032 * @hide
33 */
34public class InterfaceConfiguration implements Parcelable {
Jeff Sharkeyddba1062011-11-29 18:37:04 -080035 private String mHwAddr;
36 private LinkAddress mAddr;
37 private HashSet<String> mFlags = Sets.newHashSet();
San Mehat353ced72010-01-22 12:22:54 -080038
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +090039 // 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 Huang14f75442018-08-15 19:22:54 +080042
43 private static final String[] EMPTY_STRING_ARRAY = new String[0];
Jeff Sharkeyddba1062011-11-29 18:37:04 -080044
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 Mehat353ced72010-01-22 12:22:54 -080052 }
53
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010054 @UnsupportedAppUsage
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080055 public Iterable<String> getFlags() {
56 return mFlags;
Jeff Sharkeyddba1062011-11-29 18:37:04 -080057 }
58
59 public boolean hasFlag(String flag) {
60 validateFlag(flag);
61 return mFlags.contains(flag);
62 }
63
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010064 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080065 public void clearFlag(String flag) {
66 validateFlag(flag);
67 mFlags.remove(flag);
68 }
69
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010070 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080071 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 Inwoodfa3a7462018-08-08 14:52:47 +010079 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080080 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 Inwoodfa3a7462018-08-08 14:52:47 +010088 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080089 public void setInterfaceDown() {
90 mFlags.remove(FLAG_UP);
91 mFlags.add(FLAG_DOWN);
92 }
93
Christopher Wileyce5d9132016-09-13 12:07:58 -070094 /**
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 Sharkeyddba1062011-11-29 18:37:04 -0800102 public LinkAddress getLinkAddress() {
103 return mAddr;
104 }
105
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100106 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800107 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 Mehat353ced72010-01-22 12:22:54 -0800117 }
118
Irfan Sheriff29552092011-01-17 12:38:30 -0800119 /**
Luke Huang14f75442018-08-15 19:22:54 +0800120 * 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 Sheriff29552092011-01-17 12:38:30 -0800154 * 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 Wiley87100cc2016-10-17 10:30:28 -0700162 if (isUp()) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800163 for (byte b : mAddr.getAddress().getAddress()) {
Irfan Sheriff29552092011-01-17 12:38:30 -0800164 if (b != 0) return true;
165 }
166 }
167 } catch (NullPointerException e) {
168 return false;
169 }
170 return false;
171 }
172
Christopher Wiley08395dc2016-10-10 09:24:03 -0700173 public boolean isUp() {
174 return hasFlag(FLAG_UP);
175 }
176
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800177 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800178 public int describeContents() {
179 return 0;
180 }
181
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800182 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800183 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800184 dest.writeString(mHwAddr);
185 if (mAddr != null) {
Robert Greenwalt04808c22010-12-13 17:01:41 -0800186 dest.writeByte((byte)1);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800187 dest.writeParcelable(mAddr, flags);
Robert Greenwalt04808c22010-12-13 17:01:41 -0800188 } else {
189 dest.writeByte((byte)0);
190 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800191 dest.writeInt(mFlags.size());
192 for (String flag : mFlags) {
193 dest.writeString(flag);
194 }
San Mehat353ced72010-01-22 12:22:54 -0800195 }
196
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800197 public static final Creator<InterfaceConfiguration> CREATOR = new Creator<
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 Mehat353ced72010-01-22 12:22:54 -0800204 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800205 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 Mehat353ced72010-01-22 12:22:54 -0800211
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800212 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 Mehat353ced72010-01-22 12:22:54 -0800222}