line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Hotline::Shared; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
## Copyright(c) 1998-2002 by John C. Siracusa. All rights reserved. This |
4
|
|
|
|
|
|
|
## program is free software; you can redistribute it and/or modify it under |
5
|
|
|
|
|
|
|
## the same terms as Perl itself. |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
55
|
|
10
|
1
|
|
|
1
|
|
5
|
use IO::Handle; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
38
|
|
11
|
1
|
|
|
1
|
|
5
|
use POSIX qw(F_GETFL F_SETFL O_NONBLOCK EINTR EWOULDBLOCK EAGAIN); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
78
|
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
858
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
require Exporter; |
16
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
17
|
|
|
|
|
|
|
@EXPORT_OK = qw(_encode _write _read _hexdump _debug _set_blocking); |
18
|
|
|
|
|
|
|
%EXPORT_TAGS = (all => \@EXPORT_OK); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
$VERSION = '0.80'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _debug |
23
|
|
|
|
|
|
|
{ |
24
|
0
|
0
|
|
0
|
|
|
if($Net::Hotline::Client::DEBUG) |
25
|
|
|
|
|
|
|
{ |
26
|
0
|
|
|
|
|
|
print STDERR join('', @_); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _encode |
31
|
|
|
|
|
|
|
{ |
32
|
0
|
|
|
0
|
|
|
my($data) = join('', @_); |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my($i, $len, $enc); |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$len = length($data); |
37
|
0
|
|
|
|
|
|
$enc = ''; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
for($i = 0; $i < $len; $i++) |
40
|
|
|
|
|
|
|
{ |
41
|
0
|
|
|
|
|
|
$enc .= pack("C", (255 - unpack("C", substr($data, $i, 1)))); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
return $enc; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _write |
48
|
|
|
|
|
|
|
{ |
49
|
0
|
|
|
0
|
|
|
my($fh, $data_ref, $length) = @_; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
my($written, $offset); |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
$offset = 0; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
while($length > 0) # Handle partial writes |
56
|
|
|
|
|
|
|
{ |
57
|
0
|
|
|
|
|
|
$written = syswrite($fh, $$data_ref, $length, $offset); |
58
|
0
|
0
|
|
|
|
|
next if($! == EINTR); |
59
|
0
|
0
|
|
|
|
|
unless(defined($written)) |
60
|
|
|
|
|
|
|
{ |
61
|
0
|
0
|
0
|
|
|
|
next if($! == EWOULDBLOCK || $! == EAGAIN); |
62
|
0
|
|
|
|
|
|
croak("System write error(", $! + 0, "): $!\n"); |
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
|
|
|
|
$length -= $written; |
65
|
0
|
|
|
|
|
|
$offset += $written; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
return $offset; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _read |
72
|
|
|
|
|
|
|
{ |
73
|
0
|
|
|
0
|
|
|
my($fh, $data_ref, $length, $blocking) = @_; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my($offset) = 0; |
76
|
0
|
|
|
|
|
|
my($read) = 0; |
77
|
|
|
|
|
|
|
|
78
|
0
|
0
|
|
|
|
|
$blocking = 1 unless(defined($blocking)); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
#_debug("Reading $length..."); |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
while($length > 0) # Handle partial reads |
83
|
|
|
|
|
|
|
{ |
84
|
0
|
|
|
|
|
|
$read = sysread($fh, $$data_ref, $length, $offset); |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
unless(defined($read)) |
87
|
|
|
|
|
|
|
{ |
88
|
0
|
0
|
|
|
|
|
next if($! == EINTR); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Once we read a little bit, we keep readinuntil we get it all |
91
|
|
|
|
|
|
|
# Otherwise, we can return undef and treat it as a WOULDBLOCK |
92
|
0
|
0
|
0
|
|
|
|
if($blocking || $offset > 0) { next } |
|
0
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
else { return } |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
$offset += $read; |
97
|
0
|
|
|
|
|
|
$length -= $read; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
#_debug("read $offset ($length)\n"); |
101
|
0
|
|
|
|
|
|
return($offset); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _set_blocking |
105
|
|
|
|
|
|
|
{ |
106
|
0
|
|
|
0
|
|
|
my($fh, $blocking) = @_; |
107
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
if($IO::VERSION >= 1.19) # The easy way, with the IO module |
109
|
|
|
|
|
|
|
{ |
110
|
0
|
|
|
|
|
|
$fh->blocking($blocking); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else # The hard way...not 100% successful :-/ |
113
|
|
|
|
|
|
|
{ |
114
|
0
|
|
|
|
|
|
my($flags) = fcntl($fh, F_GETFL, 0); |
115
|
|
|
|
|
|
|
|
116
|
0
|
0
|
|
|
|
|
defined($flags) || croak "Can't get flags for socket: $!\n"; |
117
|
|
|
|
|
|
|
|
118
|
0
|
0
|
|
|
|
|
if($blocking) |
119
|
|
|
|
|
|
|
{ |
120
|
0
|
0
|
|
|
|
|
fcntl($fh, F_SETFL, $flags & ~O_NONBLOCK) || |
121
|
|
|
|
|
|
|
croak "Can't make socket blocking: $!\n"; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
else |
124
|
|
|
|
|
|
|
{ |
125
|
0
|
0
|
|
|
|
|
fcntl($fh, F_SETFL, $flags | O_NONBLOCK) || |
126
|
|
|
|
|
|
|
croak "Can't make socket nonblocking: $!\n"; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _hexdump |
132
|
|
|
|
|
|
|
{ |
133
|
0
|
|
|
0
|
|
|
my($data) = join('', @_); |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
my($ret, $hex, $ascii, $len, $i); |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
$len = length($data); |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
for($i = 0; $i < $len; $i++) |
140
|
|
|
|
|
|
|
{ |
141
|
0
|
0
|
|
|
|
|
if($i > 0) |
142
|
|
|
|
|
|
|
{ |
143
|
0
|
0
|
|
|
|
|
if($i % 4 == 0) |
144
|
|
|
|
|
|
|
{ |
145
|
0
|
|
|
|
|
|
$hex .= ' '; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
if($i % 16 == 0) |
149
|
|
|
|
|
|
|
{ |
150
|
0
|
|
|
|
|
|
$ret .= "$hex$ascii\n"; |
151
|
0
|
|
|
|
|
|
$ascii = $hex = ''; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
$hex .= sprintf("%02x ", ord(substr($data, $i, 1))); |
156
|
|
|
|
|
|
|
|
157
|
0
|
0
|
0
|
|
|
|
$ascii .= sprintf("%c", (ord(substr($data, $i, 1)) > 31 and |
158
|
|
|
|
|
|
|
ord(substr($data, $i, 1)) < 127) ? |
159
|
|
|
|
|
|
|
ord(substr($data, $i, 1)) : 46); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
0
|
0
|
|
|
|
|
if(length($hex) < 50) |
163
|
|
|
|
|
|
|
{ |
164
|
0
|
|
|
|
|
|
$hex .= ' ' x (50 - length($hex)); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
$ret .= "$hex $ascii\n"; |
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
return $ret; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
1; |