blob: 37425ffc18aae341735153d483c196421ed2e673 [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
Artur Satayev26958002019-12-10 17:47:52 +000019import android.compat.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 java.util.HashSet;
Robert Greenwalt04808c22010-12-13 17:01:41 -080024
San Mehat353ced72010-01-22 12:22:54 -080025/**
Jeff Sharkeyddba1062011-11-29 18:37:04 -080026 * Configuration details for a network interface.
27 *
San Mehat353ced72010-01-22 12:22:54 -080028 * @hide
29 */
30public class InterfaceConfiguration implements Parcelable {
Jeff Sharkeyddba1062011-11-29 18:37:04 -080031 private String mHwAddr;
32 private LinkAddress mAddr;
Roshan Piusf4c912e2019-12-09 07:37:11 -080033 private HashSet<String> mFlags = new HashSet<>();
San Mehat353ced72010-01-22 12:22:54 -080034
Remi NGUYEN VANf9a8c2e2019-02-13 18:28:35 +090035 // Must be kept in sync with constant in INetd.aidl
36 private static final String FLAG_UP = "up";
37 private static final String FLAG_DOWN = "down";
Luke Huang14f75442018-08-15 19:22:54 +080038
39 private static final String[] EMPTY_STRING_ARRAY = new String[0];
Jeff Sharkeyddba1062011-11-29 18:37:04 -080040
Artur Satayevfc46be72019-11-04 17:50:59 +000041 @UnsupportedAppUsage
42 public InterfaceConfiguration() {
43 }
44
Jeff Sharkeyddba1062011-11-29 18:37:04 -080045 @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 /**
120 * This function determines if the interface is up and has a valid IP
121 * configuration (IP address has a non zero octet).
122 *
123 * Note: It is supposed to be quick and hence should not initiate
124 * any network activity
125 */
126 public boolean isActive() {
127 try {
Christopher Wiley87100cc2016-10-17 10:30:28 -0700128 if (isUp()) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800129 for (byte b : mAddr.getAddress().getAddress()) {
Irfan Sheriff29552092011-01-17 12:38:30 -0800130 if (b != 0) return true;
131 }
132 }
133 } catch (NullPointerException e) {
134 return false;
135 }
136 return false;
137 }
138
Christopher Wiley08395dc2016-10-10 09:24:03 -0700139 public boolean isUp() {
140 return hasFlag(FLAG_UP);
141 }
142
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800143 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800144 public int describeContents() {
145 return 0;
146 }
147
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800148 /** {@inheritDoc} */
San Mehat353ced72010-01-22 12:22:54 -0800149 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800150 dest.writeString(mHwAddr);
151 if (mAddr != null) {
Robert Greenwalt04808c22010-12-13 17:01:41 -0800152 dest.writeByte((byte)1);
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800153 dest.writeParcelable(mAddr, flags);
Robert Greenwalt04808c22010-12-13 17:01:41 -0800154 } else {
155 dest.writeByte((byte)0);
156 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800157 dest.writeInt(mFlags.size());
158 for (String flag : mFlags) {
159 dest.writeString(flag);
160 }
San Mehat353ced72010-01-22 12:22:54 -0800161 }
162
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700163 public static final @android.annotation.NonNull Creator<InterfaceConfiguration> CREATOR = new Creator<
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800164 InterfaceConfiguration>() {
165 public InterfaceConfiguration createFromParcel(Parcel in) {
166 InterfaceConfiguration info = new InterfaceConfiguration();
167 info.mHwAddr = in.readString();
168 if (in.readByte() == 1) {
169 info.mAddr = in.readParcelable(null);
San Mehat353ced72010-01-22 12:22:54 -0800170 }
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800171 final int size = in.readInt();
172 for (int i = 0; i < size; i++) {
173 info.mFlags.add(in.readString());
174 }
175 return info;
176 }
San Mehat353ced72010-01-22 12:22:54 -0800177
Jeff Sharkeyddba1062011-11-29 18:37:04 -0800178 public InterfaceConfiguration[] newArray(int size) {
179 return new InterfaceConfiguration[size];
180 }
181 };
182
183 private static void validateFlag(String flag) {
184 if (flag.indexOf(' ') >= 0) {
185 throw new IllegalArgumentException("flag contains space: " + flag);
186 }
187 }
San Mehat353ced72010-01-22 12:22:54 -0800188}