xref: /openbsd/regress/usr.sbin/relayd/Server.pm (revision 037367f6)
1#	$OpenBSD: Server.pm,v 1.8 2015/05/22 19:09:18 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 Server;
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} ||= "server.log";
33	$args{up} ||= "Accepted";
34	my $self = Proc::new($class, %args);
35	$self->{listendomain}
36	    or croak "$class listen domain not given";
37	$SSL_ERROR = "";
38	my $iosocket = $self->{ssl} ? "IO::Socket::SSL" : "IO::Socket::INET6";
39	my $ls = $iosocket->new(
40	    Proto		=> "tcp",
41	    ReuseAddr		=> 1,
42	    Domain		=> $self->{listendomain},
43	    Listen		=> 1,
44	    $self->{listenaddr} ? (LocalAddr => $self->{listenaddr}) : (),
45	    $self->{listenport} ? (LocalPort => $self->{listenport}) : (),
46	    SSL_key_file	=> "server.key",
47	    SSL_cert_file	=> "server.crt",
48	    SSL_verify_mode	=> SSL_VERIFY_NONE,
49	) or die ref($self), " $iosocket socket listen failed: $!,$SSL_ERROR";
50	my $log = $self->{log};
51	print $log "listen sock: ",$ls->sockhost()," ",$ls->sockport(),"\n";
52	$self->{listenaddr} = $ls->sockhost() unless $self->{listenaddr};
53	$self->{listenport} = $ls->sockport() unless $self->{listenport};
54	$self->{ls} = $ls;
55	return $self;
56}
57
58sub child {
59	my $self = shift;
60
61	# in case we redo the accept, shutdown the old one
62	shutdown(\*STDOUT, SHUT_WR);
63	delete $self->{as};
64
65	my $as = $self->{ls}->accept()
66	    or die ref($self)," ",ref($self->{ls}),
67	    " socket accept failed: $!,$SSL_ERROR";
68	print STDERR "accept sock: ",$as->sockhost()," ",$as->sockport(),"\n";
69	print STDERR "accept peer: ",$as->peerhost()," ",$as->peerport(),"\n";
70	if ($self->{ssl}) {
71		print STDERR "ssl version: ",$as->get_sslversion(),"\n";
72		print STDERR "ssl cipher: ",$as->get_cipher(),"\n";
73		print STDERR "ssl peer certificate:\n",
74		    $as->dump_peer_certificate();
75	}
76
77	*STDIN = *STDOUT = $self->{as} = $as;
78}
79
801;
81