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
|
--- lchat.pl.orig 1998-05-29 12:05:46.000000000 -0700
+++ lchat.pl 2009-02-20 09:33:47.000000000 -0800
@@ -66,6 +66,7 @@
# Perl 5 has a special way of getting them via the 'use Socket'
# above.
$main'pf_inet = &Socket'PF_INET;
+ $main'af_inet = &Socket'AF_INET;
$main'sock_stream = &Socket'SOCK_STREAM;
local($name, $aliases, $proto) = getprotobyname( 'tcp' );
$main'tcp_proto = $proto;
@@ -74,6 +75,7 @@
# Perl 4 needs to have the socket.ph file created when perl was
# installed.
$main'pf_inet = &'PF_INET;
+ $main'af_inet = &'AF_INET;
$main'sock_stream = &'SOCK_STREAM;
local($name, $aliases, $proto) = getprotobyname( 'tcp' );
$main'tcp_proto = $proto;
@@ -85,6 +87,7 @@
# Use hardwired versions
# but who the heck would change these anyway? (:-)
$main'pf_inet = 2;
+ $main'af_inet = 2;
$main'sock_stream = 1; # Sigh... On Solaris set this to 2
$main'tcp_proto = 6;
warn "lchat.pl: using hardwired in network constantants";
@@ -93,7 +96,6 @@
# Are we using the SOCKS version of perl?
$using_socks = 0; # 0=no (default), 1=yes
-$sockaddr = 'S n a4 x8';
if( ! $on_win ){
chop( $thishost = `hostname` );
if( $thishost eq '' ){
@@ -107,6 +109,54 @@
$thishost = 'localhost';
}
+#
+# $packedsocketname = _pack_sin($portnumber, $ipaddress)
+#
+sub _pack_sin
+{
+ my $packed;
+
+ if ( $] =~ /^5\.\d+$/ ) {
+ #
+ # FreeBSD has a slightly different (from the rest of the world)
+ # sockaddr_in definition, so rely on Socket module to handle it.
+ #
+ $packed = &Socket'pack_sockaddr_in(@_[0,1]);
+ } else {
+ #
+ # Old-fashioned way to set up sockaddr_in structure. Not
+ # correct for "modern" FreeBSD (>= 5.X ?)
+ #
+ $packed = pack('S n a4 x8', $main'af_inet, 0, $thisaddr);
+ }
+ return $packed;
+}
+
+#
+# ($port, $ipaddress) = _unpack_sin($packedsocketname)
+#
+sub _unpack_sin
+{
+ my @PA;
+
+ if ( $] =~ /^5\.\d+$/ ) {
+ #
+ # FreeBSD has a slightly different (from the rest of the world)
+ # sockaddr_in definition, so rely on Socket module to handle it.
+ # Docs say "Will croak if the structure does not have AF_INET in
+ # the right place."
+ #
+ @PA = &Socket'unpack_sockaddr_in($_[0]);
+ } else {
+ #
+ # Old-fashioned way to parse sockaddr_in structure. Not
+ # correct for "modern" FreeBSD (>= 5.X ?)
+ #
+ (undef, @PA) = unpack('S n a4 x8', $_[0]);
+ }
+ return @PA;
+}
+
## &chat'open_port("server.address",$port_number);
## opens a named or numbered TCP server
@@ -117,7 +167,7 @@
# We may be multi-homed, start with 0, fixup once connexion is made
$thisaddr = "\0\0\0\0" ;
- $thisproc = pack($sockaddr, 2, 0, $thisaddr);
+ $thisproc = &_pack_sin(0, $thisaddr);
if ($server =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/) {
$serveraddr = pack('C4', $1, $2, $3, $4);
@@ -128,7 +178,7 @@
}
$serveraddr = $x[4];
}
- $serverproc = pack($sockaddr, 2, $port, $serveraddr);
+ $serverproc = &_pack_sin($port, $serveraddr);
unless (socket(S, $main'pf_inet, $main'sock_stream, $main'tcp_proto)) {
($!) = ($!, close(S)); # close S while saving $!
return undef;
@@ -150,9 +200,9 @@
# We opened with the local address set to ANY, at this stage we know
# which interface we are using. This is critical if our machine is
# multi-homed, with IP forwarding off, so fix-up.
- local($fam,$lport);
- ($fam,$lport,$thisaddr) = unpack($sockaddr, getsockname(S));
- $thisproc = pack($sockaddr, 2, 0, $thisaddr);
+ local($lport);
+ ($lport,$thisaddr) = &_unpack_sin(getsockname(S));
+ $thisproc = &_pack_sin(0, $thisaddr);
# end of post-connect fixup
select((select(S), $| = 1)[0]);
return 1;
@@ -161,13 +211,13 @@
# Similar to open_port, but does less. Used for PASV code with ftp.pl
# -Erez Zadok.
sub open_newport { ## public
- local($server, $port, $newsock) = @_;
+ local($server, $port, *newsock) = @_;
local($serveraddr,$serverproc);
# We may be multi-homed, start with 0, fixup once connexion is made
$thisaddr = "\0\0\0\0" ;
- $thisproc = pack($sockaddr, 2, 0, $thisaddr);
+ $thisproc = &_pack_sin(0, $thisaddr);
if ($server =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/) {
$serveraddr = pack('C4', $1, $2, $3, $4);
@@ -178,20 +228,20 @@
}
$serveraddr = $x[4];
}
- $serverproc = pack($sockaddr, 2, $port, $serveraddr);
+ $serverproc = &_pack_sin($port, $serveraddr);
- unless (connect($newsock, $serverproc)) {
- ($!) = ($!, close($newsock)); # close newsock while saving $!
+ unless (connect(newsock, $serverproc)) {
+ ($!) = ($!, close(newsock)); # close newsock while saving $!
return undef;
}
# We opened with the local address set to ANY, at this stage we know
# which interface we are using. This is critical if our machine is
# multi-homed, with IP forwarding off, so fix-up.
- local($fam,$lport);
- ($fam,$lport,$thisaddr) = unpack($sockaddr, getsockname($newsock));
- $thisproc = pack($sockaddr, 2, 0, $thisaddr);
+ local($lport);
+ ($lport,$thisaddr) = &_unpack_sin(getsockname(newsock));
+ $thisproc = &_pack_sin(0, $thisaddr);
# end of post-connect fixup
- select((select($newsock), $| = 1)[0]);
+ select((select(newsock), $| = 1)[0]);
return 1;
}
##############################################################################
|