| 1 | # $Id: TCP.pm 446 2004-12-27 00:57:57Z sungo $ | ||||||||||||||||||||||||||||||||
| 2 | package POE::Component::Server::Syslog::TCP; | ||||||||||||||||||||||||||||||||
| 3 | 6 | $POE::Component::Server::Syslog::TCP::VERSION = '1.22'; | |||||||||||||||||||||||||||||||
| 4 | #ABSTRACT: syslog tcp server | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | sub BINDADDR () { '0.0.0.0' } | ||||||||||||||||||||||||||||||||
| 10 | sub BINDPORT () { 514 } | ||||||||||||||||||||||||||||||||
| 11 | sub DATAGRAM_MAXLEN () { 1024 } # syslogd defaults to this. as do most | ||||||||||||||||||||||||||||||||
| 12 | # libc implementations of syslog | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Params::Validate qw(validate_with); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Socket; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use POE qw( | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | Driver::SysRW | ||||||||||||||||||||||||||||||||
| 20 | Wheel::SocketFactory | ||||||||||||||||||||||||||||||||
| 21 | Wheel::ReadWrite | ||||||||||||||||||||||||||||||||
| 22 | Filter::Syslog | ||||||||||||||||||||||||||||||||
| 23 | 6 | ); | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | sub spawn { | ||||||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 27 | 3 | my $class = shift; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | my %args = validate_with( | ||||||||||||||||||||||||||||||||
| 30 | params => \@_, | ||||||||||||||||||||||||||||||||
| 31 | spec => { | ||||||||||||||||||||||||||||||||
| 32 | InputState => { | ||||||||||||||||||||||||||||||||
| 33 | type => &Params::Validate::CODEREF, | ||||||||||||||||||||||||||||||||
| 34 | optional => 1, | ||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | default => sub {}, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||
| 37 | ErrorState => { | ||||||||||||||||||||||||||||||||
| 38 | type => &Params::Validate::CODEREF, | ||||||||||||||||||||||||||||||||
| 39 | optional => 1, | ||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | default => sub {}, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||||||
| 42 | 3 | BindAddress => { | |||||||||||||||||||||||||||||||
| 43 | type => &Params::Validate::SCALAR, | ||||||||||||||||||||||||||||||||
| 44 | optional => 1, | ||||||||||||||||||||||||||||||||
| 45 | default => BINDADDR, | ||||||||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||||||
| 47 | BindPort => { | ||||||||||||||||||||||||||||||||
| 48 | type => &Params::Validate::SCALAR, | ||||||||||||||||||||||||||||||||
| 49 | optional => 1, | ||||||||||||||||||||||||||||||||
| 50 | default => BINDPORT, | ||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||
| 52 | MaxLen => { | ||||||||||||||||||||||||||||||||
| 53 | type => &Params::Validate::SCALAR, | ||||||||||||||||||||||||||||||||
| 54 | optional => 1, | ||||||||||||||||||||||||||||||||
| 55 | default => DATAGRAM_MAXLEN, | ||||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||||
| 57 | Alias => { | ||||||||||||||||||||||||||||||||
| 58 | type => &Params::Validate::SCALAR, | ||||||||||||||||||||||||||||||||
| 59 | optional => 1, | ||||||||||||||||||||||||||||||||
| 60 | }, | ||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 3 | $args{type} = 'tcp'; | |||||||||||||||||||||||||||||||
| 65 | 3 | $args{filter} = POE::Filter::Syslog->new(); | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | my $sess = POE::Session->create( | ||||||||||||||||||||||||||||||||
| 68 | inline_states => { | ||||||||||||||||||||||||||||||||
| 69 | _start => \&start, | ||||||||||||||||||||||||||||||||
| 70 | _stop => \&shutdown, | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | socket_connect => \&socket_connect, | ||||||||||||||||||||||||||||||||
| 73 | socket_error => \&socket_error, | ||||||||||||||||||||||||||||||||
| 74 | socket_input => \&socket_input, | ||||||||||||||||||||||||||||||||
| 75 | register => \®ister, | ||||||||||||||||||||||||||||||||
| 76 | unregister => \&unregister, | ||||||||||||||||||||||||||||||||
| 77 | shutdown => \&shutdown, | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | client_input => $args{InputState}, | ||||||||||||||||||||||||||||||||
| 80 | client_error => $args{ErrorState}, | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||||||
| 83 | 3 | heap => \%args, | |||||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 3 | return $sess; | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 3 | ▶ | sub start { | ||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 91 | $_[HEAP]->{socketfactory} = POE::Wheel::SocketFactory->new( | ||||||||||||||||||||||||||||||||
| 92 | BindAddress => $_[HEAP]->{BindAddress}, | ||||||||||||||||||||||||||||||||
| 93 | BindPort => $_[HEAP]->{BindPort}, | ||||||||||||||||||||||||||||||||
| 94 | SuccessEvent => 'socket_connect', | ||||||||||||||||||||||||||||||||
| 95 | FailureEvent => 'client_error', | ||||||||||||||||||||||||||||||||
| 96 | ListenQueue => $_[HEAP]->{MaxLen}, | ||||||||||||||||||||||||||||||||
| 97 | 3 | Reuse => 'yes', | |||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 3 | ▶ | unless($_[HEAP]->{socketfactory}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | 0 | croak("Unable to setup socketfactory"); | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | $_[KERNEL]->alias_set( $_[HEAP]->{Alias} ) if $_[HEAP]->{Alias}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | 3 | return; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 3 | ▶ | sub socket_connect { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_connect: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 108 | 3 | my $handle = $_[ARG0]; | |||||||||||||||||||||||||||||||
| 109 | 3 | my $host; | |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | if( ( sockaddr_in( getpeername($handle) ) )[1]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 3 | $host = gethostbyaddr( ( sockaddr_in( getpeername($handle) ) )[1], AF_INET ); | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||
| 115 | 0 | $host = '[unknown]'; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 3 | my $wheel = POE::Wheel::ReadWrite->new( | |||||||||||||||||||||||||||||||
| 119 | Handle => $handle, | ||||||||||||||||||||||||||||||||
| 120 | Driver => POE::Driver::SysRW->new(), | ||||||||||||||||||||||||||||||||
| 121 | Filter => POE::Filter::Syslog->new(), | ||||||||||||||||||||||||||||||||
| 122 | InputEvent => 'socket_input', | ||||||||||||||||||||||||||||||||
| 123 | ErrorEvent => 'socket_error', | ||||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 3 | $_[HEAP]->{wheels}->{ $wheel->ID } = { | |||||||||||||||||||||||||||||||
| 127 | wheel => $wheel, | ||||||||||||||||||||||||||||||||
| 128 | host => $host, | ||||||||||||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||||||||||||
| 130 | 3 | return; | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub socket_error { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_error: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 134 | 0 | my ($errop, $errnum, $errstr, $wid) = @_[ARG0 .. ARG3]; | |||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | unless( ($errnum == 0) && ($errop eq 'read') ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$errnum == 0 and $errop eq "read"
MC/DC0%0/2
$errnum == 0 and $errop eq "read"
$errnum == 0
$errop eq "read"
Truth table0%0/3
A and B
A= $errnum == 0B= $errop eq "read"
| |||||||||||||||||||||||||||||||||
| 136 | 0 | $_[KERNEL]->yield( 'client_error', $errop, $errnum, $errstr ); | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | 0 | delete $_[HEAP]->{wheels}->{ $wid }; | |||||||||||||||||||||||||||||||
| 139 | 0 | return; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 3 | ▶ | sub socket_input { | ||||||||||||||||||||||||||||||
|
Subroutine
socket_input: called CC=8 SCAR=28.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 143 | 3 | my ($input, $wid) = @_[ARG0, ARG1]; | |||||||||||||||||||||||||||||||
| 144 | 3 | my $info = $_[HEAP]->{wheels}->{ $wid }; | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 3 | ▶ | if(ref $input && ref $input eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
ref $input and ref $input eq "ARRAY"
MC/DC0%0/2
ref $input and ref $input eq "ARRAY"
ref $input
ref $input eq "ARRAY"
Truth table33%1/3
A and B
A= ref $inputB= ref $input eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 147 | 0 | foreach my $record (@{ $input }) { | |||||||||||||||||||||||||||||||
| 148 | 0 | $input->{host} = $info->{host}; | |||||||||||||||||||||||||||||||
| 149 | 0 | $_[KERNEL]->yield( 'client_input', $record ); | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | ▶ | elsif(ref $input && ref $input eq 'HASH') { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $input and ref $input eq "HASH"
MC/DC0%0/2
ref $input and ref $input eq "HASH"
ref $input
ref $input eq "HASH"
Truth table33%1/3
A and B
A= ref $inputB= ref $input eq "HASH"
| |||||||||||||||||||||||||||||||||
| 153 | 3 | $input->{host} = $info->{host}; | |||||||||||||||||||||||||||||||
| 154 | 3 | $_[KERNEL]->yield( 'client_input', $input ); | |||||||||||||||||||||||||||||||
| 155 | $_[KERNEL]->post( $_, $_[HEAP]->{sessions}->{$_}->{inputevent}, $input ) | ||||||||||||||||||||||||||||||||
| 156 | 3 | for keys %{ $_[HEAP]->{sessions} }; | |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | else { | ||||||||||||||||||||||||||||||||
| 159 | 0 | $_[KERNEL]->yield( 'client_error', $input ); | |||||||||||||||||||||||||||||||
| 160 | $_[KERNEL]->post( $_, $_[HEAP]->{sessions}->{$_}->{errorevent}, $input ) | ||||||||||||||||||||||||||||||||
| 161 | 0 | for grep { defined $_[HEAP]->{sessions}->{errorevent} } | |||||||||||||||||||||||||||||||
| 162 | 0 | keys %{ $_[HEAP]->{sessions} }; | |||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | 3 | return; | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 6 | ▶ | sub shutdown { | ||||||||||||||||||||||||||||||
|
Subroutine
shutdown: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 168 | 6 | my ($kernel,$heap) = @_[KERNEL,HEAP]; | |||||||||||||||||||||||||||||||
| 169 | 6 | ▶ | if($heap->{socketfactory}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | 3 | $heap->{socketfactory}->pause_accept(); | |||||||||||||||||||||||||||||||
| 171 | 3 | delete $heap->{socketfactory}; | |||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | 6 | delete $heap->{wheels}; | |||||||||||||||||||||||||||||||
| 174 | 6 | $kernel->alarm_remove_all(); | |||||||||||||||||||||||||||||||
| 175 | 6 | $kernel->alias_remove( $_ ) for $kernel->alias_list(); | |||||||||||||||||||||||||||||||
| 176 | $kernel->refcount_decrement( $_, __PACKAGE__ ) | ||||||||||||||||||||||||||||||||
| 177 | 6 | for keys %{ $heap->{sessions} }; | |||||||||||||||||||||||||||||||
| 178 | 6 | return; | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=6 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 182 | 1 | my ($kernel,$self,$sender) = @_[KERNEL,HEAP,SENDER]; | |||||||||||||||||||||||||||||||
| 183 | 1 | my $sender_id = $sender->ID(); | |||||||||||||||||||||||||||||||
| 184 | 1 | my %args; | |||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | if ( ref $_[ARG0] eq 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 186 | 1 | %args = %{ $_[ARG0] }; | |||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | elsif ( ref $_[ARG0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||
| 189 | 0 | %args = @{ $_[ARG0] }; | |||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||
| 191 | else { | ||||||||||||||||||||||||||||||||
| 192 | 0 | %args = @_[ARG0..$#_]; | |||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||
| 194 | 1 | $args{lc $_} = delete $args{$_} for keys %args; | |||||||||||||||||||||||||||||||
| 195 | 1 | ▶ | unless ( $args{inputevent} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | 0 | warn "No 'inputevent' argument supplied\n"; | |||||||||||||||||||||||||||||||
| 197 | 0 | return; | |||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | 1 | ▶ | if ( defined $self->{sessions}->{ $sender_id } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 200 | 0 | $self->{sessions}->{ $sender_id } = \%args; | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||
| 202 | else { | ||||||||||||||||||||||||||||||||
| 203 | 1 | $self->{sessions}->{ $sender_id } = \%args; | |||||||||||||||||||||||||||||||
| 204 | 1 | $kernel->refcount_increment( $sender_id, __PACKAGE__ ); | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | 1 | return; | |||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub unregister { | ||||||||||||||||||||||||||||||
|
Subroutine
unregister: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 210 | 0 | my ($kernel,$self,$sender) = @_[KERNEL,HEAP,SENDER]; | |||||||||||||||||||||||||||||||
| 211 | 0 | my $sender_id = $sender->ID(); | |||||||||||||||||||||||||||||||
| 212 | 0 | my %args; | |||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | if ( ref $_[ARG0] eq 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 214 | 0 | %args = %{ $_[ARG0] }; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | elsif ( ref $_[ARG0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||
| 217 | 0 | %args = @{ $_[ARG0] }; | |||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||||||||||||||
| 220 | 0 | %args = @_[ARG0..$#_]; | |||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | 0 | $args{lc $_} = delete $args{$_} for keys %args; | |||||||||||||||||||||||||||||||
| 223 | 0 | my $data = delete $self->{sessions}->{ $sender_id }; | |||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | $kernel->refcount_decrement( $sender_id, __PACKAGE__ ) if $data; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 225 | 0 | return; | |||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | 6 | 1; | |||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | # sungo // vim: ts=4 sw=4 noexpandtab | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | __END__ | ||||||||||||||||||||||||||||||||