LinkProperties.java revision 37e65ebb7eb932e1a144b1cab262e11ca5fd109b
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.net.ProxyProperties;
20import android.os.Parcelable;
21import android.os.Parcel;
22import android.util.Log;
23
24import java.net.InetAddress;
25import java.net.NetworkInterface;
26import java.net.SocketException;
27import java.net.UnknownHostException;
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Collections;
31
32/**
33 * Describes the properties of a network link.
34 * TODO - consider adding optional fields like Apn and ApnType
35 * @hide
36 */
37public class LinkProperties implements Parcelable {
38
39    private NetworkInterface mIface;
40    private Collection<InetAddress> mAddresses;
41    private Collection<InetAddress> mDnses;
42    private InetAddress mGateway;
43    private ProxyProperties mHttpProxy;
44
45    public LinkProperties() {
46        clear();
47    }
48
49    // copy constructor instead of clone
50    public LinkProperties(LinkProperties source) {
51        mIface = source.getInterface();
52        mAddresses = source.getAddresses();
53        mDnses = source.getDnses();
54        mGateway = source.getGateway();
55        mHttpProxy = new ProxyProperties(source.getHttpProxy());
56    }
57
58    public void setInterface(NetworkInterface iface) {
59        mIface = iface;
60    }
61    public NetworkInterface getInterface() {
62        return mIface;
63    }
64    public String getInterfaceName() {
65        return (mIface == null ? null : mIface.getName());
66    }
67
68    public void addAddress(InetAddress address) {
69        mAddresses.add(address);
70    }
71    public Collection<InetAddress> getAddresses() {
72        return Collections.unmodifiableCollection(mAddresses);
73    }
74
75    public void addDns(InetAddress dns) {
76        mDnses.add(dns);
77    }
78    public Collection<InetAddress> getDnses() {
79        return Collections.unmodifiableCollection(mDnses);
80    }
81
82    public void setGateway(InetAddress gateway) {
83        mGateway = gateway;
84    }
85    public InetAddress getGateway() {
86        return mGateway;
87    }
88
89    public void setHttpProxy(ProxyProperties proxy) {
90        mHttpProxy = proxy;
91    }
92    public ProxyProperties getHttpProxy() {
93        return mHttpProxy;
94    }
95
96    public void clear() {
97        mIface = null;
98        mAddresses = new ArrayList<InetAddress>();
99        mDnses = new ArrayList<InetAddress>();
100        mGateway = null;
101        mHttpProxy = null;
102    }
103
104    /**
105     * Implement the Parcelable interface
106     * @hide
107     */
108    public int describeContents() {
109        return 0;
110    }
111
112    @Override
113    public String toString() {
114        String ifaceName = (mIface == null ? "" : "InterfaceName: " + mIface.getName() + " ");
115
116        String ip = "IpAddresses: [";
117        for (InetAddress addr : mAddresses) ip +=  addr.getHostAddress() + ",";
118        ip += "] ";
119
120        String dns = "DnsAddresses: [";
121        for (InetAddress addr : mDnses) dns += addr.getHostAddress() + ",";
122        dns += "] ";
123
124        String proxy = (mHttpProxy == null ? "" : "HttpProxy: " + mHttpProxy.toString() + " ");
125        String gateway = (mGateway == null ? "" : "Gateway: " + mGateway.getHostAddress() + " ");
126
127        return ifaceName + ip + gateway + dns + proxy;
128    }
129
130    /**
131     * Implement the Parcelable interface.
132     * @hide
133     */
134    public void writeToParcel(Parcel dest, int flags) {
135        dest.writeString(getInterfaceName());
136        dest.writeInt(mAddresses.size());
137        //TODO: explore an easy alternative to preserve hostname
138        // without doing a lookup
139        for(InetAddress a : mAddresses) {
140            dest.writeByteArray(a.getAddress());
141        }
142        dest.writeInt(mDnses.size());
143        for(InetAddress d : mDnses) {
144            dest.writeByteArray(d.getAddress());
145        }
146        if (mGateway != null) {
147            dest.writeByte((byte)1);
148            dest.writeByteArray(mGateway.getAddress());
149        } else {
150            dest.writeByte((byte)0);
151        }
152        if (mHttpProxy != null) {
153            dest.writeByte((byte)1);
154            dest.writeParcelable(mHttpProxy, flags);
155        } else {
156            dest.writeByte((byte)0);
157        }
158    }
159
160    /**
161     * Implement the Parcelable interface.
162     * @hide
163     */
164    public static final Creator<LinkProperties> CREATOR =
165        new Creator<LinkProperties>() {
166            public LinkProperties createFromParcel(Parcel in) {
167                LinkProperties netProp = new LinkProperties();
168                String iface = in.readString();
169                if (iface != null) {
170                    try {
171                        netProp.setInterface(NetworkInterface.getByName(iface));
172                    } catch (Exception e) {
173                        return null;
174                    }
175                }
176                int addressCount = in.readInt();
177                for (int i=0; i<addressCount; i++) {
178                    try {
179                        netProp.addAddress(InetAddress.getByAddress(in.createByteArray()));
180                    } catch (UnknownHostException e) { }
181                }
182                addressCount = in.readInt();
183                for (int i=0; i<addressCount; i++) {
184                    try {
185                        netProp.addDns(InetAddress.getByAddress(in.createByteArray()));
186                    } catch (UnknownHostException e) { }
187                }
188                if (in.readByte() == 1) {
189                    try {
190                        netProp.setGateway(InetAddress.getByAddress(in.createByteArray()));
191                    } catch (UnknownHostException e) {}
192                }
193                if (in.readByte() == 1) {
194                    netProp.setHttpProxy((ProxyProperties)in.readParcelable(null));
195                }
196                return netProp;
197            }
198
199            public LinkProperties[] newArray(int size) {
200                return new LinkProperties[size];
201            }
202        };
203}
204