3 # Versions up to 1.11 Copyright (c) 2000 Graham Barr <gbarr@pobox.com>.
5 # Changes in Version 1.11_01 onwards Copyright (C) 2013-2014 Steve Hay. All
7 # This module is free software; you can redistribute it and/or modify it under
8 # the same terms as Perl itself, i.e. under the terms of either the GNU General
9 # Public License or the Artistic License, as specified in the F<LICENCE> file.
19 use Socket qw(inet_aton inet_ntoa);
21 our @EXPORT = qw(%NetConfig);
22 our @ISA = qw(Net::LocalCfg Exporter);
23 our $VERSION = "3.08";
25 our($CONFIGURE, $LIBNET_CFG);
27 eval { local $SIG{__DIE__}; require Net::LocalCfg };
38 ftp_firewall => undef,
46 # Try to get as much configuration info as possible from InternetConfig
49 ## no critic (BuiltinFunctions::ProhibitStringyEval)
50 $^O eq 'MacOS' and eval <<TRY_INTERNET_CONFIG;
51 use Mac::InternetConfig;
55 nntp_hosts => [ \$InternetConfig{ kICNNTPHost() } ],
56 pop3_hosts => [ \$InternetConfig{ kICMailAccount() } =~ /\@(.*)/ ],
57 smtp_hosts => [ \$InternetConfig{ kICSMTPHost() } ],
58 ftp_testhost => \$InternetConfig{ kICFTPHost() } ? \$InternetConfig{ kICFTPHost()} : undef,
59 ph_hosts => [ \$InternetConfig{ kICPhHost() } ],
60 ftp_ext_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
61 ftp_int_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
63 \$InternetConfig{ kICUseSocks() } ? [ \$InternetConfig{ kICSocksHost() } ] : [],
65 \$InternetConfig{ kICUseFTPProxy() } ? [ \$InternetConfig{ kICFTPProxyHost() } ] : [],
67 \@NetConfig{keys %nc} = values %nc;
74 $file =~ s/Config.pm/libnet.cfg/;
76 $ref = eval { local $SIG{__DIE__}; do $file };
77 if (ref($ref) eq 'HASH') {
78 %NetConfig = (%NetConfig, %{$ref});
82 if ($< == $> and !$CONFIGURE) {
83 my $home = eval { local $SIG{__DIE__}; (getpwuid($>))[7] } || $ENV{HOME};
84 $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH} || '') if defined $ENV{HOMEDRIVE};
86 $file = $home . "/.libnetrc";
87 $ref = eval { local $SIG{__DIE__}; do $file } if -f $file;
88 %NetConfig = (%NetConfig, %{$ref})
89 if ref($ref) eq 'HASH';
93 while (($k, $v) = each %NetConfig) {
95 if ($k =~ /_hosts$/ and $k ne "test_hosts" and defined($v) and !ref($v));
98 # Take a hostname and determine if it is inside the firewall
101 sub requires_firewall {
102 shift; # ignore package
105 return 0 unless defined $NetConfig{'ftp_firewall'};
107 $host = inet_aton($host) or return -1;
108 $host = inet_ntoa($host);
110 if (exists $NetConfig{'local_netmask'}) {
111 my $quad = unpack("N", pack("C*", split(/\./, $host)));
112 my $list = $NetConfig{'local_netmask'};
113 $list = [$list] unless ref($list);
115 my ($net, $bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
116 my $mask = ~0 << (32 - $bits);
117 my $addr = unpack("N", pack("C*", split(/\./, $net)));
119 return 0 if (($addr & $mask) == ($quad & $mask));
127 *is_external = \&requires_firewall;
135 Net::Config - Local configuration data for libnet
139 use Net::Config qw(%NetConfig);
143 C<Net::Config> holds configuration data for the modules in the libnet
144 distribution. During installation you will be asked for these values.
146 The configuration data is held globally in a file in the perl installation
147 tree, but a user may override any of these values by providing their own. This
148 can be done by having a C<.libnetrc> file in their home directory. This file
149 should return a reference to a HASH containing the keys described below.
154 nntp_hosts => [ "my_preferred_host" ],
155 ph_hosts => [ "my_ph_server" ],
161 C<Net::Config> defines the following methods. They are methods as they are
162 invoked as class methods. This is because C<Net::Config> inherits from
163 C<Net::LocalCfg> so you can override these methods if you want.
167 =item requires_firewall ( HOST )
169 Attempts to determine if a given host is outside your firewall. Possible
172 -1 Cannot lookup hostname
173 0 Host is inside firewall (or there is no ftp_firewall entry)
174 1 Host is outside the firewall
176 This is done by using hostname lookup and the C<local_netmask> entry in
177 the configuration data.
181 =head1 NetConfig VALUES
199 Each is a reference to an array of hostnames (in order of preference),
200 which should be used for the given protocol
204 Your internet domain name
208 If you have an FTP proxy firewall (B<NOT> an HTTP or SOCKS firewall)
209 then this value should be set to the firewall hostname. If your firewall
210 does not listen to port 21, then this value should be set to
211 C<"hostname:port"> (eg C<"hostname:99">)
213 =item ftp_firewall_type
215 There are many different ftp firewall products available. But unfortunately
216 there is no standard for how to traverse a firewall. The list below shows the
217 sequence of commands that Net::FTP will use
219 user Username for remote host
220 pass Password for remote host
221 fwuser Username for firewall
222 fwpass Password for firewall
223 remote.host The hostname of the remote ftp server
233 USER user@remote.host
240 USER user@remote.host
261 USER user@fwuser@remote.site
266 USER fwuser@remote.site
273 USER user@remote.host
280 =item ftp_ext_passive
282 =item ftp_int_passive
284 FTP servers can work in passive or active mode. Active mode is when
285 you want to transfer data you have to tell the server the address and
286 port to connect to. Passive mode is when the server provide the
287 address and port and you establish the connection.
289 With some firewalls active mode does not work as the server cannot
290 connect to your machine (because you are behind a firewall) and the firewall
291 does not re-write the command. In this case you should set C<ftp_ext_passive>
294 Some servers are configured to only work in passive mode. If you have
295 one of these you can force C<Net::FTP> to always transfer in passive
296 mode; when not going via a firewall, by setting C<ftp_int_passive> to
301 A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
302 These are used by the C<requires_firewall> function to determine if a given
303 host is inside or outside your firewall.
307 The following entries are used during installation & testing on the
314 If true then C<make test> may attempt to connect to hosts given in the
319 If true then C<Configure> will check each hostname given that it exists
325 Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
327 Steve Hay E<lt>F<shay@cpan.org>E<gt> is now maintaining libnet as of version
332 Versions up to 1.11 Copyright (c) 1998-2011 Graham Barr. All rights reserved.
333 Changes in Version 1.11_01 onwards Copyright (C) 2013-2014 Steve Hay. All
336 This module is free software; you can redistribute it and/or modify it under the
337 same terms as Perl itself, i.e. under the terms of either the GNU General Public
338 License or the Artistic License, as specified in the F<LICENCE> file.