| 1 | # | ||
| 2 | # Net::Address::IP::Local class, | ||
| 3 | # a class for discovering the local system's IP address. | ||
| 4 | # | ||
| 5 | # (C) 2005-2009 Julian Mehnle <julian@mehnle.net> | ||
| 6 | # $Id: Local.pm 24 2009-01-14 21:23:40Z julian $ | ||
| 7 | # | ||
| 8 | ############################################################################### | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Net::Address::IP::Local - A class for discovering the local system's IP address | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | package Net::Address::IP::Local; | ||
| 17 | |||
| 18 | =head1 VERSION | ||
| 19 | |||
| 20 | 0.1.2 | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | use version; our $VERSION = qv('0.1.2'); | ||
| 25 | |||
| 26 | use warnings; | ||
| 27 | use strict; | ||
| 28 | |||
| 29 | use Error ':try'; | ||
| 30 | |||
| 31 | use constant TRUE => (0 == 0); | ||
| 32 | use constant FALSE => not TRUE; | ||
| 33 | |||
| 34 | use constant remote_address_ipv4_default => '198.41.0.4'; # a.root-servers.net | ||
| 35 | use constant remote_address_ipv6_default => '2001:503:ba3e::2:30'; # a.root-servers.net | ||
| 36 | |||
| 37 | use constant remote_port_default => 53; # DNS | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | use Net::Address::IP::Local; | ||
| 42 | |||
| 43 | # Get the local system's IP address that is "en route" to "the internet": | ||
| 44 | my $address = Net::Address::IP::Local->public; | ||
| 45 | my $address_ipv4 = Net::Address::IP::Local->public_ipv4; | ||
| 46 | my $address_ipv6 = Net::Address::IP::Local->public_ipv6; | ||
| 47 | |||
| 48 | # Get the local system's IP address that is "en route" to the given remote | ||
| 49 | # IP address: | ||
| 50 | my $address = Net::Address::IP::Local->connected_to($remote_address); | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | B<Net::Address::IP::Local> discovers the local system's IP address that would | ||
| 55 | be used as the source address when contacting "the internet" or a certain | ||
| 56 | specified remote IP address. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | # Implementation: | ||
| 61 | ############################################################################### | ||
| 62 | |||
| 63 | =head2 Class methods | ||
| 64 | |||
| 65 | This class just provides the following class methods: | ||
| 66 | |||
| 67 | =over | ||
| 68 | |||
| 69 | =item B<public>: returns I<string>; throws I<Net::Address::IP::Local::Error> | ||
| 70 | |||
| 71 | Returns the textual representation of the local system's IP address that is | ||
| 72 | "en route" to "the internet". If the system supports IPv6 and has an IPv6 | ||
| 73 | address that is "en route" to "the internet", that is returned. Otherwise, the | ||
| 74 | IPv4 address that is "en route" to "the internet" is returned. If there is no | ||
| 75 | route at all to the internet, a I<Net::Address::IP::Local::Error> exception is | ||
| 76 | thrown. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub public { | ||
| 81 | my ($class) = @_; | ||
| 82 | |||
| 83 | return $class->connected_to($class->remote_address_ipv4_default) | ||
| 84 | if not $class->ipv6_support; | ||
| 85 | # Short-cut for the common case with no IPv6 support. | ||
| 86 | |||
| 87 | my $ip_address; | ||
| 88 | |||
| 89 | try { | ||
| 90 | $ip_address = $class->connected_to($class->remote_address_ipv6_default); | ||
| 91 | } | ||
| 92 | catch Net::Address::IP::Local::Error with { | ||
| 93 | my $error = shift; | ||
| 94 | try { | ||
| 95 | $ip_address = $class->connected_to($class->remote_address_ipv4_default); | ||
| 96 | } | ||
| 97 | catch Net::Address::IP::Local::Error with { | ||
| 98 | # If neither the IPv4 nor IPv6 local address could be determined, | ||
| 99 | # re-throw the first error that occurred: | ||
| 100 | $error->throw; | ||
| 101 | }; | ||
| 102 | }; | ||
| 103 | |||
| 104 | return $ip_address; | ||
| 105 | } | ||
| 106 | |||
| 107 | =item B<public_ipv4>: returns I<string>; throws I<Net::Address::IP::Local::Error> | ||
| 108 | |||
| 109 | Returns the textual representation of the local system's IPv4 address that is "en | ||
| 110 | route" to "the internet". If there is no IPv4 route to the internet, a | ||
| 111 | I<Net::Address::IP::Local::Error> exception is thrown. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub public_ipv4 { | ||
| 116 | my ($class) = @_; | ||
| 117 | $class->ipv4_support | ||
| 118 | or throw Net::Address::IP::Local::Error("IPv4 not supported"); | ||
| 119 | return $class->connected_to($class->remote_address_ipv4_default); | ||
| 120 | } | ||
| 121 | |||
| 122 | =item B<public_ipv6>: returns I<string>; throws I<Net::Address::IP::Local::Error> | ||
| 123 | |||
| 124 | Returns the textual representation of the local system's IPv6 address that is "en | ||
| 125 | route" to "the internet". If there is no IPv6 route to the internet, a | ||
| 126 | I<Net::Address::IP::Local::Error> exception is thrown. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub public_ipv6 { | ||
| 131 | my ($class) = @_; | ||
| 132 | $class->ipv6_support | ||
| 133 | or throw Net::Address::IP::Local::Error("IPv6 not supported"); | ||
| 134 | return $class->connected_to($class->remote_address_ipv6_default); | ||
| 135 | } | ||
| 136 | |||
| 137 | =item B<connected_to($remote_address)>: returns I<string>; throws | ||
| 138 | I<Net::Address::IP::Local::Error> | ||
| 139 | |||
| 140 | Returns the textual representation of the local system's IP address that is "en | ||
| 141 | route" to the given remote IP address. If there is no route to the given | ||
| 142 | remote IP address, a I<Net::Address::IP::Local::Error> exception is thrown. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub connected_to { | ||
| 147 | my ($class, $remote_address) = @_; | ||
| 148 | |||
| 149 | my $socket_class; | ||
| 150 | if ($class->ipv6_support) { | ||
| 151 | $socket_class = 'IO::Socket::INET6'; | ||
| 152 | } | ||
| 153 | elsif ($class->ipv4_support) { | ||
| 154 | $socket_class = 'IO::Socket::INET'; | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | throw Net::Address::IP::Local::Error("Neither IPv4 nor IPv6 supported"); | ||
| 158 | } | ||
| 159 | |||
| 160 | my $socket = $socket_class->new( | ||
| 161 | Proto => 'udp', | ||
| 162 | PeerAddr => $remote_address, | ||
| 163 | PeerPort => $class->remote_port_default | ||
| 164 | ); | ||
| 165 | |||
| 166 | defined($socket) | ||
| 167 | or throw Net::Address::IP::Local::Error("Unable to create UDP socket: $!"); | ||
| 168 | |||
| 169 | return $socket->sockhost; | ||
| 170 | } | ||
| 171 | |||
| 172 | =back | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | # Private helper methods: | ||
| 177 | |||
| 178 | my $ipv4_support; | ||
| 179 | |||
| 180 | sub ipv4_support { | ||
| 181 | if (not defined($ipv4_support)) { | ||
| 182 | eval { require IO::Socket::INET }; | ||
| 183 | $ipv4_support = not $@; | ||
| 184 | } | ||
| 185 | return $ipv4_support; | ||
| 186 | } | ||
| 187 | |||
| 188 | my $ipv6_support; | ||
| 189 | |||
| 190 | sub ipv6_support { | ||
| 191 | if (not defined($ipv6_support)) { | ||
| 192 | eval { require IO::Socket::INET6 }; | ||
| 193 | $ipv6_support = not $@; | ||
| 194 | } | ||
| 195 | return $ipv6_support; | ||
| 196 | } | ||
| 197 | |||
| 198 | =head1 AVAILABILITY and SUPPORT | ||
| 199 | |||
| 200 | The latest version of Net::Address::IP::Local is available on CPAN and at | ||
| 201 | L<http://www.mehnle.net/software/net-address-ip-local-perl>. | ||
| 202 | |||
| 203 | Support is usually (but not guaranteed to be) given by the author, Julian | ||
| 204 | Mehnle <julian@mehnle.net>. | ||
| 205 | |||
| 206 | =head1 AUTHOR and LICENSE | ||
| 207 | |||
| 208 | Net::Address::IP::Local is Copyright (C) 2005-2009 Julian Mehnle | ||
| 209 | <julian@mehnle.net>. | ||
| 210 | |||
| 211 | Net::Address::IP::Local is free software. You may use, modify, and distribute | ||
| 212 | it under the same terms as Perl itself, i.e. under the GNU GPL or the Artistic | ||
| 213 | License. | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | package Net::Address::IP::Local::Error; | ||
| 218 | use base qw(Error::Simple); | ||
| 219 | |||
| 220 | package Net::Address::IP::Local; | ||
| 221 | |||
| 222 | TRUE; |