xref: /openbsd/regress/usr.sbin/relayd/Server.pm (revision b65dd5ea)
1#	$OpenBSD: Server.pm,v 1.15 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 Server;
21use parent 'Proc';
22use Carp;
23use Config;
24use Socket qw(:DEFAULT IPPROTO_TCP TCP_NODELAY);
25use Socket6;
26use IO::Socket::IP;
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::IP";
39	my $ls = $iosocket->new(
40	    Proto		=> "tcp",
41	    ReuseAddr		=> 1,
42	    Domain		=> $self->{listendomain},
43	    # IO::Socket::IP calls the domain family
44	    Family		=> $self->{listendomain},
45	    $self->{listenaddr} ? (LocalAddr => $self->{listenaddr}) : (),
46	    $self->{listenport} ? (LocalPort => $self->{listenport}) : (),
47	    SSL_server          => 1,
48	    SSL_key_file	=> "server.key",
49	    SSL_cert_file	=> "server.crt",
50	    SSL_verify_mode	=> SSL_VERIFY_NONE,
51	) or die ref($self), " $iosocket socket failed: $!,$SSL_ERROR";
52	if ($self->{sndbuf}) {
53		setsockopt($ls, SOL_SOCKET, SO_SNDBUF,
54		    pack('i', $self->{sndbuf}))
55		    or die ref($self), " set SO_SNDBUF failed: $!";
56	}
57	if ($self->{rcvbuf}) {
58		setsockopt($ls, SOL_SOCKET, SO_RCVBUF,
59		    pack('i', $self->{rcvbuf}))
60		    or die ref($self), " set SO_RCVBUF failed: $!";
61	}
62	my $packstr = $Config{longsize} == 8 ? 'ql!' :
63	    $Config{byteorder} == 1234 ? 'lxxxxl!xxxx' : 'xxxxll!';
64	if ($self->{sndtimeo}) {
65		setsockopt($ls, SOL_SOCKET, SO_SNDTIMEO,
66		    pack($packstr, $self->{sndtimeo}, 0))
67		    or die ref($self), " set SO_SNDTIMEO failed: $!";
68	}
69	if ($self->{rcvtimeo}) {
70		setsockopt($ls, SOL_SOCKET, SO_RCVTIMEO,
71		    pack($packstr, $self->{rcvtimeo}, 0))
72		    or die ref($self), " set SO_RCVTIMEO failed: $!";
73	}
74	setsockopt($ls, IPPROTO_TCP, TCP_NODELAY, pack('i', 1))
75	    or die ref($self), " set TCP_NODELAY failed: $!";
76	listen($ls, 1)
77	    or die ref($self), " socket listen failed: $!";
78	my $log = $self->{log};
79	print $log "listen sock: ",$ls->sockhost()," ",$ls->sockport(),"\n";
80	$self->{listenaddr} = $ls->sockhost() unless $self->{listenaddr};
81	$self->{listenport} = $ls->sockport() unless $self->{listenport};
82	$self->{ls} = $ls;
83	return $self;
84}
85
86sub child {
87	my $self = shift;
88
89	# in case we redo the accept, shutdown the old one
90	shutdown(\*STDOUT, SHUT_WR);
91	delete $self->{as};
92
93	my $as = $self->{ls}->accept()
94	    or die ref($self)," ",ref($self->{ls}),
95	    " socket accept failed: $!,$SSL_ERROR";
96	print STDERR "accept sock: ",$as->sockhost()," ",$as->sockport(),"\n";
97	print STDERR "accept peer: ",$as->peerhost()," ",$as->peerport(),"\n";
98	if ($self->{ssl}) {
99		print STDERR "ssl version: ",$as->get_sslversion(),"\n";
100		print STDERR "ssl cipher: ",$as->get_cipher(),"\n";
101		print STDERR "ssl peer certificate:\n",
102		    $as->dump_peer_certificate();
103	}
104
105	*STDIN = *STDOUT = $self->{as} = $as;
106}
107
1081;
109