1 | #! perl |
1 | #! perl # mandatory |
2 | |
2 | |
3 | # this listens for new tcp connections and hands them over to the server core |
3 | # this listens for new tcp connections and hands them over to the server core |
4 | # wether this being an extension introduces or reduces stability problems |
4 | # wether this being an extension introduces or reduces stability problems |
5 | # is unknown as of today. |
5 | # is unknown as of today. |
6 | |
6 | |
7 | use Socket; |
7 | use Socket; |
8 | use IO::Socket::INET; |
8 | use AnyEvent::Socket; |
9 | |
9 | |
10 | our $LISTEN = new IO::Socket::INET |
10 | our $BIND = $cf::CFG{bind_addresses} || [[undef, 13327]]; |
11 | LocalPort => cf::settings->csport, |
11 | our @LISTENERS; |
12 | Listen => 1, |
|
|
13 | Blocking => 0, # bugfix workaround for Event calling handler twice :( |
|
|
14 | ReuseAddr => 1; |
|
|
15 | |
12 | |
16 | if (!$LISTEN) { |
13 | for (@$BIND) { |
17 | # extension yes, completely stupid, not yet |
14 | my ($host, $port) = @$_; |
18 | warn "unable to establish listen sockect, exiting.\n"; |
15 | cf::info "listening on ", (format_hostport $host, $port), "\n"; |
19 | exit (2); |
16 | |
|
|
17 | push @LISTENERS, tcp_server $host, $port, sub { |
|
|
18 | my ($fh, $host, $port) = @_ |
|
|
19 | or return; |
|
|
20 | |
|
|
21 | my $lhost = AnyEvent::Socket::format_address |
|
|
22 | +(AnyEvent::Socket::unpack_sockaddr getsockname $fh)[1]; |
|
|
23 | |
|
|
24 | cf::info "new connection from ", (format_hostport $host, $port), "\n" |
|
|
25 | if $lhost ne $host; # do not log connections from the host, e.g. for watchdogs |
|
|
26 | |
|
|
27 | cf::client::create fileno $fh, $host; |
|
|
28 | }; |
20 | } |
29 | } |
21 | |
30 | |
22 | Event->io (fd => $LISTEN, nice => 1, poll => 'r', data => cf::WF_AUTOCANCEL, cb => sub { |
|
|
23 | my ($fh, $peername) = $LISTEN->accept |
|
|
24 | or return; |
|
|
25 | |
|
|
26 | my $fd = fileno $fh; |
|
|
27 | my $host = inet_ntoa +(sockaddr_in $peername)[1]; |
|
|
28 | |
|
|
29 | warn "new connection from $host\n"; |
|
|
30 | |
|
|
31 | cf::client::create $fd, $host; |
|
|
32 | }); |
|
|