Content-type: text/html
#!/usr/bin/perl -w -T package MyPackage;
use base qw(Net::Server);
sub process_request { my $self = shift; while (<STDIN>) { s/\r?\n$//; print "You said '$_'\r\n"; # basic echo last if /quit/i; } }
MyPackage->run(port => 160);
* Single Server Mode * Inetd Server Mode * Preforking Simple Mode (PreForkSimple) * Preforking Managed Mode (PreFork) * Forking Mode * Multiplexing Mode using a single process * Multi port accepts on Single, Preforking, and Forking modes * Simultaneous accept/recv on tcp, udp, and unix sockets * Safe signal handling in Fork/PreFork avoids perl signal trouble * User customizable hooks * Chroot ability after bind * Change of user and group after bind * Basic allow/deny access control * Customized logging (choose Syslog, log_file, or STDERR) * HUP able server (clean restarts via sig HUP) * Dequeue ability in all Fork and PreFork modes. * Taint clean * Written in Perl * Protection against buffer overflow * Clean process flow * Extensibility
"Net::Server" attempts to be a generic server as in "Net::Daemon" and "NetServer::Generic". It includes with it the ability to run as an inetd process ("Net::Server::INET"), a single connection server ("Net::Server" or "Net::Server::Single"), a forking server ("Net::Server::Fork"), a preforking server which maintains a constant number of preforked children ("Net::Server::PreForkSimple"), or as a managed preforking server which maintains the number of children based on server load ("Net::Server::PreFork"). In all but the inetd type, the server provides the ability to connect to one or to multiple server ports.
"Net::Server" uses ideologies of "Net::FTPServer" in order to provide extensibility. The additional server types are made possible via ``personalities'' or sub classes of the "Net::Server". By moving the multiple types of servers out of the main "Net::Server" class, the "Net::Server" concept is easily extended to other types (in the near future, we would like to add a ``Thread'' personality).
"Net::Server" borrows several concepts from the Apache Webserver. "Net::Server" uses ``hooks'' to allow custom servers such as SMTP, HTTP, POP3, etc. to be layered over the base "Net::Server" class. In addition the "Net::Server::PreFork" class borrows concepts of min_start_servers, max_servers, and min_waiting servers. "Net::Server::PreFork" also uses the concept of an flock serialized accept when accepting on multiple ports (PreFork can choose between flock, IPC::Semaphore, and pipe to control serialization).
Included with the Net::Server package are several basic personalities, each of which has their own use.
"Net::Server" was partially written to make it easy to add new personalities. Using separate modules built upon an open architecture allows for easy addition of new features, a separate development process, and reduced code bloat in the core module.
To make programming easier, during the post_accept phase, STDIN and STDOUT are opened to the client connection. This allows for programs to be written using <STDIN> and print ``out\n'' to print to the client connection. UDP will require using a ->send call.
#!/usr/bin/perl -w -T
package MyPackage;
use strict; use base qw(Net::Server::PreFork); # any personality will do
MyPackage->run;
### over-ridden subs below
sub process_request { my $self = shift; eval {
local $SIG{'ALRM'} = sub { die "Timed Out!\n" }; my $timeout = 30; # give the user 30 seconds to type some lines
my $previous_alarm = alarm($timeout); while (<STDIN>) { s/\r?\n$//; print "You said '$_'\r\n"; alarm($timeout); } alarm($previous_alarm);
};
if ($@ =~ /timed out/i) { print STDOUT "Timed Out.\r\n"; return; }
}
1;
Playing this file from the command line will invoke a Net::Server using the PreFork personality. When building a server layer over the Net::Server, it is important to use features such as timeouts to prevent Denial Of Service attacks.
The "options" method is used to determine which arguments the server will search for and can be used to extend the parsed parameters. Any arguments found from the command line, parameters passed to run, and arguments found in the conf_file will be matched against the keys of the options template. Any commandline parameters that do not match will be left in place and can be further processed by the server in the various hooks (by looking at @ARGV). Arguments passed to new will automatically win over any other options (this can be used if you would like to disallow a user passing in other arguments).
Arguments consist of key value pairs. On the commandline these pairs follow the POSIX fashion of "--key value" or "--key=value", and also "key=value". In the conf file the parameter passing can best be shown by the following regular expression: ($key,$val)=~/^(\w+)\s+(\S+?)\s+$/. Passing arguments to the run method is done as follows: "Net::Server-"run(key1 => 'val1')>. Passing arguments via a prebuilt object can best be shown in the following code:
#!/usr/bin/perl -w -T
package MyPackage; use strict; use base qw(Net::Server);
my $server = MyPackage->new({ key1 => 'val1', });
$server->run;
All five methods for passing arguments may be used at the same time. Once an argument has been set, it is not over written if another method passes the same argument. "Net::Server" will look for arguments in the following order:
1) Arguments passed to the C<new> method. 2) Arguments passed on command line. 3) Arguments passed to the C<run> method. 4) Arguments passed via a conf file. 5) Arguments set in the C<default_values> method.
Additionally the following hooks are available:
1) Arguments set in the configure_hook (occurs after new but before any of the other areas are checked). 2) Arguments set and validated in the post_configure_hook (occurs after all of the other areas are checked).
Each of these levels will override parameters of the same name specified in subsequent levels. For example, specifying --setsid=0 on the command line will override a value of ``setsid 1'' in the conf file.
Note that the configure_hook method doesn't return values to set, but is there to allow for setting up configured values before the configure method is called.
Key/value pairs used by the server are removed by the configuration process so that server layers on top of "Net::Server" can pass and read their own parameters.
sub options { my $self = shift; my $prop = $self->{'server'}; my $template = shift;
### setup options in the parent classes $self->SUPER::options($template);
### add a single value option $prop->{'my_option'} ||= undef; $template->{'my_option'} = \ $prop->{'my_option'};
### add a multi value option $prop->{'an_arrayref_item'} ||= []; $template->{'an_arrayref_item'} = $prop->{'an_arrayref_item'}; }
Overriding the "options" method allows for adding your own custom fields. A template hashref is passed in, that should then be modified to contain an of your custom fields. Fields which are intended to receive a single scalar value should have a reference to the destination scalar given. Fields which are intended to receive multiple values should reference the corresponding destination arrayref.
You are responsible for validating your custom options once they have been parsed. The post_configure_hook is a good place to do your validation.
Some emails have asked why we use this ``template'' method. The idea is that you are creating the the data structure to store the values in, and you are also creating a way to get the values into the data structure. The template is the way to get the values to the servers data structure. One of the possibilities (that probably isn't used that much) is that by letting you specify the mapping, you could build a nested data structure - even though the passed in arguments are flat. It also allows you to setup aliases to your names.
For example, a basic structure might look like this:
$prop = $self->{'server'}
$prop->{'my_custom_option'} ||= undef; $prop->{'my_custom_array'} ||= [];
$template = { my_custom_option => \ $prop->{'my_custom_option'}, mco => \ $prop->{'my_custom_option'}, # alias my_custom_array => $prop->{'my_custom_array'}, mca => $prop->{'my_custom_array'}, # an alias };
$template->{'mco2'} = $template->{'mco'}; # another way to alias
But you could also have more complex data:
$prop = $self->{'server'};
$prop->{'one_layer'} = { two_layer => [ undef, undef, ], };
$template = { param1 => \ $prop->{'one_layer'}->{'two_layer'}->[0], param2 => \ $prop->{'one_layer'}->{'two_layer'}->[1], };
This is of course a contrived example - but it does show that you can get the data from the flat passed in arguments to whatever type of structure you need - with only a little bit of effort.
Key Value Default conf_file "filename" undef
log_level 0-4 2 log_file (filename|Sys::Syslog) undef
## syslog parameters syslog_logsock (native|unix|inet|udp |tcp|stream|console) unix (on Sys::Syslog < 0.15) syslog_ident "identity" "net_server" syslog_logopt (cons|ndelay|nowait|pid) pid syslog_facility \w+ daemon
port \d+ 20203 host "host" "*" proto (tcp|udp|unix) "tcp" listen \d+ SOMAXCONN
reverse_lookups 1 undef allow /regex/ none deny /regex/ none cidr_allow CIDR none cidr_deny CIDR none
## daemonization parameters pid_file "filename" undef chroot "directory" undef user (uid|username) "nobody" group (gid|group) "nobody" background 1 undef setsid 1 undef
no_close_by_child (1|undef) undef
## See Net::Server::Proto::(TCP|UDP|UNIX|etc) ## for more sample parameters.
There are two ways that you can specify a default location for a conf_file. The first is to pass the default value to the run method as in:
MyServer->run({ conf_file => '/etc/my_server.conf', });
If the end user passes in --conf_file=/etc/their_server.conf then the value will be overridden.
The second way to do this was added in the 0.96 version. It uses the default_values method as in:
sub default_values { return { conf_file => '/etc/my_server.conf', } }
This method has the advantage of also being able to be overridden in the run method.
If you do not want the user to be able to specify a conf_file at all, you can pass conf_file to the new method when creating your object:
MyServer->new({ conf_file => '/etc/my_server.conf', })->run;
If passed this way, the value passed to new will ``win'' over any of the other passed in values.
See Sys::Syslog.
See L<IO::Socket>. Not used with udp protocol (or UNIX SOCK_DGRAM).
This option has no affect on STDIN and STDOUT which has a magic client property that is tied to the already open STDIN and STDOUT.
Children of a Fork server will exit after their current request. Children of a Prefork type server will finish the current request and then exit.
Note - the newly restarted parent will start up a fresh set of servers on fork servers. The new parent will attempt to keep track of the children from the former parent but custom communication channels (open pipes from the child to the old parent) will no longer be available to the old child processes. New child processes will still connect properly to the new parent.
The structure of a Net::Server object is shown below:
$self = bless( { 'server' => { 'key1' => 'val1', # more key/vals } }, 'Net::Server' );
This structure was chosen so that all server related properties are grouped under a single key of the object hashref. This is so that other objects could layer on top of the Net::Server object class and still have a fairly clean namespace in the hashref.
You may get and set properties in two ways. The suggested way is to access properties directly via
my $val = $self->{server}->{key1};
Accessing the properties directly will speed the server process - though some would deem this as bad style. A second way has been provided for object oriented types who believe in methods. The second way consists of the following methods:
my $val = $self->get_property( 'key1' ); my $self->set_property( key1 => 'val1' );
Properties are allowed to be changed at any time with caution (please do not undef the sock property or you will close the client connection).
#-------------- file test.conf --------------
### user and group to become user somebody group everybody
### logging ? log_file /var/log/server.log log_level 3 pid_file /tmp/server.pid
### optional syslog directive ### used in place of log_file above #log_file Sys::Syslog #syslog_logsock unix #syslog_ident myserver #syslog_logopt pid|cons
### access control allow .+\.(net|com) allow domain\.com deny a.+ cidr_allow 127.0.0.0/8 cidr_allow 192.0.2.0/24 cidr_deny 192.0.2.4/30
### background the process? background 1
### ports to bind (this should bind ### 127.0.0.1:20205 and localhost:20204) ### See Net::Server::Proto host 127.0.0.1 port localhost:20204 port 20205
### reverse lookups ? # reverse_lookups on
#-------------- file test.conf --------------
$self->configure_hook;
$self->configure(@_);
$self->post_configure;
$self->post_configure_hook;
$self->pre_bind;
$self->bind;
$self->post_bind_hook;
$self->post_bind;
$self->pre_loop_hook;
$self->loop;
### routines inside a standard $self->loop # $self->accept; # $self->run_client_connection; # $self->done;
$self->pre_server_close_hook;
$self->server_close;
The server then exits.
During the client processing phase ("$self->run_client_connection"), the following represents the program flow:
$self->post_accept;
$self->get_client_info;
$self->post_accept_hook;
if( $self->allow_deny
&& $self->allow_deny_hook ){
$self->process_request;
}else{
$self->request_denied_hook;
}
$self->post_process_request_hook;
$self->post_process_request;
$self->post_client_connection_hook;
The process then loops and waits for the next connection. For a more in depth discussion, please read the code.
During the server shutdown phase ("$self->server_close"), the following represents the program flow:
$self->close_children; # if any
$self->post_child_cleanup_hook;
if( Restarting server ){ $self->restart_close_hook(); $self->hup_server; }
$self->shutdown_sockets;
$self->server_exit;
The method run may be called in any of the following ways.
MyPackage->run(port => 20201);
MyPackage->new({port => 20201})->run;
my $obj = bless {server=>{port => 20201}}, 'MyPackage'; $obj->run;
The ->run method should typically be the last method called in a server start script (the server will exit at the end of the ->run method).
Under the Fork, PreFork, and PreFork simple personalities, these signals are registered using Net::Server::SIG to allow for safe signal handling.
Net::Server and Net::Server single accept only one connection at a time.
Net::Server::INET runs one connection and then exits (for use by inetd or xinetd daemons).
Net::Server::MultiPlex allows for one process to simultaneously handle multiple connections (but requires rewriting the process_request code to operate in a more ``packet-like'' manner).
Net::Server::Fork forks off a new child process for each incoming connection.
Net::Server::PreForkSimple starts up a fixed number of processes that all accept on incoming connections.
Net::Server::PreFork starts up a base number of child processes which all accept on incoming connections. The server throttles the number of processes running depending upon the number of requests coming in (similar to concept to how Apache controls its child processes in a PreFork server).
Read the documentation for each of the types for more information.
This method takes care of cleaning up any remaining child processes, setting appropriate flags on sockets (for HUPing), closing up logging, and then closing open sockets.
This is the main method to override.
The default method implements a simple echo server that will repeat whatever is sent. It will quit the child if ``quit'' is sent, and will exit the server if ``exit'' is sent.
Almost all of the default hook methods do nothing. To use a hook you simply need to override the method in your subclass. For example to add your own post_configure_hook you could do something like the following:
package MyServer;
sub post_configure_hook { my $self = shift; my $prop = $self->{'server'};
# do some validation here }
The following describes the hooks available in the plain Net::Server class (other flavors such as Fork or PreFork have additional hooks).
It is generally suggested that other avenues be pursued for sending messages via sockets not created by the Net::Server.
Should return a hashref.
sub default_values { return { port => 20201, }; }
If log_level is set to 'Sys::Syslog', the parameters may alternately be a log_level, a format string, and format string parameters. (The second parameter is assumed to be a format string if additional arguments are passed along). Passing arbitrary format strings to Sys::Syslog will allow the server to be vulnerable to exploit. The server maintainer should make sure that any string treated as a format string is controlled.
# assuming log_file = 'Sys::Syslog'
$self->log(1, "My Message with %s in it"); # sends "%s", "My Message with %s in it" to syslog
$self->log(1, "My Message with %s in it", "Foo"); # sends "My Message with %s in it", "Foo" to syslog
If log_file is set to a file (other than Sys::Syslog), the message will be appended to the log file by calling the write_to_log_hook.
If the log_file is Sys::Syslog and an error occurs during write, the handle_syslog_error method will be called and passed the error exception. The default option of handle_syslog_error is to die - but could easily be told to do nothing by using the following code in your subclassed server:
sub handle_syslog_error {}
It the log had been closed, you could attempt to reopen it in the error handler with the following code:
sub handle_syslog_error { my $self = shift; $self->open_syslog; }
package MyPackage; use base qw(Net::Server);
my $obj = MyPackage->new({port => 20201});
# same as
my $obj = bless {server => {port => 20201}}, 'MyPackage';
The Net::Server will attempt to find out the commandline used for starting the program. The attempt is made before any configuration files or other arguments are processed. The outcome of this attempt is stored using the method "->commandline". The stored commandline may also be retrieved using the same method name. The stored contents will undoubtedly contain Tainted items that will cause the server to die during a restart when using the -T flag (Taint mode). As it is impossible to arbitrarily decide what is taint safe and what is not, the individual program must clean up the tainted items before doing a restart.
sub configure_hook{ my $self = shift;
### see the contents my $ref = $self->commandline; use Data::Dumper; print Dumper $ref;
### arbitrary untainting - VERY dangerous my @untainted = map {/(.+)/;$1} @$ref;
$self->commandline(\@untainted) }
Net/Server.pm Net/Server/Fork.pm Net/Server/INET.pm Net/Server/MultiType.pm Net/Server/PreForkSimple.pm Net/Server/PreFork.pm Net/Server/Single.pm Net/Server/Daemonize.pm Net/Server/SIG.pm Net/Server/Proto.pm Net/Server/Proto/*.pm
perl Makefile.PL make make test make install
Thanks to Jonathan J. Miner <miner at doit.wisc.edu> for patching a blatant problem in the reverse lookups.
Thanks to Bennett Todd <bet at rahul.net> for pointing out a problem in Solaris 2.5.1 which does not allow multiple children to accept on the same port at the same time. Also for showing some sample code from Viktor Duchovni which now represents the semaphore option of the serialize argument in the PreFork server.
Thanks to traveler and merlyn from http://perlmonks.org for pointing me in the right direction for determining the protocol used on a socket connection.
Thanks to Jeremy Howard <j+daemonize at howard.fm> for numerous suggestions and for work on Net::Server::Daemonize.
Thanks to Vadim <vadim at hardison.net> for patches to implement parent/child communication on PreFork.pm.
Thanks to Carl Lewis for suggesting ``-'' in user names.
Thanks to Slaven Rezic for suggesing Reuse => 1 in Proto::UDP.
Thanks to Tim Watt for adding udp_broadcast to Proto::UDP.
Thanks to Christopher A Bongaarts for pointing out problems with the Proto::SSL implementation that currently locks around the socket accept and the SSL negotiation. See Net::Server::Proto::SSL.
Thanks to Alessandro Zummo for pointing out various bugs including some in configuration, commandline args, and cidr_allow.
Thanks to various other people for bug fixes over the years. These and future thank-you's are available in the Changes file as well as CVS comments.
Thanks to Ben Cohen and tye (on Permonks) for finding and diagnosing more correct behavior for dealing with re-opening STDIN and STDOUT on the client handles.
Thanks to Mark Martinec for trouble shooting other problems with STDIN and STDOUT (he proposed having a flag that is now the no_client_stdout flag).
Thanks to David (DSCHWEI) on cpan for asking for the nofatal option with syslog.
Thanks to Andreas Kippnick and Peter Beckman for suggesting leaving open child connections open during a HUP (this is now available via the leave_children_open_on_hup flag).
Thanks to LUPE on cpan for helping patch HUP with taint on.
Thanks to Michael Virnstein for fixing a bug in the check_for_dead section of PreFork server.
Thanks to Rob Mueller for patching PreForkSimple to only open lock_file once during parent call. This patch should be portable on systems supporting flock. Rob also suggested not closing STDIN/STDOUT but instead reopening them to /dev/null to prevent spurious warnings. Also suggested short circuit in post_accept if in UDP. Also for cleaning up some of the child managment code of PreFork.
Thanks to Mark Martinec for suggesting additional log messages for failure during accept.
Thanks to Bill Nesbitt and Carlos Velasco for pointing out double decrement bug in PreFork.pm (rt #21271)
Thanks to John W. Krahn for pointing out glaring precended with non-parened open and ||.
Thanks to Ricardo Signes for pointing out setuid bug for perl 5.6.1 (rt #21262).
Thanks to Carlos Velasco for updating the Syslog options (rt #21265). And for additional fixes later.
Thanks to Steven Lembark for pointing out that no_client_stdout wasn't working with the Multiplex server.
Thanks to Peter Beckman for suggesting allowing Sys::SysLog keyworks be passed through the ->log method and for suggesting we allow more types of characters through in syslog_ident. Also to Peter Beckman for pointing out that a poorly setup localhost will cause tests to hang.
Thanks to Curtis Wilbar for pointing out that the Fork server called post_accept_hook twice. Changed to only let the child process call this, but added the pre_fork_hook method.
And just a general Thanks You to everybody who is using Net::Server or who has contributed fixes over the years.
Thanks to Paul Miller for some ->autoflush, FileHandle fixes.
Thanks to Patrik Wallstrom for suggesting handling syslog errors better.
Thanks again to Rob Mueller for more logic cleanup for child accounting in PreFork server.
Thanks to David Schweikert for suggesting handling setlogsock a little better on newer versions of Sys::Syslog (>= 0.15).
Thanks to Mihail Nasedkin for suggesting adding a hook that is now called post_client_connection_hook.
Improve test suite to fully cover code (using Devel::Cover). Anybody that wanted to send me patches to the t/*.t tests that improved coverage would earn a big thank you :) (Sorry there isn't a whole lot more than that to give).
Paul Seamons <paul at seamons.com> http://seamons.com/
Rob Brown <bbb at cpan.org>
GNU General Public License or the Perl Artistic License
All rights reserved.