DJabberd.pm

filter: Statementstmt 36.4% 143 / 392 Branchbran 2.8% 4 / 140 Conditioncond 14.2% 2 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.0% 44 / 80 Podpod 0.0% 0 / 36 totaltotal 28.8% 193 / 670 CC 82 SCAR 49.5
CC
82
Cov
27%
_load_config_ref
756.0
run
72.0
_start_server
72.0
CRAP 141.5
↑ Summary DJabberd/Connection.pm »
1 #!/usr/bin/perl
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Danga::Socket 1.51;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶use DJabberd::VHost;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use DJabberd::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use DJabberd::HookDocs;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use DJabberd::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use DJabberd::Connection::ServerIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use DJabberd::Connection::ClientIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use DJabberd::Connection::ClusterIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use DJabberd::Connection::OldSSLClientIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 ▶use DJabberd::Connection::Admin;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 20 ▶use DJabberd::Stanza::StartTLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 20 ▶use DJabberd::Stanza::SASL;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 20 ▶use DJabberd::Stanza::StreamFeatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 20 ▶use DJabberd::Stanza::DialbackVerify;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 20 ▶use DJabberd::Stanza::DialbackResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 20 ▶use DJabberd::JID;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 20 ▶use DJabberd::IQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 20 ▶use DJabberd::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 20 ▶use DJabberd::Presence;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 20 ▶use DJabberd::StreamVersion;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 20 ▶use DJabberd::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 20 ▶use DJabberd::Delivery::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 20 ▶use DJabberd::Delivery::S2S;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 20 ▶use DJabberd::PresenceChecker::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 20 ▶use DJabberd::Stats;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 package DJabberd;
40 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 20 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY SOL_SOCKET SOCK_STREAM);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 20 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 20 ▶use DJabberd::Util qw(tsub as_bool as_num as_abs_path as_bind_addr);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 20 our $VERSION = '0.85';
46  
47 20 our $logger = DJabberd::Log->get_logger();
48 20 our $hook_logger = DJabberd::Log->get_logger("DJabberd::Hook");
49  
50 20 our %server;
51  
52 0 ▶$SIG{USR2} = sub { Carp::cluck("USR2") };
Subroutine __ANON__: not called CC=1 SCAR=1.2
53  
54 43 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod undocumented
55 43 my ($class, %opts) = @_;
56  
57 my $self = {
58 'daemonize' => delete $opts{daemonize},
59 's2s_port' => delete $opts{s2s_port},
60 'c2s_port' => delete($opts{c2s_port}) || 5222, # {=clientportnumber}
61 'old_ssl' => delete $opts{old_ssl},
62 43 ▶ 'vhosts' => {},
Condition50%1/2
delete $opts{'c2s_port'} || 5222
l!l
043
MC/DC0%0/1
delete $opts{'c2s_port'} || 5222
delete $opts{'c2s_port'}
Truth table50%1/2
A
A= delete $opts{'c2s_port'}
Aresult
11
00
63 'fake_peers' => {}, # for s2s testing. $hostname => "ip:port"
64 'share_parsers' => 1,
65 'monitor_host' => {},
66 };
67  
68 # if they set s2s_port to explicitly 0, it's disabled for all vhosts
69 # but not setting it means 5269 still listens, if vhosts are configured
70 # for s2s.
71 # {=serverportnumber}
72 43 ▶ $self->{s2s_port} = 5269 unless defined $self->{s2s_port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"s2s_port"} 43 0 43
73  
74 43 ▶ croak("Unknown server parameters: " . join(", ", keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 43 43
75  
76 43 bless $self, $class;
77 43 $server{$self} = $self;
78 43 Scalar::Util::weaken($server{$self});
79  
80 43 return $self;
81 }
82  
83 43 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
84 43 delete $server{$_[0]};
85 }
86  
87 # class method
88 0 ▶sub foreach_vhost {
Subroutine foreach_vhost: not called CC=3 SCAR=24.8
Pod undocumented
89 0 my (undef, $cb) = @_;
90 0 foreach my $server (values %DJabberd::server) {
91 0 foreach my $vhost (values %{$server->{vhosts}}) {
92 0 $cb->($vhost);
93 }
94 }
95 }
96  
97 0 ▶sub share_parsers { $_[0]{share_parsers} };
Subroutine share_parsers: not called CC=1 SCAR=6.9
Pod undocumented
98  
99 0 ▶sub set_config_shareparsers {
Subroutine set_config_shareparsers: not called CC=1 SCAR=6.9
Pod undocumented
100 0 my ($self, $val) = @_;
101 0 $self->{share_parsers} = as_bool($val);
102 }
103  
104 0 ▶sub set_config_declaremonitor {
Subroutine set_config_declaremonitor: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my ($self, $val) = @_;
106 0 $self->{monitor_host}{$val} = 1;
107 }
108  
109 # mimicing Apache's SSLCertificateKeyFile config
110 0 ▶sub set_config_sslcertificatekeyfile {
Subroutine set_config_sslcertificatekeyfile: not called CC=1 SCAR=6.9
Pod undocumented
111 0 my ($self, $val) = @_;
112 0 $self->{ssl_private_key_file} = as_abs_path($val);
113 }
114  
115 # mimicing Apache's SSLCertificateFile
116 0 ▶sub set_config_sslcertificatefile {
Subroutine set_config_sslcertificatefile: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my ($self, $val) = @_;
118 0 $self->{ssl_cert_file} = as_abs_path($val);
119 }
120  
121 # mimicing Apache's SSLCertificateChainFile
122 0 ▶sub set_config_sslcertificatechainfile {
Subroutine set_config_sslcertificatechainfile: not called CC=1 SCAR=6.9
Pod undocumented
123 0 my ($self, $val) = @_;
124 0 $self->{ssl_cert_chain_file} = as_abs_path($val);
125 }
126  
127 0 ▶sub ssl_private_key_file { return $_[0]{ssl_private_key_file} }
Subroutine ssl_private_key_file: not called CC=1 SCAR=6.9
Pod undocumented
128 0 ▶sub ssl_cert_file { return $_[0]{ssl_cert_file} }
Subroutine ssl_cert_file: not called CC=1 SCAR=6.9
Pod undocumented
129 0 ▶sub ssl_cert_chain_file { return $_[0]{ssl_cert_chain_file} }
Subroutine ssl_cert_chain_file: not called CC=1 SCAR=6.9
Pod undocumented
130  
131 0 ▶sub set_config_oldssl {
Subroutine set_config_oldssl: not called CC=1 SCAR=6.9
Pod undocumented
132 0 my ($self, $val) = @_;
133 0 $self->{old_ssl} = as_bool($val);
134 }
135  
136 0 ▶sub set_config_unixdomainsocket {
Subroutine set_config_unixdomainsocket: not called CC=1 SCAR=6.9
Pod undocumented
137 0 my ($self, $val) = @_;
138 0 $self->{unixdomainsocket} = $val;
139 }
140  
141 42 ▶sub set_config_clientport {
Subroutine set_config_clientport: called CC=1 SCAR=0.0
Pod undocumented
142 42 my ($self, $val) = @_;
143 42 $self->{c2s_port} = as_bind_addr($val);
144 }
145  
146 42 ▶sub set_config_serverport {
Subroutine set_config_serverport: called CC=1 SCAR=0.0
Pod undocumented
147 42 my ($self, $val) = @_;
148 42 $self->{s2s_port} = as_bind_addr($val);
149 }
150  
151 1 ▶sub set_config_adminport {
Subroutine set_config_adminport: called CC=1 SCAR=0.0
Pod undocumented
152 1 my ($self, $val) = @_;
153 1 $self->{admin_port} = as_bind_addr($val);
154 }
155  
156 0 ▶sub set_config_intradomainlisten {
Subroutine set_config_intradomainlisten: not called CC=1 SCAR=6.9
Pod undocumented
157 0 my ($self, $val) = @_;
158 0 $self->{cluster_listen} = $val;
159 }
160  
161 0 ▶sub set_config_pidfile {
Subroutine set_config_pidfile: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my ($self, $val) = @_;
163 0 $self->{pid_file} = $val;
164 }
165  
166 20 our %fake_peers;
167 23 ▶sub set_fake_s2s_peer {
Subroutine set_fake_s2s_peer: called CC=1 SCAR=0.0
Pod undocumented
168 23 my ($self, $host, $ipendpt) = @_;
169 23 $fake_peers{$host} = $ipendpt;
170 }
171  
172 0 ▶sub fake_s2s_peer {
Subroutine fake_s2s_peer: not called CC=1 SCAR=6.9
Pod undocumented
173 0 my ($self, $host) = @_;
174 0 return $fake_peers{$host};
175 }
176  
177 0 ▶sub set_config_casesensitive {
Subroutine set_config_casesensitive: not called CC=1 SCAR=6.9
Pod undocumented
178 0 my ($self, $val) = @_;
179 0 $DJabberd::JID::CASE_SENSITIVE = as_bool($val);
180 }
181  
182 43 ▶sub add_vhost {
Subroutine add_vhost: called CC=5 SCAR=19.7
Pod undocumented
183 43 my ($self, $vhost) = @_;
184 43 my $sname = lc $vhost->name;
185 43 ▶ if (my $existing = $self->{vhosts}{$sname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing = $self->{"vhosts"}{$sname}) 0 43 43
186 0 ▶ croak("Can't set vhost with name '$sname'. Already exists in this server.")
Branch: 1 branch
BranchTrueFalseTotal
if $existing != $vhost 0 0 0
187 if $existing != $vhost;
188 }
189 43 ▶ if ($vhost->server && $vhost->server != $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vhost->server and $vhost->server != $self) 0 43 43
Condition33%1/3
$vhost->server and $vhost->server != $self
!ll&&!rl&&r
4300
MC/DC0%0/2
$vhost->server and $vhost->server != $self
$vhost->server $vhost->server != $self
Truth table33%1/3
A and B
A= $vhost->serverB= $vhost->server != $self
ABresult
0X0
100
111
190 0 croak("Vhost already has a server.");
191 }
192  
193 43 $vhost->setup_default_plugins;
194  
195 43 $self->{vhosts}{$sname} = $vhost;
196 43 $vhost->set_server($self);
197 }
198  
199 # works as Server method or class method.
200 0 ▶sub lookup_vhost {
Subroutine lookup_vhost: not called CC=6 SCAR=37.4
Pod undocumented
201 0 my ($self, $hostname) = @_;
202  
203 # look at all server objects in process
204 0 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
205 0 foreach my $server (values %DJabberd::server) {
206 0 my $vh = $server->lookup_vhost($hostname);
207 0 ▶ return $vh if $vh;
Branch: 1 branch
BranchTrueFalseTotal
if $vh 0 0 0
208 }
209 0 return 0;
210 }
211  
212 # method on server object
213 0 foreach my $vhost (values %{$self->{vhosts}}) {
214 0 ▶ return $vhost
Branch: 1 branch
BranchTrueFalseTotal
if $vhost->handles_domain($hostname) 0 0 0
215 if ($vhost->handles_domain($hostname));
216 }
217 0 return 0;
218 }
219  
220 # return the version of the spec we implement
221 0 ▶sub spec_version {
Subroutine spec_version: not called CC=2 SCAR=17.9
Pod undocumented
222 0 my $self = shift;
223 0 ▶ return $self->{_spec_version} ||= DJabberd::StreamVersion->new("1.0");
Condition0%0/3
$self->{"_spec_version"} ||= "DJabberd::StreamVersion"->new("1.0")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_spec_version"} ||= "DJabberd::StreamVersion"->new("1.0")
$self->{"_spec_version"} "DJabberd::StreamVersion"->new("1.0")
Truth table0%0/3
A ||= B
A= $self->{"_spec_version"}B= "DJabberd::StreamVersion"->new("1.0")
ABresult
1X1
011
000
224 }
225  
226  
227 20 my %obj_source; # refaddr -> file/linenumber
228 my %obj_living; # file/linenumber -> ct
229 20 ▶use Scalar::Util qw(refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
230 20 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
231 0 ▶sub dump_obj_stats {
Subroutine dump_obj_stats: not called CC=2 SCAR=17.9
Pod undocumented
232 0 print Dumper(\%obj_living);
233 0 my %class_ct;
234 0 foreach (values %obj_source) {
235 0 $class_ct{ref($_->[1])}++;
236 }
237 0 print Dumper(\%class_ct);
238 }
239  
240  
241 0 ▶sub track_new_obj {
Subroutine track_new_obj: not called CC=6 SCAR=37.4
Pod undocumented
242 0 ▶ return unless $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"TRACKOBJ"} 0 0 0
243  
244 0 my ($class, $obj) = @_;
245 0 my $i = 0;
246 0 my $fileline;
247 0 ▶ while (!$fileline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fileline) 0 0 0
248 0 $i++;
249 0 my ($pkg, $filename, $line, $subname) = caller($i);
250 0 ▶ next if $subname eq "new";
Branch: 1 branch
BranchTrueFalseTotal
if $subname eq "new" 0 0 0
251 0 $fileline = "$filename/$line";
252 }
253 0 my $addr = refaddr($obj);
254 0 ▶ warn "New object $obj -- $fileline\n" if $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACKOBJ"} 0 0 0
255 0 $obj_source{$addr} = [$fileline, $obj];
256 0 weaken($obj_source{$addr}[1]);
257  
258 0 $obj_living{$fileline}++;
259 0 ▶ dump_obj_stats() if $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACKOBJ"} 0 0 0
260 }
261  
262 0 ▶sub track_destroyed_obj {
Subroutine track_destroyed_obj: not called CC=5 SCAR=34.0
Pod undocumented
263 0 ▶ return unless $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"TRACKOBJ"} 0 0 0
264  
265 0 my ($class, $obj) = @_;
266 0 my $addr = refaddr($obj);
267 0 ▶ my $fileline = $obj_source{$addr}->[0] or die "Where did $obj come from?";
Branch: 1 branch
BranchTrueFalseTotal
my $fileline = $obj_source{$addr}[0] or die "Where did $obj come from?" 0 0 0
268 0 delete $obj_source{$addr};
269 0 ▶ warn "Destroyed object $obj -- $fileline\n" if $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACKOBJ"} 0 0 0
270 0 $obj_living{$fileline}--;
271 0 ▶ dump_obj_stats() if $ENV{TRACKOBJ};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"TRACKOBJ"} 0 0 0
272 }
273  
274 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
275 0 my $self = shift;
276 0 ▶ return unless $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"debug"} 0 0 0
277 0 printf STDERR @_;
278 }
279  
280 0 ▶sub run {
Subroutine run: not called CC=8 SCAR=42.8
Pod undocumented
281 0 my $self = shift;
282 0 ▶ daemonize() if $self->{daemonize};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"daemonize"} 0 0 0
283 0 local $SIG{'PIPE'} = "IGNORE"; # handled manually
284 0 ▶ if ($self->{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid_file"}) 0 0 0
285 0 $logger->debug("Logging PID to file $self->{pid_file}");
286 0 ▶ open(PIDFILE,'>',$self->{pid_file}) or $logger->logdie("Can't open pidfile $self->{pid_file} for writing");
Branch: 1 branch
BranchTrueFalseTotal
open PIDFILE, ">", $self->{"pid_file"} or $DJabberd::logger->logdie(qq[Can't open pidfile $self->{"pid_file"} for writing]) 0 0 0
287 0 print PIDFILE "$$\n";
288 0 close(PIDFILE);
289 }
290 0 $self->start_c2s_server();
291  
292 # {=s2soptional}
293 0 ▶ $self->start_s2s_server() if $self->{s2s_port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"s2s_port"} 0 0 0
294  
295 0 ▶ $self->start_cluster_server() if $self->{cluster_listen};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cluster_listen"} 0 0 0
296  
297 0 ▶ $self->_start_server($self->{admin_port}, "DJabberd::Connection::Admin") if $self->{admin_port};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"admin_port"} 0 0 0
298  
299 0 DJabberd::Connection::Admin->on_startup;
300 0 Danga::Socket->EventLoop();
301 0 ▶ unlink($self->{pid_file}) if (-f $self->{pid_file});
Branch: 1 branch
BranchTrueFalseTotal
if -f $self->{"pid_file"} 0 0 0
302 }
303  
304 0 ▶sub _start_server {
Subroutine _start_server: not called CC=8 SCAR=42.8
305 0 my ($self, $localaddr, $class) = @_;
306  
307 # establish SERVER socket, bind and listen.
308 0 my $server;
309 0 my $not_tcp = 0;
310 0 ▶ if ($localaddr =~ m!^/!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($localaddr =~ m[^/]) { } 0 0 0
311 0 $not_tcp = 1;
312 0 ▶ $server = IO::Socket::UNIX->new(Type => SOCK_STREAM,
Branch: 1 branch
BranchTrueFalseTotal
$server = "IO::Socket::UNIX"->new("Type", 1, "Local", $localaddr, "Listen", 10) or $DJabberd::logger->logdie("Error creating unix domain socket: $@\n") 0 0 0
313 Local => $localaddr,
314 Listen => 10)
315 or $logger->logdie("Error creating unix domain socket: $@\n");
316 } else {
317 # assume it's a port if there's no colon
318 0 ▶ unless ($localaddr =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($localaddr =~ /:/) 0 0 0
319 0 $localaddr = "0.0.0.0:$localaddr";
320 }
321  
322 0 $logger->debug("Opening TCP listen socket on $localaddr");
323  
324 0 ▶ $server = IO::Socket::INET->new(LocalAddr => $localaddr,
Branch: 1 branch
BranchTrueFalseTotal
$server = "IO::Socket::INET"->new("LocalAddr", $localaddr, "Type", 1, "Proto", 6, "Reuse", 1, "Listen", 10) or $DJabberd::logger->logdie("Error creating socket: $@\n") 0 0 0
325 Type => SOCK_STREAM,
326 Proto => IPPROTO_TCP,
327 Reuse => 1,
328 Listen => 10 )
329 or $logger->logdie("Error creating socket: $@\n");
330  
331 0 my $success = $server->blocking(0);
332  
333 0 ▶ unless (defined($success)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $success) 0 0 0
334 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
335 # On Windows, we have to do this a bit differently
336 0 my $do = 1;
337 0 ▶ ioctl($server, 0x8004667E, \$do) or $logger->warn("Failed to make socket non-blocking: $!");
Branch: 1 branch
BranchTrueFalseTotal
ioctl $server, 2147772030, \$do or $DJabberd::logger->warn("Failed to make socket non-blocking: $!") 0 0 0
338 }
339 else {
340 0 $logger->warn("Failed to make socket non-blocking: $!")
341 }
342 }
343 }
344  
345 # Not sure if I'm crazy or not, but I can't see in strace where/how
346 # Perl 5.6 sets blocking to 0 without this. In Perl 5.8, IO::Socket::INET
347 # obviously sets it from watching strace.
348 0 IO::Handle::blocking($server, 0);
349  
350 0 ▶ my $accept_handler = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
351 0 local *__ANON__ = " Accept handler in ". __FILE__ ." on line ". __LINE__;
352  
353 0 my $csock = $server->accept;
354 0 ▶ return unless $csock;
Branch: 1 branch
BranchTrueFalseTotal
unless $csock 0 0 0
355  
356 0 $self->debug("Listen child making a DJabberd::Connection for %d.\n", fileno($csock));
357  
358 0 IO::Handle::blocking($csock, 0);
359 0 ▶ unless ($not_tcp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($not_tcp) 0 0 0
360 0 ▶ setsockopt($csock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or die;
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $csock, 6, 1, "\1\0\0\0" or die 0 0 0
361 }
362  
363 0 ▶ if (my $client = eval { $class->new($csock, $self) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client = eval { do { $class->new($csock, $self) } }) { } 0 0 0
364 0 $DJabberd::Stats::counter{connect}++;
365 0 $client->watch_read(1);
366 0 return;
367 } else {
368 0 $logger->error("Error creating new $class: $@");
369 }
370 0 };
371  
372 0 Danga::Socket->AddOtherFds(fileno($server) => $accept_handler);
373 }
374  
375 0 ▶sub start_c2s_server {
Subroutine start_c2s_server: not called CC=3 SCAR=24.8
Pod undocumented
376 0 my $self = shift;
377 $self->_start_server($self->{c2s_port},
378 0 "DJabberd::Connection::ClientIn");
379  
380 0 ▶ if ($self->{old_ssl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"old_ssl"}) 0 0 0
381 0 $self->_start_server(5223, "DJabberd::Connection::OldSSLClientIn");
382 }
383  
384 0 ▶ if ($self->{unixdomainsocket}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"unixdomainsocket"}) 0 0 0
385 0 $self->_start_server($self->{unixdomainsocket}, "DJabberd::Connection::ClientIn");
386 }
387 }
388  
389 0 ▶sub start_s2s_server {
Subroutine start_s2s_server: not called CC=1 SCAR=6.9
Pod undocumented
390 0 my $self = shift;
391 $self->_start_server($self->{s2s_port},
392 0 "DJabberd::Connection::ServerIn");
393 }
394  
395 0 ▶sub start_cluster_server {
Subroutine start_cluster_server: not called CC=1 SCAR=6.9
Pod undocumented
396 0 my $self = shift;
397 $self->_start_server($self->{cluster_listen},
398 0 "DJabberd::Connection::ClusterIn");
399 }
400  
401 0 ▶sub start_simple_server {
Subroutine start_simple_server: not called CC=2 SCAR=17.9
Pod undocumented
402 0 my ($self, $port) = @_;
403 0 ▶ eval "use DJabberd::Connection::SimpleIn; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "use DJabberd::Connection::SimpleIn; 1" or die "Failed to load DJabberd::Connection::SimpleIn: $@\n" 0 0 0
404 or die "Failed to load DJabberd::Connection::SimpleIn: $@\n";
405 0 $self->_start_server($port, "DJabberd::Connection::SimpleIn");
406 }
407  
408 0 ▶sub is_monitor_host {
Subroutine is_monitor_host: not called CC=1 SCAR=6.9
Pod undocumented
409 0 my ($self, $host) = @_;
410 0 return $self->{monitor_host}{$host};
411 }
412  
413 0 ▶sub load_config {
Subroutine load_config: not called CC=3 SCAR=24.8
Pod undocumented
414 0 my ($self, $arg) = @_;
415 0 ▶ if (ref $arg eq "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'SCALAR') { } 0 0 0
416 0 $self->_load_config_ref($arg);
417 } else {
418 0 ▶ open (my $fh, $arg) or die "Couldn't open config file '$arg': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $arg or die "Couldn't open config file '${arg}': $!\n" 0 0 0
419 0 my $slurp = do { local $/; <$fh>; };
420 0 $self->_load_config_ref(\$slurp);
421 }
422 }
423  
424 0 ▶sub _load_config_ref {
Subroutine _load_config_ref: not called CC=27 SCAR=66.3
425 0 my ($self, $configref) = @_;
426 0 my $linenum = 0;
427 0 my $vhost; # current vhost in scope
428 my $plugin; # current plugin in scope
429 0 my @vhost_stack = ();
430  
431 0 ▶ my $expand_var = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
432 0 my ($type, $key) = @_;
433 0 $type = uc $type;
434  
435 0 ▶ if ($type eq "ENV") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ENV") 0 0 0
436 # expands ${ENV:KEY} on a line into $ENV{'KEY'} or dies if not defined
437 0 my $val = $ENV{$key};
438 0 ▶ die "Undefined environment variable '$key'\n" unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
439 0 return $val;
440 }
441 0 die "Unknown variable type '$type'\n";
442 0 };
443  
444 0 foreach my $line (split(/\n/, $$configref)) {
445 0 $linenum++;
446  
447 0 $line =~ s/^\s+//;
448 0 ▶ next if $line =~ /^\#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\#/ 0 0 0
449 0 $line =~ s/\s+$//;
450 0 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 0 0
451  
452 0 eval {
453 # expand environment variables
454 0 $line =~ s/\$\{(\w+):(\w+)\}/$expand_var->($1, $2)/eg;
455  
456 0 ▶ if ($line =~ /^(\w+)\s+(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\w+)\s+(.+)/) 0 0 0
457 0 my $pkey = $1;
458 0 my $key = lc $1;
459 0 my $val = $2;
460 0 ▶ my $inv = $plugin || $vhost || $self;
Condition0%0/6
$plugin || $vhost || $self
l!l&&r!l&&!r
000
$plugin or $vhost
l!l&&r!l&&!r
000
MC/DC0%0/3
$plugin || $vhost || $self
$plugin $vhost $self
Truth table0%0/4
A or B || C
A= $pluginB= $vhostC= $self
ABCresult
1XX1
01X1
0011
0000
461 0 my $meth = "set_config_$key";
462 0 ▶ if ($inv->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inv->can($meth)) 0 0 0
463 0 $inv->$meth($val);
464 0 next;
465 }
466 0 $meth = "set_config__option";
467 0 ▶ if ($inv->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inv->can($meth)) 0 0 0
468 0 $inv->$meth($key, $val);
469 0 next;
470 }
471  
472 0 die "Unknown option '$pkey'\n";
473 }
474 0 ▶ if ($line =~ /<VHost\s+(\S+?)>/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /<VHost\s+(\S+?)>/i) 0 0 0
475 0 ▶ die "Can't configure a vhost in a vhost\n" if $vhost;
Branch: 1 branch
BranchTrueFalseTotal
if $vhost 0 0 0
476 0 $vhost = DJabberd::VHost->new(server_name => $1);
477 0 $vhost->set_server($self);
478 0 next;
479 }
480 0 ▶ if ($line =~ m!</VHost>!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[</VHost>]i) 0 0 0
481 0 ▶ die "Can't end a not-open vhost\n" unless $vhost;
Branch: 1 branch
BranchTrueFalseTotal
unless $vhost 0 0 0
482 0 ▶ die "Can't end a vhost with an open plugin\n" if $plugin;
Branch: 1 branch
BranchTrueFalseTotal
if $plugin 0 0 0
483 0 ▶ die "Can't end a vhost with an open subdomain\n" if @vhost_stack;
Branch: 1 branch
BranchTrueFalseTotal
if @vhost_stack 0 0 0
484 0 $self->add_vhost($vhost);
485 0 $vhost = undef;
486 0 next;
487 }
488 0 ▶ if ($line =~ /<Subdomain\s+(\S+?)>/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /<Subdomain\s+(\S+?)>/i) 0 0 0
489 0 ▶ die "Subdomain blocks can only inside VHost\n" unless $vhost;
Branch: 1 branch
BranchTrueFalseTotal
unless $vhost 0 0 0
490 0 my $subdomain_name = $1.".".$vhost->server_name;
491  
492 0 my $old_vhost = $vhost;
493 0 push @vhost_stack, $old_vhost;
494  
495 0 $vhost = DJabberd::VHost->new(server_name => $subdomain_name);
496 0 $vhost->set_server($self);
497  
498 # Automatically add the LocalVHosts delivery plugin so that these
499 # VHosts can talk to one another without S2S.
500 0 my $loaded = eval "use DJabberd::Delivery::LocalVHosts; 1;";
501 0 ▶ die "Failed to load LocalVHosts delivery plugin for subdomain" unless $loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 0 0
502  
503 0 my $ld1 = DJabberd::Delivery::LocalVHosts->new(allowvhost => $subdomain_name);
504 0 my $ld2 = DJabberd::Delivery::LocalVHosts->new(allowvhost => $old_vhost->server_name);
505 0 $old_vhost->add_plugin($ld1);
506 0 $vhost->add_plugin($ld2);
507  
508 0 next;
509 }
510 0 ▶ if ($line =~ m!</Subdomain>!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[</Subdomain>]i) 0 0 0
511 0 ▶ die "Extraneous subdomain end\n" unless @vhost_stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @vhost_stack 0 0 0
512 0 $self->add_vhost($vhost);
513 0 $vhost = pop @vhost_stack;
514 0 next;
515 }
516 0 ▶ my $close_plugin = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
517 0 ▶ die "Can't end a not-open plugin\n" unless $plugin;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin 0 0 0
518 0 $plugin->finalize;
519 0 $vhost->add_plugin($plugin);
520 0 $plugin = undef;
521 0 };
522  
523 0 ▶ if ($line =~ /<Plugin\s+([\w:]+?)(\s*\/)?>/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[<Plugin\s+([\w:]+?)(\s*/)?>]i) 0 0 0
524 0 my $class = $1;
525 0 my $immediate_close = $2;
526 0 ▶ die "Can't configure a plugin outside of a vhost config vhost\n" unless $vhost;
Branch: 1 branch
BranchTrueFalseTotal
unless $vhost 0 0 0
527 0 ▶ die "Can't configure a plugin inside of a plugin\n" if $plugin;
Branch: 1 branch
BranchTrueFalseTotal
if $plugin 0 0 0
528  
529 0 my $loaded = eval "use $class; 1;";
530 0 ▶ die "Failed to load plugin $class: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
531 0 $plugin = $class->new;
532 0 ▶ $close_plugin->() if $immediate_close;
Branch: 1 branch
BranchTrueFalseTotal
if $immediate_close 0 0 0
533 0 next;
534 }
535 0 ▶ if ($line =~ m!</Plugin>!i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[</Plugin>]i) 0 0 0
536 0 $close_plugin->();
537 0 next;
538 }
539  
540 0 die "Syntax error: '$line'\n";
541 };
542 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
543 0 die "Configuration error on line $linenum: $@\n";
544 }
545 }
546 }
547  
548 0 ▶sub daemonize {
Subroutine daemonize: not called CC=4 SCAR=30.0
Pod undocumented
549 0 my($pid, $sess_id, $i);
550  
551 ## Fork and exit parent
552 0 ▶ if ($pid = fork) { exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pid = fork) 0 0 0
553  
554 ## Detach ourselves from the terminal
555 0 ▶ Carp::croak("Cannot detach from controlling terminal")
Branch: 1 branch
BranchTrueFalseTotal
unless $sess_id = POSIX::setsid() 0 0 0
556 unless $sess_id = POSIX::setsid();
557  
558 ## Prevent possibility of acquiring a controling terminal
559 0 $SIG{'HUP'} = 'IGNORE';
560 0 ▶ if ($pid = fork) { exit 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($pid = fork) 0 0 0
561  
562 ## Change working directory
563 0 chdir "/";
564  
565 ## Clear file creation mask
566 0 umask 0;
567  
568 ## Close open file descriptors
569 0 close(STDIN);
570 0 close(STDOUT);
571 0 close(STDERR);
572  
573 ## Reopen stderr, stdout, stdin to /dev/null
574 0 open(STDIN, "+>/dev/null");
575 0 open(STDOUT, "+>&STDIN");
576 0 open(STDERR, "+>&STDIN");
577 }
578  
579 # Local Variables:
580 # mode: perl
581 # c-basic-indent: 4
582 # indent-tabs-mode: nil
583 # End:
584  
585 20 1;
586  
587 __END__
↑ Summary DJabberd/Connection.pm »