pack_sockaddr_un#

Build a packed sockaddr_un from a filesystem path, for use with Unix-domain sockets.

Synopsis#

my $sun = pack_sockaddr_un($path);

What you get back#

A fixed-size byte string matching the platform’s struct sockaddr_un, ready for bind or connect against an AF_UNIX / AF_LOCAL socket.

Examples#

socket(my $sock, AF_UNIX, SOCK_STREAM, 0) or die $!;
connect($sock, pack_sockaddr_un("/tmp/app.sock")) or die $!;
# Abstract Linux namespace: first byte is NUL.
my $sun = pack_sockaddr_un("\0app.mysvc");

Edge cases#

  • A path longer than sun_path (108 bytes) warns “Path length (N) is longer than maximum supported length (108) and will be truncated” and is cut to fit.

  • An undef path croaks “Undefined path for Socket::pack_sockaddr_un”.

  • An abstract name (leading NUL, more than one byte) gives a structure that ends where the name ends, since such a name may itself contain NULs; any other path gives the full structure.

See also#

  • unpack_sockaddr_un - reverse direction.

  • sockaddr_family - read the family byte from an unknown sockaddr.