1# $OpenBSD: Server.pm,v 1.5 2015/01/15 13:15:17 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 Socket; 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{ktracefile} ||= "server.ktrace"; 33 $args{logfile} ||= "server.log"; 34 $args{up} ||= "Accepted"; 35 my $self = Proc::new($class, %args); 36 $self->{listenproto} ||= "udp"; 37 defined($self->{listendomain}) 38 or croak "$class listen domain not given"; 39 return $self->listen(); 40} 41 42sub listen { 43 my $self = shift; 44 $SSL_ERROR = ""; 45 my $iosocket = $self->{listenproto} eq "tls" ? 46 "IO::Socket::SSL" : "IO::Socket::INET6"; 47 my $proto = $self->{listenproto}; 48 $proto = "tcp" if $proto eq "tls"; 49 my $ls = $iosocket->new( 50 Proto => $proto, 51 ReuseAddr => 1, 52 Domain => $self->{listendomain}, 53 $self->{listenaddr} ? (LocalAddr => $self->{listenaddr}) : (), 54 $self->{listenport} ? (LocalPort => $self->{listenport}) : (), 55 SSL_key_file => "server.key", 56 SSL_cert_file => "server.crt", 57 SSL_verify_mode => SSL_VERIFY_NONE, 58 ) or die ref($self), " $iosocket socket listen failed: $!,$SSL_ERROR"; 59 if ($self->{listenproto} ne "udp") { 60 listen($ls, 1) 61 or die ref($self), " socket failed: $!"; 62 } 63 my $log = $self->{log}; 64 print $log "listen sock: ",$ls->sockhost()," ",$ls->sockport(),"\n"; 65 $self->{listenaddr} = $ls->sockhost() unless $self->{listenaddr}; 66 $self->{listenport} = $ls->sockport() unless $self->{listenport}; 67 $self->{ls} = $ls; 68 return $self; 69} 70 71sub close { 72 my $self = shift; 73 $self->{ls}->close() 74 or die ref($self)," ",ref($self->{ls}), 75 " socket close failed: $!,$SSL_ERROR"; 76 delete $self->{ls}; 77 return $self; 78} 79 80sub run { 81 my $self = shift; 82 Proc::run($self, @_); 83 return $self->close(); 84} 85 86sub child { 87 my $self = shift; 88 89 my $as = $self->{ls}; 90 if ($self->{listenproto} ne "udp") { 91 $as = $self->{ls}->accept() 92 or die ref($self)," ",ref($self->{ls}), 93 " socket accept failed: $!,$SSL_ERROR"; 94 print STDERR "accept sock: ",$as->sockhost()," ", 95 $as->sockport(),"\n"; 96 print STDERR "accept peer: ",$as->peerhost()," ", 97 $as->peerport(),"\n"; 98 } 99 100 *STDIN = *STDOUT = $self->{as} = $as; 101} 102 1031; 104