--- deliantra/server/ext/tcp.ext 2009/11/29 10:52:14 1.13 +++ deliantra/server/ext/tcp.ext 2012/11/05 23:23:04 1.23 @@ -1,30 +1,79 @@ -#! perl # MANDATORY +#! perl # mandatory # this listens for new tcp connections and hands them over to the server core -# wether this being an extension introduces or reduces stability problems +# whether this being an extension introduces or reduces stability problems # is unknown as of today. use Socket; use AnyEvent::Socket; -our $BIND = $cf::CFG{bind_addresses} || [[undef, 13327]]; +CONF BIND_ADDRESSES = [[undef, 13327]]; -for (@$BIND) { +our $MAX_DETECT = 16; # how many bytes to raed to identify the protocol + +our @LISTENERS; + +sub flash_policy_server { + my ($fh) = @_; + + # socket policy file, just write answer and hope the kernel accepts it in one go + syswrite $fh, < + + + + +EOF +} + +for (@$BIND_ADDRESSES) { my ($host, $port) = @$_; - warn "listening on $host:$port\n"; + cf::info "listening on ", (format_hostport $host, $port), "\n"; - tcp_server $host, $port, sub { + push @LISTENERS, tcp_server $host, $port, sub { my ($fh, $host, $port) = @_ or return; - warn "new connection from [$host]:$port\n"; + my $lhost = AnyEvent::Socket::format_address + +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; + + my $id = format_hostport $host, $port; + + cf::info "$id: new connection\n" + if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs + + my $buf; + my $w; $w = AE::io $fh, 0, sub { + my $len = sysread $fh, $buf, 512, length $buf; + + if ($len) { + if ($buf =~ /^..version /s) { # deliantra protocol + undef $w; + + my $ns = cf::client::create fileno $fh, $host; + $ns->inbuf_append ($buf); + + } elsif ($buf =~ /^GET / && defined &ext::http::server) { # http or websocket + undef $w; + + &ext::http::server ($id, $fh, $buf); + + } elsif ($buf =~ /^\x00/) { + undef $w; + + flash_policy_server $fh; + + } elsif (length $buf >= $MAX_DETECT) { # unable to detect protocol + undef $w; + + cf::info "$id: protocol detection error\n"; + } + } else { + undef $w; - # HACK to avoid blocking on common files on log-in. - # remove once async - cf::async {#d# - warn "HACK ext/tcp.ext: $cf::LOCALDIR/crossfiremail\n" and Coro::AIO::aio_load "$cf::LOCALDIR/crossfiremail", my $dummy;#d# - cf::client::create fileno $fh, $host; - };#d#d + cf::info "$id: read error during protocol detection\n"; + } + }; }; }