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 38 Network Measurement Database */
14 * This code may be slightly broken now. If you're getting consistent
15 * (sometimes working) corrupt data exchanges, please contact adrian
16 * (adrian@squid-cache.org) to sort them out.
20 #include "CachePeer.h"
26 #include "HttpReply.h"
27 #include "HttpRequest.h"
28 #include "icmp/net_db.h"
30 #include "ip/Address.h"
32 #include "MemObject.h"
33 #include "mgr/Registration.h"
34 #include "mime_header.h"
35 #include "neighbors.h"
36 #include "SquidTime.h"
38 #include "StoreClient.h"
49 #include "icmp/IcmpSquid.h"
51 #include "StoreClient.h"
53 #define NETDB_REQBUF_SZ 4096
61 class netdbExchangeState
63 CBDATA_CLASS(netdbExchangeState
);
66 netdbExchangeState(CachePeer
*aPeer
, HttpRequest
*theReq
) :
67 p(cbdataReference(aPeer
)),
72 buf_sz(NETDB_REQBUF_SZ
),
74 connstate(STATE_HEADER
)
80 // TODO: check if we actually need to do this. should be implicit
81 r
->http_ver
= Http::ProtocolVersion();
84 ~netdbExchangeState() {
85 debugs(38, 3, e
->url());
86 storeUnregister(sc
, e
, this);
87 e
->unlock("netdbExchangeDone");
89 cbdataReferenceDone(p
);
98 char buf
[NETDB_REQBUF_SZ
];
100 netdb_conn_state_t connstate
;
103 CBDATA_CLASS_INIT(netdbExchangeState
);
105 static hash_table
*addr_table
= NULL
;
106 static hash_table
*host_table
= NULL
;
108 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
109 static void netdbRelease(netdbEntry
* n
);
111 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
112 static void netdbHashDelete(const char *key
);
113 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
114 static void netdbHostDelete(const net_db_name
* x
);
115 static void netdbPurgeLRU(void);
116 static netdbEntry
*netdbLookupHost(const char *key
);
117 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
118 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
);
119 static const char *netdbPeerName(const char *name
);
120 static IPH netdbSendPing
;
121 static QS sortPeerByRtt
;
124 static FREE netdbFreeNameEntry
;
125 static FREE netdbFreeNetdbEntry
;
126 static STCB netdbExchangeHandleReply
;
128 /* We have to keep a local list of CachePeer names. The Peers structure
129 * gets freed during a reconfigure. We want this database to
130 * remain persisitent, so _net_db_peer->peername points into this
132 static wordlist
*peer_names
= NULL
;
135 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
137 networkFromInaddr(addr
).toStr(n
->network
, MAX_IPSTRLEN
);
138 n
->hash
.key
= n
->network
;
139 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
140 hash_join(addr_table
, &n
->hash
);
144 netdbHashDelete(const char *key
)
146 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
149 debug_trap("netdbHashDelete: key not found");
153 hash_remove_link(addr_table
, hptr
);
157 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
159 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
160 x
->hash
.key
= xstrdup(hostname
);
164 assert(hash_lookup(host_table
, hostname
) == NULL
);
165 hash_join(host_table
, &x
->hash
);
170 netdbHostDelete(const net_db_name
* x
)
175 assert(x
->net_db_entry
!= NULL
);
179 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
186 hash_remove_link(host_table
, (hash_link
*) x
);
188 memFree((void *) x
, MEM_NET_DB_NAME
);
192 netdbLookupHost(const char *key
)
194 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
195 return x
? x
->net_db_entry
: NULL
;
199 netdbRelease(netdbEntry
* n
)
204 for (x
= n
->hosts
; x
; x
= next
) {
213 n
->n_peers_alloc
= 0;
215 if (n
->link_count
== 0) {
216 netdbHashDelete(n
->network
);
217 memFree(n
, MEM_NETDBENTRY
);
222 netdbLRU(const void *A
, const void *B
)
224 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
225 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
227 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
230 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
244 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
245 hash_first(addr_table
);
247 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
248 assert(list_count
< memInUse(MEM_NETDBENTRY
));
249 *(list
+ list_count
) = n
;
255 sizeof(netdbEntry
*),
258 for (k
= 0; k
< list_count
; ++k
) {
259 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
262 netdbRelease(*(list
+ k
));
271 netdbLookupAddr(const Ip::Address
&addr
)
274 char *key
= new char[MAX_IPSTRLEN
];
275 networkFromInaddr(addr
).toStr(key
,MAX_IPSTRLEN
);
276 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
282 netdbAdd(Ip::Address
&addr
)
286 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
289 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
290 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
291 netdbHashInsert(n
, addr
);
298 netdbSendPing(const ipcache_addrs
*ia
, const Dns::LookupDetails
&, void *data
)
301 char *hostname
= NULL
;
302 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
313 addr
= ia
->in_addrs
[ia
->cur
];
315 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
317 netdbHostInsert(n
, hostname
);
318 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
320 *hostname moved from 'network n' to 'network na'!
326 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
328 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
331 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
336 /* remove net_db_name from 'network n' linked list */
337 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
345 /* point to 'network na' from host entry */
346 x
->net_db_entry
= na
;
347 /* link net_db_name to 'network na' */
354 if (n
->next_ping_time
<= squid_curtime
) {
355 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
356 icmpEngine
.DomainPing(addr
, hostname
);
358 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
359 n
->last_use_time
= squid_curtime
;
366 networkFromInaddr(const Ip::Address
&in
)
372 /* in IPv6 the 'network' should be the routing section. */
374 out
.applyMask(64, AF_INET6
);
375 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
384 if (IN_CLASSC(b
.s_addr
))
385 b
.s_addr
&= IN_CLASSC_NET
;
386 else if (IN_CLASSB(b
.s_addr
))
387 b
.s_addr
&= IN_CLASSB_NET
;
388 else if (IN_CLASSA(b
.s_addr
))
389 b
.s_addr
&= IN_CLASSA_NET
;
395 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
397 /* use /24 for everything under IPv4 */
398 out
.applyMask(24, AF_INET
);
399 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
405 sortByRtt(const void *A
, const void *B
)
407 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
408 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
410 if ((*n1
)->rtt
> (*n2
)->rtt
)
412 else if ((*n1
)->rtt
< (*n2
)->rtt
)
419 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
422 net_db_peer
*p
= n
->peers
;
424 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
425 if (!strcmp(p
->peername
, peername
))
433 netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
)
440 if (n
->n_peers
== n
->n_peers_alloc
) {
442 osize
= n
->n_peers_alloc
;
444 if (n
->n_peers_alloc
== 0)
445 n
->n_peers_alloc
= 2;
447 n
->n_peers_alloc
<<= 1;
449 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
451 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
453 for (i
= 0; i
< osize
; ++i
)
454 *(n
->peers
+ i
) = *(o
+ i
);
461 p
= n
->peers
+ n
->n_peers
;
462 p
->peername
= netdbPeerName(e
->host
);
468 sortPeerByRtt(const void *A
, const void *B
)
470 const net_db_peer
*p1
= (net_db_peer
*)A
;
471 const net_db_peer
*p2
= (net_db_peer
*)B
;
473 if (p1
->rtt
> p2
->rtt
)
475 else if (p1
->rtt
< p2
->rtt
)
482 netdbSaveState(void *foo
)
484 if (strcmp(Config
.netdbFilename
, "none") == 0)
491 struct timeval start
= current_time
;
494 * This was nicer when we were using stdio, but thanks to
495 * Solaris bugs, its a bad idea. fopen can fail if more than
499 * unlink() is here because there is currently no way to make
500 * logfileOpen() use O_TRUNC.
502 unlink(Config
.netdbFilename
);
503 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
506 debugs(50, DBG_IMPORTANT
, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
510 hash_first(addr_table
);
512 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
513 if (n
->pings_recv
== 0)
516 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
522 (int) n
->next_ping_time
,
523 (int) n
->last_use_time
);
525 for (x
= n
->hosts
; x
; x
= x
->next
)
526 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
528 logfilePrintf(lf
, "\n");
538 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
539 count
<< " entries, " <<
540 tvSubMsec(start
, current_time
) << " msec" );
541 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
545 netdbReloadState(void)
547 if (strcmp(Config
.netdbFilename
, "none") == 0)
561 struct timeval start
= current_time
;
563 * This was nicer when we were using stdio, but thanks to
564 * Solaris bugs, its a bad idea. fopen can fail if more than
567 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
572 if (fstat(fd
, &sb
) < 0) {
578 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
580 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
588 while ((s
= strchr(t
, '\n'))) {
592 memset(&N
, '\0', sizeof(netdbEntry
));
593 q
= strtok(t
, w_space
);
602 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
605 if ((q
= strtok(NULL
, w_space
)) == NULL
)
608 N
.pings_sent
= atoi(q
);
610 if ((q
= strtok(NULL
, w_space
)) == NULL
)
613 N
.pings_recv
= atoi(q
);
615 if (N
.pings_recv
== 0)
618 /* give this measurement low weight */
623 if ((q
= strtok(NULL
, w_space
)) == NULL
)
628 if ((q
= strtok(NULL
, w_space
)) == NULL
)
633 if ((q
= strtok(NULL
, w_space
)) == NULL
)
636 N
.next_ping_time
= (time_t) atoi(q
);
638 if ((q
= strtok(NULL
, w_space
)) == NULL
)
641 N
.last_use_time
= (time_t) atoi(q
);
643 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
645 memcpy(n
, &N
, sizeof(netdbEntry
));
647 netdbHashInsert(n
, addr
);
649 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
650 if (netdbLookupHost(q
) != NULL
) /* no dups! */
653 netdbHostInsert(n
, q
);
661 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
662 count
<< " entries, " <<
663 tvSubMsec(start
, current_time
) << " msec" );
667 netdbPeerName(const char *name
)
671 for (w
= peer_names
; w
; w
= w
->next
) {
672 if (!strcmp(w
->key
, name
))
676 return wordlistAdd(&peer_names
, name
);
680 netdbFreeNetdbEntry(void *data
)
682 netdbEntry
*n
= (netdbEntry
*)data
;
684 memFree(n
, MEM_NETDBENTRY
);
688 netdbFreeNameEntry(void *data
)
690 net_db_name
*x
= (net_db_name
*)data
;
692 memFree(x
, MEM_NET_DB_NAME
);
696 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
700 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
704 struct in_addr line_addr
;
709 HttpReply
const *rep
;
713 int oldbufofs
= ex
->buf_ofs
;
716 rec_sz
+= 1 + sizeof(struct in_addr
);
717 rec_sz
+= 1 + sizeof(int);
718 rec_sz
+= 1 + sizeof(int);
719 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
721 if (!cbdataReferenceValid(ex
->p
)) {
722 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
727 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
729 if (receivedData
.length
== 0 && !receivedData
.flags
.error
) {
730 debugs(38, 3, "netdbExchangeHandleReply: Done");
737 /* Get the size of the buffer now */
738 size
= ex
->buf_ofs
+ receivedData
.length
;
739 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
741 /* Check if we're still doing headers */
743 if (ex
->connstate
== STATE_HEADER
) {
745 ex
->buf_ofs
+= receivedData
.length
;
747 /* skip reply headers */
749 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
750 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
751 rep
= ex
->e
->getReply();
752 assert(rep
->sline
.status() != Http::scNone
);
753 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status());
755 if (rep
->sline
.status() != Http::scOkay
) {
760 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
763 * Now, point p to the part of the buffer where the data
764 * starts, and update the size accordingly
766 assert(ex
->used
== 0);
768 size
= ex
->buf_ofs
- hdr_sz
;
771 /* Finally, set the conn state mode to STATE_BODY */
772 ex
->connstate
= STATE_BODY
;
774 StoreIOBuffer tempBuffer
;
775 tempBuffer
.offset
= ex
->buf_ofs
;
776 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
777 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
778 /* Have more headers .. */
779 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
780 netdbExchangeHandleReply
, ex
);
785 assert(ex
->connstate
== STATE_BODY
);
787 /* If we get here, we have some body to parse .. */
788 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
790 while (size
>= rec_sz
) {
791 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
795 for (o
= 0; o
< rec_sz
;) {
796 switch ((int) *(p
+ o
)) {
798 case NETDB_EX_NETWORK
:
800 /* FIXME INET6 : NetDB can still ony send IPv4 */
801 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
803 o
+= sizeof(struct in_addr
);
808 memcpy(&j
, p
+ o
, sizeof(int));
810 rtt
= (double) ntohl(j
) / 1000.0;
815 memcpy(&j
, p
+ o
, sizeof(int));
817 hops
= (double) ntohl(j
) / 1000.0;
821 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
827 if (!addr
.isAnyAddr() && rtt
> 0)
828 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
842 * Copy anything that is left over to the beginning of the buffer,
843 * and adjust buf_ofs accordingly
847 * Evilly, size refers to the buf size left now,
848 * ex->buf_ofs is the original buffer size, so just copy that
851 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
856 * And don't re-copy the remaining data ..
861 * Now the tricky bit - size _included_ the leftover bit from the _last_
862 * storeClientCopy. We don't want to include that, or our offset will be wrong.
863 * So, don't count the size of the leftover buffer we began with.
864 * This can _disappear_ when we're not tracking offsets ..
866 ex
->used
-= oldbufofs
;
868 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
870 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
871 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
874 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
876 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
877 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
879 } else if (ex
->e
->store_status
== STORE_PENDING
) {
880 StoreIOBuffer tempBuffer
;
881 tempBuffer
.offset
= ex
->used
;
882 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
883 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
884 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
885 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
886 netdbExchangeHandleReply
, ex
);
891 netdbRegisterWithCacheManager(void)
893 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
896 #endif /* USE_ICMP */
898 /* PUBLIC FUNCTIONS */
906 netdbRegisterWithCacheManager();
911 n
= hashPrime(Config
.Netdb
.high
/ 4);
913 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
915 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
917 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
919 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
927 netdbPingSite(const char *hostname
)
932 if ((n
= netdbLookupHost(hostname
)) != NULL
)
933 if (n
->next_ping_time
> squid_curtime
)
936 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
937 new generic_cbdata(xstrdup(hostname
)));
943 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
948 debugs(38, 3, "netdbHandlePingReply: from " << from
);
950 if ((n
= netdbLookupAddr(from
)) == NULL
)
961 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
963 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
965 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
966 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
967 std::setw(4) << n
->hops
);
973 netdbFreeMemory(void)
976 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
977 hashFreeMemory(addr_table
);
979 hashFreeItems(host_table
, netdbFreeNameEntry
);
980 hashFreeMemory(host_table
);
982 wordlistDestroy(&peer_names
);
988 netdbDump(StoreEntry
* sentry
)
998 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
999 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1005 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1007 hash_first(addr_table
);
1009 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1014 if (i
!= memInUse(MEM_NETDBENTRY
))
1015 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
1016 ", expected " << memInUse(MEM_NETDBENTRY
));
1018 qsort((char *) list
,
1020 sizeof(netdbEntry
*),
1023 for (k
= 0; k
< i
; ++k
) {
1025 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1032 for (x
= n
->hosts
; x
; x
= x
->next
)
1033 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1035 storeAppendPrintf(sentry
, "\n");
1039 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1040 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1050 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1055 netdbHostHops(const char *host
)
1058 netdbEntry
*n
= netdbLookupHost(host
);
1061 n
->last_use_time
= squid_curtime
;
1062 return (int) (n
->hops
+ 0.5);
1070 netdbHostRtt(const char *host
)
1073 netdbEntry
*n
= netdbLookupHost(host
);
1076 n
->last_use_time
= squid_curtime
;
1077 return (int) (n
->rtt
+ 0.5);
1085 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1088 netdbEntry
*n
= netdbLookupHost(host
);
1093 *samp
= n
->pings_recv
;
1095 *rtt
= (int) (n
->rtt
+ 0.5);
1097 *hops
= (int) (n
->hops
+ 0.5);
1099 n
->last_use_time
= squid_curtime
;
1105 netdbUpdatePeer(HttpRequest
* r
, CachePeer
* e
, int irtt
, int ihops
)
1109 double rtt
= (double) irtt
;
1110 double hops
= (double) ihops
;
1112 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1113 n
= netdbLookupHost(r
->GetHost());
1116 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1120 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1121 p
= netdbPeerAdd(n
, e
);
1127 p
->expires
= squid_curtime
+ 3600;
1132 qsort((char *) n
->peers
,
1134 sizeof(net_db_peer
),
1141 netdbExchangeUpdatePeer(Ip::Address
&addr
, CachePeer
* e
, double rtt
, double hops
)
1146 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1147 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1150 if ( !addr
.isIPv4() ) {
1151 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1155 n
= netdbLookupAddr(addr
);
1162 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1163 p
= netdbPeerAdd(n
, e
);
1169 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1174 qsort((char *) n
->peers
,
1176 sizeof(net_db_peer
),
1183 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1186 netdbEntry
*n
= netdbLookupAddr(addr
);
1191 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1198 netdbBinaryExchange(StoreEntry
* s
)
1200 HttpReply
*reply
= new HttpReply
;
1211 struct in_addr line_addr
;
1213 reply
->setHeaders(Http::scOkay
, "OK", NULL
, -1, squid_curtime
, -2);
1214 s
->replaceHttpReply(reply
);
1216 rec_sz
+= 1 + sizeof(struct in_addr
);
1217 rec_sz
+= 1 + sizeof(int);
1218 rec_sz
+= 1 + sizeof(int);
1219 buf
= (char *)memAllocate(MEM_4K_BUF
);
1221 hash_first(addr_table
);
1223 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1227 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1230 if (! (addr
= n
->network
) )
1233 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1234 if ( !addr
.isIPv4() )
1237 buf
[i
] = (char) NETDB_EX_NETWORK
;
1240 addr
.getInAddr(line_addr
);
1241 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1243 i
+= sizeof(struct in_addr
);
1245 buf
[i
] = (char) NETDB_EX_RTT
;
1248 j
= htonl((int) (n
->rtt
* 1000));
1250 memcpy(&buf
[i
], &j
, sizeof(int));
1254 buf
[i
] = (char) NETDB_EX_HOPS
;
1257 j
= htonl((int) (n
->hops
* 1000));
1259 memcpy(&buf
[i
], &j
, sizeof(int));
1263 if (i
+ rec_sz
> 4096) {
1276 memFree(buf
, MEM_4K_BUF
);
1279 reply
->setHeaders(Http::scBadRequest
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1280 s
->replaceHttpReply(reply
);
1281 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1288 netdbExchangeStart(void *data
)
1291 CachePeer
*p
= (CachePeer
*)data
;
1292 char *uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", "netdb");
1293 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1294 assert(NULL
!= uri
);
1295 HttpRequest
*req
= HttpRequest::CreateFromUrl(uri
);
1298 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1302 netdbExchangeState
*ex
= new netdbExchangeState(p
, req
);
1303 ex
->e
= storeCreateEntry(uri
, uri
, RequestFlags(), Http::METHOD_GET
);
1304 assert(NULL
!= ex
->e
);
1306 StoreIOBuffer tempBuffer
;
1307 tempBuffer
.length
= ex
->buf_sz
;
1308 tempBuffer
.data
= ex
->buf
;
1310 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1312 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1313 netdbExchangeHandleReply
, ex
);
1314 ex
->r
->flags
.loopDetected
= true; /* cheat! -- force direct */
1316 // XXX: send as Proxy-Authenticate instead
1318 ex
->r
->url
.userInfo(SBuf(p
->login
));
1320 urlCanonical(ex
->r
);
1322 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1328 netdbClosestParent(HttpRequest
* request
)
1331 CachePeer
*p
= NULL
;
1333 const ipcache_addrs
*ia
;
1336 n
= netdbLookupHost(request
->GetHost());
1340 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1343 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1349 if (0 == n
->n_peers
)
1352 n
->last_use_time
= squid_curtime
;
1355 * Find the parent with the least RTT to the origin server.
1356 * Make sure we don't return a parent who is farther away than
1357 * we are. Note, the n->peers list is pre-sorted by RTT.
1359 for (i
= 0; i
< n
->n_peers
; ++i
) {
1363 if (n
->rtt
< h
->rtt
)
1366 p
= peerFindByName(h
->peername
);
1368 if (NULL
== p
) /* not found */
1371 if (neighborType(p
, request
) != PEER_PARENT
)
1374 if (!peerHTTPOkay(p
, request
)) /* not allowed */