2 * Copyright (C) 1996-2017 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"
32 #include "MemObject.h"
33 #include "mgr/Registration.h"
34 #include "mime_header.h"
35 #include "neighbors.h"
36 #include "SquidConfig.h"
37 #include "SquidTime.h"
39 #include "StoreClient.h"
49 #include "icmp/IcmpSquid.h"
51 #include "StoreClient.h"
53 #define NETDB_REQBUF_SZ 4096
61 class netdbExchangeState
63 CBDATA_CLASS(netdbExchangeState
);
66 netdbExchangeState(CachePeer
*aPeer
, HttpRequest
*theReq
) :
67 p(cbdataReference(aPeer
)),
72 buf_sz(NETDB_REQBUF_SZ
),
74 connstate(STATE_HEADER
)
80 // TODO: check if we actually need to do this. should be implicit
81 r
->http_ver
= Http::ProtocolVersion();
84 ~netdbExchangeState() {
85 debugs(38, 3, e
->url());
86 storeUnregister(sc
, e
, this);
87 e
->unlock("netdbExchangeDone");
89 cbdataReferenceDone(p
);
98 char buf
[NETDB_REQBUF_SZ
];
100 netdb_conn_state_t connstate
;
103 CBDATA_CLASS_INIT(netdbExchangeState
);
105 static hash_table
*addr_table
= NULL
;
106 static hash_table
*host_table
= NULL
;
108 Ip::Address
networkFromInaddr(const Ip::Address
&a
);
109 static void netdbRelease(netdbEntry
* n
);
111 static void netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
);
112 static void netdbHashDelete(const char *key
);
113 static void netdbHostInsert(netdbEntry
* n
, const char *hostname
);
114 static void netdbHostDelete(const net_db_name
* x
);
115 static void netdbPurgeLRU(void);
116 static netdbEntry
*netdbLookupHost(const char *key
);
117 static net_db_peer
*netdbPeerByName(const netdbEntry
* n
, const char *);
118 static net_db_peer
*netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
);
119 static const char *netdbPeerName(const char *name
);
120 static IPH netdbSendPing
;
121 static FREE netdbFreeNameEntry
;
122 static FREE netdbFreeNetdbEntry
;
123 static STCB netdbExchangeHandleReply
;
125 /* We have to keep a local list of CachePeer names. The Peers structure
126 * gets freed during a reconfigure. We want this database to
127 * remain persisitent, so _net_db_peer->peername points into this
129 static wordlist
*peer_names
= NULL
;
132 netdbHashInsert(netdbEntry
* n
, Ip::Address
&addr
)
134 networkFromInaddr(addr
).toStr(n
->network
, MAX_IPSTRLEN
);
135 n
->hash
.key
= n
->network
;
136 assert(hash_lookup(addr_table
, n
->network
) == NULL
);
137 hash_join(addr_table
, &n
->hash
);
141 netdbHashDelete(const char *key
)
143 hash_link
*hptr
= (hash_link
*)hash_lookup(addr_table
, key
);
146 debug_trap("netdbHashDelete: key not found");
150 hash_remove_link(addr_table
, hptr
);
153 net_db_name::net_db_name(const char *hostname
, netdbEntry
*e
) :
154 next(e
? e
->hosts
: nullptr),
157 hash
.key
= xstrdup(hostname
);
165 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
167 net_db_name
*x
= new net_db_name(hostname
, n
);
168 assert(hash_lookup(host_table
, hostname
) == NULL
);
169 hash_join(host_table
, &x
->hash
);
173 netdbHostDelete(const net_db_name
* x
)
176 assert(x
->net_db_entry
!= NULL
);
178 netdbEntry
*n
= x
->net_db_entry
;
181 for (auto **X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
188 hash_remove_link(host_table
, (hash_link
*) x
);
193 netdbLookupHost(const char *key
)
195 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
196 return x
? x
->net_db_entry
: NULL
;
200 netdbRelease(netdbEntry
* n
)
205 for (x
= n
->hosts
; x
; x
= next
) {
214 n
->n_peers_alloc
= 0;
216 if (n
->link_count
== 0) {
217 netdbHashDelete(n
->network
);
223 netdbLRU(const void *A
, const void *B
)
225 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
226 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
228 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
231 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
245 list
= (netdbEntry
**)xcalloc(netdbEntry::UseCount(), sizeof(netdbEntry
*));
246 hash_first(addr_table
);
248 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
249 assert(list_count
< netdbEntry::UseCount());
250 *(list
+ list_count
) = n
;
256 sizeof(netdbEntry
*),
259 for (k
= 0; k
< list_count
; ++k
) {
260 if (netdbEntry::UseCount() < Config
.Netdb
.low
)
263 netdbRelease(*(list
+ k
));
272 netdbLookupAddr(const Ip::Address
&addr
)
275 char *key
= new char[MAX_IPSTRLEN
];
276 networkFromInaddr(addr
).toStr(key
,MAX_IPSTRLEN
);
277 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
283 netdbAdd(Ip::Address
&addr
)
287 if (netdbEntry::UseCount() > Config
.Netdb
.high
)
290 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
292 netdbHashInsert(n
, addr
);
299 netdbSendPing(const ipcache_addrs
*ia
, const Dns::LookupDetails
&, void *data
)
302 char *hostname
= NULL
;
303 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
314 addr
= ia
->in_addrs
[ia
->cur
];
316 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
318 netdbHostInsert(n
, hostname
);
319 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
321 *hostname moved from 'network n' to 'network na'!
327 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
329 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
332 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
337 /* remove net_db_name from 'network n' linked list */
338 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
346 /* point to 'network na' from host entry */
347 x
->net_db_entry
= na
;
348 /* link net_db_name to 'network na' */
355 if (n
->next_ping_time
<= squid_curtime
) {
356 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
357 icmpEngine
.DomainPing(addr
, hostname
);
359 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
360 n
->last_use_time
= squid_curtime
;
367 networkFromInaddr(const Ip::Address
&in
)
373 /* in IPv6 the 'network' should be the routing section. */
375 out
.applyMask(64, AF_INET6
);
376 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
385 if (IN_CLASSC(b
.s_addr
))
386 b
.s_addr
&= IN_CLASSC_NET
;
387 else if (IN_CLASSB(b
.s_addr
))
388 b
.s_addr
&= IN_CLASSB_NET
;
389 else if (IN_CLASSA(b
.s_addr
))
390 b
.s_addr
&= IN_CLASSA_NET
;
396 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
398 /* use /24 for everything under IPv4 */
399 out
.applyMask(24, AF_INET
);
400 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
406 sortByRtt(const void *A
, const void *B
)
408 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
409 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
411 if ((*n1
)->rtt
> (*n2
)->rtt
)
413 else if ((*n1
)->rtt
< (*n2
)->rtt
)
420 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
423 net_db_peer
*p
= n
->peers
;
425 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
426 if (!strcmp(p
->peername
, peername
))
434 netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
)
441 if (n
->n_peers
== n
->n_peers_alloc
) {
443 osize
= n
->n_peers_alloc
;
445 if (n
->n_peers_alloc
== 0)
446 n
->n_peers_alloc
= 2;
448 n
->n_peers_alloc
<<= 1;
450 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
452 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
454 for (i
= 0; i
< osize
; ++i
)
455 *(n
->peers
+ i
) = *(o
+ i
);
462 p
= n
->peers
+ n
->n_peers
;
463 p
->peername
= netdbPeerName(e
->host
);
469 sortPeerByRtt(const void *A
, const void *B
)
471 const net_db_peer
*p1
= (net_db_peer
*)A
;
472 const net_db_peer
*p2
= (net_db_peer
*)B
;
474 if (p1
->rtt
> p2
->rtt
)
476 else if (p1
->rtt
< p2
->rtt
)
483 netdbSaveState(void *foo
)
485 if (strcmp(Config
.netdbFilename
, "none") == 0)
492 struct timeval start
= current_time
;
495 * This was nicer when we were using stdio, but thanks to
496 * Solaris bugs, its a bad idea. fopen can fail if more than
500 * unlink() is here because there is currently no way to make
501 * logfileOpen() use O_TRUNC.
503 unlink(Config
.netdbFilename
);
504 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
508 debugs(50, DBG_IMPORTANT
, MYNAME
<< Config
.netdbFilename
<< ": " << xstrerr(xerrno
));
512 hash_first(addr_table
);
514 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
515 if (n
->pings_recv
== 0)
518 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
524 (int) n
->next_ping_time
,
525 (int) n
->last_use_time
);
527 for (x
= n
->hosts
; x
; x
= x
->next
)
528 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
530 logfilePrintf(lf
, "\n");
540 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
541 count
<< " entries, " <<
542 tvSubMsec(start
, current_time
) << " msec" );
543 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
547 netdbReloadState(void)
549 if (strcmp(Config
.netdbFilename
, "none") == 0)
563 struct timeval start
= current_time
;
565 * This was nicer when we were using stdio, but thanks to
566 * Solaris bugs, its a bad idea. fopen can fail if more than
569 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
574 if (fstat(fd
, &sb
) < 0) {
580 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
582 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
590 while ((s
= strchr(t
, '\n'))) {
594 memset(&N
, '\0', sizeof(netdbEntry
));
595 q
= strtok(t
, w_space
);
604 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
607 if ((q
= strtok(NULL
, w_space
)) == NULL
)
610 N
.pings_sent
= atoi(q
);
612 if ((q
= strtok(NULL
, w_space
)) == NULL
)
615 N
.pings_recv
= atoi(q
);
617 if (N
.pings_recv
== 0)
620 /* give this measurement low weight */
625 if ((q
= strtok(NULL
, w_space
)) == NULL
)
630 if ((q
= strtok(NULL
, w_space
)) == NULL
)
635 if ((q
= strtok(NULL
, w_space
)) == NULL
)
638 N
.next_ping_time
= (time_t) atoi(q
);
640 if ((q
= strtok(NULL
, w_space
)) == NULL
)
643 N
.last_use_time
= (time_t) atoi(q
);
647 memcpy(n
, &N
, sizeof(netdbEntry
));
649 netdbHashInsert(n
, addr
);
651 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
652 if (netdbLookupHost(q
) != NULL
) /* no dups! */
655 netdbHostInsert(n
, q
);
663 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
664 count
<< " entries, " <<
665 tvSubMsec(start
, current_time
) << " msec" );
669 netdbPeerName(const char *name
)
673 for (w
= peer_names
; w
; w
= w
->next
) {
674 if (!strcmp(w
->key
, name
))
678 return wordlistAdd(&peer_names
, name
);
682 netdbFreeNetdbEntry(void *data
)
684 netdbEntry
*n
= (netdbEntry
*)data
;
690 netdbFreeNameEntry(void *data
)
692 net_db_name
*x
= (net_db_name
*)data
;
697 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
701 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
705 struct in_addr line_addr
;
710 HttpReply
const *rep
;
714 int oldbufofs
= ex
->buf_ofs
;
717 rec_sz
+= 1 + sizeof(struct in_addr
);
718 rec_sz
+= 1 + sizeof(int);
719 rec_sz
+= 1 + sizeof(int);
720 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
722 if (!cbdataReferenceValid(ex
->p
)) {
723 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
728 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
730 if (receivedData
.length
== 0 && !receivedData
.flags
.error
) {
731 debugs(38, 3, "netdbExchangeHandleReply: Done");
738 /* Get the size of the buffer now */
739 size
= ex
->buf_ofs
+ receivedData
.length
;
740 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
742 /* Check if we're still doing headers */
744 if (ex
->connstate
== STATE_HEADER
) {
746 ex
->buf_ofs
+= receivedData
.length
;
748 /* skip reply headers */
750 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
751 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
752 rep
= ex
->e
->getReply();
753 assert(rep
->sline
.status() != Http::scNone
);
754 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status());
756 if (rep
->sline
.status() != Http::scOkay
) {
761 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
764 * Now, point p to the part of the buffer where the data
765 * starts, and update the size accordingly
767 assert(ex
->used
== 0);
769 size
= ex
->buf_ofs
- hdr_sz
;
772 /* Finally, set the conn state mode to STATE_BODY */
773 ex
->connstate
= STATE_BODY
;
775 StoreIOBuffer tempBuffer
;
776 tempBuffer
.offset
= ex
->buf_ofs
;
777 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
778 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
779 /* Have more headers .. */
780 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
781 netdbExchangeHandleReply
, ex
);
786 assert(ex
->connstate
== STATE_BODY
);
788 /* If we get here, we have some body to parse .. */
789 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
791 while (size
>= rec_sz
) {
792 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
796 for (o
= 0; o
< rec_sz
;) {
797 switch ((int) *(p
+ o
)) {
799 case NETDB_EX_NETWORK
:
801 /* FIXME INET6 : NetDB can still ony send IPv4 */
802 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
804 o
+= sizeof(struct in_addr
);
809 memcpy(&j
, p
+ o
, sizeof(int));
811 rtt
= (double) ntohl(j
) / 1000.0;
816 memcpy(&j
, p
+ o
, sizeof(int));
818 hops
= (double) ntohl(j
) / 1000.0;
822 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
828 if (!addr
.isAnyAddr() && rtt
> 0)
829 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
843 * Copy anything that is left over to the beginning of the buffer,
844 * and adjust buf_ofs accordingly
848 * Evilly, size refers to the buf size left now,
849 * ex->buf_ofs is the original buffer size, so just copy that
852 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
857 * And don't re-copy the remaining data ..
862 * Now the tricky bit - size _included_ the leftover bit from the _last_
863 * storeClientCopy. We don't want to include that, or our offset will be wrong.
864 * So, don't count the size of the leftover buffer we began with.
865 * This can _disappear_ when we're not tracking offsets ..
867 ex
->used
-= oldbufofs
;
869 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
871 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
872 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
875 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
877 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
878 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
880 } else if (ex
->e
->store_status
== STORE_PENDING
) {
881 StoreIOBuffer tempBuffer
;
882 tempBuffer
.offset
= ex
->used
;
883 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
884 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
885 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
886 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
887 netdbExchangeHandleReply
, ex
);
891 #endif /* USE_ICMP */
893 /* PUBLIC FUNCTIONS */
899 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
904 int 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 netdbDump(StoreEntry
* sentry
)
991 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
992 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
998 list
= (netdbEntry
**)xcalloc(netdbEntry::UseCount(), sizeof(netdbEntry
*));
1000 hash_first(addr_table
);
1002 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1007 if (i
!= netdbEntry::UseCount())
1008 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
1009 ", expected " << netdbEntry::UseCount());
1011 qsort((char *) list
,
1013 sizeof(netdbEntry
*),
1016 for (k
= 0; k
< i
; ++k
) {
1018 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1025 for (x
= n
->hosts
; x
; x
= x
->next
)
1026 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1028 storeAppendPrintf(sentry
, "\n");
1032 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1033 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1043 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1048 netdbHostHops(const char *host
)
1051 netdbEntry
*n
= netdbLookupHost(host
);
1054 n
->last_use_time
= squid_curtime
;
1055 return (int) (n
->hops
+ 0.5);
1063 netdbHostRtt(const char *host
)
1066 netdbEntry
*n
= netdbLookupHost(host
);
1069 n
->last_use_time
= squid_curtime
;
1070 return (int) (n
->rtt
+ 0.5);
1078 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1081 netdbEntry
*n
= netdbLookupHost(host
);
1086 *samp
= n
->pings_recv
;
1088 *rtt
= (int) (n
->rtt
+ 0.5);
1090 *hops
= (int) (n
->hops
+ 0.5);
1092 n
->last_use_time
= squid_curtime
;
1098 netdbUpdatePeer(const URL
&url
, CachePeer
* e
, int irtt
, int ihops
)
1102 double rtt
= (double) irtt
;
1103 double hops
= (double) ihops
;
1105 debugs(38, 3, url
.host() << ", " << ihops
<< " hops, " << irtt
<< " rtt");
1106 n
= netdbLookupHost(url
.host());
1109 debugs(38, 3, "host " << url
.host() << " not found");
1113 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1114 p
= netdbPeerAdd(n
, e
);
1120 p
->expires
= squid_curtime
+ 3600;
1125 qsort((char *) n
->peers
,
1127 sizeof(net_db_peer
),
1134 netdbExchangeUpdatePeer(Ip::Address
&addr
, CachePeer
* e
, double rtt
, double hops
)
1139 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1140 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1143 if ( !addr
.isIPv4() ) {
1144 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1148 n
= netdbLookupAddr(addr
);
1155 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1156 p
= netdbPeerAdd(n
, e
);
1162 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1167 qsort((char *) n
->peers
,
1169 sizeof(net_db_peer
),
1176 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1179 netdbEntry
*n
= netdbLookupAddr(addr
);
1184 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1191 netdbBinaryExchange(StoreEntry
* s
)
1193 HttpReply
*reply
= new HttpReply
;
1204 struct in_addr line_addr
;
1206 reply
->setHeaders(Http::scOkay
, "OK", NULL
, -1, squid_curtime
, -2);
1207 s
->replaceHttpReply(reply
);
1209 rec_sz
+= 1 + sizeof(struct in_addr
);
1210 rec_sz
+= 1 + sizeof(int);
1211 rec_sz
+= 1 + sizeof(int);
1212 buf
= (char *)memAllocate(MEM_4K_BUF
);
1214 hash_first(addr_table
);
1216 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1220 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1223 if (! (addr
= n
->network
) )
1226 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1227 if ( !addr
.isIPv4() )
1230 buf
[i
] = (char) NETDB_EX_NETWORK
;
1233 addr
.getInAddr(line_addr
);
1234 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1236 i
+= sizeof(struct in_addr
);
1238 buf
[i
] = (char) NETDB_EX_RTT
;
1241 j
= htonl((int) (n
->rtt
* 1000));
1243 memcpy(&buf
[i
], &j
, sizeof(int));
1247 buf
[i
] = (char) NETDB_EX_HOPS
;
1250 j
= htonl((int) (n
->hops
* 1000));
1252 memcpy(&buf
[i
], &j
, sizeof(int));
1256 if (i
+ rec_sz
> 4096) {
1269 memFree(buf
, MEM_4K_BUF
);
1272 reply
->setHeaders(Http::scBadRequest
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1273 s
->replaceHttpReply(reply
);
1274 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1281 netdbExchangeStart(void *data
)
1284 CachePeer
*p
= (CachePeer
*)data
;
1285 static const SBuf
netDB("netdb");
1286 char *uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", netDB
);
1287 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1288 assert(NULL
!= uri
);
1289 HttpRequest
*req
= HttpRequest::CreateFromUrl(uri
);
1292 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1296 netdbExchangeState
*ex
= new netdbExchangeState(p
, req
);
1297 ex
->e
= storeCreateEntry(uri
, uri
, RequestFlags(), Http::METHOD_GET
);
1298 assert(NULL
!= ex
->e
);
1300 StoreIOBuffer tempBuffer
;
1301 tempBuffer
.length
= ex
->buf_sz
;
1302 tempBuffer
.data
= ex
->buf
;
1304 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1306 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1307 netdbExchangeHandleReply
, ex
);
1308 ex
->r
->flags
.loopDetected
= true; /* cheat! -- force direct */
1310 // XXX: send as Proxy-Authenticate instead
1312 ex
->r
->url
.userInfo(SBuf(p
->login
));
1314 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1320 netdbClosestParent(HttpRequest
* request
)
1323 CachePeer
*p
= NULL
;
1325 const ipcache_addrs
*ia
;
1328 n
= netdbLookupHost(request
->url
.host());
1332 ia
= ipcache_gethostbyname(request
->url
.host(), 0);
1335 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1341 if (0 == n
->n_peers
)
1344 n
->last_use_time
= squid_curtime
;
1347 * Find the parent with the least RTT to the origin server.
1348 * Make sure we don't return a parent who is farther away than
1349 * we are. Note, the n->peers list is pre-sorted by RTT.
1351 for (i
= 0; i
< n
->n_peers
; ++i
) {
1355 if (n
->rtt
< h
->rtt
)
1358 p
= peerFindByName(h
->peername
);
1360 if (NULL
== p
) /* not found */
1363 if (neighborType(p
, request
->url
) != PEER_PARENT
)
1366 if (!peerHTTPOkay(p
, request
)) /* not allowed */