--------------------- PatchSet 3659 Date: 2002/02/10 17:55:55 Author: serassio Branch: nt-2_3 Tag: (none) Log: Added intermittent connections patch from O'Reilly Linux DevCenter at http://linux.oreillynet.com/pub/a/linux/2001/08/02/offline_squid.html Members: include/squid-mswin.h:1.1.2.5->1.1.2.6 src/cache_manager.c:1.1.1.2.4.1.2.1->1.1.1.2.4.1.2.2 src/cf.data.pre:1.1.1.3.4.3.2.3->1.1.1.3.4.3.2.4 src/client_side.c:1.1.1.3.4.6.2.7->1.1.1.3.4.6.2.8 src/forward.c:1.1.1.3.4.1.2.5->1.1.1.3.4.1.2.6 src/fqdncache.c:1.1.1.3.4.1.2.1->1.1.1.3.4.1.2.2 src/ipcache.c:1.1.1.3.4.1.2.1->1.1.1.3.4.1.2.2 src/protos.h:1.1.1.3.4.1.2.10->1.1.1.3.4.1.2.11 src/refresh.c:1.1.1.3.4.1.2.2->1.1.1.3.4.1.2.3 src/structs.h:1.1.1.3.4.2.2.6->1.1.1.3.4.2.2.7 src/tools.c:1.1.1.3.4.1.2.14->1.1.1.3.4.1.2.15 src/win32.c:1.1.2.12->1.1.2.13 Index: squid/include/squid-mswin.h =================================================================== RCS file: /cvsroot/squid-sf//squid/include/Attic/squid-mswin.h,v retrieving revision 1.1.2.5 retrieving revision 1.1.2.6 diff -u -r1.1.2.5 -r1.1.2.6 --- squid/include/squid-mswin.h 1 Nov 2001 17:31:07 -0000 1.1.2.5 +++ squid/include/squid-mswin.h 10 Feb 2002 17:55:55 -0000 1.1.2.6 @@ -1,5 +1,5 @@ /* - * $Id: squid-mswin.h,v 1.1.2.5 2001/11/01 17:31:07 serassio Exp $ + * $Id: squid-mswin.h,v 1.1.2.6 2002/02/10 17:55:55 serassio Exp $ * * AUTHOR: Guido Serassio & Andrey Shorin * @@ -49,6 +49,7 @@ #define getpid _getpid #define geteuid() 100 #define getrusage WIN32_getrusage +#define is_interface_down WIN32_is_interface_down #define lseek _lseeki64 #define memccpy _memccpy #define mkdir(p,m) _mkdir(p) Index: squid/src/cache_manager.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/cache_manager.c,v retrieving revision 1.1.1.2.4.1.2.1 retrieving revision 1.1.1.2.4.1.2.2 diff -u -r1.1.1.2.4.1.2.1 -r1.1.1.2.4.1.2.2 --- squid/src/cache_manager.c 6 Jan 2001 12:40:12 -0000 1.1.1.2.4.1.2.1 +++ squid/src/cache_manager.c 10 Feb 2002 17:55:55 -0000 1.1.1.2.4.1.2.2 @@ -1,6 +1,6 @@ /* - * $Id: cache_manager.c,v 1.1.1.2.4.1.2.1 2001/01/06 12:40:12 hno Exp $ + * $Id: cache_manager.c,v 1.1.1.2.4.1.2.2 2002/02/10 17:55:55 serassio Exp $ * * DEBUG: section 16 Cache Manager Objects * AUTHOR: Duane Wessels @@ -284,13 +284,22 @@ static void cachemgrOfflineToggle(StoreEntry * sentry) { - Config.onoff.offline = !Config.onoff.offline; + switch(Config.offline) + { + case 0: + Config.offline=2; + break; + case 1: + break; + case 2: + Config.offline=0; + break; + } debug(16, 0) ("offline_mode now %s.\n", - Config.onoff.offline ? "ON" : "OFF"); + Config.offline ? (Config.offline==1?"MAYBE":"ON") : "OFF"); storeAppendPrintf(sentry, "offline_mode is now %s\n", - Config.onoff.offline ? "ON" : "OFF"); + Config.offline ? (Config.offline==1?"MAYBE":"ON") : "OFF"); } - static const char * cachemgrActionProtection(const action_table * at) { Index: squid/src/cf.data.pre =================================================================== RCS file: /cvsroot/squid-sf//squid/src/cf.data.pre,v retrieving revision 1.1.1.3.4.3.2.3 retrieving revision 1.1.1.3.4.3.2.4 diff -u -r1.1.1.3.4.3.2.3 -r1.1.1.3.4.3.2.4 --- squid/src/cf.data.pre 13 Aug 2001 17:09:17 -0000 1.1.1.3.4.3.2.3 +++ squid/src/cf.data.pre 10 Feb 2002 17:55:55 -0000 1.1.1.3.4.3.2.4 @@ -1,6 +1,6 @@ # -# $Id: cf.data.pre,v 1.1.1.3.4.3.2.3 2001/08/13 17:09:17 serassio Exp $ +# $Id: cf.data.pre,v 1.1.1.3.4.3.2.4 2002/02/10 17:55:55 serassio Exp $ # # # SQUID Internet Object Cache http://squid.nlanr.net/Squid/ @@ -2995,12 +2995,25 @@ DOC_NONE NAME: offline_mode -TYPE: onoff -LOC: Config.onoff.offline -DEFAULT: off +TYPE: int +LOC: Config.offline +DEFAULT: 0 DOC_START - Enable this option and Squid will never try to validate cached - objects. + If set to 2, Squid will never try to validate cached objects. + If set to 1, Squid will not try to cache objects if network_interface + is down + If set to 0, Squid will operate normally. +DOC_END + +NAME: network_interface +TYPE: eol +LOC: Config.network_interface +DEFAULT: none +DOC_START + Used in conjunction with the new offline code. If offline_mode is 1, + then the status of this interface (up or down) tells us if + we should be operating in offline mode. If it's down, we pretend + we're in offline mode. If it's up, we behave normally. DOC_END NAME: uri_whitespace Index: squid/src/client_side.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/client_side.c,v retrieving revision 1.1.1.3.4.6.2.7 retrieving revision 1.1.1.3.4.6.2.8 diff -u -r1.1.1.3.4.6.2.7 -r1.1.1.3.4.6.2.8 --- squid/src/client_side.c 27 Jan 2002 18:38:40 -0000 1.1.1.3.4.6.2.7 +++ squid/src/client_side.c 10 Feb 2002 17:55:55 -0000 1.1.1.3.4.6.2.8 @@ -1,6 +1,6 @@ /* - * $Id: client_side.c,v 1.1.1.3.4.6.2.7 2002/01/27 18:38:40 serassio Exp $ + * $Id: client_side.c,v 1.1.1.3.4.6.2.8 2002/02/10 17:55:55 serassio Exp $ * * DEBUG: section 33 Client-side Routines * AUTHOR: Duane Wessels @@ -1432,7 +1432,7 @@ */ if (e->mem_status == IN_MEMORY) http->log_type = LOG_TCP_MEM_HIT; - else if (Config.onoff.offline) + else if (check_offline()) http->log_type = LOG_TCP_OFFLINE_HIT; clientSendMoreData(data, buf, size); } @@ -1915,7 +1915,7 @@ debug(33, 3) ("clientProcessRequest2: storeGet() MISS\n"); return LOG_TCP_MISS; } - if (Config.onoff.offline) { + if (check_offline()) { debug(33, 3) ("clientProcessRequest2: offline HIT\n"); http->entry = e; return LOG_TCP_HIT; Index: squid/src/forward.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/forward.c,v retrieving revision 1.1.1.3.4.1.2.5 retrieving revision 1.1.1.3.4.1.2.6 diff -u -r1.1.1.3.4.1.2.5 -r1.1.1.3.4.1.2.6 --- squid/src/forward.c 27 Jan 2002 18:38:40 -0000 1.1.1.3.4.1.2.5 +++ squid/src/forward.c 10 Feb 2002 17:55:56 -0000 1.1.1.3.4.1.2.6 @@ -1,6 +1,6 @@ /* - * $Id: forward.c,v 1.1.1.3.4.1.2.5 2002/01/27 18:38:40 serassio Exp $ + * $Id: forward.c,v 1.1.1.3.4.1.2.6 2002/02/10 17:55:56 serassio Exp $ * * DEBUG: section 17 Request Forwarding * AUTHOR: Duane Wessels @@ -439,6 +439,16 @@ return; } } + if (check_offline()) { + /* + * Check if we're in offline mode + */ + err = errorCon(ERR_CANNOT_FORWARD, HTTP_SERVICE_UNAVAILABLE); + err->request = requestLink(r); + err->src_addr = r->client_addr; + errorAppendEntry(e, err); + return; + } debug(17, 3) ("fwdStart: '%s'\n", storeUrl(e)); e->mem_obj->request = requestLink(r); e->mem_obj->fd = fd; Index: squid/src/fqdncache.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/fqdncache.c,v retrieving revision 1.1.1.3.4.1.2.1 retrieving revision 1.1.1.3.4.1.2.2 diff -u -r1.1.1.3.4.1.2.1 -r1.1.1.3.4.1.2.2 --- squid/src/fqdncache.c 6 Jan 2001 12:40:13 -0000 1.1.1.3.4.1.2.1 +++ squid/src/fqdncache.c 10 Feb 2002 17:55:57 -0000 1.1.1.3.4.1.2.2 @@ -1,7 +1,7 @@ /* - * $Id: fqdncache.c,v 1.1.1.3.4.1.2.1 2001/01/06 12:40:13 hno Exp $ + * $Id: fqdncache.c,v 1.1.1.3.4.1.2.2 2002/02/10 17:55:57 serassio Exp $ * * DEBUG: section 35 FQDN Cache * AUTHOR: Harvest Derived @@ -123,6 +123,8 @@ static int fqdncacheExpiredEntry(const fqdncache_entry * f) { + if(check_offline()) + return 0; /* In offline mode, don't expire cached entries */ if (f->status == FQDN_PENDING) return 0; if (f->status == FQDN_DISPATCHED) @@ -197,7 +199,10 @@ if (hp) fqdncacheAddHostent(f, hp); f->status = status; - f->lastref = squid_curtime; + if(check_offline()) + f->lastref = 0; /* Expires instantly when we go online */ + else + f->lastref = squid_curtime; return f; } @@ -386,6 +391,15 @@ } if (f == NULL) { /* MISS: No entry, create the new one */ + if(check_offline()) + { + struct hostent he={ (char *)name,NULL,0,0,NULL}; + /* Fake it */ + f = fqdncacheAddNew(name,&he,FQDN_CACHED); + fqdncacheAddPending(f, handler, handlerData); + fqdncache_call_pending(f); + return; + } debug(35, 5) ("fqdncache_nbgethostbyaddr: MISS for '%s'\n", name); FqdncacheStats.misses++; f = fqdncacheAddNew(name, NULL, FQDN_PENDING); Index: squid/src/ipcache.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/ipcache.c,v retrieving revision 1.1.1.3.4.1.2.1 retrieving revision 1.1.1.3.4.1.2.2 diff -u -r1.1.1.3.4.1.2.1 -r1.1.1.3.4.1.2.2 --- squid/src/ipcache.c 6 Jan 2001 12:40:14 -0000 1.1.1.3.4.1.2.1 +++ squid/src/ipcache.c 10 Feb 2002 17:55:57 -0000 1.1.1.3.4.1.2.2 @@ -1,6 +1,6 @@ /* - * $Id: ipcache.c,v 1.1.1.3.4.1.2.1 2001/01/06 12:40:14 hno Exp $ + * $Id: ipcache.c,v 1.1.1.3.4.1.2.2 2002/02/10 17:55:57 serassio Exp $ * * DEBUG: section 14 IP Cache * AUTHOR: Harvest Derived @@ -148,6 +148,8 @@ static int ipcacheExpiredEntry(ipcache_entry * i) { + if(check_offline()) + return 0; /* In offline mode, don't expire cached entries */ if (i->status == IP_PENDING) return 0; if (i->status == IP_DISPATCHED) @@ -207,8 +209,9 @@ int k; safe_free(i->addrs.in_addrs); safe_free(i->addrs.bad_mask); - while ((addr_count < 255) && *(hp->h_addr_list + addr_count)) - ++addr_count; + if(hp->h_addr_list) + while ((addr_count < 255) && *(hp->h_addr_list + addr_count)) + ++addr_count; i->addrs.count = (unsigned char) addr_count; i->addrs.in_addrs = xcalloc(addr_count, sizeof(struct in_addr)); i->addrs.bad_mask = xcalloc(addr_count, sizeof(unsigned char)); @@ -232,7 +235,10 @@ if (hp) ipcacheAddHostent(i, hp); i->status = status; - i->lastref = squid_curtime; + if(check_offline()) + i->lastref = 0; /* Expires instantly when we go online */ + else + i->lastref = squid_curtime; return i; } @@ -460,6 +466,17 @@ } if (i == NULL) { /* MISS: No entry, create the new one */ + if(check_offline()) + { + struct hostent he={ (char *)name,NULL,0,0,NULL}; + /* Not permitted to look it up in offline mode */ + debug(14, 5) ("ipcache_nbgethostbyname: OFFLINE! Will not look it up\n"); + /* Fake it */ + i = ipcacheAddNew(name, &he, IP_CACHED); + ipcacheAddPending(i, handler, handlerData); + ipcache_call_pending(i); + return; + } debug(14, 5) ("ipcache_nbgethostbyname: MISS for '%s'\n", name); IpcacheStats.misses++; i = ipcacheAddNew(name, NULL, IP_PENDING); Index: squid/src/protos.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/protos.h,v retrieving revision 1.1.1.3.4.1.2.10 retrieving revision 1.1.1.3.4.1.2.11 diff -u -r1.1.1.3.4.1.2.10 -r1.1.1.3.4.1.2.11 --- squid/src/protos.h 6 Jan 2002 15:01:39 -0000 1.1.1.3.4.1.2.10 +++ squid/src/protos.h 10 Feb 2002 17:55:57 -0000 1.1.1.3.4.1.2.11 @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.1.1.3.4.1.2.10 2002/01/06 15:01:39 serassio Exp $ + * $Id: protos.h,v 1.1.1.3.4.1.2.11 2002/02/10 17:55:57 serassio Exp $ * * * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ @@ -1112,6 +1112,7 @@ extern int stringHasCntl(const char *); extern void linklistPush(link_list **, void *); extern void *linklistShift(link_list **); +extern int check_offline(void); #if USE_HTCP extern void htcpInit(void); @@ -1242,5 +1243,6 @@ extern void WIN32_InstallService(void); extern void WIN32_RemoveService(void); extern int WIN32_getrusage(int, struct rusage *); +extern int WIN32_is_interface_down(const char *); #endif Index: squid/src/refresh.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/refresh.c,v retrieving revision 1.1.1.3.4.1.2.2 retrieving revision 1.1.1.3.4.1.2.3 diff -u -r1.1.1.3.4.1.2.2 -r1.1.1.3.4.1.2.3 --- squid/src/refresh.c 15 Sep 2001 12:15:19 -0000 1.1.1.3.4.1.2.2 +++ squid/src/refresh.c 10 Feb 2002 17:55:57 -0000 1.1.1.3.4.1.2.3 @@ -1,7 +1,7 @@ /* - * $Id: refresh.c,v 1.1.1.3.4.1.2.2 2001/09/15 12:15:19 serassio Exp $ + * $Id: refresh.c,v 1.1.1.3.4.1.2.3 2002/02/10 17:55:57 serassio Exp $ * * DEBUG: section 22 Refresh Calculation * AUTHOR: Harvest Derived @@ -289,6 +289,8 @@ int refreshCheckHTTP(const StoreEntry * entry, request_t * request) { + if(check_offline()) + return 0; /* If we're offline, we're always fresh */ return refreshCheck(entry, request, 0, &refreshCounts[rcHTTP]); } Index: squid/src/structs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/structs.h,v retrieving revision 1.1.1.3.4.2.2.6 retrieving revision 1.1.1.3.4.2.2.7 diff -u -r1.1.1.3.4.2.2.6 -r1.1.1.3.4.2.2.7 --- squid/src/structs.h 15 Sep 2001 12:15:19 -0000 1.1.1.3.4.2.2.6 +++ squid/src/structs.h 10 Feb 2002 17:55:57 -0000 1.1.1.3.4.2.2.7 @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.1.1.3.4.2.2.6 2001/09/15 12:15:19 serassio Exp $ + * $Id: structs.h,v 1.1.1.3.4.2.2.7 2002/02/10 17:55:57 serassio Exp $ * * * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ @@ -406,7 +406,6 @@ #if HTTP_VIOLATIONS int reload_into_ims; #endif - int offline; int redir_rewrites_host; int prefer_direct; int strip_query_terms; @@ -420,6 +419,8 @@ int log_redirected_url; int ie_refresh; } onoff; + int offline; + char *network_interface; acl *aclList; struct { acl_access *http; Index: squid/src/tools.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/tools.c,v retrieving revision 1.1.1.3.4.1.2.14 retrieving revision 1.1.1.3.4.1.2.15 diff -u -r1.1.1.3.4.1.2.14 -r1.1.1.3.4.1.2.15 --- squid/src/tools.c 8 Dec 2001 09:22:06 -0000 1.1.1.3.4.1.2.14 +++ squid/src/tools.c 10 Feb 2002 17:55:57 -0000 1.1.1.3.4.1.2.15 @@ -1,6 +1,6 @@ /* - * $Id: tools.c,v 1.1.1.3.4.1.2.14 2001/12/08 09:22:06 serassio Exp $ + * $Id: tools.c,v 1.1.1.3.4.1.2.15 2002/02/10 17:55:57 serassio Exp $ * * DEBUG: section 21 Misc Functions * AUTHOR: Harvest Derived @@ -913,3 +913,68 @@ } return 0; } + +#ifndef _SQUID_MSWIN_ +#include +#include + +/* int is_interface_down(const char *interface_name) + * + * returns 0 if the interface exists and is UP + * returns 1 if the interface is down, does not exist or there is an + * error trying to create a datagram socket to query interface status + */ +int +is_interface_down(const char *interface_name) +{ + int r,sockfd; + struct ifreq ifr; + + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) + { + return 1; /* Cannot create a socket. + I guess we can assume that the network is + nil operandum */ + } + strncpy(ifr.ifr_name, interface_name, IFNAMSIZ); + ifr.ifr_name[IFNAMSIZ-1] = '\0'; + if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) == -1) + { + close(sockfd); + return 1; /* Interface probably doesn't exist */ + } + if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) + r=1; /* Down */ + else + r=0; /* UP */ + + close(sockfd); + return r; +} +#endif + +/* + * int check_offline(void) + * Returns 0 if offline_mode is 0. + * Returns 0 if offline_mode is 1 and the network_interface is up. + * Returns 1 if offline_mode is 1 and the network_interface is down. + * Returns 1 if offline_mode is 2. + */ +int +check_offline(void) +{ + static time_t last_checked=0; + static int last_status=0; + time_t now; + + /* Only check once per 5 second or so. */ + time(&now); + if(now<=last_checked) + return last_status; + last_checked=now+5; + if(Config.offline!=1) + return Config.offline; + last_status=is_interface_down(Config.network_interface); + return last_status; +} Index: squid/src/win32.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/win32.c,v retrieving revision 1.1.2.12 retrieving revision 1.1.2.13 diff -u -r1.1.2.12 -r1.1.2.13 --- squid/src/win32.c 3 Feb 2002 15:15:35 -0000 1.1.2.12 +++ squid/src/win32.c 10 Feb 2002 17:55:57 -0000 1.1.2.13 @@ -43,6 +43,7 @@ /* The following code section is part of an EXPERIMENTAL native */ /* Windows NT/2000 Squid port - Compiles only on MS Visual C++ */ #if defined(_SQUID_MSWIN_) +#include static int Win32SockInit(void); static void Win32SockCleanup(void); #endif /* End native Windows NT EXPERIMENTAL PORT */ @@ -750,6 +751,29 @@ return 0; } +int WIN32_is_interface_down(const char *interface_name) +{ + PMIB_IFTABLE pIfTable = NULL; /* buffer for interface table */ + ULONG pdwSize=0; /* size of buffer */ + int i, ret=1; + char buffer[MAX_INTERFACE_NAME_LEN+1]="\0"; + + safe_free(pIfTable); + GetIfTable(pIfTable, &pdwSize, FALSE); + pIfTable = (PMIB_IFTABLE) xmalloc((unsigned int) pdwSize); + GetIfTable(pIfTable, &pdwSize, FALSE); + for (i=0; i < (int) pIfTable->dwNumEntries; i++) + { + if ((strcmp(interface_name, pIfTable->table[i].bDescr) == 0)) + { + debug(1,3) ("Interface %s: status: UP, Admin: %i, Oper: %i \n", interface_name, pIfTable->table[i].dwAdminStatus, pIfTable->table[i].dwOperStatus); + ret=0; + } + } + safe_free(pIfTable); + return ret; +} + static int Win32SockInit(void) { int iVersionRequested;