1 /* 2 * Copyright (C) 2023 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.server.connectivity.mdns; 18 19 import android.annotation.Nullable; 20 import android.net.Network; 21 22 import java.util.Objects; 23 24 /** 25 * A class that identifies a socket. 26 * 27 * <p> A socket is typically created with an associated network. However, tethering interfaces do 28 * not have an associated network, only an interface index. This means that the socket cannot be 29 * identified in some places. Therefore, this class is necessary for identifying a socket. It 30 * includes both the network and interface index. 31 */ 32 public class SocketKey { 33 @Nullable 34 private final Network mNetwork; 35 private final int mInterfaceIndex; 36 SocketKey(int interfaceIndex)37 SocketKey(int interfaceIndex) { 38 this(null /* network */, interfaceIndex); 39 } 40 SocketKey(@ullable Network network, int interfaceIndex)41 SocketKey(@Nullable Network network, int interfaceIndex) { 42 mNetwork = network; 43 mInterfaceIndex = interfaceIndex; 44 } 45 46 @Nullable getNetwork()47 public Network getNetwork() { 48 return mNetwork; 49 } 50 getInterfaceIndex()51 public int getInterfaceIndex() { 52 return mInterfaceIndex; 53 } 54 55 @Override hashCode()56 public int hashCode() { 57 return Objects.hash(mNetwork, mInterfaceIndex); 58 } 59 60 @Override equals(@ullable Object other)61 public boolean equals(@Nullable Object other) { 62 if (!(other instanceof SocketKey)) { 63 return false; 64 } 65 return Objects.equals(mNetwork, ((SocketKey) other).mNetwork) 66 && mInterfaceIndex == ((SocketKey) other).mInterfaceIndex; 67 } 68 69 @Override toString()70 public String toString() { 71 return "SocketKey{ network=" + mNetwork + " interfaceIndex=" + mInterfaceIndex + " }"; 72 } 73 } 74