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"
48 #include "icmp/IcmpSquid.h"
50 #include "StoreClient.h"
52 #define NETDB_REQBUF_SZ 4096
60 class netdbExchangeState
62 CBDATA_CLASS(netdbExchangeState
);
65 netdbExchangeState(CachePeer
*aPeer
, HttpRequest
*theReq
) :
66 p(cbdataReference(aPeer
)),
71 buf_sz(NETDB_REQBUF_SZ
),
73 connstate(STATE_HEADER
)
79 // TODO: check if we actually need to do this. should be implicit
80 r
->http_ver
= Http::ProtocolVersion();
83 ~netdbExchangeState() {
84 debugs(38, 3, e
->url());
85 storeUnregister(sc
, e
, this);
86 e
->unlock("netdbExchangeDone");
88 cbdataReferenceDone(p
);
97 char buf
[NETDB_REQBUF_SZ
];
99 netdb_conn_state_t connstate
;
102 CBDATA_CLASS_INIT(netdbExchangeState
);
104 static hash_table
*addr_table
= NULL
;
105 static hash_table
*host_table
= NULL
;
107 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
108 static void netdbRelease(netdbEntry
* n
);
110 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
111 static void netdbHashDelete(const char *key
);
112 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
113 static void netdbHostDelete(const net_db_name
* x
);
114 static void netdbPurgeLRU(void);
115 static netdbEntry
*netdbLookupHost(const char *key
);
116 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
117 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
);
118 static const char *netdbPeerName(const char *name
);
119 static IPH netdbSendPing
;
120 static FREE netdbFreeNameEntry
;
121 static FREE netdbFreeNetdbEntry
;
122 static STCB netdbExchangeHandleReply
;
124 /* We have to keep a local list of CachePeer names. The Peers structure
125 * gets freed during a reconfigure. We want this database to
126 * remain persisitent, so _net_db_peer->peername points into this
128 static wordlist
*peer_names
= NULL
;
131 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
133 networkFromInaddr(addr
).toStr(n
->network
, MAX_IPSTRLEN
);
134 n
->hash
.key
= n
->network
;
135 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
136 hash_join(addr_table
, &n
->hash
);
140 netdbHashDelete(const char *key
)
142 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
145 debug_trap("netdbHashDelete: key not found");
149 hash_remove_link(addr_table
, hptr
);
153 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
155 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
156 x
->hash
.key
= xstrdup(hostname
);
160 assert(hash_lookup(host_table
, hostname
) == NULL
);
161 hash_join(host_table
, &x
->hash
);
166 netdbHostDelete(const net_db_name
* x
)
171 assert(x
->net_db_entry
!= NULL
);
175 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
182 hash_remove_link(host_table
, (hash_link
*) x
);
184 memFree((void *) x
, MEM_NET_DB_NAME
);
188 netdbLookupHost(const char *key
)
190 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
191 return x
? x
->net_db_entry
: NULL
;
195 netdbRelease(netdbEntry
* n
)
200 for (x
= n
->hosts
; x
; x
= next
) {
209 n
->n_peers_alloc
= 0;
211 if (n
->link_count
== 0) {
212 netdbHashDelete(n
->network
);
213 memFree(n
, MEM_NETDBENTRY
);
218 netdbLRU(const void *A
, const void *B
)
220 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
221 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
223 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
226 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
240 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
241 hash_first(addr_table
);
243 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
244 assert(list_count
< memInUse(MEM_NETDBENTRY
));
245 *(list
+ list_count
) = n
;
251 sizeof(netdbEntry
*),
254 for (k
= 0; k
< list_count
; ++k
) {
255 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
258 netdbRelease(*(list
+ k
));
267 netdbLookupAddr(const Ip::Address
&addr
)
270 char *key
= new char[MAX_IPSTRLEN
];
271 networkFromInaddr(addr
).toStr(key
,MAX_IPSTRLEN
);
272 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
278 netdbAdd(Ip::Address
&addr
)
282 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
285 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
286 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
287 netdbHashInsert(n
, addr
);
294 netdbSendPing(const ipcache_addrs
*ia
, const Dns::LookupDetails
&, void *data
)
297 char *hostname
= NULL
;
298 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
309 addr
= ia
->in_addrs
[ia
->cur
];
311 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
313 netdbHostInsert(n
, hostname
);
314 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
316 *hostname moved from 'network n' to 'network na'!
322 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
324 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
327 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
332 /* remove net_db_name from 'network n' linked list */
333 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
341 /* point to 'network na' from host entry */
342 x
->net_db_entry
= na
;
343 /* link net_db_name to 'network na' */
350 if (n
->next_ping_time
<= squid_curtime
) {
351 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
352 icmpEngine
.DomainPing(addr
, hostname
);
354 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
355 n
->last_use_time
= squid_curtime
;
362 networkFromInaddr(const Ip::Address
&in
)
368 /* in IPv6 the 'network' should be the routing section. */
370 out
.applyMask(64, AF_INET6
);
371 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
380 if (IN_CLASSC(b
.s_addr
))
381 b
.s_addr
&= IN_CLASSC_NET
;
382 else if (IN_CLASSB(b
.s_addr
))
383 b
.s_addr
&= IN_CLASSB_NET
;
384 else if (IN_CLASSA(b
.s_addr
))
385 b
.s_addr
&= IN_CLASSA_NET
;
391 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
393 /* use /24 for everything under IPv4 */
394 out
.applyMask(24, AF_INET
);
395 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
401 sortByRtt(const void *A
, const void *B
)
403 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
404 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
406 if ((*n1
)->rtt
> (*n2
)->rtt
)
408 else if ((*n1
)->rtt
< (*n2
)->rtt
)
415 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
418 net_db_peer
*p
= n
->peers
;
420 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
421 if (!strcmp(p
->peername
, peername
))
429 netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
)
436 if (n
->n_peers
== n
->n_peers_alloc
) {
438 osize
= n
->n_peers_alloc
;
440 if (n
->n_peers_alloc
== 0)
441 n
->n_peers_alloc
= 2;
443 n
->n_peers_alloc
<<= 1;
445 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
447 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
449 for (i
= 0; i
< osize
; ++i
)
450 *(n
->peers
+ i
) = *(o
+ i
);
457 p
= n
->peers
+ n
->n_peers
;
458 p
->peername
= netdbPeerName(e
->host
);
464 sortPeerByRtt(const void *A
, const void *B
)
466 const net_db_peer
*p1
= (net_db_peer
*)A
;
467 const net_db_peer
*p2
= (net_db_peer
*)B
;
469 if (p1
->rtt
> p2
->rtt
)
471 else if (p1
->rtt
< p2
->rtt
)
478 netdbSaveState(void *foo
)
480 if (strcmp(Config
.netdbFilename
, "none") == 0)
487 struct timeval start
= current_time
;
490 * This was nicer when we were using stdio, but thanks to
491 * Solaris bugs, its a bad idea. fopen can fail if more than
495 * unlink() is here because there is currently no way to make
496 * logfileOpen() use O_TRUNC.
498 unlink(Config
.netdbFilename
);
499 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
502 debugs(50, DBG_IMPORTANT
, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
506 hash_first(addr_table
);
508 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
509 if (n
->pings_recv
== 0)
512 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
518 (int) n
->next_ping_time
,
519 (int) n
->last_use_time
);
521 for (x
= n
->hosts
; x
; x
= x
->next
)
522 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
524 logfilePrintf(lf
, "\n");
534 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
535 count
<< " entries, " <<
536 tvSubMsec(start
, current_time
) << " msec" );
537 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
541 netdbReloadState(void)
543 if (strcmp(Config
.netdbFilename
, "none") == 0)
557 struct timeval start
= current_time
;
559 * This was nicer when we were using stdio, but thanks to
560 * Solaris bugs, its a bad idea. fopen can fail if more than
563 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
568 if (fstat(fd
, &sb
) < 0) {
574 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
576 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
584 while ((s
= strchr(t
, '\n'))) {
588 memset(&N
, '\0', sizeof(netdbEntry
));
589 q
= strtok(t
, w_space
);
598 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
601 if ((q
= strtok(NULL
, w_space
)) == NULL
)
604 N
.pings_sent
= atoi(q
);
606 if ((q
= strtok(NULL
, w_space
)) == NULL
)
609 N
.pings_recv
= atoi(q
);
611 if (N
.pings_recv
== 0)
614 /* give this measurement low weight */
619 if ((q
= strtok(NULL
, w_space
)) == NULL
)
624 if ((q
= strtok(NULL
, w_space
)) == NULL
)
629 if ((q
= strtok(NULL
, w_space
)) == NULL
)
632 N
.next_ping_time
= (time_t) atoi(q
);
634 if ((q
= strtok(NULL
, w_space
)) == NULL
)
637 N
.last_use_time
= (time_t) atoi(q
);
639 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
641 memcpy(n
, &N
, sizeof(netdbEntry
));
643 netdbHashInsert(n
, addr
);
645 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
646 if (netdbLookupHost(q
) != NULL
) /* no dups! */
649 netdbHostInsert(n
, q
);
657 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
658 count
<< " entries, " <<
659 tvSubMsec(start
, current_time
) << " msec" );
663 netdbPeerName(const char *name
)
667 for (w
= peer_names
; w
; w
= w
->next
) {
668 if (!strcmp(w
->key
, name
))
672 return wordlistAdd(&peer_names
, name
);
676 netdbFreeNetdbEntry(void *data
)
678 netdbEntry
*n
= (netdbEntry
*)data
;
680 memFree(n
, MEM_NETDBENTRY
);
684 netdbFreeNameEntry(void *data
)
686 net_db_name
*x
= (net_db_name
*)data
;
688 memFree(x
, MEM_NET_DB_NAME
);
692 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
696 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
700 struct in_addr line_addr
;
705 HttpReply
const *rep
;
709 int oldbufofs
= ex
->buf_ofs
;
712 rec_sz
+= 1 + sizeof(struct in_addr
);
713 rec_sz
+= 1 + sizeof(int);
714 rec_sz
+= 1 + sizeof(int);
715 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
717 if (!cbdataReferenceValid(ex
->p
)) {
718 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
723 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
725 if (receivedData
.length
== 0 && !receivedData
.flags
.error
) {
726 debugs(38, 3, "netdbExchangeHandleReply: Done");
733 /* Get the size of the buffer now */
734 size
= ex
->buf_ofs
+ receivedData
.length
;
735 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
737 /* Check if we're still doing headers */
739 if (ex
->connstate
== STATE_HEADER
) {
741 ex
->buf_ofs
+= receivedData
.length
;
743 /* skip reply headers */
745 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
746 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
747 rep
= ex
->e
->getReply();
748 assert(rep
->sline
.status() != Http::scNone
);
749 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status());
751 if (rep
->sline
.status() != Http::scOkay
) {
756 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
759 * Now, point p to the part of the buffer where the data
760 * starts, and update the size accordingly
762 assert(ex
->used
== 0);
764 size
= ex
->buf_ofs
- hdr_sz
;
767 /* Finally, set the conn state mode to STATE_BODY */
768 ex
->connstate
= STATE_BODY
;
770 StoreIOBuffer tempBuffer
;
771 tempBuffer
.offset
= ex
->buf_ofs
;
772 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
773 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
774 /* Have more headers .. */
775 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
776 netdbExchangeHandleReply
, ex
);
781 assert(ex
->connstate
== STATE_BODY
);
783 /* If we get here, we have some body to parse .. */
784 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
786 while (size
>= rec_sz
) {
787 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
791 for (o
= 0; o
< rec_sz
;) {
792 switch ((int) *(p
+ o
)) {
794 case NETDB_EX_NETWORK
:
796 /* FIXME INET6 : NetDB can still ony send IPv4 */
797 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
799 o
+= sizeof(struct in_addr
);
804 memcpy(&j
, p
+ o
, sizeof(int));
806 rtt
= (double) ntohl(j
) / 1000.0;
811 memcpy(&j
, p
+ o
, sizeof(int));
813 hops
= (double) ntohl(j
) / 1000.0;
817 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
823 if (!addr
.isAnyAddr() && rtt
> 0)
824 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
838 * Copy anything that is left over to the beginning of the buffer,
839 * and adjust buf_ofs accordingly
843 * Evilly, size refers to the buf size left now,
844 * ex->buf_ofs is the original buffer size, so just copy that
847 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
852 * And don't re-copy the remaining data ..
857 * Now the tricky bit - size _included_ the leftover bit from the _last_
858 * storeClientCopy. We don't want to include that, or our offset will be wrong.
859 * So, don't count the size of the leftover buffer we began with.
860 * This can _disappear_ when we're not tracking offsets ..
862 ex
->used
-= oldbufofs
;
864 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
866 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
867 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
870 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
872 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
873 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
875 } else if (ex
->e
->store_status
== STORE_PENDING
) {
876 StoreIOBuffer tempBuffer
;
877 tempBuffer
.offset
= ex
->used
;
878 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
879 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
880 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
881 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
882 netdbExchangeHandleReply
, ex
);
887 netdbRegisterWithCacheManager(void)
889 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
892 #endif /* USE_ICMP */
894 /* PUBLIC FUNCTIONS */
902 netdbRegisterWithCacheManager();
907 n
= hashPrime(Config
.Netdb
.high
/ 4);
909 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
911 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
913 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
915 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
923 netdbPingSite(const char *hostname
)
928 if ((n
= netdbLookupHost(hostname
)) != NULL
)
929 if (n
->next_ping_time
> squid_curtime
)
932 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
933 new generic_cbdata(xstrdup(hostname
)));
939 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
944 debugs(38, 3, "netdbHandlePingReply: from " << from
);
946 if ((n
= netdbLookupAddr(from
)) == NULL
)
957 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
959 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
961 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
962 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
963 std::setw(4) << n
->hops
);
969 netdbFreeMemory(void)
972 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
973 hashFreeMemory(addr_table
);
975 hashFreeItems(host_table
, netdbFreeNameEntry
);
976 hashFreeMemory(host_table
);
978 wordlistDestroy(&peer_names
);
984 netdbDump(StoreEntry
* sentry
)
994 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
995 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1001 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1003 hash_first(addr_table
);
1005 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1010 if (i
!= memInUse(MEM_NETDBENTRY
))
1011 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
1012 ", expected " << memInUse(MEM_NETDBENTRY
));
1014 qsort((char *) list
,
1016 sizeof(netdbEntry
*),
1019 for (k
= 0; k
< i
; ++k
) {
1021 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1028 for (x
= n
->hosts
; x
; x
= x
->next
)
1029 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1031 storeAppendPrintf(sentry
, "\n");
1035 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1036 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1046 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1051 netdbHostHops(const char *host
)
1054 netdbEntry
*n
= netdbLookupHost(host
);
1057 n
->last_use_time
= squid_curtime
;
1058 return (int) (n
->hops
+ 0.5);
1066 netdbHostRtt(const char *host
)
1069 netdbEntry
*n
= netdbLookupHost(host
);
1072 n
->last_use_time
= squid_curtime
;
1073 return (int) (n
->rtt
+ 0.5);
1081 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1084 netdbEntry
*n
= netdbLookupHost(host
);
1089 *samp
= n
->pings_recv
;
1091 *rtt
= (int) (n
->rtt
+ 0.5);
1093 *hops
= (int) (n
->hops
+ 0.5);
1095 n
->last_use_time
= squid_curtime
;
1101 netdbUpdatePeer(const URL
&url
, CachePeer
* e
, int irtt
, int ihops
)
1105 double rtt
= (double) irtt
;
1106 double hops
= (double) ihops
;
1108 debugs(38, 3, url
.host() << ", " << ihops
<< " hops, " << irtt
<< " rtt");
1109 n
= netdbLookupHost(url
.host());
1112 debugs(38, 3, "host " << url
.host() << " not found");
1116 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1117 p
= netdbPeerAdd(n
, e
);
1123 p
->expires
= squid_curtime
+ 3600;
1128 qsort((char *) n
->peers
,
1130 sizeof(net_db_peer
),
1137 netdbExchangeUpdatePeer(Ip::Address
&addr
, CachePeer
* e
, double rtt
, double hops
)
1142 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1143 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1146 if ( !addr
.isIPv4() ) {
1147 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1151 n
= netdbLookupAddr(addr
);
1158 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1159 p
= netdbPeerAdd(n
, e
);
1165 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1170 qsort((char *) n
->peers
,
1172 sizeof(net_db_peer
),
1179 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1182 netdbEntry
*n
= netdbLookupAddr(addr
);
1187 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1194 netdbBinaryExchange(StoreEntry
* s
)
1196 HttpReply
*reply
= new HttpReply
;
1207 struct in_addr line_addr
;
1209 reply
->setHeaders(Http::scOkay
, "OK", NULL
, -1, squid_curtime
, -2);
1210 s
->replaceHttpReply(reply
);
1212 rec_sz
+= 1 + sizeof(struct in_addr
);
1213 rec_sz
+= 1 + sizeof(int);
1214 rec_sz
+= 1 + sizeof(int);
1215 buf
= (char *)memAllocate(MEM_4K_BUF
);
1217 hash_first(addr_table
);
1219 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1223 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1226 if (! (addr
= n
->network
) )
1229 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1230 if ( !addr
.isIPv4() )
1233 buf
[i
] = (char) NETDB_EX_NETWORK
;
1236 addr
.getInAddr(line_addr
);
1237 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1239 i
+= sizeof(struct in_addr
);
1241 buf
[i
] = (char) NETDB_EX_RTT
;
1244 j
= htonl((int) (n
->rtt
* 1000));
1246 memcpy(&buf
[i
], &j
, sizeof(int));
1250 buf
[i
] = (char) NETDB_EX_HOPS
;
1253 j
= htonl((int) (n
->hops
* 1000));
1255 memcpy(&buf
[i
], &j
, sizeof(int));
1259 if (i
+ rec_sz
> 4096) {
1272 memFree(buf
, MEM_4K_BUF
);
1275 reply
->setHeaders(Http::scBadRequest
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1276 s
->replaceHttpReply(reply
);
1277 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1284 netdbExchangeStart(void *data
)
1287 CachePeer
*p
= (CachePeer
*)data
;
1288 static const SBuf
netDB("netdb");
1289 char *uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", netDB
);
1290 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1291 assert(NULL
!= uri
);
1292 HttpRequest
*req
= HttpRequest::CreateFromUrl(uri
);
1295 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1299 netdbExchangeState
*ex
= new netdbExchangeState(p
, req
);
1300 ex
->e
= storeCreateEntry(uri
, uri
, RequestFlags(), Http::METHOD_GET
);
1301 assert(NULL
!= ex
->e
);
1303 StoreIOBuffer tempBuffer
;
1304 tempBuffer
.length
= ex
->buf_sz
;
1305 tempBuffer
.data
= ex
->buf
;
1307 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1309 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1310 netdbExchangeHandleReply
, ex
);
1311 ex
->r
->flags
.loopDetected
= true; /* cheat! -- force direct */
1313 // XXX: send as Proxy-Authenticate instead
1315 ex
->r
->url
.userInfo(SBuf(p
->login
));
1317 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1323 netdbClosestParent(HttpRequest
* request
)
1326 CachePeer
*p
= NULL
;
1328 const ipcache_addrs
*ia
;
1331 n
= netdbLookupHost(request
->url
.host());
1335 ia
= ipcache_gethostbyname(request
->url
.host(), 0);
1338 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1344 if (0 == n
->n_peers
)
1347 n
->last_use_time
= squid_curtime
;
1350 * Find the parent with the least RTT to the origin server.
1351 * Make sure we don't return a parent who is farther away than
1352 * we are. Note, the n->peers list is pre-sorted by RTT.
1354 for (i
= 0; i
< n
->n_peers
; ++i
) {
1358 if (n
->rtt
< h
->rtt
)
1361 p
= peerFindByName(h
->peername
);
1363 if (NULL
== p
) /* not found */
1366 if (neighborType(p
, request
->url
) != PEER_PARENT
)
1369 if (!peerHTTPOkay(p
, request
)) /* not allowed */