| 1 | # outgoing connection to another server, including setting up dialback secret | ||||||||||||||||||||||||||||
| 2 | package DJabberd::Connection::ServerOut; | ||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use base 'DJabberd::Connection'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use fields ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 20 | 'state', | |||||||||||||||||||||||||||
| 7 | 'queue', # our DJabberd::Queue::ServerOut | ||||||||||||||||||||||||||||
| 8 | 20 | ); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 20 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 20 | ▶ | use Socket qw(PF_INET IPPROTO_TCP SOCK_STREAM); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 20 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 0 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | my $ip = delete $opts{ip}; | |||||||||||||||||||||||||||
| 18 | 0 | my $endpt = delete $opts{endpoint}; | |||||||||||||||||||||||||||
| 19 | 0 | ▶ | my $queue = delete $opts{queue} or croak "no queue"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | die "unknown options" if %opts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | croak "No 'ip' or 'endpoint'\n" unless $ip || $endpt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ip or $endpt
MC/DC0%0/2
$ip or $endpt
$ip
$endpt
Truth table0%0/3
A or B
A= $ipB= $endpt
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | $endpt ||= DJabberd::IPEndpoint->new($ip, 5269); | ||||||||||||||||||||||||||
|
Condition0%0/3
$endpt ||= "DJabberd::IPEndpoint"->new($ip, 5269)
MC/DC0%0/2
$endpt ||= "DJabberd::IPEndpoint"->new($ip, 5269)
$endpt
"DJabberd::IPEndpoint"->new($ip, 5269)
Truth table0%0/3
A ||= B
A= $endptB= "DJabberd::IPEndpoint"->new($ip, 5269)
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my $sock; | |||||||||||||||||||||||||||
| 26 | 0 | socket $sock, PF_INET, SOCK_STREAM, IPPROTO_TCP; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | unless ($sock && defined fileno($sock)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sock and defined fileno $sock
MC/DC0%0/2
$sock and defined fileno $sock
$sock
defined fileno $sock
Truth table0%0/3
A and B
A= $sockB= defined fileno $sock
| |||||||||||||||||||||||||||||
| 28 | 0 | $queue->on_connection_failed("Cannot alloc socket"); | |||||||||||||||||||||||||||
| 29 | 0 | return; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 0 | IO::Handle::blocking($sock, 0); | |||||||||||||||||||||||||||
| 32 | 0 | $ip = $endpt->addr; | |||||||||||||||||||||||||||
| 33 | 0 | connect $sock, Socket::sockaddr_in($endpt->port, Socket::inet_aton($ip)); | |||||||||||||||||||||||||||
| 34 | 0 | $DJabberd::Stats::counter{connect}++; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | my $self = $class->SUPER::new($sock, $queue->vhost->server); | |||||||||||||||||||||||||||
| 37 | 0 | $self->log->debug("Connecting to '$ip' for '$queue->{domain}'"); | |||||||||||||||||||||||||||
| 38 | 0 | $self->{state} = "connecting"; | |||||||||||||||||||||||||||
| 39 | 0 | $self->{queue} = $queue; | |||||||||||||||||||||||||||
| 40 | 0 | $self->{vhost} = $queue->vhost; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | Scalar::Util::weaken($self->{queue}); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | return $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub namespace { | ||||||||||||||||||||||||||
|
Subroutine
namespace: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | return "jabber:server"; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub start_connecting { | ||||||||||||||||||||||||||
|
Subroutine
start_connecting: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | 0 | $self->watch_write(1); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub on_connected { | ||||||||||||||||||||||||||
|
Subroutine
on_connected: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 58 | $self->start_init_stream(extra_attr => "xmlns:db='jabber:server:dialback'", | ||||||||||||||||||||||||||||
| 59 | 0 | to => $self->{queue}->{domain}); | |||||||||||||||||||||||||||
| 60 | 0 | $self->watch_read(1); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub event_write { | ||||||||||||||||||||||||||
|
Subroutine
event_write: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ($self->{state} eq "connecting") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $self->{state} = "connected"; | |||||||||||||||||||||||||||
| 68 | 0 | $self->on_connected; | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 0 | return $self->SUPER::event_write; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub on_stream_start { | ||||||||||||||||||||||||||
|
Subroutine
on_stream_start: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 0 | my ($self, $ss) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | $self->{in_stream} = 1; | |||||||||||||||||||||||||||
| 78 | 0 | $self->log->debug("We got a stream back from connection $self->{id}!\n"); | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | unless ($ss->announced_dialback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | $self->log->warn("Connection $self->{id} doesn't support dialback, failing"); | |||||||||||||||||||||||||||
| 81 | 0 | $self->{queue}->on_connection_failed($self, "no dialback"); | |||||||||||||||||||||||||||
| 82 | 0 | return; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | $self->log->debug("Connection $self->{id} supports dialback"); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $vhost = $self->{queue}->vhost; | |||||||||||||||||||||||||||
| 88 | 0 | my $orig_server = $vhost->name; | |||||||||||||||||||||||||||
| 89 | 0 | my $recv_server = $self->{queue}->domain; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $db_params = DJabberd::DialbackParams->new( | |||||||||||||||||||||||||||
| 92 | id => $ss->id, | ||||||||||||||||||||||||||||
| 93 | recv => $recv_server, | ||||||||||||||||||||||||||||
| 94 | orig => $orig_server, | ||||||||||||||||||||||||||||
| 95 | vhost => $vhost, | ||||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $db_params->generate_dialback_result(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | 0 | my $res = shift; | |||||||||||||||||||||||||||
| 99 | 0 | $self->log->debug("$self->{id} sending res '$res'"); | |||||||||||||||||||||||||||
| 100 | 0 | $self->write(qq{<db:result to='$recv_server' from='$orig_server'>$res</db:result>}); | |||||||||||||||||||||||||||
| 101 | 0 | }); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub on_stanza_received { | ||||||||||||||||||||||||||
|
Subroutine
on_stanza_received: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 0 | my ($self, $node) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($self->xmllog->is_info) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $self->log_incoming_data($node); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # we only deal with dialback verifies here. kinda ghetto | ||||||||||||||||||||||||||||
| 113 | # don't make a Stanza::DialbackVerify, maybe we should. | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | unless ($node->element eq "{jabber:server:dialback}result") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | return $self->SUPER::process_incoming_stanza_from_s2s_out($node); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | unless ($node->attr("{}type") eq "valid") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | # FIXME: also verify other attributes | ||||||||||||||||||||||||||||
| 120 | 0 | warn "Not valid?\n"; | |||||||||||||||||||||||||||
| 121 | 0 | return; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | $self->log->debug("Connection $self->{id} established"); | |||||||||||||||||||||||||||
| 125 | 0 | $self->{queue}->on_connection_connected($self); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub event_err { | ||||||||||||||||||||||||||
|
Subroutine
event_err: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 130 | 0 | $self->{queue}->on_connection_error($self); | |||||||||||||||||||||||||||
| 131 | 0 | return $self->SUPER::event_err; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub event_hup { | ||||||||||||||||||||||||||
|
Subroutine
event_hup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | 0 | return $self->event_err; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | return if $self->{closed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | $self->{queue}->on_connection_error($self); | |||||||||||||||||||||||||||
| 144 | 0 | return $self->SUPER::close; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 20 | 1; | |||||||||||||||||||||||||||