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
68 char buf
[NETDB_REQBUF_SZ
];
70 netdb_conn_state_t connstate
;
73 static hash_table
*addr_table
= NULL
;
74 static hash_table
*host_table
= NULL
;
76 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
77 static void netdbRelease(netdbEntry
* n
);
79 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
80 static void netdbHashDelete(const char *key
);
81 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
82 static void netdbHostDelete(const net_db_name
* x
);
83 static void netdbPurgeLRU(void);
84 static netdbEntry
*netdbLookupHost(const char *key
);
85 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
86 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
);
87 static const char *netdbPeerName(const char *name
);
88 static IPH netdbSendPing
;
89 static QS sortPeerByRtt
;
92 static FREE netdbFreeNameEntry
;
93 static FREE netdbFreeNetdbEntry
;
94 static STCB netdbExchangeHandleReply
;
95 static void netdbExchangeDone(void *);
97 /* We have to keep a local list of CachePeer names. The Peers structure
98 * gets freed during a reconfigure. We want this database to
99 * remain persisitent, so _net_db_peer->peername points into this
101 static wordlist
*peer_names
= NULL
;
104 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
106 networkFromInaddr(addr
).toStr(n
->network
, MAX_IPSTRLEN
);
107 n
->hash
.key
= n
->network
;
108 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
109 hash_join(addr_table
, &n
->hash
);
113 netdbHashDelete(const char *key
)
115 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
118 debug_trap("netdbHashDelete: key not found");
122 hash_remove_link(addr_table
, hptr
);
126 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
128 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
129 x
->hash
.key
= xstrdup(hostname
);
133 assert(hash_lookup(host_table
, hostname
) == NULL
);
134 hash_join(host_table
, &x
->hash
);
139 netdbHostDelete(const net_db_name
* x
)
144 assert(x
->net_db_entry
!= NULL
);
148 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
155 hash_remove_link(host_table
, (hash_link
*) x
);
157 memFree((void *) x
, MEM_NET_DB_NAME
);
161 netdbLookupHost(const char *key
)
163 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
164 return x
? x
->net_db_entry
: NULL
;
168 netdbRelease(netdbEntry
* n
)
173 for (x
= n
->hosts
; x
; x
= next
) {
182 n
->n_peers_alloc
= 0;
184 if (n
->link_count
== 0) {
185 netdbHashDelete(n
->network
);
186 memFree(n
, MEM_NETDBENTRY
);
191 netdbLRU(const void *A
, const void *B
)
193 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
194 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
196 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
199 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
213 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
214 hash_first(addr_table
);
216 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
217 assert(list_count
< memInUse(MEM_NETDBENTRY
));
218 *(list
+ list_count
) = n
;
224 sizeof(netdbEntry
*),
227 for (k
= 0; k
< list_count
; ++k
) {
228 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
231 netdbRelease(*(list
+ k
));
240 netdbLookupAddr(const Ip::Address
&addr
)
243 char *key
= new char[MAX_IPSTRLEN
];
244 networkFromInaddr(addr
).toStr(key
,MAX_IPSTRLEN
);
245 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
251 netdbAdd(Ip::Address
&addr
)
255 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
258 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
259 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
260 netdbHashInsert(n
, addr
);
267 netdbSendPing(const ipcache_addrs
*ia
, const DnsLookupDetails
&, void *data
)
270 char *hostname
= NULL
;
271 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
282 addr
= ia
->in_addrs
[ia
->cur
];
284 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
286 netdbHostInsert(n
, hostname
);
287 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
289 *hostname moved from 'network n' to 'network na'!
295 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
297 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
300 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
305 /* remove net_db_name from 'network n' linked list */
306 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
314 /* point to 'network na' from host entry */
315 x
->net_db_entry
= na
;
316 /* link net_db_name to 'network na' */
323 if (n
->next_ping_time
<= squid_curtime
) {
324 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
325 icmpEngine
.DomainPing(addr
, hostname
);
327 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
328 n
->last_use_time
= squid_curtime
;
335 networkFromInaddr(const Ip::Address
&in
)
341 /* in IPv6 the 'network' should be the routing section. */
343 out
.applyMask(64, AF_INET6
);
344 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
353 if (IN_CLASSC(b
.s_addr
))
354 b
.s_addr
&= IN_CLASSC_NET
;
355 else if (IN_CLASSB(b
.s_addr
))
356 b
.s_addr
&= IN_CLASSB_NET
;
357 else if (IN_CLASSA(b
.s_addr
))
358 b
.s_addr
&= IN_CLASSA_NET
;
364 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
366 /* use /24 for everything under IPv4 */
367 out
.applyMask(24, AF_INET
);
368 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
374 sortByRtt(const void *A
, const void *B
)
376 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
377 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
379 if ((*n1
)->rtt
> (*n2
)->rtt
)
381 else if ((*n1
)->rtt
< (*n2
)->rtt
)
388 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
391 net_db_peer
*p
= n
->peers
;
393 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
394 if (!strcmp(p
->peername
, peername
))
402 netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
)
409 if (n
->n_peers
== n
->n_peers_alloc
) {
411 osize
= n
->n_peers_alloc
;
413 if (n
->n_peers_alloc
== 0)
414 n
->n_peers_alloc
= 2;
416 n
->n_peers_alloc
<<= 1;
418 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
420 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
422 for (i
= 0; i
< osize
; ++i
)
423 *(n
->peers
+ i
) = *(o
+ i
);
430 p
= n
->peers
+ n
->n_peers
;
431 p
->peername
= netdbPeerName(e
->host
);
437 sortPeerByRtt(const void *A
, const void *B
)
439 const net_db_peer
*p1
= (net_db_peer
*)A
;
440 const net_db_peer
*p2
= (net_db_peer
*)B
;
442 if (p1
->rtt
> p2
->rtt
)
444 else if (p1
->rtt
< p2
->rtt
)
451 netdbSaveState(void *foo
)
453 if (strcmp(Config
.netdbFilename
, "none") == 0)
460 struct timeval start
= current_time
;
463 * This was nicer when we were using stdio, but thanks to
464 * Solaris bugs, its a bad idea. fopen can fail if more than
468 * unlink() is here because there is currently no way to make
469 * logfileOpen() use O_TRUNC.
471 unlink(Config
.netdbFilename
);
472 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
475 debugs(50, DBG_IMPORTANT
, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
479 hash_first(addr_table
);
481 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
482 if (n
->pings_recv
== 0)
485 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
491 (int) n
->next_ping_time
,
492 (int) n
->last_use_time
);
494 for (x
= n
->hosts
; x
; x
= x
->next
)
495 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
497 logfilePrintf(lf
, "\n");
507 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
508 count
<< " entries, " <<
509 tvSubMsec(start
, current_time
) << " msec" );
510 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
514 netdbReloadState(void)
516 if (strcmp(Config
.netdbFilename
, "none") == 0)
530 struct timeval start
= current_time
;
532 * This was nicer when we were using stdio, but thanks to
533 * Solaris bugs, its a bad idea. fopen can fail if more than
536 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
541 if (fstat(fd
, &sb
) < 0) {
547 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
549 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
557 while ((s
= strchr(t
, '\n'))) {
561 memset(&N
, '\0', sizeof(netdbEntry
));
562 q
= strtok(t
, w_space
);
571 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
574 if ((q
= strtok(NULL
, w_space
)) == NULL
)
577 N
.pings_sent
= atoi(q
);
579 if ((q
= strtok(NULL
, w_space
)) == NULL
)
582 N
.pings_recv
= atoi(q
);
584 if (N
.pings_recv
== 0)
587 /* give this measurement low weight */
592 if ((q
= strtok(NULL
, w_space
)) == NULL
)
597 if ((q
= strtok(NULL
, w_space
)) == NULL
)
602 if ((q
= strtok(NULL
, w_space
)) == NULL
)
605 N
.next_ping_time
= (time_t) atoi(q
);
607 if ((q
= strtok(NULL
, w_space
)) == NULL
)
610 N
.last_use_time
= (time_t) atoi(q
);
612 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
614 memcpy(n
, &N
, sizeof(netdbEntry
));
616 netdbHashInsert(n
, addr
);
618 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
619 if (netdbLookupHost(q
) != NULL
) /* no dups! */
622 netdbHostInsert(n
, q
);
630 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
631 count
<< " entries, " <<
632 tvSubMsec(start
, current_time
) << " msec" );
636 netdbPeerName(const char *name
)
640 for (w
= peer_names
; w
; w
= w
->next
) {
641 if (!strcmp(w
->key
, name
))
645 return wordlistAdd(&peer_names
, name
);
649 netdbFreeNetdbEntry(void *data
)
651 netdbEntry
*n
= (netdbEntry
*)data
;
653 memFree(n
, MEM_NETDBENTRY
);
657 netdbFreeNameEntry(void *data
)
659 net_db_name
*x
= (net_db_name
*)data
;
661 memFree(x
, MEM_NET_DB_NAME
);
665 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
669 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
673 struct in_addr line_addr
;
678 HttpReply
const *rep
;
682 int oldbufofs
= ex
->buf_ofs
;
685 rec_sz
+= 1 + sizeof(struct in_addr
);
686 rec_sz
+= 1 + sizeof(int);
687 rec_sz
+= 1 + sizeof(int);
688 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
690 if (!cbdataReferenceValid(ex
->p
)) {
691 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
692 netdbExchangeDone(ex
);
696 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
698 if (receivedData
.length
== 0 &&
699 !receivedData
.flags
.error
) {
700 debugs(38, 3, "netdbExchangeHandleReply: Done");
701 netdbExchangeDone(ex
);
707 /* Get the size of the buffer now */
708 size
= ex
->buf_ofs
+ receivedData
.length
;
709 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
711 /* Check if we're still doing headers */
713 if (ex
->connstate
== STATE_HEADER
) {
715 ex
->buf_ofs
+= receivedData
.length
;
717 /* skip reply headers */
719 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
720 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
721 rep
= ex
->e
->getReply();
722 assert(rep
->sline
.status() != Http::scNone
);
723 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status());
725 if (rep
->sline
.status() != Http::scOkay
) {
726 netdbExchangeDone(ex
);
730 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
733 * Now, point p to the part of the buffer where the data
734 * starts, and update the size accordingly
736 assert(ex
->used
== 0);
738 size
= ex
->buf_ofs
- hdr_sz
;
741 /* Finally, set the conn state mode to STATE_BODY */
742 ex
->connstate
= STATE_BODY
;
744 StoreIOBuffer tempBuffer
;
745 tempBuffer
.offset
= ex
->buf_ofs
;
746 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
747 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
748 /* Have more headers .. */
749 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
750 netdbExchangeHandleReply
, ex
);
755 assert(ex
->connstate
== STATE_BODY
);
757 /* If we get here, we have some body to parse .. */
758 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
760 while (size
>= rec_sz
) {
761 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
765 for (o
= 0; o
< rec_sz
;) {
766 switch ((int) *(p
+ o
)) {
768 case NETDB_EX_NETWORK
:
770 /* FIXME INET6 : NetDB can still ony send IPv4 */
771 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
773 o
+= sizeof(struct in_addr
);
778 memcpy(&j
, p
+ o
, sizeof(int));
780 rtt
= (double) ntohl(j
) / 1000.0;
785 memcpy(&j
, p
+ o
, sizeof(int));
787 hops
= (double) ntohl(j
) / 1000.0;
791 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
792 netdbExchangeDone(ex
);
797 if (!addr
.isAnyAddr() && rtt
> 0)
798 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
812 * Copy anything that is left over to the beginning of the buffer,
813 * and adjust buf_ofs accordingly
817 * Evilly, size refers to the buf size left now,
818 * ex->buf_ofs is the original buffer size, so just copy that
821 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
826 * And don't re-copy the remaining data ..
831 * Now the tricky bit - size _included_ the leftover bit from the _last_
832 * storeClientCopy. We don't want to include that, or our offset will be wrong.
833 * So, don't count the size of the leftover buffer we began with.
834 * This can _disappear_ when we're not tracking offsets ..
836 ex
->used
-= oldbufofs
;
838 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
840 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
841 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
844 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
846 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
847 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
848 netdbExchangeDone(ex
);
849 } else if (ex
->e
->store_status
== STORE_PENDING
) {
850 StoreIOBuffer tempBuffer
;
851 tempBuffer
.offset
= ex
->used
;
852 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
853 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
854 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
855 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
856 netdbExchangeHandleReply
, ex
);
861 netdbExchangeDone(void *data
)
863 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
864 debugs(38, 3, "netdbExchangeDone: " << ex
->e
->url() );
865 HTTPMSGUNLOCK(ex
->r
);
866 storeUnregister(ex
->sc
, ex
->e
, ex
);
867 ex
->e
->unlock("netdbExchangeDone");
868 cbdataReferenceDone(ex
->p
);
873 netdbRegisterWithCacheManager(void)
875 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
878 #endif /* USE_ICMP */
880 /* PUBLIC FUNCTIONS */
888 netdbRegisterWithCacheManager();
893 n
= hashPrime(Config
.Netdb
.high
/ 4);
895 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
897 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
899 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
901 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
909 netdbPingSite(const char *hostname
)
914 if ((n
= netdbLookupHost(hostname
)) != NULL
)
915 if (n
->next_ping_time
> squid_curtime
)
918 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
919 new generic_cbdata(xstrdup(hostname
)));
925 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
930 debugs(38, 3, "netdbHandlePingReply: from " << from
);
932 if ((n
= netdbLookupAddr(from
)) == NULL
)
943 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
945 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
947 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
948 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
949 std::setw(4) << n
->hops
);
955 netdbFreeMemory(void)
958 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
959 hashFreeMemory(addr_table
);
961 hashFreeItems(host_table
, netdbFreeNameEntry
);
962 hashFreeMemory(host_table
);
964 wordlistDestroy(&peer_names
);
970 netdbDump(StoreEntry
* sentry
)
980 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
981 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
987 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
989 hash_first(addr_table
);
991 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
996 if (i
!= memInUse(MEM_NETDBENTRY
))
997 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
998 ", expected " << memInUse(MEM_NETDBENTRY
));
1000 qsort((char *) list
,
1002 sizeof(netdbEntry
*),
1005 for (k
= 0; k
< i
; ++k
) {
1007 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1014 for (x
= n
->hosts
; x
; x
= x
->next
)
1015 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1017 storeAppendPrintf(sentry
, "\n");
1021 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1022 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1032 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1037 netdbHostHops(const char *host
)
1040 netdbEntry
*n
= netdbLookupHost(host
);
1043 n
->last_use_time
= squid_curtime
;
1044 return (int) (n
->hops
+ 0.5);
1052 netdbHostRtt(const char *host
)
1055 netdbEntry
*n
= netdbLookupHost(host
);
1058 n
->last_use_time
= squid_curtime
;
1059 return (int) (n
->rtt
+ 0.5);
1067 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1070 netdbEntry
*n
= netdbLookupHost(host
);
1075 *samp
= n
->pings_recv
;
1077 *rtt
= (int) (n
->rtt
+ 0.5);
1079 *hops
= (int) (n
->hops
+ 0.5);
1081 n
->last_use_time
= squid_curtime
;
1087 netdbUpdatePeer(HttpRequest
* r
, CachePeer
* e
, int irtt
, int ihops
)
1091 double rtt
= (double) irtt
;
1092 double hops
= (double) ihops
;
1094 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1095 n
= netdbLookupHost(r
->GetHost());
1098 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1102 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1103 p
= netdbPeerAdd(n
, e
);
1109 p
->expires
= squid_curtime
+ 3600;
1114 qsort((char *) n
->peers
,
1116 sizeof(net_db_peer
),
1123 netdbExchangeUpdatePeer(Ip::Address
&addr
, CachePeer
* e
, double rtt
, double hops
)
1128 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1129 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1132 if ( !addr
.isIPv4() ) {
1133 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1137 n
= netdbLookupAddr(addr
);
1144 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1145 p
= netdbPeerAdd(n
, e
);
1151 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1156 qsort((char *) n
->peers
,
1158 sizeof(net_db_peer
),
1165 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1168 netdbEntry
*n
= netdbLookupAddr(addr
);
1173 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1180 netdbBinaryExchange(StoreEntry
* s
)
1182 HttpReply
*reply
= new HttpReply
;
1193 struct in_addr line_addr
;
1195 reply
->setHeaders(Http::scOkay
, "OK", NULL
, -1, squid_curtime
, -2);
1196 s
->replaceHttpReply(reply
);
1198 rec_sz
+= 1 + sizeof(struct in_addr
);
1199 rec_sz
+= 1 + sizeof(int);
1200 rec_sz
+= 1 + sizeof(int);
1201 buf
= (char *)memAllocate(MEM_4K_BUF
);
1203 hash_first(addr_table
);
1205 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1209 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1212 if (! (addr
= n
->network
) )
1215 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1216 if ( !addr
.isIPv4() )
1219 buf
[i
] = (char) NETDB_EX_NETWORK
;
1222 addr
.getInAddr(line_addr
);
1223 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1225 i
+= sizeof(struct in_addr
);
1227 buf
[i
] = (char) NETDB_EX_RTT
;
1230 j
= htonl((int) (n
->rtt
* 1000));
1232 memcpy(&buf
[i
], &j
, sizeof(int));
1236 buf
[i
] = (char) NETDB_EX_HOPS
;
1239 j
= htonl((int) (n
->hops
* 1000));
1241 memcpy(&buf
[i
], &j
, sizeof(int));
1245 if (i
+ rec_sz
> 4096) {
1258 memFree(buf
, MEM_4K_BUF
);
1261 reply
->setHeaders(Http::scBadRequest
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1262 s
->replaceHttpReply(reply
);
1263 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1270 CBDATA_TYPE(netdbExchangeState
);
1274 netdbExchangeStart(void *data
)
1277 CachePeer
*p
= (CachePeer
*)data
;
1279 netdbExchangeState
*ex
;
1280 StoreIOBuffer tempBuffer
;
1281 CBDATA_INIT_TYPE(netdbExchangeState
);
1282 ex
= cbdataAlloc(netdbExchangeState
);
1283 ex
->p
= cbdataReference(p
);
1284 uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", "netdb");
1285 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1286 assert(NULL
!= uri
);
1287 ex
->r
= HttpRequest::CreateFromUrl(uri
);
1289 if (NULL
== ex
->r
) {
1290 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1295 assert(NULL
!= ex
->r
);
1296 ex
->r
->http_ver
= Http::ProtocolVersion();
1297 ex
->connstate
= STATE_HEADER
;
1298 ex
->e
= storeCreateEntry(uri
, uri
, RequestFlags(), Http::METHOD_GET
);
1299 ex
->buf_sz
= NETDB_REQBUF_SZ
;
1300 assert(NULL
!= ex
->e
);
1301 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1302 tempBuffer
.offset
= 0;
1303 tempBuffer
.length
= ex
->buf_sz
;
1304 tempBuffer
.data
= ex
->buf
;
1305 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1306 netdbExchangeHandleReply
, ex
);
1307 ex
->r
->flags
.loopDetected
= true; /* cheat! -- force direct */
1309 // XXX: send as Proxy-Authenticate instead
1311 ex
->r
->url
.userInfo(SBuf(p
->login
));
1313 urlCanonical(ex
->r
);
1315 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1321 netdbClosestParent(HttpRequest
* request
)
1324 CachePeer
*p
= NULL
;
1326 const ipcache_addrs
*ia
;
1329 n
= netdbLookupHost(request
->GetHost());
1333 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1336 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1342 if (0 == n
->n_peers
)
1345 n
->last_use_time
= squid_curtime
;
1348 * Find the parent with the least RTT to the origin server.
1349 * Make sure we don't return a parent who is farther away than
1350 * we are. Note, the n->peers list is pre-sorted by RTT.
1352 for (i
= 0; i
< n
->n_peers
; ++i
) {
1356 if (n
->rtt
< h
->rtt
)
1359 p
= peerFindByName(h
->peername
);
1361 if (NULL
== p
) /* not found */
1364 if (neighborType(p
, request
) != PEER_PARENT
)
1367 if (!peerHTTPOkay(p
, request
)) /* not allowed */