2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 46 Access Log */
12 #include "AccessLogEntry.h"
13 #include "acl/Checklist.h"
15 #include "adaptation/Config.h"
17 #include "CachePeer.h"
18 #include "err_detail_type.h"
19 #include "errorpage.h"
20 #include "errorpage.h"
21 #include "errorpage.h"
22 #include "format/Token.h"
24 #include "hier_code.h"
25 #include "HttpReply.h"
26 #include "HttpRequest.h"
27 #include "log/access_log.h"
28 #include "log/Config.h"
29 #include "log/CustomLog.h"
31 #include "log/Formats.h"
33 #include "mgr/Registration.h"
35 #include "SquidConfig.h"
36 #include "SquidTime.h"
40 #include "eui/Eui48.h"
41 #include "eui/Eui64.h"
45 static Logfile
*headerslog
= NULL
;
48 #if MULTICAST_MISS_STREAM
49 static int mcast_miss_fd
= -1;
51 static struct sockaddr_in mcast_miss_to
;
52 static void mcast_encode(unsigned int *, size_t, const unsigned int *);
61 static hash_table
*via_table
= NULL
;
62 static hash_table
*forw_table
= NULL
;
63 static void fvdbInit();
64 static void fvdbDumpTable(StoreEntry
* e
, hash_table
* hash
);
65 static void fvdbCount(hash_table
* hash
, const char *key
);
66 static OBJH fvdbDumpVia
;
67 static OBJH fvdbDumpForw
;
68 static FREE fvdbFreeEntry
;
69 static void fvdbClear(void);
70 static void fvdbRegisterWithCacheManager();
73 int LogfileStatus
= LOG_DISABLE
;
76 accessLogLogTo(CustomLog
* log
, AccessLogEntry::Pointer
&al
, ACLChecklist
* checklist
)
82 if (!al
->http
.content_type
|| *al
->http
.content_type
== '\0')
83 al
->http
.content_type
= dash_str
;
86 al
->_private
.method_str
= icp_opcode_str
[al
->icp
.opcode
];
87 else if (al
->htcp
.opcode
)
88 al
->_private
.method_str
= al
->htcp
.opcode
;
90 al
->_private
.method_str
= NULL
;
92 if (al
->hier
.host
[0] == '\0')
93 xstrncpy(al
->hier
.host
, dash_str
, SQUIDHOSTNAMELEN
);
95 for (; log
; log
= log
->next
) {
96 if (log
->aclList
&& checklist
&& checklist
->fastCheck(log
->aclList
) != ACCESS_ALLOWED
)
100 logfileLineStart(log
->logfile
);
104 case Log::Format::CLF_SQUID
:
105 Log::Format::SquidNative(al
, log
->logfile
);
108 case Log::Format::CLF_COMBINED
:
109 Log::Format::HttpdCombined(al
, log
->logfile
);
112 case Log::Format::CLF_COMMON
:
113 Log::Format::HttpdCommon(al
, log
->logfile
);
116 case Log::Format::CLF_REFERER
:
117 Log::Format::SquidReferer(al
, log
->logfile
);
120 case Log::Format::CLF_USERAGENT
:
121 Log::Format::SquidUserAgent(al
, log
->logfile
);
124 case Log::Format::CLF_CUSTOM
:
125 Log::Format::SquidCustom(al
, log
);
129 case Log::Format::CLF_ICAP_SQUID
:
130 Log::Format::SquidIcap(al
, log
->logfile
);
134 case Log::Format::CLF_NONE
:
138 fatalf("Unknown log format %d\n", log
->type
);
142 logfileLineEnd(log
->logfile
);
145 // NP: WTF? if _any_ log line has no checklist ignore the following ones?
152 accessLogLog(AccessLogEntry::Pointer
&al
, ACLChecklist
* checklist
)
154 if (LogfileStatus
!= LOG_ENABLE
)
157 accessLogLogTo(Config
.Log
.accesslogs
, al
, checklist
);
158 #if MULTICAST_MISS_STREAM
160 if (al
->cache
.code
!= LOG_TCP_MISS
)
162 else if (al
->http
.method
!= METHOD_GET
)
164 else if (mcast_miss_fd
< 0)
167 unsigned int ibuf
[365];
169 xstrncpy((char *) ibuf
, al
->url
, 364 * sizeof(int));
170 isize
= ((strlen(al
->url
) + 8) / 8) * 2;
175 mcast_encode((unsigned int *) ibuf
, isize
,
176 (const unsigned int *) Config
.mcast_miss
.encode_key
);
178 comm_udp_sendto(mcast_miss_fd
,
179 &mcast_miss_to
, sizeof(mcast_miss_to
),
180 ibuf
, isize
* sizeof(int));
187 accessLogRotate(void)
195 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
197 logfileRotate(log
->logfile
);
203 logfileRotate(headerslog
);
213 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
215 logfileClose(log
->logfile
);
222 logfileClose(headerslog
);
229 HierarchyLogEntry::HierarchyLogEntry() :
231 cd_lookup(LOOKUP_NONE
),
234 peer_reply_status(Http::scNone
),
238 memset(host
, '\0', SQUIDHOSTNAMELEN
);
239 memset(cd_host
, '\0', SQUIDHOSTNAMELEN
);
241 peer_select_start
.tv_sec
=0;
242 peer_select_start
.tv_usec
=0;
244 store_complete_stop
.tv_sec
=0;
245 store_complete_stop
.tv_usec
=0;
247 peer_http_request_sent
.tv_sec
= 0;
248 peer_http_request_sent
.tv_usec
= 0;
250 peer_response_time
.tv_sec
= -1;
251 peer_response_time
.tv_usec
= 0;
253 totalResponseTime_
.tv_sec
= -1;
254 totalResponseTime_
.tv_usec
= 0;
256 firstConnStart_
.tv_sec
= 0;
257 firstConnStart_
.tv_usec
= 0;
261 HierarchyLogEntry::note(const Comm::ConnectionPointer
&server
, const char *requestedHost
)
264 if (tcpServer
== NULL
) {
266 xstrncpy(host
, requestedHost
, sizeof(host
));
268 code
= tcpServer
->peerType
;
270 if (tcpServer
->getPeer()) {
271 // went to peer, log peer host name
272 xstrncpy(host
, tcpServer
->getPeer()->name
, sizeof(host
));
274 xstrncpy(host
, requestedHost
, sizeof(host
));
280 HierarchyLogEntry::startPeerClock()
282 if (!firstConnStart_
.tv_sec
)
283 firstConnStart_
= current_time
;
287 HierarchyLogEntry::stopPeerClock(const bool force
)
289 debugs(46, 5, "First connection started: " << firstConnStart_
.tv_sec
<< "." <<
290 std::setfill('0') << std::setw(6) << firstConnStart_
.tv_usec
<<
291 ", current total response time value: " << (totalResponseTime_
.tv_sec
* 1000 + totalResponseTime_
.tv_usec
/1000) <<
292 (force
? ", force fixing" : ""));
293 if (!force
&& totalResponseTime_
.tv_sec
!= -1)
296 if (firstConnStart_
.tv_sec
)
297 tvSub(totalResponseTime_
, firstConnStart_
, current_time
);
301 HierarchyLogEntry::totalResponseTime(struct timeval
&responseTime
)
303 // This should not really happen, but there may be rare code
304 // paths that lead to FwdState discarded (or transaction logged)
305 // without (or before) a stopPeerClock() call.
306 if (firstConnStart_
.tv_sec
&& totalResponseTime_
.tv_sec
== -1)
307 stopPeerClock(false);
309 responseTime
= totalResponseTime_
;
313 accessLogRegisterWithCacheManager(void)
316 fvdbRegisterWithCacheManager();
325 accessLogRegisterWithCacheManager();
328 Log::TheConfig
.hasAdaptToken
= false;
331 Log::TheConfig
.hasIcapToken
= false;
334 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
335 if (log
->type
== Log::Format::CLF_NONE
)
338 log
->logfile
= logfileOpen(log
->filename
, log
->bufferSize
, log
->fatal
);
340 LogfileStatus
= LOG_ENABLE
;
343 for (Format::Token
* curr_token
= (log
->logFormat
?log
->logFormat
->format
:NULL
); curr_token
; curr_token
= curr_token
->next
) {
344 if (curr_token
->type
== Format::LFT_ADAPTATION_SUM_XACT_TIMES
||
345 curr_token
->type
== Format::LFT_ADAPTATION_ALL_XACT_TIMES
||
346 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER
||
347 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER_ELEM
||
348 curr_token
->type
== Format::LFT_ADAPTATION_LAST_ALL_HEADERS
||
349 (curr_token
->type
== Format::LFT_NOTE
&& !Adaptation::Config::metaHeaders
.empty())) {
350 Log::TheConfig
.hasAdaptToken
= true;
353 if (curr_token
->type
== Format::LFT_ICAP_TOTAL_TIME
) {
354 Log::TheConfig
.hasIcapToken
= true;
363 headerslog
= logfileOpen("/usr/local/squid/logs/headers.log", 512);
365 assert(NULL
!= headerslog
);
368 #if MULTICAST_MISS_STREAM
370 if (Config
.mcast_miss
.addr
.s_addr
!= no_addr
.s_addr
) {
371 memset(&mcast_miss_to
, '\0', sizeof(mcast_miss_to
));
372 mcast_miss_to
.sin_family
= AF_INET
;
373 mcast_miss_to
.sin_port
= htons(Config
.mcast_miss
.port
);
374 mcast_miss_to
.sin_addr
.s_addr
= Config
.mcast_miss
.addr
.s_addr
;
375 mcast_miss_fd
= comm_open(SOCK_DGRAM
,
377 Config
.Addrs
.udp_incoming
,
378 Config
.mcast_miss
.port
,
380 "Multicast Miss Stream");
382 if (mcast_miss_fd
< 0)
383 fatal("Cannot open Multicast Miss Stream Socket");
385 debugs(46, DBG_IMPORTANT
, "Multicast Miss Stream Socket opened on FD " << mcast_miss_fd
);
387 mcastSetTtl(mcast_miss_fd
, Config
.mcast_miss
.ttl
);
389 if (strlen(Config
.mcast_miss
.encode_key
) < 16)
390 fatal("mcast_encode_key is too short, must be 16 characters");
406 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
407 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
411 fvdbRegisterWithCacheManager(void)
413 Mgr::RegisterAction("via_headers", "Via Request Headers", fvdbDumpVia
, 0, 1);
414 Mgr::RegisterAction("forw_headers", "X-Forwarded-For Request Headers",
419 fvdbCount(hash_table
* hash
, const char *key
)
426 fv
= (fvdb_entry
*)hash_lookup(hash
, key
);
429 fv
= static_cast <fvdb_entry
*>(xcalloc(1, sizeof(fvdb_entry
)));
430 fv
->hash
.key
= xstrdup(key
);
431 hash_join(hash
, &fv
->hash
);
438 fvdbCountVia(const char *key
)
440 fvdbCount(via_table
, key
);
444 fvdbCountForw(const char *key
)
446 fvdbCount(forw_table
, key
);
450 fvdbDumpTable(StoreEntry
* e
, hash_table
* hash
)
460 while ((h
= hash_next(hash
))) {
461 fv
= (fvdb_entry
*) h
;
462 storeAppendPrintf(e
, "%9d %s\n", fv
->n
, hashKeyStr(&fv
->hash
));
467 fvdbDumpVia(StoreEntry
* e
)
469 fvdbDumpTable(e
, via_table
);
473 fvdbDumpForw(StoreEntry
* e
)
475 fvdbDumpTable(e
, forw_table
);
480 fvdbFreeEntry(void *data
)
482 fvdb_entry
*fv
= static_cast <fvdb_entry
*>(data
);
490 hashFreeItems(via_table
, fvdbFreeEntry
);
491 hashFreeMemory(via_table
);
492 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
493 hashFreeItems(forw_table
, fvdbFreeEntry
);
494 hashFreeMemory(forw_table
);
495 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
500 #if MULTICAST_MISS_STREAM
502 * From http://www.io.com/~paulhart/game/algorithms/tea.html
504 * size of 'ibuf' must be a multiple of 2.
505 * size of 'key' must be 4.
506 * 'ibuf' is modified in place, encrypted data is written in
507 * network byte order.
510 mcast_encode(unsigned int *ibuf
, size_t isize
, const unsigned int *key
)
515 const unsigned int delta
= 0x9e3779b9;
517 const unsigned int k0
= htonl(key
[0]);
518 const unsigned int k1
= htonl(key
[1]);
519 const unsigned int k2
= htonl(key
[2]);
520 const unsigned int k3
= htonl(key
[3]);
523 for (i
= 0; i
< isize
; i
+= 2) {
525 z
= htonl(ibuf
[i
+ 1]);
528 for (n
= 32; n
; --n
) {
530 y
+= (z
<< 4) + (k0
^ z
) + (sum
^ (z
>> 5)) + k1
;
531 z
+= (y
<< 4) + (k2
^ y
) + (sum
^ (y
>> 5)) + k3
;
535 ibuf
[i
+ 1] = htonl(z
);
543 headersLog(int cs
, int pq
, const HttpRequestMethod
& method
, void *data
)
547 unsigned short magic
= 0;
548 unsigned char M
= (unsigned char) m
;
557 hmask
= rep
->header
.mask
;
559 if (rep
->cache_control
)
560 ccmask
= rep
->cache_control
->mask
;
566 hmask
= req
->header
.mask
;
568 if (req
->cache_control
)
569 ccmask
= req
->cache_control
->mask
;
580 magic
= htons(magic
);
581 ccmask
= htonl(ccmask
);
583 unsigned short S
= 0;
585 S
= static_cast<unsigned short>(rep
->sline
.status());
587 logfileWrite(headerslog
, &magic
, sizeof(magic
));
588 logfileWrite(headerslog
, &M
, sizeof(M
));
589 logfileWrite(headerslog
, &S
, sizeof(S
));
590 logfileWrite(headerslog
, hmask
, sizeof(HttpHeaderMask
));
591 logfileWrite(headerslog
, &ccmask
, sizeof(int));