xref: /openbsd/regress/usr.sbin/relayd/Server.pm (revision bf5ac567)
1#	$OpenBSD: Server.pm,v 1.2 2012/12/28 20:36:25 bluhm Exp $
2
3# Copyright (c) 2010,2011 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 qw(IPPROTO_TCP TCP_NODELAY);
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	my $iosocket = $self->{ssl} ? "IO::Socket::SSL" : "IO::Socket::INET6";
38	my $ls = $iosocket->new(
39	    Proto	=> "tcp",
40	    ReuseAddr	=> 1,
41	    Domain	=> $self->{listendomain},
42	    Listen      => 1,
43	    $self->{listenaddr} ? (LocalAddr => $self->{listenaddr}) : (),
44	    $self->{listenport} ? (LocalPort => $self->{listenport}) : (),
45	    SSL_key_file  => "server-key.pem",
46	    SSL_cert_file => "server-cert.pem",
47	) or die ref($self), " $iosocket socket listen failed: $!";
48	my $log = $self->{log};
49	print $log "listen sock: ",$ls->sockhost()," ",$ls->sockport(),"\n";
50	$self->{listenaddr} = $ls->sockhost() unless $self->{listenaddr};
51	$self->{listenport} = $ls->sockport() unless $self->{listenport};
52	$self->{ls} = $ls;
53	return $self;
54}
55
56sub child {
57	my $self = shift;
58
59	my $iosocket = $self->{ssl} ? "IO::Socket::SSL" : "IO::Socket::INET6";
60	my $as = $self->{ls}->accept()
61	    or die ref($self), " $iosocket socket accept failed: $!";
62	print STDERR "accept sock: ",$as->sockhost()," ",$as->sockport(),"\n";
63	print STDERR "accept peer: ",$as->peerhost()," ",$as->peerport(),"\n";
64
65	*STDIN = *STDOUT = $self->{as} = $as;
66}
67
681;
69