/** * Gets the actual length of this address (including len/family header) * * @return The actual size of this address, in bytes */ public int length() { return 2 + strlen(getPathField()); }
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); }
/** * Gets the file system path of this socket address * * @return A String */ public final java.lang.String getPath() { return getPathField().get(); }
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; }
/** * Updates the file system path of this socket address. * In order to support abstract namespaces, this MUST be * called after any native syscall that sets this * path struct like getsockname(), getpeername(), accept(). * * @param len the value of the addrlen var, set by the above syscalls. */ void updatePath(final int len) { if (currentOS == OS.LINUX) { // Linux always returns an accurate length in // order to support abstract namespace, where // path STARTS with a NUL byte. cachedPath = len == HEADER_LENGTH ? "" : getPath(len - HEADER_LENGTH); } else { // All others might return a len > 0 (typically 14) AND the path is terminated // by a NUL byte if it is shorter than sizeof(sun_path) cachedPath = getPathField().get(); int slen = len - HEADER_LENGTH; if (slen <= 0) { cachedPath = ""; } else { if (slen < getPathField().length() && slen < cachedPath.length()) { cachedPath = cachedPath.substring(0, slen); } } } }
int length() { return address.length(); }
/** * Sets the protocol family of this unix socket address. * * @param family The protocol family, usually {@link ProtocolFamily#PF_UNIX} */ final void setFamily(ProtocolFamily family) { getFamilyField().set(family.intValue()); }
public static int recvfrom(int fd, ByteBuffer dst, SockAddrUnix addr) throws IOException { if (dst == null) { throw new IllegalArgumentException("Destination buffer cannot be null"); } if (dst.isReadOnly()) { throw new IllegalArgumentException("Read-only buffer"); } IntByReference addrlen = (null == addr) ? null : new IntByReference(addr.getMaximumLength()); int n; do { n = libsocket().recvfrom(fd, dst, dst.remaining(), 0, addr, addrlen); } while (n < 0 && Errno.EINTR.equals(getLastError())); if (n > 0) { dst.position(dst.position() + n); } return n; } }
/** * Retrieves the path. * @return The path of this AF_UNIX address. * Note: On Linux, can contain a leading NUL byte, if this address * resides in abstract namespace. */ public String path() { return address.getPath(); }
@Override public void setPath(java.lang.String path) { super.setPath(path); sun_len.set(path.length()); } protected UTF8String getPathField() {
/** * Sets the file system path of this socket address * * @param path The unix socket address */ public final void setPath(java.lang.String path) { getPathField().set(path); }
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; }
int length() { return address.length(); }
/** * Gets the protocol family of this unix socket address. * * @return The protocol family */ final ProtocolFamily getFamily() { return ProtocolFamily.valueOf(getFamilyField().intValue()); }
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; } }