public void bind(SocketAddress endpoint, int backlog) throws java.io.IOException { if (!(endpoint instanceof UnixSocketAddress)) { throw new IOException("Invalid address"); } UnixSocketAddress addr = (UnixSocketAddress) endpoint; if (Native.bind(fd, addr.getStruct(), addr.length()) < 0) { throw new IOException("bind failed: " + Native.getLastErrorString()); } if (Native.listen(fd, backlog) < 0) { throw new IOException("listen failed: " + Native.getLastErrorString()); } }
static int listen(int fd, int backlog) { return libsocket().listen(fd, backlog); }
public void bind(SocketAddress endpoint, int backlog) throws IOException { if (null != endpoint && !(endpoint instanceof UnixSocketAddress)) { throw new UnsupportedAddressTypeException(); } localAddress = Common.bind(fd, (UnixSocketAddress)endpoint); if (Native.listen(fd, backlog) < 0) { throw new IOException(Native.getLastErrorString()); } }
static Credentials getCredentials(int fd) { Ucred c = new Ucred(); int error = Native.getsockopt(fd, SocketLevel.SOL_SOCKET, SocketOption.SO_PEERCRED, c); if (error != 0) { throw new UnsupportedOperationException(Native.getLastErrorString()); } return new Credentials(c); } }
public UnixSocketChannel accept() throws IOException { UnixSocketAddress remote = new UnixSocketAddress(); SockAddrUnix addr = remote.getStruct(); IntByReference len = new IntByReference(addr.getMaximumLength()); int clientfd = Native.accept(getFD(), addr, len); if (clientfd < 0) { throw new IOException("accept failed: " + Native.getLastErrorString()); } // Always force the socket back to blocking mode Native.setBlocking(clientfd, true); return new UnixSocketChannel(clientfd, remote); }
static int socket(ProtocolFamily domain, Sock type, int protocol) throws IOException { int fd = libsocket().socket(domain.intValue(), type.intValue(), protocol); if (fd < 0) { throw new IOException(getLastErrorString()); } return fd; }
static UnixSocketAddress getsockname(int sockfd) { UnixSocketAddress remote = new UnixSocketAddress(); IntByReference len = new IntByReference(remote.getStruct().getMaximumLength()); if (Native.libc().getsockname(sockfd, remote.getStruct(), len) < 0) { throw new Error(Native.getLastErrorString()); } return remote; } }
if (0 != Native.setsockopt(fd, SocketLevel.SOL_SOCKET, optname, optvalue)) { throw new IOException(Native.getLastErrorString());
static UnixSocketAddress bind(int fd, UnixSocketAddress local) throws IOException { SockAddrUnix sa; if (null == local) { // Support autobind sa = SockAddrUnix.create(); sa.setFamily(ProtocolFamily.PF_UNIX); if (currentOS == OS.LINUX) { // On Linux, we simply set an empty path sa.setPath(""); } else { // Emulate something similar (bind to some random unique address), // but use regular namespace File f = Files.createTempFile("jnr-unixsocket-tmp", ".sock").toFile(); f.deleteOnExit(); f.delete(); sa.setPath(f.getPath()); } } else { sa = local.getStruct(); } if (Native.bind(fd, sa, sa.length()) < 0) { throw new IOException(Native.getLastErrorString()); } return getsockname(fd); }
private UnixSocketChannel() throws IOException { super(Native.socket(ProtocolFamily.PF_UNIX, Sock.SOCK_STREAM, 0), SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE); state = State.IDLE; }
public static int sendto(int fd, ByteBuffer src, SockAddrUnix addr, int len) throws IOException { if (src == null) { throw new IllegalArgumentException("Source buffer cannot be null"); } int n; do { n = libsocket().sendto(fd, src, src.remaining(), 0, addr, len); } while (n < 0 && Errno.EINTR.equals(getLastError())); if (n > 0) { src.position(src.position() + n); } return n; }
@Override public UnixSocketAddress receive(ByteBuffer src) throws IOException { UnixSocketAddress remote = new UnixSocketAddress(); int n = Native.recvfrom(getFD(), src, remote.getStruct()); if (n < 0) { throw new IOException(Native.getLastErrorString()); } return remote; }
@Override public int send(ByteBuffer src, SocketAddress target) throws IOException { UnixSocketAddress remote = null; if (null == target) { if (isConnected()) { remote = remoteAddress; } else { throw new IllegalArgumentException("Destination address cannot be null on unconnected datagram sockets"); } } else { if (!(target instanceof UnixSocketAddress)) { throw new UnsupportedAddressTypeException(); } remote = (UnixSocketAddress)target; } SockAddrUnix sa = (null == remote) ? null : remote.getStruct(); int addrlen = (null == sa) ? 0 : sa.length(); int n = Native.sendto(getFD(), src, sa, addrlen); if (n < 0) { throw new IOException(Native.getLastErrorString()); } return n; }
public static int setsockopt(int s, SocketLevel level, SocketOption optname, boolean optval) { return setsockopt(s, level, optname, optval ? 1 : 0); }
private final boolean doConnect(SockAddrUnix remote) throws IOException { if (Native.connect(getFD(), remote, remote.length()) != 0) { Errno error = Errno.valueOf(LastError.getLastError(jnr.ffi.Runtime.getSystemRuntime())); switch (error) { case EAGAIN: case EWOULDBLOCK: return false; default: throw new IOException(error.toString()); } } return true; }
public UnixSocketChannel accept() throws IOException { UnixSocketAddress remote = new UnixSocketAddress(); SockAddrUnix addr = remote.getStruct(); int maxLength = addr.getMaximumLength(); IntByReference len = new IntByReference(maxLength); int clientfd = Native.accept(getFD(), addr, len); if (clientfd < 0) { if (isBlocking()) { throw new IOException("accept failed: " + Native.getLastErrorString()); } return null; } // Handle unnamed sockets and sockets in Linux' abstract namespace addr.updatePath(len.getValue()); // Always force the socket back to blocking mode Native.setBlocking(clientfd, true); return new UnixSocketChannel(clientfd); }
static int socket(ProtocolFamily domain, Sock type, int protocol) throws IOException { int fd = libsocket().socket(domain.intValue(), type.intValue(), protocol); if (fd < 0) { throw new IOException(getLastErrorString()); } return fd; }
static UnixSocketAddress getpeername(int sockfd) { UnixSocketAddress remote = new UnixSocketAddress(); IntByReference len = new IntByReference(remote.getStruct().getMaximumLength()); if (Native.libc().getpeername(sockfd, remote.getStruct(), len) < 0) { throw new Error(Native.getLastErrorString()); } return remote; }
UnixSocketChannel() throws IOException { this(Native.socket(ProtocolFamily.PF_UNIX, Sock.SOCK_STREAM, 0)); }