xref: /openbsd/regress/usr.sbin/relayd/Client.pm (revision 3bef86f7)
1#	$OpenBSD: Client.pm,v 1.14 2021/12/22 11:50:28 bluhm Exp $
2
3# Copyright (c) 2010-2021 Alexander Bluhm <bluhm@openbsd.org>
4#
5# Permission to use, copy, modify, and distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17use strict;
18use warnings;
19
20package Client;
21use parent 'Proc';
22use Carp;
23use Socket qw(:DEFAULT IPPROTO_TCP TCP_NODELAY);
24use Socket6;
25use IO::Socket::IP;
26use IO::Socket::SSL;
27
28sub new {
29	my $class = shift;
30	my %args = @_;
31	$args{logfile} ||= "client.log";
32	$args{up} ||= "Connected";
33	$args{timefile} //= "time.log";
34	my $self = Proc::new($class, %args);
35	$self->{connectdomain}
36	    or croak "$class connect domain not given";
37	$self->{connectaddr}
38	    or croak "$class connect addr not given";
39	$self->{connectport}
40	    or croak "$class connect port not given";
41	return $self;
42}
43
44sub child {
45	my $self = shift;
46
47	# in case we redo the connect, shutdown the old one
48	shutdown(\*STDOUT, SHUT_WR);
49	delete $self->{cs};
50
51	$SSL_ERROR = "";
52	my $iosocket = $self->{ssl} ? "IO::Socket::SSL" : "IO::Socket::IP";
53	my $cs = $iosocket->new(
54	    Proto		=> "tcp",
55	    Domain		=> $self->{connectdomain},
56	    # IO::Socket::IP calls the domain family
57	    Family		=> $self->{connectdomain},
58	    PeerAddr		=> $self->{connectaddr},
59	    PeerPort		=> $self->{connectport},
60	    SSL_verify_mode	=> SSL_VERIFY_NONE,
61	) or die ref($self), " $iosocket socket connect failed: $!,$SSL_ERROR";
62	if ($self->{sndbuf}) {
63		setsockopt($cs, SOL_SOCKET, SO_SNDBUF,
64		    pack('i', $self->{sndbuf}))
65		    or die ref($self), " set SO_SNDBUF failed: $!";
66	}
67	if ($self->{rcvbuf}) {
68		setsockopt($cs, SOL_SOCKET, SO_RCVBUF,
69		    pack('i', $self->{rcvbuf}))
70		    or die ref($self), " set SO_SNDBUF failed: $!";
71	}
72	if ($self->{sndtimeo}) {
73		setsockopt($cs, SOL_SOCKET, SO_SNDTIMEO,
74		    pack('l!l!', $self->{sndtimeo}, 0))
75		    or die ref($self), " set SO_SNDTIMEO failed: $!";
76	}
77	if ($self->{rcvtimeo}) {
78		setsockopt($cs, SOL_SOCKET, SO_RCVTIMEO,
79		    pack('l!l!', $self->{rcvtimeo}, 0))
80		    or die ref($self), " set SO_RCVTIMEO failed: $!";
81	}
82	setsockopt($cs, IPPROTO_TCP, TCP_NODELAY, pack('i', 1))
83	    or die ref($self), " set TCP_NODELAY failed: $!";
84
85	print STDERR "connect sock: ",$cs->sockhost()," ",$cs->sockport(),"\n";
86	print STDERR "connect peer: ",$cs->peerhost()," ",$cs->peerport(),"\n";
87	if ($self->{ssl}) {
88		print STDERR "ssl version: ",$cs->get_sslversion(),"\n";
89		print STDERR "ssl cipher: ",$cs->get_cipher(),"\n";
90		print STDERR "ssl peer certificate:\n",
91		    $cs->dump_peer_certificate();
92	}
93
94	*STDIN = *STDOUT = $self->{cs} = $cs;
95}
96
971;
98