… | |
… | |
35 | |
35 | |
36 | BEGIN { |
36 | BEGIN { |
37 | *socket_inet_aton = \&Socket::inet_aton; # take a copy, in case Coro::LWP overrides it |
37 | *socket_inet_aton = \&Socket::inet_aton; # take a copy, in case Coro::LWP overrides it |
38 | } |
38 | } |
39 | |
39 | |
40 | our @EXPORT = qw(inet_aton tcp_server tcp_connect); |
40 | our @EXPORT = qw(parse_ipv4 parse_ipv6 parse_ip format_ip inet_aton tcp_server tcp_connect); |
41 | |
41 | |
42 | our $VERSION = '1.0'; |
42 | our $VERSION = '1.0'; |
43 | |
43 | |
44 | =item $ipn = parse_ipv4 $dotted_quad |
44 | =item $ipn = parse_ipv4 $dotted_quad |
45 | |
45 | |
… | |
… | |
67 | + ($_[0] << 24) |
67 | + ($_[0] << 24) |
68 | + ($_[1] << 16) |
68 | + ($_[1] << 16) |
69 | + ($_[2] << 8); |
69 | + ($_[2] << 8); |
70 | } |
70 | } |
71 | |
71 | |
72 | =item $ipn = parse_ipv4 $dotted_quad |
72 | =item $ipn = parse_ipv6 $textual_ipv6_address |
73 | |
73 | |
74 | Tries to parse the given IPv6 address and return it in |
74 | Tries to parse the given IPv6 address and return it in |
75 | octet form (or undef when it isn't in a parsable format). |
75 | octet form (or undef when it isn't in a parsable format). |
76 | |
76 | |
77 | Should support all forms specified by RFC 2373 (and additionally all IPv4 |
77 | Should support all forms specified by RFC 2373 (and additionally all IPv4 |
78 | formst supported by parse_ipv4). |
78 | forms supported by parse_ipv4). |
|
|
79 | |
|
|
80 | This function works similarly to C<inet_pton AF_INET6, ...>. |
79 | |
81 | |
80 | =cut |
82 | =cut |
81 | |
83 | |
82 | sub parse_ipv6($) { |
84 | sub parse_ipv6($) { |
83 | # quick test to avoid longer processing |
85 | # quick test to avoid longer processing |
… | |
… | |
91 | } |
93 | } |
92 | |
94 | |
93 | my @h = split /:/, $h; |
95 | my @h = split /:/, $h; |
94 | my @t = split /:/, $t; |
96 | my @t = split /:/, $t; |
95 | |
97 | |
96 | # check four ipv4 tail |
98 | # check for ipv4 tail |
97 | if (@t && $t[-1]=~ /\./) { |
99 | if (@t && $t[-1]=~ /\./) { |
98 | return undef if $n > 6; |
100 | return undef if $n > 6; |
99 | |
101 | |
100 | my $ipn = parse_ipv4 pop @t |
102 | my $ipn = parse_ipv4 pop @t |
101 | or return undef; |
103 | or return undef; |
… | |
… | |
128 | |
130 | |
129 | =item $text = format_ip $ipn |
131 | =item $text = format_ip $ipn |
130 | |
132 | |
131 | Takes either an IPv4 address (4 octets) or and IPv6 address (16 octets) |
133 | Takes either an IPv4 address (4 octets) or and IPv6 address (16 octets) |
132 | and converts it into textual form. |
134 | and converts it into textual form. |
|
|
135 | |
|
|
136 | This function works similarly to C<inet_ntop AF_INET || AF_INET6, ...>, |
|
|
137 | except it automatically detects the address type. |
133 | |
138 | |
134 | =cut |
139 | =cut |
135 | |
140 | |
136 | sub format_ip; |
141 | sub format_ip; |
137 | sub format_ip($) { |
142 | sub format_ip($) { |