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.08"; # $Id: //depot/libnet/Net/Config.pm#13 $
18 eval { local $SIG{__DIE__}; require Net::LocalCfg };
29 ftp_firewall => undef,
38 $file =~ s/Config.pm/libnet.cfg/;
40 $ref = eval { local $SIG{__DIE__}; do $file };
41 if (ref($ref) eq 'HASH') {
42 %NetConfig = (%NetConfig, %{ $ref });
46 if ($< == $> and !$CONFIGURE) {
47 my $home = eval { local $SIG{__DIE__}; (getpwuid($>))[7] } || $ENV{HOME};
48 $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH}||'') if defined $ENV{HOMEDRIVE};
50 $file = $home . "/.libnetrc";
51 $ref = eval { local $SIG{__DIE__}; do $file } if -f $file;
52 %NetConfig = (%NetConfig, %{ $ref })
53 if ref($ref) eq 'HASH';
57 while(($k,$v) = each %NetConfig) {
58 $NetConfig{$k} = [ $v ]
59 if($k =~ /_hosts$/ && !ref($v));
62 # Take a hostname and determine if it is inside the firewall
64 sub requires_firewall {
65 shift; # ignore package
68 return 0 unless defined $NetConfig{'ftp_firewall'};
70 $host = inet_aton($host) or return -1;
71 $host = inet_ntoa($host);
73 if(exists $NetConfig{'local_netmask'}) {
74 my $quad = unpack("N",pack("C*",split(/\./,$host)));
75 my $list = $NetConfig{'local_netmask'};
76 $list = [$list] unless ref($list);
78 my($net,$bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
79 my $mask = ~0 << (32 - $bits);
80 my $addr = unpack("N",pack("C*",split(/\./,$net)));
82 return 0 if (($addr & $mask) == ($quad & $mask));
90 use vars qw(*is_external);
91 *is_external = \&requires_firewall;
99 Net::Config - Local configuration data for libnet
103 use Net::Config qw(%NetConfig);
107 C<Net::Config> holds configuration data for the modules in the libnet
108 distribuion. During installation you will be asked for these values.
110 The configuration data is held globally in a file in the perl installation
111 tree, but a user may override any of these values by providing their own. This
112 can be done by having a C<.libnetrc> file in their home directory. This file
113 should return a reference to a HASH containing the keys described below.
118 nntp_hosts => [ "my_prefered_host" ],
119 ph_hosts => [ "my_ph_server" ],
125 C<Net::Config> defines the following methods. They are methods as they are
126 invoked as class methods. This is because C<Net::Config> inherits from
127 C<Net::LocalCfg> so you can override these methods if you want.
131 =item requires_firewall HOST
133 Attempts to determine if a given host is outside your firewall. Possible
136 -1 Cannot lookup hostname
137 0 Host is inside firewall (or there is no ftp_firewall entry)
138 1 Host is outside the firewall
140 This is done by using hostname lookup and the C<local_netmask> entry in
141 the configuration data.
145 =head1 NetConfig VALUES
163 Each is a reference to an array of hostnames (in order of preference),
164 which should be used for the given protocol
168 Your internet domain name
172 If you have an FTP proxy firewall (B<NOT> a HTTP or SOCKS firewall)
173 then this value should be set to the firewall hostname. If your firewall
174 does not listen to port 21, then this value should be set to
175 C<"hostname:port"> (eg C<"hostname:99">)
177 =item ftp_firewall_type
179 There are many different ftp firewall products available. But unfortunately
180 there is no standard for how to traverse a firewall. The list below shows the
181 sequence of commands that Net::FTP will use
183 user Username for remote host
184 pass Password for remote host
185 fwuser Username for firewall
186 fwpass Password for firewall
187 remote.host The hostname of the remote ftp server
197 USER user@remote.host
204 USER user@remote.host
225 USER user@fwuser@remote.site
230 USER fwuser@remote.site
237 USER user@remote.host
244 =item ftp_ext_passive
248 FTP servers normally work on a non-passive mode. That is when you want to
249 transfer data you have to tell the server the address and port to
252 With some firewalls this does not work as the server cannot
253 connect to your machine (because you are behind a firewall) and the firewall
254 does not re-write the command. In this case you should set C<ftp_ext_passive>
257 Some servers are configured to only work in passive mode. If you have
258 one of these you can force C<Net::FTP> to always transfer in passive
259 mode; when not going via a firewall, by setting C<ftp_int_passive> to
264 A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
265 These are used by the C<requires_firewall> function to determine if a given
266 host is inside or outside your firewall.
270 The following entries are used during installation & testing on the
277 If true then C<make test> may attempt to connect to hosts given in the
282 If true then C<Configure> will check each hostname given that it exists
288 I<$Id: //depot/libnet/Net/Config.pm#13 $>