| 1 | # | ||
| 2 | # Net::Address::IPv4::Local class, | ||
| 3 | # a class for discovering the local system's IP address. | ||
| 4 | # | ||
| 5 | # (C) 2005 Julian Mehnle <julian@mehnle.net> | ||
| 6 | # $Id: Local.pm,v 1.4 2005/05/05 12:57:28 julian Exp $ | ||
| 7 | # | ||
| 8 | ############################################################################## | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Net::Address::IPv4::Local - A class for discovering the local system's IP | ||
| 13 | address | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | package Net::Address::IPv4::Local; | ||
| 18 | |||
| 19 | =head1 VERSION | ||
| 20 | |||
| 21 | 0.12 | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | our $VERSION = '0.12'; | ||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | use Net::Address::IPv4::Local; | ||
| 30 | |||
| 31 | # Get the local system's IP address that is "connected" to "the internet": | ||
| 32 | my $address = Net::Address::IPv4::Local->public; | ||
| 33 | |||
| 34 | # Get the local system's IP address that is "connected" to the given remote | ||
| 35 | # IP address: | ||
| 36 | my $address = Net::Address::IPv4::Local->connected_to($remote_address); | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | use warnings; | ||
| 41 | use strict; | ||
| 42 | |||
| 43 | use Error qw(:try); | ||
| 44 | |||
| 45 | use IO::Socket::INET; | ||
| 46 | |||
| 47 | use constant DEFAULT_REMOTE_ADDRESS => '198.41.0.4'; # a.root-servers.net | ||
| 48 | use constant DEFAULT_REMOTE_PORT => 53; # DNS | ||
| 49 | |||
| 50 | # Interface: | ||
| 51 | ############################################################################## | ||
| 52 | |||
| 53 | =head1 DESCRIPTION | ||
| 54 | |||
| 55 | B<Net::Address::IPv4::Local> discovers the local system's IP address that would | ||
| 56 | be used as the source address when contacting "the internet" or a certain | ||
| 57 | specified remote IP address. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub public; | ||
| 62 | sub connected_to; | ||
| 63 | |||
| 64 | # Implementation: | ||
| 65 | ############################################################################## | ||
| 66 | |||
| 67 | =head2 Instance methods | ||
| 68 | |||
| 69 | This class just provides the following instance methods: | ||
| 70 | |||
| 71 | =over | ||
| 72 | |||
| 73 | =item B<public>: RETURNS SCALAR; THROWS Net::Address::IPv4::Local::Error | ||
| 74 | |||
| 75 | Returns the textual representation of the local system's IP address that is | ||
| 76 | "connected" to "the internet". | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub public { | ||
| 81 | my ($class) = @_; | ||
| 82 | return $class->connected_to(DEFAULT_REMOTE_ADDRESS); | ||
| 83 | } | ||
| 84 | |||
| 85 | =item B<connected_to($remote_address)>: RETURNS SCALAR; THROWS | ||
| 86 | Net::Address::IPv4::Local::Error | ||
| 87 | |||
| 88 | Returns the textual representation of the local system's IP address that is | ||
| 89 | "connected" to the given remote IP address. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub connected_to { | ||
| 94 | my ($class, $remote_address) = @_; | ||
| 95 | |||
| 96 | my $socket = IO::Socket::INET->new( | ||
| 97 | Proto => 'udp', | ||
| 98 | PeerAddr => $remote_address, | ||
| 99 | PeerPort => DEFAULT_REMOTE_PORT | ||
| 100 | ); | ||
| 101 | |||
| 102 | throw Net::Address::IPv4::Local::Error("Unable to create UDP socket: $!") | ||
| 103 | if not defined($socket); | ||
| 104 | |||
| 105 | return inet_ntoa($socket->sockaddr); | ||
| 106 | } | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =head1 AVAILABILITY and SUPPORT | ||
| 111 | |||
| 112 | The latest version of Net::Address::IPv4::Local is available on CPAN and at | ||
| 113 | L<http://www.mehnle.net/software/net-address-ipv4-local>. | ||
| 114 | |||
| 115 | Support is usually (but not guaranteed to be) given by the author, Julian | ||
| 116 | Mehnle <julian@mehnle.net>. | ||
| 117 | |||
| 118 | =head1 AUTHOR and LICENSE | ||
| 119 | |||
| 120 | Net::Address::IPv4::Local is Copyright (C) 2005 Julian Mehnle | ||
| 121 | <julian@mehnle.net>. | ||
| 122 | |||
| 123 | Net::Address::IPv4::Local is free software. You may use, modify, and | ||
| 124 | distribute it under the same terms as Perl itself, i.e. under the GNU GPL or | ||
| 125 | the Artistic License. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | package Net::Address::IPv4::Local::Error; | ||
| 130 | use base qw(Error::Simple); | ||
| 131 | |||
| 132 | package Net::Address::IPv4::Local; | ||
| 133 | |||
| 134 | 1; | ||
| 135 | |||
| 136 | # vim:tw=79 |