xref: /openbsd-src/regress/usr.sbin/relayd/Server.pm (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#	$OpenBSD: Server.pm,v 1.10 2016/08/25 22:56:13 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 Config;
24use Socket;
25use Socket6;
26use IO::Socket;
27use IO::Socket::INET6;
28use IO::Socket::SSL;
29
30sub new {
31	my $class = shift;
32	my %args = @_;
33	$args{logfile} ||= "server.log";
34	$args{up} ||= "Accepted";
35	my $self = Proc::new($class, %args);
36	$self->{listendomain}
37	    or croak "$class listen domain not given";
38	$SSL_ERROR = "";
39	my $iosocket = $self->{ssl} ? "IO::Socket::SSL" : "IO::Socket::INET6";
40	my $ls = $iosocket->new(
41	    Proto		=> "tcp",
42	    ReuseAddr		=> 1,
43	    Domain		=> $self->{listendomain},
44	    Listen		=> 1,
45	    $self->{listenaddr} ? (LocalAddr => $self->{listenaddr}) : (),
46	    $self->{listenport} ? (LocalPort => $self->{listenport}) : (),
47	    SSL_key_file	=> "server.key",
48	    SSL_cert_file	=> "server.crt",
49	    SSL_verify_mode	=> SSL_VERIFY_NONE,
50	) or die ref($self), " $iosocket socket listen failed: $!,$SSL_ERROR";
51	if ($self->{sndbuf}) {
52		setsockopt($ls, SOL_SOCKET, SO_SNDBUF,
53		    pack('i', $self->{sndbuf}))
54		    or die ref($self), " set sndbuf SO_SNDBUF failed: $!";
55	}
56	if ($self->{rcvbuf}) {
57		setsockopt($ls, SOL_SOCKET, SO_RCVBUF,
58		    pack('i', $self->{rcvbuf}))
59		    or die ref($self), " set rcvbuf SO_RCVBUF failed: $!";
60	}
61	my $packstr = $Config{longsize} == 8 ? 'ql!' :
62	    $Config{byteorder} == 1234 ? 'lxxxxl!' : 'xxxxll!';
63	if ($self->{sndtimeo}) {
64		setsockopt($ls, SOL_SOCKET, SO_SNDTIMEO,
65		    pack($packstr, $self->{sndtimeo}, 0))
66		    or die ref($self), " set SO_SNDTIMEO failed failed: $!";
67	}
68	if ($self->{rcvtimeo}) {
69		setsockopt($ls, SOL_SOCKET, SO_RCVTIMEO,
70		    pack($packstr, $self->{rcvtimeo}, 0))
71		    or die ref($self), " set SO_RCVTIMEO failed failed: $!";
72	}
73	my $log = $self->{log};
74	print $log "listen sock: ",$ls->sockhost()," ",$ls->sockport(),"\n";
75	$self->{listenaddr} = $ls->sockhost() unless $self->{listenaddr};
76	$self->{listenport} = $ls->sockport() unless $self->{listenport};
77	$self->{ls} = $ls;
78	return $self;
79}
80
81sub child {
82	my $self = shift;
83
84	# in case we redo the accept, shutdown the old one
85	shutdown(\*STDOUT, SHUT_WR);
86	delete $self->{as};
87
88	my $as = $self->{ls}->accept()
89	    or die ref($self)," ",ref($self->{ls}),
90	    " socket accept failed: $!,$SSL_ERROR";
91	print STDERR "accept sock: ",$as->sockhost()," ",$as->sockport(),"\n";
92	print STDERR "accept peer: ",$as->peerhost()," ",$as->peerport(),"\n";
93	if ($self->{ssl}) {
94		print STDERR "ssl version: ",$as->get_sslversion(),"\n";
95		print STDERR "ssl cipher: ",$as->get_cipher(),"\n";
96		print STDERR "ssl peer certificate:\n",
97		    $as->dump_peer_certificate();
98	}
99
100	*STDIN = *STDOUT = $self->{as} = $as;
101}
102
1031;
104