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);
16 $VERSION = "1.10"; # $Id: //depot/libnet/Net/Config.pm#17 $
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) {
82 $NetConfig{$k} = [ $v ]
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
88 sub requires_firewall {
89 shift; # ignore package
92 return 0 unless defined $NetConfig{'ftp_firewall'};
94 $host = inet_aton($host) or return -1;
95 $host = inet_ntoa($host);
97 if(exists $NetConfig{'local_netmask'}) {
98 my $quad = unpack("N",pack("C*",split(/\./,$host)));
99 my $list = $NetConfig{'local_netmask'};
100 $list = [$list] unless ref($list);
102 my($net,$bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
103 my $mask = ~0 << (32 - $bits);
104 my $addr = unpack("N",pack("C*",split(/\./,$net)));
106 return 0 if (($addr & $mask) == ($quad & $mask));
114 use vars qw(*is_external);
115 *is_external = \&requires_firewall;
123 Net::Config - Local configuration data for libnet
127 use Net::Config qw(%NetConfig);
131 C<Net::Config> holds configuration data for the modules in the libnet
132 distribuion. During installation you will be asked for these values.
134 The configuration data is held globally in a file in the perl installation
135 tree, but a user may override any of these values by providing their own. This
136 can be done by having a C<.libnetrc> file in their home directory. This file
137 should return a reference to a HASH containing the keys described below.
142 nntp_hosts => [ "my_prefered_host" ],
143 ph_hosts => [ "my_ph_server" ],
149 C<Net::Config> defines the following methods. They are methods as they are
150 invoked as class methods. This is because C<Net::Config> inherits from
151 C<Net::LocalCfg> so you can override these methods if you want.
155 =item requires_firewall HOST
157 Attempts to determine if a given host is outside your firewall. Possible
160 -1 Cannot lookup hostname
161 0 Host is inside firewall (or there is no ftp_firewall entry)
162 1 Host is outside the firewall
164 This is done by using hostname lookup and the C<local_netmask> entry in
165 the configuration data.
169 =head1 NetConfig VALUES
187 Each is a reference to an array of hostnames (in order of preference),
188 which should be used for the given protocol
192 Your internet domain name
196 If you have an FTP proxy firewall (B<NOT> an HTTP or SOCKS firewall)
197 then this value should be set to the firewall hostname. If your firewall
198 does not listen to port 21, then this value should be set to
199 C<"hostname:port"> (eg C<"hostname:99">)
201 =item ftp_firewall_type
203 There are many different ftp firewall products available. But unfortunately
204 there is no standard for how to traverse a firewall. The list below shows the
205 sequence of commands that Net::FTP will use
207 user Username for remote host
208 pass Password for remote host
209 fwuser Username for firewall
210 fwpass Password for firewall
211 remote.host The hostname of the remote ftp server
221 USER user@remote.host
228 USER user@remote.host
249 USER user@fwuser@remote.site
254 USER fwuser@remote.site
261 USER user@remote.host
268 =item ftp_ext_passive
272 FTP servers normally work on a non-passive mode. That is when you want to
273 transfer data you have to tell the server the address and port to
276 With some firewalls this does not work as the server cannot
277 connect to your machine (because you are behind a firewall) and the firewall
278 does not re-write the command. In this case you should set C<ftp_ext_passive>
281 Some servers are configured to only work in passive mode. If you have
282 one of these you can force C<Net::FTP> to always transfer in passive
283 mode; when not going via a firewall, by setting C<ftp_int_passive> to
288 A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
289 These are used by the C<requires_firewall> function to determine if a given
290 host is inside or outside your firewall.
294 The following entries are used during installation & testing on the
301 If true then C<make test> may attempt to connect to hosts given in the
306 If true then C<Configure> will check each hostname given that it exists
312 I<$Id: //depot/libnet/Net/Config.pm#17 $>