Aurimas Liutikas | 88c7ff1 | 2023-08-10 12:42:26 -0700 | [diff] [blame] | 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 com.android.net.module.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 | |
| 48 | /** |
| 49 | * Return InterfaceParams corresponding with an interface name |
| 50 | * @param name the interface name |
| 51 | */ |
| 52 | public static InterfaceParams getByName(String name) { |
| 53 | final NetworkInterface netif = getNetworkInterfaceByName(name); |
| 54 | if (netif == null) return null; |
| 55 | |
| 56 | // Not all interfaces have MAC addresses, e.g. rmnet_data0. |
| 57 | final MacAddress macAddr = getMacAddress(netif); |
| 58 | |
| 59 | try { |
| 60 | return new InterfaceParams(name, netif.getIndex(), macAddr, netif.getMTU()); |
| 61 | } catch (IllegalArgumentException | SocketException e) { |
| 62 | return null; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public InterfaceParams(String name, int index, MacAddress macAddr) { |
| 67 | this(name, index, macAddr, ETHER_MTU); |
| 68 | } |
| 69 | |
| 70 | public InterfaceParams(String name, int index, MacAddress macAddr, int defaultMtu) { |
| 71 | if (TextUtils.isEmpty(name)) { |
| 72 | throw new IllegalArgumentException("impossible interface name"); |
| 73 | } |
| 74 | |
| 75 | if (index <= 0) throw new IllegalArgumentException("invalid interface index"); |
| 76 | |
| 77 | this.name = name; |
| 78 | this.index = index; |
| 79 | this.hasMacAddress = (macAddr != null); |
| 80 | this.macAddr = hasMacAddress ? macAddr : MacAddress.fromBytes(new byte[] { |
| 81 | 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 }); |
| 82 | this.defaultMtu = (defaultMtu > IPV6_MIN_MTU) ? defaultMtu : IPV6_MIN_MTU; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return String.format("%s/%d/%s/%d", name, index, macAddr, defaultMtu); |
| 88 | } |
| 89 | |
| 90 | private static NetworkInterface getNetworkInterfaceByName(String name) { |
| 91 | try { |
| 92 | return NetworkInterface.getByName(name); |
| 93 | } catch (NullPointerException | SocketException e) { |
| 94 | return null; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private static MacAddress getMacAddress(NetworkInterface netif) { |
| 99 | try { |
| 100 | return MacAddress.fromBytes(netif.getHardwareAddress()); |
| 101 | } catch (IllegalArgumentException | NullPointerException | SocketException e) { |
| 102 | return null; |
| 103 | } |
| 104 | } |
| 105 | } |