private static int maybeThrowAfterSendto(boolean isDatagram, ErrnoException errnoException) throws SocketException { if (isDatagram) { if (errnoException.errno == ECONNRESET || errnoException.errno == ECONNREFUSED) { return 0; } } else { if (errnoException.errno == EAGAIN) { // We were asked to write to a non-blocking socket, but were told // it would block, so report "no bytes written". return 0; } } throw errnoException.rethrowAsSocketException(); }
private static int maybeThrowAfterRecvfrom(boolean isRead, boolean isConnected, ErrnoException errnoException) throws SocketException, SocketTimeoutException { if (isRead) { if (errnoException.errno == EAGAIN) { return 0; } else { throw errnoException.rethrowAsSocketException(); } } else { if (isConnected && errnoException.errno == ECONNREFUSED) { throw new PortUnreachableException("", errnoException); } else if (errnoException.errno == EAGAIN) { throw new SocketTimeoutException(errnoException); } else { throw errnoException.rethrowAsSocketException(); } } }
/** * java.net has its own socket options similar to the underlying Unix ones. We paper over the * differences here. */ public static Object getSocketOption(FileDescriptor fd, int option) throws SocketException { try { return getSocketOptionErrno(fd, option); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
@Override protected void sendUrgentData(int value) throws IOException { try { byte[] buffer = new byte[] { (byte) value }; Libcore.os.sendto(fd, buffer, 0, 1, MSG_OOB, null, 0); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
/** * Shutdown the output portion of the socket. */ @Override protected void shutdownOutput() throws IOException { try { Libcore.os.shutdown(fd, SHUT_WR); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
public static InetAddress getSocketLocalAddress(FileDescriptor fd) throws SocketException { try { SocketAddress sa = Libcore.os.getsockname(fd); InetSocketAddress isa = (InetSocketAddress) sa; return isa.getAddress(); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
public static int getSocketLocalPort(FileDescriptor fd) throws SocketException { try { SocketAddress sa = Libcore.os.getsockname(fd); InetSocketAddress isa = (InetSocketAddress) sa; return isa.getPort(); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } } }
/** * java.net has its own socket options similar to the underlying Unix ones. We paper over the * differences here. */ public static void setSocketOption(FileDescriptor fd, int option, Object value) throws SocketException { try { setSocketOptionErrno(fd, option, value); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
/** * Shutdown the input portion of the socket. */ @Override protected void shutdownInput() throws IOException { shutdownInput = true; try { Libcore.os.shutdown(fd, SHUT_RD); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
/** * Sets the network interface used by this socket. Any packets sent * via this socket are transmitted via the specified interface. Any * packets received by this socket will come from the specified * interface. Broadcast datagrams received on this interface will * be processed by this socket. This corresponds to Linux's SO_BINDTODEVICE. * * @hide used by GoogleTV for DHCP */ public void setNetworkInterface(NetworkInterface netInterface) throws SocketException { if (netInterface == null) { throw new NullPointerException("netInterface == null"); } try { Libcore.os.setsockoptIfreq(impl.fd, SOL_SOCKET, SO_BINDTODEVICE, netInterface.getName()); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
@Override protected void listen(int backlog) throws IOException { if (usingSocks()) { // Do nothing for a SOCKS connection. The listen occurs on the // server during the bind. return; } try { Libcore.os.listen(fd, backlog); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
public static FileDescriptor socket(boolean stream) throws SocketException { FileDescriptor fd; try { fd = Libcore.os.socket(AF_INET6, stream ? SOCK_STREAM : SOCK_DGRAM, 0); // The RFC (http://www.ietf.org/rfc/rfc3493.txt) says that IPV6_MULTICAST_HOPS defaults // to 1. The Linux kernel (at least up to 2.6.38) accidentally defaults to 64 (which // would be correct for the *unicast* hop limit). // See http://www.spinics.net/lists/netdev/msg129022.html, though no patch appears to // have been applied as a result of that discussion. If that bug is ever fixed, we can // remove this code. Until then, we manually set the hop limit on IPv6 datagram sockets. // (IPv4 is already correct.) if (!stream) { Libcore.os.setsockoptInt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 1); } return fd; } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
throw new SocketTimeoutException(errnoException); throw errnoException.rethrowAsSocketException();
/** * Shutdown the input portion of the socket. */ @Override protected void shutdownInput() throws IOException { shutdownInput = true; try { Libcore.os.shutdown(fd, SHUT_RD); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
@Override protected void sendUrgentData(int value) throws IOException { try { byte[] buffer = new byte[] { (byte) value }; Libcore.os.sendto(fd, buffer, 0, 1, MSG_OOB, null, 0); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
/** * java.net has its own socket options similar to the underlying Unix ones. We paper over the * differences here. */ public static Object getSocketOption(FileDescriptor fd, int option) throws SocketException { try { return getSocketOptionErrno(fd, option); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
/** * java.net has its own socket options similar to the underlying Unix ones. We paper over the * differences here. */ public static void setSocketOption(FileDescriptor fd, int option, Object value) throws SocketException { try { setSocketOptionErrno(fd, option, value); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
public static int getSocketLocalPort(FileDescriptor fd) throws SocketException { try { SocketAddress sa = Libcore.os.getsockname(fd); InetSocketAddress isa = (InetSocketAddress) sa; return isa.getPort(); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } } }
/** * java.net has its own socket options similar to the underlying Unix ones. We paper over the * differences here. */ public static Object getSocketOption(FileDescriptor fd, int option) throws SocketException { try { return getSocketOptionErrno(fd, option); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }
/** * Shutdown the output portion of the socket. */ @Override protected void shutdownOutput() throws IOException { try { Libcore.os.shutdown(fd, SHUT_WR); } catch (ErrnoException errnoException) { throw errnoException.rethrowAsSocketException(); } }