line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Net::NBsocket; |
4
|
16
|
|
|
16
|
|
28474
|
use strict; |
|
16
|
|
|
|
|
33
|
|
|
16
|
|
|
|
|
612
|
|
5
|
|
|
|
|
|
|
#use diagnostics; |
6
|
|
|
|
|
|
|
|
7
|
16
|
|
|
|
|
1538
|
use vars qw( |
8
|
|
|
|
|
|
|
$VERSION @ISA @EXPORT_OK $UDP $TCP |
9
|
16
|
|
|
16
|
|
76
|
); |
|
16
|
|
|
|
|
31
|
|
10
|
16
|
|
|
16
|
|
14376
|
use POSIX; |
|
16
|
|
|
|
|
133361
|
|
|
16
|
|
|
|
|
124
|
|
11
|
16
|
|
|
|
|
140
|
use NetAddr::IP::InetBase qw( |
12
|
|
|
|
|
|
|
ipv6_aton |
13
|
|
|
|
|
|
|
ipv6_n2x |
14
|
|
|
|
|
|
|
ipv6_n2d |
15
|
|
|
|
|
|
|
inet_pton |
16
|
|
|
|
|
|
|
inet_ntop |
17
|
|
|
|
|
|
|
AF_INET6 |
18
|
16
|
|
|
16
|
|
102453
|
); |
|
16
|
|
|
|
|
180217
|
|
19
|
16
|
|
|
16
|
|
3543
|
use AutoLoader 'AUTOLOAD'; |
|
16
|
|
|
|
|
31
|
|
|
16
|
|
|
|
|
172
|
|
20
|
|
|
|
|
|
|
require Socket; |
21
|
|
|
|
|
|
|
import Socket qw( |
22
|
|
|
|
|
|
|
sockaddr_in |
23
|
|
|
|
|
|
|
sockaddr_un |
24
|
|
|
|
|
|
|
inet_aton |
25
|
|
|
|
|
|
|
inet_ntoa |
26
|
|
|
|
|
|
|
INADDR_ANY |
27
|
|
|
|
|
|
|
INADDR_BROADCAST |
28
|
|
|
|
|
|
|
INADDR_LOOPBACK |
29
|
|
|
|
|
|
|
INADDR_NONE |
30
|
|
|
|
|
|
|
AF_INET |
31
|
|
|
|
|
|
|
AF_UNIX |
32
|
|
|
|
|
|
|
SOCK_STREAM |
33
|
|
|
|
|
|
|
SOCK_DGRAM |
34
|
|
|
|
|
|
|
SO_LINGER |
35
|
|
|
|
|
|
|
SO_REUSEADDR |
36
|
|
|
|
|
|
|
SOL_SOCKET |
37
|
|
|
|
|
|
|
SOMAXCONN |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
require Exporter; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$VERSION = do { my @r = (q$Revision: 0.24 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# dynamic configuration for this HOST |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# used a lot, create once per session |
49
|
|
|
|
|
|
|
# SEGFAULTS with Apache mod_perl |
50
|
|
|
|
|
|
|
#$UDP = getprotobyname('udp'); |
51
|
|
|
|
|
|
|
#$TCP = getprotobyname('tcp'); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$UDP = 17; |
54
|
|
|
|
|
|
|
$TCP = 6; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $havesock6 = 0; |
57
|
|
|
|
|
|
|
my $isupport6 = 0; |
58
|
|
|
|
|
|
|
my $in6addr_any = ipv6_aton('::0'); |
59
|
|
|
|
|
|
|
my $in6addr_loopback = ipv6_aton('::1'); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
if (eval { require Socket6 } ) { # have Socket6? |
62
|
|
|
|
|
|
|
$havesock6 = 1; |
63
|
|
|
|
|
|
|
import Socket6 qw( |
64
|
|
|
|
|
|
|
pack_sockaddr_in6 |
65
|
|
|
|
|
|
|
unpack_sockaddr_in6 |
66
|
|
|
|
|
|
|
in6addr_any |
67
|
|
|
|
|
|
|
in6addr_loopback |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $sock = do { local *SOCK; }; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
if (socket($sock,AF_INET6(),SOCK_STREAM(),$TCP)) { |
73
|
|
|
|
|
|
|
$isupport6 = 1; |
74
|
|
|
|
|
|
|
close $sock; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
|
|
|
|
|
|
*pack_sockaddr_in6 = \&_dummy; |
79
|
|
|
|
|
|
|
*unpack_sockaddr_in6 = \&_dummy; |
80
|
0
|
|
|
0
|
|
0
|
*in6addr_any = sub { return $in6addr_any }; |
81
|
0
|
|
|
0
|
|
0
|
*in6addr_loopback = sub { return $in6addr_loopback}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# get the index into sockaddr_in for the AF family byte |
85
|
|
|
|
|
|
|
# |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# return an index of '0' or '1' for the AF family byte |
88
|
|
|
|
|
|
|
# '2' in the first rv indicates a not found |
89
|
|
|
|
|
|
|
# true in the second rv indicates "don't know" |
90
|
|
|
|
|
|
|
# neither of these conditions should occur |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub findAFi { |
93
|
16
|
|
|
16
|
0
|
191
|
my($af,$pa) = @_; |
94
|
16
|
|
|
|
|
323
|
my @pa = unpack('C2',$pa); |
95
|
16
|
|
|
|
|
26
|
my $pai; |
96
|
16
|
|
|
|
|
89
|
for ($pai = 0; $pai < 2; $pai++) { |
97
|
16
|
50
|
|
|
|
70
|
last if $pa[$pai] == $af; |
98
|
|
|
|
|
|
|
} |
99
|
16
|
|
|
|
|
29
|
my $unsure = 0; |
100
|
16
|
50
|
|
|
|
69
|
unless ($pai) { # if $pai is zero |
101
|
16
|
50
|
|
|
|
56
|
$unsure = 1 if $pa[0] == $pa[1]; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
return wantarray |
104
|
16
|
50
|
|
|
|
138
|
? ($pai,$unsure) |
105
|
|
|
|
|
|
|
: $pai; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
my($af_index,$unsure) = findAFi(AF_INET(), |
109
|
|
|
|
|
|
|
scalar sockaddr_in(1,INADDR_LOOPBACK())); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _af_index { |
112
|
5
|
|
|
5
|
|
155
|
return $af_index; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
116
|
|
|
|
|
|
|
open_UDP |
117
|
|
|
|
|
|
|
open_udpNB |
118
|
|
|
|
|
|
|
open_Listen |
119
|
|
|
|
|
|
|
open_listenNB |
120
|
|
|
|
|
|
|
connectBlk |
121
|
|
|
|
|
|
|
connect_NB |
122
|
|
|
|
|
|
|
accept_Blk |
123
|
|
|
|
|
|
|
accept_NB |
124
|
|
|
|
|
|
|
set_NB |
125
|
|
|
|
|
|
|
set_so_linger |
126
|
|
|
|
|
|
|
dyn_bind |
127
|
|
|
|
|
|
|
inet_aton |
128
|
|
|
|
|
|
|
inet_ntoa |
129
|
|
|
|
|
|
|
sockaddr_in |
130
|
|
|
|
|
|
|
sockaddr_un |
131
|
|
|
|
|
|
|
inet_pton |
132
|
|
|
|
|
|
|
inet_ntop |
133
|
|
|
|
|
|
|
ipv6_aton |
134
|
|
|
|
|
|
|
ipv6_n2x |
135
|
|
|
|
|
|
|
ipv6_n2d |
136
|
|
|
|
|
|
|
INADDR_ANY |
137
|
|
|
|
|
|
|
INADDR_BROADCAST |
138
|
|
|
|
|
|
|
INADDR_LOOPBACK |
139
|
|
|
|
|
|
|
INADDR_NONE |
140
|
|
|
|
|
|
|
in6addr_any |
141
|
|
|
|
|
|
|
in6addr_loopback |
142
|
|
|
|
|
|
|
AF_INET |
143
|
|
|
|
|
|
|
AF_INET6 |
144
|
|
|
|
|
|
|
havesock6 |
145
|
|
|
|
|
|
|
isupport6 |
146
|
|
|
|
|
|
|
pack_sockaddr_in6 |
147
|
|
|
|
|
|
|
unpack_sockaddr_in6 |
148
|
|
|
|
|
|
|
); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub TCP { |
151
|
9
|
|
|
9
|
0
|
549
|
return $TCP; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub UDP { |
155
|
5
|
|
|
5
|
0
|
536
|
return $UDP; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub _dummy { |
159
|
|
|
|
|
|
|
return wantarray |
160
|
|
|
|
|
|
|
? () |
161
|
2
|
100
|
|
2
|
|
921
|
: undef; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub havesock6 { |
165
|
7
|
|
|
7
|
1
|
1766
|
return $havesock6; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub isupport6 { |
169
|
2
|
|
|
2
|
1
|
540
|
return $isupport6; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub open_listenNB { |
173
|
5
|
|
|
5
|
1
|
31357
|
my $lsock = &open_Listen; |
174
|
5
|
50
|
33
|
|
|
112
|
return $lsock if $lsock && set_NB($lsock); |
175
|
0
|
0
|
|
|
|
0
|
close $lsock if $lsock; |
176
|
0
|
|
|
|
|
0
|
return undef; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub connectBlk { |
180
|
0
|
|
|
0
|
1
|
0
|
unshift @_,1; |
181
|
0
|
|
|
|
|
0
|
&_connect; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub connect_NB { |
185
|
5
|
|
|
5
|
1
|
1275
|
unshift @_,0; |
186
|
5
|
|
|
|
|
71
|
&_connect; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub accept_NB { |
190
|
9
|
|
|
9
|
1
|
4444673
|
unshift @_,0; |
191
|
9
|
|
|
|
|
314
|
my @rv = &_accept; |
192
|
9
|
100
|
|
|
|
65
|
return @rv if wantarray; |
193
|
3
|
100
|
|
|
|
70
|
return $rv[0] if @rv; |
194
|
2
|
|
|
|
|
7
|
return undef; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub accept_Blk { |
198
|
0
|
|
|
0
|
1
|
0
|
unshift @_,1; |
199
|
0
|
|
|
|
|
0
|
my @rv= &_accept; |
200
|
0
|
0
|
|
|
|
0
|
return @rv if wantarray; |
201
|
0
|
0
|
|
|
|
0
|
return $rv[0] if @rv; |
202
|
0
|
|
|
|
|
0
|
return undef; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub set_so_linger { |
206
|
0
|
|
|
0
|
1
|
0
|
my ($FH,$sec) = @_; |
207
|
0
|
|
|
|
|
0
|
setsockopt($FH,SOL_SOCKET(),SO_LINGER(),pack("ll",1,$sec)); |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
0
|
|
|
0
|
|
0
|
sub DESTROY {}; |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
1; |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
__END__ |