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()); } }
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 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()); } }
@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; }
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 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 int socketpair(ProtocolFamily domain, Sock type, int protocol, int[] sv) throws IOException { if (libsocket().socketpair(domain.intValue(), type.intValue(), protocol, sv) < 0) { throw new IOException("socketpair(2) failed " + Native.getLastErrorString()); } return 0; }
static int socketpair(ProtocolFamily domain, Sock type, int protocol, int[] sv) throws IOException { if (libsocket().socketpair(domain.intValue(), type.intValue(), protocol, sv) < 0) { throw new IOException("socketpair(2) failed " + Native.getLastErrorString()); } return 0; }
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); }
@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; }
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; } }
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; }
static UnixSocketAddress getsockname(int sockfd) { UnixSocketAddress local = new UnixSocketAddress(); SockAddrUnix addr = local.getStruct(); IntByReference len = new IntByReference(addr.getMaximumLength()); if (Native.libc().getsockname(sockfd, addr, len) < 0) { throw new Error(Native.getLastErrorString()); } addr.updatePath(len.getValue()); return local; }
static UnixSocketAddress getpeername(int sockfd) { UnixSocketAddress remote = new UnixSocketAddress(); SockAddrUnix addr = remote.getStruct(); IntByReference len = new IntByReference(addr.getMaximumLength()); if (Native.libc().getpeername(sockfd, addr, len) < 0) { throw new Error(Native.getLastErrorString()); } addr.updatePath(len.getValue()); return remote; }
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); }
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); }