• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 
17 package android.net.util;
18 
19 import android.net.MacAddress;
20 import android.text.TextUtils;
21 
22 import java.net.NetworkInterface;
23 import java.net.SocketException;
24 
25 
26 /**
27  * Encapsulate the interface parameters common to IpClient/IpServer components.
28  *
29  * Basically all java.net.NetworkInterface methods throw Exceptions. IpClient
30  * and IpServer (sub)components need most or all of this information at some
31  * point during their lifecycles, so pass only this simplified object around
32  * which can be created once when IpClient/IpServer are told to start.
33  *
34  * @hide
35  */
36 public class InterfaceParams {
37     public final String name;
38     public final int index;
39     public final boolean hasMacAddress;
40     public final MacAddress macAddr;
41     public final int defaultMtu;
42 
43     // TODO: move the below to NetworkStackConstants when this class is moved to the NetworkStack.
44     private static final int ETHER_MTU = 1500;
45     private static final int IPV6_MIN_MTU = 1280;
46 
47 
getByName(String name)48     public static InterfaceParams getByName(String name) {
49         final NetworkInterface netif = getNetworkInterfaceByName(name);
50         if (netif == null) return null;
51 
52         // Not all interfaces have MAC addresses, e.g. rmnet_data0.
53         final MacAddress macAddr = getMacAddress(netif);
54 
55         try {
56             return new InterfaceParams(name, netif.getIndex(), macAddr, netif.getMTU());
57         } catch (IllegalArgumentException|SocketException e) {
58             return null;
59         }
60     }
61 
InterfaceParams(String name, int index, MacAddress macAddr)62     public InterfaceParams(String name, int index, MacAddress macAddr) {
63         this(name, index, macAddr, ETHER_MTU);
64     }
65 
InterfaceParams(String name, int index, MacAddress macAddr, int defaultMtu)66     public InterfaceParams(String name, int index, MacAddress macAddr, int defaultMtu) {
67         if (TextUtils.isEmpty(name)) {
68             throw new IllegalArgumentException("impossible interface name");
69         }
70 
71         if (index <= 0) throw new IllegalArgumentException("invalid interface index");
72 
73         this.name = name;
74         this.index = index;
75         this.hasMacAddress = (macAddr != null);
76         this.macAddr = hasMacAddress ? macAddr : MacAddress.fromBytes(new byte[] {
77                 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 });
78         this.defaultMtu = (defaultMtu > IPV6_MIN_MTU) ? defaultMtu : IPV6_MIN_MTU;
79     }
80 
81     @Override
toString()82     public String toString() {
83         return String.format("%s/%d/%s/%d", name, index, macAddr, defaultMtu);
84     }
85 
getNetworkInterfaceByName(String name)86     private static NetworkInterface getNetworkInterfaceByName(String name) {
87         try {
88             return NetworkInterface.getByName(name);
89         } catch (NullPointerException|SocketException e) {
90             return null;
91         }
92     }
93 
getMacAddress(NetworkInterface netif)94     private static MacAddress getMacAddress(NetworkInterface netif) {
95         try {
96             return MacAddress.fromBytes(netif.getHardwareAddress());
97         } catch (IllegalArgumentException|NullPointerException|SocketException e) {
98             return null;
99         }
100     }
101 }
102