icpserver.pl
上传用户:liugui
上传日期:2007-01-04
资源大小:822k
文件大小:4k
- #!/usr/local/bin/perl
- # parse and answer ICP type 1 requests via unicast/multicast UDP
- # cf. <URL:http://excalibur.usc.edu/icpdoc/icp.html>
- #
- # returns ICP response code, e.g. 2 == HIT, 3 == MISS, 4 == ERROR
- # by looking at CERN or Netscape style cache directory $cachedir
- #
- # martin hamilton <m.t.hamilton@lut.ac.uk>
- # Id: icpserver,v 1.11 1995/11/24 16:20:13 martin Exp martin
- # usage: icpserver [-c cachedir] [-n] [-p port] [multicast_group]
- #
- # -c -> set cache directory
- # -n -> use Netscape cache format (default is CERN)
- # -p -> port number to listen on (default 3130)
- # -v -> verbose - writes activitiy log to stderr
- #
- # group -> multicast group to listen on
- require "getopts.pl";
- &Getopts("c:np:v");
- @CODES=("xxx", "QUERY", "HIT", "MISS", "ERROR");
- $CACHEDIR=$opt_c||"/usr/local/www/cache";
- $PORT=$opt_p||3130;
- $SERVER=$ARGV[0]||"0.0.0.0";
- $SERVERIP= ($SERVER =~ m!d+.d+.d+.d+!) ?
- pack("C4", split(/./, $SERVER)) : (gethostbyname($SERVER))[4]; # lazy!
- $SOCKADDR = 'S n a4 x8';
- socket(S, 2, 2, 17) || socket(S, 2, 1, 17) || die "Couldn't get socket: $!";
- $us1 = pack($SOCKADDR, 2, $PORT, $SERVERIP);
- $us2 = pack($SOCKADDR, 2, $PORT, pack("C4", 0,0,0,0));
- bind(S, $us1) || bind(S, $us2) || die "Couldn't bind socket: $!";
- #bind(S, $us2) || die "Couldn't bind socket: $!";
- if ($SERVER ne "0.0.0.0") { # i.e. multicast
- $whoami = (`uname -a`)[0];
- $IP_ADD_MEMBERSHIP=5;
- $whoami =~ /SunOS [^s]+ 5/ && ($IP_MULTICAST_TTL=19);
- $whoami =~ /IRIX [^s]+ 5/ && ($IP_MULTICAST_TTL=23);
- $whoami =~ /OSF1/ && ($IP_MULTICAST_TTL=12);
- # any more funnies ?
- setsockopt(S, 0, $IP_ADD_MEMBERSHIP, $SERVERIP."