1 /* 2 * Copyright (C) 2022 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 static android.net.util.SocketUtils.closeSocket; 20 21 import android.annotation.NonNull; 22 import android.annotation.SuppressLint; 23 import android.system.NetlinkSocketAddress; 24 25 import java.io.FileDescriptor; 26 import java.io.IOException; 27 import java.net.SocketAddress; 28 29 /** 30 * Collection of utilities to interact with raw sockets. 31 * 32 * This class also provides utilities to interact with {@link android.net.util.SocketUtils} 33 * because it is in the API surface could not be simply move the frameworks/libs/net/ 34 * to share with module. 35 * 36 * TODO: deprecate android.net.util.SocketUtils and replace with this class. 37 */ 38 public class SocketUtils { 39 40 /** 41 * Make a socket address to communicate with netlink. 42 */ 43 // NetlinkSocketAddress was CorePlatformApi on R and linter warns this is available on S+. 44 // android.net.util.SocketUtils.makeNetlinkSocketAddress can be used instead, but this method 45 // has been used on R, so suppress the linter and keep as it is. 46 @SuppressLint("NewApi") 47 @NonNull makeNetlinkSocketAddress(int portId, int groupsMask)48 public static SocketAddress makeNetlinkSocketAddress(int portId, int groupsMask) { 49 return new NetlinkSocketAddress(portId, groupsMask); 50 } 51 52 /** 53 * Close a socket, ignoring any exception while closing. 54 */ closeSocketQuietly(FileDescriptor fd)55 public static void closeSocketQuietly(FileDescriptor fd) { 56 try { 57 closeSocket(fd); 58 } catch (IOException ignored) { 59 } 60 } 61 SocketUtils()62 private SocketUtils() {} 63 } 64