2 * DEBUG: section 38 Network Measurement Database
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
36 * This code may be slightly broken now. If you're getting consistent
37 * (sometimes working) corrupt data exchanges, please contact adrian
38 * (adrian@squid-cache.org) to sort them out.
47 #include "HttpReply.h"
48 #include "HttpRequest.h"
49 #include "icmp/net_db.h"
51 #include "ip/Address.h"
54 #include "MemObject.h"
55 #include "mgr/Registration.h"
56 #include "mime_header.h"
57 #include "neighbors.h"
58 #include "SquidTime.h"
60 #include "StoreClient.h"
71 #include "icmp/IcmpSquid.h"
73 #include "StoreClient.h"
75 #define NETDB_REQBUF_SZ 4096
90 char buf
[NETDB_REQBUF_SZ
];
92 netdb_conn_state_t connstate
;
95 static hash_table
*addr_table
= NULL
;
96 static hash_table
*host_table
= NULL
;
98 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
99 static void netdbRelease(netdbEntry
* n
);
101 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
102 static void netdbHashDelete(const char *key
);
103 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
104 static void netdbHostDelete(const net_db_name
* x
);
105 static void netdbPurgeLRU(void);
106 static netdbEntry
*netdbLookupHost(const char *key
);
107 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
108 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, peer
* e
);
109 static const char *netdbPeerName(const char *name
);
110 static IPH netdbSendPing
;
111 static QS sortPeerByRtt
;
114 static FREE netdbFreeNameEntry
;
115 static FREE netdbFreeNetdbEntry
;
116 static STCB netdbExchangeHandleReply
;
117 static void netdbExchangeDone(void *);
119 /* We have to keep a local list of peer names. The Peers structure
120 * gets freed during a reconfigure. We want this database to
121 * remain persisitent, so _net_db_peer->peername points into this
123 static wordlist
*peer_names
= NULL
;
126 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
128 networkFromInaddr(addr
).NtoA(n
->network
, MAX_IPSTRLEN
);
129 n
->hash
.key
= n
->network
;
130 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
131 hash_join(addr_table
, &n
->hash
);
135 netdbHashDelete(const char *key
)
137 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
140 debug_trap("netdbHashDelete: key not found");
144 hash_remove_link(addr_table
, hptr
);
148 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
150 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
151 x
->hash
.key
= xstrdup(hostname
);
155 assert(hash_lookup(host_table
, hostname
) == NULL
);
156 hash_join(host_table
, &x
->hash
);
161 netdbHostDelete(const net_db_name
* x
)
166 assert(x
->net_db_entry
!= NULL
);
170 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
177 hash_remove_link(host_table
, (hash_link
*) x
);
179 memFree((void *) x
, MEM_NET_DB_NAME
);
183 netdbLookupHost(const char *key
)
185 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
186 return x
? x
->net_db_entry
: NULL
;
190 netdbRelease(netdbEntry
* n
)
195 for (x
= n
->hosts
; x
; x
= next
) {
204 n
->n_peers_alloc
= 0;
206 if (n
->link_count
== 0) {
207 netdbHashDelete(n
->network
);
208 memFree(n
, MEM_NETDBENTRY
);
213 netdbLRU(const void *A
, const void *B
)
215 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
216 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
218 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
221 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
235 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
236 hash_first(addr_table
);
238 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
239 assert(list_count
< memInUse(MEM_NETDBENTRY
));
240 *(list
+ list_count
) = n
;
246 sizeof(netdbEntry
*),
249 for (k
= 0; k
< list_count
; ++k
) {
250 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
253 netdbRelease(*(list
+ k
));
262 netdbLookupAddr(const Ip::Address
&addr
)
265 char *key
= new char[MAX_IPSTRLEN
];
266 networkFromInaddr(addr
).NtoA(key
,MAX_IPSTRLEN
);
267 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
273 netdbAdd(Ip::Address
&addr
)
277 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
280 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
281 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
282 netdbHashInsert(n
, addr
);
289 netdbSendPing(const ipcache_addrs
*ia
, const DnsLookupDetails
&, void *data
)
292 char *hostname
= NULL
;
293 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
304 addr
= ia
->in_addrs
[ia
->cur
];
306 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
308 netdbHostInsert(n
, hostname
);
309 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
311 *hostname moved from 'network n' to 'network na'!
317 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
319 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
322 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
327 /* remove net_db_name from 'network n' linked list */
328 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
336 /* point to 'network na' from host entry */
337 x
->net_db_entry
= na
;
338 /* link net_db_name to 'network na' */
345 if (n
->next_ping_time
<= squid_curtime
) {
346 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
347 icmpEngine
.DomainPing(addr
, hostname
);
349 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
350 n
->last_use_time
= squid_curtime
;
357 networkFromInaddr(const Ip::Address
&in
)
363 /* in IPv6 the 'network' should be the routing section. */
365 out
.ApplyMask(64, AF_INET6
);
366 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
375 if (IN_CLASSC(b
.s_addr
))
376 b
.s_addr
&= IN_CLASSC_NET
;
377 else if (IN_CLASSB(b
.s_addr
))
378 b
.s_addr
&= IN_CLASSB_NET
;
379 else if (IN_CLASSA(b
.s_addr
))
380 b
.s_addr
&= IN_CLASSA_NET
;
386 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
388 /* use /24 for everything under IPv4 */
389 out
.ApplyMask(24, AF_INET
);
390 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
396 sortByRtt(const void *A
, const void *B
)
398 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
399 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
401 if ((*n1
)->rtt
> (*n2
)->rtt
)
403 else if ((*n1
)->rtt
< (*n2
)->rtt
)
410 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
413 net_db_peer
*p
= n
->peers
;
415 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
416 if (!strcmp(p
->peername
, peername
))
424 netdbPeerAdd(netdbEntry
* n
, peer
* e
)
431 if (n
->n_peers
== n
->n_peers_alloc
) {
433 osize
= n
->n_peers_alloc
;
435 if (n
->n_peers_alloc
== 0)
436 n
->n_peers_alloc
= 2;
438 n
->n_peers_alloc
<<= 1;
440 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
442 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
444 for (i
= 0; i
< osize
; ++i
)
445 *(n
->peers
+ i
) = *(o
+ i
);
452 p
= n
->peers
+ n
->n_peers
;
453 p
->peername
= netdbPeerName(e
->host
);
459 sortPeerByRtt(const void *A
, const void *B
)
461 const net_db_peer
*p1
= (net_db_peer
*)A
;
462 const net_db_peer
*p2
= (net_db_peer
*)B
;
464 if (p1
->rtt
> p2
->rtt
)
466 else if (p1
->rtt
< p2
->rtt
)
473 netdbSaveState(void *foo
)
475 if (strcmp(Config
.netdbFilename
, "none") == 0)
482 struct timeval start
= current_time
;
485 * This was nicer when we were using stdio, but thanks to
486 * Solaris bugs, its a bad idea. fopen can fail if more than
490 * unlink() is here because there is currently no way to make
491 * logfileOpen() use O_TRUNC.
493 unlink(Config
.netdbFilename
);
494 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
497 debugs(50, DBG_IMPORTANT
, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
501 hash_first(addr_table
);
503 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
504 if (n
->pings_recv
== 0)
507 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
513 (int) n
->next_ping_time
,
514 (int) n
->last_use_time
);
516 for (x
= n
->hosts
; x
; x
= x
->next
)
517 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
519 logfilePrintf(lf
, "\n");
529 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
530 count
<< " entries, " <<
531 tvSubMsec(start
, current_time
) << " msec" );
532 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
536 netdbReloadState(void)
538 if (strcmp(Config
.netdbFilename
, "none") == 0)
552 struct timeval start
= current_time
;
554 * This was nicer when we were using stdio, but thanks to
555 * Solaris bugs, its a bad idea. fopen can fail if more than
558 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
563 if (fstat(fd
, &sb
) < 0) {
569 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
571 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
579 while ((s
= strchr(t
, '\n'))) {
583 memset(&N
, '\0', sizeof(netdbEntry
));
584 q
= strtok(t
, w_space
);
593 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
596 if ((q
= strtok(NULL
, w_space
)) == NULL
)
599 N
.pings_sent
= atoi(q
);
601 if ((q
= strtok(NULL
, w_space
)) == NULL
)
604 N
.pings_recv
= atoi(q
);
606 if (N
.pings_recv
== 0)
609 /* give this measurement low weight */
614 if ((q
= strtok(NULL
, w_space
)) == NULL
)
619 if ((q
= strtok(NULL
, w_space
)) == NULL
)
624 if ((q
= strtok(NULL
, w_space
)) == NULL
)
627 N
.next_ping_time
= (time_t) atoi(q
);
629 if ((q
= strtok(NULL
, w_space
)) == NULL
)
632 N
.last_use_time
= (time_t) atoi(q
);
634 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
636 memcpy(n
, &N
, sizeof(netdbEntry
));
638 netdbHashInsert(n
, addr
);
640 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
641 if (netdbLookupHost(q
) != NULL
) /* no dups! */
644 netdbHostInsert(n
, q
);
652 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
653 count
<< " entries, " <<
654 tvSubMsec(start
, current_time
) << " msec" );
658 netdbPeerName(const char *name
)
662 for (w
= peer_names
; w
; w
= w
->next
) {
663 if (!strcmp(w
->key
, name
))
667 return wordlistAdd(&peer_names
, name
);
671 netdbFreeNetdbEntry(void *data
)
673 netdbEntry
*n
= (netdbEntry
*)data
;
675 memFree(n
, MEM_NETDBENTRY
);
679 netdbFreeNameEntry(void *data
)
681 net_db_name
*x
= (net_db_name
*)data
;
683 memFree(x
, MEM_NET_DB_NAME
);
687 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
691 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
695 struct in_addr line_addr
;
700 HttpReply
const *rep
;
704 int oldbufofs
= ex
->buf_ofs
;
707 rec_sz
+= 1 + sizeof(struct in_addr
);
708 rec_sz
+= 1 + sizeof(int);
709 rec_sz
+= 1 + sizeof(int);
710 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
712 if (!cbdataReferenceValid(ex
->p
)) {
713 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
714 netdbExchangeDone(ex
);
718 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
720 if (receivedData
.length
== 0 &&
721 !receivedData
.flags
.error
) {
722 debugs(38, 3, "netdbExchangeHandleReply: Done");
723 netdbExchangeDone(ex
);
729 /* Get the size of the buffer now */
730 size
= ex
->buf_ofs
+ receivedData
.length
;
731 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
733 /* Check if we're still doing headers */
735 if (ex
->connstate
== STATE_HEADER
) {
737 ex
->buf_ofs
+= receivedData
.length
;
739 /* skip reply headers */
741 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
742 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
743 rep
= ex
->e
->getReply();
744 assert (0 != rep
->sline
.status
);
745 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status
);
747 if (HTTP_OK
!= rep
->sline
.status
) {
748 netdbExchangeDone(ex
);
752 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
755 * Now, point p to the part of the buffer where the data
756 * starts, and update the size accordingly
758 assert(ex
->used
== 0);
760 size
= ex
->buf_ofs
- hdr_sz
;
763 /* Finally, set the conn state mode to STATE_BODY */
764 ex
->connstate
= STATE_BODY
;
766 StoreIOBuffer tempBuffer
;
767 tempBuffer
.offset
= ex
->buf_ofs
;
768 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
769 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
770 /* Have more headers .. */
771 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
772 netdbExchangeHandleReply
, ex
);
777 assert(ex
->connstate
== STATE_BODY
);
779 /* If we get here, we have some body to parse .. */
780 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
782 while (size
>= rec_sz
) {
783 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
787 for (o
= 0; o
< rec_sz
;) {
788 switch ((int) *(p
+ o
)) {
790 case NETDB_EX_NETWORK
:
792 /* FIXME INET6 : NetDB can still ony send IPv4 */
793 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
795 o
+= sizeof(struct in_addr
);
800 memcpy(&j
, p
+ o
, sizeof(int));
802 rtt
= (double) ntohl(j
) / 1000.0;
807 memcpy(&j
, p
+ o
, sizeof(int));
809 hops
= (double) ntohl(j
) / 1000.0;
813 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
814 netdbExchangeDone(ex
);
819 if (!addr
.IsAnyAddr() && rtt
> 0)
820 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
834 * Copy anything that is left over to the beginning of the buffer,
835 * and adjust buf_ofs accordingly
839 * Evilly, size refers to the buf size left now,
840 * ex->buf_ofs is the original buffer size, so just copy that
843 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
848 * And don't re-copy the remaining data ..
853 * Now the tricky bit - size _included_ the leftover bit from the _last_
854 * storeClientCopy. We don't want to include that, or our offset will be wrong.
855 * So, don't count the size of the leftover buffer we began with.
856 * This can _disappear_ when we're not tracking offsets ..
858 ex
->used
-= oldbufofs
;
860 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
862 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
863 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
866 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
868 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
869 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
870 netdbExchangeDone(ex
);
871 } else if (ex
->e
->store_status
== STORE_PENDING
) {
872 StoreIOBuffer tempBuffer
;
873 tempBuffer
.offset
= ex
->used
;
874 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
875 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
876 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
877 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
878 netdbExchangeHandleReply
, ex
);
883 netdbExchangeDone(void *data
)
885 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
886 debugs(38, 3, "netdbExchangeDone: " << ex
->e
->url() );
887 HTTPMSGUNLOCK(ex
->r
);
888 storeUnregister(ex
->sc
, ex
->e
, ex
);
890 cbdataReferenceDone(ex
->p
);
895 netdbRegisterWithCacheManager(void)
897 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
900 #endif /* USE_ICMP */
902 /* PUBLIC FUNCTIONS */
910 netdbRegisterWithCacheManager();
915 n
= hashPrime(Config
.Netdb
.high
/ 4);
917 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
919 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
921 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
923 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
931 netdbPingSite(const char *hostname
)
936 if ((n
= netdbLookupHost(hostname
)) != NULL
)
937 if (n
->next_ping_time
> squid_curtime
)
940 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
941 new generic_cbdata(xstrdup(hostname
)));
947 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
952 debugs(38, 3, "netdbHandlePingReply: from " << from
);
954 if ((n
= netdbLookupAddr(from
)) == NULL
)
965 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
967 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
969 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
970 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
971 std::setw(4) << n
->hops
);
977 netdbFreeMemory(void)
980 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
981 hashFreeMemory(addr_table
);
983 hashFreeItems(host_table
, netdbFreeNameEntry
);
984 hashFreeMemory(host_table
);
986 wordlistDestroy(&peer_names
);
991 #if 0 // AYJ: Looks to be unused code.
993 netdbHops(Ip::Address
&addr
)
996 netdbEntry
*n
= netdbLookupAddr(addr
);
998 if (n
&& n
->pings_recv
) {
999 n
->last_use_time
= squid_curtime
;
1000 return (int) (n
->hops
+ 0.5);
1009 netdbDump(StoreEntry
* sentry
)
1019 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
1020 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1026 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1028 hash_first(addr_table
);
1030 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1035 if (i
!= memInUse(MEM_NETDBENTRY
))
1036 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
1037 ", expected " << memInUse(MEM_NETDBENTRY
));
1039 qsort((char *) list
,
1041 sizeof(netdbEntry
*),
1044 for (k
= 0; k
< i
; ++k
) {
1046 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1053 for (x
= n
->hosts
; x
; x
= x
->next
)
1054 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1056 storeAppendPrintf(sentry
, "\n");
1060 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1061 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1071 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1076 netdbHostHops(const char *host
)
1079 netdbEntry
*n
= netdbLookupHost(host
);
1082 n
->last_use_time
= squid_curtime
;
1083 return (int) (n
->hops
+ 0.5);
1091 netdbHostRtt(const char *host
)
1094 netdbEntry
*n
= netdbLookupHost(host
);
1097 n
->last_use_time
= squid_curtime
;
1098 return (int) (n
->rtt
+ 0.5);
1106 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1109 netdbEntry
*n
= netdbLookupHost(host
);
1114 *samp
= n
->pings_recv
;
1116 *rtt
= (int) (n
->rtt
+ 0.5);
1118 *hops
= (int) (n
->hops
+ 0.5);
1120 n
->last_use_time
= squid_curtime
;
1126 netdbUpdatePeer(HttpRequest
* r
, peer
* e
, int irtt
, int ihops
)
1130 double rtt
= (double) irtt
;
1131 double hops
= (double) ihops
;
1133 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1134 n
= netdbLookupHost(r
->GetHost());
1137 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1141 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1142 p
= netdbPeerAdd(n
, e
);
1148 p
->expires
= squid_curtime
+ 3600;
1153 qsort((char *) n
->peers
,
1155 sizeof(net_db_peer
),
1162 netdbExchangeUpdatePeer(Ip::Address
&addr
, peer
* e
, double rtt
, double hops
)
1167 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1168 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1171 if ( !addr
.IsIPv4() ) {
1172 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1176 n
= netdbLookupAddr(addr
);
1183 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1184 p
= netdbPeerAdd(n
, e
);
1190 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1195 qsort((char *) n
->peers
,
1197 sizeof(net_db_peer
),
1204 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1207 netdbEntry
*n
= netdbLookupAddr(addr
);
1212 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1219 netdbBinaryExchange(StoreEntry
* s
)
1221 HttpReply
*reply
= new HttpReply
;
1232 struct in_addr line_addr
;
1234 reply
->setHeaders(HTTP_OK
, "OK", NULL
, -1, squid_curtime
, -2);
1235 s
->replaceHttpReply(reply
);
1237 rec_sz
+= 1 + sizeof(struct in_addr
);
1238 rec_sz
+= 1 + sizeof(int);
1239 rec_sz
+= 1 + sizeof(int);
1240 buf
= (char *)memAllocate(MEM_4K_BUF
);
1242 hash_first(addr_table
);
1244 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1248 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1251 if (! (addr
= n
->network
) )
1254 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1255 if ( !addr
.IsIPv4() )
1258 buf
[i
] = (char) NETDB_EX_NETWORK
;
1261 addr
.GetInAddr(line_addr
);
1262 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1264 i
+= sizeof(struct in_addr
);
1266 buf
[i
] = (char) NETDB_EX_RTT
;
1269 j
= htonl((int) (n
->rtt
* 1000));
1271 memcpy(&buf
[i
], &j
, sizeof(int));
1275 buf
[i
] = (char) NETDB_EX_HOPS
;
1278 j
= htonl((int) (n
->hops
* 1000));
1280 memcpy(&buf
[i
], &j
, sizeof(int));
1284 if (i
+ rec_sz
> 4096) {
1297 memFree(buf
, MEM_4K_BUF
);
1300 reply
->setHeaders(HTTP_BAD_REQUEST
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1301 s
->replaceHttpReply(reply
);
1302 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1309 CBDATA_TYPE(netdbExchangeState
);
1313 netdbExchangeStart(void *data
)
1316 peer
*p
= (peer
*)data
;
1318 netdbExchangeState
*ex
;
1319 StoreIOBuffer tempBuffer
;
1320 CBDATA_INIT_TYPE(netdbExchangeState
);
1321 ex
= cbdataAlloc(netdbExchangeState
);
1322 ex
->p
= cbdataReference(p
);
1323 uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", "netdb");
1324 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1325 assert(NULL
!= uri
);
1326 ex
->r
= HttpRequest::CreateFromUrl(uri
);
1328 if (NULL
== ex
->r
) {
1329 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1334 assert(NULL
!= ex
->r
);
1335 ex
->r
->http_ver
= HttpVersion(1,1);
1336 ex
->connstate
= STATE_HEADER
;
1337 ex
->e
= storeCreateEntry(uri
, uri
, request_flags(), METHOD_GET
);
1338 ex
->buf_sz
= NETDB_REQBUF_SZ
;
1339 assert(NULL
!= ex
->e
);
1340 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1341 tempBuffer
.offset
= 0;
1342 tempBuffer
.length
= ex
->buf_sz
;
1343 tempBuffer
.data
= ex
->buf
;
1344 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1345 netdbExchangeHandleReply
, ex
);
1346 ex
->r
->flags
.loopdetect
= 1; /* cheat! -- force direct */
1349 xstrncpy(ex
->r
->login
, p
->login
, MAX_LOGIN_SZ
);
1351 urlCanonical(ex
->r
);
1353 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1359 netdbClosestParent(HttpRequest
* request
)
1364 const ipcache_addrs
*ia
;
1367 n
= netdbLookupHost(request
->GetHost());
1371 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1374 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1380 if (0 == n
->n_peers
)
1383 n
->last_use_time
= squid_curtime
;
1386 * Find the parent with the least RTT to the origin server.
1387 * Make sure we don't return a parent who is farther away than
1388 * we are. Note, the n->peers list is pre-sorted by RTT.
1390 for (i
= 0; i
< n
->n_peers
; ++i
) {
1394 if (n
->rtt
< h
->rtt
)
1397 p
= peerFindByName(h
->peername
);
1399 if (NULL
== p
) /* not found */
1402 if (neighborType(p
, request
) != PEER_PARENT
)
1405 if (!peerHTTPOkay(p
, request
)) /* not allowed */