xref: /openbsd-src/regress/sys/net/pf_divert/Server.pm (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#	$OpenBSD: Server.pm,v 1.3 2013/06/05 04:34:27 bluhm Exp $
2
3# Copyright (c) 2010-2013 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;
27
28sub new {
29	my $class = shift;
30	my %args = @_;
31	$args{logfile} ||= "server.log";
32	$args{up} ||= "Accepted";
33	my $self = Proc::new($class, %args);
34	$self->{domain}
35	    or croak "$class domain not given";
36	$self->{protocol}
37	    or croak "$class protocol not given";
38	my $ls = do { local $> = 0; IO::Socket::INET6->new(
39	    Type	=> $self->{socktype},
40	    Proto	=> $self->{protocol},
41	    ReuseAddr	=> 1,
42	    Domain	=> $self->{domain},
43	    $self->{listenaddr} ? (LocalAddr => $self->{listenaddr}) : (),
44	    $self->{listenport} ? (LocalPort => $self->{listenport}) : (),
45	) } or die ref($self), " socket failed: $!";
46	if ($self->{oobinline}) {
47		setsockopt($ls, SOL_SOCKET, SO_OOBINLINE, pack('i', 1))
48		    or die ref($self), " set oobinline listen failed: $!";
49	}
50	if ($self->{sndbuf}) {
51		setsockopt($ls, SOL_SOCKET, SO_SNDBUF,
52		    pack('i', $self->{sndbuf}))
53		    or die ref($self), " set sndbuf listen failed: $!";
54	}
55	if ($self->{rcvbuf}) {
56		setsockopt($ls, SOL_SOCKET, SO_RCVBUF,
57		    pack('i', $self->{rcvbuf}))
58		    or die ref($self), " set rcvbuf listen failed: $!";
59	}
60	if ($self->{protocol} eq "tcp") {
61		setsockopt($ls, IPPROTO_TCP, TCP_NODELAY, pack('i', 1))
62		    or die ref($self), " set nodelay listen failed: $!";
63		listen($ls, 1)
64		    or die ref($self), " socket failed: $!";
65	}
66	my $log = $self->{log};
67	print $log "listen sock: ",$ls->sockhost()," ",$ls->sockport(),"\n";
68	$self->{listenaddr} = $ls->sockhost() unless $self->{listenaddr};
69	$self->{listenport} = $ls->sockport() unless $self->{listenport};
70	$self->{ls} = $ls;
71	return $self;
72}
73
74sub child {
75	my $self = shift;
76
77	my $as = $self->{ls};
78	if ($self->{protocol} eq "tcp") {
79		$as = $self->{ls}->accept()
80		    or die ref($self), " socket accept failed: $!";
81		print STDERR "accept sock: ",$as->sockhost()," ",
82		    $as->sockport(),"\n";
83		print STDERR "accept peer: ",$as->peerhost()," ",
84		    $as->peerport(),"\n";
85	}
86	$as->blocking($self->{nonblocking} ? 0 : 1)
87	    or die ref($self), " non-blocking accept failed: $!";
88
89	open(STDIN, '<&', $as)
90	    or die ref($self), " dup STDIN failed: $!";
91	open(STDOUT, '>&', $as)
92	    or die ref($self), " dup STDOUT failed: $!";
93}
94
951;
96