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 "icmp/net_db.h"
46 #include "mgr/Registration.h"
49 #include "HttpRequest.h"
50 #include "HttpReply.h"
51 #include "MemObject.h"
55 #include "SquidTime.h"
57 #include "ip/Address.h"
64 #include "icmp/IcmpSquid.h"
66 #include "StoreClient.h"
68 #define NETDB_REQBUF_SZ 4096
83 char buf
[NETDB_REQBUF_SZ
];
85 netdb_conn_state_t connstate
;
88 static hash_table
*addr_table
= NULL
;
89 static hash_table
*host_table
= NULL
;
91 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
92 static void netdbRelease(netdbEntry
* n
);
94 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
95 static void netdbHashDelete(const char *key
);
96 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
97 static void netdbHostDelete(const net_db_name
* x
);
98 static void netdbPurgeLRU(void);
99 static netdbEntry
*netdbLookupHost(const char *key
);
100 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
101 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, peer
* e
);
102 static const char *netdbPeerName(const char *name
);
103 static IPH netdbSendPing
;
104 static QS sortPeerByRtt
;
107 static FREE netdbFreeNameEntry
;
108 static FREE netdbFreeNetdbEntry
;
109 static STCB netdbExchangeHandleReply
;
110 static void netdbExchangeDone(void *);
112 /* We have to keep a local list of peer names. The Peers structure
113 * gets freed during a reconfigure. We want this database to
114 * remain persisitent, so _net_db_peer->peername points into this
116 static wordlist
*peer_names
= NULL
;
119 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
121 networkFromInaddr(addr
).NtoA(n
->network
, MAX_IPSTRLEN
);
122 n
->hash
.key
= n
->network
;
123 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
124 hash_join(addr_table
, &n
->hash
);
128 netdbHashDelete(const char *key
)
130 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
133 debug_trap("netdbHashDelete: key not found");
137 hash_remove_link(addr_table
, hptr
);
141 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
143 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
144 x
->hash
.key
= xstrdup(hostname
);
148 assert(hash_lookup(host_table
, hostname
) == NULL
);
149 hash_join(host_table
, &x
->hash
);
154 netdbHostDelete(const net_db_name
* x
)
159 assert(x
->net_db_entry
!= NULL
);
163 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
170 hash_remove_link(host_table
, (hash_link
*) x
);
172 memFree((void *) x
, MEM_NET_DB_NAME
);
176 netdbLookupHost(const char *key
)
178 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
179 return x
? x
->net_db_entry
: NULL
;
183 netdbRelease(netdbEntry
* n
)
188 for (x
= n
->hosts
; x
; x
= next
) {
197 n
->n_peers_alloc
= 0;
199 if (n
->link_count
== 0) {
200 netdbHashDelete(n
->network
);
201 memFree(n
, MEM_NETDBENTRY
);
206 netdbLRU(const void *A
, const void *B
)
208 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
209 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
211 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
214 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
228 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
229 hash_first(addr_table
);
231 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
232 assert(list_count
< memInUse(MEM_NETDBENTRY
));
233 *(list
+ list_count
) = n
;
239 sizeof(netdbEntry
*),
242 for (k
= 0; k
< list_count
; ++k
) {
243 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
246 netdbRelease(*(list
+ k
));
255 netdbLookupAddr(const Ip::Address
&addr
)
258 char *key
= new char[MAX_IPSTRLEN
];
259 networkFromInaddr(addr
).NtoA(key
,MAX_IPSTRLEN
);
260 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
266 netdbAdd(Ip::Address
&addr
)
270 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
273 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
274 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
275 netdbHashInsert(n
, addr
);
282 netdbSendPing(const ipcache_addrs
*ia
, const DnsLookupDetails
&, void *data
)
285 char *hostname
= NULL
;
286 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
297 addr
= ia
->in_addrs
[ia
->cur
];
299 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
301 netdbHostInsert(n
, hostname
);
302 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
304 *hostname moved from 'network n' to 'network na'!
310 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
312 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
315 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
320 /* remove net_db_name from 'network n' linked list */
321 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
329 /* point to 'network na' from host entry */
330 x
->net_db_entry
= na
;
331 /* link net_db_name to 'network na' */
338 if (n
->next_ping_time
<= squid_curtime
) {
339 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
340 icmpEngine
.DomainPing(addr
, hostname
);
342 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
343 n
->last_use_time
= squid_curtime
;
350 networkFromInaddr(const Ip::Address
&in
)
356 /* in IPv6 the 'network' should be the routing section. */
358 out
.ApplyMask(64, AF_INET6
);
359 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
368 if (IN_CLASSC(b
.s_addr
))
369 b
.s_addr
&= IN_CLASSC_NET
;
370 else if (IN_CLASSB(b
.s_addr
))
371 b
.s_addr
&= IN_CLASSB_NET
;
372 else if (IN_CLASSA(b
.s_addr
))
373 b
.s_addr
&= IN_CLASSA_NET
;
379 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
381 /* use /24 for everything under IPv4 */
382 out
.ApplyMask(24, AF_INET
);
383 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
389 sortByRtt(const void *A
, const void *B
)
391 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
392 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
394 if ((*n1
)->rtt
> (*n2
)->rtt
)
396 else if ((*n1
)->rtt
< (*n2
)->rtt
)
403 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
406 net_db_peer
*p
= n
->peers
;
408 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
409 if (!strcmp(p
->peername
, peername
))
417 netdbPeerAdd(netdbEntry
* n
, peer
* e
)
424 if (n
->n_peers
== n
->n_peers_alloc
) {
426 osize
= n
->n_peers_alloc
;
428 if (n
->n_peers_alloc
== 0)
429 n
->n_peers_alloc
= 2;
431 n
->n_peers_alloc
<<= 1;
433 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
435 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
437 for (i
= 0; i
< osize
; ++i
)
438 *(n
->peers
+ i
) = *(o
+ i
);
445 p
= n
->peers
+ n
->n_peers
;
446 p
->peername
= netdbPeerName(e
->host
);
452 sortPeerByRtt(const void *A
, const void *B
)
454 const net_db_peer
*p1
= (net_db_peer
*)A
;
455 const net_db_peer
*p2
= (net_db_peer
*)B
;
457 if (p1
->rtt
> p2
->rtt
)
459 else if (p1
->rtt
< p2
->rtt
)
466 netdbSaveState(void *foo
)
468 if (strcmp(Config
.netdbFilename
, "none") == 0)
475 struct timeval start
= current_time
;
478 * This was nicer when we were using stdio, but thanks to
479 * Solaris bugs, its a bad idea. fopen can fail if more than
483 * unlink() is here because there is currently no way to make
484 * logfileOpen() use O_TRUNC.
486 unlink(Config
.netdbFilename
);
487 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
490 debugs(50, DBG_IMPORTANT
, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
494 hash_first(addr_table
);
496 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
497 if (n
->pings_recv
== 0)
500 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
506 (int) n
->next_ping_time
,
507 (int) n
->last_use_time
);
509 for (x
= n
->hosts
; x
; x
= x
->next
)
510 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
512 logfilePrintf(lf
, "\n");
522 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
523 count
<< " entries, " <<
524 tvSubMsec(start
, current_time
) << " msec" );
525 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
529 netdbReloadState(void)
531 if (strcmp(Config
.netdbFilename
, "none") == 0)
545 struct timeval start
= current_time
;
547 * This was nicer when we were using stdio, but thanks to
548 * Solaris bugs, its a bad idea. fopen can fail if more than
551 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
556 if (fstat(fd
, &sb
) < 0) {
562 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
564 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
572 while ((s
= strchr(t
, '\n'))) {
576 memset(&N
, '\0', sizeof(netdbEntry
));
577 q
= strtok(t
, w_space
);
586 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
589 if ((q
= strtok(NULL
, w_space
)) == NULL
)
592 N
.pings_sent
= atoi(q
);
594 if ((q
= strtok(NULL
, w_space
)) == NULL
)
597 N
.pings_recv
= atoi(q
);
599 if (N
.pings_recv
== 0)
602 /* give this measurement low weight */
607 if ((q
= strtok(NULL
, w_space
)) == NULL
)
612 if ((q
= strtok(NULL
, w_space
)) == NULL
)
617 if ((q
= strtok(NULL
, w_space
)) == NULL
)
620 N
.next_ping_time
= (time_t) atoi(q
);
622 if ((q
= strtok(NULL
, w_space
)) == NULL
)
625 N
.last_use_time
= (time_t) atoi(q
);
627 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
629 memcpy(n
, &N
, sizeof(netdbEntry
));
631 netdbHashInsert(n
, addr
);
633 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
634 if (netdbLookupHost(q
) != NULL
) /* no dups! */
637 netdbHostInsert(n
, q
);
645 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
646 count
<< " entries, " <<
647 tvSubMsec(start
, current_time
) << " msec" );
651 netdbPeerName(const char *name
)
655 for (w
= peer_names
; w
; w
= w
->next
) {
656 if (!strcmp(w
->key
, name
))
660 return wordlistAdd(&peer_names
, name
);
664 netdbFreeNetdbEntry(void *data
)
666 netdbEntry
*n
= (netdbEntry
*)data
;
668 memFree(n
, MEM_NETDBENTRY
);
672 netdbFreeNameEntry(void *data
)
674 net_db_name
*x
= (net_db_name
*)data
;
676 memFree(x
, MEM_NET_DB_NAME
);
680 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
684 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
688 struct in_addr line_addr
;
693 HttpReply
const *rep
;
697 int oldbufofs
= ex
->buf_ofs
;
700 rec_sz
+= 1 + sizeof(struct in_addr
);
701 rec_sz
+= 1 + sizeof(int);
702 rec_sz
+= 1 + sizeof(int);
703 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
705 if (!cbdataReferenceValid(ex
->p
)) {
706 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
707 netdbExchangeDone(ex
);
711 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
713 if (receivedData
.length
== 0 &&
714 !receivedData
.flags
.error
) {
715 debugs(38, 3, "netdbExchangeHandleReply: Done");
716 netdbExchangeDone(ex
);
722 /* Get the size of the buffer now */
723 size
= ex
->buf_ofs
+ receivedData
.length
;
724 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
726 /* Check if we're still doing headers */
728 if (ex
->connstate
== STATE_HEADER
) {
730 ex
->buf_ofs
+= receivedData
.length
;
732 /* skip reply headers */
734 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
735 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
736 rep
= ex
->e
->getReply();
737 assert (0 != rep
->sline
.status
);
738 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status
);
740 if (HTTP_OK
!= rep
->sline
.status
) {
741 netdbExchangeDone(ex
);
745 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
748 * Now, point p to the part of the buffer where the data
749 * starts, and update the size accordingly
751 assert(ex
->used
== 0);
753 size
= ex
->buf_ofs
- hdr_sz
;
756 /* Finally, set the conn state mode to STATE_BODY */
757 ex
->connstate
= STATE_BODY
;
759 StoreIOBuffer tempBuffer
;
760 tempBuffer
.offset
= ex
->buf_ofs
;
761 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
762 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
763 /* Have more headers .. */
764 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
765 netdbExchangeHandleReply
, ex
);
770 assert(ex
->connstate
== STATE_BODY
);
772 /* If we get here, we have some body to parse .. */
773 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
775 while (size
>= rec_sz
) {
776 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
780 for (o
= 0; o
< rec_sz
;) {
781 switch ((int) *(p
+ o
)) {
783 case NETDB_EX_NETWORK
:
785 /* FIXME INET6 : NetDB can still ony send IPv4 */
786 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
788 o
+= sizeof(struct in_addr
);
793 memcpy(&j
, p
+ o
, sizeof(int));
795 rtt
= (double) ntohl(j
) / 1000.0;
800 memcpy(&j
, p
+ o
, sizeof(int));
802 hops
= (double) ntohl(j
) / 1000.0;
806 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
807 netdbExchangeDone(ex
);
812 if (!addr
.IsAnyAddr() && rtt
> 0)
813 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
827 * Copy anything that is left over to the beginning of the buffer,
828 * and adjust buf_ofs accordingly
832 * Evilly, size refers to the buf size left now,
833 * ex->buf_ofs is the original buffer size, so just copy that
836 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
841 * And don't re-copy the remaining data ..
846 * Now the tricky bit - size _included_ the leftover bit from the _last_
847 * storeClientCopy. We don't want to include that, or our offset will be wrong.
848 * So, don't count the size of the leftover buffer we began with.
849 * This can _disappear_ when we're not tracking offsets ..
851 ex
->used
-= oldbufofs
;
853 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
855 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
856 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
859 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
861 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
862 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
863 netdbExchangeDone(ex
);
864 } else if (ex
->e
->store_status
== STORE_PENDING
) {
865 StoreIOBuffer tempBuffer
;
866 tempBuffer
.offset
= ex
->used
;
867 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
868 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
869 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
870 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
871 netdbExchangeHandleReply
, ex
);
876 netdbExchangeDone(void *data
)
878 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
879 debugs(38, 3, "netdbExchangeDone: " << ex
->e
->url() );
880 HTTPMSGUNLOCK(ex
->r
);
881 storeUnregister(ex
->sc
, ex
->e
, ex
);
883 cbdataReferenceDone(ex
->p
);
888 netdbRegisterWithCacheManager(void)
890 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
893 #endif /* USE_ICMP */
895 /* PUBLIC FUNCTIONS */
903 netdbRegisterWithCacheManager();
908 n
= hashPrime(Config
.Netdb
.high
/ 4);
910 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
912 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
914 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
916 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
924 netdbPingSite(const char *hostname
)
929 if ((n
= netdbLookupHost(hostname
)) != NULL
)
930 if (n
->next_ping_time
> squid_curtime
)
933 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
934 new generic_cbdata(xstrdup(hostname
)));
940 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
945 debugs(38, 3, "netdbHandlePingReply: from " << from
);
947 if ((n
= netdbLookupAddr(from
)) == NULL
)
958 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
960 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
962 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
963 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
964 std::setw(4) << n
->hops
);
970 netdbFreeMemory(void)
973 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
974 hashFreeMemory(addr_table
);
976 hashFreeItems(host_table
, netdbFreeNameEntry
);
977 hashFreeMemory(host_table
);
979 wordlistDestroy(&peer_names
);
984 #if 0 // AYJ: Looks to be unused code.
986 netdbHops(Ip::Address
&addr
)
989 netdbEntry
*n
= netdbLookupAddr(addr
);
991 if (n
&& n
->pings_recv
) {
992 n
->last_use_time
= squid_curtime
;
993 return (int) (n
->hops
+ 0.5);
1002 netdbDump(StoreEntry
* sentry
)
1012 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
1013 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1019 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1021 hash_first(addr_table
);
1023 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1028 if (i
!= memInUse(MEM_NETDBENTRY
))
1029 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
1030 ", expected " << memInUse(MEM_NETDBENTRY
));
1032 qsort((char *) list
,
1034 sizeof(netdbEntry
*),
1037 for (k
= 0; k
< i
; ++k
) {
1039 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1046 for (x
= n
->hosts
; x
; x
= x
->next
)
1047 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1049 storeAppendPrintf(sentry
, "\n");
1053 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1054 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1064 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1069 netdbHostHops(const char *host
)
1072 netdbEntry
*n
= netdbLookupHost(host
);
1075 n
->last_use_time
= squid_curtime
;
1076 return (int) (n
->hops
+ 0.5);
1084 netdbHostRtt(const char *host
)
1087 netdbEntry
*n
= netdbLookupHost(host
);
1090 n
->last_use_time
= squid_curtime
;
1091 return (int) (n
->rtt
+ 0.5);
1099 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1102 netdbEntry
*n
= netdbLookupHost(host
);
1107 *samp
= n
->pings_recv
;
1109 *rtt
= (int) (n
->rtt
+ 0.5);
1111 *hops
= (int) (n
->hops
+ 0.5);
1113 n
->last_use_time
= squid_curtime
;
1119 netdbUpdatePeer(HttpRequest
* r
, peer
* e
, int irtt
, int ihops
)
1123 double rtt
= (double) irtt
;
1124 double hops
= (double) ihops
;
1126 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1127 n
= netdbLookupHost(r
->GetHost());
1130 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1134 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1135 p
= netdbPeerAdd(n
, e
);
1141 p
->expires
= squid_curtime
+ 3600;
1146 qsort((char *) n
->peers
,
1148 sizeof(net_db_peer
),
1155 netdbExchangeUpdatePeer(Ip::Address
&addr
, peer
* e
, double rtt
, double hops
)
1160 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1161 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1164 if ( !addr
.IsIPv4() ) {
1165 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1169 n
= netdbLookupAddr(addr
);
1176 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1177 p
= netdbPeerAdd(n
, e
);
1183 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1188 qsort((char *) n
->peers
,
1190 sizeof(net_db_peer
),
1197 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1200 netdbEntry
*n
= netdbLookupAddr(addr
);
1205 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1212 netdbBinaryExchange(StoreEntry
* s
)
1214 HttpReply
*reply
= new HttpReply
;
1225 struct in_addr line_addr
;
1227 reply
->setHeaders(HTTP_OK
, "OK", NULL
, -1, squid_curtime
, -2);
1228 s
->replaceHttpReply(reply
);
1230 rec_sz
+= 1 + sizeof(struct in_addr
);
1231 rec_sz
+= 1 + sizeof(int);
1232 rec_sz
+= 1 + sizeof(int);
1233 buf
= (char *)memAllocate(MEM_4K_BUF
);
1235 hash_first(addr_table
);
1237 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1241 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1244 if (! (addr
= n
->network
) )
1247 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1248 if ( !addr
.IsIPv4() )
1251 buf
[i
] = (char) NETDB_EX_NETWORK
;
1254 addr
.GetInAddr(line_addr
);
1255 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1257 i
+= sizeof(struct in_addr
);
1259 buf
[i
] = (char) NETDB_EX_RTT
;
1262 j
= htonl((int) (n
->rtt
* 1000));
1264 memcpy(&buf
[i
], &j
, sizeof(int));
1268 buf
[i
] = (char) NETDB_EX_HOPS
;
1271 j
= htonl((int) (n
->hops
* 1000));
1273 memcpy(&buf
[i
], &j
, sizeof(int));
1277 if (i
+ rec_sz
> 4096) {
1290 memFree(buf
, MEM_4K_BUF
);
1293 reply
->setHeaders(HTTP_BAD_REQUEST
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1294 s
->replaceHttpReply(reply
);
1295 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1302 CBDATA_TYPE(netdbExchangeState
);
1306 netdbExchangeStart(void *data
)
1309 peer
*p
= (peer
*)data
;
1311 netdbExchangeState
*ex
;
1312 StoreIOBuffer tempBuffer
;
1313 CBDATA_INIT_TYPE(netdbExchangeState
);
1314 ex
= cbdataAlloc(netdbExchangeState
);
1315 ex
->p
= cbdataReference(p
);
1316 uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", "netdb");
1317 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1318 assert(NULL
!= uri
);
1319 ex
->r
= HttpRequest::CreateFromUrl(uri
);
1321 if (NULL
== ex
->r
) {
1322 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1327 assert(NULL
!= ex
->r
);
1328 ex
->r
->http_ver
= HttpVersion(1,1);
1329 ex
->connstate
= STATE_HEADER
;
1330 ex
->e
= storeCreateEntry(uri
, uri
, request_flags(), METHOD_GET
);
1331 ex
->buf_sz
= NETDB_REQBUF_SZ
;
1332 assert(NULL
!= ex
->e
);
1333 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1334 tempBuffer
.offset
= 0;
1335 tempBuffer
.length
= ex
->buf_sz
;
1336 tempBuffer
.data
= ex
->buf
;
1337 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1338 netdbExchangeHandleReply
, ex
);
1339 ex
->r
->flags
.loopdetect
= 1; /* cheat! -- force direct */
1342 xstrncpy(ex
->r
->login
, p
->login
, MAX_LOGIN_SZ
);
1344 urlCanonical(ex
->r
);
1346 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1352 netdbClosestParent(HttpRequest
* request
)
1357 const ipcache_addrs
*ia
;
1360 n
= netdbLookupHost(request
->GetHost());
1364 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1367 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1373 if (0 == n
->n_peers
)
1376 n
->last_use_time
= squid_curtime
;
1379 * Find the parent with the least RTT to the origin server.
1380 * Make sure we don't return a parent who is farther away than
1381 * we are. Note, the n->peers list is pre-sorted by RTT.
1383 for (i
= 0; i
< n
->n_peers
; ++i
) {
1387 if (n
->rtt
< h
->rtt
)
1390 p
= peerFindByName(h
->peername
);
1392 if (NULL
== p
) /* not found */
1395 if (neighborType(p
, request
) != PEER_PARENT
)
1398 if (!peerHTTPOkay(p
, request
)) /* not allowed */