| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | DJabberd::Delivery::ComponentConnection - Delivery through a JEP-0114 component connection | ||
| 5 | |||
| 6 | =head1 INTRODUCTION | ||
| 7 | |||
| 8 | This class allows you to route stanzas over a JEP-0114 component connection. It can operate | ||
| 9 | both as a server (to which you can connect third-party JEP-0114 components) and as a client, | ||
| 10 | where a DJabberd VHost itself acts as a component. | ||
| 11 | |||
| 12 | The server support allows you to connect third-party components and transports such as | ||
| 13 | the popular pyXXXt range of transports to other IM networks. | ||
| 14 | |||
| 15 | The client support means that you can expose any DJabberd VHost as a JEP-0114 component | ||
| 16 | and connect the resulting component to any other Jabberd implementation which supports | ||
| 17 | JEP-0114. In effect this allows you to use DJabberd as a platform to write a component while | ||
| 18 | retaining your existing Jabber server implementation. | ||
| 19 | |||
| 20 | Note: The server mode doesn't currently work. See DJabberd::Component::External. | ||
| 21 | This module may change name at some point; the combination of the client and server modes into | ||
| 22 | on module is not finalized. | ||
| 23 | |||
| 24 | =head1 LICENCE | ||
| 25 | |||
| 26 | Copyright 2006 Martin Atkins and Six Apart | ||
| 27 | |||
| 28 | This library is part of the Jabber server DJabberd. It can be modified and distributed | ||
| 29 | under the same terms as DJabberd itself. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | package DJabberd::Delivery::ComponentConnection; | ||
| 34 | |||
| 35 | use strict; | ||
| 36 | use base qw(DJabberd::Delivery); | ||
| 37 | use DJabberd::Connection::ComponentOut; | ||
| 38 | use DJabberd::DNS; | ||
| 39 | use Socket qw(PF_INET IPPROTO_TCP SOCK_STREAM TCP_NODELAY); | ||
| 40 | |||
| 41 | our $logger = DJabberd::Log->get_logger(); | ||
| 42 | |||
| 43 | sub set_config_listenport { | ||
| 44 | my ($self, $port) = @_; | ||
| 45 | |||
| 46 | return $self->set_config_listenaddr($port); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub set_config_secret { | ||
| 50 | my ($self, $secret) = @_; | ||
| 51 | |||
| 52 | $self->{secret} = $secret; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub set_config_listenaddr { | ||
| 56 | my ($self, $addr) = @_; | ||
| 57 | |||
| 58 | $addr = DJabberd::Util::as_bind_addr($addr); | ||
| 59 | |||
| 60 | my ($ipaddr, $port) = split(/:/, $addr); | ||
| 61 | |||
| 62 | unless (defined($port)) { | ||
| 63 | $port = $ipaddr; | ||
| 64 | $ipaddr = "127.0.0.1"; | ||
| 65 | } | ||
| 66 | |||
| 67 | $self->{listenaddr} = $ipaddr; | ||
| 68 | $self->{listenport} = $port; | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | sub set_config_remoteaddr { | ||
| 73 | my ($self, $addr) = @_; | ||
| 74 | |||
| 75 | # TODO: Make this support DNS lookups later | ||
| 76 | $addr = DJabberd::Util::as_bind_addr($addr); | ||
| 77 | |||
| 78 | my ($ipaddr, $port) = split(/:/, $addr); | ||
| 79 | |||
| 80 | unless (defined($port)) { | ||
| 81 | $port = $ipaddr; | ||
| 82 | $ipaddr = "127.0.0.1"; | ||
| 83 | } | ||
| 84 | |||
| 85 | $self->{remoteaddr} = $ipaddr; | ||
| 86 | $self->{remoteport} = $port; | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | sub finalize { | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | $logger->logdie("Must specify either ListenAddr (to connect an external component) or RemoteAddr (to expose this vhost as a component)") unless $self->{listenaddr} || $self->{remoteaddr}; | ||
| 94 | |||
| 95 | $self->{i_am_the_component} = $self->{remoteaddr} ? 1 : 0; | ||
| 96 | |||
| 97 | $logger->logdie("No Secret specified for component") unless $self->{secret}; | ||
| 98 | |||
| 99 | my $dispaddr = $self->{i_am_the_component} ? $self->{remoteaddr}.":".$self->{remoteport} : $self->{listenaddr}.":".$self->{listenport}; | ||
| 100 | $logger->debug("I'm running in ".($self->{i_am_the_component} ? "client" : "server")." mode and using ".$dispaddr); | ||
| 101 | |||
| 102 | $self->SUPER::finalize; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub register { | ||
| 106 | my ($self, $vhost) = @_; | ||
| 107 | |||
| 108 | $self->SUPER::register($vhost); | ||
| 109 | |||
| 110 | $self->_prepare_connection(); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub deliver { | ||
| 114 | my ($self, $vhost, $cb, $stanza) = @_; | ||
| 115 | |||
| 116 | if ($self->_can_deliver_stanza($stanza)) { | ||
| 117 | if (my $conn = $self->connection) { | ||
| 118 | $conn->write_stanza($stanza); | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | # If we've currently got no connection at all, | ||
| 122 | # let's throw a wobbly. | ||
| 123 | # FIXME: Could this get into an infinite loop in some cases? Probably. | ||
| 124 | $stanza->make_error_response('503', 'cancel', 'service-unavailable')->deliver($vhost); | ||
| 125 | } | ||
| 126 | return $cb->delivered; | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | return $cb->decline; | ||
| 130 | } | ||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | sub _i_am_the_component { | ||
| 135 | return $_[0]->{i_am_the_component}; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _can_deliver_stanza { | ||
| 139 | my ($self, $stanza) = @_; | ||
| 140 | |||
| 141 | # If we're the component, we want to offload anything with a domain | ||
| 142 | # other than our own. If we aren't the component, the reverse is true. | ||
| 143 | my $same_host = ($stanza->to_jid->domain eq $self->vhost->server_name); | ||
| 144 | |||
| 145 | return $self->_i_am_the_component ? ! $same_host : $same_host; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub connection { | ||
| 149 | return $_[0]->{connection}; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _prepare_connection { | ||
| 153 | my ($self) = @_; | ||
| 154 | |||
| 155 | if ($self->_i_am_the_component) { | ||
| 156 | $self->_start_connection($self); | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | $self->_start_listener($self); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub _start_connection { | ||
| 164 | my ($self) = @_; | ||
| 165 | |||
| 166 | my $vhost = $self->vhost; | ||
| 167 | |||
| 168 | my $connection = DJabberd::Connection::ComponentOut->new( | ||
| 169 | endpoint => DJabberd::IPEndPoint->new($self->{remoteaddr}, $self->{remoteport}), | ||
| 170 | vhost => $vhost, | ||
| 171 | secret => $self->{secret}, | ||
| 172 | ); | ||
| 173 | $connection->watch_read(1); | ||
| 174 | $connection->add_connect_handler(sub { | ||
| 175 | $logger->debug("Connection established and channel open."); | ||
| 176 | $self->{connection} = $connection; | ||
| 177 | }); | ||
| 178 | $connection->add_disconnect_handler(sub { | ||
| 179 | # FIXME: Maybe this delay should get exponentially longer each retry? | ||
| 180 | # We're probably only connecting to localhost anyway, so I shant bother for now. | ||
| 181 | $logger->debug("Outgoing connection was lost. Will attempt to re-establish it in 30 seconds."); | ||
| 182 | |||
| 183 | Danga::Socket->AddTimer(30, sub { | ||
| 184 | $self->{connection} = undef; | ||
| 185 | $self->_prepare_connection(); | ||
| 186 | }); | ||
| 187 | }); | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | sub _start_listener { | ||
| 192 | my ($self) = @_; | ||
| 193 | my $vhost = $self->vhost; | ||
| 194 | |||
| 195 | my $bindaddr = $self->{listenaddr}; | ||
| 196 | |||
| 197 | # FIXME: Maybe shouldn't duplicate all of this code out of DJabberd.pm. | ||
| 198 | |||
| 199 | my $server; | ||
| 200 | my $not_tcp = 0; | ||
| 201 | if ($bindaddr =~ m!^/!) { | ||
| 202 | $not_tcp = 1; | ||
| 203 | $server = IO::Socket::UNIX->new( | ||
| 204 | Type => SOCK_STREAM, | ||
| 205 | Local => $bindaddr, | ||
| 206 | Listen => 10 | ||
| 207 | ); | ||
| 208 | $logger->logdie("Error creating UNIX domain socket $bindaddr: $@") unless $server; | ||
| 209 | $logger->info("Started listener for component ".$self->domain." on UNIX domain socket $bindaddr"); | ||
| 210 | } else { | ||
| 211 | $server = IO::Socket::INET->new( | ||
| 212 | LocalAddr => $bindaddr, | ||
| 213 | Type => SOCK_STREAM, | ||
| 214 | Proto => IPPROTO_TCP, | ||
| 215 | Blocking => 0, | ||
| 216 | Reuse => 1, | ||
| 217 | Listen => 10 | ||
| 218 | ); | ||
| 219 | $logger->logdie("Error creating listen socket for <$bindaddr>: $@") unless $server; | ||
| 220 | $logger->info("Started listener for component ".$self->domain." on TCP socket <$bindaddr>"); | ||
| 221 | } | ||
| 222 | |||
| 223 | # Brad thinks this is necessary under Perl 5.6, and who am I to argue? | ||
| 224 | IO::Handle::blocking($server, 0); | ||
| 225 | |||
| 226 | $self->{listener} = $server; | ||
| 227 | |||
| 228 | my $accept_handler = sub { | ||
| 229 | my $csock = $server->accept; | ||
| 230 | return unless $csock; | ||
| 231 | |||
| 232 | $logger->debug("Accepting connection from component ".$self->domain); | ||
| 233 | |||
| 234 | IO::Handle::blocking($csock, 0); | ||
| 235 | unless ($not_tcp) { | ||
| 236 | setsockopt($csock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or $logger->logdie("Couldn't set TCP_NODELAY"); | ||
| 237 | } | ||
| 238 | |||
| 239 | my $connection = DJabberd::Connection::ComponentIn->new($csock, $vhost->server, $self); | ||
| 240 | $connection->watch_read(1); | ||
| 241 | $self->{connection} = $connection; | ||
| 242 | |||
| 243 | # We only need to support one connection at a time, so | ||
| 244 | # shut down the listen socket now to save resources. | ||
| 245 | $self->_stop_listener($self); | ||
| 246 | }; | ||
| 247 | |||
| 248 | Danga::Socket->AddOtherFds(fileno($server) => $accept_handler); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub _stop_listener { | ||
| 252 | my ($self) = @_; | ||
| 253 | |||
| 254 | return unless $self->{listener}; | ||
| 255 | $logger->info("Shutting down listener for component ".$self->domain); | ||
| 256 | $self->{listener} = undef if $self->{listener}->close(); | ||
| 257 | return $self->{listener} == undef; | ||
| 258 | } | ||
| 259 | |||
| 260 | 1; |