2 * Copyright (C) 1996-2015 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 "SquidTime.h"
38 #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
);
154 netdbHostInsert(netdbEntry
* n
, const char *hostname
)
156 net_db_name
*x
= (net_db_name
*)memAllocate(MEM_NET_DB_NAME
);
157 x
->hash
.key
= xstrdup(hostname
);
161 assert(hash_lookup(host_table
, hostname
) == NULL
);
162 hash_join(host_table
, &x
->hash
);
167 netdbHostDelete(const net_db_name
* x
)
172 assert(x
->net_db_entry
!= NULL
);
176 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
183 hash_remove_link(host_table
, (hash_link
*) x
);
185 memFree((void *) x
, MEM_NET_DB_NAME
);
189 netdbLookupHost(const char *key
)
191 net_db_name
*x
= (net_db_name
*) hash_lookup(host_table
, key
);
192 return x
? x
->net_db_entry
: NULL
;
196 netdbRelease(netdbEntry
* n
)
201 for (x
= n
->hosts
; x
; x
= next
) {
210 n
->n_peers_alloc
= 0;
212 if (n
->link_count
== 0) {
213 netdbHashDelete(n
->network
);
214 memFree(n
, MEM_NETDBENTRY
);
219 netdbLRU(const void *A
, const void *B
)
221 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
222 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
224 if ((*n1
)->last_use_time
> (*n2
)->last_use_time
)
227 if ((*n1
)->last_use_time
< (*n2
)->last_use_time
)
241 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
242 hash_first(addr_table
);
244 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
245 assert(list_count
< memInUse(MEM_NETDBENTRY
));
246 *(list
+ list_count
) = n
;
252 sizeof(netdbEntry
*),
255 for (k
= 0; k
< list_count
; ++k
) {
256 if (memInUse(MEM_NETDBENTRY
) < Config
.Netdb
.low
)
259 netdbRelease(*(list
+ k
));
268 netdbLookupAddr(const Ip::Address
&addr
)
271 char *key
= new char[MAX_IPSTRLEN
];
272 networkFromInaddr(addr
).toStr(key
,MAX_IPSTRLEN
);
273 n
= (netdbEntry
*) hash_lookup(addr_table
, key
);
279 netdbAdd(Ip::Address
&addr
)
283 if (memInUse(MEM_NETDBENTRY
) > Config
.Netdb
.high
)
286 if ((n
= netdbLookupAddr(addr
)) == NULL
) {
287 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
288 netdbHashInsert(n
, addr
);
295 netdbSendPing(const ipcache_addrs
*ia
, const Dns::LookupDetails
&, void *data
)
298 char *hostname
= NULL
;
299 static_cast<generic_cbdata
*>(data
)->unwrap(&hostname
);
310 addr
= ia
->in_addrs
[ia
->cur
];
312 if ((n
= netdbLookupHost(hostname
)) == NULL
) {
314 netdbHostInsert(n
, hostname
);
315 } else if ((na
= netdbLookupAddr(addr
)) != n
) {
317 *hostname moved from 'network n' to 'network na'!
323 debugs(38, 3, "netdbSendPing: " << hostname
<< " moved from " << n
->network
<< " to " << na
->network
);
325 x
= (net_db_name
*) hash_lookup(host_table
, hostname
);
328 debugs(38, DBG_IMPORTANT
, "netdbSendPing: net_db_name list bug: " << hostname
<< " not found");
333 /* remove net_db_name from 'network n' linked list */
334 for (X
= &n
->hosts
; *X
; X
= &(*X
)->next
) {
342 /* point to 'network na' from host entry */
343 x
->net_db_entry
= na
;
344 /* link net_db_name to 'network na' */
351 if (n
->next_ping_time
<= squid_curtime
) {
352 debugs(38, 3, "netdbSendPing: pinging " << hostname
);
353 icmpEngine
.DomainPing(addr
, hostname
);
355 n
->next_ping_time
= squid_curtime
+ Config
.Netdb
.period
;
356 n
->last_use_time
= squid_curtime
;
363 networkFromInaddr(const Ip::Address
&in
)
369 /* in IPv6 the 'network' should be the routing section. */
371 out
.applyMask(64, AF_INET6
);
372 debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in
<< "/64 routing part.");
381 if (IN_CLASSC(b
.s_addr
))
382 b
.s_addr
&= IN_CLASSC_NET
;
383 else if (IN_CLASSB(b
.s_addr
))
384 b
.s_addr
&= IN_CLASSB_NET
;
385 else if (IN_CLASSA(b
.s_addr
))
386 b
.s_addr
&= IN_CLASSA_NET
;
392 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out
<< "/24.");
394 /* use /24 for everything under IPv4 */
395 out
.applyMask(24, AF_INET
);
396 debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in
<< "/24.");
402 sortByRtt(const void *A
, const void *B
)
404 const netdbEntry
*const *n1
= (const netdbEntry
*const *)A
;
405 const netdbEntry
*const *n2
= (const netdbEntry
*const *)B
;
407 if ((*n1
)->rtt
> (*n2
)->rtt
)
409 else if ((*n1
)->rtt
< (*n2
)->rtt
)
416 netdbPeerByName(const netdbEntry
* n
, const char *peername
)
419 net_db_peer
*p
= n
->peers
;
421 for (i
= 0; i
< n
->n_peers
; ++i
, ++p
) {
422 if (!strcmp(p
->peername
, peername
))
430 netdbPeerAdd(netdbEntry
* n
, CachePeer
* e
)
437 if (n
->n_peers
== n
->n_peers_alloc
) {
439 osize
= n
->n_peers_alloc
;
441 if (n
->n_peers_alloc
== 0)
442 n
->n_peers_alloc
= 2;
444 n
->n_peers_alloc
<<= 1;
446 debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n
->network
<< "' to " << n
->n_peers_alloc
);
448 n
->peers
= (net_db_peer
*)xcalloc(n
->n_peers_alloc
, sizeof(net_db_peer
));
450 for (i
= 0; i
< osize
; ++i
)
451 *(n
->peers
+ i
) = *(o
+ i
);
458 p
= n
->peers
+ n
->n_peers
;
459 p
->peername
= netdbPeerName(e
->host
);
465 sortPeerByRtt(const void *A
, const void *B
)
467 const net_db_peer
*p1
= (net_db_peer
*)A
;
468 const net_db_peer
*p2
= (net_db_peer
*)B
;
470 if (p1
->rtt
> p2
->rtt
)
472 else if (p1
->rtt
< p2
->rtt
)
479 netdbSaveState(void *foo
)
481 if (strcmp(Config
.netdbFilename
, "none") == 0)
488 struct timeval start
= current_time
;
491 * This was nicer when we were using stdio, but thanks to
492 * Solaris bugs, its a bad idea. fopen can fail if more than
496 * unlink() is here because there is currently no way to make
497 * logfileOpen() use O_TRUNC.
499 unlink(Config
.netdbFilename
);
500 lf
= logfileOpen(Config
.netdbFilename
, 4096, 0);
503 debugs(50, DBG_IMPORTANT
, "netdbSaveState: " << Config
.netdbFilename
<< ": " << xstrerror());
507 hash_first(addr_table
);
509 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
510 if (n
->pings_recv
== 0)
513 logfilePrintf(lf
, "%s %d %d %10.5f %10.5f %d %d",
519 (int) n
->next_ping_time
,
520 (int) n
->last_use_time
);
522 for (x
= n
->hosts
; x
; x
= x
->next
)
523 logfilePrintf(lf
, " %s", hashKeyStr(&x
->hash
));
525 logfilePrintf(lf
, "\n");
535 debugs(38, DBG_IMPORTANT
, "NETDB state saved; " <<
536 count
<< " entries, " <<
537 tvSubMsec(start
, current_time
) << " msec" );
538 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
542 netdbReloadState(void)
544 if (strcmp(Config
.netdbFilename
, "none") == 0)
558 struct timeval start
= current_time
;
560 * This was nicer when we were using stdio, but thanks to
561 * Solaris bugs, its a bad idea. fopen can fail if more than
564 fd
= file_open(Config
.netdbFilename
, O_RDONLY
| O_BINARY
);
569 if (fstat(fd
, &sb
) < 0) {
575 char *buf
= (char *)xcalloc(1, sb
.st_size
+ 1);
577 l
= FD_READ_METHOD(fd
, buf
, sb
.st_size
);
585 while ((s
= strchr(t
, '\n'))) {
589 memset(&N
, '\0', sizeof(netdbEntry
));
590 q
= strtok(t
, w_space
);
599 if (netdbLookupAddr(addr
) != NULL
) /* no dups! */
602 if ((q
= strtok(NULL
, w_space
)) == NULL
)
605 N
.pings_sent
= atoi(q
);
607 if ((q
= strtok(NULL
, w_space
)) == NULL
)
610 N
.pings_recv
= atoi(q
);
612 if (N
.pings_recv
== 0)
615 /* give this measurement low weight */
620 if ((q
= strtok(NULL
, w_space
)) == NULL
)
625 if ((q
= strtok(NULL
, w_space
)) == NULL
)
630 if ((q
= strtok(NULL
, w_space
)) == NULL
)
633 N
.next_ping_time
= (time_t) atoi(q
);
635 if ((q
= strtok(NULL
, w_space
)) == NULL
)
638 N
.last_use_time
= (time_t) atoi(q
);
640 n
= (netdbEntry
*)memAllocate(MEM_NETDBENTRY
);
642 memcpy(n
, &N
, sizeof(netdbEntry
));
644 netdbHashInsert(n
, addr
);
646 while ((q
= strtok(NULL
, w_space
)) != NULL
) {
647 if (netdbLookupHost(q
) != NULL
) /* no dups! */
650 netdbHostInsert(n
, q
);
658 debugs(38, DBG_IMPORTANT
, "NETDB state reloaded; " <<
659 count
<< " entries, " <<
660 tvSubMsec(start
, current_time
) << " msec" );
664 netdbPeerName(const char *name
)
668 for (w
= peer_names
; w
; w
= w
->next
) {
669 if (!strcmp(w
->key
, name
))
673 return wordlistAdd(&peer_names
, name
);
677 netdbFreeNetdbEntry(void *data
)
679 netdbEntry
*n
= (netdbEntry
*)data
;
681 memFree(n
, MEM_NETDBENTRY
);
685 netdbFreeNameEntry(void *data
)
687 net_db_name
*x
= (net_db_name
*)data
;
689 memFree(x
, MEM_NET_DB_NAME
);
693 netdbExchangeHandleReply(void *data
, StoreIOBuffer receivedData
)
697 netdbExchangeState
*ex
= (netdbExchangeState
*)data
;
701 struct in_addr line_addr
;
706 HttpReply
const *rep
;
710 int oldbufofs
= ex
->buf_ofs
;
713 rec_sz
+= 1 + sizeof(struct in_addr
);
714 rec_sz
+= 1 + sizeof(int);
715 rec_sz
+= 1 + sizeof(int);
716 debugs(38, 3, "netdbExchangeHandleReply: " << receivedData
.length
<< " read bytes");
718 if (!cbdataReferenceValid(ex
->p
)) {
719 debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid");
724 debugs(38, 3, "netdbExchangeHandleReply: for '" << ex
->p
->host
<< ":" << ex
->p
->http_port
<< "'");
726 if (receivedData
.length
== 0 && !receivedData
.flags
.error
) {
727 debugs(38, 3, "netdbExchangeHandleReply: Done");
734 /* Get the size of the buffer now */
735 size
= ex
->buf_ofs
+ receivedData
.length
;
736 debugs(38, 3, "netdbExchangeHandleReply: " << size
<< " bytes buf");
738 /* Check if we're still doing headers */
740 if (ex
->connstate
== STATE_HEADER
) {
742 ex
->buf_ofs
+= receivedData
.length
;
744 /* skip reply headers */
746 if ((hdr_sz
= headersEnd(p
, ex
->buf_ofs
))) {
747 debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz
);
748 rep
= ex
->e
->getReply();
749 assert(rep
->sline
.status() != Http::scNone
);
750 debugs(38, 3, "netdbExchangeHandleReply: reply status " << rep
->sline
.status());
752 if (rep
->sline
.status() != Http::scOkay
) {
757 assert((size_t)ex
->buf_ofs
>= hdr_sz
);
760 * Now, point p to the part of the buffer where the data
761 * starts, and update the size accordingly
763 assert(ex
->used
== 0);
765 size
= ex
->buf_ofs
- hdr_sz
;
768 /* Finally, set the conn state mode to STATE_BODY */
769 ex
->connstate
= STATE_BODY
;
771 StoreIOBuffer tempBuffer
;
772 tempBuffer
.offset
= ex
->buf_ofs
;
773 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
774 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
775 /* Have more headers .. */
776 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
777 netdbExchangeHandleReply
, ex
);
782 assert(ex
->connstate
== STATE_BODY
);
784 /* If we get here, we have some body to parse .. */
785 debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size
);
787 while (size
>= rec_sz
) {
788 debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size
);
792 for (o
= 0; o
< rec_sz
;) {
793 switch ((int) *(p
+ o
)) {
795 case NETDB_EX_NETWORK
:
797 /* FIXME INET6 : NetDB can still ony send IPv4 */
798 memcpy(&line_addr
, p
+ o
, sizeof(struct in_addr
));
800 o
+= sizeof(struct in_addr
);
805 memcpy(&j
, p
+ o
, sizeof(int));
807 rtt
= (double) ntohl(j
) / 1000.0;
812 memcpy(&j
, p
+ o
, sizeof(int));
814 hops
= (double) ntohl(j
) / 1000.0;
818 debugs(38, DBG_IMPORTANT
, "netdbExchangeHandleReply: corrupt data, aborting");
824 if (!addr
.isAnyAddr() && rtt
> 0)
825 netdbExchangeUpdatePeer(addr
, ex
->p
, rtt
, hops
);
839 * Copy anything that is left over to the beginning of the buffer,
840 * and adjust buf_ofs accordingly
844 * Evilly, size refers to the buf size left now,
845 * ex->buf_ofs is the original buffer size, so just copy that
848 memmove(ex
->buf
, ex
->buf
+ (ex
->buf_ofs
- size
), size
);
853 * And don't re-copy the remaining data ..
858 * Now the tricky bit - size _included_ the leftover bit from the _last_
859 * storeClientCopy. We don't want to include that, or our offset will be wrong.
860 * So, don't count the size of the leftover buffer we began with.
861 * This can _disappear_ when we're not tracking offsets ..
863 ex
->used
-= oldbufofs
;
865 debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size
<< " bytes");
867 debugs(38, 3, "netdbExchangeHandleReply: used " << nused
<<
868 " entries, (x " << rec_sz
<< " bytes) == " << nused
* rec_sz
<<
871 debugs(38, 3, "netdbExchangeHandleReply: used " << ex
->used
);
873 if (EBIT_TEST(ex
->e
->flags
, ENTRY_ABORTED
)) {
874 debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED");
876 } else if (ex
->e
->store_status
== STORE_PENDING
) {
877 StoreIOBuffer tempBuffer
;
878 tempBuffer
.offset
= ex
->used
;
879 tempBuffer
.length
= ex
->buf_sz
- ex
->buf_ofs
;
880 tempBuffer
.data
= ex
->buf
+ ex
->buf_ofs
;
881 debugs(38, 3, "netdbExchangeHandleReply: EOF not received");
882 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
883 netdbExchangeHandleReply
, ex
);
888 netdbRegisterWithCacheManager(void)
890 Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump
, 0, 1);
893 #endif /* USE_ICMP */
895 /* PUBLIC FUNCTIONS */
903 netdbRegisterWithCacheManager();
908 n
= hashPrime(Config
.Netdb
.high
/ 4);
910 addr_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
912 n
= hashPrime(3 * Config
.Netdb
.high
/ 4);
914 host_table
= hash_create((HASHCMP
*) strcmp
, n
, hash_string
);
916 eventAddIsh("netdbSaveState", netdbSaveState
, NULL
, 3600.0, 1);
924 netdbPingSite(const char *hostname
)
929 if ((n
= netdbLookupHost(hostname
)) != NULL
)
930 if (n
->next_ping_time
> squid_curtime
)
933 ipcache_nbgethostbyname(hostname
, netdbSendPing
,
934 new generic_cbdata(xstrdup(hostname
)));
940 netdbHandlePingReply(const Ip::Address
&from
, int hops
, int rtt
)
945 debugs(38, 3, "netdbHandlePingReply: from " << from
);
947 if ((n
= netdbLookupAddr(from
)) == NULL
)
958 n
->hops
= ((n
->hops
* (N
- 1)) + hops
) / N
;
960 n
->rtt
= ((n
->rtt
* (N
- 1)) + rtt
) / N
;
962 debugs(38, 3, "netdbHandlePingReply: " << n
->network
<< "; rtt="<<
963 std::setw(5)<< std::setprecision(2) << n
->rtt
<< " hops="<<
964 std::setw(4) << n
->hops
);
970 netdbFreeMemory(void)
973 hashFreeItems(addr_table
, netdbFreeNetdbEntry
);
974 hashFreeMemory(addr_table
);
976 hashFreeItems(host_table
, netdbFreeNameEntry
);
977 hashFreeMemory(host_table
);
979 wordlistDestroy(&peer_names
);
985 netdbDump(StoreEntry
* sentry
)
995 storeAppendPrintf(sentry
, "Network DB Statistics:\n");
996 storeAppendPrintf(sentry
, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */
1002 list
= (netdbEntry
**)xcalloc(memInUse(MEM_NETDBENTRY
), sizeof(netdbEntry
*));
1004 hash_first(addr_table
);
1006 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1011 if (i
!= memInUse(MEM_NETDBENTRY
))
1012 debugs(38, DBG_CRITICAL
, "WARNING: netdb_addrs count off, found " << i
<<
1013 ", expected " << memInUse(MEM_NETDBENTRY
));
1015 qsort((char *) list
,
1017 sizeof(netdbEntry
*),
1020 for (k
= 0; k
< i
; ++k
) {
1022 storeAppendPrintf(sentry
, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */
1029 for (x
= n
->hosts
; x
; x
= x
->next
)
1030 storeAppendPrintf(sentry
, " %s", hashKeyStr(&x
->hash
));
1032 storeAppendPrintf(sentry
, "\n");
1036 for (j
= 0; j
< n
->n_peers
; ++j
, ++p
) {
1037 storeAppendPrintf(sentry
, " %-22.22s %7.1f %5.1f\n",
1047 storeAppendPrintf(sentry
,"NETDB support not compiled into this Squid cache.\n");
1052 netdbHostHops(const char *host
)
1055 netdbEntry
*n
= netdbLookupHost(host
);
1058 n
->last_use_time
= squid_curtime
;
1059 return (int) (n
->hops
+ 0.5);
1067 netdbHostRtt(const char *host
)
1070 netdbEntry
*n
= netdbLookupHost(host
);
1073 n
->last_use_time
= squid_curtime
;
1074 return (int) (n
->rtt
+ 0.5);
1082 netdbHostData(const char *host
, int *samp
, int *rtt
, int *hops
)
1085 netdbEntry
*n
= netdbLookupHost(host
);
1090 *samp
= n
->pings_recv
;
1092 *rtt
= (int) (n
->rtt
+ 0.5);
1094 *hops
= (int) (n
->hops
+ 0.5);
1096 n
->last_use_time
= squid_curtime
;
1102 netdbUpdatePeer(const URL
&url
, CachePeer
* e
, int irtt
, int ihops
)
1106 double rtt
= (double) irtt
;
1107 double hops
= (double) ihops
;
1109 debugs(38, 3, url
.host() << ", " << ihops
<< " hops, " << irtt
<< " rtt");
1110 n
= netdbLookupHost(url
.host());
1113 debugs(38, 3, "host " << url
.host() << " not found");
1117 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1118 p
= netdbPeerAdd(n
, e
);
1124 p
->expires
= squid_curtime
+ 3600;
1129 qsort((char *) n
->peers
,
1131 sizeof(net_db_peer
),
1138 netdbExchangeUpdatePeer(Ip::Address
&addr
, CachePeer
* e
, double rtt
, double hops
)
1143 debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr
<< "', "<<
1144 std::setfill('0')<< std::setprecision(2) << hops
<< " hops, " <<
1147 if ( !addr
.isIPv4() ) {
1148 debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr
<< "', NetDB cannot handle IPv6.");
1152 n
= netdbLookupAddr(addr
);
1159 if ((p
= netdbPeerByName(n
, e
->host
)) == NULL
)
1160 p
= netdbPeerAdd(n
, e
);
1166 p
->expires
= squid_curtime
+ 3600; /* XXX ? */
1171 qsort((char *) n
->peers
,
1173 sizeof(net_db_peer
),
1180 netdbDeleteAddrNetwork(Ip::Address
&addr
)
1183 netdbEntry
*n
= netdbLookupAddr(addr
);
1188 debugs(38, 3, "netdbDeleteAddrNetwork: " << n
->network
);
1195 netdbBinaryExchange(StoreEntry
* s
)
1197 HttpReply
*reply
= new HttpReply
;
1208 struct in_addr line_addr
;
1210 reply
->setHeaders(Http::scOkay
, "OK", NULL
, -1, squid_curtime
, -2);
1211 s
->replaceHttpReply(reply
);
1213 rec_sz
+= 1 + sizeof(struct in_addr
);
1214 rec_sz
+= 1 + sizeof(int);
1215 rec_sz
+= 1 + sizeof(int);
1216 buf
= (char *)memAllocate(MEM_4K_BUF
);
1218 hash_first(addr_table
);
1220 while ((n
= (netdbEntry
*) hash_next(addr_table
))) {
1224 if (n
->rtt
> 60000) /* RTT > 1 MIN probably bogus */
1227 if (! (addr
= n
->network
) )
1230 /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */
1231 if ( !addr
.isIPv4() )
1234 buf
[i
] = (char) NETDB_EX_NETWORK
;
1237 addr
.getInAddr(line_addr
);
1238 memcpy(&buf
[i
], &line_addr
, sizeof(struct in_addr
));
1240 i
+= sizeof(struct in_addr
);
1242 buf
[i
] = (char) NETDB_EX_RTT
;
1245 j
= htonl((int) (n
->rtt
* 1000));
1247 memcpy(&buf
[i
], &j
, sizeof(int));
1251 buf
[i
] = (char) NETDB_EX_HOPS
;
1254 j
= htonl((int) (n
->hops
* 1000));
1256 memcpy(&buf
[i
], &j
, sizeof(int));
1260 if (i
+ rec_sz
> 4096) {
1273 memFree(buf
, MEM_4K_BUF
);
1276 reply
->setHeaders(Http::scBadRequest
, "Bad Request", NULL
, -1, squid_curtime
, -2);
1277 s
->replaceHttpReply(reply
);
1278 storeAppendPrintf(s
, "NETDB support not compiled into this Squid cache.\n");
1285 netdbExchangeStart(void *data
)
1288 CachePeer
*p
= (CachePeer
*)data
;
1289 static const SBuf
netDB("netdb");
1290 char *uri
= internalRemoteUri(p
->host
, p
->http_port
, "/squid-internal-dynamic/", netDB
);
1291 debugs(38, 3, "netdbExchangeStart: Requesting '" << uri
<< "'");
1292 assert(NULL
!= uri
);
1293 HttpRequest
*req
= HttpRequest::CreateFromUrl(uri
);
1296 debugs(38, DBG_IMPORTANT
, "netdbExchangeStart: Bad URI " << uri
);
1300 netdbExchangeState
*ex
= new netdbExchangeState(p
, req
);
1301 ex
->e
= storeCreateEntry(uri
, uri
, RequestFlags(), Http::METHOD_GET
);
1302 assert(NULL
!= ex
->e
);
1304 StoreIOBuffer tempBuffer
;
1305 tempBuffer
.length
= ex
->buf_sz
;
1306 tempBuffer
.data
= ex
->buf
;
1308 ex
->sc
= storeClientListAdd(ex
->e
, ex
);
1310 storeClientCopy(ex
->sc
, ex
->e
, tempBuffer
,
1311 netdbExchangeHandleReply
, ex
);
1312 ex
->r
->flags
.loopDetected
= true; /* cheat! -- force direct */
1314 // XXX: send as Proxy-Authenticate instead
1316 ex
->r
->url
.userInfo(SBuf(p
->login
));
1318 FwdState::fwdStart(Comm::ConnectionPointer(), ex
->e
, ex
->r
);
1324 netdbClosestParent(HttpRequest
* request
)
1327 CachePeer
*p
= NULL
;
1329 const ipcache_addrs
*ia
;
1332 n
= netdbLookupHost(request
->url
.host());
1336 ia
= ipcache_gethostbyname(request
->url
.host(), 0);
1339 n
= netdbLookupAddr(ia
->in_addrs
[ia
->cur
]);
1345 if (0 == n
->n_peers
)
1348 n
->last_use_time
= squid_curtime
;
1351 * Find the parent with the least RTT to the origin server.
1352 * Make sure we don't return a parent who is farther away than
1353 * we are. Note, the n->peers list is pre-sorted by RTT.
1355 for (i
= 0; i
< n
->n_peers
; ++i
) {
1359 if (n
->rtt
< h
->rtt
)
1362 p
= peerFindByName(h
->peername
);
1364 if (NULL
== p
) /* not found */
1367 if (neighborType(p
, request
->url
) != PEER_PARENT
)
1370 if (!peerHTTPOkay(p
, request
)) /* not allowed */