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.RequiresApi; 23 import android.os.Build; 24 import android.system.NetlinkSocketAddress; 25 26 import java.io.FileDescriptor; 27 import java.io.IOException; 28 import java.net.SocketAddress; 29 30 /** 31 * Collection of utilities to interact with raw sockets. 32 * 33 * This class also provides utilities to interact with {@link android.net.util.SocketUtils} 34 * because it is in the API surface could not be simply move the frameworks/libs/net/ 35 * to share with module. 36 * 37 * TODO: deprecate android.net.util.SocketUtils and replace with this class. 38 */ 39 public class SocketUtils { 40 41 /** 42 * Make a socket address to communicate with netlink. 43 */ 44 @NonNull @RequiresApi(Build.VERSION_CODES.S) makeNetlinkSocketAddress(int portId, int groupsMask)45 public static SocketAddress makeNetlinkSocketAddress(int portId, int groupsMask) { 46 return new NetlinkSocketAddress(portId, groupsMask); 47 } 48 49 /** 50 * Close a socket, ignoring any exception while closing. 51 */ closeSocketQuietly(FileDescriptor fd)52 public static void closeSocketQuietly(FileDescriptor fd) { 53 try { 54 closeSocket(fd); 55 } catch (IOException ignored) { 56 } 57 } 58 SocketUtils()59 private SocketUtils() {} 60 } 61