2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 38 Network Measurement Database */
14 * This code may be slightly broken now. If you're getting consistent
15 * (sometimes working) corrupt data exchanges, please contact adrian
16 * (adrian@squid-cache.org) to sort them out.
20 #include "CachePeer.h"
26 #include "HttpReply.h"
27 #include "HttpRequest.h"
28 #include "icmp/net_db.h"
30 #include "ip/Address.h"
33 #include "MemObject.h"
34 #include "mgr/Registration.h"
35 #include "mime_header.h"
36 #include "neighbors.h"
37 #include "SquidTime.h"
39 #include "StoreClient.h"
50 #include "icmp/IcmpSquid.h"
52 #include "StoreClient.h"
54 #define NETDB_REQBUF_SZ 4096
69 char buf
[NETDB_REQBUF_SZ
];
71 netdb_conn_state_t connstate
;
74 static hash_table
*addr_table
= NULL
;
75 static hash_table
*host_table
= NULL
;
77 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
78 static void netdbRelease(netdbEntry
* n
);
80 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
81 static void netdbHashDelete(const char *key
);
82 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
83 static void netdbHostDelete(const net_db_name
* x
);
84 static void netdbPurgeLRU(void);
85 static netdbEntry
*netdbLookupHost(const char *key
);
86 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
87 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
);
88 static const char *netdbPeerName(const char *name
);
89 static IPH netdbSendPing
;
90 static QS sortPeerByRtt
;
93 static FREE netdbFreeNameEntry
;
94 static FREE netdbFreeNetdbEntry
;
95 static STCB netdbExchangeHandleReply
;
96 static void netdbExchangeDone(void *);
98 /* We have to keep a local list of CachePeer names. The Peers structure
99 * gets freed during a reconfigure. We want this database to
100 * remain persisitent, so _net_db_peer->peername points into this
102 static wordlist
*peer_names
= NULL
;
105 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
107 networkFromInaddr(addr
).toStr(n
->network
, MAX_IPSTRLEN
);
108 n
->hash
.key
= n
->network
;
109 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
110 hash_join(addr_table
, &n
->hash
);
114 netdbHashDelete(const char *key
)
116 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
119 debug_trap("netdbHashDelete: key not found");
123 hash_remove_link(addr_table
, hptr
);
127 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
129 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
130 x
->hash
.key
= xstrdup(hostname
);
134 assert(hash_lookup(host_table
, hostname
) == NULL
);
135 hash_join(host_table
, &x
->hash
);
140 netdbHostDelete(const net_db_name
* x
)
145 assert(x
->net_db_entry
!= NULL
);
149 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
156 hash_remove_link(host_table
, (hash_link
*) x
);
158 memFree((void *) x
, MEM_NET_DB_NAME
);
162 netdbLookupHost(const char *key
)
164 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
165 return x
? x
->net_db_entry
: NULL
;
169 netdbRelease(netdbEntry
* n
)
174 for (x
= n
->hosts
; x
; x
= next
) {
183 n
->n_peers_alloc
= 0;
185 if (n
->link_count
== 0) {
186 netdbHashDelete(n
->network
);
187 memFree(n
, MEM_NETDBENTRY
);
192 netdbLRU(const void *A
, const void *B
)
194 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
195 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
197 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
200 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
214 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
215 hash_first(addr_table
);
217 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
218 assert(list_count
< memInUse(MEM_NETDBENTRY
));
219 *(list
+ list_count
) = n
;
225 sizeof(netdbEntry
*),
228 for (k
= 0; k
< list_count
; ++k
) {
229 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
232 netdbRelease(*(list
+ k
));
241 netdbLookupAddr(const Ip::Address
&addr
)
244 char *key
= new char[MAX_IPSTRLEN
];
245 networkFromInaddr(addr
).toStr(key
,MAX_IPSTRLEN
);
246 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
252 netdbAdd(Ip::Address
&addr
)
256 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
259 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
260 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
261 netdbHashInsert(n
, addr
);
268 netdbSendPing(const ipcache_addrs
*ia
, const DnsLookupDetails
&, void *data
)
271 char *hostname
= NULL
;
272 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
283 addr
= ia
->in_addrs
[ia
->cur
];
285 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
287 netdbHostInsert(n
, hostname
);
288 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
290 *hostname moved from 'network n' to 'network na'!
296 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
298 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
301 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
306 /* remove net_db_name from 'network n' linked list */
307 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
315 /* point to 'network na' from host entry */
316 x
->net_db_entry
= na
;
317 /* link net_db_name to 'network na' */
324 if (n
->next_ping_time
<= squid_curtime
) {
325 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
326 icmpEngine
.DomainPing(addr
, hostname
);
328 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
329 n
->last_use_time
= squid_curtime
;
336 networkFromInaddr(const Ip::Address
&in
)
342 /* in IPv6 the 'network' should be the routing section. */
344 out
.applyMask(64, AF_INET6
);
345 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
354 if (IN_CLASSC(b
.s_addr
))
355 b
.s_addr
&= IN_CLASSC_NET
;
356 else if (IN_CLASSB(b
.s_addr
))
357 b
.s_addr
&= IN_CLASSB_NET
;
358 else if (IN_CLASSA(b
.s_addr
))
359 b
.s_addr
&= IN_CLASSA_NET
;
365 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
367 /* use /24 for everything under IPv4 */
368 out
.applyMask(24, AF_INET
);
369 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
375 sortByRtt(const void *A
, const void *B
)
377 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
378 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
380 if ((*n1
)->rtt
> (*n2
)->rtt
)
382 else if ((*n1
)->rtt
< (*n2
)->rtt
)
389 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
392 net_db_peer
*p
= n
->peers
;
394 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
395 if (!strcmp(p
->peername
, peername
))
403 netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
)
410 if (n
->n_peers
== n
->n_peers_alloc
) {
412 osize
= n
->n_peers_alloc
;
414 if (n
->n_peers_alloc
== 0)
415 n
->n_peers_alloc
= 2;
417 n
->n_peers_alloc
<<= 1;
419 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
421 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
423 for (i
= 0; i
< osize
; ++i
)
424 *(n
->peers
+ i
) = *(o
+ i
);
431 p
= n
->peers
+ n
->n_peers
;
432 p
->peername
= netdbPeerName(e
->host
);
438 sortPeerByRtt(const void *A
, const void *B
)
440 const net_db_peer
*p1
= (net_db_peer
*)A
;
441 const net_db_peer
*p2
= (net_db_peer
*)B
;
443 if (p1
->rtt
> p2
->rtt
)
445 else if (p1
->rtt
< p2
->rtt
)
452 netdbSaveState(void *foo
)
454 if (strcmp(Config
.netdbFilename
, "none") == 0)
461 struct timeval start
= current_time
;
464 * This was nicer when we were using stdio, but thanks to
465 * Solaris bugs, its a bad idea. fopen can fail if more than
469 * unlink() is here because there is currently no way to make
470 * logfileOpen() use O_TRUNC.
472 unlink(Config
.netdbFilename
);
473 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
476 debugs(50, DBG_IMPORTANT
, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
480 hash_first(addr_table
);
482 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
483 if (n
->pings_recv
== 0)
486 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
492 (int) n
->next_ping_time
,
493 (int) n
->last_use_time
);
495 for (x
= n
->hosts
; x
; x
= x
->next
)
496 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
498 logfilePrintf(lf
, "\n");
508 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
509 count
<< " entries, " <<
510 tvSubMsec(start
, current_time
) << " msec" );
511 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
515 netdbReloadState(void)
517 if (strcmp(Config
.netdbFilename
, "none") == 0)
531 struct timeval start
= current_time
;
533 * This was nicer when we were using stdio, but thanks to
534 * Solaris bugs, its a bad idea. fopen can fail if more than
537 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
542 if (fstat(fd
, &sb
) < 0) {
548 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
550 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
558 while ((s
= strchr(t
, '\n'))) {
562 memset(&N
, '\0', sizeof(netdbEntry
));
563 q
= strtok(t
, w_space
);
572 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
575 if ((q
= strtok(NULL
, w_space
)) == NULL
)
578 N
.pings_sent
= atoi(q
);
580 if ((q
= strtok(NULL
, w_space
)) == NULL
)
583 N
.pings_recv
= atoi(q
);
585 if (N
.pings_recv
== 0)
588 /* give this measurement low weight */
593 if ((q
= strtok(NULL
, w_space
)) == NULL
)
598 if ((q
= strtok(NULL
, w_space
)) == NULL
)
603 if ((q
= strtok(NULL
, w_space
)) == NULL
)
606 N
.next_ping_time
= (time_t) atoi(q
);
608 if ((q
= strtok(NULL
, w_space
)) == NULL
)
611 N
.last_use_time
= (time_t) atoi(q
);
613 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
615 memcpy(n
, &N
, sizeof(netdbEntry
));
617 netdbHashInsert(n
, addr
);
619 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
620 if (netdbLookupHost(q
) != NULL
) /* no dups! */
623 netdbHostInsert(n
, q
);
631 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
632 count
<< " entries, " <<
633 tvSubMsec(start
, current_time
) << " msec" );
637 netdbPeerName(const char *name
)
641 for (w
= peer_names
; w
; w
= w
->next
) {
642 if (!strcmp(w
->key
, name
))
646 return wordlistAdd(&peer_names
, name
);
650 netdbFreeNetdbEntry(void *data
)
652 netdbEntry
*n
= (netdbEntry
*)data
;
654 memFree(n
, MEM_NETDBENTRY
);
658 netdbFreeNameEntry(void *data
)
660 net_db_name
*x
= (net_db_name
*)data
;
662 memFree(x
, MEM_NET_DB_NAME
);
666 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
670 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
674 struct in_addr line_addr
;
679 HttpReply
const *rep
;
683 int oldbufofs
= ex
->buf_ofs
;
686 rec_sz
+= 1 + sizeof(struct in_addr
);
687 rec_sz
+= 1 + sizeof(int);
688 rec_sz
+= 1 + sizeof(int);
689 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
691 if (!cbdataReferenceValid(ex
->p
)) {
692 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
693 netdbExchangeDone(ex
);
697 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
699 if (receivedData
.length
== 0 &&
700 !receivedData
.flags
.error
) {
701 debugs(38, 3, "netdbExchangeHandleReply: Done");
702 netdbExchangeDone(ex
);
708 /* Get the size of the buffer now */
709 size
= ex
->buf_ofs
+ receivedData
.length
;
710 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
712 /* Check if we're still doing headers */
714 if (ex
->connstate
== STATE_HEADER
) {
716 ex
->buf_ofs
+= receivedData
.length
;
718 /* skip reply headers */
720 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
721 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
722 rep
= ex
->e
->getReply();
723 assert(rep
->sline
.status() != Http::scNone
);
724 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status());
726 if (rep
->sline
.status() != Http::scOkay
) {
727 netdbExchangeDone(ex
);
731 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
734 * Now, point p to the part of the buffer where the data
735 * starts, and update the size accordingly
737 assert(ex
->used
== 0);
739 size
= ex
->buf_ofs
- hdr_sz
;
742 /* Finally, set the conn state mode to STATE_BODY */
743 ex
->connstate
= STATE_BODY
;
745 StoreIOBuffer tempBuffer
;
746 tempBuffer
.offset
= ex
->buf_ofs
;
747 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
748 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
749 /* Have more headers .. */
750 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
751 netdbExchangeHandleReply
, ex
);
756 assert(ex
->connstate
== STATE_BODY
);
758 /* If we get here, we have some body to parse .. */
759 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
761 while (size
>= rec_sz
) {
762 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
766 for (o
= 0; o
< rec_sz
;) {
767 switch ((int) *(p
+ o
)) {
769 case NETDB_EX_NETWORK
:
771 /* FIXME INET6 : NetDB can still ony send IPv4 */
772 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
774 o
+= sizeof(struct in_addr
);
779 memcpy(&j
, p
+ o
, sizeof(int));
781 rtt
= (double) ntohl(j
) / 1000.0;
786 memcpy(&j
, p
+ o
, sizeof(int));
788 hops
= (double) ntohl(j
) / 1000.0;
792 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
793 netdbExchangeDone(ex
);
798 if (!addr
.isAnyAddr() && rtt
> 0)
799 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
813 * Copy anything that is left over to the beginning of the buffer,
814 * and adjust buf_ofs accordingly
818 * Evilly, size refers to the buf size left now,
819 * ex->buf_ofs is the original buffer size, so just copy that
822 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
827 * And don't re-copy the remaining data ..
832 * Now the tricky bit - size _included_ the leftover bit from the _last_
833 * storeClientCopy. We don't want to include that, or our offset will be wrong.
834 * So, don't count the size of the leftover buffer we began with.
835 * This can _disappear_ when we're not tracking offsets ..
837 ex
->used
-= oldbufofs
;
839 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
841 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
842 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
845 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
847 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
848 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
849 netdbExchangeDone(ex
);
850 } else if (ex
->e
->store_status
== STORE_PENDING
) {
851 StoreIOBuffer tempBuffer
;
852 tempBuffer
.offset
= ex
->used
;
853 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
854 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
855 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
856 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
857 netdbExchangeHandleReply
, ex
);
862 netdbExchangeDone(void *data
)
864 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
865 debugs(38, 3, "netdbExchangeDone: " << ex
->e
->url() );
866 HTTPMSGUNLOCK(ex
->r
);
867 storeUnregister(ex
->sc
, ex
->e
, ex
);
868 ex
->e
->unlock("netdbExchangeDone");
869 cbdataReferenceDone(ex
->p
);
874 netdbRegisterWithCacheManager(void)
876 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
879 #endif /* USE_ICMP */
881 /* PUBLIC FUNCTIONS */
889 netdbRegisterWithCacheManager();
894 n
= hashPrime(Config
.Netdb
.high
/ 4);
896 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
898 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
900 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
902 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
910 netdbPingSite(const char *hostname
)
915 if ((n
= netdbLookupHost(hostname
)) != NULL
)
916 if (n
->next_ping_time
> squid_curtime
)
919 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
920 new generic_cbdata(xstrdup(hostname
)));
926 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
931 debugs(38, 3, "netdbHandlePingReply: from " << from
);
933 if ((n
= netdbLookupAddr(from
)) == NULL
)
944 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
946 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
948 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
949 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
950 std::setw(4) << n
->hops
);
956 netdbFreeMemory(void)
959 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
960 hashFreeMemory(addr_table
);
962 hashFreeItems(host_table
, netdbFreeNameEntry
);
963 hashFreeMemory(host_table
);
965 wordlistDestroy(&peer_names
);
971 netdbDump(StoreEntry
* sentry
)
981 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
982 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
988 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
990 hash_first(addr_table
);
992 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
997 if (i
!= memInUse(MEM_NETDBENTRY
))
998 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
999 ", expected " << memInUse(MEM_NETDBENTRY
));
1001 qsort((char *) list
,
1003 sizeof(netdbEntry
*),
1006 for (k
= 0; k
< i
; ++k
) {
1008 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1015 for (x
= n
->hosts
; x
; x
= x
->next
)
1016 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1018 storeAppendPrintf(sentry
, "\n");
1022 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1023 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1033 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1038 netdbHostHops(const char *host
)
1041 netdbEntry
*n
= netdbLookupHost(host
);
1044 n
->last_use_time
= squid_curtime
;
1045 return (int) (n
->hops
+ 0.5);
1053 netdbHostRtt(const char *host
)
1056 netdbEntry
*n
= netdbLookupHost(host
);
1059 n
->last_use_time
= squid_curtime
;
1060 return (int) (n
->rtt
+ 0.5);
1068 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1071 netdbEntry
*n
= netdbLookupHost(host
);
1076 *samp
= n
->pings_recv
;
1078 *rtt
= (int) (n
->rtt
+ 0.5);
1080 *hops
= (int) (n
->hops
+ 0.5);
1082 n
->last_use_time
= squid_curtime
;
1088 netdbUpdatePeer(HttpRequest
* r
, CachePeer
* e
, int irtt
, int ihops
)
1092 double rtt
= (double) irtt
;
1093 double hops
= (double) ihops
;
1095 debugs(38, 3, "netdbUpdatePeer: '" << r
->GetHost() << "', " << ihops
<< " hops, " << irtt
<< " rtt");
1096 n
= netdbLookupHost(r
->GetHost());
1099 debugs(38, 3, "netdbUpdatePeer: host '" << r
->GetHost() << "' not found");
1103 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1104 p
= netdbPeerAdd(n
, e
);
1110 p
->expires
= squid_curtime
+ 3600;
1115 qsort((char *) n
->peers
,
1117 sizeof(net_db_peer
),
1124 netdbExchangeUpdatePeer(Ip::Address
&addr
, CachePeer
* e
, double rtt
, double hops
)
1129 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1130 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1133 if ( !addr
.isIPv4() ) {
1134 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1138 n
= netdbLookupAddr(addr
);
1145 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1146 p
= netdbPeerAdd(n
, e
);
1152 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1157 qsort((char *) n
->peers
,
1159 sizeof(net_db_peer
),
1166 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1169 netdbEntry
*n
= netdbLookupAddr(addr
);
1174 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1181 netdbBinaryExchange(StoreEntry
* s
)
1183 HttpReply
*reply
= new HttpReply
;
1194 struct in_addr line_addr
;
1196 reply
->setHeaders(Http::scOkay
, "OK", NULL
, -1, squid_curtime
, -2);
1197 s
->replaceHttpReply(reply
);
1199 rec_sz
+= 1 + sizeof(struct in_addr
);
1200 rec_sz
+= 1 + sizeof(int);
1201 rec_sz
+= 1 + sizeof(int);
1202 buf
= (char *)memAllocate(MEM_4K_BUF
);
1204 hash_first(addr_table
);
1206 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1210 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1213 if (! (addr
= n
->network
) )
1216 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1217 if ( !addr
.isIPv4() )
1220 buf
[i
] = (char) NETDB_EX_NETWORK
;
1223 addr
.getInAddr(line_addr
);
1224 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1226 i
+= sizeof(struct in_addr
);
1228 buf
[i
] = (char) NETDB_EX_RTT
;
1231 j
= htonl((int) (n
->rtt
* 1000));
1233 memcpy(&buf
[i
], &j
, sizeof(int));
1237 buf
[i
] = (char) NETDB_EX_HOPS
;
1240 j
= htonl((int) (n
->hops
* 1000));
1242 memcpy(&buf
[i
], &j
, sizeof(int));
1246 if (i
+ rec_sz
> 4096) {
1259 memFree(buf
, MEM_4K_BUF
);
1262 reply
->setHeaders(Http::scBadRequest
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1263 s
->replaceHttpReply(reply
);
1264 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1271 CBDATA_TYPE(netdbExchangeState
);
1275 netdbExchangeStart(void *data
)
1278 CachePeer
*p
= (CachePeer
*)data
;
1280 netdbExchangeState
*ex
;
1281 StoreIOBuffer tempBuffer
;
1282 CBDATA_INIT_TYPE(netdbExchangeState
);
1283 ex
= cbdataAlloc(netdbExchangeState
);
1284 ex
->p
= cbdataReference(p
);
1285 uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", "netdb");
1286 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1287 assert(NULL
!= uri
);
1288 ex
->r
= HttpRequest::CreateFromUrl(uri
);
1290 if (NULL
== ex
->r
) {
1291 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1296 assert(NULL
!= ex
->r
);
1297 ex
->r
->http_ver
= Http::ProtocolVersion(1,1);
1298 ex
->connstate
= STATE_HEADER
;
1299 ex
->e
= storeCreateEntry(uri
, uri
, RequestFlags(), Http::METHOD_GET
);
1300 ex
->buf_sz
= NETDB_REQBUF_SZ
;
1301 assert(NULL
!= ex
->e
);
1302 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1303 tempBuffer
.offset
= 0;
1304 tempBuffer
.length
= ex
->buf_sz
;
1305 tempBuffer
.data
= ex
->buf
;
1306 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1307 netdbExchangeHandleReply
, ex
);
1308 ex
->r
->flags
.loopDetected
= true; /* cheat! -- force direct */
1311 xstrncpy(ex
->r
->login
, p
->login
, MAX_LOGIN_SZ
);
1313 urlCanonical(ex
->r
);
1315 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1321 netdbClosestParent(HttpRequest
* request
)
1324 CachePeer
*p
= NULL
;
1326 const ipcache_addrs
*ia
;
1329 n
= netdbLookupHost(request
->GetHost());
1333 ia
= ipcache_gethostbyname(request
->GetHost(), 0);
1336 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1342 if (0 == n
->n_peers
)
1345 n
->last_use_time
= squid_curtime
;
1348 * Find the parent with the least RTT to the origin server.
1349 * Make sure we don't return a parent who is farther away than
1350 * we are. Note, the n->peers list is pre-sorted by RTT.
1352 for (i
= 0; i
< n
->n_peers
; ++i
) {
1356 if (n
->rtt
< h
->rtt
)
1359 p
= peerFindByName(h
->peername
);
1361 if (NULL
== p
) /* not found */
1364 if (neighborType(p
, request
) != PEER_PARENT
)
1367 if (!peerHTTPOkay(p
, request
)) /* not allowed */