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
);
259 netdbAdd(Ip::Address
&addr
)
263 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
266 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
267 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
268 netdbHashInsert(n
, addr
);
275 netdbSendPing(const ipcache_addrs
*ia
, const DnsLookupDetails
&, void *data
)
278 char *hostname
= NULL
;
279 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
290 addr
= ia
->in_addrs
[ia
->cur
];
292 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
294 netdbHostInsert(n
, hostname
);
295 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
297 *hostname moved from 'network n' to 'network na'!
303 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
305 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
308 debugs(38, 1, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
313 /* remove net_db_name from 'network n' linked list */
314 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
322 /* point to 'network na' from host entry */
323 x
->net_db_entry
= na
;
324 /* link net_db_name to 'network na' */
331 if (n
->next_ping_time
<= squid_curtime
) {
332 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
333 icmpEngine
.DomainPing(addr
, hostname
);
335 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
336 n
->last_use_time
= squid_curtime
;
343 networkFromInaddr(const Ip::Address
&in
)
349 /* in IPv6 the 'network' should be the routing section. */
351 out
.ApplyMask(64, AF_INET6
);
352 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
361 if (IN_CLASSC(b
.s_addr
))
362 b
.s_addr
&= IN_CLASSC_NET
;
363 else if (IN_CLASSB(b
.s_addr
))
364 b
.s_addr
&= IN_CLASSB_NET
;
365 else if (IN_CLASSA(b
.s_addr
))
366 b
.s_addr
&= IN_CLASSA_NET
;
372 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
374 /* use /24 for everything under IPv4 */
375 out
.ApplyMask(24, AF_INET
);
376 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
382 sortByRtt(const void *A
, const void *B
)
384 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
385 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
387 if ((*n1
)->rtt
> (*n2
)->rtt
)
389 else if ((*n1
)->rtt
< (*n2
)->rtt
)
396 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
399 net_db_peer
*p
= n
->peers
;
401 for (i
= 0; i
< n
->n_peers
; i
++, p
++) {
402 if (!strcmp(p
->peername
, peername
))
410 netdbPeerAdd(netdbEntry
* n
, peer
* e
)
417 if (n
->n_peers
== n
->n_peers_alloc
) {
419 osize
= n
->n_peers_alloc
;
421 if (n
->n_peers_alloc
== 0)
422 n
->n_peers_alloc
= 2;
424 n
->n_peers_alloc
<<= 1;
426 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
428 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
430 for (i
= 0; i
< osize
; i
++)
431 *(n
->peers
+ i
) = *(o
+ i
);
438 p
= n
->peers
+ n
->n_peers
;
439 p
->peername
= netdbPeerName(e
->host
);
445 sortPeerByRtt(const void *A
, const void *B
)
447 const net_db_peer
*p1
= (net_db_peer
*)A
;
448 const net_db_peer
*p2
= (net_db_peer
*)B
;
450 if (p1
->rtt
> p2
->rtt
)
452 else if (p1
->rtt
< p2
->rtt
)
459 netdbSaveState(void *foo
)
461 if (strcmp(Config
.netdbFilename
, "none") == 0)
468 struct timeval start
= current_time
;
471 * This was nicer when we were using stdio, but thanks to
472 * Solaris bugs, its a bad idea. fopen can fail if more than
476 * unlink() is here because there is currently no way to make
477 * logfileOpen() use O_TRUNC.
479 unlink(Config
.netdbFilename
);
480 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
483 debugs(50, 1, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
487 hash_first(addr_table
);
489 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
490 if (n
->pings_recv
== 0)
493 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
499 (int) n
->next_ping_time
,
500 (int) n
->last_use_time
);
502 for (x
= n
->hosts
; x
; x
= x
->next
)
503 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
505 logfilePrintf(lf
, "\n");
515 debugs(38, 1, "NETDB state saved; " <<
516 count
<< " entries, " <<
517 tvSubMsec(start
, current_time
) << " msec" );
518 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
522 netdbReloadState(void)
524 if (strcmp(Config
.netdbFilename
, "none") == 0)
538 struct timeval start
= current_time
;
540 * This was nicer when we were using stdio, but thanks to
541 * Solaris bugs, its a bad idea. fopen can fail if more than
544 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
549 if (fstat(fd
, &sb
) < 0) {
555 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
557 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
565 while ((s
= strchr(t
, '\n'))) {
569 memset(&N
, '\0', sizeof(netdbEntry
));
570 q
= strtok(t
, w_space
);
579 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
582 if ((q
= strtok(NULL
, w_space
)) == NULL
)
585 N
.pings_sent
= atoi(q
);
587 if ((q
= strtok(NULL
, w_space
)) == NULL
)
590 N
.pings_recv
= atoi(q
);
592 if (N
.pings_recv
== 0)
595 /* give this measurement low weight */
600 if ((q
= strtok(NULL
, w_space
)) == NULL
)
605 if ((q
= strtok(NULL
, w_space
)) == NULL
)
610 if ((q
= strtok(NULL
, w_space
)) == NULL
)
613 N
.next_ping_time
= (time_t) atoi(q
);
615 if ((q
= strtok(NULL
, w_space
)) == NULL
)
618 N
.last_use_time
= (time_t) atoi(q
);
620 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
622 xmemcpy(n
, &N
, sizeof(netdbEntry
));
624 netdbHashInsert(n
, addr
);
626 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
627 if (netdbLookupHost(q
) != NULL
) /* no dups! */
630 netdbHostInsert(n
, q
);
638 debugs(38, 1, "NETDB state reloaded; " <<
639 count
<< " entries, " <<
640 tvSubMsec(start
, current_time
) << " msec" );
644 netdbPeerName(const char *name
)
648 for (w
= peer_names
; w
; w
= w
->next
) {
649 if (!strcmp(w
->key
, name
))
653 return wordlistAdd(&peer_names
, name
);
657 netdbFreeNetdbEntry(void *data
)
659 netdbEntry
*n
= (netdbEntry
*)data
;
661 memFree(n
, MEM_NETDBENTRY
);
665 netdbFreeNameEntry(void *data
)
667 net_db_name
*x
= (net_db_name
*)data
;
669 memFree(x
, MEM_NET_DB_NAME
);
674 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
678 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
682 struct in_addr line_addr
;
687 HttpReply
const *rep
;
691 int oldbufofs
= ex
->buf_ofs
;
694 rec_sz
+= 1 + sizeof(struct in_addr
);
695 rec_sz
+= 1 + sizeof(int);
696 rec_sz
+= 1 + sizeof(int);
697 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
699 if (!cbdataReferenceValid(ex
->p
)) {
700 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
701 netdbExchangeDone(ex
);
705 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
707 if (receivedData
.length
== 0 &&
708 !receivedData
.flags
.error
) {
709 debugs(38, 3, "netdbExchangeHandleReply: Done");
710 netdbExchangeDone(ex
);
716 /* Get the size of the buffer now */
717 size
= ex
->buf_ofs
+ receivedData
.length
;
718 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
720 /* Check if we're still doing headers */
722 if (ex
->connstate
== STATE_HEADER
) {
724 ex
->buf_ofs
+= receivedData
.length
;
726 /* skip reply headers */
728 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
729 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
730 rep
= ex
->e
->getReply();
731 assert (0 != rep
->sline
.status
);
732 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status
);
734 if (HTTP_OK
!= rep
->sline
.status
) {
735 netdbExchangeDone(ex
);
739 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
742 * Now, point p to the part of the buffer where the data
743 * starts, and update the size accordingly
745 assert(ex
->used
== 0);
747 size
= ex
->buf_ofs
- hdr_sz
;
750 /* Finally, set the conn state mode to STATE_BODY */
751 ex
->connstate
= STATE_BODY
;
753 StoreIOBuffer tempBuffer
;
754 tempBuffer
.offset
= ex
->buf_ofs
;
755 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
756 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
757 /* Have more headers .. */
758 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
759 netdbExchangeHandleReply
, ex
);
764 assert(ex
->connstate
== STATE_BODY
);
766 /* If we get here, we have some body to parse .. */
767 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
769 while (size
>= rec_sz
) {
770 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
774 for (o
= 0; o
< rec_sz
;) {
775 switch ((int) *(p
+ o
)) {
777 case NETDB_EX_NETWORK
:
779 /* FIXME INET6 : NetDB can still ony send IPv4 */
780 xmemcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
782 o
+= sizeof(struct in_addr
);
787 xmemcpy(&j
, p
+ o
, sizeof(int));
789 rtt
= (double) ntohl(j
) / 1000.0;
794 xmemcpy(&j
, p
+ o
, sizeof(int));
796 hops
= (double) ntohl(j
) / 1000.0;
800 debugs(38, 1, "netdbExchangeHandleReply: corrupt data, aborting");
801 netdbExchangeDone(ex
);
806 if (!addr
.IsAnyAddr() && rtt
> 0)
807 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
821 * Copy anything that is left over to the beginning of the buffer,
822 * and adjust buf_ofs accordingly
826 * Evilly, size refers to the buf size left now,
827 * ex->buf_ofs is the original buffer size, so just copy that
830 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
835 * And don't re-copy the remaining data ..
840 * Now the tricky bit - size _included_ the leftover bit from the _last_
841 * storeClientCopy. We don't want to include that, or our offset will be wrong.
842 * So, don't count the size of the leftover buffer we began with.
843 * This can _disappear_ when we're not tracking offsets ..
845 ex
->used
-= oldbufofs
;
847 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
849 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
850 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
853 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
855 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
856 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
857 netdbExchangeDone(ex
);
858 } else if (ex
->e
->store_status
== STORE_PENDING
) {
859 StoreIOBuffer tempBuffer
;
860 tempBuffer
.offset
= ex
->used
;
861 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
862 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
863 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
864 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
865 netdbExchangeHandleReply
, ex
);
870 netdbExchangeDone(void *data
)
872 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
873 debugs(38, 3, "netdbExchangeDone: " << ex
->e
->url() );
874 HTTPMSGUNLOCK(ex
->r
);
875 storeUnregister(ex
->sc
, ex
->e
, ex
);
877 cbdataReferenceDone(ex
->p
);
882 netdbRegisterWithCacheManager(void)
884 CacheManager::GetInstance()->
885 registerAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
888 #endif /* USE_ICMP */
891 /* PUBLIC FUNCTIONS */
899 netdbRegisterWithCacheManager();
904 n
= hashPrime(Config
.Netdb
.high
/ 4);
906 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
908 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
910 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
912 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
920 netdbPingSite(const char *hostname
)
925 if ((n
= netdbLookupHost(hostname
)) != NULL
)
926 if (n
->next_ping_time
> squid_curtime
)
929 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
930 new generic_cbdata(xstrdup(hostname
)));
936 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
941 debugs(38, 3, "netdbHandlePingReply: from " << from
);
943 if ((n
= netdbLookupAddr(from
)) == NULL
)
954 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
956 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
958 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
959 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
960 std::setw(4) << n
->hops
);
966 netdbFreeMemory(void)
969 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
970 hashFreeMemory(addr_table
);
972 hashFreeItems(host_table
, netdbFreeNameEntry
);
973 hashFreeMemory(host_table
);
975 wordlistDestroy(&peer_names
);
981 #if 0 // AYJ: Looks to be unused code.
983 netdbHops(Ip::Address
&addr
)
986 netdbEntry
*n
= netdbLookupAddr(addr
);
988 if (n
&& n
->pings_recv
) {
989 n
->last_use_time
= squid_curtime
;
990 return (int) (n
->hops
+ 0.5);
999 netdbDump(StoreEntry
* sentry
)
1009 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
1010 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1016 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1018 hash_first(addr_table
);
1020 while ((n
= (netdbEntry
*) hash_next(addr_table
)))
1023 if (i
!= memInUse(MEM_NETDBENTRY
))
1024 debugs(38, 0, "WARNING: netdb_addrs count off, found " << i
<<
1025 ", expected " << memInUse(MEM_NETDBENTRY
));
1027 qsort((char *) list
,
1029 sizeof(netdbEntry
*),
1032 for (k
= 0; k
< i
; k
++) {
1034 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1041 for (x
= n
->hosts
; x
; x
= x
->next
)
1042 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1044 storeAppendPrintf(sentry
, "\n");
1048 for (j
= 0; j
< n
->n_peers
; j
++, p
++) {
1049 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1059 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1064 netdbHostHops(const char *host
)
1067 netdbEntry
*n
= netdbLookupHost(host
);
1070 n
->last_use_time
= squid_curtime
;
1071 return (int) (n
->hops
+ 0.5);
1079 netdbHostRtt(const char *host
)
1082 netdbEntry
*n
= netdbLookupHost(host
);
1085 n
->last_use_time
= squid_curtime
;
1086 return (int) (n
->rtt
+ 0.5);
1094 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1097 netdbEntry
*n
= netdbLookupHost(host
);
1102 *samp
= n
->pings_recv
;
1104 *rtt
= (int) (n
->rtt
+ 0.5);
1106 *hops
= (int) (n
->hops
+ 0.5);
1108 n
->last_use_time
= squid_curtime
;
1114 netdbUpdatePeer(HttpRequest
* r
, peer
* e
, int irtt
, int ihops
)
1118 double rtt
= (double) irtt
;
1119 double hops
= (double) ihops
;
1121 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1122 n
= netdbLookupHost(r
->GetHost());
1125 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1129 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1130 p
= netdbPeerAdd(n
, e
);
1136 p
->expires
= squid_curtime
+ 3600;
1141 qsort((char *) n
->peers
,
1143 sizeof(net_db_peer
),
1150 netdbExchangeUpdatePeer(Ip::Address
&addr
, peer
* e
, double rtt
, double hops
)
1155 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1156 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1159 if ( !addr
.IsIPv4() ) {
1160 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1164 n
= netdbLookupAddr(addr
);
1171 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1172 p
= netdbPeerAdd(n
, e
);
1178 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1183 qsort((char *) n
->peers
,
1185 sizeof(net_db_peer
),
1192 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1195 netdbEntry
*n
= netdbLookupAddr(addr
);
1200 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1208 netdbBinaryExchange(StoreEntry
* s
)
1210 HttpReply
*reply
= new HttpReply
;
1221 struct in_addr line_addr
;
1223 reply
->setHeaders(HTTP_OK
, "OK", NULL
, -1, squid_curtime
, -2);
1224 s
->replaceHttpReply(reply
);
1226 rec_sz
+= 1 + sizeof(struct in_addr
);
1227 rec_sz
+= 1 + sizeof(int);
1228 rec_sz
+= 1 + sizeof(int);
1229 buf
= (char *)memAllocate(MEM_4K_BUF
);
1231 hash_first(addr_table
);
1233 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1237 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1240 if (! (addr
= n
->network
) )
1243 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1244 if ( !addr
.IsIPv4() )
1247 buf
[i
++] = (char) NETDB_EX_NETWORK
;
1249 addr
.GetInAddr(line_addr
);
1250 xmemcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1252 i
+= sizeof(struct in_addr
);
1254 buf
[i
++] = (char) NETDB_EX_RTT
;
1256 j
= htonl((int) (n
->rtt
* 1000));
1258 xmemcpy(&buf
[i
], &j
, sizeof(int));
1262 buf
[i
++] = (char) NETDB_EX_HOPS
;
1264 j
= htonl((int) (n
->hops
* 1000));
1266 xmemcpy(&buf
[i
], &j
, sizeof(int));
1270 if (i
+ rec_sz
> 4096) {
1283 memFree(buf
, MEM_4K_BUF
);
1286 reply
->setHeaders(HTTP_BAD_REQUEST
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1287 s
->replaceHttpReply(reply
);
1288 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1295 CBDATA_TYPE(netdbExchangeState
);
1299 netdbExchangeStart(void *data
)
1302 peer
*p
= (peer
*)data
;
1304 netdbExchangeState
*ex
;
1305 StoreIOBuffer tempBuffer
;
1306 CBDATA_INIT_TYPE(netdbExchangeState
);
1307 ex
= cbdataAlloc(netdbExchangeState
);
1308 ex
->p
= cbdataReference(p
);
1309 uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", "netdb");
1310 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1311 assert(NULL
!= uri
);
1312 ex
->r
= HttpRequest::CreateFromUrl(uri
);
1314 if (NULL
== ex
->r
) {
1315 debugs(38, 1, "netdbExchangeStart: Bad URI " << uri
);
1320 assert(NULL
!= ex
->r
);
1321 ex
->r
->http_ver
= HttpVersion(1,1);
1322 ex
->connstate
= STATE_HEADER
;
1323 ex
->e
= storeCreateEntry(uri
, uri
, request_flags(), METHOD_GET
);
1324 ex
->buf_sz
= NETDB_REQBUF_SZ
;
1325 assert(NULL
!= ex
->e
);
1326 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1327 tempBuffer
.offset
= 0;
1328 tempBuffer
.length
= ex
->buf_sz
;
1329 tempBuffer
.data
= ex
->buf
;
1330 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1331 netdbExchangeHandleReply
, ex
);
1332 ex
->r
->flags
.loopdetect
= 1; /* cheat! -- force direct */
1335 xstrncpy(ex
->r
->login
, p
->login
, MAX_LOGIN_SZ
);
1337 urlCanonical(ex
->r
);
1339 Comm::ConnectionPointer nul
;
1340 FwdState::fwdStart(nul
, ex
->e
, ex
->r
);
1346 netdbClosestParent(HttpRequest
* request
)
1351 const ipcache_addrs
*ia
;
1354 n
= netdbLookupHost(request
->GetHost());
1358 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1361 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1367 if (0 == n
->n_peers
)
1370 n
->last_use_time
= squid_curtime
;
1373 * Find the parent with the least RTT to the origin server.
1374 * Make sure we don't return a parent who is farther away than
1375 * we are. Note, the n->peers list is pre-sorted by RTT.
1377 for (i
= 0; i
< n
->n_peers
; i
++) {
1381 if (n
->rtt
< h
->rtt
)
1384 p
= peerFindByName(h
->peername
);
1386 if (NULL
== p
) /* not found */
1389 if (neighborType(p
, request
) != PEER_PARENT
)
1392 if (!peerHTTPOkay(p
, request
)) /* not allowed */