3 # Copyright (C) 2000 Graham Barr. All rights reserved.
4 # Copyright (C) 2013-2014, 2016, 2020 Steve Hay. All rights reserved.
5 # This module is free software; you can redistribute it and/or modify it under
6 # the same terms as Perl itself, i.e. under the terms of either the GNU General
7 # Public License or the Artistic License, as specified in the F<LICENCE> file.
17 use Socket qw(inet_aton inet_ntoa);
19 our @EXPORT = qw(%NetConfig);
20 our @ISA = qw(Net::LocalCfg Exporter);
21 our $VERSION = "3.12";
23 our($CONFIGURE, $LIBNET_CFG);
27 pop @INC if $INC[-1] eq '.';
29 require Net::LocalCfg;
41 ftp_firewall => undef,
49 # Try to get as much configuration info as possible from InternetConfig
52 ## no critic (BuiltinFunctions::ProhibitStringyEval)
53 $^O eq 'MacOS' and eval <<TRY_INTERNET_CONFIG;
54 use Mac::InternetConfig;
58 nntp_hosts => [ \$InternetConfig{ kICNNTPHost() } ],
59 pop3_hosts => [ \$InternetConfig{ kICMailAccount() } =~ /\@(.*)/ ],
60 smtp_hosts => [ \$InternetConfig{ kICSMTPHost() } ],
61 ftp_testhost => \$InternetConfig{ kICFTPHost() } ? \$InternetConfig{ kICFTPHost()} : undef,
62 ph_hosts => [ \$InternetConfig{ kICPhHost() } ],
63 ftp_ext_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
64 ftp_int_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
66 \$InternetConfig{ kICUseSocks() } ? [ \$InternetConfig{ kICSocksHost() } ] : [],
68 \$InternetConfig{ kICUseFTPProxy() } ? [ \$InternetConfig{ kICFTPProxyHost() } ] : [],
70 \@NetConfig{keys %nc} = values %nc;
77 $file =~ s/Config.pm/libnet.cfg/;
79 $ref = eval { local $SIG{__DIE__}; do $file };
80 if (ref($ref) eq 'HASH') {
81 %NetConfig = (%NetConfig, %{$ref});
85 if ($< == $> and !$CONFIGURE) {
86 my $home = eval { local $SIG{__DIE__}; (getpwuid($>))[7] } || $ENV{HOME};
87 $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH} || '') if defined $ENV{HOMEDRIVE};
89 $file = $home . "/.libnetrc";
90 $ref = eval { local $SIG{__DIE__}; do $file } if -f $file;
91 %NetConfig = (%NetConfig, %{$ref})
92 if ref($ref) eq 'HASH';
96 while (($k, $v) = each %NetConfig) {
98 if ($k =~ /_hosts$/ and $k ne "test_hosts" and defined($v) and !ref($v));
101 # Take a hostname and determine if it is inside the firewall
104 sub requires_firewall {
105 shift; # ignore package
108 return 0 unless defined $NetConfig{'ftp_firewall'};
110 $host = inet_aton($host) or return -1;
111 $host = inet_ntoa($host);
113 if (exists $NetConfig{'local_netmask'}) {
114 my $quad = unpack("N", pack("C*", split(/\./, $host)));
115 my $list = $NetConfig{'local_netmask'};
116 $list = [$list] unless ref($list);
118 my ($net, $bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
119 my $mask = ~0 << (32 - $bits);
120 my $addr = unpack("N", pack("C*", split(/\./, $net)));
122 return 0 if (($addr & $mask) == ($quad & $mask));
130 *is_external = \&requires_firewall;
138 Net::Config - Local configuration data for libnet
142 use Net::Config qw(%NetConfig);
146 C<Net::Config> holds configuration data for the modules in the libnet
147 distribution. During installation you will be asked for these values.
149 The configuration data is held globally in a file in the perl installation
150 tree, but a user may override any of these values by providing their own. This
151 can be done by having a C<.libnetrc> file in their home directory. This file
152 should return a reference to a HASH containing the keys described below.
157 nntp_hosts => [ "my_preferred_host" ],
158 ph_hosts => [ "my_ph_server" ],
164 C<Net::Config> defines the following methods. They are methods as they are
165 invoked as class methods. This is because C<Net::Config> inherits from
166 C<Net::LocalCfg> so you can override these methods if you want.
170 =item C<requires_firewall($host)>
172 Attempts to determine if a given host is outside your firewall. Possible
175 -1 Cannot lookup hostname
176 0 Host is inside firewall (or there is no ftp_firewall entry)
177 1 Host is outside the firewall
179 This is done by using hostname lookup and the C<local_netmask> entry in
180 the configuration data.
184 =head2 NetConfig Values
202 Each is a reference to an array of hostnames (in order of preference),
203 which should be used for the given protocol
207 Your internet domain name
211 If you have an FTP proxy firewall (B<NOT> an HTTP or SOCKS firewall)
212 then this value should be set to the firewall hostname. If your firewall
213 does not listen to port 21, then this value should be set to
214 C<"hostname:port"> (eg C<"hostname:99">)
216 =item ftp_firewall_type
218 There are many different ftp firewall products available. But unfortunately
219 there is no standard for how to traverse a firewall. The list below shows the
220 sequence of commands that Net::FTP will use
222 user Username for remote host
223 pass Password for remote host
224 fwuser Username for firewall
225 fwpass Password for firewall
226 remote.host The hostname of the remote ftp server
236 USER user@remote.host
243 USER user@remote.host
264 USER user@fwuser@remote.site
269 USER fwuser@remote.site
276 USER user@remote.host
283 =item ftp_ext_passive
285 =item ftp_int_passive
287 FTP servers can work in passive or active mode. Active mode is when
288 you want to transfer data you have to tell the server the address and
289 port to connect to. Passive mode is when the server provide the
290 address and port and you establish the connection.
292 With some firewalls active mode does not work as the server cannot
293 connect to your machine (because you are behind a firewall) and the firewall
294 does not re-write the command. In this case you should set C<ftp_ext_passive>
297 Some servers are configured to only work in passive mode. If you have
298 one of these you can force C<Net::FTP> to always transfer in passive
299 mode; when not going via a firewall, by setting C<ftp_int_passive> to
304 A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
305 These are used by the C<requires_firewall> function to determine if a given
306 host is inside or outside your firewall.
310 The following entries are used during installation & testing on the
317 If true then C<make test> may attempt to connect to hosts given in the
322 If true then C<Configure> will check each hostname given that it exists
328 The following symbols are, or can be, exported by this module:
332 =item Default Exports
336 =item Optional Exports
352 Graham Barr E<lt>L<gbarr@pobox.com|mailto:gbarr@pobox.com>E<gt>.
354 Steve Hay E<lt>L<shay@cpan.org|mailto:shay@cpan.org>E<gt> is now maintaining
355 libnet as of version 1.22_02.
359 Copyright (C) 2000 Graham Barr. All rights reserved.
361 Copyright (C) 2013-2014, 2016, 2020 Steve Hay. All rights reserved.
365 This module is free software; you can redistribute it and/or modify it under the
366 same terms as Perl itself, i.e. under the terms of either the GNU General Public
367 License or the Artistic License, as specified in the F<LICENCE> file.
379 See the F<Changes> file.