3 # Copyright (c) 2000 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
10 use vars qw(@ISA @EXPORT %NetConfig $VERSION $CONFIGURE $LIBNET_CFG);
11 use Socket qw(inet_aton inet_ntoa);
14 @EXPORT = qw(%NetConfig);
15 @ISA = qw(Net::LocalCfg Exporter);
18 eval { local $SIG{__DIE__}; require Net::LocalCfg };
29 ftp_firewall => undef,
37 # Try to get as much configuration info as possible from InternetConfig
39 $^O eq 'MacOS' and eval <<TRY_INTERNET_CONFIG;
40 use Mac::InternetConfig;
44 nntp_hosts => [ \$InternetConfig{ kICNNTPHost() } ],
45 pop3_hosts => [ \$InternetConfig{ kICMailAccount() } =~ /\@(.*)/ ],
46 smtp_hosts => [ \$InternetConfig{ kICSMTPHost() } ],
47 ftp_testhost => \$InternetConfig{ kICFTPHost() } ? \$InternetConfig{ kICFTPHost()} : undef,
48 ph_hosts => [ \$InternetConfig{ kICPhHost() } ],
49 ftp_ext_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
50 ftp_int_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
52 \$InternetConfig{ kICUseSocks() } ? [ \$InternetConfig{ kICSocksHost() } ] : [],
54 \$InternetConfig{ kICUseFTPProxy() } ? [ \$InternetConfig{ kICFTPProxyHost() } ] : [],
56 \@NetConfig{keys %nc} = values %nc;
62 $file =~ s/Config.pm/libnet.cfg/;
64 $ref = eval { local $SIG{__DIE__}; do $file };
65 if (ref($ref) eq 'HASH') {
66 %NetConfig = (%NetConfig, %{$ref});
70 if ($< == $> and !$CONFIGURE) {
71 my $home = eval { local $SIG{__DIE__}; (getpwuid($>))[7] } || $ENV{HOME};
72 $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH} || '') if defined $ENV{HOMEDRIVE};
74 $file = $home . "/.libnetrc";
75 $ref = eval { local $SIG{__DIE__}; do $file } if -f $file;
76 %NetConfig = (%NetConfig, %{$ref})
77 if ref($ref) eq 'HASH';
81 while (($k, $v) = each %NetConfig) {
83 if ($k =~ /_hosts$/ and $k ne "test_hosts" and defined($v) and !ref($v));
86 # Take a hostname and determine if it is inside the firewall
89 sub requires_firewall {
90 shift; # ignore package
93 return 0 unless defined $NetConfig{'ftp_firewall'};
95 $host = inet_aton($host) or return -1;
96 $host = inet_ntoa($host);
98 if (exists $NetConfig{'local_netmask'}) {
99 my $quad = unpack("N", pack("C*", split(/\./, $host)));
100 my $list = $NetConfig{'local_netmask'};
101 $list = [$list] unless ref($list);
103 my ($net, $bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
104 my $mask = ~0 << (32 - $bits);
105 my $addr = unpack("N", pack("C*", split(/\./, $net)));
107 return 0 if (($addr & $mask) == ($quad & $mask));
115 use vars qw(*is_external);
116 *is_external = \&requires_firewall;
124 Net::Config - Local configuration data for libnet
128 use Net::Config qw(%NetConfig);
132 C<Net::Config> holds configuration data for the modules in the libnet
133 distribution. During installation you will be asked for these values.
135 The configuration data is held globally in a file in the perl installation
136 tree, but a user may override any of these values by providing their own. This
137 can be done by having a C<.libnetrc> file in their home directory. This file
138 should return a reference to a HASH containing the keys described below.
143 nntp_hosts => [ "my_preferred_host" ],
144 ph_hosts => [ "my_ph_server" ],
150 C<Net::Config> defines the following methods. They are methods as they are
151 invoked as class methods. This is because C<Net::Config> inherits from
152 C<Net::LocalCfg> so you can override these methods if you want.
156 =item requires_firewall HOST
158 Attempts to determine if a given host is outside your firewall. Possible
161 -1 Cannot lookup hostname
162 0 Host is inside firewall (or there is no ftp_firewall entry)
163 1 Host is outside the firewall
165 This is done by using hostname lookup and the C<local_netmask> entry in
166 the configuration data.
170 =head1 NetConfig VALUES
188 Each is a reference to an array of hostnames (in order of preference),
189 which should be used for the given protocol
193 Your internet domain name
197 If you have an FTP proxy firewall (B<NOT> an HTTP or SOCKS firewall)
198 then this value should be set to the firewall hostname. If your firewall
199 does not listen to port 21, then this value should be set to
200 C<"hostname:port"> (eg C<"hostname:99">)
202 =item ftp_firewall_type
204 There are many different ftp firewall products available. But unfortunately
205 there is no standard for how to traverse a firewall. The list below shows the
206 sequence of commands that Net::FTP will use
208 user Username for remote host
209 pass Password for remote host
210 fwuser Username for firewall
211 fwpass Password for firewall
212 remote.host The hostname of the remote ftp server
222 USER user@remote.host
229 USER user@remote.host
250 USER user@fwuser@remote.site
255 USER fwuser@remote.site
262 USER user@remote.host
269 =item ftp_ext_passive
271 =item ftp_int_passive
273 FTP servers can work in passive or active mode. Active mode is when
274 you want to transfer data you have to tell the server the address and
275 port to connect to. Passive mode is when the server provide the
276 address and port and you establish the connection.
278 With some firewalls active mode does not work as the server cannot
279 connect to your machine (because you are behind a firewall) and the firewall
280 does not re-write the command. In this case you should set C<ftp_ext_passive>
283 Some servers are configured to only work in passive mode. If you have
284 one of these you can force C<Net::FTP> to always transfer in passive
285 mode; when not going via a firewall, by setting C<ftp_int_passive> to
290 A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
291 These are used by the C<requires_firewall> function to determine if a given
292 host is inside or outside your firewall.
296 The following entries are used during installation & testing on the
303 If true then C<make test> may attempt to connect to hosts given in the
308 If true then C<Configure> will check each hostname given that it exists