1# $OpenBSD: Server.pm,v 1.5 2014/07/09 16:48:55 reyk Exp $ 2 3# Copyright (c) 2010-2012 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 $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.pem", 47 SSL_cert_file => "server-cert.pem", 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 if ($self->{mreqs}) { 62 print STDERR "connection per request\n"; 63 return; 64 } 65 my $iosocket = $self->{ssl} ? "IO::Socket::SSL" : "IO::Socket::INET6"; 66 my $as = $self->{ls}->accept() 67 or die ref($self), " $iosocket socket accept failed: $!"; 68 print STDERR "accept sock: ",$as->sockhost()," ",$as->sockport(),"\n"; 69 print STDERR "accept peer: ",$as->peerhost()," ",$as->peerport(),"\n"; 70 print STDERR "single connection\n"; 71 72 *STDIN = *STDOUT = $self->{as} = $as; 73} 74 751; 76