« lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Handle.pm
↑ Summary
lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Pool.pm »
| 1 | #!/usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # Simple Flash Socket Policy Server | ||
| 4 | # http://www.lightsphere.com/dev/articles/flash_socket_policy.html | ||
| 5 | # | ||
| 6 | # Copyright (C) 2008 Jacqueline Kira Hamilton | ||
| 7 | # | ||
| 8 | # This program is free software: you can redistribute it and/or modify | ||
| 9 | # it under the terms of the GNU General Public License as published by | ||
| 10 | # the Free Software Foundation, either version 3 of the License, or | ||
| 11 | # (at your option) any later version. | ||
| 12 | # | ||
| 13 | # This program is distributed in the hope that it will be useful, | ||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | # GNU General Public License for more details. | ||
| 17 | # | ||
| 18 | # You should have received a copy of the GNU General Public License | ||
| 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | |||
| 21 | |||
| 22 | use Socket; | ||
| 23 | use IO::Handle; | ||
| 24 | |||
| 25 | my $should_be_logging = 1; # change to 0 to turn off logging. | ||
| 26 | |||
| 27 | my $logfile = 'log'; | ||
| 28 | |||
| 29 | if ($should_be_logging) { | ||
| 30 | open(LOG, ">$logfile") or warn "Can't open $logfile: $!\n"; | ||
| 31 | LOG->autoflush(1); | ||
| 32 | } | ||
| 33 | |||
| 34 | my $port = 843; | ||
| 35 | my $proto = getprotobyname('tcp'); | ||
| 36 | |||
| 37 | # start the server: | ||
| 38 | |||
| 39 | &log("Starting server on port $port"); | ||
| 40 | socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; | ||
| 41 | setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 1 ) or die "setsockopt: $!"; | ||
| 42 | bind(Server,sockaddr_in($port,INADDR_ANY)) or die "bind: $!"; | ||
| 43 | listen(Server,SOMAXCONN) or die "listen: $!"; | ||
| 44 | |||
| 45 | Server->autoflush( 1 ); | ||
| 46 | |||
| 47 | my $paddr; | ||
| 48 | &log("Server started. Waiting for connections."); | ||
| 49 | |||
| 50 | $/ = "\0"; # reset terminator to null char | ||
| 51 | |||
| 52 | # listening loop. | ||
| 53 | |||
| 54 | for ( ; $paddr = accept(Client,Server); close Client) { | ||
| 55 | Client->autoflush(1); | ||
| 56 | my($port,$iaddr) = sockaddr_in($paddr); | ||
| 57 | my $ip_address = inet_ntoa($iaddr); | ||
| 58 | my $name = gethostbyaddr($iaddr,AF_INET) || $ip_address; | ||
| 59 | &log( scalar localtime() . ": Connection from $name" ); | ||
| 60 | |||
| 61 | my $line = <Client>; | ||
| 62 | &log("Input: $line"); | ||
| 63 | |||
| 64 | if ($line =~ /.*policy\-file.*/i) { | ||
| 65 | print Client &xml_policy; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | sub xml_policy { | ||
| 70 | my $str = qq(<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>\0); | ||
| 71 | return $str; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub log { | ||
| 75 | my($msg) = @_; | ||
| 76 | if ($should_be_logging) { | ||
| 77 | print LOG $msg,"\n"; | ||
| 78 | } | ||
| 79 | } | ||
| 80 |