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 "CacheManager.h"
49 #include "HttpRequest.h"
50 #include "HttpReply.h"
51 #include "MemObject.h"
54 #include "SquidTime.h"
56 #include "ip/Address.h"
59 #include "icmp/IcmpSquid.h"
60 #include "StoreClient.h"
62 #define NETDB_REQBUF_SZ 4096
77 char buf
[NETDB_REQBUF_SZ
];
79 netdb_conn_state_t connstate
;
82 static hash_table
*addr_table
= NULL
;
83 static hash_table
*host_table
= NULL
;
85 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
86 static void netdbRelease(netdbEntry
* n
);
88 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
89 static void netdbHashDelete(const char *key
);
90 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
91 static void netdbHostDelete(const net_db_name
* x
);
92 static void netdbPurgeLRU(void);
93 static netdbEntry
*netdbLookupHost(const char *key
);
94 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
95 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, peer
* e
);
96 static const char *netdbPeerName(const char *name
);
97 static IPH netdbSendPing
;
98 static QS sortPeerByRtt
;
101 static FREE netdbFreeNameEntry
;
102 static FREE netdbFreeNetdbEntry
;
103 static STCB netdbExchangeHandleReply
;
104 static void netdbExchangeDone(void *);
106 /* We have to keep a local list of peer names. The Peers structure
107 * gets freed during a reconfigure. We want this database to
108 * remain persisitent, so _net_db_peer->peername points into this
110 static wordlist
*peer_names
= NULL
;
113 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
115 networkFromInaddr(addr
).NtoA(n
->network
, MAX_IPSTRLEN
);
116 n
->hash
.key
= n
->network
;
117 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
118 hash_join(addr_table
, &n
->hash
);
122 netdbHashDelete(const char *key
)
124 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
127 debug_trap("netdbHashDelete: key not found");
131 hash_remove_link(addr_table
, hptr
);
135 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
137 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
138 x
->hash
.key
= xstrdup(hostname
);
142 assert(hash_lookup(host_table
, hostname
) == NULL
);
143 hash_join(host_table
, &x
->hash
);
148 netdbHostDelete(const net_db_name
* x
)
153 assert(x
->net_db_entry
!= NULL
);
157 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
164 hash_remove_link(host_table
, (hash_link
*) x
);
166 memFree((void *) x
, MEM_NET_DB_NAME
);
170 netdbLookupHost(const char *key
)
172 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
173 return x
? x
->net_db_entry
: NULL
;
177 netdbRelease(netdbEntry
* n
)
182 for (x
= n
->hosts
; x
; x
= next
) {
191 n
->n_peers_alloc
= 0;
193 if (n
->link_count
== 0) {
194 netdbHashDelete(n
->network
);
195 memFree(n
, MEM_NETDBENTRY
);
200 netdbLRU(const void *A
, const void *B
)
202 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
203 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
205 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
208 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
222 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
223 hash_first(addr_table
);
225 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
226 assert(list_count
< memInUse(MEM_NETDBENTRY
));
227 *(list
+ list_count
) = n
;
233 sizeof(netdbEntry
*),
236 for (k
= 0; k
< list_count
; k
++) {
237 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
240 netdbRelease(*(list
+ k
));
249 netdbLookupAddr(const Ip::Address
&addr
)
252 char *key
= new char[MAX_IPSTRLEN
];
253 networkFromInaddr(addr
).NtoA(key
,MAX_IPSTRLEN
);
254 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
260 netdbAdd(Ip::Address
&addr
)
264 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
267 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
268 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
269 netdbHashInsert(n
, addr
);
276 netdbSendPing(const ipcache_addrs
*ia
, const DnsLookupDetails
&, void *data
)
279 char *hostname
= NULL
;
280 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
291 addr
= ia
->in_addrs
[ia
->cur
];
293 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
295 netdbHostInsert(n
, hostname
);
296 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
298 *hostname moved from 'network n' to 'network na'!
304 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
306 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
309 debugs(38, 1, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
314 /* remove net_db_name from 'network n' linked list */
315 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
323 /* point to 'network na' from host entry */
324 x
->net_db_entry
= na
;
325 /* link net_db_name to 'network na' */
332 if (n
->next_ping_time
<= squid_curtime
) {
333 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
334 icmpEngine
.DomainPing(addr
, hostname
);
336 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
337 n
->last_use_time
= squid_curtime
;
344 networkFromInaddr(const Ip::Address
&in
)
350 /* in IPv6 the 'network' should be the routing section. */
352 out
.ApplyMask(64, AF_INET6
);
353 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
362 if (IN_CLASSC(b
.s_addr
))
363 b
.s_addr
&= IN_CLASSC_NET
;
364 else if (IN_CLASSB(b
.s_addr
))
365 b
.s_addr
&= IN_CLASSB_NET
;
366 else if (IN_CLASSA(b
.s_addr
))
367 b
.s_addr
&= IN_CLASSA_NET
;
373 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
375 /* use /24 for everything under IPv4 */
376 out
.ApplyMask(24, AF_INET
);
377 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
383 sortByRtt(const void *A
, const void *B
)
385 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
386 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
388 if ((*n1
)->rtt
> (*n2
)->rtt
)
390 else if ((*n1
)->rtt
< (*n2
)->rtt
)
397 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
400 net_db_peer
*p
= n
->peers
;
402 for (i
= 0; i
< n
->n_peers
; i
++, p
++) {
403 if (!strcmp(p
->peername
, peername
))
411 netdbPeerAdd(netdbEntry
* n
, peer
* e
)
418 if (n
->n_peers
== n
->n_peers_alloc
) {
420 osize
= n
->n_peers_alloc
;
422 if (n
->n_peers_alloc
== 0)
423 n
->n_peers_alloc
= 2;
425 n
->n_peers_alloc
<<= 1;
427 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
429 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
431 for (i
= 0; i
< osize
; i
++)
432 *(n
->peers
+ i
) = *(o
+ i
);
439 p
= n
->peers
+ n
->n_peers
;
440 p
->peername
= netdbPeerName(e
->host
);
446 sortPeerByRtt(const void *A
, const void *B
)
448 const net_db_peer
*p1
= (net_db_peer
*)A
;
449 const net_db_peer
*p2
= (net_db_peer
*)B
;
451 if (p1
->rtt
> p2
->rtt
)
453 else if (p1
->rtt
< p2
->rtt
)
460 netdbSaveState(void *foo
)
462 if (strcmp(Config
.netdbFilename
, "none") == 0)
469 struct timeval start
= current_time
;
472 * This was nicer when we were using stdio, but thanks to
473 * Solaris bugs, its a bad idea. fopen can fail if more than
477 * unlink() is here because there is currently no way to make
478 * logfileOpen() use O_TRUNC.
480 unlink(Config
.netdbFilename
);
481 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
484 debugs(50, 1, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
488 hash_first(addr_table
);
490 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
491 if (n
->pings_recv
== 0)
494 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
500 (int) n
->next_ping_time
,
501 (int) n
->last_use_time
);
503 for (x
= n
->hosts
; x
; x
= x
->next
)
504 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
506 logfilePrintf(lf
, "\n");
516 debugs(38, 1, "NETDB state saved; " <<
517 count
<< " entries, " <<
518 tvSubMsec(start
, current_time
) << " msec" );
519 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
523 netdbReloadState(void)
525 if (strcmp(Config
.netdbFilename
, "none") == 0)
539 struct timeval start
= current_time
;
541 * This was nicer when we were using stdio, but thanks to
542 * Solaris bugs, its a bad idea. fopen can fail if more than
545 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
550 if (fstat(fd
, &sb
) < 0) {
556 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
558 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
566 while ((s
= strchr(t
, '\n'))) {
570 memset(&N
, '\0', sizeof(netdbEntry
));
571 q
= strtok(t
, w_space
);
580 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
583 if ((q
= strtok(NULL
, w_space
)) == NULL
)
586 N
.pings_sent
= atoi(q
);
588 if ((q
= strtok(NULL
, w_space
)) == NULL
)
591 N
.pings_recv
= atoi(q
);
593 if (N
.pings_recv
== 0)
596 /* give this measurement low weight */
601 if ((q
= strtok(NULL
, w_space
)) == NULL
)
606 if ((q
= strtok(NULL
, w_space
)) == NULL
)
611 if ((q
= strtok(NULL
, w_space
)) == NULL
)
614 N
.next_ping_time
= (time_t) atoi(q
);
616 if ((q
= strtok(NULL
, w_space
)) == NULL
)
619 N
.last_use_time
= (time_t) atoi(q
);
621 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
623 xmemcpy(n
, &N
, sizeof(netdbEntry
));
625 netdbHashInsert(n
, addr
);
627 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
628 if (netdbLookupHost(q
) != NULL
) /* no dups! */
631 netdbHostInsert(n
, q
);
639 debugs(38, 1, "NETDB state reloaded; " <<
640 count
<< " entries, " <<
641 tvSubMsec(start
, current_time
) << " msec" );
645 netdbPeerName(const char *name
)
649 for (w
= peer_names
; w
; w
= w
->next
) {
650 if (!strcmp(w
->key
, name
))
654 return wordlistAdd(&peer_names
, name
);
658 netdbFreeNetdbEntry(void *data
)
660 netdbEntry
*n
= (netdbEntry
*)data
;
662 memFree(n
, MEM_NETDBENTRY
);
666 netdbFreeNameEntry(void *data
)
668 net_db_name
*x
= (net_db_name
*)data
;
670 memFree(x
, MEM_NET_DB_NAME
);
675 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
679 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
683 struct in_addr line_addr
;
688 HttpReply
const *rep
;
692 int oldbufofs
= ex
->buf_ofs
;
695 rec_sz
+= 1 + sizeof(struct in_addr
);
696 rec_sz
+= 1 + sizeof(int);
697 rec_sz
+= 1 + sizeof(int);
698 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
700 if (!cbdataReferenceValid(ex
->p
)) {
701 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
702 netdbExchangeDone(ex
);
706 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
708 if (receivedData
.length
== 0 &&
709 !receivedData
.flags
.error
) {
710 debugs(38, 3, "netdbExchangeHandleReply: Done");
711 netdbExchangeDone(ex
);
717 /* Get the size of the buffer now */
718 size
= ex
->buf_ofs
+ receivedData
.length
;
719 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
721 /* Check if we're still doing headers */
723 if (ex
->connstate
== STATE_HEADER
) {
725 ex
->buf_ofs
+= receivedData
.length
;
727 /* skip reply headers */
729 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
730 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
731 rep
= ex
->e
->getReply();
732 assert (0 != rep
->sline
.status
);
733 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status
);
735 if (HTTP_OK
!= rep
->sline
.status
) {
736 netdbExchangeDone(ex
);
740 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
743 * Now, point p to the part of the buffer where the data
744 * starts, and update the size accordingly
746 assert(ex
->used
== 0);
748 size
= ex
->buf_ofs
- hdr_sz
;
751 /* Finally, set the conn state mode to STATE_BODY */
752 ex
->connstate
= STATE_BODY
;
754 StoreIOBuffer tempBuffer
;
755 tempBuffer
.offset
= ex
->buf_ofs
;
756 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
757 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
758 /* Have more headers .. */
759 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
760 netdbExchangeHandleReply
, ex
);
765 assert(ex
->connstate
== STATE_BODY
);
767 /* If we get here, we have some body to parse .. */
768 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
770 while (size
>= rec_sz
) {
771 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
775 for (o
= 0; o
< rec_sz
;) {
776 switch ((int) *(p
+ o
)) {
778 case NETDB_EX_NETWORK
:
780 /* FIXME INET6 : NetDB can still ony send IPv4 */
781 xmemcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
783 o
+= sizeof(struct in_addr
);
788 xmemcpy(&j
, p
+ o
, sizeof(int));
790 rtt
= (double) ntohl(j
) / 1000.0;
795 xmemcpy(&j
, p
+ o
, sizeof(int));
797 hops
= (double) ntohl(j
) / 1000.0;
801 debugs(38, 1, "netdbExchangeHandleReply: corrupt data, aborting");
802 netdbExchangeDone(ex
);
807 if (!addr
.IsAnyAddr() && rtt
> 0)
808 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
822 * Copy anything that is left over to the beginning of the buffer,
823 * and adjust buf_ofs accordingly
827 * Evilly, size refers to the buf size left now,
828 * ex->buf_ofs is the original buffer size, so just copy that
831 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
836 * And don't re-copy the remaining data ..
841 * Now the tricky bit - size _included_ the leftover bit from the _last_
842 * storeClientCopy. We don't want to include that, or our offset will be wrong.
843 * So, don't count the size of the leftover buffer we began with.
844 * This can _disappear_ when we're not tracking offsets ..
846 ex
->used
-= oldbufofs
;
848 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
850 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
851 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
854 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
856 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
857 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
858 netdbExchangeDone(ex
);
859 } else if (ex
->e
->store_status
== STORE_PENDING
) {
860 StoreIOBuffer tempBuffer
;
861 tempBuffer
.offset
= ex
->used
;
862 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
863 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
864 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
865 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
866 netdbExchangeHandleReply
, ex
);
871 netdbExchangeDone(void *data
)
873 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
874 debugs(38, 3, "netdbExchangeDone: " << ex
->e
->url() );
875 HTTPMSGUNLOCK(ex
->r
);
876 storeUnregister(ex
->sc
, ex
->e
, ex
);
878 cbdataReferenceDone(ex
->p
);
883 netdbRegisterWithCacheManager(void)
885 CacheManager::GetInstance()->
886 registerAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
889 #endif /* USE_ICMP */
892 /* PUBLIC FUNCTIONS */
900 netdbRegisterWithCacheManager();
905 n
= hashPrime(Config
.Netdb
.high
/ 4);
907 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
909 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
911 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
913 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
921 netdbPingSite(const char *hostname
)
926 if ((n
= netdbLookupHost(hostname
)) != NULL
)
927 if (n
->next_ping_time
> squid_curtime
)
930 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
931 new generic_cbdata(xstrdup(hostname
)));
937 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
942 debugs(38, 3, "netdbHandlePingReply: from " << from
);
944 if ((n
= netdbLookupAddr(from
)) == NULL
)
955 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
957 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
959 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
960 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
961 std::setw(4) << n
->hops
);
967 netdbFreeMemory(void)
970 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
971 hashFreeMemory(addr_table
);
973 hashFreeItems(host_table
, netdbFreeNameEntry
);
974 hashFreeMemory(host_table
);
976 wordlistDestroy(&peer_names
);
982 #if 0 // AYJ: Looks to be unused code.
984 netdbHops(Ip::Address
&addr
)
987 netdbEntry
*n
= netdbLookupAddr(addr
);
989 if (n
&& n
->pings_recv
) {
990 n
->last_use_time
= squid_curtime
;
991 return (int) (n
->hops
+ 0.5);
1000 netdbDump(StoreEntry
* sentry
)
1010 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
1011 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1017 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1019 hash_first(addr_table
);
1021 while ((n
= (netdbEntry
*) hash_next(addr_table
)))
1024 if (i
!= memInUse(MEM_NETDBENTRY
))
1025 debugs(38, 0, "WARNING: netdb_addrs count off, found " << i
<<
1026 ", expected " << memInUse(MEM_NETDBENTRY
));
1028 qsort((char *) list
,
1030 sizeof(netdbEntry
*),
1033 for (k
= 0; k
< i
; k
++) {
1035 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1042 for (x
= n
->hosts
; x
; x
= x
->next
)
1043 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1045 storeAppendPrintf(sentry
, "\n");
1049 for (j
= 0; j
< n
->n_peers
; j
++, p
++) {
1050 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1060 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1065 netdbHostHops(const char *host
)
1068 netdbEntry
*n
= netdbLookupHost(host
);
1071 n
->last_use_time
= squid_curtime
;
1072 return (int) (n
->hops
+ 0.5);
1080 netdbHostRtt(const char *host
)
1083 netdbEntry
*n
= netdbLookupHost(host
);
1086 n
->last_use_time
= squid_curtime
;
1087 return (int) (n
->rtt
+ 0.5);
1095 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1098 netdbEntry
*n
= netdbLookupHost(host
);
1103 *samp
= n
->pings_recv
;
1105 *rtt
= (int) (n
->rtt
+ 0.5);
1107 *hops
= (int) (n
->hops
+ 0.5);
1109 n
->last_use_time
= squid_curtime
;
1115 netdbUpdatePeer(HttpRequest
* r
, peer
* e
, int irtt
, int ihops
)
1119 double rtt
= (double) irtt
;
1120 double hops
= (double) ihops
;
1122 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1123 n
= netdbLookupHost(r
->GetHost());
1126 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1130 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1131 p
= netdbPeerAdd(n
, e
);
1137 p
->expires
= squid_curtime
+ 3600;
1142 qsort((char *) n
->peers
,
1144 sizeof(net_db_peer
),
1151 netdbExchangeUpdatePeer(Ip::Address
&addr
, peer
* e
, double rtt
, double hops
)
1156 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1157 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1160 if ( !addr
.IsIPv4() ) {
1161 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1165 n
= netdbLookupAddr(addr
);
1172 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1173 p
= netdbPeerAdd(n
, e
);
1179 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1184 qsort((char *) n
->peers
,
1186 sizeof(net_db_peer
),
1193 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1196 netdbEntry
*n
= netdbLookupAddr(addr
);
1201 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1209 netdbBinaryExchange(StoreEntry
* s
)
1211 HttpReply
*reply
= new HttpReply
;
1222 struct in_addr line_addr
;
1224 reply
->setHeaders(HTTP_OK
, "OK", NULL
, -1, squid_curtime
, -2);
1225 s
->replaceHttpReply(reply
);
1227 rec_sz
+= 1 + sizeof(struct in_addr
);
1228 rec_sz
+= 1 + sizeof(int);
1229 rec_sz
+= 1 + sizeof(int);
1230 buf
= (char *)memAllocate(MEM_4K_BUF
);
1232 hash_first(addr_table
);
1234 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1238 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1241 if (! (addr
= n
->network
) )
1244 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1245 if ( !addr
.IsIPv4() )
1248 buf
[i
++] = (char) NETDB_EX_NETWORK
;
1250 addr
.GetInAddr(line_addr
);
1251 xmemcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1253 i
+= sizeof(struct in_addr
);
1255 buf
[i
++] = (char) NETDB_EX_RTT
;
1257 j
= htonl((int) (n
->rtt
* 1000));
1259 xmemcpy(&buf
[i
], &j
, sizeof(int));
1263 buf
[i
++] = (char) NETDB_EX_HOPS
;
1265 j
= htonl((int) (n
->hops
* 1000));
1267 xmemcpy(&buf
[i
], &j
, sizeof(int));
1271 if (i
+ rec_sz
> 4096) {
1284 memFree(buf
, MEM_4K_BUF
);
1287 reply
->setHeaders(HTTP_BAD_REQUEST
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1288 s
->replaceHttpReply(reply
);
1289 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1296 CBDATA_TYPE(netdbExchangeState
);
1300 netdbExchangeStart(void *data
)
1303 peer
*p
= (peer
*)data
;
1305 netdbExchangeState
*ex
;
1306 StoreIOBuffer tempBuffer
;
1307 CBDATA_INIT_TYPE(netdbExchangeState
);
1308 ex
= cbdataAlloc(netdbExchangeState
);
1309 ex
->p
= cbdataReference(p
);
1310 uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", "netdb");
1311 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1312 assert(NULL
!= uri
);
1313 ex
->r
= HttpRequest::CreateFromUrl(uri
);
1315 if (NULL
== ex
->r
) {
1316 debugs(38, 1, "netdbExchangeStart: Bad URI " << uri
);
1321 assert(NULL
!= ex
->r
);
1322 ex
->r
->http_ver
= HttpVersion(1,1);
1323 ex
->connstate
= STATE_HEADER
;
1324 ex
->e
= storeCreateEntry(uri
, uri
, request_flags(), METHOD_GET
);
1325 ex
->buf_sz
= NETDB_REQBUF_SZ
;
1326 assert(NULL
!= ex
->e
);
1327 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1328 tempBuffer
.offset
= 0;
1329 tempBuffer
.length
= ex
->buf_sz
;
1330 tempBuffer
.data
= ex
->buf
;
1331 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1332 netdbExchangeHandleReply
, ex
);
1333 ex
->r
->flags
.loopdetect
= 1; /* cheat! -- force direct */
1336 xstrncpy(ex
->r
->login
, p
->login
, MAX_LOGIN_SZ
);
1338 urlCanonical(ex
->r
);
1340 Comm::ConnectionPointer nul
;
1341 FwdState::fwdStart(nul
, ex
->e
, ex
->r
);
1347 netdbClosestParent(HttpRequest
* request
)
1352 const ipcache_addrs
*ia
;
1355 n
= netdbLookupHost(request
->GetHost());
1359 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1362 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1368 if (0 == n
->n_peers
)
1371 n
->last_use_time
= squid_curtime
;
1374 * Find the parent with the least RTT to the origin server.
1375 * Make sure we don't return a parent who is farther away than
1376 * we are. Note, the n->peers list is pre-sorted by RTT.
1378 for (i
= 0; i
< n
->n_peers
; i
++) {
1382 if (n
->rtt
< h
->rtt
)
1385 p
= peerFindByName(h
->peername
);
1387 if (NULL
== p
) /* not found */
1390 if (neighborType(p
, request
) != PEER_PARENT
)
1393 if (!peerHTTPOkay(p
, request
)) /* not allowed */