SSLGlue/Socket.pm untested

filter: Statementstmt 0.0% 0 / 143 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 239 SCAR 48.8
CC
11
Cov
0%
new
20.0
start_SSL
20.0
BEGIN
6.0
CRAP 132.0
1  
2 package Net::SSLGlue::Socket;
3 our $VERSION = 1.002;
4  
5 use strict;
6 use warnings;
7 use Carp 'croak';
8 use Symbol 'gensym';
9 use IO::Socket::SSL;
10 my $IPCLASS;
11 BEGIN {
12 for(qw(IO::Socket::IP IO::Socket::INET6 IO::Socket::INET)) {
13 $IPCLASS = $_,last if eval "require $_";
14 }
15 }
16  
17 # this can be overwritten (with local) to get arguments passed around
18 # to strict calls of the socket class new
19 our %ARGS;
20  
21 sub new {
22 my $class = shift;
23 my %args = @_>1 ? @_ : ( PeerAddr => shift() );
24 %args = ( %args, %ARGS );
25  
26 my %sslargs;
27 for(keys %args) {
28 $sslargs{$_} = delete $args{$_} if m{^SSL_};
29 }
30  
31 my $ssl = delete $args{SSL};
32 my $sock = $ssl
33 ? IO::Socket::SSL->new(%args,%sslargs)
34 : $IPCLASS->new(%args)
35 or return;
36  
37 my $self = gensym();
38 bless $self,$class;
39 ${*$self}{sock} = $sock;
40 ${*$self}{ssl} = $ssl;
41 ${*$self}{sslargs} = \%sslargs;
42 tie *{$self}, "Net::SSLGlue::Socket::HANDLE", $self;
43  
44 return $self;
45 }
46  
47 sub DESTROY {
48 my $self = shift;
49 %{*$self} = ();
50 }
51  
52 for my $sub (qw(
53 fileno sysread syswrite close connect fcntl
54 read write readline print printf getc say eof getline getlines
55 blocking autoflush timeout
56 sockhost sockport peerhost peerport sockdomain
57 truncate stat setbuf setvbuf fdopen ungetc send recv
58 )) {
59 no strict 'refs';
60 *$sub = sub {
61 my $self = shift;
62 my $sock = ${*$self}{sock} or return;
63 my $sock_sub = $sock->can($sub) or croak("$sock does not support $sub");
64 unshift @_,$sock;
65 # warn "*** $sub called";
66 goto &$sock_sub;
67 };
68 }
69  
70 sub accept {
71 my ($self,$class) = @_;
72 my $sock = ${*$self}{sock} or return;
73 my $conn = $sock->accept();
74  
75 return bless $conn,$class
76 if $class && ! $class->isa('Net::SSLGlue::Socket');
77  
78 $class ||= ref($self);
79 my $wrap = gensym;
80 *$wrap = *$conn; # clone original handle
81 bless $wrap, $class;
82 ${*$wrap}{sock} = $conn;
83 ${*$wrap}{ssl} = ${*$self}{ssl};
84 ${*$wrap}{sslargs} = ${*$self}{sslargs};
85 return $wrap;
86 };
87  
88 sub start_SSL {
89 my ($self,%args) = @_;
90 croak("start_SSL called on SSL socket") if ${*$self}{ssl};
91  
92 %args = (%{${*$self}{sslargs}},%args);
93 if (my $ctx = $args{SSL_reuse_ctx}) {
94 # take the context from the attached socket
95 $args{SSL_reuse_ctx} = ${*$ctx}{sock}
96 if $ctx->isa('Net::SSLGlue::Socket');
97 }
98 IO::Socket::SSL->start_SSL(${*$self}{sock},%args) or return;
99 ${*$self}{ssl} = 1;
100 return $self;
101 }
102  
103 sub stop_SSL {
104 my $self = shift;
105 croak("stop_SSL called on plain socket") if ! ${*$self}{ssl};
106 ${*$self}{sock}->stop_SSL(@_) or return;
107 ${*$self}{ssl} = 0;
108 return $self;
109 }
110  
111 sub can_read {
112 my ($self,$timeout) = @_;
113 return 1 if ${*$self}{ssl} && ${*$self}{sock}->pending;
114 vec( my $vec,fileno(${*$self}{sock}),1) = 1;
115 return select($vec,undef,undef,$timeout);
116 }
117  
118 sub peer_certificate {
119 my $self = shift;
120 return ${*$self}{ssl} && ${*$self}{sock}->peer_certificate(@_);
121 }
122  
123 sub is_ssl {
124 my $self = shift;
125 return ${*$self}{ssl} && ${*$self}{sock};
126 }
127  
128 package Net::SSLGlue::Socket::HANDLE;
129 use strict;
130 use Errno 'EBADF';
131 use Scalar::Util 'weaken';
132  
133 sub TIEHANDLE {
134 my ($class, $handle) = @_;
135 weaken($handle);
136 bless \$handle, $class;
137 }
138  
139 sub READ { ${shift()}->sysread(@_) }
140 sub READLINE { ${shift()}->readline(@_) }
141 sub GETC { ${shift()}->getc(@_) }
142 sub PRINT { ${shift()}->print(@_) }
143 sub PRINTF { ${shift()}->printf(@_) }
144 sub WRITE { ${shift()}->syswrite(@_) }
145 sub FILENO { ${shift()}->fileno(@_) }
146 sub TELL { $! = EBADF; return -1 }
147 sub BINMODE { return 0 } # not perfect, but better than not implementing the method
148 sub CLOSE { #<---- Do not change this function!
149 my $ssl = ${$_[0]};
150 local @_;
151 $ssl->close();
152 }
153  
154  
155 1;
156  
157 =head1 NAME
158  
159 Net::SSLGlue::Socket - socket which can be either SSL or plain IP (IPv4/IPv6)
160  
161 =head1 SYNOPSIS
162  
163 use Net::SSLGlue::Socket;
164 # SSL right from start
165 my $ssl = Net::SSLGlue::Socket->new(
166 PeerHost => ..., # IPv4|IPv6 address
167 PeerPort => ...,
168 SSL => 1,
169 SSL_ca_path => ...
170 );
171  
172 # SSL through upgrade of plain connection
173 my $plain = Net::SSLGlue::Socket->new(...);
174 $plain->start_SSL( SSL_ca_path => ... );
175 ...
176 $plain->stop_SSL
177  
178  
179 =head1 DESCRIPTION
180  
181 First, it is recommended to use L<IO::Socket::SSL> directly instead of this
182 module, since this kind of functionality is available in IO::Socket::SSL since
183 version 1.994.
184  
185 L<Net::SSLGlue::Socket> implements a socket which can be either plain or SSL.
186 If IO::Socket::IP or IO::Socket::INET6 are installed it will also transparently
187 handle IPv6 connections.
188  
189 A socket can be either start directly with SSL or it can be start plain and
190 later be upgraded to SSL (because of a STARTTLS commando or similar) and also
191 downgraded again.
192  
193 It is possible but not recommended to use the socket in non-blocking
194 mode, because in this case special care must be taken with SSL (see
195 documentation of L<IO::Socket::SSL>).
196  
197 Additionally to the usual socket methods the following methods are defined or
198 extended:
199  
200 =head1 METHODS
201  
202 =over 4
203  
204 =item new
205  
206 The method C<new> of L<Net::SSLGlue::Socket> can have the argument SSL. If this
207 is true the SSL upgrade will be done immediatly. If not set any SSL_* args will
208 still be saved and used at a later start_SSL call.
209  
210 =item start_SSL
211  
212 This will upgrade the plain socket to SSL. See L<IO::Socket::SSL> for
213 arguments to C<start_SSL>. Any SSL_* arguments given to new will be applied
214 here too.
215  
216 =item stop_SSL
217  
218 This will downgrade the socket from SSL to plain.
219  
220 =item peer_certificate ...
221  
222 Once the SSL connection is established you can use this method to get
223 information about the certificate. See the L<IO::Socket::SSL> documentation.
224  
225 =item can_read(timeout)
226  
227 This will check for available data. For a plain socket this will only use
228 C<select> to check the socket, but for SSL it will check if there are any
229 pending data before trying a select.
230 Because SSL needs to read the whole frame before decryption can be done, a
231 successful return of can_read is no guarantee that data can be read
232 immediatly, only that new data are either available or in the process of
233 arriving.
234  
235 =back
236  
237 =head1 SEE ALSO
238  
239 IO::Socket::SSL
240  
241 =head1 COPYRIGHT
242  
243 This module is copyright (c) 2013..2015, Steffen Ullrich.
244 All Rights Reserved.
245 This module is free software. It may be used, redistributed and/or modified
246 under the same terms as Perl itself.