2 * Copyright (C) 1996-2020 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 "error/Detail.h"
19 #include "errorpage.h"
20 #include "format/Token.h"
22 #include "hier_code.h"
23 #include "HttpReply.h"
24 #include "HttpRequest.h"
25 #include "log/access_log.h"
26 #include "log/Config.h"
27 #include "log/CustomLog.h"
29 #include "log/Formats.h"
31 #include "mgr/Registration.h"
33 #include "SquidConfig.h"
34 #include "SquidTime.h"
38 #include "eui/Eui48.h"
39 #include "eui/Eui64.h"
43 static Logfile
*headerslog
= NULL
;
46 #if MULTICAST_MISS_STREAM
47 static int mcast_miss_fd
= -1;
49 static struct sockaddr_in mcast_miss_to
;
50 static void mcast_encode(unsigned int *, size_t, const unsigned int *);
59 static hash_table
*via_table
= NULL
;
60 static hash_table
*forw_table
= NULL
;
61 static void fvdbInit();
62 static void fvdbDumpTable(StoreEntry
* e
, hash_table
* hash
);
63 static void fvdbCount(hash_table
* hash
, const char *key
);
64 static OBJH fvdbDumpVia
;
65 static OBJH fvdbDumpForw
;
66 static FREE fvdbFreeEntry
;
67 static void fvdbClear(void);
68 static void fvdbRegisterWithCacheManager();
71 int LogfileStatus
= LOG_DISABLE
;
74 accessLogLogTo(CustomLog
* log
, AccessLogEntry::Pointer
&al
, ACLChecklist
* checklist
)
77 if (al
->url
.isEmpty())
78 al
->url
= Format::Dash
;
80 if (!al
->http
.content_type
|| *al
->http
.content_type
== '\0')
81 al
->http
.content_type
= dash_str
;
83 if (al
->hier
.host
[0] == '\0')
84 xstrncpy(al
->hier
.host
, dash_str
, SQUIDHOSTNAMELEN
);
86 for (; log
; log
= log
->next
) {
87 if (log
->aclList
&& checklist
&& !checklist
->fastCheck(log
->aclList
).allowed())
90 // The special-case "none" type has no logfile object set
91 if (log
->type
== Log::Format::CLF_NONE
)
95 logfileLineStart(log
->logfile
);
99 case Log::Format::CLF_SQUID
:
100 Log::Format::SquidNative(al
, log
->logfile
);
103 case Log::Format::CLF_COMBINED
:
104 Log::Format::HttpdCombined(al
, log
->logfile
);
107 case Log::Format::CLF_COMMON
:
108 Log::Format::HttpdCommon(al
, log
->logfile
);
111 case Log::Format::CLF_REFERER
:
112 Log::Format::SquidReferer(al
, log
->logfile
);
115 case Log::Format::CLF_USERAGENT
:
116 Log::Format::SquidUserAgent(al
, log
->logfile
);
119 case Log::Format::CLF_CUSTOM
:
120 Log::Format::SquidCustom(al
, log
);
124 case Log::Format::CLF_ICAP_SQUID
:
125 Log::Format::SquidIcap(al
, log
->logfile
);
130 fatalf("Unknown log format %d\n", log
->type
);
134 logfileLineEnd(log
->logfile
);
137 // NP: WTF? if _any_ log line has no checklist ignore the following ones?
144 accessLogLog(AccessLogEntry::Pointer
&al
, ACLChecklist
* checklist
)
146 if (LogfileStatus
!= LOG_ENABLE
)
149 accessLogLogTo(Config
.Log
.accesslogs
, al
, checklist
);
150 #if MULTICAST_MISS_STREAM
152 if (al
->cache
.code
!= LOG_TCP_MISS
)
154 else if (al
->http
.method
!= METHOD_GET
)
156 else if (mcast_miss_fd
< 0)
159 unsigned int ibuf
[365];
161 xstrncpy((char *) ibuf
, al
->url
.c_str(), 364 * sizeof(int));
162 isize
= ((al
->url
.length() + 8) / 8) * 2;
167 mcast_encode((unsigned int *) ibuf
, isize
,
168 (const unsigned int *) Config
.mcast_miss
.encode_key
);
170 comm_udp_sendto(mcast_miss_fd
,
171 &mcast_miss_to
, sizeof(mcast_miss_to
),
172 ibuf
, isize
* sizeof(int));
179 accessLogRotate(void)
187 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
189 int16_t rc
= (log
->rotateCount
>= 0 ? log
->rotateCount
: Config
.Log
.rotateNumber
);
190 logfileRotate(log
->logfile
, rc
);
196 logfileRotate(headerslog
, Config
.Log
.rotateNumber
);
206 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
208 logfileClose(log
->logfile
);
215 logfileClose(headerslog
);
222 HierarchyLogEntry::HierarchyLogEntry() :
224 cd_lookup(LOOKUP_NONE
),
227 peer_reply_status(Http::scNone
),
231 memset(host
, '\0', SQUIDHOSTNAMELEN
);
232 memset(cd_host
, '\0', SQUIDHOSTNAMELEN
);
234 peer_select_start
.tv_sec
=0;
235 peer_select_start
.tv_usec
=0;
237 store_complete_stop
.tv_sec
=0;
238 store_complete_stop
.tv_usec
=0;
242 totalResponseTime_
.tv_sec
= -1;
243 totalResponseTime_
.tv_usec
= 0;
245 firstConnStart_
.tv_sec
= 0;
246 firstConnStart_
.tv_usec
= 0;
250 HierarchyLogEntry::resetPeerNotes(const Comm::ConnectionPointer
&server
, const char *requestedHost
)
255 if (tcpServer
== NULL
) {
257 xstrncpy(host
, requestedHost
, sizeof(host
));
259 code
= tcpServer
->peerType
;
261 if (tcpServer
->getPeer()) {
262 // went to peer, log peer host name
263 xstrncpy(host
, tcpServer
->getPeer()->name
, sizeof(host
));
265 xstrncpy(host
, requestedHost
, sizeof(host
));
270 /// forget previous notePeerRead() and notePeerWrite() calls (if any)
272 HierarchyLogEntry::clearPeerNotes()
274 peer_last_read_
.tv_sec
= 0;
275 peer_last_read_
.tv_usec
= 0;
277 peer_last_write_
.tv_sec
= 0;
278 peer_last_write_
.tv_usec
= 0;
284 HierarchyLogEntry::notePeerRead()
286 peer_last_read_
= current_time
;
290 HierarchyLogEntry::notePeerWrite()
292 peer_last_write_
= current_time
;
296 HierarchyLogEntry::startPeerClock()
298 if (!firstConnStart_
.tv_sec
)
299 firstConnStart_
= current_time
;
303 HierarchyLogEntry::stopPeerClock(const bool force
)
305 debugs(46, 5, "First connection started: " << firstConnStart_
<<
306 ", current total response time value: " << (totalResponseTime_
.tv_sec
* 1000 + totalResponseTime_
.tv_usec
/1000) <<
307 (force
? ", force fixing" : ""));
308 if (!force
&& totalResponseTime_
.tv_sec
!= -1)
311 if (firstConnStart_
.tv_sec
)
312 tvSub(totalResponseTime_
, firstConnStart_
, current_time
);
316 HierarchyLogEntry::peerResponseTime(struct timeval
&responseTime
)
319 if (peer_last_write_
.tv_sec
<= 0 && peer_last_read_
.tv_sec
<= 0)
322 // accommodate read without (completed) write
323 const auto last_write
= peer_last_write_
.tv_sec
> 0 ?
324 peer_last_write_
: peer_last_read_
;
326 // accommodate write without (completed) read
327 const auto last_read
= peer_last_read_
.tv_sec
> 0 ?
328 peer_last_read_
: peer_last_write_
;
330 tvSub(responseTime
, last_write
, last_read
);
331 // The peer response time (%<pt) stopwatch is currently defined to start
332 // when we wrote the entire request. Thus, if we wrote something after the
333 // last read, report zero peer response time.
334 if (responseTime
.tv_sec
< 0) {
335 responseTime
.tv_sec
= 0;
336 responseTime
.tv_usec
= 0;
343 HierarchyLogEntry::totalResponseTime(struct timeval
&responseTime
)
345 // This should not really happen, but there may be rare code
346 // paths that lead to FwdState discarded (or transaction logged)
347 // without (or before) a stopPeerClock() call.
348 if (firstConnStart_
.tv_sec
&& totalResponseTime_
.tv_sec
== -1)
349 stopPeerClock(false);
351 responseTime
= totalResponseTime_
;
352 return responseTime
.tv_sec
>= 0 && responseTime
.tv_usec
>= 0;
356 accessLogRegisterWithCacheManager(void)
359 fvdbRegisterWithCacheManager();
368 accessLogRegisterWithCacheManager();
371 Log::TheConfig
.hasAdaptToken
= false;
374 Log::TheConfig
.hasIcapToken
= false;
377 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
378 if (log
->type
== Log::Format::CLF_NONE
)
381 log
->logfile
= logfileOpen(log
->filename
, log
->bufferSize
, log
->fatal
);
383 LogfileStatus
= LOG_ENABLE
;
386 for (Format::Token
* curr_token
= (log
->logFormat
?log
->logFormat
->format
:NULL
); curr_token
; curr_token
= curr_token
->next
) {
387 if (curr_token
->type
== Format::LFT_ADAPTATION_SUM_XACT_TIMES
||
388 curr_token
->type
== Format::LFT_ADAPTATION_ALL_XACT_TIMES
||
389 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER
||
390 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER_ELEM
||
391 curr_token
->type
== Format::LFT_ADAPTATION_LAST_ALL_HEADERS
||
392 (curr_token
->type
== Format::LFT_NOTE
&& !Adaptation::Config::metaHeaders
.empty())) {
393 Log::TheConfig
.hasAdaptToken
= true;
396 if (curr_token
->type
== Format::LFT_ICAP_TOTAL_TIME
) {
397 Log::TheConfig
.hasIcapToken
= true;
406 headerslog
= logfileOpen("/usr/local/squid/logs/headers.log", 512);
408 assert(NULL
!= headerslog
);
411 #if MULTICAST_MISS_STREAM
413 if (Config
.mcast_miss
.addr
.s_addr
!= no_addr
.s_addr
) {
414 memset(&mcast_miss_to
, '\0', sizeof(mcast_miss_to
));
415 mcast_miss_to
.sin_family
= AF_INET
;
416 mcast_miss_to
.sin_port
= htons(Config
.mcast_miss
.port
);
417 mcast_miss_to
.sin_addr
.s_addr
= Config
.mcast_miss
.addr
.s_addr
;
418 mcast_miss_fd
= comm_open(SOCK_DGRAM
,
420 Config
.Addrs
.udp_incoming
,
421 Config
.mcast_miss
.port
,
423 "Multicast Miss Stream");
425 if (mcast_miss_fd
< 0)
426 fatal("Cannot open Multicast Miss Stream Socket");
428 debugs(46, DBG_IMPORTANT
, "Multicast Miss Stream Socket opened on FD " << mcast_miss_fd
);
430 mcastSetTtl(mcast_miss_fd
, Config
.mcast_miss
.ttl
);
432 if (strlen(Config
.mcast_miss
.encode_key
) < 16)
433 fatal("mcast_encode_key is too short, must be 16 characters");
449 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
450 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
454 fvdbRegisterWithCacheManager(void)
456 Mgr::RegisterAction("via_headers", "Via Request Headers", fvdbDumpVia
, 0, 1);
457 Mgr::RegisterAction("forw_headers", "X-Forwarded-For Request Headers",
462 fvdbCount(hash_table
* hash
, const char *key
)
469 fv
= (fvdb_entry
*)hash_lookup(hash
, key
);
472 fv
= static_cast <fvdb_entry
*>(xcalloc(1, sizeof(fvdb_entry
)));
473 fv
->hash
.key
= xstrdup(key
);
474 hash_join(hash
, &fv
->hash
);
481 fvdbCountVia(const char *key
)
483 fvdbCount(via_table
, key
);
487 fvdbCountForw(const char *key
)
489 fvdbCount(forw_table
, key
);
493 fvdbDumpTable(StoreEntry
* e
, hash_table
* hash
)
503 while ((h
= hash_next(hash
))) {
504 fv
= (fvdb_entry
*) h
;
505 storeAppendPrintf(e
, "%9d %s\n", fv
->n
, hashKeyStr(&fv
->hash
));
510 fvdbDumpVia(StoreEntry
* e
)
512 fvdbDumpTable(e
, via_table
);
516 fvdbDumpForw(StoreEntry
* e
)
518 fvdbDumpTable(e
, forw_table
);
523 fvdbFreeEntry(void *data
)
525 fvdb_entry
*fv
= static_cast <fvdb_entry
*>(data
);
533 hashFreeItems(via_table
, fvdbFreeEntry
);
534 hashFreeMemory(via_table
);
535 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
536 hashFreeItems(forw_table
, fvdbFreeEntry
);
537 hashFreeMemory(forw_table
);
538 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
543 #if MULTICAST_MISS_STREAM
545 * From http://www.io.com/~paulhart/game/algorithms/tea.html
547 * size of 'ibuf' must be a multiple of 2.
548 * size of 'key' must be 4.
549 * 'ibuf' is modified in place, encrypted data is written in
550 * network byte order.
553 mcast_encode(unsigned int *ibuf
, size_t isize
, const unsigned int *key
)
558 const unsigned int delta
= 0x9e3779b9;
560 const unsigned int k0
= htonl(key
[0]);
561 const unsigned int k1
= htonl(key
[1]);
562 const unsigned int k2
= htonl(key
[2]);
563 const unsigned int k3
= htonl(key
[3]);
566 for (i
= 0; i
< isize
; i
+= 2) {
568 z
= htonl(ibuf
[i
+ 1]);
571 for (n
= 32; n
; --n
) {
573 y
+= (z
<< 4) + (k0
^ z
) + (sum
^ (z
>> 5)) + k1
;
574 z
+= (y
<< 4) + (k2
^ y
) + (sum
^ (y
>> 5)) + k3
;
578 ibuf
[i
+ 1] = htonl(z
);
586 headersLog(int cs
, int pq
, const HttpRequestMethod
& method
, void *data
)
590 unsigned short magic
= 0;
591 unsigned char M
= (unsigned char) m
;
600 hmask
= rep
->header
.mask
;
602 if (rep
->cache_control
)
603 ccmask
= rep
->cache_control
->mask
;
609 hmask
= req
->header
.mask
;
611 if (req
->cache_control
)
612 ccmask
= req
->cache_control
->mask
;
623 magic
= htons(magic
);
624 ccmask
= htonl(ccmask
);
626 unsigned short S
= 0;
628 S
= static_cast<unsigned short>(rep
->sline
.status());
630 logfileWrite(headerslog
, &magic
, sizeof(magic
));
631 logfileWrite(headerslog
, &M
, sizeof(M
));
632 logfileWrite(headerslog
, &S
, sizeof(S
));
633 logfileWrite(headerslog
, hmask
, sizeof(HttpHeaderMask
));
634 logfileWrite(headerslog
, &ccmask
, sizeof(int));