+sub unpack_sockaddr_in46 {
+ my ($sin) = @_;
+ my $family = Socket::sockaddr_family($sin);
+ my ($port, $host) = ($family == AF_INET6 ? Socket::unpack_sockaddr_in6($sin)
+ : Socket::unpack_sockaddr_in($sin));
+ return ($family, $port, $host);
+}
+
+sub getaddrinfo_all {
+ my ($hostname, @opts) = @_;
+ my %hints = ( flags => AI_V4MAPPED | AI_ALL,
+ @opts );
+ my ($err, @res) = Socket::getaddrinfo($hostname, '0', \%hints);
+ die "failed to get address info for: $hostname: $err\n" if $err;
+ return @res;
+}
+
+sub get_host_address_family {
+ my ($hostname, $socktype) = @_;
+ my @res = getaddrinfo_all($hostname, socktype => $socktype);
+ return $res[0]->{family};
+}
+
+# Parses any sane kind of host, or host+port pair:
+# The port is always optional and thus may be undef.
+sub parse_host_and_port {
+ my ($address) = @_;
+ if ($address =~ /^($IPV4RE|[[:alnum:]\-.]+)(?::(\d+))?$/ || # ipv4 or host with optional ':port'
+ $address =~ /^\[($IPV6RE|$IPV4RE|[[:alnum:]\-.]+)\](?::(\d+))?$/ || # anything in brackets with optional ':port'
+ $address =~ /^($IPV6RE)(?:\.(\d+))?$/) # ipv6 with optional port separated by dot
+ {
+ return ($1, $2, 1); # end with 1 to support simple if(parse...) tests
+ }
+ return; # nothing
+}
+
+sub unshare($) {
+ my ($flags) = @_;
+ return 0 == syscall(272, $flags);
+}
+