| 1 | # $Id: UDP.pm 449 2004-12-27 01:37:33Z sungo $ | ||||||||||||||||||||||||||||
| 2 | package POE::Component::Server::Syslog::UDP; | ||||||||||||||||||||||||||||
| 3 | 6 | $POE::Component::Server::Syslog::UDP::VERSION = '1.22'; | |||||||||||||||||||||||||||
| 4 | #ABSTRACT: syslog udp 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 | |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use POE; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use POE::Filter::Syslog; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 6 | ▶ | use Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 6 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | sub spawn { | ||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 3 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my %args = validate_with( | ||||||||||||||||||||||||||||
| 27 | params => \@_, | ||||||||||||||||||||||||||||
| 28 | spec => { | ||||||||||||||||||||||||||||
| 29 | InputState => { | ||||||||||||||||||||||||||||
| 30 | type => &Params::Validate::CODEREF, | ||||||||||||||||||||||||||||
| 31 | optional => 1, | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | default => sub {}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||
| 34 | ErrorState => { | ||||||||||||||||||||||||||||
| 35 | type => &Params::Validate::CODEREF, | ||||||||||||||||||||||||||||
| 36 | optional => 1, | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | default => sub {}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||
| 39 | 3 | BindAddress => { | |||||||||||||||||||||||||||
| 40 | type => &Params::Validate::SCALAR, | ||||||||||||||||||||||||||||
| 41 | optional => 1, | ||||||||||||||||||||||||||||
| 42 | default => BINDADDR, | ||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | BindPort => { | ||||||||||||||||||||||||||||
| 45 | type => &Params::Validate::SCALAR, | ||||||||||||||||||||||||||||
| 46 | optional => 1, | ||||||||||||||||||||||||||||
| 47 | default => BINDPORT, | ||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | MaxLen => { | ||||||||||||||||||||||||||||
| 50 | type => &Params::Validate::SCALAR, | ||||||||||||||||||||||||||||
| 51 | optional => 1, | ||||||||||||||||||||||||||||
| 52 | default => DATAGRAM_MAXLEN, | ||||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||
| 54 | Alias => { | ||||||||||||||||||||||||||||
| 55 | type => &Params::Validate::SCALAR, | ||||||||||||||||||||||||||||
| 56 | optional => 1, | ||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | $args{type} = 'udp'; | |||||||||||||||||||||||||||
| 62 | 3 | $args{filter} = POE::Filter::Syslog->new(); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | my $sess = POE::Session->create( | ||||||||||||||||||||||||||||
| 65 | inline_states => { | ||||||||||||||||||||||||||||
| 66 | _start => \&socket_start, | ||||||||||||||||||||||||||||
| 67 | _stop => \&shutdown, | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | select_read => \&select_read, | ||||||||||||||||||||||||||||
| 70 | register => \®ister, | ||||||||||||||||||||||||||||
| 71 | unregister => \&unregister, | ||||||||||||||||||||||||||||
| 72 | shutdown => \&shutdown, | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | client_input => $args{InputState}, | ||||||||||||||||||||||||||||
| 75 | client_error => $args{ErrorState}, | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | }, | ||||||||||||||||||||||||||||
| 78 | 3 | heap => \%args, | |||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | return $sess; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # This is a really good spot to discuss why this is using IO::Socket | ||||||||||||||||||||||||||||
| 86 | # instead of a POE wheel of some variety for this. The answer, for once | ||||||||||||||||||||||||||||
| 87 | # in my life, is pretty simple. POE::Wheel::SocketFactory doesn't support | ||||||||||||||||||||||||||||
| 88 | # connectionless sockets as of the time of writing. In this scenario, | ||||||||||||||||||||||||||||
| 89 | # there is no chance of IO::Socket blocking, unless IO::Socket decides | ||||||||||||||||||||||||||||
| 90 | # to lose its mind. If it does THAT, there's not a whole hell of a lot | ||||||||||||||||||||||||||||
| 91 | # left that's right in the world. :) except maybe pizza. well, good | ||||||||||||||||||||||||||||
| 92 | # pizza like you find at Generous George's in Alexandria, VA. and rum. | ||||||||||||||||||||||||||||
| 93 | # pretty much any rum. Um, but anyway... | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 3 | ▶ | sub socket_start { | ||||||||||||||||||||||||||
|
Subroutine
socket_start: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | $_[HEAP]->{handle} = IO::Socket::INET->new( | ||||||||||||||||||||||||||||
| 97 | Blocking => 0, | ||||||||||||||||||||||||||||
| 98 | LocalAddr => $_[HEAP]->{BindAddress}, | ||||||||||||||||||||||||||||
| 99 | LocalPort => $_[HEAP]->{BindPort}, | ||||||||||||||||||||||||||||
| 100 | 3 | Proto => 'udp', | |||||||||||||||||||||||||||
| 101 | ReuseAddr => 1, | ||||||||||||||||||||||||||||
| 102 | SocketType => SOCK_DGRAM, | ||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | ▶ | if (defined $_[HEAP]->{handle}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 3 | $_[KERNEL]->select_read( $_[HEAP]->{handle}, 'select_read' ); | |||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 0 | croak "Unable to create UDP Listener: $!"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 3 | ▶ | $_[KERNEL]->alias_set( $_[HEAP]->{Alias} ) if $_[HEAP]->{Alias}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 3 | return; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 3 | ▶ | sub select_read { | ||||||||||||||||||||||||||
|
Subroutine
select_read: called CC=11 SCAR=26.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 3 | my $message; | |||||||||||||||||||||||||||
| 116 | 3 | my $remote_socket = $_[HEAP]->{handle}->recv($message, $_[HEAP]->{MaxLen}, 0 ); | |||||||||||||||||||||||||||
| 117 | 3 | ▶ | if (defined $message) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 3 | $_[HEAP]->{filter}->get_one_start([ $message ]); | |||||||||||||||||||||||||||
| 119 | 3 | my $records = []; | |||||||||||||||||||||||||||
| 120 | 3 | ▶ | while( ($records = $_[HEAP]->{filter}->get_one()) and (@$records > 0)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$records = $_[3]{"filter"}->get_one and @$records > 0
MC/DC50%1/2
$records = $_[3]{"filter"}->get_one and @$records > 0
$records = $_[3]{"filter"}->get_one
@$records > 0
Truth table66%2/3
A and B
A= $records = $_[3]{"filter"}->get_oneB= @$records > 0
| |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | if(defined $records and ref $records eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $records and ref $records eq "ARRAY"
MC/DC0%0/2
defined $records and ref $records eq "ARRAY"
defined $records
ref $records eq "ARRAY"
Truth table33%1/3
A and B
A= defined $recordsB= ref $records eq "ARRAY"
| |||||||||||||||||||||||||||||
| 122 | 3 | foreach my $record (@$records) { | |||||||||||||||||||||||||||
| 123 | 3 | ▶ | if (my $addr = (sockaddr_in($remote_socket))[1]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 3 | $record->{addr} = inet_ntoa($addr); | |||||||||||||||||||||||||||
| 125 | 3 | ▶ | if (my $host = gethostbyaddr($addr, AF_INET)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 3 | $record->{host} = $host; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | $_[KERNEL]->yield( 'client_input', $record ); | |||||||||||||||||||||||||||
| 131 | $_[KERNEL]->post( $_, $_[HEAP]->{sessions}->{$_}->{inputevent}, $record ) | ||||||||||||||||||||||||||||
| 132 | 3 | for keys %{ $_[HEAP]->{sessions} }; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } else { | ||||||||||||||||||||||||||||
| 135 | 0 | $_[KERNEL]->yield( 'client_error', $message ); | |||||||||||||||||||||||||||
| 136 | $_[KERNEL]->post( $_, $_[HEAP]->{sessions}->{$_}->{errorevent}, $message ) | ||||||||||||||||||||||||||||
| 137 | 0 | for grep { defined $_[HEAP]->{sessions}->{errorevent} } | |||||||||||||||||||||||||||
| 138 | 0 | keys %{ $_[HEAP]->{sessions} }; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 3 | return; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 6 | ▶ | sub shutdown { | ||||||||||||||||||||||||||
|
Subroutine
shutdown: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 6 | my ($kernel,$heap) = @_[KERNEL,HEAP]; | |||||||||||||||||||||||||||
| 147 | 6 | ▶ | if($heap->{handle}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 3 | $kernel->select_read($heap->{handle}); | |||||||||||||||||||||||||||
| 149 | 3 | $heap->{handle}->close(); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 6 | delete $heap->{handle}; | |||||||||||||||||||||||||||
| 152 | 6 | $kernel->alarm_remove_all(); | |||||||||||||||||||||||||||
| 153 | 6 | $kernel->alias_remove( $_ ) for $kernel->alias_list(); | |||||||||||||||||||||||||||
| 154 | $kernel->refcount_decrement( $_, __PACKAGE__ ) | ||||||||||||||||||||||||||||
| 155 | 6 | for keys %{ $heap->{sessions} }; | |||||||||||||||||||||||||||
| 156 | 6 | return; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=6 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 1 | my ($kernel,$self,$sender) = @_[KERNEL,HEAP,SENDER]; | |||||||||||||||||||||||||||
| 161 | 1 | my $sender_id = $sender->ID(); | |||||||||||||||||||||||||||
| 162 | 1 | my %args; | |||||||||||||||||||||||||||
| 163 | 1 | ▶ | if ( ref $_[ARG0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 164 | 1 | %args = %{ $_[ARG0] }; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | elsif ( ref $_[ARG0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 167 | 0 | %args = @{ $_[ARG0] }; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 0 | %args = @_[ARG0..$#_]; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 1 | $args{lc $_} = delete $args{$_} for keys %args; | |||||||||||||||||||||||||||
| 173 | 1 | ▶ | unless ( $args{inputevent} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | warn "No 'inputevent' argument supplied\n"; | |||||||||||||||||||||||||||
| 175 | 0 | return; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 1 | ▶ | if ( defined $self->{sessions}->{ $sender_id } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | $self->{sessions}->{ $sender_id } = \%args; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||
| 181 | 1 | $self->{sessions}->{ $sender_id } = \%args; | |||||||||||||||||||||||||||
| 182 | 1 | $kernel->refcount_increment( $sender_id, __PACKAGE__ ); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 1 | return; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub unregister { | ||||||||||||||||||||||||||
|
Subroutine
unregister: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 0 | my ($kernel,$self,$sender) = @_[KERNEL,HEAP,SENDER]; | |||||||||||||||||||||||||||
| 189 | 0 | my $sender_id = $sender->ID(); | |||||||||||||||||||||||||||
| 190 | 0 | my %args; | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | if ( ref $_[ARG0] eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 192 | 0 | %args = %{ $_[ARG0] }; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | elsif ( ref $_[ARG0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 195 | 0 | %args = @{ $_[ARG0] }; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||
| 198 | 0 | %args = @_[ARG0..$#_]; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 0 | $args{lc $_} = delete $args{$_} for keys %args; | |||||||||||||||||||||||||||
| 201 | 0 | my $data = delete $self->{sessions}->{ $sender_id }; | |||||||||||||||||||||||||||
| 202 | 0 | ▶ | $kernel->refcount_decrement( $sender_id, __PACKAGE__ ) if $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | return; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 6 | 1; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # sungo // vim: ts=4 sw=4 noexpandtab | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||||||||||