1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
#-*- perl -*-
#
# Copyright (C) 2001 Ken'ichi Fukamachi
# All rights reserved. This program is free software; you can
# redistribute it and/or modify it under the same terms as Perl itself.
#
# $Id$
# $FML$
#
package MailingList::Net::INET6;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK);
use Carp;
use MailingList::Utils;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(is_ipv6_ready is_ipv6_mta_syntax connect6);
sub _we_can_use_Socket6
{
my ($self, $args) = @_;
eval q{
use Socket;
use Socket6;
};
if ($@ =~ /Can\'t locate Socket6.pm/) {
$self->{_ipv6_ready} = 'no';
}
else {
Log("IPv6 ready");
$self->{_ipv6_ready} = 'yes';
}
}
sub is_ipv6_ready
{
my ($self, $args) = @_;
# probe the IPv6 availability for the first time
unless ($self->{_ipv6_ready}) {
_we_can_use_Socket6($self, $args);
};
$self->{_ipv6_ready} eq 'yes' ? 1 : 0;
}
sub is_ipv6_mta_syntax
{
my ($self, $host) = @_;
my ($x_host, $x_port);
# check the mta syntax whether it is ipv6 form or not.
if ( $host =~ /\[([\d:]+)\]:(\d+)/) {
($x_host, $x_port) = ($1, $2);
return ($x_host, $x_port);
}
else {
return wantarray ? () : undef;
}
}
sub connect6
{
my ($self, $args) = @_;
my $mta = $args->{ _mta };
# check the mta syntax is $ipv6_addr:$port or not.
my ($host, $port) = $self->is_ipv6_mta_syntax( $args->{ _mta } );
# if mta is ipv6 raw address syntax,
# try to parse $mta to $host:$port style.
unless ($host) {
if ($mta =~ /(\S+):(\S+)/) {
($host, $port) = ($1, $2);
}
}
# hmm, invalid MTA
unless ($host && $port) {
Log("connect6: cannot find mta=$mta");
$self->{_socket} = undef;
return undef;
}
$self->{_socket} = undef;
return undef;
eval q{
use IO::Handle;
use Socket;
use Socket6;
my ($family, $type, $proto, $saddr, $canonname);
my $fh = new IO::Socket;
my $inet6_family = &AF_INET6;
# resolve socket info by getaddrinfo()
my @res = getaddrinfo($host, $port, AF_UNSPEC, SOCK_STREAM);
$family = -1;
LOOP:
while (scalar(@res) >= 5) {
($family, $type, $proto, $saddr, $canonname, @res) = @res;
my ($host, $port) =
getnameinfo($saddr, NI_NUMERICHOST | NI_NUMERICSERV);
# check only IPv6 case here.
next LOOP if $family != $inet6_family;
socket($fh, $family, $type, $proto) || do {
Log("Error: cannot create IPv6 socket");
next LOOP;
};
if (connect($fh, $saddr)) {
Log("(debug6) o.k. connect $host");
last LOOP;
}
else {
Log("Error: cannot connect via IPv6");
}
$family = -1;
}
if ($family != -1) {
$self->{_socket} = $fh;
Log("connected to $host:$port by IPv6");
} else {
$self->{_socket} = undef;
Log("(debug6) fail to connect $host:$port by IPv6");
}
};
}
=head1 NAME
MailingList::Net::INET6 - establish tcp connection over IPv6
=head1 SYNOPSIS
if ($self->is_ipv6_ready($args)) {
$self->connect6($args);
}
=head1 DESCRIPTION
This module tries to create a socket and establish a tcp connection
over IPv6. It is used within C<MailingList::SMTP> module.
=head1 METHODS
=item C<is_ipv6_ready()>
It checks whether your environment has Socket6.pm or not?
If Socket6 module exists, we assume your operating system is IPv6 ready!
=item C<connect6()>
try L<connect(2)>.
If it succeeds, returned
$self->{ _socket } has true value.
If not,
$self->{ _socket } is undef.
Avaialble arguments follows:
connect6( { _mta => $mta });
$mta is a hostname or [raw_ipv6_addr]:port form, for example,
[::1]:25.
=head1 SEE ALSO
L<MailingList::SMTP>,
L<Socket6>,
L<Socket>,
L<IO::Handle>,
L<IO::Socket>,
L<MailingList::Utils>
=head1 AUTHOR
Ken'ichi Fukamachi
=head1 COPYRIGHT
Copyright (C) 2001 Ken'ichi Fukamachi
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 HISTORY
MailingList::Net::INET6 appeared in fml5 mailing list driver package.
See C<http://www.fml.org/> for more details.
=cut
1;
|