1 /*
2  * Copyright (C) 2011 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 libcore.io;
18 
19 import android.annotation.SystemApi;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.system.ErrnoException;
22 import android.system.StructGroupReq;
23 import android.system.StructLinger;
24 import android.system.StructPollfd;
25 import android.system.StructTimeval;
26 
27 import java.io.FileDescriptor;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.net.BindException;
31 import java.net.ConnectException;
32 import java.net.DatagramPacket;
33 import java.net.Inet4Address;
34 import java.net.Inet6Address;
35 import java.net.InetAddress;
36 import java.net.InetSocketAddress;
37 import java.net.NetworkInterface;
38 import java.net.NoRouteToHostException;
39 import java.net.PortUnreachableException;
40 import java.net.Socket;
41 import java.net.SocketAddress;
42 import java.net.SocketException;
43 import java.net.SocketOptions;
44 import java.net.SocketTimeoutException;
45 import java.net.UnknownHostException;
46 import java.nio.ByteBuffer;
47 import java.util.concurrent.TimeUnit;
48 
49 import libcore.util.ArrayUtils;
50 import libcore.util.NonNull;
51 import libcore.util.Nullable;
52 
53 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
54 import static android.system.OsConstants.*;
55 
56 /**
57  * Collection of utility methods to work with blocking and non-blocking I/O that wrap raw POSIX
58  * system calls, e.g. {@link android.system.Os}. These wrappers are to signal other blocked I/O
59  * threads and avoid boilerplate code of routine error checks when using raw system calls.
60  *
61  * <p>
62  * For example, when using {@link Os#read(FileDescriptor, byte[], int, int)}, return value can
63  * contain:
64  * <ul>
65  *   <li>{@code 0} which means EOF</li>
66  *   <li>{@code N > 0} which means number of bytes read</li>
67  *   <li>{@code -1} which means error, and {@link ErrnoException} is thrown</li>
68  * </ul>
69  *
70  * <p>
71  * {@link ErrnoException} in its turn can be one of:
72  * <ul>
73  *   <li>{@link android.system.OsConstants#EAGAIN} which means the file descriptor refers to a file
74  *       or a socket, which has been marked nonblocking
75  *       ({@link android.system.OsConstants#O_NONBLOCK}), and the read would block</li>
76  *   <li>{@link android.system.OsConstants#EBADF} which means the file descriptor is not a valid
77  *       file descriptor or is not open for reading</li>
78  *   <li>{@link android.system.OsConstants#EFAULT} which means given buffer is outside accessible
79  *       address space</li>
80  *   <li>{@link android.system.OsConstants#EINTR} which means the call was interrupted by a signal
81  *       before any data was read</li>
82  *   <li>{@link android.system.OsConstants#EINVAL} which means the file descriptor is attached to
83  *       an object which is unsuitable for reading; or the file was opened with the
84  *       {@link android.system.OsConstants#O_DIRECT} flag, and either the address specified in
85  *       {@code buffer}, the value specified in {@code count}, or the file {@code offset} is not
86  *       suitably aligned</li>
87  *   <li>{@link android.system.OsConstants#EIO} which means I/O error happened</li>
88  *   <li>{@link android.system.OsConstants#EISDIR} which means the file descriptor refers to a
89  *       directory</li>
90  * </ul>
91  *
92  * All these errors require handling, and this class contains some wrapper methods that handle most
93  * common cases, making usage of system calls more user friendly.
94  *
95  * @hide
96  */
97 @SystemApi(client = MODULE_LIBRARIES)
98 public final class IoBridge {
99 
IoBridge()100     private IoBridge() {
101     }
102 
103     /** @hide */
available(FileDescriptor fd)104     public static int available(FileDescriptor fd) throws IOException {
105         try {
106             int available = Libcore.os.ioctlInt(fd, FIONREAD);
107             if (available < 0) {
108                 // If the fd refers to a regular file, the result is the difference between
109                 // the file size and the file position. This may be negative if the position
110                 // is past the end of the file. If the fd refers to a special file masquerading
111                 // as a regular file, the result may be negative because the special file
112                 // may appear to have zero size and yet a previous read call may have
113                 // read some amount of data and caused the file position to be advanced.
114                 available = 0;
115             }
116             return available;
117         } catch (ErrnoException errnoException) {
118             if (errnoException.errno == ENOTTY) {
119                 // The fd is unwilling to opine about its read buffer.
120                 return 0;
121             }
122             throw errnoException.rethrowAsIOException();
123         }
124     }
125 
126     /** @hide */
bind(FileDescriptor fd, InetAddress address, int port)127     public static void bind(FileDescriptor fd, InetAddress address, int port) throws SocketException {
128         if (address instanceof Inet6Address) {
129             Inet6Address inet6Address = (Inet6Address) address;
130             if (inet6Address.getScopeId() == 0 && inet6Address.isLinkLocalAddress()) {
131                 // Linux won't let you bind a link-local address without a scope id.
132                 // Find one.
133                 NetworkInterface nif = NetworkInterface.getByInetAddress(address);
134                 if (nif == null) {
135                     throw new SocketException("Can't bind to a link-local address without a scope id: " + address);
136                 }
137                 try {
138                     address = Inet6Address.getByAddress(address.getHostName(), address.getAddress(), nif.getIndex());
139                 } catch (UnknownHostException ex) {
140                     throw new AssertionError(ex); // Can't happen.
141                 }
142             }
143         }
144         try {
145             Libcore.os.bind(fd, address, port);
146         } catch (ErrnoException errnoException) {
147             if (errnoException.errno == EADDRINUSE || errnoException.errno == EADDRNOTAVAIL ||
148                 errnoException.errno == EPERM || errnoException.errno == EACCES) {
149                 throw new BindException(errnoException.getMessage(), errnoException);
150             } else {
151                 throw new SocketException(errnoException.getMessage(), errnoException);
152             }
153         }
154     }
155 
156 
157     /**
158      * Connects socket 'fd' to 'inetAddress' on 'port', with no timeout. The lack of a timeout
159      * means this method won't throw SocketTimeoutException.
160      *
161      * @hide
162      */
connect(FileDescriptor fd, InetAddress inetAddress, int port)163     public static void connect(FileDescriptor fd, InetAddress inetAddress, int port) throws SocketException {
164         try {
165             IoBridge.connect(fd, inetAddress, port, 0);
166         } catch (SocketTimeoutException ex) {
167             throw new AssertionError(ex); // Can't happen for a connect without a timeout.
168         }
169     }
170 
171     /**
172      * Connects socket 'fd' to 'inetAddress' on 'port', with a the given 'timeoutMs'.
173      * Use timeoutMs == 0 for a blocking connect with no timeout.
174      *
175      * @hide
176      */
connect(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs)177     public static void connect(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs) throws SocketException, SocketTimeoutException {
178         try {
179             connectErrno(fd, inetAddress, port, timeoutMs);
180         } catch (ErrnoException errnoException) {
181             if (errnoException.errno == EHOSTUNREACH) {
182                 throw new NoRouteToHostException("Host unreachable");
183             }
184             if (errnoException.errno == EADDRNOTAVAIL) {
185                 throw new NoRouteToHostException("Address not available");
186             }
187             throw new ConnectException(createMessageForException(fd, inetAddress, port, timeoutMs,
188                     errnoException), errnoException);
189         } catch (SocketException ex) {
190             throw ex; // We don't want to doubly wrap these.
191         } catch (SocketTimeoutException ex) {
192             throw ex; // We don't want to doubly wrap these.
193         } catch (IOException ex) {
194             throw new SocketException(ex);
195         }
196     }
197 
connectErrno(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs)198     private static void connectErrno(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs) throws ErrnoException, IOException {
199         // With no timeout, just call connect(2) directly.
200         if (timeoutMs <= 0) {
201             Libcore.os.connect(fd, inetAddress, port);
202             return;
203         }
204 
205         // For connect with a timeout, we:
206         //   1. set the socket to non-blocking,
207         //   2. connect(2),
208         //   3. loop using poll(2) to decide whether we're connected, whether we should keep
209         //      waiting, or whether we've seen a permanent failure and should give up,
210         //   4. set the socket back to blocking.
211 
212         // 1. set the socket to non-blocking.
213         IoUtils.setBlocking(fd, false);
214 
215         // 2. call connect(2) non-blocking.
216         long finishTimeNanos = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMs);
217         try {
218             Libcore.os.connect(fd, inetAddress, port);
219             IoUtils.setBlocking(fd, true); // 4. set the socket back to blocking.
220             return; // We connected immediately.
221         } catch (ErrnoException errnoException) {
222             if (errnoException.errno != EINPROGRESS) {
223                 throw errnoException;
224             }
225             // EINPROGRESS means we should keep trying...
226         }
227 
228         // 3. loop using poll(2).
229         int remainingTimeoutMs;
230         do {
231             remainingTimeoutMs =
232                     (int) TimeUnit.NANOSECONDS.toMillis(finishTimeNanos - System.nanoTime());
233             if (remainingTimeoutMs <= 0) {
234                 throw new SocketTimeoutException(
235                         createMessageForException(fd, inetAddress, port, timeoutMs, null));
236             }
237         } while (!IoBridge.isConnected(fd, inetAddress, port, timeoutMs, remainingTimeoutMs));
238         IoUtils.setBlocking(fd, true); // 4. set the socket back to blocking.
239     }
240 
241     /**
242      * Constructs the message for an exception that the caller is about to throw.
243      *
244      * @hide
245      */
createMessageForException(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs, Exception causeOrNull)246     private static String createMessageForException(FileDescriptor fd, InetAddress inetAddress,
247             int port, int timeoutMs, Exception causeOrNull) {
248         // Figure out source address from fd.
249         InetSocketAddress localAddress = null;
250         try {
251             localAddress = getLocalInetSocketAddress(fd);
252         } catch (SocketException ignored) {
253             // The caller is about to throw an exception, so this one would only distract.
254         }
255 
256         StringBuilder sb = new StringBuilder("failed to connect")
257               .append(" to ")
258               .append(inetAddress)
259               .append(" (port ")
260               .append(port)
261               .append(")");
262         if (localAddress != null) {
263             sb.append(" from ")
264               .append(localAddress.getAddress())
265               .append(" (port ")
266               .append(localAddress.getPort())
267               .append(")");
268         }
269         if (timeoutMs > 0) {
270             sb.append(" after ")
271               .append(timeoutMs)
272               .append("ms");
273         }
274         if (causeOrNull != null) {
275             sb.append(": ")
276               .append(causeOrNull.getMessage());
277         }
278         return sb.toString();
279     }
280 
281     /**
282      * Closes the Unix file descriptor associated with the supplied file descriptor, resets the
283      * internal int to -1, and sends a signal to any threads are currently blocking. In order for
284      * the signal to be sent the blocked threads must have registered with the
285      * {@link AsynchronousCloseMonitor} before they entered the blocking operation. {@code fd} will be
286      * invalid after this call.
287      *
288      * <p>This method is a no-op if passed a {@code null} or already-closed file descriptor.
289      *
290      * @param fd file descriptor to be closed
291      * @throws IOException if underlying system call fails with {@link ErrnoException}
292      *
293      * @hide
294      */
295     @SystemApi(client = MODULE_LIBRARIES)
closeAndSignalBlockedThreads(@onNull FileDescriptor fd)296     public static void closeAndSignalBlockedThreads(@NonNull FileDescriptor fd) throws IOException {
297         if (fd == null) {
298             return;
299         }
300 
301         // fd is invalid after we call release$.
302         // If multiple threads reach this point simultaneously, release$ is synchronized, so one
303         // of them will receive the old fd, and the rest will get an empty FileDescriptor.
304         FileDescriptor oldFd = fd.release$();
305         if (!oldFd.valid()) {
306             return;
307         }
308 
309         AsynchronousCloseMonitor.signalBlockedThreads(oldFd);
310         try {
311             Libcore.os.close(oldFd);
312         } catch (ErrnoException errnoException) {
313             throw errnoException.rethrowAsIOException();
314         }
315     }
316 
317     /** @hide */
318     @UnsupportedAppUsage
isConnected(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs, int remainingTimeoutMs)319     public static boolean isConnected(FileDescriptor fd, InetAddress inetAddress, int port,
320             int timeoutMs, int remainingTimeoutMs) throws IOException {
321         ErrnoException cause;
322         try {
323             StructPollfd[] pollFds = new StructPollfd[] { new StructPollfd() };
324             pollFds[0].fd = fd;
325             pollFds[0].events = (short) POLLOUT;
326             int rc = Libcore.os.poll(pollFds, remainingTimeoutMs);
327             if (rc == 0) {
328                 return false; // Timeout.
329             }
330             int connectError = Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_ERROR);
331             if (connectError == 0) {
332                 return true; // Success!
333             }
334             throw new ErrnoException("isConnected", connectError); // The connect(2) failed.
335         } catch (ErrnoException errnoException) {
336             if (!fd.valid()) {
337                 throw new SocketException("Socket closed");
338             }
339             cause = errnoException;
340         }
341         String detail = createMessageForException(fd, inetAddress, port, timeoutMs, cause);
342         if (cause.errno == ETIMEDOUT) {
343             SocketTimeoutException e = new SocketTimeoutException(detail);
344             e.initCause(cause);
345             throw e;
346         }
347         throw new ConnectException(detail, cause);
348     }
349 
350     // Socket options used by java.net but not exposed in SocketOptions.
351     /** @hide */
352     public static final int JAVA_MCAST_JOIN_GROUP = 19;
353     /** @hide */
354     public static final int JAVA_MCAST_LEAVE_GROUP = 20;
355     /** @hide */
356     public static final int JAVA_IP_MULTICAST_TTL = 17;
357     /** @hide */
358     public static final int JAVA_IP_TTL = 25;
359 
360     /**
361      * java.net has its own socket options similar to the underlying Unix ones. We paper over the
362      * differences here.
363      * @hide
364      */
getSocketOption(FileDescriptor fd, int option)365     public static Object getSocketOption(FileDescriptor fd, int option) throws SocketException {
366         try {
367             return getSocketOptionErrno(fd, option);
368         } catch (ErrnoException errnoException) {
369             throw errnoException.rethrowAsSocketException();
370         }
371     }
372 
373     @SuppressWarnings("NewApi") // False positive lint limitation, see b/177434707.
getSocketOptionErrno(FileDescriptor fd, int option)374     private static Object getSocketOptionErrno(FileDescriptor fd, int option) throws ErrnoException, SocketException {
375         switch (option) {
376         case SocketOptions.IP_MULTICAST_IF:
377         case SocketOptions.IP_MULTICAST_IF2:
378             return Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF);
379         case SocketOptions.IP_MULTICAST_LOOP:
380             // Since setting this from java.net always sets IPv4 and IPv6 to the same value,
381             // it doesn't matter which we return.
382             // NOTE: getsockopt's return value means "isEnabled", while OpenJDK code java.net
383             // requires a value that means "isDisabled" so we NEGATE the system call value here.
384             return !booleanFromInt(Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP));
385         case IoBridge.JAVA_IP_MULTICAST_TTL:
386             // Since setting this from java.net always sets IPv4 and IPv6 to the same value,
387             // it doesn't matter which we return.
388             return Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
389         case IoBridge.JAVA_IP_TTL:
390             // Since setting this from java.net always sets IPv4 and IPv6 to the same value,
391             // it doesn't matter which we return.
392             return Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS);
393         case SocketOptions.IP_TOS:
394             // Since setting this from java.net always sets IPv4 and IPv6 to the same value,
395             // it doesn't matter which we return.
396             return Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_TCLASS);
397         case SocketOptions.SO_BROADCAST:
398             return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_BROADCAST));
399         case SocketOptions.SO_KEEPALIVE:
400             return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_KEEPALIVE));
401         case SocketOptions.SO_LINGER:
402             StructLinger linger = Libcore.os.getsockoptLinger(fd, SOL_SOCKET, SO_LINGER);
403             if (!linger.isOn()) {
404                 return false;
405             }
406             return linger.l_linger;
407         case SocketOptions.SO_OOBINLINE:
408             return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_OOBINLINE));
409         case SocketOptions.SO_RCVBUF:
410             return Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_RCVBUF);
411         case SocketOptions.SO_REUSEADDR:
412             return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_REUSEADDR));
413         case SocketOptions.SO_SNDBUF:
414             return Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_SNDBUF);
415         case SocketOptions.SO_TIMEOUT:
416             return (int) Libcore.os.getsockoptTimeval(fd, SOL_SOCKET, SO_RCVTIMEO).toMillis();
417         case SocketOptions.TCP_NODELAY:
418             return booleanFromInt(Libcore.os.getsockoptInt(fd, IPPROTO_TCP, TCP_NODELAY));
419         case SocketOptions.SO_BINDADDR:
420             return ((InetSocketAddress) Libcore.os.getsockname(fd)).getAddress();
421         default:
422             throw new SocketException("Unknown socket option: " + option);
423         }
424     }
425 
booleanFromInt(int i)426     private static boolean booleanFromInt(int i) {
427         return (i != 0);
428     }
429 
booleanToInt(boolean b)430     private static int booleanToInt(boolean b) {
431         return b ? 1 : 0;
432     }
433 
434     /**
435      * java.net has its own socket options similar to the underlying Unix ones. We paper over the
436      * differences here.
437      *
438      * @hide
439      */
setSocketOption(FileDescriptor fd, int option, Object value)440     public static void setSocketOption(FileDescriptor fd, int option, Object value) throws SocketException {
441         try {
442             setSocketOptionErrno(fd, option, value);
443         } catch (ErrnoException errnoException) {
444             throw errnoException.rethrowAsSocketException();
445         }
446     }
447 
448     @SuppressWarnings("NewApi") // False positive lint limitation, see b/177434707.
setSocketOptionErrno(FileDescriptor fd, int option, Object value)449     private static void setSocketOptionErrno(FileDescriptor fd, int option, Object value) throws ErrnoException, SocketException {
450         switch (option) {
451         case SocketOptions.IP_MULTICAST_IF:
452             NetworkInterface nif = NetworkInterface.getByInetAddress((InetAddress) value);
453             if (nif == null) {
454                 throw new SocketException(
455                         "bad argument for IP_MULTICAST_IF : address not bound to any interface");
456             }
457             // Although IPv6 was cleaned up to use int, IPv4 uses an ip_mreqn containing an int.
458             Libcore.os.setsockoptIpMreqn(fd, IPPROTO_IP, IP_MULTICAST_IF, nif.getIndex());
459             Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, nif.getIndex());
460             return;
461         case SocketOptions.IP_MULTICAST_IF2:
462             // Although IPv6 was cleaned up to use int, IPv4 uses an ip_mreqn containing an int.
463             Libcore.os.setsockoptIpMreqn(fd, IPPROTO_IP, IP_MULTICAST_IF, (Integer) value);
464             Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, (Integer) value);
465             return;
466         case SocketOptions.IP_MULTICAST_LOOP:
467             // Although IPv6 was cleaned up to use int, IPv4 multicast loopback uses a byte.
468             // NOTE: setsockopt's arguement value means "isEnabled", while OpenJDK code java.net
469             // uses a value that means "isDisabled" so we NEGATE the system call value here.
470             int enable = booleanToInt(!((Boolean) value));
471             Libcore.os.setsockoptByte(fd, IPPROTO_IP, IP_MULTICAST_LOOP, enable);
472             Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, enable);
473             return;
474         case IoBridge.JAVA_IP_MULTICAST_TTL:
475             // Although IPv6 was cleaned up to use int, and IPv4 non-multicast TTL uses int,
476             // IPv4 multicast TTL uses a byte.
477             Libcore.os.setsockoptByte(fd, IPPROTO_IP, IP_MULTICAST_TTL, (Integer) value);
478             Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (Integer) value);
479             return;
480         case IoBridge.JAVA_IP_TTL:
481             Libcore.os.setsockoptInt(fd, IPPROTO_IP, IP_TTL, (Integer) value);
482             Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, (Integer) value);
483             return;
484         case SocketOptions.IP_TOS:
485             Libcore.os.setsockoptInt(fd, IPPROTO_IP, IP_TOS, (Integer) value);
486             Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_TCLASS, (Integer) value);
487             return;
488         case SocketOptions.SO_BROADCAST:
489             Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_BROADCAST, booleanToInt((Boolean) value));
490             return;
491         case SocketOptions.SO_KEEPALIVE:
492             Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_KEEPALIVE, booleanToInt((Boolean) value));
493             return;
494         case SocketOptions.SO_LINGER:
495             boolean on = false;
496             int seconds = 0;
497             if (value instanceof Integer) {
498                 on = true;
499                 seconds = Math.min((Integer) value, 65535);
500             }
501             StructLinger linger = new StructLinger(booleanToInt(on), seconds);
502             Libcore.os.setsockoptLinger(fd, SOL_SOCKET, SO_LINGER, linger);
503             return;
504         case SocketOptions.SO_OOBINLINE:
505             Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_OOBINLINE, booleanToInt((Boolean) value));
506             return;
507         case SocketOptions.SO_RCVBUF:
508             Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_RCVBUF, (Integer) value);
509             return;
510         case SocketOptions.SO_REUSEADDR:
511             Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_REUSEADDR, booleanToInt((Boolean) value));
512             return;
513         case SocketOptions.SO_SNDBUF:
514             Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_SNDBUF, (Integer) value);
515             return;
516         case SocketOptions.SO_TIMEOUT:
517             int millis = (Integer) value;
518             StructTimeval tv = StructTimeval.fromMillis(millis);
519             Libcore.os.setsockoptTimeval(fd, SOL_SOCKET, SO_RCVTIMEO, tv);
520             return;
521         case SocketOptions.TCP_NODELAY:
522             Libcore.os.setsockoptInt(fd, IPPROTO_TCP, TCP_NODELAY, booleanToInt((Boolean) value));
523             return;
524         case IoBridge.JAVA_MCAST_JOIN_GROUP:
525         case IoBridge.JAVA_MCAST_LEAVE_GROUP:
526         {
527             StructGroupReq groupReq = (StructGroupReq) value;
528             int level = (groupReq.gr_group instanceof Inet4Address) ? IPPROTO_IP : IPPROTO_IPV6;
529             int op = (option == JAVA_MCAST_JOIN_GROUP) ? MCAST_JOIN_GROUP : MCAST_LEAVE_GROUP;
530             Libcore.os.setsockoptGroupReq(fd, level, op, groupReq);
531             return;
532         }
533         default:
534             throw new SocketException("Unknown socket option: " + option);
535         }
536     }
537 
538     /**
539      * Wrapper for {@link Os#open(String, int, int)} that behaves similar to {@link java.io.File}.
540      * When a {@link java.io.File} is opened and there is an error, it throws
541      * {@link java.io.FileNotFoundException} regardless of what went wrong, when POSIX
542      * {@link Os#open(String, int, int)} throws more grained exceptions of what went wrong.
543      *
544      * <p>Additionally, attempt to open directory using {@link java.io.File} is also error, however
545      * POSIX {@link Os#open(String, int, int)} for read-only directories is not error.
546      *
547      * @see <a href="https://man7.org/linux/man-pages/man2/open.2.html">open(2)</a>.
548      *
549      * @param path  path of the file to be opened
550      * @param flags bitmask of the access, file creation and file status flags
551      * @return {@link FileDescriptor} of an opened file
552      * @throws FileNotFoundException if there was error opening file under {@code path}
553      *
554      * @hide
555      */
556     @SystemApi(client = MODULE_LIBRARIES)
open(@onNull String path, int flags)557     public static @NonNull FileDescriptor open(@NonNull String path, int flags) throws FileNotFoundException {
558         FileDescriptor fd = null;
559         try {
560             fd = Libcore.os.open(path, flags, 0666);
561             // Posix open(2) fails with EISDIR only if you ask for write permission.
562             // Java disallows reading directories too.f
563             if (S_ISDIR(Libcore.os.fstat(fd).st_mode)) {
564                 throw new ErrnoException("open", EISDIR);
565             }
566             return fd;
567         } catch (ErrnoException errnoException) {
568             try {
569                 if (fd != null) {
570                     closeAndSignalBlockedThreads(fd);
571                 }
572             } catch (IOException ignored) {
573             }
574             FileNotFoundException ex = new FileNotFoundException(path + ": " + errnoException.getMessage());
575             ex.initCause(errnoException);
576             throw ex;
577         }
578     }
579 
580     /**
581      * Wrapper for {@link Os#read(FileDescriptor, byte[], int, int)} that behaves similar to
582      * {@link java.io.FileInputStream#read(byte[], int, int)} and
583      * {@link java.io.FileReader#read(char[], int, int)} which interpret reading at {@code EOF} as
584      * error, when POSIX system call returns {@code 0} (and future reads return {@code -1}).
585      *
586      * <p>@see <a href="https://man7.org/linux/man-pages/man2/read.2.html">read(2)</a>.
587      *
588      * @param fd         file descriptor to read from
589      * @param bytes      buffer to put data read from {@code fd}
590      * @param byteOffset offset in {@code bytes} buffer to put read data at
591      * @param byteCount  number of bytes to read from {@code fd}
592      * @return           number of bytes read, if read operation was successful
593      * @throws IOException if underlying system call returned error
594      *
595      * @hide
596      */
597     @SystemApi(client = MODULE_LIBRARIES)
read(@onNull FileDescriptor fd, @NonNull byte[] bytes, int byteOffset, int byteCount)598     public static int read(@NonNull FileDescriptor fd, @NonNull byte[] bytes, int byteOffset, int byteCount) throws IOException {
599         ArrayUtils.throwsIfOutOfBounds(bytes.length, byteOffset, byteCount);
600         if (byteCount == 0) {
601             return 0;
602         }
603         try {
604             int readCount = Libcore.os.read(fd, bytes, byteOffset, byteCount);
605             if (readCount == 0) {
606                 return -1;
607             }
608             return readCount;
609         } catch (ErrnoException errnoException) {
610             if (errnoException.errno == EAGAIN) {
611                 // We return 0 rather than throw if we try to read from an empty non-blocking pipe.
612                 return 0;
613             }
614             throw errnoException.rethrowAsIOException();
615         }
616     }
617 
618     /**
619      * Wrapper for {@link Os#write(FileDescriptor, byte[], int, int)} that behaves similar to
620      * {@link java.io.FileOutputStream#write(byte[], int, int)} and
621      * {@link java.io.FileWriter#write(char[], int, int)} which always either write all requested
622      * bytes, or fail with error; as opposed to POSIX write, when the number of bytes written may
623      * be less than {@code bytes}. This may happen, for example, if there is insufficient space on
624      * the underlying  physical medium, or the {@code RLIMIT_FSIZE} resource limit is encountered,
625      * or the call was interrupted by a signal handler after having written less than {@code bytes}
626      * bytes.
627      *
628      * <p>@see <a href="https://man7.org/linux/man-pages/man2/write.2.html">write(2)</a>.
629      *
630      * @param fd         file descriptor to write to
631      * @param bytes      buffer containing the data to be written
632      * @param byteOffset offset in {@code bytes} buffer to read written data from
633      * @param byteCount  number of bytes to write to {@code fd}
634      * @throws IOException if underlying system call returned error
635      *
636      * @hide
637      */
638     @SystemApi(client = MODULE_LIBRARIES)
write(@onNull FileDescriptor fd,@NonNull byte[] bytes, int byteOffset, int byteCount)639     public static void write(@NonNull FileDescriptor fd,@NonNull  byte[] bytes, int byteOffset, int byteCount) throws IOException {
640         ArrayUtils.throwsIfOutOfBounds(bytes.length, byteOffset, byteCount);
641         if (byteCount == 0) {
642             return;
643         }
644         try {
645             while (byteCount > 0) {
646                 int bytesWritten = Libcore.os.write(fd, bytes, byteOffset, byteCount);
647                 byteCount -= bytesWritten;
648                 byteOffset += bytesWritten;
649             }
650         } catch (ErrnoException errnoException) {
651             throw errnoException.rethrowAsIOException();
652         }
653     }
654 
655     /**
656      * Wrapper around {@link Os#sendto(FileDescriptor, byte[], int, int, int, InetAddress, int)}
657      * that allows sending data over both TCP and UDP socket; handles
658      * {@link android.system.OsConstants#EAGAIN} and {@link android.system.OsConstants#ECONNREFUSED}
659      * and behaves similar to and behaves similar to
660      * {@link java.net.DatagramSocket#send(DatagramPacket)} and
661      * {@link Socket#getOutputStream()#write(FileDescriptor, byte[], int, int)}.
662      *
663      * <p>See {@link android.system.OsConstants} for available flags.
664      *
665      * <p>@see <a href="https://man7.org/linux/man-pages/man2/send.2.html">send(2)</a>.
666      *
667      * @param fd          {@link FileDescriptor} of the socket to send data over
668      * @param bytes       byte buffer containing the data to be sent
669      * @param byteOffset  offset in {@code bytes} at which data to be sent starts
670      * @param byteCount   number of bytes to be sent
671      * @param flags       bitwise OR of zero or more of flags, like {@link android.system.OsConstants#MSG_DONTROUTE}
672      * @param inetAddress destination address
673      * @param port        destination port
674      * @return            number of bytes sent on success
675      * @throws IOException if underlying system call returned error
676      *
677      * @hide
678      */
sendto(@onNull FileDescriptor fd, @NonNull byte[] bytes, int byteOffset, int byteCount, int flags, @Nullable InetAddress inetAddress, int port)679     public static int sendto(@NonNull FileDescriptor fd, @NonNull byte[] bytes, int byteOffset, int byteCount, int flags, @Nullable InetAddress inetAddress, int port) throws IOException {
680         boolean isDatagram = (inetAddress != null);
681         if (!isDatagram && byteCount <= 0) {
682             return 0;
683         }
684         int result;
685         try {
686             result = Libcore.os.sendto(fd, bytes, byteOffset, byteCount, flags, inetAddress, port);
687         } catch (ErrnoException errnoException) {
688             result = maybeThrowAfterSendto(isDatagram, errnoException);
689         }
690         return result;
691     }
692 
693     /** @hide */
sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port)694     public static int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws IOException {
695         boolean isDatagram = (inetAddress != null);
696         if (!isDatagram && buffer.remaining() == 0) {
697             return 0;
698         }
699         int result;
700         try {
701             result = Libcore.os.sendto(fd, buffer, flags, inetAddress, port);
702         } catch (ErrnoException errnoException) {
703             result = maybeThrowAfterSendto(isDatagram, errnoException);
704         }
705         return result;
706     }
707 
maybeThrowAfterSendto(boolean isDatagram, ErrnoException errnoException)708     private static int maybeThrowAfterSendto(boolean isDatagram, ErrnoException errnoException)
709             throws IOException {
710         if (isDatagram) {
711             if (errnoException.errno == ECONNREFUSED) {
712                 throw new PortUnreachableException("ICMP Port Unreachable");
713             }
714         } else {
715             if (errnoException.errno == EAGAIN) {
716                 // We were asked to write to a non-blocking socket, but were told
717                 // it would block, so report "no bytes written".
718                 return 0;
719             }
720         }
721         throw errnoException.rethrowAsIOException();
722     }
723 
724     /**
725      * Wrapper around {@link Os#recvfrom(FileDescriptor, byte[], int, int, int, InetSocketAddress)}
726      * that receives a message from both TCP and UDP socket; handles
727      * {@link android.system.OsConstants#EAGAIN} and {@link android.system.OsConstants#ECONNREFUSED}
728      * and behaves similar to {@link java.net.DatagramSocket#receive(DatagramPacket)} and
729      * {@link Socket#getInputStream()#recvfrom(boolean, FileDescriptor, byte[], int, int, int, DatagramPacket, boolean)}.
730      *
731      * <p>If {@code packet} is not {@code null}, and the underlying protocol provides the source
732      * address of the message, that source address is placed in the {@code packet}.
733      *
734      * @see <a href="https://man7.org/linux/man-pages/man2/recv.2.html">recv(2)</a>.
735      *
736      * @param isRead      {@code true} if some data been read already from {@code fd}
737      * @param fd          socket to receive data from
738      * @param bytes       buffer to put data read from {@code fd}
739      * @param byteOffset  offset in {@code bytes} buffer to put read data at
740      * @param byteCount   number of bytes to read from {@code fd}
741      * @param flags       bitwise OR of zero or more of flags, like {@link android.system.OsConstants#MSG_DONTROUTE}
742      * @param packet      {@link DatagramPacket} to fill with source address
743      * @param isConnected {@code true} if socket {@code fd} is connected
744      * @return            number of bytes read, if read operation was successful
745      * @throws IOException if underlying system call returned error
746      *
747      * @hide
748      */
recvfrom(boolean isRead, @NonNull FileDescriptor fd, @NonNull byte[] bytes, int byteOffset, int byteCount, int flags, @Nullable DatagramPacket packet, boolean isConnected)749     public static int recvfrom(boolean isRead, @NonNull FileDescriptor fd, @NonNull byte[] bytes, int byteOffset, int byteCount, int flags, @Nullable DatagramPacket packet, boolean isConnected) throws IOException {
750         int result;
751         try {
752             InetSocketAddress srcAddress = packet != null ? new InetSocketAddress() : null;
753             result = Libcore.os.recvfrom(fd, bytes, byteOffset, byteCount, flags, srcAddress);
754             result = postRecvfrom(isRead, packet, srcAddress, result);
755         } catch (ErrnoException errnoException) {
756             result = maybeThrowAfterRecvfrom(isRead, isConnected, errnoException);
757         }
758         return result;
759     }
760 
761     /** @hide */
recvfrom(boolean isRead, FileDescriptor fd, ByteBuffer buffer, int flags, DatagramPacket packet, boolean isConnected)762     public static int recvfrom(boolean isRead, FileDescriptor fd, ByteBuffer buffer, int flags, DatagramPacket packet, boolean isConnected) throws IOException {
763         int result;
764         try {
765             InetSocketAddress srcAddress = packet != null ? new InetSocketAddress() : null;
766             result = Libcore.os.recvfrom(fd, buffer, flags, srcAddress);
767             result = postRecvfrom(isRead, packet, srcAddress, result);
768         } catch (ErrnoException errnoException) {
769             result = maybeThrowAfterRecvfrom(isRead, isConnected, errnoException);
770         }
771         return result;
772     }
773 
postRecvfrom(boolean isRead, DatagramPacket packet, InetSocketAddress srcAddress, int byteCount)774     private static int postRecvfrom(boolean isRead, DatagramPacket packet, InetSocketAddress srcAddress, int byteCount) {
775         if (isRead && byteCount == 0) {
776             return -1;
777         }
778         if (packet != null) {
779             packet.setReceivedLength(byteCount);
780             packet.setPort(srcAddress.getPort());
781 
782             // packet.address should only be changed when it is different from srcAddress.
783             if (!srcAddress.getAddress().equals(packet.getAddress())) {
784                 packet.setAddress(srcAddress.getAddress());
785             }
786         }
787         return byteCount;
788     }
789 
maybeThrowAfterRecvfrom(boolean isRead, boolean isConnected, ErrnoException errnoException)790     private static int maybeThrowAfterRecvfrom(boolean isRead, boolean isConnected, ErrnoException errnoException) throws SocketException, SocketTimeoutException {
791         if (isRead) {
792             if (errnoException.errno == EAGAIN) {
793                 return 0;
794             } else {
795                 throw errnoException.rethrowAsSocketException();
796             }
797         } else {
798             if (isConnected && errnoException.errno == ECONNREFUSED) {
799                 throw new PortUnreachableException("ICMP Port Unreachable", errnoException);
800             } else if (errnoException.errno == EAGAIN) {
801                 SocketTimeoutException e = new SocketTimeoutException();
802                 e.initCause(errnoException);
803                 throw e;
804             } else {
805                 throw errnoException.rethrowAsSocketException();
806             }
807         }
808     }
809 
810     /**
811      * Creates an endpoint for communication and returns a file descriptor that refers
812      * to that endpoint.
813      *
814      * <p>The {@code domain} specifies a communication domain; this selects the protocol
815      * family which will be used for communication, e.g. {@link android.system.OsConstants#AF_UNIX}
816      * {@link android.system.OsConstants#AF_INET}.
817      *
818      * <p>The socket has the indicated type, which specifies the communication semantics,
819      * e.g. {@link android.system.OsConstants#SOCK_STREAM} or
820      * {@link android.system.OsConstants#SOCK_DGRAM}.
821      *
822      * <p>The protocol specifies a particular protocol to be used with the
823      * socket. Normally only a single protocol exists to support a
824      * particular socket type within a given protocol family, in which
825      * case protocol can be specified as {@code 0}.
826      *
827      * @see <a href="https://man7.org/linux/man-pages/man2/socket.2.html">socket(2)</a>.
828      *
829      * @param domain   socket domain
830      * @param type     socket type
831      * @param protocol socket protocol
832      * @return {@link FileDescriptor} of an opened socket
833      * @throws SocketException if underlying system call returned error
834      *
835      * @hide
836      */
socket(int domain, int type, int protocol)837     public static @NonNull FileDescriptor socket(int domain, int type, int protocol) throws SocketException {
838         FileDescriptor fd;
839         try {
840             fd = Libcore.os.socket(domain, type, protocol);
841 
842             return fd;
843         } catch (ErrnoException errnoException) {
844             throw errnoException.rethrowAsSocketException();
845         }
846     }
847 
848     /**
849      * Wait for some event on a file descriptor, blocks until the event happened or timeout period
850      * passed. See poll(2) and @link{android.system.Os.Poll}.
851      *
852      * @throws SocketException if poll(2) fails.
853      * @throws SocketTimeoutException if the event has not happened before timeout period has passed.
854      *
855      * @hide
856      */
poll(FileDescriptor fd, int events, int timeout)857     public static void poll(FileDescriptor fd, int events, int timeout)
858             throws SocketException, SocketTimeoutException {
859         StructPollfd[] pollFds = new StructPollfd[]{ new StructPollfd() };
860         pollFds[0].fd = fd;
861         pollFds[0].events = (short) events;
862 
863         try {
864             int ret = android.system.Os.poll(pollFds, timeout);
865             if (ret == 0) {
866                 throw new SocketTimeoutException("Poll timed out");
867             }
868         } catch (ErrnoException e) {
869             e.rethrowAsSocketException();
870         }
871     }
872 
873     /**
874      * Returns the current address to which the socket {@code fd} is bound.
875      *
876      * @see <a href="https://man7.org/linux/man-pages/man2/getsockname.2.html">getsockname(2)</a>.
877      *
878      * @param fd socket to get the bounded address of
879      * @return current address to which the socket {@code fd} is bound
880      * @throws SocketException if {@code fd} is not currently bound to an {@link InetSocketAddress}
881      *
882      * @hide
883      */
getLocalInetSocketAddress(@onNull FileDescriptor fd)884     public static @NonNull InetSocketAddress getLocalInetSocketAddress(@NonNull FileDescriptor fd)
885             throws SocketException {
886         try {
887             SocketAddress socketAddress = Libcore.os.getsockname(fd);
888             // When a Socket is pending closure because socket.close() was called but other threads
889             // are still using it, the FileDescriptor can be dup2'ed to an AF_UNIX one; see the
890             // deferred close logic in PlainSocketImpl.socketClose0(true) for details.
891             // If socketAddress is not the expected type then we assume that the socket is being
892             // closed, so we throw a SocketException (just like in the case of an ErrnoException).
893             // http://b/64209834
894             if ((socketAddress != null) && !(socketAddress instanceof InetSocketAddress)) {
895                 throw new SocketException("Socket assumed to be pending closure: Expected sockname "
896                         + "to be an InetSocketAddress, got " + socketAddress.getClass());
897             }
898             return (InetSocketAddress) socketAddress;
899         } catch (ErrnoException errnoException) {
900             throw errnoException.rethrowAsSocketException();
901         }
902     }
903 }
904