1# $OpenBSD: Client.pm,v 1.15 2024/10/28 19:57:02 tb 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 SSL_use_cert => $self->{offertlscert} ? 1 : 0, 62 SSL_cert_file => $self->{offertlscert} ? 63 "client.crt" : "", 64 SSL_key_file => $self->{offertlscert} ? 65 "client.key" : "", 66 ) or die ref($self), " $iosocket socket connect failed: $!,$SSL_ERROR"; 67 if ($self->{sndbuf}) { 68 setsockopt($cs, SOL_SOCKET, SO_SNDBUF, 69 pack('i', $self->{sndbuf})) 70 or die ref($self), " set SO_SNDBUF failed: $!"; 71 } 72 if ($self->{rcvbuf}) { 73 setsockopt($cs, SOL_SOCKET, SO_RCVBUF, 74 pack('i', $self->{rcvbuf})) 75 or die ref($self), " set SO_SNDBUF failed: $!"; 76 } 77 if ($self->{sndtimeo}) { 78 setsockopt($cs, SOL_SOCKET, SO_SNDTIMEO, 79 pack('l!l!', $self->{sndtimeo}, 0)) 80 or die ref($self), " set SO_SNDTIMEO failed: $!"; 81 } 82 if ($self->{rcvtimeo}) { 83 setsockopt($cs, SOL_SOCKET, SO_RCVTIMEO, 84 pack('l!l!', $self->{rcvtimeo}, 0)) 85 or die ref($self), " set SO_RCVTIMEO failed: $!"; 86 } 87 setsockopt($cs, IPPROTO_TCP, TCP_NODELAY, pack('i', 1)) 88 or die ref($self), " set TCP_NODELAY failed: $!"; 89 90 print STDERR "connect sock: ",$cs->sockhost()," ",$cs->sockport(),"\n"; 91 print STDERR "connect peer: ",$cs->peerhost()," ",$cs->peerport(),"\n"; 92 if ($self->{ssl}) { 93 print STDERR "ssl version: ",$cs->get_sslversion(),"\n"; 94 print STDERR "ssl cipher: ",$cs->get_cipher(),"\n"; 95 print STDERR "ssl peer certificate:\n", 96 $cs->dump_peer_certificate(); 97 98 if ($self->{offertlscert}) { 99 print STDERR "ssl client certificate:\n"; 100 print STDERR "Subject Name: ", 101 "${\$cs->sock_certificate('subject')}\n"; 102 print STDERR "Issuer Name: ", 103 "${\$cs->sock_certificate('issuer')}\n"; 104 } 105 } 106 107 *STDIN = *STDOUT = $self->{cs} = $cs; 108} 109 1101; 111