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
)
99 // The special-case "none" type has no logfile object set
100 if (log
->type
== Log::Format::CLF_NONE
)
104 logfileLineStart(log
->logfile
);
108 case Log::Format::CLF_SQUID
:
109 Log::Format::SquidNative(al
, log
->logfile
);
112 case Log::Format::CLF_COMBINED
:
113 Log::Format::HttpdCombined(al
, log
->logfile
);
116 case Log::Format::CLF_COMMON
:
117 Log::Format::HttpdCommon(al
, log
->logfile
);
120 case Log::Format::CLF_REFERER
:
121 Log::Format::SquidReferer(al
, log
->logfile
);
124 case Log::Format::CLF_USERAGENT
:
125 Log::Format::SquidUserAgent(al
, log
->logfile
);
128 case Log::Format::CLF_CUSTOM
:
129 Log::Format::SquidCustom(al
, log
);
133 case Log::Format::CLF_ICAP_SQUID
:
134 Log::Format::SquidIcap(al
, log
->logfile
);
139 fatalf("Unknown log format %d\n", log
->type
);
143 logfileLineEnd(log
->logfile
);
146 // NP: WTF? if _any_ log line has no checklist ignore the following ones?
153 accessLogLog(AccessLogEntry::Pointer
&al
, ACLChecklist
* checklist
)
155 if (LogfileStatus
!= LOG_ENABLE
)
158 accessLogLogTo(Config
.Log
.accesslogs
, al
, checklist
);
159 #if MULTICAST_MISS_STREAM
161 if (al
->cache
.code
!= LOG_TCP_MISS
)
163 else if (al
->http
.method
!= METHOD_GET
)
165 else if (mcast_miss_fd
< 0)
168 unsigned int ibuf
[365];
170 xstrncpy((char *) ibuf
, al
->url
, 364 * sizeof(int));
171 isize
= ((strlen(al
->url
) + 8) / 8) * 2;
176 mcast_encode((unsigned int *) ibuf
, isize
,
177 (const unsigned int *) Config
.mcast_miss
.encode_key
);
179 comm_udp_sendto(mcast_miss_fd
,
180 &mcast_miss_to
, sizeof(mcast_miss_to
),
181 ibuf
, isize
* sizeof(int));
188 accessLogRotate(void)
196 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
198 int16_t rc
= (log
->rotateCount
>= 0 ? log
->rotateCount
: Config
.Log
.rotateNumber
);
199 logfileRotate(log
->logfile
, rc
);
205 logfileRotate(headerslog
, Config
.Log
.rotateNumber
);
215 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
217 logfileClose(log
->logfile
);
224 logfileClose(headerslog
);
231 HierarchyLogEntry::HierarchyLogEntry() :
233 cd_lookup(LOOKUP_NONE
),
236 peer_reply_status(Http::scNone
),
240 memset(host
, '\0', SQUIDHOSTNAMELEN
);
241 memset(cd_host
, '\0', SQUIDHOSTNAMELEN
);
243 peer_select_start
.tv_sec
=0;
244 peer_select_start
.tv_usec
=0;
246 store_complete_stop
.tv_sec
=0;
247 store_complete_stop
.tv_usec
=0;
249 peer_http_request_sent
.tv_sec
= 0;
250 peer_http_request_sent
.tv_usec
= 0;
252 peer_response_time
.tv_sec
= -1;
253 peer_response_time
.tv_usec
= 0;
255 totalResponseTime_
.tv_sec
= -1;
256 totalResponseTime_
.tv_usec
= 0;
258 firstConnStart_
.tv_sec
= 0;
259 firstConnStart_
.tv_usec
= 0;
263 HierarchyLogEntry::note(const Comm::ConnectionPointer
&server
, const char *requestedHost
)
266 if (tcpServer
== NULL
) {
268 xstrncpy(host
, requestedHost
, sizeof(host
));
270 code
= tcpServer
->peerType
;
272 if (tcpServer
->getPeer()) {
273 // went to peer, log peer host name
274 xstrncpy(host
, tcpServer
->getPeer()->name
, sizeof(host
));
276 xstrncpy(host
, requestedHost
, sizeof(host
));
282 HierarchyLogEntry::startPeerClock()
284 if (!firstConnStart_
.tv_sec
)
285 firstConnStart_
= current_time
;
289 HierarchyLogEntry::stopPeerClock(const bool force
)
291 debugs(46, 5, "First connection started: " << firstConnStart_
.tv_sec
<< "." <<
292 std::setfill('0') << std::setw(6) << firstConnStart_
.tv_usec
<<
293 ", current total response time value: " << (totalResponseTime_
.tv_sec
* 1000 + totalResponseTime_
.tv_usec
/1000) <<
294 (force
? ", force fixing" : ""));
295 if (!force
&& totalResponseTime_
.tv_sec
!= -1)
298 if (firstConnStart_
.tv_sec
)
299 tvSub(totalResponseTime_
, firstConnStart_
, current_time
);
303 HierarchyLogEntry::totalResponseTime(struct timeval
&responseTime
)
305 // This should not really happen, but there may be rare code
306 // paths that lead to FwdState discarded (or transaction logged)
307 // without (or before) a stopPeerClock() call.
308 if (firstConnStart_
.tv_sec
&& totalResponseTime_
.tv_sec
== -1)
309 stopPeerClock(false);
311 responseTime
= totalResponseTime_
;
315 accessLogRegisterWithCacheManager(void)
318 fvdbRegisterWithCacheManager();
327 accessLogRegisterWithCacheManager();
330 Log::TheConfig
.hasAdaptToken
= false;
333 Log::TheConfig
.hasIcapToken
= false;
336 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
337 if (log
->type
== Log::Format::CLF_NONE
)
340 log
->logfile
= logfileOpen(log
->filename
, log
->bufferSize
, log
->fatal
);
342 LogfileStatus
= LOG_ENABLE
;
345 for (Format::Token
* curr_token
= (log
->logFormat
?log
->logFormat
->format
:NULL
); curr_token
; curr_token
= curr_token
->next
) {
346 if (curr_token
->type
== Format::LFT_ADAPTATION_SUM_XACT_TIMES
||
347 curr_token
->type
== Format::LFT_ADAPTATION_ALL_XACT_TIMES
||
348 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER
||
349 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER_ELEM
||
350 curr_token
->type
== Format::LFT_ADAPTATION_LAST_ALL_HEADERS
||
351 (curr_token
->type
== Format::LFT_NOTE
&& !Adaptation::Config::metaHeaders
.empty())) {
352 Log::TheConfig
.hasAdaptToken
= true;
355 if (curr_token
->type
== Format::LFT_ICAP_TOTAL_TIME
) {
356 Log::TheConfig
.hasIcapToken
= true;
365 headerslog
= logfileOpen("/usr/local/squid/logs/headers.log", 512);
367 assert(NULL
!= headerslog
);
370 #if MULTICAST_MISS_STREAM
372 if (Config
.mcast_miss
.addr
.s_addr
!= no_addr
.s_addr
) {
373 memset(&mcast_miss_to
, '\0', sizeof(mcast_miss_to
));
374 mcast_miss_to
.sin_family
= AF_INET
;
375 mcast_miss_to
.sin_port
= htons(Config
.mcast_miss
.port
);
376 mcast_miss_to
.sin_addr
.s_addr
= Config
.mcast_miss
.addr
.s_addr
;
377 mcast_miss_fd
= comm_open(SOCK_DGRAM
,
379 Config
.Addrs
.udp_incoming
,
380 Config
.mcast_miss
.port
,
382 "Multicast Miss Stream");
384 if (mcast_miss_fd
< 0)
385 fatal("Cannot open Multicast Miss Stream Socket");
387 debugs(46, DBG_IMPORTANT
, "Multicast Miss Stream Socket opened on FD " << mcast_miss_fd
);
389 mcastSetTtl(mcast_miss_fd
, Config
.mcast_miss
.ttl
);
391 if (strlen(Config
.mcast_miss
.encode_key
) < 16)
392 fatal("mcast_encode_key is too short, must be 16 characters");
408 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
409 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
413 fvdbRegisterWithCacheManager(void)
415 Mgr::RegisterAction("via_headers", "Via Request Headers", fvdbDumpVia
, 0, 1);
416 Mgr::RegisterAction("forw_headers", "X-Forwarded-For Request Headers",
421 fvdbCount(hash_table
* hash
, const char *key
)
428 fv
= (fvdb_entry
*)hash_lookup(hash
, key
);
431 fv
= static_cast <fvdb_entry
*>(xcalloc(1, sizeof(fvdb_entry
)));
432 fv
->hash
.key
= xstrdup(key
);
433 hash_join(hash
, &fv
->hash
);
440 fvdbCountVia(const char *key
)
442 fvdbCount(via_table
, key
);
446 fvdbCountForw(const char *key
)
448 fvdbCount(forw_table
, key
);
452 fvdbDumpTable(StoreEntry
* e
, hash_table
* hash
)
462 while ((h
= hash_next(hash
))) {
463 fv
= (fvdb_entry
*) h
;
464 storeAppendPrintf(e
, "%9d %s\n", fv
->n
, hashKeyStr(&fv
->hash
));
469 fvdbDumpVia(StoreEntry
* e
)
471 fvdbDumpTable(e
, via_table
);
475 fvdbDumpForw(StoreEntry
* e
)
477 fvdbDumpTable(e
, forw_table
);
482 fvdbFreeEntry(void *data
)
484 fvdb_entry
*fv
= static_cast <fvdb_entry
*>(data
);
492 hashFreeItems(via_table
, fvdbFreeEntry
);
493 hashFreeMemory(via_table
);
494 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
495 hashFreeItems(forw_table
, fvdbFreeEntry
);
496 hashFreeMemory(forw_table
);
497 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
502 #if MULTICAST_MISS_STREAM
504 * From http://www.io.com/~paulhart/game/algorithms/tea.html
506 * size of 'ibuf' must be a multiple of 2.
507 * size of 'key' must be 4.
508 * 'ibuf' is modified in place, encrypted data is written in
509 * network byte order.
512 mcast_encode(unsigned int *ibuf
, size_t isize
, const unsigned int *key
)
517 const unsigned int delta
= 0x9e3779b9;
519 const unsigned int k0
= htonl(key
[0]);
520 const unsigned int k1
= htonl(key
[1]);
521 const unsigned int k2
= htonl(key
[2]);
522 const unsigned int k3
= htonl(key
[3]);
525 for (i
= 0; i
< isize
; i
+= 2) {
527 z
= htonl(ibuf
[i
+ 1]);
530 for (n
= 32; n
; --n
) {
532 y
+= (z
<< 4) + (k0
^ z
) + (sum
^ (z
>> 5)) + k1
;
533 z
+= (y
<< 4) + (k2
^ y
) + (sum
^ (y
>> 5)) + k3
;
537 ibuf
[i
+ 1] = htonl(z
);
545 headersLog(int cs
, int pq
, const HttpRequestMethod
& method
, void *data
)
549 unsigned short magic
= 0;
550 unsigned char M
= (unsigned char) m
;
559 hmask
= rep
->header
.mask
;
561 if (rep
->cache_control
)
562 ccmask
= rep
->cache_control
->mask
;
568 hmask
= req
->header
.mask
;
570 if (req
->cache_control
)
571 ccmask
= req
->cache_control
->mask
;
582 magic
= htons(magic
);
583 ccmask
= htonl(ccmask
);
585 unsigned short S
= 0;
587 S
= static_cast<unsigned short>(rep
->sline
.status());
589 logfileWrite(headerslog
, &magic
, sizeof(magic
));
590 logfileWrite(headerslog
, &M
, sizeof(M
));
591 logfileWrite(headerslog
, &S
, sizeof(S
));
592 logfileWrite(headerslog
, hmask
, sizeof(HttpHeaderMask
));
593 logfileWrite(headerslog
, &ccmask
, sizeof(int));