# Copyright (c) 2000 Graham Barr <gbarr@pobox.com>. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
use vars
qw(@ISA @EXPORT %NetConfig $VERSION $CONFIGURE $LIBNET_CFG);
use Socket qw(inet_aton inet_ntoa);
@EXPORT = qw(%NetConfig);
@ISA = qw(Net::LocalCfg Exporter);
$VERSION = "1.10"; # $Id: //depot/libnet/Net/Config.pm#17 $
eval { local $SIG{__DIE__
}; require Net
::LocalCfg
};
# Try to get as much configuration info as possible from InternetConfig
$^O
eq 'MacOS' and eval <<TRY_INTERNET_CONFIG;
nntp_hosts => [ \$InternetConfig{ kICNNTPHost() } ],
pop3_hosts => [ \$InternetConfig{ kICMailAccount() } =~ /\@(.*)/ ],
smtp_hosts => [ \$InternetConfig{ kICSMTPHost() } ],
ftp_testhost => \$InternetConfig{ kICFTPHost() } ? \$InternetConfig{ kICFTPHost()} : undef,
ph_hosts => [ \$InternetConfig{ kICPhHost() } ],
ftp_ext_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
ftp_int_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
\$InternetConfig{ kICUseSocks() } ? [ \$InternetConfig{ kICSocksHost() } ] : [],
\$InternetConfig{ kICUseFTPProxy() } ? [ \$InternetConfig{ kICFTPProxyHost() } ] : [],
\@NetConfig{keys %nc} = values %nc;
$file =~ s/Config.pm/libnet.cfg/;
$ref = eval { local $SIG{__DIE__}; do $file };
if (ref($ref) eq 'HASH') {
%NetConfig = (%NetConfig, %{ $ref });
if ($< == $> and !$CONFIGURE) {
my $home = eval { local $SIG{__DIE__}; (getpwuid($>))[7] } || $ENV{HOME};
$home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH}||'') if defined $ENV{HOMEDRIVE};
$file = $home . "/.libnetrc";
$ref = eval { local $SIG{__DIE__}; do $file } if -f $file;
%NetConfig = (%NetConfig, %{ $ref })
while(($k,$v) = each %NetConfig) {
if($k =~ /_hosts$/ and $k ne "test_hosts" and defined($v) and !ref($v));
# Take a hostname and determine if it is inside the firewall
return 0 unless defined $NetConfig{'ftp_firewall'};
$host = inet_aton($host) or return -1;
$host = inet_ntoa($host);
if(exists $NetConfig{'local_netmask'}) {
my $quad = unpack("N",pack("C*",split(/\./,$host)));
my $list = $NetConfig{'local_netmask'};
$list = [$list] unless ref($list);
my($net,$bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
my $mask = ~0 << (32 - $bits);
my $addr = unpack("N",pack("C*",split(/\./,$net)));
return 0 if (($addr & $mask) == ($quad & $mask));
use vars qw(*is_external);
*is_external
= \
&requires_firewall
;
Net::Config - Local configuration data for libnet
use Net::Config qw(%NetConfig);
C<Net::Config> holds configuration data for the modules in the libnet
distribuion. During installation you will be asked for these values.
The configuration data is held globally in a file in the perl installation
tree, but a user may override any of these values by providing their own. This
can be done by having a C<.libnetrc> file in their home directory. This file
should return a reference to a HASH containing the keys described below.
nntp_hosts => [ "my_prefered_host" ],
ph_hosts => [ "my_ph_server" ],
C<Net::Config> defines the following methods. They are methods as they are
invoked as class methods. This is because C<Net::Config> inherits from
C<Net::LocalCfg> so you can override these methods if you want.
=item requires_firewall HOST
Attempts to determine if a given host is outside your firewall. Possible
-1 Cannot lookup hostname
0 Host is inside firewall (or there is no ftp_firewall entry)
1 Host is outside the firewall
This is done by using hostname lookup and the C<local_netmask> entry in
Each is a reference to an array of hostnames (in order of preference),
which should be used for the given protocol
Your internet domain name
If you have an FTP proxy firewall (B<NOT> an HTTP or SOCKS firewall)
then this value should be set to the firewall hostname. If your firewall
does not listen to port 21, then this value should be set to
C<"hostname:port"> (eg C<"hostname:99">)
There are many different ftp firewall products available. But unfortunately
there is no standard for how to traverse a firewall. The list below shows the
sequence of commands that Net::FTP will use
user Username for remote host
pass Password for remote host
fwuser Username for firewall
fwpass Password for firewall
remote.host The hostname of the remote ftp server
USER user@fwuser@remote.site
FTP servers normally work on a non-passive mode. That is when you want to
transfer data you have to tell the server the address and port to
With some firewalls this does not work as the server cannot
connect to your machine (because you are behind a firewall) and the firewall
does not re-write the command. In this case you should set C<ftp_ext_passive>
Some servers are configured to only work in passive mode. If you have
one of these you can force C<Net::FTP> to always transfer in passive
mode; when not going via a firewall, by setting C<ftp_int_passive> to
A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
These are used by the C<requires_firewall> function to determine if a given
host is inside or outside your firewall.
The following entries are used during installation & testing on the
If true then C<make test> may attempt to connect to hosts given in the
If true then C<Configure> will check each hostname given that it exists
I<$Id: //depot/libnet/Net/Config.pm#17 $>