2 * Copyright (C) 1996-2017 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 "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_
.tv_sec
<< "." <<
306 std::setfill('0') << std::setw(6) << firstConnStart_
.tv_usec
<<
307 ", current total response time value: " << (totalResponseTime_
.tv_sec
* 1000 + totalResponseTime_
.tv_usec
/1000) <<
308 (force
? ", force fixing" : ""));
309 if (!force
&& totalResponseTime_
.tv_sec
!= -1)
312 if (firstConnStart_
.tv_sec
)
313 tvSub(totalResponseTime_
, firstConnStart_
, current_time
);
317 HierarchyLogEntry::peerResponseTime(struct timeval
&responseTime
)
320 if (peer_last_write_
.tv_sec
<= 0 && peer_last_read_
.tv_sec
<= 0)
323 // accommodate read without (completed) write
324 const auto last_write
= peer_last_write_
.tv_sec
> 0 ?
325 peer_last_write_
: peer_last_read_
;
327 // accommodate write without (completed) read
328 const auto last_read
= peer_last_read_
.tv_sec
> 0 ?
329 peer_last_read_
: peer_last_write_
;
331 tvSub(responseTime
, last_write
, last_read
);
332 // The peer response time (%<pt) stopwatch is currently defined to start
333 // when we wrote the entire request. Thus, if we wrote something after the
334 // last read, report zero peer response time.
335 if (responseTime
.tv_sec
< 0) {
336 responseTime
.tv_sec
= 0;
337 responseTime
.tv_usec
= 0;
344 HierarchyLogEntry::totalResponseTime(struct timeval
&responseTime
)
346 // This should not really happen, but there may be rare code
347 // paths that lead to FwdState discarded (or transaction logged)
348 // without (or before) a stopPeerClock() call.
349 if (firstConnStart_
.tv_sec
&& totalResponseTime_
.tv_sec
== -1)
350 stopPeerClock(false);
352 responseTime
= totalResponseTime_
;
353 return responseTime
.tv_sec
>= 0 && responseTime
.tv_usec
>= 0;
357 accessLogRegisterWithCacheManager(void)
360 fvdbRegisterWithCacheManager();
369 accessLogRegisterWithCacheManager();
372 Log::TheConfig
.hasAdaptToken
= false;
375 Log::TheConfig
.hasIcapToken
= false;
378 for (log
= Config
.Log
.accesslogs
; log
; log
= log
->next
) {
379 if (log
->type
== Log::Format::CLF_NONE
)
382 log
->logfile
= logfileOpen(log
->filename
, log
->bufferSize
, log
->fatal
);
384 LogfileStatus
= LOG_ENABLE
;
387 for (Format::Token
* curr_token
= (log
->logFormat
?log
->logFormat
->format
:NULL
); curr_token
; curr_token
= curr_token
->next
) {
388 if (curr_token
->type
== Format::LFT_ADAPTATION_SUM_XACT_TIMES
||
389 curr_token
->type
== Format::LFT_ADAPTATION_ALL_XACT_TIMES
||
390 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER
||
391 curr_token
->type
== Format::LFT_ADAPTATION_LAST_HEADER_ELEM
||
392 curr_token
->type
== Format::LFT_ADAPTATION_LAST_ALL_HEADERS
||
393 (curr_token
->type
== Format::LFT_NOTE
&& !Adaptation::Config::metaHeaders
.empty())) {
394 Log::TheConfig
.hasAdaptToken
= true;
397 if (curr_token
->type
== Format::LFT_ICAP_TOTAL_TIME
) {
398 Log::TheConfig
.hasIcapToken
= true;
407 headerslog
= logfileOpen("/usr/local/squid/logs/headers.log", 512);
409 assert(NULL
!= headerslog
);
412 #if MULTICAST_MISS_STREAM
414 if (Config
.mcast_miss
.addr
.s_addr
!= no_addr
.s_addr
) {
415 memset(&mcast_miss_to
, '\0', sizeof(mcast_miss_to
));
416 mcast_miss_to
.sin_family
= AF_INET
;
417 mcast_miss_to
.sin_port
= htons(Config
.mcast_miss
.port
);
418 mcast_miss_to
.sin_addr
.s_addr
= Config
.mcast_miss
.addr
.s_addr
;
419 mcast_miss_fd
= comm_open(SOCK_DGRAM
,
421 Config
.Addrs
.udp_incoming
,
422 Config
.mcast_miss
.port
,
424 "Multicast Miss Stream");
426 if (mcast_miss_fd
< 0)
427 fatal("Cannot open Multicast Miss Stream Socket");
429 debugs(46, DBG_IMPORTANT
, "Multicast Miss Stream Socket opened on FD " << mcast_miss_fd
);
431 mcastSetTtl(mcast_miss_fd
, Config
.mcast_miss
.ttl
);
433 if (strlen(Config
.mcast_miss
.encode_key
) < 16)
434 fatal("mcast_encode_key is too short, must be 16 characters");
450 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
451 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
455 fvdbRegisterWithCacheManager(void)
457 Mgr::RegisterAction("via_headers", "Via Request Headers", fvdbDumpVia
, 0, 1);
458 Mgr::RegisterAction("forw_headers", "X-Forwarded-For Request Headers",
463 fvdbCount(hash_table
* hash
, const char *key
)
470 fv
= (fvdb_entry
*)hash_lookup(hash
, key
);
473 fv
= static_cast <fvdb_entry
*>(xcalloc(1, sizeof(fvdb_entry
)));
474 fv
->hash
.key
= xstrdup(key
);
475 hash_join(hash
, &fv
->hash
);
482 fvdbCountVia(const char *key
)
484 fvdbCount(via_table
, key
);
488 fvdbCountForw(const char *key
)
490 fvdbCount(forw_table
, key
);
494 fvdbDumpTable(StoreEntry
* e
, hash_table
* hash
)
504 while ((h
= hash_next(hash
))) {
505 fv
= (fvdb_entry
*) h
;
506 storeAppendPrintf(e
, "%9d %s\n", fv
->n
, hashKeyStr(&fv
->hash
));
511 fvdbDumpVia(StoreEntry
* e
)
513 fvdbDumpTable(e
, via_table
);
517 fvdbDumpForw(StoreEntry
* e
)
519 fvdbDumpTable(e
, forw_table
);
524 fvdbFreeEntry(void *data
)
526 fvdb_entry
*fv
= static_cast <fvdb_entry
*>(data
);
534 hashFreeItems(via_table
, fvdbFreeEntry
);
535 hashFreeMemory(via_table
);
536 via_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
537 hashFreeItems(forw_table
, fvdbFreeEntry
);
538 hashFreeMemory(forw_table
);
539 forw_table
= hash_create((HASHCMP
*) strcmp
, 977, hash4
);
544 #if MULTICAST_MISS_STREAM
546 * From http://www.io.com/~paulhart/game/algorithms/tea.html
548 * size of 'ibuf' must be a multiple of 2.
549 * size of 'key' must be 4.
550 * 'ibuf' is modified in place, encrypted data is written in
551 * network byte order.
554 mcast_encode(unsigned int *ibuf
, size_t isize
, const unsigned int *key
)
559 const unsigned int delta
= 0x9e3779b9;
561 const unsigned int k0
= htonl(key
[0]);
562 const unsigned int k1
= htonl(key
[1]);
563 const unsigned int k2
= htonl(key
[2]);
564 const unsigned int k3
= htonl(key
[3]);
567 for (i
= 0; i
< isize
; i
+= 2) {
569 z
= htonl(ibuf
[i
+ 1]);
572 for (n
= 32; n
; --n
) {
574 y
+= (z
<< 4) + (k0
^ z
) + (sum
^ (z
>> 5)) + k1
;
575 z
+= (y
<< 4) + (k2
^ y
) + (sum
^ (y
>> 5)) + k3
;
579 ibuf
[i
+ 1] = htonl(z
);
587 headersLog(int cs
, int pq
, const HttpRequestMethod
& method
, void *data
)
591 unsigned short magic
= 0;
592 unsigned char M
= (unsigned char) m
;
601 hmask
= rep
->header
.mask
;
603 if (rep
->cache_control
)
604 ccmask
= rep
->cache_control
->mask
;
610 hmask
= req
->header
.mask
;
612 if (req
->cache_control
)
613 ccmask
= req
->cache_control
->mask
;
624 magic
= htons(magic
);
625 ccmask
= htonl(ccmask
);
627 unsigned short S
= 0;
629 S
= static_cast<unsigned short>(rep
->sline
.status());
631 logfileWrite(headerslog
, &magic
, sizeof(magic
));
632 logfileWrite(headerslog
, &M
, sizeof(M
));
633 logfileWrite(headerslog
, &S
, sizeof(S
));
634 logfileWrite(headerslog
, hmask
, sizeof(HttpHeaderMask
));
635 logfileWrite(headerslog
, &ccmask
, sizeof(int));