2 * DEBUG: section 46 Access Log
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "AccessLogEntry.h"
35 #include "acl/Checklist.h"
37 #include "adaptation/Config.h"
39 #include "CachePeer.h"
40 #include "err_detail_type.h"
41 #include "errorpage.h"
42 #include "errorpage.h"
43 #include "errorpage.h"
44 #include "format/Token.h"
46 #include "hier_code.h"
47 #include "HttpReply.h"
48 #include "HttpRequest.h"
49 #include "log/access_log.h"
50 #include "log/Config.h"
51 #include "log/CustomLog.h"
53 #include "log/Formats.h"
55 #include "mgr/Registration.h"
57 #include "SquidConfig.h"
58 #include "SquidTime.h"
62 #include "eui/Eui48.h"
63 #include "eui/Eui64.h"
67 static Logfile
*headerslog
= NULL
;
70 #if MULTICAST_MISS_STREAM
71 static int mcast_miss_fd
= -1;
73 static struct sockaddr_in mcast_miss_to
;
74 static void mcast_encode(unsigned int *, size_t, const unsigned int *);
83 static hash_table
*via_table
= NULL
;
84 static hash_table
*forw_table
= NULL
;
85 static void fvdbInit();
86 static void fvdbDumpTable(StoreEntry
* e
, hash_table
* hash
);
87 static void fvdbCount(hash_table
* hash
, const char *key
);
88 static OBJH fvdbDumpVia
;
89 static OBJH fvdbDumpForw
;
90 static FREE fvdbFreeEntry
;
91 static void fvdbClear(void);
92 static void fvdbRegisterWithCacheManager();
95 int LogfileStatus
= LOG_DISABLE
;
98 accessLogLogTo(CustomLog
* log
, AccessLogEntry::Pointer
&al
, ACLChecklist
* checklist
)
104 if (!al
->http
.content_type
|| *al
->http
.content_type
== '\0')
105 al
->http
.content_type
= dash_str
;
108 al
->_private
.method_str
= icp_opcode_str
[al
->icp
.opcode
];
109 else if (al
->htcp
.opcode
)
110 al
->_private
.method_str
= al
->htcp
.opcode
;
112 al
->_private
.method_str
= RequestMethodStr(al
->http
.method
);
114 if (al
->hier
.host
[0] == '\0')
115 xstrncpy(al
->hier
.host
, dash_str
, SQUIDHOSTNAMELEN
);
117 for (; log
; log
= log
->next
) {
118 if (log
->aclList
&& checklist
&& checklist
->fastCheck(log
->aclList
) != ACCESS_ALLOWED
)
122 logfileLineStart(log
->logfile
);
126 case Log::Format::CLF_SQUID
:
127 Log::Format::SquidNative(al
, log
->logfile
);
130 case Log::Format::CLF_COMBINED
:
131 Log::Format::HttpdCombined(al
, log
->logfile
);
134 case Log::Format::CLF_COMMON
:
135 Log::Format::HttpdCommon(al
, log
->logfile
);
138 case Log::Format::CLF_REFERER
:
139 Log::Format::SquidReferer(al
, log
->logfile
);
142 case Log::Format::CLF_USERAGENT
:
143 Log::Format::SquidUserAgent(al
, log
->logfile
);
146 case Log::Format::CLF_CUSTOM
:
147 Log::Format::SquidCustom(al
, log
);
151 case Log::Format::CLF_ICAP_SQUID
:
152 Log::Format::SquidIcap(al
, log
->logfile
);
156 case Log::Format::CLF_NONE
:
160 fatalf("Unknown log format %d\n", log
->type
);
164 logfileLineEnd(log
->logfile
);
167 // NP: WTF? if _any_ log line has no checklist ignore the following ones?
174 accessLogLog(AccessLogEntry::Pointer
&al
, ACLChecklist
* checklist
)
176 if (LogfileStatus
!= LOG_ENABLE
)
179 accessLogLogTo(Config
.Log
.accesslogs
, al
, checklist
);
180 #if MULTICAST_MISS_STREAM
182 if (al
->cache
.code
!= LOG_TCP_MISS
)
184 else if (al
->http
.method
!= METHOD_GET
)
186 else if (mcast_miss_fd
< 0)
189 unsigned int ibuf
[365];
191 xstrncpy((char *) ibuf
, al
->url
, 364 * sizeof(int));
192 isize
= ((strlen(al
->url
) + 8) / 8) * 2;
197 mcast_encode((unsigned int *) ibuf
, isize
,
198 (const unsigned int *) Config
.mcast_miss
.encode_key
);
200 comm_udp_sendto(mcast_miss_fd
,
201 &mcast_miss_to
, sizeof(mcast_miss_to
),
202 ibuf
, isize
* sizeof(int));
209 accessLogRotate(void)
217 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
219 logfileRotate(log
->logfile
);
225 logfileRotate(headerslog
);
235 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
237 logfileClose(log
->logfile
);
244 logfileClose(headerslog
);
251 HierarchyLogEntry::HierarchyLogEntry() :
253 cd_lookup(LOOKUP_NONE
),
256 peer_reply_status(Http::scNone
),
257 peer_response_time(-1),
258 total_response_time(-1),
262 memset(host
, '\0', SQUIDHOSTNAMELEN
);
263 memset(cd_host
, '\0', SQUIDHOSTNAMELEN
);
265 peer_select_start
.tv_sec
=0;
266 peer_select_start
.tv_usec
=0;
268 store_complete_stop
.tv_sec
=0;
269 store_complete_stop
.tv_usec
=0;
271 peer_http_request_sent
.tv_sec
= 0;
272 peer_http_request_sent
.tv_usec
= 0;
274 first_conn_start
.tv_sec
= 0;
275 first_conn_start
.tv_usec
= 0;
279 HierarchyLogEntry::note(const Comm::ConnectionPointer
&server
, const char *requestedHost
)
282 if (tcpServer
== NULL
) {
284 xstrncpy(host
, requestedHost
, sizeof(host
));
286 code
= tcpServer
->peerType
;
288 if (tcpServer
->getPeer()) {
289 // went to peer, log peer host name
290 xstrncpy(host
, tcpServer
->getPeer()->name
, sizeof(host
));
292 xstrncpy(host
, requestedHost
, sizeof(host
));
298 accessLogRegisterWithCacheManager(void)
301 fvdbRegisterWithCacheManager();
310 accessLogRegisterWithCacheManager();
313 Log::TheConfig
.hasAdaptToken
= false;
316 Log::TheConfig
.hasIcapToken
= false;
319 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
320 if (log
->type
== Log::Format::CLF_NONE
)
323 log
->logfile
= logfileOpen(log
->filename
, log
->bufferSize
, log
->fatal
);
325 LogfileStatus
= LOG_ENABLE
;
328 for (Format::Token
* curr_token
= (log
->logFormat
?log
->logFormat
->format
:NULL
); curr_token
; curr_token
= curr_token
->next
) {
329 if (curr_token
->type
== Format::LFT_ADAPTATION_SUM_XACT_TIMES
||
330 curr_token
->type
== Format::LFT_ADAPTATION_ALL_XACT_TIMES
||
331 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER
||
332 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER_ELEM
||
333 curr_token
->type
== Format::LFT_ADAPTATION_LAST_ALL_HEADERS
||
334 (curr_token
->type
== Format::LFT_NOTE
&& !Adaptation::Config::metaHeaders
.empty())) {
335 Log::TheConfig
.hasAdaptToken
= true;
338 if (curr_token
->type
== Format::LFT_ICAP_TOTAL_TIME
) {
339 Log::TheConfig
.hasIcapToken
= true;
348 headerslog
= logfileOpen("/usr/local/squid/logs/headers.log", 512);
350 assert(NULL
!= headerslog
);
353 #if MULTICAST_MISS_STREAM
355 if (Config
.mcast_miss
.addr
.s_addr
!= no_addr
.s_addr
) {
356 memset(&mcast_miss_to
, '\0', sizeof(mcast_miss_to
));
357 mcast_miss_to
.sin_family
= AF_INET
;
358 mcast_miss_to
.sin_port
= htons(Config
.mcast_miss
.port
);
359 mcast_miss_to
.sin_addr
.s_addr
= Config
.mcast_miss
.addr
.s_addr
;
360 mcast_miss_fd
= comm_open(SOCK_DGRAM
,
362 Config
.Addrs
.udp_incoming
,
363 Config
.mcast_miss
.port
,
365 "Multicast Miss Stream");
367 if (mcast_miss_fd
< 0)
368 fatal("Cannot open Multicast Miss Stream Socket");
370 debugs(46, DBG_IMPORTANT
, "Multicast Miss Stream Socket opened on FD " << mcast_miss_fd
);
372 mcastSetTtl(mcast_miss_fd
, Config
.mcast_miss
.ttl
);
374 if (strlen(Config
.mcast_miss
.encode_key
) < 16)
375 fatal("mcast_encode_key is too short, must be 16 characters");
391 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
392 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
396 fvdbRegisterWithCacheManager(void)
398 Mgr::RegisterAction("via_headers", "Via Request Headers", fvdbDumpVia
, 0, 1);
399 Mgr::RegisterAction("forw_headers", "X-Forwarded-For Request Headers",
404 fvdbCount(hash_table
* hash
, const char *key
)
411 fv
= (fvdb_entry
*)hash_lookup(hash
, key
);
414 fv
= static_cast <fvdb_entry
*>(xcalloc(1, sizeof(fvdb_entry
)));
415 fv
->hash
.key
= xstrdup(key
);
416 hash_join(hash
, &fv
->hash
);
423 fvdbCountVia(const char *key
)
425 fvdbCount(via_table
, key
);
429 fvdbCountForw(const char *key
)
431 fvdbCount(forw_table
, key
);
435 fvdbDumpTable(StoreEntry
* e
, hash_table
* hash
)
445 while ((h
= hash_next(hash
))) {
446 fv
= (fvdb_entry
*) h
;
447 storeAppendPrintf(e
, "%9d %s\n", fv
->n
, hashKeyStr(&fv
->hash
));
452 fvdbDumpVia(StoreEntry
* e
)
454 fvdbDumpTable(e
, via_table
);
458 fvdbDumpForw(StoreEntry
* e
)
460 fvdbDumpTable(e
, forw_table
);
465 fvdbFreeEntry(void *data
)
467 fvdb_entry
*fv
= static_cast <fvdb_entry
*>(data
);
475 hashFreeItems(via_table
, fvdbFreeEntry
);
476 hashFreeMemory(via_table
);
477 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
478 hashFreeItems(forw_table
, fvdbFreeEntry
);
479 hashFreeMemory(forw_table
);
480 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
485 #if MULTICAST_MISS_STREAM
487 * From http://www.io.com/~paulhart/game/algorithms/tea.html
489 * size of 'ibuf' must be a multiple of 2.
490 * size of 'key' must be 4.
491 * 'ibuf' is modified in place, encrypted data is written in
492 * network byte order.
495 mcast_encode(unsigned int *ibuf
, size_t isize
, const unsigned int *key
)
500 const unsigned int delta
= 0x9e3779b9;
502 const unsigned int k0
= htonl(key
[0]);
503 const unsigned int k1
= htonl(key
[1]);
504 const unsigned int k2
= htonl(key
[2]);
505 const unsigned int k3
= htonl(key
[3]);
508 for (i
= 0; i
< isize
; i
+= 2) {
510 z
= htonl(ibuf
[i
+ 1]);
513 for (n
= 32; n
; --n
) {
515 y
+= (z
<< 4) + (k0
^ z
) + (sum
^ (z
>> 5)) + k1
;
516 z
+= (y
<< 4) + (k2
^ y
) + (sum
^ (y
>> 5)) + k3
;
520 ibuf
[i
+ 1] = htonl(z
);
528 headersLog(int cs
, int pq
, const HttpRequestMethod
& method
, void *data
)
532 unsigned short magic
= 0;
533 unsigned char M
= (unsigned char) m
;
542 hmask
= rep
->header
.mask
;
544 if (rep
->cache_control
)
545 ccmask
= rep
->cache_control
->mask
;
551 hmask
= req
->header
.mask
;
553 if (req
->cache_control
)
554 ccmask
= req
->cache_control
->mask
;
565 magic
= htons(magic
);
566 ccmask
= htonl(ccmask
);
568 unsigned short S
= 0;
570 S
= static_cast<unsigned short>(rep
->sline
.status());
572 logfileWrite(headerslog
, &magic
, sizeof(magic
));
573 logfileWrite(headerslog
, &M
, sizeof(M
));
574 logfileWrite(headerslog
, &S
, sizeof(S
));
575 logfileWrite(headerslog
, hmask
, sizeof(HttpHeaderMask
));
576 logfileWrite(headerslog
, &ccmask
, sizeof(int));