This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade libnet from version 3.07 to 3.08
[perl5.git] / cpan / libnet / lib / Net / Config.pm
CommitLineData
686337f3
JH
1# Net::Config.pm
2#
2e173144
CBW
3# Versions up to 1.11 Copyright (c) 2000 Graham Barr <gbarr@pobox.com>.
4# All rights reserved.
5# Changes in Version 1.11_01 onwards Copyright (C) 2013-2014 Steve Hay. All
6# rights reserved.
a4f8ff46
SH
7# This module is free software; you can redistribute it and/or modify it under
8# the same terms as Perl itself, i.e. under the terms of either the GNU General
9# Public License or the Artistic License, as specified in the F<LICENCE> file.
406c51ee
JH
10
11package Net::Config;
406c51ee 12
2e173144
CBW
13use 5.008001;
14
406c51ee 15use strict;
2e173144
CBW
16use warnings;
17
18use Exporter;
19use Socket qw(inet_aton inet_ntoa);
406c51ee 20
2e173144
CBW
21our @EXPORT = qw(%NetConfig);
22our @ISA = qw(Net::LocalCfg Exporter);
bfdb5bfe 23our $VERSION = "3.08";
2e173144
CBW
24
25our($CONFIGURE, $LIBNET_CFG);
406c51ee
JH
26
27eval { local $SIG{__DIE__}; require Net::LocalCfg };
28
2e173144 29our %NetConfig = (
b3f6f6a6
RGS
30 nntp_hosts => [],
31 snpp_hosts => [],
32 pop3_hosts => [],
33 smtp_hosts => [],
34 ph_hosts => [],
35 daytime_hosts => [],
36 time_hosts => [],
37 inet_domain => undef,
38 ftp_firewall => undef,
39 ftp_ext_passive => 1,
40 ftp_int_passive => 1,
41 test_hosts => 1,
42 test_exist => 1,
406c51ee
JH
43);
44
c8570720
GB
45#
46# Try to get as much configuration info as possible from InternetConfig
47#
2e173144
CBW
48{
49## no critic (BuiltinFunctions::ProhibitStringyEval)
12df23ee 50$^O eq 'MacOS' and eval <<TRY_INTERNET_CONFIG;
c8570720
GB
51use Mac::InternetConfig;
52
53{
54my %nc = (
12df23ee
GB
55 nntp_hosts => [ \$InternetConfig{ kICNNTPHost() } ],
56 pop3_hosts => [ \$InternetConfig{ kICMailAccount() } =~ /\@(.*)/ ],
57 smtp_hosts => [ \$InternetConfig{ kICSMTPHost() } ],
58 ftp_testhost => \$InternetConfig{ kICFTPHost() } ? \$InternetConfig{ kICFTPHost()} : undef,
59 ph_hosts => [ \$InternetConfig{ kICPhHost() } ],
60 ftp_ext_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
61 ftp_int_passive => \$InternetConfig{"646F676F\xA5UsePassiveMode"} || 0,
c8570720 62 socks_hosts =>
5abafd4c 63 \$InternetConfig{ kICUseSocks() } ? [ \$InternetConfig{ kICSocksHost() } ] : [],
c8570720 64 ftp_firewall =>
5abafd4c 65 \$InternetConfig{ kICUseFTPProxy() } ? [ \$InternetConfig{ kICFTPProxyHost() } ] : [],
c8570720 66);
12df23ee 67\@NetConfig{keys %nc} = values %nc;
c8570720
GB
68}
69TRY_INTERNET_CONFIG
2e173144 70}
c8570720 71
406c51ee
JH
72my $file = __FILE__;
73my $ref;
74$file =~ s/Config.pm/libnet.cfg/;
b3f6f6a6
RGS
75if (-f $file) {
76 $ref = eval { local $SIG{__DIE__}; do $file };
77 if (ref($ref) eq 'HASH') {
78 %NetConfig = (%NetConfig, %{$ref});
79 $LIBNET_CFG = $file;
80 }
406c51ee 81}
b3f6f6a6
RGS
82if ($< == $> and !$CONFIGURE) {
83 my $home = eval { local $SIG{__DIE__}; (getpwuid($>))[7] } || $ENV{HOME};
84 $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH} || '') if defined $ENV{HOMEDRIVE};
85 if (defined $home) {
86 $file = $home . "/.libnetrc";
87 $ref = eval { local $SIG{__DIE__}; do $file } if -f $file;
88 %NetConfig = (%NetConfig, %{$ref})
89 if ref($ref) eq 'HASH';
90 }
406c51ee 91}
b3f6f6a6
RGS
92my ($k, $v);
93while (($k, $v) = each %NetConfig) {
94 $NetConfig{$k} = [$v]
95 if ($k =~ /_hosts$/ and $k ne "test_hosts" and defined($v) and !ref($v));
406c51ee
JH
96}
97
302c2e6b 98# Take a hostname and determine if it is inside the firewall
406c51ee 99
b3f6f6a6 100
406c51ee 101sub requires_firewall {
b3f6f6a6
RGS
102 shift; # ignore package
103 my $host = shift;
104
105 return 0 unless defined $NetConfig{'ftp_firewall'};
106
107 $host = inet_aton($host) or return -1;
108 $host = inet_ntoa($host);
109
110 if (exists $NetConfig{'local_netmask'}) {
111 my $quad = unpack("N", pack("C*", split(/\./, $host)));
112 my $list = $NetConfig{'local_netmask'};
113 $list = [$list] unless ref($list);
114 foreach (@$list) {
115 my ($net, $bits) = (m#^(\d+\.\d+\.\d+\.\d+)/(\d+)$#) or next;
116 my $mask = ~0 << (32 - $bits);
117 my $addr = unpack("N", pack("C*", split(/\./, $net)));
118
119 return 0 if (($addr & $mask) == ($quad & $mask));
406c51ee 120 }
b3f6f6a6
RGS
121 return 1;
122 }
406c51ee 123
b3f6f6a6 124 return 0;
406c51ee
JH
125}
126
406c51ee
JH
127*is_external = \&requires_firewall;
128
1291;
130
131__END__
132
133=head1 NAME
134
135Net::Config - Local configuration data for libnet
136
db956464 137=head1 SYNOPSIS
406c51ee
JH
138
139 use Net::Config qw(%NetConfig);
140
141=head1 DESCRIPTION
142
143C<Net::Config> holds configuration data for the modules in the libnet
3c4b39be 144distribution. During installation you will be asked for these values.
406c51ee
JH
145
146The configuration data is held globally in a file in the perl installation
ea5a7fad 147tree, but a user may override any of these values by providing their own. This
148can be done by having a C<.libnetrc> file in their home directory. This file
406c51ee
JH
149should return a reference to a HASH containing the keys described below.
150For example
151
152 # .libnetrc
153 {
3c4b39be 154 nntp_hosts => [ "my_preferred_host" ],
5abafd4c 155 ph_hosts => [ "my_ph_server" ],
406c51ee
JH
156 }
157 __END__
158
159=head1 METHODS
160
161C<Net::Config> defines the following methods. They are methods as they are
162invoked as class methods. This is because C<Net::Config> inherits from
163C<Net::LocalCfg> so you can override these methods if you want.
164
165=over 4
166
2e173144 167=item requires_firewall ( HOST )
406c51ee
JH
168
169Attempts to determine if a given host is outside your firewall. Possible
170return values are.
171
172 -1 Cannot lookup hostname
173 0 Host is inside firewall (or there is no ftp_firewall entry)
174 1 Host is outside the firewall
175
176This is done by using hostname lookup and the C<local_netmask> entry in
177the configuration data.
178
179=back
180
181=head1 NetConfig VALUES
182
183=over 4
184
185=item nntp_hosts
186
187=item snpp_hosts
188
189=item pop3_hosts
190
191=item smtp_hosts
192
193=item ph_hosts
194
195=item daytime_hosts
196
197=item time_hosts
198
199Each is a reference to an array of hostnames (in order of preference),
200which should be used for the given protocol
201
202=item inet_domain
203
204Your internet domain name
205
206=item ftp_firewall
207
d1be9408 208If you have an FTP proxy firewall (B<NOT> an HTTP or SOCKS firewall)
406c51ee
JH
209then this value should be set to the firewall hostname. If your firewall
210does not listen to port 21, then this value should be set to
211C<"hostname:port"> (eg C<"hostname:99">)
212
686337f3
JH
213=item ftp_firewall_type
214
ea5a7fad 215There are many different ftp firewall products available. But unfortunately
216there is no standard for how to traverse a firewall. The list below shows the
686337f3
JH
217sequence of commands that Net::FTP will use
218
219 user Username for remote host
220 pass Password for remote host
221 fwuser Username for firewall
222 fwpass Password for firewall
223 remote.host The hostname of the remote ftp server
224
225=over 4
226
0d0bc230 227=item 0Z<>
686337f3
JH
228
229There is no firewall
230
0d0bc230 231=item 1Z<>
686337f3
JH
232
233 USER user@remote.host
234 PASS pass
235
0d0bc230 236=item 2Z<>
686337f3
JH
237
238 USER fwuser
239 PASS fwpass
240 USER user@remote.host
241 PASS pass
242
0d0bc230 243=item 3Z<>
686337f3
JH
244
245 USER fwuser
246 PASS fwpass
247 SITE remote.site
248 USER user
249 PASS pass
250
0d0bc230 251=item 4Z<>
686337f3
JH
252
253 USER fwuser
254 PASS fwpass
255 OPEN remote.site
256 USER user
257 PASS pass
258
0d0bc230 259=item 5Z<>
686337f3
JH
260
261 USER user@fwuser@remote.site
262 PASS pass@fwpass
263
0d0bc230 264=item 6Z<>
686337f3
JH
265
266 USER fwuser@remote.site
267 PASS fwpass
268 USER user
269 PASS pass
270
0d0bc230 271=item 7Z<>
686337f3
JH
272
273 USER user@remote.host
274 PASS pass
275 AUTH fwuser
276 RESP fwpass
277
278=back
279
406c51ee
JH
280=item ftp_ext_passive
281
3c4b39be 282=item ftp_int_passive
406c51ee 283
8460ac15
GA
284FTP servers can work in passive or active mode. Active mode is when
285you want to transfer data you have to tell the server the address and
286port to connect to. Passive mode is when the server provide the
287address and port and you establish the connection.
8723f121 288
8460ac15 289With some firewalls active mode does not work as the server cannot
ea5a7fad 290connect to your machine (because you are behind a firewall) and the firewall
291does not re-write the command. In this case you should set C<ftp_ext_passive>
406c51ee
JH
292to a I<true> value.
293
294Some servers are configured to only work in passive mode. If you have
295one of these you can force C<Net::FTP> to always transfer in passive
ea5a7fad 296mode; when not going via a firewall, by setting C<ftp_int_passive> to
406c51ee
JH
297a I<true> value.
298
299=item local_netmask
300
301A reference to a list of netmask strings in the form C<"134.99.4.0/24">.
302These are used by the C<requires_firewall> function to determine if a given
303host is inside or outside your firewall.
304
305=back
306
307The following entries are used during installation & testing on the
308libnet package
309
310=over 4
311
312=item test_hosts
313
ea5a7fad 314If true then C<make test> may attempt to connect to hosts given in the
406c51ee
JH
315configuration.
316
317=item test_exists
318
ea5a7fad 319If true then C<Configure> will check each hostname given that it exists
406c51ee
JH
320
321=back
322
a4f8ff46
SH
323=head1 AUTHOR
324
325Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
326
327Steve Hay E<lt>F<shay@cpan.org>E<gt> is now maintaining libnet as of version
3281.22_02
329
330=head1 COPYRIGHT
331
332Versions up to 1.11 Copyright (c) 1998-2011 Graham Barr. All rights reserved.
333Changes in Version 1.11_01 onwards Copyright (C) 2013-2014 Steve Hay. All
334rights reserved.
335
336This module is free software; you can redistribute it and/or modify it under the
337same terms as Perl itself, i.e. under the terms of either the GNU General Public
338License or the Artistic License, as specified in the F<LICENCE> file.
339
406c51ee 340=cut