• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 android.net.util;
18 
19 import static android.system.OsConstants.SOL_SOCKET;
20 import static android.system.OsConstants.SO_BINDTODEVICE;
21 
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.SystemApi;
25 import android.annotation.TestApi;
26 import android.net.NetworkUtils;
27 import android.system.ErrnoException;
28 import android.system.NetlinkSocketAddress;
29 import android.system.Os;
30 import android.system.PacketSocketAddress;
31 
32 import libcore.io.IoBridge;
33 
34 import java.io.FileDescriptor;
35 import java.io.IOException;
36 import java.net.SocketAddress;
37 
38 /**
39  * Collection of utilities to interact with raw sockets.
40  * @hide
41  */
42 @SystemApi
43 @TestApi
44 public final class SocketUtils {
45     /**
46      * Create a raw datagram socket that is bound to an interface.
47      *
48      * <p>Data sent through the socket will go directly to the underlying network, ignoring VPNs.
49      */
bindSocketToInterface(@onNull FileDescriptor socket, @NonNull String iface)50     public static void bindSocketToInterface(@NonNull FileDescriptor socket, @NonNull String iface)
51             throws ErrnoException {
52         // SO_BINDTODEVICE actually takes a string. This works because the first member
53         // of struct ifreq is a NULL-terminated interface name.
54         // TODO: add a setsockoptString()
55         Os.setsockoptIfreq(socket, SOL_SOCKET, SO_BINDTODEVICE, iface);
56         NetworkUtils.protectFromVpn(socket);
57     }
58 
59     /**
60      * Make a socket address to communicate with netlink.
61      */
62     @NonNull
makeNetlinkSocketAddress(int portId, int groupsMask)63     public static SocketAddress makeNetlinkSocketAddress(int portId, int groupsMask) {
64         return new NetlinkSocketAddress(portId, groupsMask);
65     }
66 
67     /**
68      * Make socket address that packet sockets can bind to.
69      */
70     @NonNull
makePacketSocketAddress(int protocol, int ifIndex)71     public static SocketAddress makePacketSocketAddress(int protocol, int ifIndex) {
72         return new PacketSocketAddress((short) protocol, ifIndex);
73     }
74 
75     /**
76      * Make a socket address that packet socket can send packets to.
77      */
78     @NonNull
makePacketSocketAddress(int ifIndex, @NonNull byte[] hwAddr)79     public static SocketAddress makePacketSocketAddress(int ifIndex, @NonNull byte[] hwAddr) {
80         return new PacketSocketAddress(ifIndex, hwAddr);
81     }
82 
83     /**
84      * @see IoBridge#closeAndSignalBlockedThreads(FileDescriptor)
85      */
closeSocket(@ullable FileDescriptor fd)86     public static void closeSocket(@Nullable FileDescriptor fd) throws IOException {
87         IoBridge.closeAndSignalBlockedThreads(fd);
88     }
89 
SocketUtils()90     private SocketUtils() {}
91 }
92