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.
42 #include "CachePeer.h"
48 #include "HttpReply.h"
49 #include "HttpRequest.h"
50 #include "icmp/net_db.h"
52 #include "ip/Address.h"
55 #include "MemObject.h"
56 #include "mgr/Registration.h"
57 #include "mime_header.h"
58 #include "neighbors.h"
59 #include "SquidTime.h"
61 #include "StoreClient.h"
72 #include "icmp/IcmpSquid.h"
74 #include "StoreClient.h"
76 #define NETDB_REQBUF_SZ 4096
91 char buf
[NETDB_REQBUF_SZ
];
93 netdb_conn_state_t connstate
;
96 static hash_table
*addr_table
= NULL
;
97 static hash_table
*host_table
= NULL
;
99 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
100 static void netdbRelease(netdbEntry
* n
);
102 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
103 static void netdbHashDelete(const char *key
);
104 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
105 static void netdbHostDelete(const net_db_name
* x
);
106 static void netdbPurgeLRU(void);
107 static netdbEntry
*netdbLookupHost(const char *key
);
108 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
109 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
);
110 static const char *netdbPeerName(const char *name
);
111 static IPH netdbSendPing
;
112 static QS sortPeerByRtt
;
115 static FREE netdbFreeNameEntry
;
116 static FREE netdbFreeNetdbEntry
;
117 static STCB netdbExchangeHandleReply
;
118 static void netdbExchangeDone(void *);
120 /* We have to keep a local list of CachePeer names. The Peers structure
121 * gets freed during a reconfigure. We want this database to
122 * remain persisitent, so _net_db_peer->peername points into this
124 static wordlist
*peer_names
= NULL
;
127 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
129 networkFromInaddr(addr
).NtoA(n
->network
, MAX_IPSTRLEN
);
130 n
->hash
.key
= n
->network
;
131 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
132 hash_join(addr_table
, &n
->hash
);
136 netdbHashDelete(const char *key
)
138 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
141 debug_trap("netdbHashDelete: key not found");
145 hash_remove_link(addr_table
, hptr
);
149 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
151 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
152 x
->hash
.key
= xstrdup(hostname
);
156 assert(hash_lookup(host_table
, hostname
) == NULL
);
157 hash_join(host_table
, &x
->hash
);
162 netdbHostDelete(const net_db_name
* x
)
167 assert(x
->net_db_entry
!= NULL
);
171 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
178 hash_remove_link(host_table
, (hash_link
*) x
);
180 memFree((void *) x
, MEM_NET_DB_NAME
);
184 netdbLookupHost(const char *key
)
186 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
187 return x
? x
->net_db_entry
: NULL
;
191 netdbRelease(netdbEntry
* n
)
196 for (x
= n
->hosts
; x
; x
= next
) {
205 n
->n_peers_alloc
= 0;
207 if (n
->link_count
== 0) {
208 netdbHashDelete(n
->network
);
209 memFree(n
, MEM_NETDBENTRY
);
214 netdbLRU(const void *A
, const void *B
)
216 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
217 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
219 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
222 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
236 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
237 hash_first(addr_table
);
239 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
240 assert(list_count
< memInUse(MEM_NETDBENTRY
));
241 *(list
+ list_count
) = n
;
247 sizeof(netdbEntry
*),
250 for (k
= 0; k
< list_count
; ++k
) {
251 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
254 netdbRelease(*(list
+ k
));
263 netdbLookupAddr(const Ip::Address
&addr
)
266 char *key
= new char[MAX_IPSTRLEN
];
267 networkFromInaddr(addr
).NtoA(key
,MAX_IPSTRLEN
);
268 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
274 netdbAdd(Ip::Address
&addr
)
278 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
281 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
282 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
283 netdbHashInsert(n
, addr
);
290 netdbSendPing(const ipcache_addrs
*ia
, const DnsLookupDetails
&, void *data
)
293 char *hostname
= NULL
;
294 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
305 addr
= ia
->in_addrs
[ia
->cur
];
307 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
309 netdbHostInsert(n
, hostname
);
310 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
312 *hostname moved from 'network n' to 'network na'!
318 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
320 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
323 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
328 /* remove net_db_name from 'network n' linked list */
329 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
337 /* point to 'network na' from host entry */
338 x
->net_db_entry
= na
;
339 /* link net_db_name to 'network na' */
346 if (n
->next_ping_time
<= squid_curtime
) {
347 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
348 icmpEngine
.DomainPing(addr
, hostname
);
350 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
351 n
->last_use_time
= squid_curtime
;
358 networkFromInaddr(const Ip::Address
&in
)
364 /* in IPv6 the 'network' should be the routing section. */
366 out
.ApplyMask(64, AF_INET6
);
367 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
376 if (IN_CLASSC(b
.s_addr
))
377 b
.s_addr
&= IN_CLASSC_NET
;
378 else if (IN_CLASSB(b
.s_addr
))
379 b
.s_addr
&= IN_CLASSB_NET
;
380 else if (IN_CLASSA(b
.s_addr
))
381 b
.s_addr
&= IN_CLASSA_NET
;
387 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
389 /* use /24 for everything under IPv4 */
390 out
.ApplyMask(24, AF_INET
);
391 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
397 sortByRtt(const void *A
, const void *B
)
399 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
400 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
402 if ((*n1
)->rtt
> (*n2
)->rtt
)
404 else if ((*n1
)->rtt
< (*n2
)->rtt
)
411 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
414 net_db_peer
*p
= n
->peers
;
416 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
417 if (!strcmp(p
->peername
, peername
))
425 netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
)
432 if (n
->n_peers
== n
->n_peers_alloc
) {
434 osize
= n
->n_peers_alloc
;
436 if (n
->n_peers_alloc
== 0)
437 n
->n_peers_alloc
= 2;
439 n
->n_peers_alloc
<<= 1;
441 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
443 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
445 for (i
= 0; i
< osize
; ++i
)
446 *(n
->peers
+ i
) = *(o
+ i
);
453 p
= n
->peers
+ n
->n_peers
;
454 p
->peername
= netdbPeerName(e
->host
);
460 sortPeerByRtt(const void *A
, const void *B
)
462 const net_db_peer
*p1
= (net_db_peer
*)A
;
463 const net_db_peer
*p2
= (net_db_peer
*)B
;
465 if (p1
->rtt
> p2
->rtt
)
467 else if (p1
->rtt
< p2
->rtt
)
474 netdbSaveState(void *foo
)
476 if (strcmp(Config
.netdbFilename
, "none") == 0)
483 struct timeval start
= current_time
;
486 * This was nicer when we were using stdio, but thanks to
487 * Solaris bugs, its a bad idea. fopen can fail if more than
491 * unlink() is here because there is currently no way to make
492 * logfileOpen() use O_TRUNC.
494 unlink(Config
.netdbFilename
);
495 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
498 debugs(50, DBG_IMPORTANT
, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
502 hash_first(addr_table
);
504 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
505 if (n
->pings_recv
== 0)
508 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
514 (int) n
->next_ping_time
,
515 (int) n
->last_use_time
);
517 for (x
= n
->hosts
; x
; x
= x
->next
)
518 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
520 logfilePrintf(lf
, "\n");
530 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
531 count
<< " entries, " <<
532 tvSubMsec(start
, current_time
) << " msec" );
533 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
537 netdbReloadState(void)
539 if (strcmp(Config
.netdbFilename
, "none") == 0)
553 struct timeval start
= current_time
;
555 * This was nicer when we were using stdio, but thanks to
556 * Solaris bugs, its a bad idea. fopen can fail if more than
559 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
564 if (fstat(fd
, &sb
) < 0) {
570 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
572 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
580 while ((s
= strchr(t
, '\n'))) {
584 memset(&N
, '\0', sizeof(netdbEntry
));
585 q
= strtok(t
, w_space
);
594 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
597 if ((q
= strtok(NULL
, w_space
)) == NULL
)
600 N
.pings_sent
= atoi(q
);
602 if ((q
= strtok(NULL
, w_space
)) == NULL
)
605 N
.pings_recv
= atoi(q
);
607 if (N
.pings_recv
== 0)
610 /* give this measurement low weight */
615 if ((q
= strtok(NULL
, w_space
)) == NULL
)
620 if ((q
= strtok(NULL
, w_space
)) == NULL
)
625 if ((q
= strtok(NULL
, w_space
)) == NULL
)
628 N
.next_ping_time
= (time_t) atoi(q
);
630 if ((q
= strtok(NULL
, w_space
)) == NULL
)
633 N
.last_use_time
= (time_t) atoi(q
);
635 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
637 memcpy(n
, &N
, sizeof(netdbEntry
));
639 netdbHashInsert(n
, addr
);
641 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
642 if (netdbLookupHost(q
) != NULL
) /* no dups! */
645 netdbHostInsert(n
, q
);
653 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
654 count
<< " entries, " <<
655 tvSubMsec(start
, current_time
) << " msec" );
659 netdbPeerName(const char *name
)
663 for (w
= peer_names
; w
; w
= w
->next
) {
664 if (!strcmp(w
->key
, name
))
668 return wordlistAdd(&peer_names
, name
);
672 netdbFreeNetdbEntry(void *data
)
674 netdbEntry
*n
= (netdbEntry
*)data
;
676 memFree(n
, MEM_NETDBENTRY
);
680 netdbFreeNameEntry(void *data
)
682 net_db_name
*x
= (net_db_name
*)data
;
684 memFree(x
, MEM_NET_DB_NAME
);
688 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
692 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
696 struct in_addr line_addr
;
701 HttpReply
const *rep
;
705 int oldbufofs
= ex
->buf_ofs
;
708 rec_sz
+= 1 + sizeof(struct in_addr
);
709 rec_sz
+= 1 + sizeof(int);
710 rec_sz
+= 1 + sizeof(int);
711 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
713 if (!cbdataReferenceValid(ex
->p
)) {
714 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
715 netdbExchangeDone(ex
);
719 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
721 if (receivedData
.length
== 0 &&
722 !receivedData
.flags
.error
) {
723 debugs(38, 3, "netdbExchangeHandleReply: Done");
724 netdbExchangeDone(ex
);
730 /* Get the size of the buffer now */
731 size
= ex
->buf_ofs
+ receivedData
.length
;
732 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
734 /* Check if we're still doing headers */
736 if (ex
->connstate
== STATE_HEADER
) {
738 ex
->buf_ofs
+= receivedData
.length
;
740 /* skip reply headers */
742 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
743 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
744 rep
= ex
->e
->getReply();
745 assert (0 != rep
->sline
.status
);
746 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status
);
748 if (HTTP_OK
!= rep
->sline
.status
) {
749 netdbExchangeDone(ex
);
753 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
756 * Now, point p to the part of the buffer where the data
757 * starts, and update the size accordingly
759 assert(ex
->used
== 0);
761 size
= ex
->buf_ofs
- hdr_sz
;
764 /* Finally, set the conn state mode to STATE_BODY */
765 ex
->connstate
= STATE_BODY
;
767 StoreIOBuffer tempBuffer
;
768 tempBuffer
.offset
= ex
->buf_ofs
;
769 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
770 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
771 /* Have more headers .. */
772 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
773 netdbExchangeHandleReply
, ex
);
778 assert(ex
->connstate
== STATE_BODY
);
780 /* If we get here, we have some body to parse .. */
781 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
783 while (size
>= rec_sz
) {
784 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
788 for (o
= 0; o
< rec_sz
;) {
789 switch ((int) *(p
+ o
)) {
791 case NETDB_EX_NETWORK
:
793 /* FIXME INET6 : NetDB can still ony send IPv4 */
794 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
796 o
+= sizeof(struct in_addr
);
801 memcpy(&j
, p
+ o
, sizeof(int));
803 rtt
= (double) ntohl(j
) / 1000.0;
808 memcpy(&j
, p
+ o
, sizeof(int));
810 hops
= (double) ntohl(j
) / 1000.0;
814 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
815 netdbExchangeDone(ex
);
820 if (!addr
.IsAnyAddr() && rtt
> 0)
821 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
835 * Copy anything that is left over to the beginning of the buffer,
836 * and adjust buf_ofs accordingly
840 * Evilly, size refers to the buf size left now,
841 * ex->buf_ofs is the original buffer size, so just copy that
844 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
849 * And don't re-copy the remaining data ..
854 * Now the tricky bit - size _included_ the leftover bit from the _last_
855 * storeClientCopy. We don't want to include that, or our offset will be wrong.
856 * So, don't count the size of the leftover buffer we began with.
857 * This can _disappear_ when we're not tracking offsets ..
859 ex
->used
-= oldbufofs
;
861 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
863 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
864 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
867 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
869 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
870 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
871 netdbExchangeDone(ex
);
872 } else if (ex
->e
->store_status
== STORE_PENDING
) {
873 StoreIOBuffer tempBuffer
;
874 tempBuffer
.offset
= ex
->used
;
875 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
876 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
877 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
878 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
879 netdbExchangeHandleReply
, ex
);
884 netdbExchangeDone(void *data
)
886 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
887 debugs(38, 3, "netdbExchangeDone: " << ex
->e
->url() );
888 HTTPMSGUNLOCK(ex
->r
);
889 storeUnregister(ex
->sc
, ex
->e
, ex
);
891 cbdataReferenceDone(ex
->p
);
896 netdbRegisterWithCacheManager(void)
898 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
901 #endif /* USE_ICMP */
903 /* PUBLIC FUNCTIONS */
911 netdbRegisterWithCacheManager();
916 n
= hashPrime(Config
.Netdb
.high
/ 4);
918 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
920 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
922 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
924 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
932 netdbPingSite(const char *hostname
)
937 if ((n
= netdbLookupHost(hostname
)) != NULL
)
938 if (n
->next_ping_time
> squid_curtime
)
941 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
942 new generic_cbdata(xstrdup(hostname
)));
948 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
953 debugs(38, 3, "netdbHandlePingReply: from " << from
);
955 if ((n
= netdbLookupAddr(from
)) == NULL
)
966 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
968 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
970 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
971 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
972 std::setw(4) << n
->hops
);
978 netdbFreeMemory(void)
981 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
982 hashFreeMemory(addr_table
);
984 hashFreeItems(host_table
, netdbFreeNameEntry
);
985 hashFreeMemory(host_table
);
987 wordlistDestroy(&peer_names
);
992 #if 0 // AYJ: Looks to be unused code.
994 netdbHops(Ip::Address
&addr
)
997 netdbEntry
*n
= netdbLookupAddr(addr
);
999 if (n
&& n
->pings_recv
) {
1000 n
->last_use_time
= squid_curtime
;
1001 return (int) (n
->hops
+ 0.5);
1010 netdbDump(StoreEntry
* sentry
)
1020 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
1021 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1027 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1029 hash_first(addr_table
);
1031 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1036 if (i
!= memInUse(MEM_NETDBENTRY
))
1037 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
1038 ", expected " << memInUse(MEM_NETDBENTRY
));
1040 qsort((char *) list
,
1042 sizeof(netdbEntry
*),
1045 for (k
= 0; k
< i
; ++k
) {
1047 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1054 for (x
= n
->hosts
; x
; x
= x
->next
)
1055 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1057 storeAppendPrintf(sentry
, "\n");
1061 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1062 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1072 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1077 netdbHostHops(const char *host
)
1080 netdbEntry
*n
= netdbLookupHost(host
);
1083 n
->last_use_time
= squid_curtime
;
1084 return (int) (n
->hops
+ 0.5);
1092 netdbHostRtt(const char *host
)
1095 netdbEntry
*n
= netdbLookupHost(host
);
1098 n
->last_use_time
= squid_curtime
;
1099 return (int) (n
->rtt
+ 0.5);
1107 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1110 netdbEntry
*n
= netdbLookupHost(host
);
1115 *samp
= n
->pings_recv
;
1117 *rtt
= (int) (n
->rtt
+ 0.5);
1119 *hops
= (int) (n
->hops
+ 0.5);
1121 n
->last_use_time
= squid_curtime
;
1127 netdbUpdatePeer(HttpRequest
* r
, CachePeer
* e
, int irtt
, int ihops
)
1131 double rtt
= (double) irtt
;
1132 double hops
= (double) ihops
;
1134 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1135 n
= netdbLookupHost(r
->GetHost());
1138 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1142 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1143 p
= netdbPeerAdd(n
, e
);
1149 p
->expires
= squid_curtime
+ 3600;
1154 qsort((char *) n
->peers
,
1156 sizeof(net_db_peer
),
1163 netdbExchangeUpdatePeer(Ip::Address
&addr
, CachePeer
* e
, double rtt
, double hops
)
1168 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1169 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1172 if ( !addr
.IsIPv4() ) {
1173 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1177 n
= netdbLookupAddr(addr
);
1184 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1185 p
= netdbPeerAdd(n
, e
);
1191 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1196 qsort((char *) n
->peers
,
1198 sizeof(net_db_peer
),
1205 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1208 netdbEntry
*n
= netdbLookupAddr(addr
);
1213 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1220 netdbBinaryExchange(StoreEntry
* s
)
1222 HttpReply
*reply
= new HttpReply
;
1233 struct in_addr line_addr
;
1235 reply
->setHeaders(HTTP_OK
, "OK", NULL
, -1, squid_curtime
, -2);
1236 s
->replaceHttpReply(reply
);
1238 rec_sz
+= 1 + sizeof(struct in_addr
);
1239 rec_sz
+= 1 + sizeof(int);
1240 rec_sz
+= 1 + sizeof(int);
1241 buf
= (char *)memAllocate(MEM_4K_BUF
);
1243 hash_first(addr_table
);
1245 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1249 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1252 if (! (addr
= n
->network
) )
1255 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1256 if ( !addr
.IsIPv4() )
1259 buf
[i
] = (char) NETDB_EX_NETWORK
;
1262 addr
.GetInAddr(line_addr
);
1263 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1265 i
+= sizeof(struct in_addr
);
1267 buf
[i
] = (char) NETDB_EX_RTT
;
1270 j
= htonl((int) (n
->rtt
* 1000));
1272 memcpy(&buf
[i
], &j
, sizeof(int));
1276 buf
[i
] = (char) NETDB_EX_HOPS
;
1279 j
= htonl((int) (n
->hops
* 1000));
1281 memcpy(&buf
[i
], &j
, sizeof(int));
1285 if (i
+ rec_sz
> 4096) {
1298 memFree(buf
, MEM_4K_BUF
);
1301 reply
->setHeaders(HTTP_BAD_REQUEST
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1302 s
->replaceHttpReply(reply
);
1303 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1310 CBDATA_TYPE(netdbExchangeState
);
1314 netdbExchangeStart(void *data
)
1317 CachePeer
*p
= (CachePeer
*)data
;
1319 netdbExchangeState
*ex
;
1320 StoreIOBuffer tempBuffer
;
1321 CBDATA_INIT_TYPE(netdbExchangeState
);
1322 ex
= cbdataAlloc(netdbExchangeState
);
1323 ex
->p
= cbdataReference(p
);
1324 uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", "netdb");
1325 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1326 assert(NULL
!= uri
);
1327 ex
->r
= HttpRequest::CreateFromUrl(uri
);
1329 if (NULL
== ex
->r
) {
1330 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1335 assert(NULL
!= ex
->r
);
1336 ex
->r
->http_ver
= HttpVersion(1,1);
1337 ex
->connstate
= STATE_HEADER
;
1338 ex
->e
= storeCreateEntry(uri
, uri
, RequestFlags(), METHOD_GET
);
1339 ex
->buf_sz
= NETDB_REQBUF_SZ
;
1340 assert(NULL
!= ex
->e
);
1341 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1342 tempBuffer
.offset
= 0;
1343 tempBuffer
.length
= ex
->buf_sz
;
1344 tempBuffer
.data
= ex
->buf
;
1345 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1346 netdbExchangeHandleReply
, ex
);
1347 ex
->r
->flags
.loopDetected
= 1; /* cheat! -- force direct */
1350 xstrncpy(ex
->r
->login
, p
->login
, MAX_LOGIN_SZ
);
1352 urlCanonical(ex
->r
);
1354 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1360 netdbClosestParent(HttpRequest
* request
)
1363 CachePeer
*p
= NULL
;
1365 const ipcache_addrs
*ia
;
1368 n
= netdbLookupHost(request
->GetHost());
1372 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1375 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1381 if (0 == n
->n_peers
)
1384 n
->last_use_time
= squid_curtime
;
1387 * Find the parent with the least RTT to the origin server.
1388 * Make sure we don't return a parent who is farther away than
1389 * we are. Note, the n->peers list is pre-sorted by RTT.
1391 for (i
= 0; i
< n
->n_peers
; ++i
) {
1395 if (n
->rtt
< h
->rtt
)
1398 p
= peerFindByName(h
->peername
);
1400 if (NULL
== p
) /* not found */
1403 if (neighborType(p
, request
) != PEER_PARENT
)
1406 if (!peerHTTPOkay(p
, request
)) /* not allowed */