Subject: | Add new hostport methods |
Since IPv4 and IPv6 have different formats for host/port combos, this should be
normalized to a single method for handling both. The methods below should handle
all cases of host/port/scope for both domain models.
sub sockhostport {
@_ == 1 or croak 'usage: $sock->sockhostport()';
my ($sock) = @_;
return undef unless (my $host = $sock->sockhost);
if ($sock->sockdomain == AF_INET6) {
my $s = $sock->sockscope;
$s = $s ? sprintf('%%%u', $s) : '';
return sprintf('[%s%s]:%u', $host, $s, $sock->sockport);
}
return sprintf('%s:%d', $host, $sock->sockport);
}
sub peerhostport {
@_ == 1 or croak 'usage: $sock->peerhostport()';
my ($sock) = @_;
return undef unless (my $host = $sock->peerhost);
if ($sock->sockdomain == AF_INET6) {
my $s = $sock->peerscope;
$s = $s ? sprintf('%%%u', $s) : '';
return sprintf('[%s%s]:%u', $host, $s, $sock->peerport);
}
return sprintf('%s:%d', $host, $sock->peerport);
}