summaryrefslogtreecommitdiff
path: root/inc/Test/Net/SSLeay/Socket.pm
blob: ca3089f9b3b35f451194cf87511ecccca7408b5e (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package Test::Net::SSLeay::Socket;

use 5.008001;
use strict;
use warnings;

use Carp qw(croak);
use English qw( $EVAL_ERROR $OS_ERROR $OUTPUT_AUTOFLUSH -no_match_vars );
use Scalar::Util qw(refaddr reftype);
use SelectSaver;
use Socket qw(
    AF_INET SOCK_DGRAM SOCK_STREAM
    inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
);

our $VERSION = '1.94';

my %PROTOS = (
    tcp => SOCK_STREAM,
    udp => SOCK_DGRAM,
);

sub new {
    my ( $class, %args ) = @_;

    my $self  = bless {
        addr  => delete $args{addr}  || '127.0.0.1',
        port  => delete $args{port}  || 0,
        proto => delete $args{proto} || 'tcp',
        queue => delete $args{queue} || 5,
    }, $class;

    if ( !exists $PROTOS{ $self->{proto} } ) {
        croak "Unknown protocol '$self->{proto}'";
    }

    $self->_init_server();

    return $self;
}

sub _init_server {
    my ($self) = @_;

    my $addr = eval { inet_aton( $self->{addr} ) }
        or croak 'Could not pack IP address'
        . ( $EVAL_ERROR ? ": $EVAL_ERROR" : q{} );

    my $sockaddr = eval { pack_sockaddr_in( $self->{port}, $addr ) }
        or croak 'Could not create sockaddr_in structure'
        . ( $EVAL_ERROR ? ": $EVAL_ERROR" : q{} );

    socket $self->{sock}, AF_INET, $PROTOS{ $self->{proto} }, 0
        or croak "Could not open server socket: $OS_ERROR";

    if ( $self->{proto} eq 'tcp' ) {
        bind $self->{sock}, $sockaddr
            or croak "Could not bind server socket: $OS_ERROR";

        listen $self->{sock}, $self->{queue}
            or croak "Could not listen on server socket: $OS_ERROR";
    }

    my $sockname = getsockname $self->{sock};
    ( $self->{sport}, $self->{saddr} ) = unpack_sockaddr_in($sockname);
    $self->{saddr} = inet_ntoa( $self->{saddr} );

    return 1;
}

sub get_addr {
    my ($self) = @_;

    return $self->{saddr};
}

sub get_port {
    my ($self) = @_;

    return $self->{sport};
}

sub accept {
    my ( $self, $sock ) = @_;

    if ( defined $sock && reftype($sock) ne 'GLOB' ) {
        croak 'Argument #1 to accept() must be a typeglob reference';
    }

    accept $sock, $self->{sock}
        or croak "Could not accept connection: $OS_ERROR";

    my $saver = SelectSaver->new($sock);
    local $OUTPUT_AUTOFLUSH = 1;

    return $sock;
}

sub connect {
    my ($self) = @_;

    my $addr = eval { inet_aton( $self->{saddr} ) }
        or croak 'Could not pack IP address in connect'
        . ( $EVAL_ERROR ? ": $EVAL_ERROR" : q{} );

    my $sockaddr = eval { pack_sockaddr_in( $self->{sport}, $addr ) }
        or croak 'Could not create sockaddr_in structure in connect'
        . ( $EVAL_ERROR ? ": $EVAL_ERROR" : q{} );

    socket my $sock, AF_INET, $PROTOS{ $self->{proto} }, 0
        or croak "Could not open server socket in connect: $OS_ERROR";
    connect $sock, $sockaddr
        or croak "Could not connect to server socket: $OS_ERROR";

    my $saver = SelectSaver->new($sock);
    local $OUTPUT_AUTOFLUSH = 1;

    return $sock;
}

sub close {
    my ($self) = @_;

    return close $self->{sock};
}

1;

__END__

=head1 NAME

Test::Net::SSLeay::Socket - Socket class for the Net-SSLeay test suite

=head1 VERSION

This document describes version 1.94 of Test::Net::SSLeay::Socket.

=head1 SYNOPSIS

    use Test::Net::SSLeay::Socket;

    # Create TCP server socket listening on localhost on a random unused port
    my $server = Test::Net::SSLeay::Socket->new( protocol => 'tcp' );

    # To wait for a connection to the server socket:
    my $sock = $server->accept();

    # Open a connection to the server socket:
    my $client_sock = $server->connect();

    # Or do so using Net::SSLeay's high-level API:
    use Net::SSLeay qw(tcpcat);
    my ( $response, $err ) =
        tcpcat( $server->get_addr(), $server->get_port(), 'request' );

=head1 DESCRIPTION

Test scripts in the Net-SSLeay test suite commonly need to establish server
and client sockets over which TLS communication can be tested. This module
simplifies the process of creating server sockets and client sockets that know
how to connect to them.

This module is not intended to be used directly by test scripts; use the
helper functions in L<Test::Net::SSLeay|Test::Net::SSLeay/"HELPER FUNCTIONS">
instead.

=head1 CONSTRUCTOR

=head2 new

    # TCP server socket listening on localhost on a random unused port:
    my $server = Test::Net::SSLeay::Socket->new();

    # TCP server socket listening on a private IP address on the standard HTTP
    # port:
    my $server = Test::Net::SSLeay::Socket->new(
        addr  => '10.0.0.1',
        port  => 80,
        proto => 'tcp',
    );

Creates a new C<Test::Net::SSLeay::Socket> object. A server socket is created
that binds to a given (or the default) address and port number.

Supported options:

=over 4

=item *

C<addr> (optional): the IPv4 address that the server socket should bind to.
Defaults to C<'127.0.0.1'>.

=item *

C<port> (optional): the port number that the server socket should bind to.
Defaults to the number of a random unused port chosen by the operating system.

=item *

C<proto> (optional): the transport protocol that the server socket should use;
C<'tcp'> for TCP, C<'udp'> for UDP. Defaults to C<'tcp'>.

=item *

C<queue> (optional): the maximum number of pending connections to allow for
the server socket. Defaults to 5.

=back

Dies on failure.

=head1 METHODS

=head2 get_addr

    my $address = $server->get_addr();

Returns the address on which the server socket is listening. Useful when
manually creating a connection to the server socket (e.g. via one of
Net::SSLeay's high-level API functions) and an address was not specified in
the constructor.

=head2 get_port

    my $port = $server->get_port();

Returns the port number on which the server socket is listening. Useful when
manually creating a client socket to connect to the server socket (e.g. via
one of Net::SSLeay's high-level API functions) and a port number was not
specified in the constructor.

=head2 accept

    # Communicate with the client, creating a new file handle:
    my $sock = $server->accept();

    # Communicate with the client using an existing typeglob as the file
    # handle:
    $server->accept(*Net::SSLeay::SSLCAT_S);

Accepts an incoming connection request to the server socket, and enables
autoflush on the resulting file handle.

If a typeglob is passed as the first argument, it becomes the socket's file
handle. This is useful when creating sockets for testing Net::SSLeay's
high-level API functions, which perform their operations on the
C<Net::SSLeay::SSLCAT_S> typeglob.

Returns the file handle for the new socket. Dies on failure.

=head2 connect

    my $sock = $server->connect();

Creates a new connection to the server socket, and enables autoflush on the
resulting file handle.

Returns the file handle for the new socket. Dies on failure.

=head2 close

    $server->close();

Closes the file handle for the server socket.

Returns true on success, or false on failure (just like Perl's
L<close|perlfunc/close> builtin).

=head1 SEE ALSO

L<Test::Net::SSLeay|Test::Net::SSLeay>, for an easier way to use this module
from Net-SSLeay test scripts.

=head1 BUGS

If you encounter a problem with this module that you believe is a bug, please
L<create a new issue|https://github.com/radiator-software/p5-net-ssleay/issues/new>
in the Net-SSLeay GitHub repository. Please make sure your bug report includes
the following information:

=over

=item *

the code you are trying to run (ideally a minimum working example that
reproduces the problem), or the full output of the Net-SSLeay test suite if
the problem relates to a test failure;

=item *

your operating system name and version;

=item *

the output of C<perl -V>;

=item *

the version of Net-SSLeay you are using;

=item *

the version of OpenSSL or LibreSSL you are using.

=back

=head1 AUTHORS

Originally written by Chris Novakovic.

Maintained by Chris Novakovic and Heikki Vatiainen.

=head1 COPYRIGHT AND LICENSE

Copyright 2020- Chris Novakovic <chris@chrisn.me.uk>.

Copyright 2020- Heikki Vatiainen <hvn@radiatorsoftware.com>.

This module is released under the terms of the Artistic License 2.0. For
details, see the C<LICENSE> file distributed with Net-SSLeay's source code.

=cut