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