Class UnixSocketAddress

All Implemented Interfaces:
SocketConnectable, Proxy

@Generated("org.javagi.JavaGI") public class UnixSocketAddress extends SocketAddress implements SocketConnectable

Support for UNIX-domain (also known as local) sockets, corresponding to struct sockaddr_un.

UNIX domain sockets are generally visible in the filesystem. However, some systems support abstract socket names which are not visible in the filesystem and not affected by the filesystem permissions, visibility, etc. Currently this is only supported under Linux. If you attempt to use abstract sockets on other systems, function calls may return G_IO_ERROR_NOT_SUPPORTED errors. You can use abstractNamesSupported() to see if abstract names are supported.

Since GLib 2.72, GUnixSocketAddress is available on all platforms. It requires underlying system support (such as Windows 10 with AF_UNIX) at run time.

Before GLib 2.72, <gio/gunixsocketaddress.h> belonged to the UNIX-specific GIO interfaces, thus you had to use the gio-unix-2.0.pc pkg-config file when using it. This is no longer necessary since GLib 2.72.

  • Constructor Details

    • UnixSocketAddress

      public UnixSocketAddress(MemorySegment address)
      Create a UnixSocketAddress instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • UnixSocketAddress

      public UnixSocketAddress(String path)

      Creates a new GUnixSocketAddress for path.

      To create abstract socket addresses, on systems that support that, use g_unix_socket_address_new_abstract().

      Parameters:
      path - the socket path
      Since:
      2.22
    • UnixSocketAddress

      public UnixSocketAddress()
      Create a new UnixSocketAddress.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the UnixSocketAddress class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected UnixSocketAddress asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class SocketAddress
      Returns:
      the instance as if it were its parent type
    • abstract_

      @Deprecated public static SocketAddress abstract_(@Nullable byte @Nullable [] path)
      Deprecated.
      Use g_unix_socket_address_new_with_type().
      Creates a new UnixSocketAddressType.ABSTRACT_PADDED GUnixSocketAddress for path.
      Parameters:
      path - the abstract name
      Returns:
      a new GUnixSocketAddress
    • withType

      public static SocketAddress withType(@Nullable byte @Nullable [] path, UnixSocketAddressType type)

      Creates a new GUnixSocketAddress of type type with name path.

      If type is UnixSocketAddressType.PATH, this is equivalent to calling g_unix_socket_address_new().

      If type is UnixSocketAddressType.ANONYMOUS, path and pathLen will be ignored.

      If pathType is UnixSocketAddressType.ABSTRACT, then pathLen bytes of path will be copied to the socket's path, and only those bytes will be considered part of the name. (If pathLen is -1, then path is assumed to be NUL-terminated.) For example, if path was "test", then calling g_socket_address_get_native_size() on the returned socket would return 7 (2 bytes of overhead, 1 byte for the abstract-socket indicator byte, and 4 bytes for the name "test").

      If pathType is UnixSocketAddressType.ABSTRACT_PADDED, then pathLen bytes of path will be copied to the socket's path, the rest of the path will be padded with 0 bytes, and the entire zero-padded buffer will be considered the name. (As above, if pathLen is -1, then path is assumed to be NUL-terminated.) In this case, g_socket_address_get_native_size() will always return the full size of a struct sockaddr_un, although g_unix_socket_address_get_path_len() will still return just the length of path.

      UnixSocketAddressType.ABSTRACT is preferred over UnixSocketAddressType.ABSTRACT_PADDED for new programs. Of course, when connecting to a server created by another process, you must use the appropriate type corresponding to how that process created its listening socket.

      Parameters:
      path - the name
      type - a GUnixSocketAddressType
      Returns:
      a new GUnixSocketAddress
      Since:
      2.26
    • abstractNamesSupported

      public static boolean abstractNamesSupported()
      Checks if abstract UNIX domain socket names are supported.
      Returns:
      true if supported, false otherwise
      Since:
      2.22
    • getAddressType

      public UnixSocketAddressType getAddressType()
      Gets address's type.
      Returns:
      a GUnixSocketAddressType
      Since:
      2.26
    • getIsAbstract

      @Deprecated public boolean getIsAbstract()
      Deprecated.
      Use g_unix_socket_address_get_address_type()
      Tests if this UnixSocketAddress is abstract.
      Returns:
      true if the address is abstract, false otherwise
      Since:
      2.22
    • getPath

      public String getPath()

      Gets address's path, or for abstract sockets the "name".

      Guaranteed to be zero-terminated, but an abstract socket may contain embedded zeros, and thus you should use g_unix_socket_address_get_path_len() to get the true length of this string.

      Returns:
      the path for this UnixSocketAddress
      Since:
      2.22
    • getPathLen

      public long getPathLen()

      Gets the length of address's path.

      For details, see g_unix_socket_address_get_path().

      Returns:
      the length of the path
      Since:
      2.22
    • builder

      public static UnixSocketAddress.Builder<? extends UnixSocketAddress.Builder> builder()
      A UnixSocketAddress.Builder object constructs a UnixSocketAddress with the specified properties. Use the various set...() methods to set properties, and finish construction with UnixSocketAddress.Builder.build().
      Returns:
      the builder object