summaryrefslogtreecommitdiff
path: root/ftp/ftpmirror/files/patch-TCP.pm
blob: d8ed2c572d39e89da353a21b67f0df78b2087d53 (plain) (blame)
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
Index: Fan/TCP/TCP.pm
===================================================================
RCS file: /usr/home/ume/ncvs/src/ftpmirror/Fan/TCP/TCP.pm,v
retrieving revision 1.1.1.1
retrieving revision 1.12
diff -u -r1.1.1.1 -r1.12
--- Fan/TCP/TCP.pm	1999/10/24 10:28:15	1.1.1.1
+++ Fan/TCP/TCP.pm	2000/03/09 13:32:06	1.12
@@ -35,15 +35,16 @@
 package Fan::TCP;
 
 use strict;
-use vars qw($VERSION $LOG
-	$tcp_proto $seq_id $sent_octets $recv_octets);
+use vars qw($VERSION $LOG $seq_id $sent_octets $recv_octets);
 
 use Carp;
 use Socket;
+
+use Socket6;
+
 use AutoLoader 'AUTOLOAD';
 
 $VERSION = '0.03';
-$tcp_proto = (getprotobyname('tcp'))[2];
 
 ;#
 BEGIN {
@@ -463,27 +464,21 @@
 
 	# check local side port #.
 	my $port = $params{tcp_bindport} || $self->{tcp_bindport} || 0;
-	if ($port !~ /^\d+$/) {
-		if (!defined($port = getservbyname($port, 'tcp'))) {
-			my $e = $!.'';
-			$self->error($e, &FATAL);
-			carp("$self: getservbyname($port) - $e");
-			return undef;
-		}
-	}
 
 	# define local side address if bindaddr is not null string.
-	my $addr = inet_aton(
-		$params{tcp_bindaddr} || $self->{tcp_bindaddr} || '0.0.0.0');
-
-	# parameter for bind.
-	my $me = sockaddr_in($port, $addr);
+	my $family = $params{tcp_family} ? $params{tcp_family} : AF_INET;
+	my $tcp_bindaddr = $params{tcp_bindaddr} || $self->{tcp_bindaddr} ||
+			   (($family == AF_INET) ? '0.0.0.0' : '::');
+
+	my ($socktype, $proto, $me, $canonname);
+	($family, $socktype, $proto, $me, $canonname)
+		= getaddrinfo($tcp_bindaddr, $port, $family, SOCK_STREAM);
 
 	# local file handle...
 	local *SOCKET;
 
 	# creating a stream socket.
-	unless (socket(SOCKET, PF_INET, SOCK_STREAM, $tcp_proto)) {
+	unless (socket(SOCKET, $family, $socktype, $proto)) {
 		my $e = $!.'';
 		$self->error($e, &FATAL);
 		carp("$self: socket - $e") if $LOG >= 5;
@@ -539,41 +534,27 @@
 		return undef;
 	}
 
-	# try to parse port number
-	if ($port !~ /^\d+$/) {
-		if (!defined($port = getservbyname($port, 'tcp'))) {
-			my $e = $!.'';
-			$self->error($e, &FATAL);
-			carp("$self: getservbyname($port) - $e");
-			return undef;
-		}
-	}
-
-	# check server name
-	my @addr;
-	if ($host =~ /^(\d+)\.(\d+)\.(\d+).(\d+)$/) {
-		@addr = (pack('C4', $1, $2, $3, $4));
-	} else {
-		if ((@addr = gethostbyname($host)) < 5) {
-			carp("$self: gethostbyname - $?");
-			my $e = $?.'';
-			$self->error($e, &FATAL);
-			carp("$self: gethostbyname - $e");
-			return undef;
-		}
-		splice(@addr, 0, 4);
+	# check server name and try to parse port number
+	my @infos = getaddrinfo($host, $port, AF_UNSPEC, SOCK_STREAM);
+	if ($#infos < 1) {
+		carp("$self: getaddrinfo($host, $port) - $?");
+		my $e = $?.'';
+		$self->error($e, &FATAL);
+		carp("$self: getaddrinfo($host, $port) - $e");
+		return undef;
 	}
 
 	# Perl's bug? once connect fails, we could not any more
 	# connect (connect returns "Invalid Argument"). So we
 	# create/close a socket in each iteration.
-	for my $i (@addr) {
+	while ($#infos >= 1) {
+		my ($family, $socktype, $proto, $peer, $canonname)
+		    = splice(@infos, 0, 5);
 
 		# open socket stores any error
+		$params{tcp_family} = $family;
 		$self->open_socket(%params) || return undef;
 
-		# target address.
-		my $peer = sockaddr_in($port, $i);
 		my $result = undef;
 
 		# do real work.
@@ -758,10 +739,13 @@
 	my $sock = $self->handle;
 
 	if (defined($sock)) {
-		my($port, $a) = sockaddr_in(getsockname($sock));
-		my $addr = join('.', unpack('C4', $a));
-carp("$self sockname=$addr:$port") if $LOG > 7;
-		return wantarray ? ($port, $addr) : "$addr:$port";
+		my $sa = getsockname($sock);
+		my $family = (unpack('CC', $sa))[1];
+		my ($addr, $port) = getnameinfo($sa, NI_NUMERICHOST |
+						     NI_NUMERICSERV);
+		carp("$self sockname=$addr|$port|$family") if $LOG > 7;
+		return wantarray ? ($port, $addr, $family)
+				 : "$addr|$port|$family";
 	}
 	return wantarray ? () : undef;
 }
@@ -772,10 +756,13 @@
 	my $sock = $self->handle;
 
 	if (defined($sock)) {
-		my($port, $a) = sockaddr_in(getpeername($sock));
-		my $addr = join('.', unpack('C4', $a));
-carp("$self sockpeer=$addr:$port") if $LOG > 7;
-		return wantarray ? ($port, $addr) : "$addr:$port";
+		my $sa = getpeername($sock);
+		my $family = (unpack('CC', $sa))[1];
+		my ($addr, $port) = getnameinfo($sa, NI_NUMERICHOST |
+						     NI_NUMERICSERV);
+		carp("$self sockpeer=$addr|$port|$family") if $LOG > 7;
+		return wantarray ? ($port, $addr, $family)
+				 : "$addr|$port|$family";
 	}
 	return wantarray ? () : undef;
 }