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
17package android.net;
18
19import android.os.Parcelable;
20import android.os.Parcel;
21
22/**
23 * A simple object for retrieving / setting an interfaces configuration
24 * @hide
25 */
26public class InterfaceConfiguration implements Parcelable {
27    public String hwAddr;
28    public int ipAddr;
29    public int netmask;
30    public String interfaceFlags;
31
32    public InterfaceConfiguration() {
33        super();
34    }
35
36    public String toString() {
37        StringBuffer str = new StringBuffer();
38
39        str.append("ipddress "); putAddress(str, ipAddr);
40        str.append(" netmask "); putAddress(str, netmask);
41        str.append(" flags ").append(interfaceFlags);
42        str.append(" hwaddr ").append(hwAddr);
43
44        return str.toString();
45    }
46
47    private static void putAddress(StringBuffer buf, int addr) {
48        buf.append((addr >> 24) & 0xff).append('.').
49            append((addr >> 16) & 0xff).append('.').
50            append((addr >> 8) & 0xff).append('.').
51            append(addr & 0xff);
52    }
53
54    /**
55     * This function determines if the interface is up and has a valid IP
56     * configuration (IP address has a non zero octet).
57     *
58     * Note: It is supposed to be quick and hence should not initiate
59     * any network activity
60     */
61    public boolean isActive() {
62        try {
63            if(interfaceFlags.contains("up")) {
64                if (ipAddr != 0) return true;
65            }
66        } catch (NullPointerException e) {
67            return false;
68        }
69        return false;
70    }
71
72    /** Implement the Parcelable interface {@hide} */
73    public int describeContents() {
74        return 0;
75    }
76
77    /** Implement the Parcelable interface {@hide} */
78    public void writeToParcel(Parcel dest, int flags) {
79        dest.writeString(hwAddr);
80        dest.writeInt(ipAddr);
81        dest.writeInt(netmask);
82        dest.writeString(interfaceFlags);
83    }
84
85    /** Implement the Parcelable interface {@hide} */
86    public static final Creator<InterfaceConfiguration> CREATOR =
87        new Creator<InterfaceConfiguration>() {
88            public InterfaceConfiguration createFromParcel(Parcel in) {
89                InterfaceConfiguration info = new InterfaceConfiguration();
90                info.hwAddr = in.readString();
91                info.ipAddr = in.readInt();
92                info.netmask = in.readInt();
93                info.interfaceFlags = in.readString();
94                return info;
95            }
96
97            public InterfaceConfiguration[] newArray(int size) {
98                return new InterfaceConfiguration[size];
99            }
100        };
101}
102