xref: /openbsd/regress/usr.sbin/relayd/Server.pm (revision e5dd7070)
1#	$OpenBSD: Server.pm,v 1.13 2019/07/05 13:15:52 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	    $self->{listenaddr} ? (LocalAddr => $self->{listenaddr}) : (),
45	    $self->{listenport} ? (LocalPort => $self->{listenport}) : (),
46	    SSL_server          => 1,
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 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 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 SO_RCVBUF failed: $!";
60	}
61	my $packstr = $Config{longsize} == 8 ? 'ql!' :
62	    $Config{byteorder} == 1234 ? 'lxxxxl!xxxx' : '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: $!";
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: $!";
72	}
73	listen($ls, 1)
74	    or die ref($self), " socket listen failed: $!";
75	my $log = $self->{log};
76	print $log "listen sock: ",$ls->sockhost()," ",$ls->sockport(),"\n";
77	$self->{listenaddr} = $ls->sockhost() unless $self->{listenaddr};
78	$self->{listenport} = $ls->sockport() unless $self->{listenport};
79	$self->{ls} = $ls;
80	return $self;
81}
82
83sub child {
84	my $self = shift;
85
86	# in case we redo the accept, shutdown the old one
87	shutdown(\*STDOUT, SHUT_WR);
88	delete $self->{as};
89
90	my $as = $self->{ls}->accept()
91	    or die ref($self)," ",ref($self->{ls}),
92	    " socket accept failed: $!,$SSL_ERROR";
93	print STDERR "accept sock: ",$as->sockhost()," ",$as->sockport(),"\n";
94	print STDERR "accept peer: ",$as->peerhost()," ",$as->peerport(),"\n";
95	if ($self->{ssl}) {
96		print STDERR "ssl version: ",$as->get_sslversion(),"\n";
97		print STDERR "ssl cipher: ",$as->get_cipher(),"\n";
98		print STDERR "ssl peer certificate:\n",
99		    $as->dump_peer_certificate();
100	}
101
102	*STDIN = *STDOUT = $self->{as} = $as;
103}
104
1051;
106