HTCP Multicast Squid purging support
authorMark Bergsma <mark@users.mediawiki.org>
Wed, 6 Apr 2005 17:14:43 +0000 (17:14 +0000)
committerMark Bergsma <mark@users.mediawiki.org>
Wed, 6 Apr 2005 17:14:43 +0000 (17:14 +0000)
Uses (UDP) HTCP packets to send 'CLR' (purge) requests to Squids,
using a multicast group address. Needs a patched Squid (Squid doesn't
support HTCP CLR):
http://www.nedworks.org/~mark/patches/squid-htcp-clr.diff

includes/DefaultSettings.php
includes/SquidUpdate.php

index da27dda..fadfd72 100644 (file)
@@ -662,6 +662,10 @@ $wgSquidServersNoPurge = array();
 /** Maximum number of titles to purge in any one client operation */
 $wgMaxSquidPurgeTitles = 400;
 
+/** HTCP multicast purging */
+$wgHTCPPort = 4827;
+$wgHTCPMulticastTTL = 1;
+# $wgHTCPMulticastAddress = "224.0.0.85";
 
 # Cookie settings:
 #
index 294fca4..d971d0a 100644 (file)
@@ -91,13 +91,16 @@ class SquidUpdate {
        XXX report broken Squids per mail or log */
 
        /* static */ function purge( $urlArr ) {
-               global  $wgSquidServers;
+               global $wgSquidServers, $wgHTCPMulticastAddress, $wgHTCPPort;
 
                if ( $wgSquidServers == 'echo' ) {
                        echo implode("<br />\n", $urlArr);
                        return;
                }
 
+               if ( $wgHTCPMulticastAddress && $wgHTCPPort )
+                       SquidUpdate::HTCPPurge( $urlArr );
+
                $fname = 'SquidUpdate::purge';
                wfProfileIn( $fname );
                
@@ -200,6 +203,59 @@ class SquidUpdate {
                wfProfileOut( $fname );
        }
 
+       /* static */ function HTCPPurge( $urlArr ) {
+               global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort;
+               $fname = 'SquidUpdate::HTCPPurge';
+               wfProfileIn( $fname );
+
+               $htcpOpCLR = 4;                 // HTCP CLR
+
+               // FIXME PHP doesn't support these socket constants (include/linux/in.h)
+               define( "IPPROTO_IP", 0 );
+               define( "IP_MULTICAST_LOOP", 34 );
+               define( "IP_MULTICAST_TTL", 33 );
+
+               // pfsockopen doesn't work because we need set_sock_opt
+               $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
+               if ( $conn ) {
+                       // Set socket options
+                       socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
+                       if ( $wgHTCPMulticastTTL != 1 )
+                               socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
+                                       $wgHTCPMulticastTTL );
+
+                       foreach ( $urlArr as $url ) {
+                               // Construct a minimal HTCP request diagram
+                               // as per RFC 2756
+                               // Opcode 'CLR', no response desired, no auth
+                               $htcpTransID = rand();
+
+                               $htcpSpecifier = pack( 'na4na*na8n',
+                                       4, 'NONE', strlen( $url ), $url,
+                                       8, 'HTTP/1.0', 0 );
+
+                               $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
+                               $htcpLen = 4 + $htcpDataLen + 2;
+
+                               // Note! Squid gets the bit order of the first
+                               // word wrong, wrt the RFC. Apparently no other
+                               // implementation exists, so adapt to Squid
+                               $htcpPacket = pack( 'nxxnCxNxxa*n',
+                                       $htcpLen, $htcpDataLen, $htcpOpCLR,
+                                       $htcpTransID, $htcpSpecifier, 2);
+
+                               // Send out
+                               debug( "Purging URL $url via HTCP\n" );
+                               socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
+                                       $wgHTCPMulticastAddress, $wgHTCPPort );
+                       }
+               } else {
+                       $errstr = socket_strerror( socket_last_error() );
+                       debug( "SquidUpdate::HTCPPurge(): Error opening UDP socket: $errstr\n" );
+               }
+               wfProfileOut( $fname );
+       }
+
        function debug( $text ) {
                global $wgDebugSquid;
                if ( $wgDebugSquid ) {