2 * $Id: net_db.cc,v 1.201 2007/12/29 18:15:45 hno Exp $
4 * DEBUG: section 38 Network Measurement Database
5 * AUTHOR: Duane Wessels
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 * This code may be slightly broken now. If you're getting consistent
39 * (sometimes working) corrupt data exchanges, please contact adrian
40 * (adrian@squid-cache.org) to sort them out.
46 #include "CacheManager.h"
49 #include "HttpRequest.h"
50 #include "HttpReply.h"
51 #include "MemObject.h"
54 #include "SquidTime.h"
56 #include "IPAddress.h"
59 #include "ICMPSquid.h"
60 #include "StoreClient.h"
62 #define NETDB_REQBUF_SZ 4096
78 char buf
[NETDB_REQBUF_SZ
];
80 netdb_conn_state_t connstate
;
85 static hash_table
*addr_table
= NULL
;
86 static hash_table
*host_table
= NULL
;
88 IPAddress
networkFromInaddr(const IPAddress
&a
);
89 static void netdbRelease(netdbEntry
* n
);
91 static void netdbHashInsert(netdbEntry
* n
, IPAddress
&addr
);
92 static void netdbHashDelete(const char *key
);
93 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
94 static void netdbHostDelete(const net_db_name
* x
);
95 static void netdbPurgeLRU(void);
96 static netdbEntry
*netdbLookupHost(const char *key
);
97 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
98 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, peer
* e
);
99 static const char *netdbPeerName(const char *name
);
100 static IPH netdbSendPing
;
101 static QS sortPeerByRtt
;
104 static FREE netdbFreeNameEntry
;
105 static FREE netdbFreeNetdbEntry
;
106 static STCB netdbExchangeHandleReply
;
107 static void netdbExchangeDone(void *);
109 /* We have to keep a local list of peer names. The Peers structure
110 * gets freed during a reconfigure. We want this database to
111 * remain persisitent, so _net_db_peer->peername points into this
113 static wordlist
*peer_names
= NULL
;
116 netdbHashInsert(netdbEntry
* n
, IPAddress
&addr
)
118 networkFromInaddr(addr
).NtoA(n
->network
, MAX_IPSTRLEN
);
119 n
->hash
.key
= n
->network
;
120 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
121 hash_join(addr_table
, &n
->hash
);
125 netdbHashDelete(const char *key
)
127 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
130 debug_trap("netdbHashDelete: key not found");
134 hash_remove_link(addr_table
, hptr
);
138 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
140 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
141 x
->hash
.key
= xstrdup(hostname
);
145 assert(hash_lookup(host_table
, hostname
) == NULL
);
146 hash_join(host_table
, &x
->hash
);
151 netdbHostDelete(const net_db_name
* x
)
156 assert(x
->net_db_entry
!= NULL
);
160 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
167 hash_remove_link(host_table
, (hash_link
*) x
);
169 memFree((void *) x
, MEM_NET_DB_NAME
);
173 netdbLookupHost(const char *key
)
175 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
176 return x
? x
->net_db_entry
: NULL
;
180 netdbRelease(netdbEntry
* n
)
185 for (x
= n
->hosts
; x
; x
= next
) {
194 n
->n_peers_alloc
= 0;
196 if (n
->link_count
== 0) {
197 netdbHashDelete(n
->network
);
198 memFree(n
, MEM_NETDBENTRY
);
203 netdbLRU(const void *A
, const void *B
)
205 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
206 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
208 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
211 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
225 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
226 hash_first(addr_table
);
228 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
229 assert(list_count
< memInUse(MEM_NETDBENTRY
));
230 *(list
+ list_count
) = n
;
236 sizeof(netdbEntry
*),
239 for (k
= 0; k
< list_count
; k
++) {
240 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
243 netdbRelease(*(list
+ k
));
252 netdbLookupAddr(const IPAddress
&addr
)
255 char *key
= new char[MAX_IPSTRLEN
];
256 networkFromInaddr(addr
).NtoA(key
,MAX_IPSTRLEN
);
257 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
262 netdbAdd(IPAddress
&addr
)
266 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
269 if ((n
= netdbLookupAddr(addr
)) == NULL
)
271 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
272 netdbHashInsert(n
, addr
);
279 netdbSendPing(const ipcache_addrs
* ia
, void *data
)
282 char *hostname
= NULL
;
283 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
294 addr
= ia
->in_addrs
[ia
->cur
];
296 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
298 netdbHostInsert(n
, hostname
);
299 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
301 *hostname moved from 'network n' to 'network na'!
307 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
309 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
312 debugs(38, 1, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
317 /* remove net_db_name from 'network n' linked list */
318 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
326 /* point to 'network na' from host entry */
327 x
->net_db_entry
= na
;
328 /* link net_db_name to 'network na' */
335 if (n
->next_ping_time
<= squid_curtime
) {
336 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
337 icmpEngine
.DomainPing(addr
, hostname
);
339 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
340 n
->last_use_time
= squid_curtime
;
347 networkFromInaddr(const IPAddress
&in
)
354 /* 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.");
369 if (IN_CLASSC(b
.s_addr
))
370 b
.s_addr
&= IN_CLASSC_NET
;
371 else if (IN_CLASSB(b
.s_addr
))
372 b
.s_addr
&= IN_CLASSB_NET
;
373 else if (IN_CLASSA(b
.s_addr
))
374 b
.s_addr
&= IN_CLASSA_NET
;
380 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
382 /* use /24 for everything under IPv4 */
383 out
.ApplyMask(24, AF_INET
);
384 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
390 sortByRtt(const void *A
, const void *B
)
392 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
393 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
395 if ((*n1
)->rtt
> (*n2
)->rtt
)
397 else if ((*n1
)->rtt
< (*n2
)->rtt
)
404 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
407 net_db_peer
*p
= n
->peers
;
409 for (i
= 0; i
< n
->n_peers
; i
++, p
++) {
410 if (!strcmp(p
->peername
, peername
))
418 netdbPeerAdd(netdbEntry
* n
, peer
* e
)
425 if (n
->n_peers
== n
->n_peers_alloc
) {
427 osize
= n
->n_peers_alloc
;
429 if (n
->n_peers_alloc
== 0)
430 n
->n_peers_alloc
= 2;
432 n
->n_peers_alloc
<<= 1;
434 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
436 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
438 for (i
= 0; i
< osize
; i
++)
439 *(n
->peers
+ i
) = *(o
+ i
);
446 p
= n
->peers
+ n
->n_peers
;
447 p
->peername
= netdbPeerName(e
->host
);
453 sortPeerByRtt(const void *A
, const void *B
)
455 const net_db_peer
*p1
= (net_db_peer
*)A
;
456 const net_db_peer
*p2
= (net_db_peer
*)B
;
458 if (p1
->rtt
> p2
->rtt
)
460 else if (p1
->rtt
< p2
->rtt
)
467 netdbSaveState(void *foo
)
469 if (strcmp(Config
.netdbFilename
, "none") == 0)
476 struct timeval start
= current_time
;
479 * This was nicer when we were using stdio, but thanks to
480 * Solaris bugs, its a bad idea. fopen can fail if more than
484 * unlink() is here because there is currently no way to make
485 * logfileOpen() use O_TRUNC.
487 unlink(Config
.netdbFilename
);
488 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
491 debugs(50, 1, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
495 hash_first(addr_table
);
497 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
498 if (n
->pings_recv
== 0)
501 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
507 (int) n
->next_ping_time
,
508 (int) n
->last_use_time
);
510 for (x
= n
->hosts
; x
; x
= x
->next
)
511 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
513 logfilePrintf(lf
, "\n");
523 debugs(38, 1, "NETDB state saved; " <<
524 count
<< " entries, " <<
525 tvSubMsec(start
, current_time
) << " msec" );
526 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
530 netdbReloadState(void)
532 if (strcmp(Config
.netdbFilename
, "none") == 0)
546 struct timeval start
= current_time
;
548 * This was nicer when we were using stdio, but thanks to
549 * Solaris bugs, its a bad idea. fopen can fail if more than
552 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
557 if (fstat(fd
, &sb
) < 0) {
563 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
565 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
573 while ((s
= strchr(t
, '\n'))) {
577 memset(&N
, '\0', sizeof(netdbEntry
));
578 q
= strtok(t
, w_space
);
587 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
590 if ((q
= strtok(NULL
, w_space
)) == NULL
)
593 N
.pings_sent
= atoi(q
);
595 if ((q
= strtok(NULL
, w_space
)) == NULL
)
598 N
.pings_recv
= atoi(q
);
600 if (N
.pings_recv
== 0)
603 /* give this measurement low weight */
608 if ((q
= strtok(NULL
, w_space
)) == NULL
)
613 if ((q
= strtok(NULL
, w_space
)) == NULL
)
618 if ((q
= strtok(NULL
, w_space
)) == NULL
)
621 N
.next_ping_time
= (time_t) atoi(q
);
623 if ((q
= strtok(NULL
, w_space
)) == NULL
)
626 N
.last_use_time
= (time_t) atoi(q
);
628 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
630 xmemcpy(n
, &N
, sizeof(netdbEntry
));
632 netdbHashInsert(n
, addr
);
634 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
635 if (netdbLookupHost(q
) != NULL
) /* no dups! */
638 netdbHostInsert(n
, q
);
646 debugs(38, 1, "NETDB state reloaded; " <<
647 count
<< " entries, " <<
648 tvSubMsec(start
, current_time
) << " msec" );
652 netdbPeerName(const char *name
)
656 for (w
= peer_names
; w
; w
= w
->next
) {
657 if (!strcmp(w
->key
, name
))
661 return wordlistAdd(&peer_names
, name
);
665 netdbFreeNetdbEntry(void *data
)
667 netdbEntry
*n
= (netdbEntry
*)data
;
669 memFree(n
, MEM_NETDBENTRY
);
673 netdbFreeNameEntry(void *data
)
675 net_db_name
*x
= (net_db_name
*)data
;
677 memFree(x
, MEM_NET_DB_NAME
);
682 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
686 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
690 struct in_addr line_addr
;
695 HttpReply
const *rep
;
699 int oldbufofs
= ex
->buf_ofs
;
702 rec_sz
+= 1 + sizeof(struct in_addr
);
703 rec_sz
+= 1 + sizeof(int);
704 rec_sz
+= 1 + sizeof(int);
705 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
707 if (!cbdataReferenceValid(ex
->p
)) {
708 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
709 netdbExchangeDone(ex
);
713 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
715 if (receivedData
.length
== 0 &&
716 !receivedData
.flags
.error
) {
717 debugs(38, 3, "netdbExchangeHandleReply: Done");
718 netdbExchangeDone(ex
);
724 /* Get the size of the buffer now */
725 size
= ex
->buf_ofs
+ receivedData
.length
;
726 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
728 /* Check if we're still doing headers */
730 if (ex
->connstate
== STATE_HEADER
) {
732 ex
->buf_ofs
+= receivedData
.length
;
734 /* skip reply headers */
736 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
737 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
738 rep
= ex
->e
->getReply();
739 assert (0 != rep
->sline
.status
);
740 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status
);
742 if (HTTP_OK
!= rep
->sline
.status
) {
743 netdbExchangeDone(ex
);
747 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
750 * Now, point p to the part of the buffer where the data
751 * starts, and update the size accordingly
753 assert(ex
->used
== 0);
755 size
= ex
->buf_ofs
- hdr_sz
;
758 /* Finally, set the conn state mode to STATE_BODY */
759 ex
->connstate
= STATE_BODY
;
761 StoreIOBuffer tempBuffer
;
762 tempBuffer
.offset
= ex
->buf_ofs
;
763 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
764 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
765 /* Have more headers .. */
766 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
767 netdbExchangeHandleReply
, ex
);
772 assert(ex
->connstate
== STATE_BODY
);
774 /* If we get here, we have some body to parse .. */
775 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
777 while (size
>= rec_sz
) {
778 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
782 for (o
= 0; o
< rec_sz
;) {
783 switch ((int) *(p
+ o
)) {
785 case NETDB_EX_NETWORK
:
787 /* FIXME INET6 : NetDB can still ony send IPv4 */
788 xmemcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
790 o
+= sizeof(struct in_addr
);
795 xmemcpy(&j
, p
+ o
, sizeof(int));
797 rtt
= (double) ntohl(j
) / 1000.0;
802 xmemcpy(&j
, p
+ o
, sizeof(int));
804 hops
= (double) ntohl(j
) / 1000.0;
808 debugs(38, 1, "netdbExchangeHandleReply: corrupt data, aborting");
809 netdbExchangeDone(ex
);
814 if (!addr
.IsAnyAddr() && rtt
> 0)
815 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
829 * Copy anything that is left over to the beginning of the buffer,
830 * and adjust buf_ofs accordingly
834 * Evilly, size refers to the buf size left now,
835 * ex->buf_ofs is the original buffer size, so just copy that
838 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
843 * And don't re-copy the remaining data ..
848 * Now the tricky bit - size _included_ the leftover bit from the _last_
849 * storeClientCopy. We don't want to include that, or our offset will be wrong.
850 * So, don't count the size of the leftover buffer we began with.
851 * This can _disappear_ when we're not tracking offsets ..
853 ex
->used
-= oldbufofs
;
855 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
857 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
858 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
861 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
863 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
864 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
865 netdbExchangeDone(ex
);
866 } else if (ex
->e
->store_status
== STORE_PENDING
) {
867 StoreIOBuffer tempBuffer
;
868 tempBuffer
.offset
= ex
->used
;
869 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
870 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
871 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
872 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
873 netdbExchangeHandleReply
, ex
);
878 netdbExchangeDone(void *data
)
880 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
881 debugs(38, 3, "netdbExchangeDone: " << ex
->e
->url() );
882 HTTPMSGUNLOCK(ex
->r
);
883 storeUnregister(ex
->sc
, ex
->e
, ex
);
885 cbdataReferenceDone(ex
->p
);
889 #endif /* USE_ICMP */
891 /* PUBLIC FUNCTIONS */
902 n
= hashPrime(Config
.Netdb
.high
/ 4);
904 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
906 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
908 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
910 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
918 netdbRegisterWitHCacheManager(CacheManager
& manager
)
921 manager
.registerAction("netdb",
922 "Network Measurement Database",
929 netdbPingSite(const char *hostname
)
934 if ((n
= netdbLookupHost(hostname
)) != NULL
)
935 if (n
->next_ping_time
> squid_curtime
)
938 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
939 new generic_cbdata(xstrdup(hostname
)));
945 netdbHandlePingReply(const IPAddress
&from
, int hops
, int rtt
)
950 debugs(38, 3, "netdbHandlePingReply: from " << from
);
952 if ((n
= netdbLookupAddr(from
)) == NULL
)
963 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
965 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
967 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
968 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
969 std::setw(4) << n
->hops
);
975 netdbFreeMemory(void)
978 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
979 hashFreeMemory(addr_table
);
981 hashFreeItems(host_table
, netdbFreeNameEntry
);
982 hashFreeMemory(host_table
);
984 wordlistDestroy(&peer_names
);
990 #if 0 // AYJ: Looks to be unused code.
992 netdbHops(IPAddress
&addr
)
995 netdbEntry
*n
= netdbLookupAddr(addr
);
997 if (n
&& n
->pings_recv
)
999 n
->last_use_time
= squid_curtime
;
1000 return (int) (n
->hops
+ 0.5);
1009 netdbDump(StoreEntry
* sentry
)
1019 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
1020 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1026 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1028 hash_first(addr_table
);
1030 while ((n
= (netdbEntry
*) hash_next(addr_table
)))
1033 if (i
!= memInUse(MEM_NETDBENTRY
))
1034 debugs(38, 0, "WARNING: netdb_addrs count off, found " << i
<<
1035 ", expected " << memInUse(MEM_NETDBENTRY
));
1037 qsort((char *) list
,
1039 sizeof(netdbEntry
*),
1042 for (k
= 0; k
< i
; k
++) {
1044 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1051 for (x
= n
->hosts
; x
; x
= x
->next
)
1052 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1054 storeAppendPrintf(sentry
, "\n");
1058 for (j
= 0; j
< n
->n_peers
; j
++, p
++) {
1059 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1069 storeAppendPrintf(sentry
,
1070 "NETDB support not compiled into this Squid cache.\n");
1075 netdbHostHops(const char *host
)
1078 netdbEntry
*n
= netdbLookupHost(host
);
1081 n
->last_use_time
= squid_curtime
;
1082 return (int) (n
->hops
+ 0.5);
1090 netdbHostRtt(const char *host
)
1093 netdbEntry
*n
= netdbLookupHost(host
);
1096 n
->last_use_time
= squid_curtime
;
1097 return (int) (n
->rtt
+ 0.5);
1105 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1108 netdbEntry
*n
= netdbLookupHost(host
);
1113 *samp
= n
->pings_recv
;
1115 *rtt
= (int) (n
->rtt
+ 0.5);
1117 *hops
= (int) (n
->hops
+ 0.5);
1119 n
->last_use_time
= squid_curtime
;
1125 netdbUpdatePeer(HttpRequest
* r
, peer
* e
, int irtt
, int ihops
)
1129 double rtt
= (double) irtt
;
1130 double hops
= (double) ihops
;
1132 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1133 n
= netdbLookupHost(r
->GetHost());
1136 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1140 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1141 p
= netdbPeerAdd(n
, e
);
1147 p
->expires
= squid_curtime
+ 3600;
1152 qsort((char *) n
->peers
,
1154 sizeof(net_db_peer
),
1161 netdbExchangeUpdatePeer(IPAddress
&addr
, peer
* e
, double rtt
, double hops
)
1166 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1167 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1170 if( !addr
.IsIPv4() ) {
1171 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1175 n
= netdbLookupAddr(addr
);
1182 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1183 p
= netdbPeerAdd(n
, e
);
1189 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1194 qsort((char *) n
->peers
,
1196 sizeof(net_db_peer
),
1204 netdbDeleteAddrNetwork(IPAddress
&addr
)
1207 netdbEntry
*n
= netdbLookupAddr(addr
);
1212 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1220 netdbBinaryExchange(StoreEntry
* s
)
1222 HttpReply
*reply
= new HttpReply
;
1233 struct in_addr line_addr
;
1235 HttpVersion
version(1, 0);
1236 reply
->setHeaders(version
, HTTP_OK
, "OK", NULL
, -1, squid_curtime
, -2);
1237 s
->replaceHttpReply(reply
);
1239 rec_sz
+= 1 + sizeof(struct in_addr
);
1240 rec_sz
+= 1 + sizeof(int);
1241 rec_sz
+= 1 + sizeof(int);
1242 buf
= (char *)memAllocate(MEM_4K_BUF
);
1244 hash_first(addr_table
);
1246 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1250 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1253 if (! (addr
= n
->network
) )
1256 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1257 if( !addr
.IsIPv4() )
1260 buf
[i
++] = (char) NETDB_EX_NETWORK
;
1262 addr
.GetInAddr(line_addr
);
1263 xmemcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1265 i
+= sizeof(struct in_addr
);
1267 buf
[i
++] = (char) NETDB_EX_RTT
;
1269 j
= htonl((int) (n
->rtt
* 1000));
1271 xmemcpy(&buf
[i
], &j
, sizeof(int));
1275 buf
[i
++] = (char) NETDB_EX_HOPS
;
1277 j
= htonl((int) (n
->hops
* 1000));
1279 xmemcpy(&buf
[i
], &j
, sizeof(int));
1283 if (i
+ rec_sz
> 4096) {
1296 memFree(buf
, MEM_4K_BUF
);
1299 HttpVersion
version(1,0);
1300 reply
->setHeaders(version
, HTTP_BAD_REQUEST
, "Bad Request",
1301 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 peer
*p
= (peer
*)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, 1, "netdbExchangeStart: Bad URI " << uri
);
1335 assert(NULL
!= ex
->r
);
1336 ex
->r
->http_ver
= HttpVersion(1,0);
1337 ex
->connstate
= STATE_HEADER
;
1338 ex
->e
= storeCreateEntry(uri
, uri
, request_flags(), 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
.loopdetect
= 1; /* cheat! -- force direct */
1350 xstrncpy(ex
->r
->login
, p
->login
, MAX_LOGIN_SZ
);
1352 FwdState::fwdStart(-1, ex
->e
, ex
->r
);
1358 netdbClosestParent(HttpRequest
* request
)
1363 const ipcache_addrs
*ia
;
1366 n
= netdbLookupHost(request
->GetHost());
1370 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1373 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1379 if (0 == n
->n_peers
)
1382 n
->last_use_time
= squid_curtime
;
1385 * Find the parent with the least RTT to the origin server.
1386 * Make sure we don't return a parent who is farther away than
1387 * we are. Note, the n->peers list is pre-sorted by RTT.
1389 for (i
= 0; i
< n
->n_peers
; i
++) {
1393 if (n
->rtt
< h
->rtt
)
1396 p
= peerFindByName(h
->peername
);
1398 if (NULL
== p
) /* not found */
1401 if (neighborType(p
, request
) != PEER_PARENT
)
1404 if (!peerHTTPOkay(p
, request
)) /* not allowed */