1# $OpenBSD: Client.pm,v 1.12 2016/09/22 01:16:29 bluhm Exp $ 2 3# Copyright (c) 2010-2015 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; 24use Socket6; 25use IO::Socket; 26use IO::Socket::INET6; 27use IO::Socket::SSL; 28 29sub new { 30 my $class = shift; 31 my %args = @_; 32 $args{logfile} ||= "client.log"; 33 $args{up} ||= "Connected"; 34 $args{timefile} //= "time.log"; 35 my $self = Proc::new($class, %args); 36 $self->{connectdomain} 37 or croak "$class connect domain not given"; 38 $self->{connectaddr} 39 or croak "$class connect addr not given"; 40 $self->{connectport} 41 or croak "$class connect port not given"; 42 return $self; 43} 44 45sub child { 46 my $self = shift; 47 48 # in case we redo the connect, shutdown the old one 49 shutdown(\*STDOUT, SHUT_WR); 50 delete $self->{cs}; 51 52 $SSL_ERROR = ""; 53 my $iosocket = $self->{ssl} ? "IO::Socket::SSL" : "IO::Socket::INET6"; 54 my $cs = $iosocket->new( 55 Proto => "tcp", 56 Domain => $self->{connectdomain}, 57 PeerAddr => $self->{connectaddr}, 58 PeerPort => $self->{connectport}, 59 SSL_verify_mode => SSL_VERIFY_NONE, 60 ) or die ref($self), " $iosocket socket connect failed: $!,$SSL_ERROR"; 61 if ($self->{sndbuf}) { 62 setsockopt($cs, SOL_SOCKET, SO_SNDBUF, 63 pack('i', $self->{sndbuf})) 64 or die ref($self), " set SO_SNDBUF failed: $!"; 65 } 66 if ($self->{rcvbuf}) { 67 setsockopt($cs, SOL_SOCKET, SO_RCVBUF, 68 pack('i', $self->{rcvbuf})) 69 or die ref($self), " set SO_SNDBUF failed: $!"; 70 } 71 if ($self->{sndtimeo}) { 72 setsockopt($cs, SOL_SOCKET, SO_SNDTIMEO, 73 pack('l!l!', $self->{sndtimeo}, 0)) 74 or die ref($self), " set SO_SNDTIMEO failed: $!"; 75 } 76 if ($self->{rcvtimeo}) { 77 setsockopt($cs, SOL_SOCKET, SO_RCVTIMEO, 78 pack('l!l!', $self->{rcvtimeo}, 0)) 79 or die ref($self), " set SO_RCVTIMEO failed: $!"; 80 } 81 82 print STDERR "connect sock: ",$cs->sockhost()," ",$cs->sockport(),"\n"; 83 print STDERR "connect peer: ",$cs->peerhost()," ",$cs->peerport(),"\n"; 84 if ($self->{ssl}) { 85 print STDERR "ssl version: ",$cs->get_sslversion(),"\n"; 86 print STDERR "ssl cipher: ",$cs->get_cipher(),"\n"; 87 print STDERR "ssl peer certificate:\n", 88 $cs->dump_peer_certificate(); 89 } 90 91 *STDIN = *STDOUT = $self->{cs} = $cs; 92} 93 941; 95