static build
[p5sagit/p5-mst-13.2.git] / lib / Net / Config.pm
CommitLineData
406c51ee 1
2package Net::Config;
3# $Id: //depot/libnet/Net/Config.pm#6 $
4
5require Exporter;
6use vars qw(@ISA @EXPORT %NetConfig $VERSION $CONFIGURE $LIBNET_CFG);
7use Socket qw(inet_aton inet_ntoa);
8use strict;
9
10@EXPORT = qw(%NetConfig);
11@ISA = qw(Net::LocalCfg Exporter);
12$VERSION = "1.04";
13
14eval { local $SIG{__DIE__}; require Net::LocalCfg };
15
16%NetConfig = (
17 nntp_hosts => [],
18 snpp_hosts => [],
19 pop3_hosts => [],
20 smtp_hosts => [],
21 ph_hosts => [],
22 daytime_hosts => [],
23 time_hosts => [],
24 inet_domain => undef,
25 ftp_firewall => undef,
26 ftp_ext_passive => 0,
27 ftp_int_passive => 0,
28 test_hosts => 1,
29 test_exist => 1,
30);
31
32my $file = __FILE__;
33my $ref;
34$file =~ s/Config.pm/libnet.cfg/;
35if ( -f $file ) {
36 $ref = eval { do $file };
37 if (ref($ref) eq 'HASH') {
38 %NetConfig = (%NetConfig, %{ $ref });
39 $LIBNET_CFG = $file;
40 }
41}
42if ($< == $> and !$CONFIGURE) {
43 my $home = eval { (getpwuid($>))[7] } || $ENV{HOME};
44 $file = $home . "/.libnetrc";
45 $ref = eval { do $file } if -f $file;
46 %NetConfig = (%NetConfig, %{ $ref })
47 if ref($ref) eq 'HASH';
48}
49my ($k,$v);
50while(($k,$v) = each %NetConfig) {
51 $v = [ $v ]
52 if($k =~ /_hosts$/ && !ref($v));
53}
54
55# Take a hostname and determine if it is inside te firewall
56
57sub requires_firewall {
58 shift; # ignore package
59 my $host = shift;
60
61 return 0 unless defined $NetConfig{'ftp_firewall'};
62
63 $host = inet_aton($host) or return -1;
64 $host = inet_ntoa($host);
65
66 if(exists $NetConfig{'local_netmask'}) {
67 my $quad = unpack("N",pack("C*",split(/\./,$host)));
68 my $list = $NetConfig{'local_netmask'};
69 $list = [$list] unless ref($list);
70 foreach (@$list) {
71 my($net,$bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
72 my $mask = ~0 << (32 - $bits);
73 my $addr = unpack("N",pack("C*",split(/\./,$net)));
74
75 return 0 if (($addr & $mask) == ($quad & $mask));
76 }
77 return 1;
78 }
79
80 return 0;
81}
82
83use vars qw(*is_external);
84*is_external = \&requires_firewall;
85
861;
87
88__END__
89
90=head1 NAME
91
92Net::Config - Local configuration data for libnet
93
94=head1 SYNOPSYS
95
96 use Net::Config qw(%NetConfig);
97
98=head1 DESCRIPTION
99
100C<Net::Config> holds configuration data for the modules in the libnet
101distribuion. During installation you will be asked for these values.
102
103The configuration data is held globally in a file in the perl installation
104tree, but a user may override any of these values by providing thier own. This
105can be done by having a C<.libnetrc> file in thier home directory. This file
106should return a reference to a HASH containing the keys described below.
107For example
108
109 # .libnetrc
110 {
111 nntp_hosts => [ "my_prefered_host" ],
112 ph_hosts => [ "my_ph_server" ],
113 }
114 __END__
115
116=head1 METHODS
117
118C<Net::Config> defines the following methods. They are methods as they are
119invoked as class methods. This is because C<Net::Config> inherits from
120C<Net::LocalCfg> so you can override these methods if you want.
121
122=over 4
123
124=item requires_firewall HOST
125
126Attempts to determine if a given host is outside your firewall. Possible
127return values are.
128
129 -1 Cannot lookup hostname
130 0 Host is inside firewall (or there is no ftp_firewall entry)
131 1 Host is outside the firewall
132
133This is done by using hostname lookup and the C<local_netmask> entry in
134the configuration data.
135
136=back
137
138=head1 NetConfig VALUES
139
140=over 4
141
142=item nntp_hosts
143
144=item snpp_hosts
145
146=item pop3_hosts
147
148=item smtp_hosts
149
150=item ph_hosts
151
152=item daytime_hosts
153
154=item time_hosts
155
156Each is a reference to an array of hostnames (in order of preference),
157which should be used for the given protocol
158
159=item inet_domain
160
161Your internet domain name
162
163=item ftp_firewall
164
165If you have an FTP proxy firewall (B<NOT> a HTTP or SOCKS firewall)
166then this value should be set to the firewall hostname. If your firewall
167does not listen to port 21, then this value should be set to
168C<"hostname:port"> (eg C<"hostname:99">)
169
170=item ftp_ext_passive
171
172=item ftp_int_pasive
173
174FTP servers normally work on a non-passive mode. That is when you want to
175transfer data you have to tell the server the address and port to
176connect to.
177
178With some firewalls this does not work as te server cannot
179connect to your machine (because you are beind a firewall) and the firewall
180does not re-write te command. In this case you should set C<ftp_ext_passive>
181to a I<true> value.
182
183Some servers are configured to only work in passive mode. If you have
184one of these you can force C<Net::FTP> to always transfer in passive
185mode, when not going via a firewall, by cetting C<ftp_int_passive> to
186a I<true> value.
187
188=item local_netmask
189
190A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
191These are used by the C<requires_firewall> function to determine if a given
192host is inside or outside your firewall.
193
194=back
195
196The following entries are used during installation & testing on the
197libnet package
198
199=over 4
200
201=item test_hosts
202
203If true them C<make test> may attempt to connect to hosts given in the
204configuration.
205
206=item test_exists
207
208If true the C<Configure> will check each hostname given that it exists
209
210=back
211
212=cut