1# $OpenBSD: Server.pm,v 1.6 2021/12/12 21:16:53 bluhm Exp $ 2 3# Copyright (c) 2010-2017 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::IP -register; 27 28sub new { 29 my $class = shift; 30 my %args = @_; 31 $args{ktracefile} ||= "server.ktrace"; 32 $args{logfile} ||= "server.log"; 33 $args{up} ||= "Accepted"; 34 $args{down} ||= "Shutdown $class"; 35 my $self = Proc::new($class, %args); 36 $self->{domain} 37 or croak "$class domain not given"; 38 $self->{protocol} 39 or croak "$class protocol not given"; 40 41 if ($self->{ktrace}) { 42 unlink $self->{ktracefile}; 43 my @cmd = ("ktrace", "-f", $self->{ktracefile}, "-p", $$); 44 do { local $> = 0; system(@cmd) } 45 and die ref($self), " system '@cmd' failed: $?"; 46 } 47 48 my $ls = do { local $> = 0; IO::Socket->new( 49 Type => $self->{socktype}, 50 Proto => $self->{protocol}, 51 ReuseAddr => 1, 52 Domain => $self->{domain}, 53 $self->{listenaddr} ? (LocalAddr => $self->{listenaddr}) : (), 54 $self->{listenport} ? (LocalPort => $self->{listenport}) : (), 55 ) } or die ref($self), " socket failed: $!"; 56 if ($self->{oobinline}) { 57 setsockopt($ls, SOL_SOCKET, SO_OOBINLINE, pack('i', 1)) 58 or die ref($self), " set oobinline listen failed: $!"; 59 } 60 if ($self->{sndbuf}) { 61 setsockopt($ls, SOL_SOCKET, SO_SNDBUF, 62 pack('i', $self->{sndbuf})) 63 or die ref($self), " set sndbuf listen failed: $!"; 64 } 65 if ($self->{rcvbuf}) { 66 setsockopt($ls, SOL_SOCKET, SO_RCVBUF, 67 pack('i', $self->{rcvbuf})) 68 or die ref($self), " set rcvbuf listen failed: $!"; 69 } 70 if ($self->{protocol} eq "tcp") { 71 setsockopt($ls, IPPROTO_TCP, TCP_NODELAY, pack('i', 1)) 72 or die ref($self), " set nodelay listen failed: $!"; 73 listen($ls, 1) 74 or die ref($self), " socket failed: $!"; 75 } 76 my $log = $self->{log}; 77 print $log "listen sock: ",$ls->sockhost()," ",$ls->sockport(),"\n"; 78 $self->{listenaddr} = $ls->sockhost() unless $self->{listenaddr}; 79 $self->{listenport} = $ls->sockport() unless $self->{listenport}; 80 $self->{ls} = $ls; 81 82 if ($self->{ktrace}) { 83 my @cmd = ("ktrace", "-c", "-f", $self->{ktracefile}, "-p", $$); 84 do { local $> = 0; system(@cmd) } 85 and die ref($self), " system '@cmd' failed: $?"; 86 } 87 88 return $self; 89} 90 91sub child { 92 my $self = shift; 93 94 my $as = $self->{ls}; 95 if ($self->{protocol} eq "tcp") { 96 $as = $self->{ls}->accept() 97 or die ref($self), " socket accept failed: $!"; 98 print STDERR "accept sock: ",$as->sockhost()," ", 99 $as->sockport(),"\n"; 100 print STDERR "accept peer: ",$as->peerhost()," ", 101 $as->peerport(),"\n"; 102 } 103 if ($self->{nonblocking}) { 104 $as->blocking(0) 105 or die ref($self), " set non-blocking accept failed: $!"; 106 } 107 108 open(STDIN, '<&', $as) 109 or die ref($self), " dup STDIN failed: $!"; 110 open(STDOUT, '>&', $as) 111 or die ref($self), " dup STDOUT failed: $!"; 112} 113 1141; 115