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