IoBridge.java revision f0d40d662d9dfdb04215c718961765837d2cf00c
10b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes/* 20b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Copyright (C) 2011 The Android Open Source Project 30b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * 40b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 50b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * you may not use this file except in compliance with the License. 60b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * You may obtain a copy of the License at 70b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * 80b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 90b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * 100b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Unless required by applicable law or agreed to in writing, software 110b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 120b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * See the License for the specific language governing permissions and 140b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * limitations under the License. 150b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 160b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 170b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughespackage libcore.io; 180b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 190b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.io.FileDescriptor; 200b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.io.FileNotFoundException; 210b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.io.IOException; 220b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.BindException; 230b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.ConnectException; 240b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.DatagramPacket; 250b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.Inet4Address; 260b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.Inet6Address; 270b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.InetAddress; 280b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.InetSocketAddress; 290b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.NetworkInterface; 300b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.PortUnreachableException; 310b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.SocketAddress; 320b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.SocketException; 330b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.SocketOptions; 340b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.SocketTimeoutException; 350b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.net.UnknownHostException; 360b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.nio.ByteBuffer; 370b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport java.util.Arrays; 380b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughesimport static libcore.io.OsConstants.*; 392ca11d6eae460f94bc42bc2e453fac93f4b94eb0Jesse Wilsonimport libcore.util.MutableInt; 400b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 410b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes/** 420b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Implements java.io/java.net/java.nio semantics in terms of the underlying POSIX system calls. 430b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 440b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughespublic final class IoBridge { 450b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 460b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes private IoBridge() { 470b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 480b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 490b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static int available(FileDescriptor fd) throws IOException { 500b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 510b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes MutableInt available = new MutableInt(0); 522ca11d6eae460f94bc42bc2e453fac93f4b94eb0Jesse Wilson Libcore.os.ioctlInt(fd, FIONREAD, available); 530b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (available.value < 0) { 540b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // If the fd refers to a regular file, the result is the difference between 550b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // the file size and the file position. This may be negative if the position 560b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // is past the end of the file. If the fd refers to a special file masquerading 570b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // as a regular file, the result may be negative because the special file 580b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // may appear to have zero size and yet a previous read call may have 590b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // read some amount of data and caused the file position to be advanced. 600b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes available.value = 0; 610b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 620b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return available.value; 630b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 640b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (errnoException.errno == ENOTTY) { 650b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // The fd is unwilling to opine about its read buffer. 660b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return 0; 670b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 680b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsIOException(); 690b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 700b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 710b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 720b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 730b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static void bind(FileDescriptor fd, InetAddress address, int port) throws SocketException { 74df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller if (address instanceof Inet6Address) { 75df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller Inet6Address inet6Address = (Inet6Address) address; 76df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller if (inet6Address.getScopeId() == 0 && inet6Address.isLinkLocalAddress()) { 77df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller // Linux won't let you bind a link-local address without a scope id. 78df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller // Find one. 79df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller NetworkInterface nif = NetworkInterface.getByInetAddress(address); 80df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller if (nif == null) { 81df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller throw new SocketException("Can't bind to a link-local address without a scope id: " + address); 82df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 83df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller try { 84df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller address = Inet6Address.getByAddress(address.getHostName(), address.getAddress(), nif.getIndex()); 85df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } catch (UnknownHostException ex) { 86df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller throw new AssertionError(ex); // Can't happen. 87df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 880b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 890b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 900b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 910b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.bind(fd, address, port); 920b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 930b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new BindException(errnoException.getMessage(), errnoException); 940b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 950b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 960b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 970b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 980b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes /** 990b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Connects socket 'fd' to 'inetAddress' on 'port', with no timeout. The lack of a timeout 1000b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * means this method won't throw SocketTimeoutException. 1010b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 102933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller public static void connect(FileDescriptor fd, InetAddress inetAddress, int port) throws SocketException { 1030b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 104933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller IoBridge.connect(fd, inetAddress, port, 0); 1050b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (SocketTimeoutException ex) { 1060b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new AssertionError(ex); // Can't happen for a connect without a timeout. 1070b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1080b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1090b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 1100b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes /** 1110b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Connects socket 'fd' to 'inetAddress' on 'port', with a the given 'timeoutMs'. 1120b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Use timeoutMs == 0 for a blocking connect with no timeout. 1130b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 114933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller public static void connect(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs) throws SocketException, SocketTimeoutException { 1150b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 116933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller connectErrno(fd, inetAddress, port, timeoutMs); 1170b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 1180b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new ConnectException(connectDetail(inetAddress, port, timeoutMs, errnoException), errnoException); 1190b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (SocketException ex) { 1200b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw ex; // We don't want to doubly wrap these. 1210b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (SocketTimeoutException ex) { 1220b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw ex; // We don't want to doubly wrap these. 1230b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (IOException ex) { 1240b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new SocketException(ex); 1250b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1260b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1270b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 128933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller private static void connectErrno(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs) throws ErrnoException, IOException { 1290b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // With no timeout, just call connect(2) directly. 1300b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (timeoutMs == 0) { 1310b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.connect(fd, inetAddress, port); 132933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller return; 1330b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1340b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 135796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // For connect with a timeout, we: 136796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // 1. set the socket to non-blocking, 137796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // 2. connect(2), 138796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // 3. loop using poll(2) to decide whether we're connected, whether we should keep 139796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // waiting, or whether we've seen a permanent failure and should give up, 140796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // 4. set the socket back to blocking. 141796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes 142796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // 1. set the socket to non-blocking. 1430b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes IoUtils.setBlocking(fd, false); 144796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes 145796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // 2. call connect(2) non-blocking. 146796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes long finishTimeMs = System.currentTimeMillis() + timeoutMs; 1470b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 148796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes Libcore.os.connect(fd, inetAddress, port); 149796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes IoUtils.setBlocking(fd, true); // 4. set the socket back to blocking. 150933fbbf606268eec9fc430632b8bca7002a833b3Neil Fuller return; // We connected immediately. 151796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes } catch (ErrnoException errnoException) { 152796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes if (errnoException.errno != EINPROGRESS) { 153796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes throw errnoException; 1540b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 155796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // EINPROGRESS means we should keep trying... 1560b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 157796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes 158796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes // 3. loop using poll(2). 159796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes int remainingTimeoutMs; 160796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes do { 161796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes remainingTimeoutMs = (int) (finishTimeMs - System.currentTimeMillis()); 162796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes if (remainingTimeoutMs <= 0) { 163796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes throw new SocketTimeoutException(connectDetail(inetAddress, port, timeoutMs, null)); 164796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes } 165796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes } while (!IoBridge.isConnected(fd, inetAddress, port, timeoutMs, remainingTimeoutMs)); 166796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes IoUtils.setBlocking(fd, true); // 4. set the socket back to blocking. 1670b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1680b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 1690b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes private static String connectDetail(InetAddress inetAddress, int port, int timeoutMs, ErrnoException cause) { 1700b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes String detail = "failed to connect to " + inetAddress + " (port " + port + ")"; 1710b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (timeoutMs > 0) { 1720b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes detail += " after " + timeoutMs + "ms"; 1730b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1740b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (cause != null) { 1750b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes detail += ": " + cause.getMessage(); 1760b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1770b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return detail; 1780b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1790b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 180f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller /** 181f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * Closes the supplied file descriptor and sends a signal to any threads are currently blocking. 182f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * In order for the signal to be sent the blocked threads must have registered with 183f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * the AsynchronousCloseMonitor before they entered the blocking operation. 184f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * 185f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller * <p>This method is a no-op if passed a {@code null} or already-closed file descriptor. 186f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller */ 187f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller public static void closeAndSignalBlockedThreads(FileDescriptor fd) throws IOException { 188f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller if (fd == null || !fd.valid()) { 1890b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 1900b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 1910b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int intFd = fd.getInt$(); 1920b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes fd.setInt$(-1); 1930b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes FileDescriptor oldFd = new FileDescriptor(); 1940b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes oldFd.setInt$(intFd); 1950b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes AsynchronousCloseMonitor.signalBlockedThreads(oldFd); 1960b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 1970b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.close(oldFd); 1980b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 1990b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // TODO: are there any cases in which we should throw? 2000b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2010b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2020b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 2030b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static boolean isConnected(FileDescriptor fd, InetAddress inetAddress, int port, int timeoutMs, int remainingTimeoutMs) throws IOException { 2042ca11d6eae460f94bc42bc2e453fac93f4b94eb0Jesse Wilson ErrnoException cause; 2050b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 2060b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes StructPollfd[] pollFds = new StructPollfd[] { new StructPollfd() }; 2070b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes pollFds[0].fd = fd; 2080b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes pollFds[0].events = (short) POLLOUT; 2090b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int rc = Libcore.os.poll(pollFds, remainingTimeoutMs); 2100b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (rc == 0) { 2110b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return false; // Timeout. 2120b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2130b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int connectError = Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_ERROR); 2140b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (connectError == 0) { 2150b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return true; // Success! 2160b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2170b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new ErrnoException("isConnected", connectError); // The connect(2) failed. 2180b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 219796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes if (!fd.valid()) { 220796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes throw new SocketException("Socket closed"); 221796f0d5a4e7b83c3efc5e587b6766977dc20b0c3Elliott Hughes } 2220b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (errnoException.errno == EINTR) { 2230b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return false; // Punt and ask the caller to try again. 2240b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } else { 2250b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes cause = errnoException; 2260b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2270b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2280b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes String detail = connectDetail(inetAddress, port, timeoutMs, cause); 22951f50987458f4b9c3bf3d53f829331a335f67113Elliott Hughes if (cause.errno == ETIMEDOUT) { 2300b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new SocketTimeoutException(detail, cause); 2310b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 23251f50987458f4b9c3bf3d53f829331a335f67113Elliott Hughes throw new ConnectException(detail, cause); 2330b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2340b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 2350b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // Socket options used by java.net but not exposed in SocketOptions. 2360b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static final int JAVA_MCAST_JOIN_GROUP = 19; 2370b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static final int JAVA_MCAST_LEAVE_GROUP = 20; 238df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller public static final int JAVA_MCAST_JOIN_SOURCE_GROUP = 21; 239df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller public static final int JAVA_MCAST_LEAVE_SOURCE_GROUP = 22; 240df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller public static final int JAVA_MCAST_BLOCK_SOURCE = 23; 241df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller public static final int JAVA_MCAST_UNBLOCK_SOURCE = 24; 2420b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static final int JAVA_IP_MULTICAST_TTL = 17; 2430b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 2440b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes /** 2450b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * java.net has its own socket options similar to the underlying Unix ones. We paper over the 2460b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * differences here. 2470b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 2480b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static Object getSocketOption(FileDescriptor fd, int option) throws SocketException { 2490b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 2500b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return getSocketOptionErrno(fd, option); 2510b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 2520b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsSocketException(); 2530b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2540b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2550b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 2569b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes private static Object getSocketOptionErrno(FileDescriptor fd, int option) throws ErrnoException, SocketException { 2570b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes switch (option) { 2580b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.IP_MULTICAST_IF: 2590b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // This is IPv4-only. 2600b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return Libcore.os.getsockoptInAddr(fd, IPPROTO_IP, IP_MULTICAST_IF); 2610b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.IP_MULTICAST_IF2: 2620b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // This is IPv6-only. 2630b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF); 2640b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.IP_MULTICAST_LOOP: 2650b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // Since setting this from java.net always sets IPv4 and IPv6 to the same value, 2660b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // it doesn't matter which we return. 2670b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return booleanFromInt(Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP)); 2680b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case IoBridge.JAVA_IP_MULTICAST_TTL: 2690b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // Since setting this from java.net always sets IPv4 and IPv6 to the same value, 2700b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // it doesn't matter which we return. 2710b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS); 2720b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.IP_TOS: 2730b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // Since setting this from java.net always sets IPv4 and IPv6 to the same value, 2740b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // it doesn't matter which we return. 2750b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return Libcore.os.getsockoptInt(fd, IPPROTO_IPV6, IPV6_TCLASS); 2760b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_BROADCAST: 2770b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_BROADCAST)); 2780b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_KEEPALIVE: 2790b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_KEEPALIVE)); 2800b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_LINGER: 2810b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes StructLinger linger = Libcore.os.getsockoptLinger(fd, SOL_SOCKET, SO_LINGER); 2820b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (!linger.isOn()) { 2830b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return false; 2840b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 2850b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return linger.l_linger; 2860b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_OOBINLINE: 2870b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_OOBINLINE)); 2880b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_RCVBUF: 28951cf1b49bca54ec0229a51df400ad1bee580b1bbBrian Carlstrom return Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_RCVBUF); 2900b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_REUSEADDR: 2910b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return booleanFromInt(Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_REUSEADDR)); 2920b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_SNDBUF: 2930b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return Libcore.os.getsockoptInt(fd, SOL_SOCKET, SO_SNDBUF); 2940b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_TIMEOUT: 2950b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return (int) Libcore.os.getsockoptTimeval(fd, SOL_SOCKET, SO_RCVTIMEO).toMillis(); 2960b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.TCP_NODELAY: 2970b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return booleanFromInt(Libcore.os.getsockoptInt(fd, IPPROTO_TCP, TCP_NODELAY)); 2980b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes default: 2990b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new SocketException("Unknown socket option: " + option); 3000b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 3010b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 3020b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 3030b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes private static boolean booleanFromInt(int i) { 3040b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return (i != 0); 3050b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 3060b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 3070b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes private static int booleanToInt(boolean b) { 3080b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return b ? 1 : 0; 3090b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 3100b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 3110b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes /** 3120b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * java.net has its own socket options similar to the underlying Unix ones. We paper over the 3130b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * differences here. 3140b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 3150b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static void setSocketOption(FileDescriptor fd, int option, Object value) throws SocketException { 3160b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 3170b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes setSocketOptionErrno(fd, option, value); 3180b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 3190b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsSocketException(); 3200b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 3210b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 3220b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 3239b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes private static void setSocketOptionErrno(FileDescriptor fd, int option, Object value) throws ErrnoException, SocketException { 3240b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes switch (option) { 3250b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.IP_MULTICAST_IF: 3260b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new UnsupportedOperationException("Use IP_MULTICAST_IF2 on Android"); 3270b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.IP_MULTICAST_IF2: 3280b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // Although IPv6 was cleaned up to use int, IPv4 uses an ip_mreqn containing an int. 3290b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptIpMreqn(fd, IPPROTO_IP, IP_MULTICAST_IF, (Integer) value); 3300b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, (Integer) value); 3310b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3320b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.IP_MULTICAST_LOOP: 3330b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // Although IPv6 was cleaned up to use int, IPv4 multicast loopback uses a byte. 3340b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptByte(fd, IPPROTO_IP, IP_MULTICAST_LOOP, booleanToInt((Boolean) value)); 3350b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, booleanToInt((Boolean) value)); 3360b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3370b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case IoBridge.JAVA_IP_MULTICAST_TTL: 3380b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // Although IPv6 was cleaned up to use int, and IPv4 non-multicast TTL uses int, 3390b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // IPv4 multicast TTL uses a byte. 3400b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptByte(fd, IPPROTO_IP, IP_MULTICAST_TTL, (Integer) value); 3410b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (Integer) value); 3420b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3430b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.IP_TOS: 3440b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, IPPROTO_IP, IP_TOS, (Integer) value); 3450b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_TCLASS, (Integer) value); 3460b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3470b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_BROADCAST: 3480b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_BROADCAST, booleanToInt((Boolean) value)); 3490b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3500b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_KEEPALIVE: 3510b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_KEEPALIVE, booleanToInt((Boolean) value)); 3520b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3530b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_LINGER: 3540b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes boolean on = false; 3550b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int seconds = 0; 3560b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (value instanceof Integer) { 3570b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes on = true; 3580b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes seconds = Math.min((Integer) value, 65535); 3590b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 3600b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes StructLinger linger = new StructLinger(booleanToInt(on), seconds); 3610b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptLinger(fd, SOL_SOCKET, SO_LINGER, linger); 3620b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3630b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_OOBINLINE: 3640b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_OOBINLINE, booleanToInt((Boolean) value)); 3650b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3660b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_RCVBUF: 3670b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_RCVBUF, (Integer) value); 3680b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3690b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_REUSEADDR: 3700b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_REUSEADDR, booleanToInt((Boolean) value)); 3710b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3720b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_SNDBUF: 3730b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, SOL_SOCKET, SO_SNDBUF, (Integer) value); 3740b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3750b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.SO_TIMEOUT: 3760b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int millis = (Integer) value; 3770b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes StructTimeval tv = StructTimeval.fromMillis(millis); 3780b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptTimeval(fd, SOL_SOCKET, SO_RCVTIMEO, tv); 3790b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3800b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case SocketOptions.TCP_NODELAY: 3810b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, IPPROTO_TCP, TCP_NODELAY, booleanToInt((Boolean) value)); 3820b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 3830b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case IoBridge.JAVA_MCAST_JOIN_GROUP: 3840b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes case IoBridge.JAVA_MCAST_LEAVE_GROUP: 385df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller { 3860b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes StructGroupReq groupReq = (StructGroupReq) value; 3870b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int level = (groupReq.gr_group instanceof Inet4Address) ? IPPROTO_IP : IPPROTO_IPV6; 3880b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int op = (option == JAVA_MCAST_JOIN_GROUP) ? MCAST_JOIN_GROUP : MCAST_LEAVE_GROUP; 3890b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptGroupReq(fd, level, op, groupReq); 3900b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 391df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 392df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller case IoBridge.JAVA_MCAST_JOIN_SOURCE_GROUP: 393df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller case IoBridge.JAVA_MCAST_LEAVE_SOURCE_GROUP: 394df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller case IoBridge.JAVA_MCAST_BLOCK_SOURCE: 395df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller case IoBridge.JAVA_MCAST_UNBLOCK_SOURCE: 396df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller { 397df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller StructGroupSourceReq groupSourceReq = (StructGroupSourceReq) value; 398df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller int level = (groupSourceReq.gsr_group instanceof Inet4Address) 399df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller ? IPPROTO_IP : IPPROTO_IPV6; 400df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller int op = getGroupSourceReqOp(option); 401df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller Libcore.os.setsockoptGroupSourceReq(fd, level, op, groupSourceReq); 402df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller return; 403df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 4040b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes default: 4050b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new SocketException("Unknown socket option: " + option); 4060b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4070b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4080b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 409df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller private static int getGroupSourceReqOp(int javaValue) { 410df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller switch (javaValue) { 411df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller case IoBridge.JAVA_MCAST_JOIN_SOURCE_GROUP: 412df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller return MCAST_JOIN_SOURCE_GROUP; 413df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller case IoBridge.JAVA_MCAST_LEAVE_SOURCE_GROUP: 414df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller return MCAST_LEAVE_SOURCE_GROUP; 415df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller case IoBridge.JAVA_MCAST_BLOCK_SOURCE: 416df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller return MCAST_BLOCK_SOURCE; 417df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller case IoBridge.JAVA_MCAST_UNBLOCK_SOURCE: 418df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller return MCAST_UNBLOCK_SOURCE; 419df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller default: 420df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller throw new AssertionError( 421df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller "Unknown java value for setsocketopt op lookup: " + javaValue); 422df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 423df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller } 424df29508a7aa622f265aaebdc472eb7d679185ebbNeil Fuller 4250b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes /** 4260b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * java.io only throws FileNotFoundException when opening files, regardless of what actually 4270b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * went wrong. Additionally, java.io is more restrictive than POSIX when it comes to opening 4280b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * directories: POSIX says read-only is okay, but java.io doesn't even allow that. We also 4290b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * have an Android-specific hack to alter the default permissions. 4300b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 4310b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static FileDescriptor open(String path, int flags) throws FileNotFoundException { 4320b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes FileDescriptor fd = null; 4330b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 4340b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // On Android, we don't want default permissions to allow global access. 4350b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int mode = ((flags & O_ACCMODE) == O_RDONLY) ? 0 : 0600; 4360b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes fd = Libcore.os.open(path, flags, mode); 43715443035ff7b8ac7d6a7e6e2caffcd88f2faef67Narayan Kamath // Posix open(2) fails with EISDIR only if you ask for write permission. 43815443035ff7b8ac7d6a7e6e2caffcd88f2faef67Narayan Kamath // Java disallows reading directories too. 43915443035ff7b8ac7d6a7e6e2caffcd88f2faef67Narayan Kamath if (S_ISDIR(Libcore.os.fstat(fd).st_mode)) { 44015443035ff7b8ac7d6a7e6e2caffcd88f2faef67Narayan Kamath throw new ErrnoException("open", EISDIR); 4410b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4420b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return fd; 4430b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 4440b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 4450b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (fd != null) { 4460b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes IoUtils.close(fd); 4470b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4480b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (IOException ignored) { 4490b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4500b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes FileNotFoundException ex = new FileNotFoundException(path + ": " + errnoException.getMessage()); 4510b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes ex.initCause(errnoException); 4520b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw ex; 4530b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4540b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4550b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 4560b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes /** 4570b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * java.io thinks that a read at EOF is an error and should return -1, contrary to traditional 4580b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Unix practice where you'd read until you got 0 bytes (and any future read would return -1). 4590b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 4600b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws IOException { 4610b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Arrays.checkOffsetAndCount(bytes.length, byteOffset, byteCount); 4620b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (byteCount == 0) { 4630b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return 0; 4640b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4650b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 4660b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int readCount = Libcore.os.read(fd, bytes, byteOffset, byteCount); 4670b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (readCount == 0) { 4680b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return -1; 4690b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4700b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return readCount; 4710b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 4720b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (errnoException.errno == EAGAIN) { 4730b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // We return 0 rather than throw if we try to read from an empty non-blocking pipe. 4740b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return 0; 4750b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4760b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsIOException(); 4770b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4780b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4790b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 4800b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes /** 4810b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * java.io always writes every byte it's asked to, or fails with an error. (That is, unlike 4820b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes * Unix it never just writes as many bytes as happens to be convenient.) 4830b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes */ 4840b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static void write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws IOException { 4850b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Arrays.checkOffsetAndCount(bytes.length, byteOffset, byteCount); 4860b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (byteCount == 0) { 4870b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return; 4880b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4890b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 4900b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes while (byteCount > 0) { 4910b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int bytesWritten = Libcore.os.write(fd, bytes, byteOffset, byteCount); 4920b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes byteCount -= bytesWritten; 4930b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes byteOffset += bytesWritten; 4940b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4950b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 4960b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsIOException(); 4970b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4980b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 4990b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 5000b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static int sendto(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, InetAddress inetAddress, int port) throws IOException { 5010b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes boolean isDatagram = (inetAddress != null); 5020b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (!isDatagram && byteCount <= 0) { 5030b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return 0; 5040b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5050b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int result; 5060b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 5070b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = Libcore.os.sendto(fd, bytes, byteOffset, byteCount, flags, inetAddress, port); 5080b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 5090b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = maybeThrowAfterSendto(isDatagram, errnoException); 5100b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5110b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return result; 5120b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5130b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 5140b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static int sendto(FileDescriptor fd, ByteBuffer buffer, int flags, InetAddress inetAddress, int port) throws IOException { 5150b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes boolean isDatagram = (inetAddress != null); 5160b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (!isDatagram && buffer.remaining() == 0) { 5170b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return 0; 5180b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5190b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int result; 5200b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 5210b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = Libcore.os.sendto(fd, buffer, flags, inetAddress, port); 5220b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 5230b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = maybeThrowAfterSendto(isDatagram, errnoException); 5240b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5250b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return result; 5260b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5270b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 5280b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes private static int maybeThrowAfterSendto(boolean isDatagram, ErrnoException errnoException) throws SocketException { 5290b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (isDatagram) { 5300b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (errnoException.errno == ECONNRESET || errnoException.errno == ECONNREFUSED) { 5310b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return 0; 5320b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5330b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } else { 534f4a4259d8a548ab172ca98b702feafcd0ceb1411Elliott Hughes if (errnoException.errno == EAGAIN) { 5350b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // We were asked to write to a non-blocking socket, but were told 5360b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // it would block, so report "no bytes written". 5370b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return 0; 5380b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5390b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5400b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsSocketException(); 5410b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5420b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 5430b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static int recvfrom(boolean isRead, FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount, int flags, DatagramPacket packet, boolean isConnected) throws IOException { 5440b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int result; 5450b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 5460b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes InetSocketAddress srcAddress = (packet != null && !isConnected) ? new InetSocketAddress() : null; 5470b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = Libcore.os.recvfrom(fd, bytes, byteOffset, byteCount, flags, srcAddress); 5480b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = postRecvfrom(isRead, packet, isConnected, srcAddress, result); 5490b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 5500b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = maybeThrowAfterRecvfrom(isRead, isConnected, errnoException); 5510b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5520b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return result; 5530b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5540b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 5550b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static int recvfrom(boolean isRead, FileDescriptor fd, ByteBuffer buffer, int flags, DatagramPacket packet, boolean isConnected) throws IOException { 5560b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes int result; 5570b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 5580b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes InetSocketAddress srcAddress = (packet != null && !isConnected) ? new InetSocketAddress() : null; 5590b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = Libcore.os.recvfrom(fd, buffer, flags, srcAddress); 5600b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = postRecvfrom(isRead, packet, isConnected, srcAddress, result); 5610b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 5620b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes result = maybeThrowAfterRecvfrom(isRead, isConnected, errnoException); 5630b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5640b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return result; 5650b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5660b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 5670b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes private static int postRecvfrom(boolean isRead, DatagramPacket packet, boolean isConnected, InetSocketAddress srcAddress, int byteCount) { 5680b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (isRead && byteCount == 0) { 5690b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return -1; 5700b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5710b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (packet != null) { 572e50d82455c813210a2d452070f45fd38d9903159Elliott Hughes packet.setReceivedLength(byteCount); 5730b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (!isConnected) { 5740b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes packet.setAddress(srcAddress.getAddress()); 5750b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes packet.setPort(srcAddress.getPort()); 5760b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5770b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5780b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return byteCount; 5790b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5800b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 5810b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes private static int maybeThrowAfterRecvfrom(boolean isRead, boolean isConnected, ErrnoException errnoException) throws SocketException, SocketTimeoutException { 5820b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (isRead) { 583f4a4259d8a548ab172ca98b702feafcd0ceb1411Elliott Hughes if (errnoException.errno == EAGAIN) { 5840b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return 0; 5850b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } else { 5860b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsSocketException(); 5870b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5880b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } else { 5890b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (isConnected && errnoException.errno == ECONNREFUSED) { 5900b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new PortUnreachableException("", errnoException); 591f4a4259d8a548ab172ca98b702feafcd0ceb1411Elliott Hughes } else if (errnoException.errno == EAGAIN) { 5920b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw new SocketTimeoutException(errnoException); 5930b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } else { 5940b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsSocketException(); 5950b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5960b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5970b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 5980b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 5990b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes public static FileDescriptor socket(boolean stream) throws SocketException { 6000b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes FileDescriptor fd; 6010b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes try { 6020b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes fd = Libcore.os.socket(AF_INET6, stream ? SOCK_STREAM : SOCK_DGRAM, 0); 6030b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 6040b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // The RFC (http://www.ietf.org/rfc/rfc3493.txt) says that IPV6_MULTICAST_HOPS defaults 6050b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // to 1. The Linux kernel (at least up to 2.6.38) accidentally defaults to 64 (which 6060b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // would be correct for the *unicast* hop limit). 6070b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // See http://www.spinics.net/lists/netdev/msg129022.html, though no patch appears to 6080b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // have been applied as a result of that discussion. If that bug is ever fixed, we can 6090b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // remove this code. Until then, we manually set the hop limit on IPv6 datagram sockets. 6100b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes // (IPv4 is already correct.) 6110b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes if (!stream) { 6120b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 1); 6130b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 6140b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 6150b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes return fd; 6160b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } catch (ErrnoException errnoException) { 6170b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes throw errnoException.rethrowAsSocketException(); 6180b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 6190b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 6200b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 621255a6f4ab321614ed1ca26849d1df7fa9c0610f5Elliott Hughes public static InetAddress getSocketLocalAddress(FileDescriptor fd) throws SocketException { 6229b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes try { 6239b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes SocketAddress sa = Libcore.os.getsockname(fd); 6249b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes InetSocketAddress isa = (InetSocketAddress) sa; 6259b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes return isa.getAddress(); 6269b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes } catch (ErrnoException errnoException) { 627255a6f4ab321614ed1ca26849d1df7fa9c0610f5Elliott Hughes throw errnoException.rethrowAsSocketException(); 6289b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes } 6290b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 6300b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes 631255a6f4ab321614ed1ca26849d1df7fa9c0610f5Elliott Hughes public static int getSocketLocalPort(FileDescriptor fd) throws SocketException { 6329b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes try { 6339b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes SocketAddress sa = Libcore.os.getsockname(fd); 6349b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes InetSocketAddress isa = (InetSocketAddress) sa; 6359b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes return isa.getPort(); 6369b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes } catch (ErrnoException errnoException) { 637255a6f4ab321614ed1ca26849d1df7fa9c0610f5Elliott Hughes throw errnoException.rethrowAsSocketException(); 6389b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes } 6390b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes } 6400b736ebc4efef64f2db1999aea90297ad8196146Elliott Hughes} 641