blob: c97b37b55c78ebebe03fb236b5be45c76577c5e7 [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
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 Mehat353ced72010-01-22 12:22:54 -080050 }
51
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010052 @UnsupportedAppUsage
Jeff Sharkeyba2896e2011-11-30 18:13:54 -080053 public Iterable<String> getFlags() {
54 return mFlags;
Jeff Sharkeyddba1062011-11-29 18:37:04 -080055 }
56
57 public boolean hasFlag(String flag) {
58 validateFlag(flag);
59 return mFlags.contains(flag);
60 }
61
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010062 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080063 public void clearFlag(String flag) {
64 validateFlag(flag);
65 mFlags.remove(flag);
66 }
67
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010068 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080069 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 Inwoodfa3a7462018-08-08 14:52:47 +010077 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080078 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 Inwoodfa3a7462018-08-08 14:52:47 +010086 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -080087 public void setInterfaceDown() {
88 mFlags.remove(FLAG_UP);
89 mFlags.add(FLAG_DOWN);
90 }
91
Christopher Wileyce5d9132016-09-13 12:07:58 -070092 /**
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 Sharkeyddba1062011-11-29 18:37:04 -0800100 public LinkAddress getLinkAddress() {
101 return mAddr;
102 }
103
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100104 @UnsupportedAppUsage
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800105 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 Mehat353ced72010-01-22 12:22:54 -0800115 }
116
Irfan Sheriff29552092011-01-17 12:38:30 -0800117 /**
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 Wiley87100cc2016-10-17 10:30:28 -0700126 if (isUp()) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800127 for (byte b : mAddr.getAddress().getAddress()) {
Irfan Sheriff29552092011-01-17 12:38:30 -0800128 if (b != 0) return true;
129 }
130 }
131 } catch (NullPointerException e) {
132 return false;
133 }
134 return false;
135 }
136
Christopher Wiley08395dc2016-10-10 09:24:03 -0700137 public boolean isUp() {
138 return hasFlag(FLAG_UP);
139 }
140
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800141 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800142 public int describeContents() {
143 return 0;
144 }
145
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800146 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800147 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800148 dest.writeString(mHwAddr);
149 if (mAddr != null) {
Robert Greenwalt04808c22010-12-13 17:01:41 -0800150 dest.writeByte((byte)1);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800151 dest.writeParcelable(mAddr, flags);
Robert Greenwalt04808c22010-12-13 17:01:41 -0800152 } else {
153 dest.writeByte((byte)0);
154 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800155 dest.writeInt(mFlags.size());
156 for (String flag : mFlags) {
157 dest.writeString(flag);
158 }
San Mehat353ced72010-01-22 12:22:54 -0800159 }
160
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700161 public static final @android.annotation.NonNull Creator<InterfaceConfiguration> CREATOR = new Creator<
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800162 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 Mehat353ced72010-01-22 12:22:54 -0800168 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800169 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 Mehat353ced72010-01-22 12:22:54 -0800175
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800176 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 Mehat353ced72010-01-22 12:22:54 -0800186}