markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package com.android.server.connectivity; |
| 17 | |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 18 | import static android.net.SocketKeepalive.DATA_RECEIVED; |
| 19 | import static android.net.SocketKeepalive.ERROR_INVALID_SOCKET; |
| 20 | import static android.net.SocketKeepalive.ERROR_SOCKET_NOT_IDLE; |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 21 | import static android.net.SocketKeepalive.ERROR_UNSUPPORTED; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 22 | import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_ERROR; |
| 23 | import static android.os.MessageQueue.OnFileDescriptorEventListener.EVENT_INPUT; |
markchien | ce7fdd4 | 2019-02-20 22:47:08 +0800 | [diff] [blame] | 24 | import static android.system.OsConstants.ENOPROTOOPT; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 25 | import static android.system.OsConstants.FIONREAD; |
markchien | 1fc82b2 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 26 | import static android.system.OsConstants.IPPROTO_IP; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 27 | import static android.system.OsConstants.IPPROTO_TCP; |
markchien | 1fc82b2 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 28 | import static android.system.OsConstants.IP_TOS; |
| 29 | import static android.system.OsConstants.IP_TTL; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 30 | import static android.system.OsConstants.TIOCOUTQ; |
| 31 | |
| 32 | import android.annotation.NonNull; |
Aaron Huang | 17c660d | 2019-10-02 01:39:46 +0800 | [diff] [blame^] | 33 | import android.net.InvalidPacketException; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 34 | import android.net.NetworkUtils; |
| 35 | import android.net.SocketKeepalive.InvalidSocketException; |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 36 | import android.net.TcpKeepalivePacketData; |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 37 | import android.net.TcpKeepalivePacketDataParcelable; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 38 | import android.net.TcpRepairWindow; |
| 39 | import android.os.Handler; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 40 | import android.os.MessageQueue; |
| 41 | import android.os.Messenger; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 42 | import android.system.ErrnoException; |
| 43 | import android.system.Int32Ref; |
| 44 | import android.system.Os; |
| 45 | import android.util.Log; |
| 46 | import android.util.SparseArray; |
| 47 | |
| 48 | import com.android.internal.annotations.GuardedBy; |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 49 | import com.android.server.connectivity.KeepaliveTracker.KeepaliveInfo; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 50 | |
| 51 | import java.io.FileDescriptor; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 52 | import java.net.InetSocketAddress; |
| 53 | import java.net.SocketAddress; |
| 54 | import java.net.SocketException; |
| 55 | |
| 56 | /** |
| 57 | * Manage tcp socket which offloads tcp keepalive. |
| 58 | * |
| 59 | * The input socket will be changed to repair mode and the application |
| 60 | * will not have permission to read/write data. If the application wants |
| 61 | * to write data, it must stop tcp keepalive offload to leave repair mode |
| 62 | * first. If a remote packet arrives, repair mode will be turned off and |
| 63 | * offload will be stopped. The application will receive a callback to know |
| 64 | * it can start reading data. |
| 65 | * |
| 66 | * {start,stop}SocketMonitor are thread-safe, but care must be taken in the |
| 67 | * order in which they are called. Please note that while calling |
| 68 | * {@link #startSocketMonitor(FileDescriptor, Messenger, int)} multiple times |
| 69 | * with either the same slot or the same FileDescriptor without stopping it in |
| 70 | * between will result in an exception, calling {@link #stopSocketMonitor(int)} |
| 71 | * multiple times with the same int is explicitly a no-op. |
| 72 | * Please also note that switching the socket to repair mode is not synchronized |
| 73 | * with either of these operations and has to be done in an orderly fashion |
| 74 | * with stopSocketMonitor. Take care in calling these in the right order. |
| 75 | * @hide |
| 76 | */ |
| 77 | public class TcpKeepaliveController { |
| 78 | private static final String TAG = "TcpKeepaliveController"; |
| 79 | private static final boolean DBG = false; |
| 80 | |
| 81 | private final MessageQueue mFdHandlerQueue; |
| 82 | |
| 83 | private static final int FD_EVENTS = EVENT_INPUT | EVENT_ERROR; |
| 84 | |
| 85 | // Reference include/uapi/linux/tcp.h |
| 86 | private static final int TCP_REPAIR = 19; |
| 87 | private static final int TCP_REPAIR_QUEUE = 20; |
| 88 | private static final int TCP_QUEUE_SEQ = 21; |
| 89 | private static final int TCP_NO_QUEUE = 0; |
| 90 | private static final int TCP_RECV_QUEUE = 1; |
| 91 | private static final int TCP_SEND_QUEUE = 2; |
| 92 | private static final int TCP_REPAIR_OFF = 0; |
| 93 | private static final int TCP_REPAIR_ON = 1; |
| 94 | // Reference include/uapi/linux/sockios.h |
| 95 | private static final int SIOCINQ = FIONREAD; |
| 96 | private static final int SIOCOUTQ = TIOCOUTQ; |
| 97 | |
| 98 | /** |
| 99 | * Keeps track of packet listeners. |
| 100 | * Key: slot number of keepalive offload. |
| 101 | * Value: {@link FileDescriptor} being listened to. |
| 102 | */ |
| 103 | @GuardedBy("mListeners") |
| 104 | private final SparseArray<FileDescriptor> mListeners = new SparseArray<>(); |
| 105 | |
| 106 | public TcpKeepaliveController(final Handler connectivityServiceHandler) { |
| 107 | mFdHandlerQueue = connectivityServiceHandler.getLooper().getQueue(); |
| 108 | } |
| 109 | |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 110 | /** Build tcp keepalive packet. */ |
| 111 | public static TcpKeepalivePacketData getTcpKeepalivePacket(@NonNull FileDescriptor fd) |
| 112 | throws InvalidPacketException, InvalidSocketException { |
| 113 | try { |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 114 | final TcpKeepalivePacketDataParcelable tcpDetails = switchToRepairMode(fd); |
| 115 | return TcpKeepalivePacketData.tcpKeepalivePacket(tcpDetails); |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 116 | } catch (InvalidPacketException | InvalidSocketException e) { |
| 117 | switchOutOfRepairMode(fd); |
| 118 | throw e; |
| 119 | } |
| 120 | } |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 121 | /** |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 122 | * Switch the tcp socket to repair mode and query detail tcp information. |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 123 | * |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 124 | * @param fd the fd of socket on which to use keepalive offload. |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 125 | * @return a {@link TcpKeepalivePacketData#TcpKeepalivePacketDataParcelable} object for current |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 126 | * tcp/ip information. |
| 127 | */ |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 128 | private static TcpKeepalivePacketDataParcelable switchToRepairMode(FileDescriptor fd) |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 129 | throws InvalidSocketException { |
| 130 | if (DBG) Log.i(TAG, "switchToRepairMode to start tcp keepalive : " + fd); |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 131 | final TcpKeepalivePacketDataParcelable tcpDetails = new TcpKeepalivePacketDataParcelable(); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 132 | final SocketAddress srcSockAddr; |
| 133 | final SocketAddress dstSockAddr; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 134 | final TcpRepairWindow trw; |
| 135 | |
| 136 | // Query source address and port. |
| 137 | try { |
| 138 | srcSockAddr = Os.getsockname(fd); |
| 139 | } catch (ErrnoException e) { |
| 140 | Log.e(TAG, "Get sockname fail: ", e); |
| 141 | throw new InvalidSocketException(ERROR_INVALID_SOCKET, e); |
| 142 | } |
| 143 | if (srcSockAddr instanceof InetSocketAddress) { |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 144 | tcpDetails.srcAddress = getAddress((InetSocketAddress) srcSockAddr); |
| 145 | tcpDetails.srcPort = getPort((InetSocketAddress) srcSockAddr); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 146 | } else { |
| 147 | Log.e(TAG, "Invalid or mismatched SocketAddress"); |
| 148 | throw new InvalidSocketException(ERROR_INVALID_SOCKET); |
| 149 | } |
| 150 | // Query destination address and port. |
| 151 | try { |
| 152 | dstSockAddr = Os.getpeername(fd); |
| 153 | } catch (ErrnoException e) { |
| 154 | Log.e(TAG, "Get peername fail: ", e); |
| 155 | throw new InvalidSocketException(ERROR_INVALID_SOCKET, e); |
| 156 | } |
| 157 | if (dstSockAddr instanceof InetSocketAddress) { |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 158 | tcpDetails.dstAddress = getAddress((InetSocketAddress) dstSockAddr); |
| 159 | tcpDetails.dstPort = getPort((InetSocketAddress) dstSockAddr); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 160 | } else { |
| 161 | Log.e(TAG, "Invalid or mismatched peer SocketAddress"); |
| 162 | throw new InvalidSocketException(ERROR_INVALID_SOCKET); |
| 163 | } |
| 164 | |
| 165 | // Query sequence and ack number |
| 166 | dropAllIncomingPackets(fd, true); |
| 167 | try { |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 168 | // Switch to tcp repair mode. |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 169 | Os.setsockoptInt(fd, IPPROTO_TCP, TCP_REPAIR, TCP_REPAIR_ON); |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 170 | |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 171 | // Check if socket is idle. |
| 172 | if (!isSocketIdle(fd)) { |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 173 | Log.e(TAG, "Socket is not idle"); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 174 | throw new InvalidSocketException(ERROR_SOCKET_NOT_IDLE); |
| 175 | } |
| 176 | // Query write sequence number from SEND_QUEUE. |
| 177 | Os.setsockoptInt(fd, IPPROTO_TCP, TCP_REPAIR_QUEUE, TCP_SEND_QUEUE); |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 178 | tcpDetails.seq = Os.getsockoptInt(fd, IPPROTO_TCP, TCP_QUEUE_SEQ); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 179 | // Query read sequence number from RECV_QUEUE. |
| 180 | Os.setsockoptInt(fd, IPPROTO_TCP, TCP_REPAIR_QUEUE, TCP_RECV_QUEUE); |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 181 | tcpDetails.ack = Os.getsockoptInt(fd, IPPROTO_TCP, TCP_QUEUE_SEQ); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 182 | // Switch to NO_QUEUE to prevent illegal socket read/write in repair mode. |
| 183 | Os.setsockoptInt(fd, IPPROTO_TCP, TCP_REPAIR_QUEUE, TCP_NO_QUEUE); |
| 184 | // Finally, check if socket is still idle. TODO : this check needs to move to |
| 185 | // after starting polling to prevent a race. |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 186 | if (!isReceiveQueueEmpty(fd)) { |
| 187 | Log.e(TAG, "Fatal: receive queue of this socket is not empty"); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 188 | throw new InvalidSocketException(ERROR_INVALID_SOCKET); |
| 189 | } |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 190 | if (!isSendQueueEmpty(fd)) { |
| 191 | Log.e(TAG, "Socket is not idle"); |
| 192 | throw new InvalidSocketException(ERROR_SOCKET_NOT_IDLE); |
| 193 | } |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 194 | |
| 195 | // Query tcp window size. |
| 196 | trw = NetworkUtils.getTcpRepairWindow(fd); |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 197 | tcpDetails.rcvWnd = trw.rcvWnd; |
| 198 | tcpDetails.rcvWndScale = trw.rcvWndScale; |
markchien | 1fc82b2 | 2019-03-21 22:26:54 +0800 | [diff] [blame] | 199 | if (tcpDetails.srcAddress.length == 4 /* V4 address length */) { |
| 200 | // Query TOS. |
| 201 | tcpDetails.tos = Os.getsockoptInt(fd, IPPROTO_IP, IP_TOS); |
| 202 | // Query TTL. |
| 203 | tcpDetails.ttl = Os.getsockoptInt(fd, IPPROTO_IP, IP_TTL); |
| 204 | } |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 205 | } catch (ErrnoException e) { |
| 206 | Log.e(TAG, "Exception reading TCP state from socket", e); |
markchien | ce7fdd4 | 2019-02-20 22:47:08 +0800 | [diff] [blame] | 207 | if (e.errno == ENOPROTOOPT) { |
| 208 | // ENOPROTOOPT may happen in kernel version lower than 4.8. |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 209 | // Treat it as ERROR_UNSUPPORTED. |
| 210 | throw new InvalidSocketException(ERROR_UNSUPPORTED, e); |
markchien | ce7fdd4 | 2019-02-20 22:47:08 +0800 | [diff] [blame] | 211 | } else { |
| 212 | throw new InvalidSocketException(ERROR_INVALID_SOCKET, e); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 213 | } |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 214 | } finally { |
| 215 | dropAllIncomingPackets(fd, false); |
| 216 | } |
| 217 | |
| 218 | // Keepalive sequence number is last sequence number - 1. If it couldn't be retrieved, |
| 219 | // then it must be set to -1, so decrement in all cases. |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 220 | tcpDetails.seq = tcpDetails.seq - 1; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 221 | |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 222 | return tcpDetails; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 223 | } |
| 224 | |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 225 | /** |
| 226 | * Switch the tcp socket out of repair mode. |
| 227 | * |
| 228 | * @param fd the fd of socket to switch back to normal. |
| 229 | */ |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 230 | private static void switchOutOfRepairMode(@NonNull final FileDescriptor fd) { |
| 231 | try { |
| 232 | Os.setsockoptInt(fd, IPPROTO_TCP, TCP_REPAIR, TCP_REPAIR_OFF); |
| 233 | } catch (ErrnoException e) { |
| 234 | Log.e(TAG, "Cannot switch socket out of repair mode", e); |
| 235 | // Well, there is not much to do here to recover |
| 236 | } |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Start monitoring incoming packets. |
| 241 | * |
| 242 | * @param fd socket fd to monitor. |
junyulai | 1d5cd19 | 2019-03-04 11:49:17 +0800 | [diff] [blame] | 243 | * @param ki a {@link KeepaliveInfo} that tracks information about a socket keepalive. |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 244 | * @param slot keepalive slot. |
| 245 | */ |
| 246 | public void startSocketMonitor(@NonNull final FileDescriptor fd, |
junyulai | 1d5cd19 | 2019-03-04 11:49:17 +0800 | [diff] [blame] | 247 | @NonNull final KeepaliveInfo ki, final int slot) |
| 248 | throws IllegalArgumentException, InvalidSocketException { |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 249 | synchronized (mListeners) { |
| 250 | if (null != mListeners.get(slot)) { |
| 251 | throw new IllegalArgumentException("This slot is already taken"); |
| 252 | } |
| 253 | for (int i = 0; i < mListeners.size(); ++i) { |
| 254 | if (fd.equals(mListeners.valueAt(i))) { |
junyulai | 1d5cd19 | 2019-03-04 11:49:17 +0800 | [diff] [blame] | 255 | Log.e(TAG, "This fd is already registered."); |
| 256 | throw new InvalidSocketException(ERROR_INVALID_SOCKET); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | mFdHandlerQueue.addOnFileDescriptorEventListener(fd, FD_EVENTS, (readyFd, events) -> { |
| 260 | // This can't be called twice because the queue guarantees that once the listener |
| 261 | // is unregistered it can't be called again, even for a message that arrived |
| 262 | // before it was unregistered. |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 263 | final int reason; |
| 264 | if (0 != (events & EVENT_ERROR)) { |
| 265 | reason = ERROR_INVALID_SOCKET; |
| 266 | } else { |
| 267 | reason = DATA_RECEIVED; |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 268 | } |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 269 | ki.onFileDescriptorInitiatedStop(reason); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 270 | // The listener returns the new set of events to listen to. Because 0 means no |
| 271 | // event, the listener gets unregistered. |
| 272 | return 0; |
| 273 | }); |
| 274 | mListeners.put(slot, fd); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /** Stop socket monitor */ |
| 279 | // This slot may have been stopped automatically already because the socket received data, |
| 280 | // was closed on the other end or otherwise suffered some error. In this case, this function |
| 281 | // is a no-op. |
| 282 | public void stopSocketMonitor(final int slot) { |
| 283 | final FileDescriptor fd; |
| 284 | synchronized (mListeners) { |
| 285 | fd = mListeners.get(slot); |
| 286 | if (null == fd) return; |
| 287 | mListeners.remove(slot); |
| 288 | } |
| 289 | mFdHandlerQueue.removeOnFileDescriptorEventListener(fd); |
markchien | 656fbe9 | 2019-02-20 21:16:44 +0800 | [diff] [blame] | 290 | if (DBG) Log.d(TAG, "Moving socket out of repair mode for stop : " + fd); |
| 291 | switchOutOfRepairMode(fd); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 292 | } |
| 293 | |
markchien | d42fca6 | 2019-03-19 21:25:33 +0800 | [diff] [blame] | 294 | private static byte [] getAddress(InetSocketAddress inetAddr) { |
| 295 | return inetAddr.getAddress().getAddress(); |
markchien | 150e191 | 2018-12-27 22:49:51 +0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | private static int getPort(InetSocketAddress inetAddr) { |
| 299 | return inetAddr.getPort(); |
| 300 | } |
| 301 | |
| 302 | private static boolean isSocketIdle(FileDescriptor fd) throws ErrnoException { |
| 303 | return isReceiveQueueEmpty(fd) && isSendQueueEmpty(fd); |
| 304 | } |
| 305 | |
| 306 | private static boolean isReceiveQueueEmpty(FileDescriptor fd) |
| 307 | throws ErrnoException { |
| 308 | Int32Ref result = new Int32Ref(-1); |
| 309 | Os.ioctlInt(fd, SIOCINQ, result); |
| 310 | if (result.value != 0) { |
| 311 | Log.e(TAG, "Read queue has data"); |
| 312 | return false; |
| 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | private static boolean isSendQueueEmpty(FileDescriptor fd) |
| 318 | throws ErrnoException { |
| 319 | Int32Ref result = new Int32Ref(-1); |
| 320 | Os.ioctlInt(fd, SIOCOUTQ, result); |
| 321 | if (result.value != 0) { |
| 322 | Log.e(TAG, "Write queue has data"); |
| 323 | return false; |
| 324 | } |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | private static void dropAllIncomingPackets(FileDescriptor fd, boolean enable) |
| 329 | throws InvalidSocketException { |
| 330 | try { |
| 331 | if (enable) { |
| 332 | NetworkUtils.attachDropAllBPFFilter(fd); |
| 333 | } else { |
| 334 | NetworkUtils.detachBPFFilter(fd); |
| 335 | } |
| 336 | } catch (SocketException e) { |
| 337 | Log.e(TAG, "Socket Exception: ", e); |
| 338 | throw new InvalidSocketException(ERROR_INVALID_SOCKET, e); |
| 339 | } |
| 340 | } |
| 341 | } |