]>
Commit | Line | Data |
---|---|---|
516350ca | 1 | /* |
77b1029d | 2 | * Copyright (C) 1996-2020 The Squid Software Foundation and contributors |
e25c139f | 3 | * |
bbc27441 AJ |
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. | |
516350ca | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 38 Network Measurement Database */ |
10 | ||
add2192d | 11 | /* |
12 | * XXX XXX XXX | |
13 | * | |
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. | |
17 | */ | |
18 | ||
582c2af2 | 19 | #include "squid.h" |
a011edee | 20 | #include "CachePeer.h" |
aa839030 | 21 | #include "cbdata.h" |
a553a5a3 | 22 | #include "event.h" |
528b2c61 | 23 | #include "fde.h" |
b3f7fd88 | 24 | #include "fs_io.h" |
eb13c21e | 25 | #include "FwdState.h" |
5bed43d6 | 26 | #include "HttpReply.h" |
5bed43d6 | 27 | #include "icmp/net_db.h" |
fc54b8d2 | 28 | #include "internal.h" |
5bed43d6 FC |
29 | #include "ip/Address.h" |
30 | #include "log/File.h" | |
5bed43d6 FC |
31 | #include "MemObject.h" |
32 | #include "mgr/Registration.h" | |
b6149797 | 33 | #include "mime_header.h" |
5bed43d6 | 34 | #include "neighbors.h" |
cb365059 | 35 | #include "PeerSelectState.h" |
2a0ab044 | 36 | #include "SquidConfig.h" |
985c86bc | 37 | #include "SquidTime.h" |
5bed43d6 | 38 | #include "Store.h" |
e87137f1 | 39 | #include "StoreClient.h" |
5bed43d6 | 40 | #include "tools.h" |
5bed43d6 | 41 | #include "wordlist.h" |
fc54b8d2 | 42 | |
582c2af2 FC |
43 | #if HAVE_SYS_STAT_H |
44 | #include <sys/stat.h> | |
45 | #endif | |
46 | ||
a97cfa48 | 47 | #if USE_ICMP |
9b5c4a9a | 48 | #include "icmp/IcmpSquid.h" |
714e68b7 | 49 | #include "ipcache.h" |
4b725156 | 50 | #include "StoreClient.h" |
51 | ||
f53969cc | 52 | #define NETDB_REQBUF_SZ 4096 |
add2192d | 53 | |
54 | typedef enum { | |
fa80a8ef | 55 | STATE_NONE, |
56 | STATE_HEADER, | |
57 | STATE_BODY | |
add2192d | 58 | } netdb_conn_state_t; |
8b833697 | 59 | |
fa91d030 AJ |
60 | class netdbExchangeState |
61 | { | |
62 | CBDATA_CLASS(netdbExchangeState); | |
63 | ||
64 | public: | |
891a70f1 | 65 | netdbExchangeState(CachePeer *aPeer, const HttpRequestPointer &theReq) : |
66 | p(aPeer), | |
67 | r(theReq) | |
fa91d030 AJ |
68 | { |
69 | *buf = 0; | |
891a70f1 | 70 | assert(r); |
fa91d030 AJ |
71 | // TODO: check if we actually need to do this. should be implicit |
72 | r->http_ver = Http::ProtocolVersion(); | |
73 | } | |
74 | ||
75 | ~netdbExchangeState() { | |
76 | debugs(38, 3, e->url()); | |
77 | storeUnregister(sc, e, this); | |
78 | e->unlock("netdbExchangeDone"); | |
fa91d030 AJ |
79 | } |
80 | ||
891a70f1 | 81 | CbcPointer<CachePeer> p; |
82 | StoreEntry *e = nullptr; | |
83 | store_client *sc = nullptr; | |
84 | HttpRequestPointer r; | |
85 | int64_t used = 0; | |
86 | size_t buf_sz = NETDB_REQBUF_SZ; | |
add2192d | 87 | char buf[NETDB_REQBUF_SZ]; |
891a70f1 | 88 | int buf_ofs = 0; |
89 | netdb_conn_state_t connstate = STATE_HEADER; | |
fa91d030 AJ |
90 | }; |
91 | ||
92 | CBDATA_CLASS_INIT(netdbExchangeState); | |
9ad1cbca | 93 | |
ce75f381 | 94 | static hash_table *addr_table = NULL; |
365e5b34 | 95 | static hash_table *host_table = NULL; |
87801fcb | 96 | |
b7ac5457 | 97 | Ip::Address networkFromInaddr(const Ip::Address &a); |
f5b8bbc4 | 98 | static void netdbRelease(netdbEntry * n); |
62e76326 | 99 | |
b7ac5457 | 100 | static void netdbHashInsert(netdbEntry * n, Ip::Address &addr); |
f5b8bbc4 | 101 | static void netdbHashDelete(const char *key); |
587d8445 | 102 | static void netdbHostInsert(netdbEntry * n, const char *hostname); |
103 | static void netdbHostDelete(const net_db_name * x); | |
f5b8bbc4 | 104 | static void netdbPurgeLRU(void); |
23d92c64 | 105 | static netdbEntry *netdbLookupHost(const char *key); |
f5b8bbc4 | 106 | static net_db_peer *netdbPeerByName(const netdbEntry * n, const char *); |
a3c6762c | 107 | static net_db_peer *netdbPeerAdd(netdbEntry * n, CachePeer * e); |
858783c9 | 108 | static const char *netdbPeerName(const char *name); |
b69f7771 | 109 | static IPH netdbSendPing; |
ec878047 | 110 | static FREE netdbFreeNameEntry; |
111 | static FREE netdbFreeNetdbEntry; | |
9ad1cbca | 112 | static STCB netdbExchangeHandleReply; |
429fdbec | 113 | |
a3c6762c | 114 | /* We have to keep a local list of CachePeer names. The Peers structure |
429fdbec | 115 | * gets freed during a reconfigure. We want this database to |
116 | * remain persisitent, so _net_db_peer->peername points into this | |
117 | * linked list */ | |
118 | static wordlist *peer_names = NULL; | |
67508012 | 119 | |
87801fcb | 120 | static void |
b7ac5457 | 121 | netdbHashInsert(netdbEntry * n, Ip::Address &addr) |
87801fcb | 122 | { |
4dd643d5 | 123 | networkFromInaddr(addr).toStr(n->network, MAX_IPSTRLEN); |
4ec218d5 | 124 | n->key = n->network; |
587d8445 | 125 | assert(hash_lookup(addr_table, n->network) == NULL); |
4ec218d5 | 126 | hash_join(addr_table, n); |
87801fcb | 127 | } |
128 | ||
129 | static void | |
0ee4272b | 130 | netdbHashDelete(const char *key) |
87801fcb | 131 | { |
e6ccf245 | 132 | hash_link *hptr = (hash_link *)hash_lookup(addr_table, key); |
62e76326 | 133 | |
87801fcb | 134 | if (hptr == NULL) { |
62e76326 | 135 | debug_trap("netdbHashDelete: key not found"); |
136 | return; | |
87801fcb | 137 | } |
62e76326 | 138 | |
e0b1c6aa | 139 | hash_remove_link(addr_table, hptr); |
67508012 | 140 | } |
141 | ||
3c670b50 AJ |
142 | net_db_name::net_db_name(const char *hostname, netdbEntry *e) : |
143 | next(e ? e->hosts : nullptr), | |
144 | net_db_entry(e) | |
145 | { | |
4ec218d5 | 146 | key = xstrdup(hostname); |
3c670b50 AJ |
147 | if (e) { |
148 | e->hosts = this; | |
149 | ++ e->link_count; | |
150 | } | |
151 | } | |
152 | ||
67508012 | 153 | static void |
587d8445 | 154 | netdbHostInsert(netdbEntry * n, const char *hostname) |
67508012 | 155 | { |
3c670b50 | 156 | net_db_name *x = new net_db_name(hostname, n); |
587d8445 | 157 | assert(hash_lookup(host_table, hostname) == NULL); |
4ec218d5 | 158 | hash_join(host_table, x); |
67508012 | 159 | } |
160 | ||
161 | static void | |
587d8445 | 162 | netdbHostDelete(const net_db_name * x) |
67508012 | 163 | { |
b19361fd | 164 | assert(x != NULL); |
587d8445 | 165 | assert(x->net_db_entry != NULL); |
3c670b50 AJ |
166 | |
167 | netdbEntry *n = x->net_db_entry; | |
7c64cac7 | 168 | -- n->link_count; |
62e76326 | 169 | |
3c670b50 | 170 | for (auto **X = &n->hosts; *X; X = &(*X)->next) { |
62e76326 | 171 | if (*X == x) { |
172 | *X = x->next; | |
173 | break; | |
174 | } | |
587d8445 | 175 | } |
62e76326 | 176 | |
587d8445 | 177 | hash_remove_link(host_table, (hash_link *) x); |
3c670b50 | 178 | delete x; |
87801fcb | 179 | } |
180 | ||
181 | static netdbEntry * | |
0ee4272b | 182 | netdbLookupHost(const char *key) |
87801fcb | 183 | { |
587d8445 | 184 | net_db_name *x = (net_db_name *) hash_lookup(host_table, key); |
185 | return x ? x->net_db_entry : NULL; | |
87801fcb | 186 | } |
187 | ||
c907000b | 188 | static void |
189 | netdbRelease(netdbEntry * n) | |
190 | { | |
429fdbec | 191 | net_db_name *x; |
587d8445 | 192 | net_db_name *next; |
62e76326 | 193 | |
c907000b | 194 | for (x = n->hosts; x; x = next) { |
62e76326 | 195 | next = x->next; |
196 | netdbHostDelete(x); | |
c907000b | 197 | } |
62e76326 | 198 | |
c907000b | 199 | n->hosts = NULL; |
429fdbec | 200 | safe_free(n->peers); |
429fdbec | 201 | n->peers = NULL; |
202 | n->n_peers = 0; | |
203 | n->n_peers_alloc = 0; | |
62e76326 | 204 | |
c907000b | 205 | if (n->link_count == 0) { |
62e76326 | 206 | netdbHashDelete(n->network); |
1a7cfe02 | 207 | delete n; |
c907000b | 208 | } |
209 | } | |
210 | ||
211 | static int | |
79d39a72 | 212 | netdbLRU(const void *A, const void *B) |
c907000b | 213 | { |
e6ccf245 | 214 | const netdbEntry *const *n1 = (const netdbEntry *const *)A; |
215 | const netdbEntry *const *n2 = (const netdbEntry *const *)B; | |
62e76326 | 216 | |
c907000b | 217 | if ((*n1)->last_use_time > (*n2)->last_use_time) |
62e76326 | 218 | return (1); |
219 | ||
c907000b | 220 | if ((*n1)->last_use_time < (*n2)->last_use_time) |
62e76326 | 221 | return (-1); |
222 | ||
c907000b | 223 | return (0); |
224 | } | |
225 | ||
c907000b | 226 | static void |
227 | netdbPurgeLRU(void) | |
228 | { | |
229 | netdbEntry *n; | |
230 | netdbEntry **list; | |
231 | int k = 0; | |
232 | int list_count = 0; | |
233 | int removed = 0; | |
1a7cfe02 | 234 | list = (netdbEntry **)xcalloc(netdbEntry::UseCount(), sizeof(netdbEntry *)); |
0f6bebac | 235 | hash_first(addr_table); |
62e76326 | 236 | |
0f6bebac | 237 | while ((n = (netdbEntry *) hash_next(addr_table))) { |
1a7cfe02 | 238 | assert(list_count < netdbEntry::UseCount()); |
62e76326 | 239 | *(list + list_count) = n; |
7c64cac7 | 240 | ++list_count; |
c907000b | 241 | } |
62e76326 | 242 | |
c907000b | 243 | qsort((char *) list, |
62e76326 | 244 | list_count, |
245 | sizeof(netdbEntry *), | |
246 | netdbLRU); | |
247 | ||
7c64cac7 | 248 | for (k = 0; k < list_count; ++k) { |
1a7cfe02 | 249 | if (netdbEntry::UseCount() < Config.Netdb.low) |
62e76326 | 250 | break; |
251 | ||
252 | netdbRelease(*(list + k)); | |
253 | ||
7c64cac7 | 254 | ++removed; |
c907000b | 255 | } |
62e76326 | 256 | |
c907000b | 257 | xfree(list); |
258 | } | |
259 | ||
67508012 | 260 | static netdbEntry * |
b7ac5457 | 261 | netdbLookupAddr(const Ip::Address &addr) |
87801fcb | 262 | { |
587d8445 | 263 | netdbEntry *n; |
cc192b50 | 264 | char *key = new char[MAX_IPSTRLEN]; |
4dd643d5 | 265 | networkFromInaddr(addr).toStr(key,MAX_IPSTRLEN); |
587d8445 | 266 | n = (netdbEntry *) hash_lookup(addr_table, key); |
4a7799f9 | 267 | delete[] key; |
587d8445 | 268 | return n; |
87801fcb | 269 | } |
270 | ||
67508012 | 271 | static netdbEntry * |
b7ac5457 | 272 | netdbAdd(Ip::Address &addr) |
87801fcb | 273 | { |
274 | netdbEntry *n; | |
62e76326 | 275 | |
1a7cfe02 | 276 | if (netdbEntry::UseCount() > Config.Netdb.high) |
62e76326 | 277 | netdbPurgeLRU(); |
278 | ||
26ac0430 | 279 | if ((n = netdbLookupAddr(addr)) == NULL) { |
1a7cfe02 | 280 | n = new netdbEntry; |
62e76326 | 281 | netdbHashInsert(n, addr); |
87801fcb | 282 | } |
62e76326 | 283 | |
67508012 | 284 | return n; |
87801fcb | 285 | } |
286 | ||
287 | static void | |
4a3b98d7 | 288 | netdbSendPing(const ipcache_addrs *ia, const Dns::LookupDetails &, void *data) |
87801fcb | 289 | { |
b7ac5457 | 290 | Ip::Address addr; |
2eaf5ca0 | 291 | char *hostname = NULL; |
aa839030 | 292 | static_cast<generic_cbdata *>(data)->unwrap(&hostname); |
87801fcb | 293 | netdbEntry *n; |
b19361fd | 294 | netdbEntry *na; |
587d8445 | 295 | net_db_name *x; |
296 | net_db_name **X; | |
62e76326 | 297 | |
e5f6c5c2 | 298 | if (ia == NULL) { |
62e76326 | 299 | xfree(hostname); |
300 | return; | |
cb190ed7 | 301 | } |
62e76326 | 302 | |
fd9c47d1 | 303 | addr = ia->current(); |
62e76326 | 304 | |
b19361fd | 305 | if ((n = netdbLookupHost(hostname)) == NULL) { |
62e76326 | 306 | n = netdbAdd(addr); |
307 | netdbHostInsert(n, hostname); | |
b19361fd | 308 | } else if ((na = netdbLookupAddr(addr)) != n) { |
62e76326 | 309 | /* |
310 | *hostname moved from 'network n' to 'network na'! | |
311 | */ | |
312 | ||
313 | if (na == NULL) | |
314 | na = netdbAdd(addr); | |
315 | ||
bf8fe701 | 316 | debugs(38, 3, "netdbSendPing: " << hostname << " moved from " << n->network << " to " << na->network); |
62e76326 | 317 | |
318 | x = (net_db_name *) hash_lookup(host_table, hostname); | |
319 | ||
320 | if (x == NULL) { | |
e0236918 | 321 | debugs(38, DBG_IMPORTANT, "netdbSendPing: net_db_name list bug: " << hostname << " not found"); |
62e76326 | 322 | xfree(hostname); |
323 | return; | |
324 | } | |
325 | ||
326 | /* remove net_db_name from 'network n' linked list */ | |
327 | for (X = &n->hosts; *X; X = &(*X)->next) { | |
328 | if (*X == x) { | |
329 | *X = x->next; | |
330 | break; | |
331 | } | |
332 | } | |
333 | ||
7c64cac7 | 334 | -- n->link_count; |
62e76326 | 335 | /* point to 'network na' from host entry */ |
336 | x->net_db_entry = na; | |
337 | /* link net_db_name to 'network na' */ | |
338 | x->next = na->hosts; | |
339 | na->hosts = x; | |
7c64cac7 | 340 | ++ na->link_count; |
62e76326 | 341 | n = na; |
b19361fd | 342 | } |
62e76326 | 343 | |
674ac814 | 344 | if (n->next_ping_time <= squid_curtime) { |
bf8fe701 | 345 | debugs(38, 3, "netdbSendPing: pinging " << hostname); |
cc192b50 | 346 | icmpEngine.DomainPing(addr, hostname); |
7c64cac7 | 347 | ++ n->pings_sent; |
62e76326 | 348 | n->next_ping_time = squid_curtime + Config.Netdb.period; |
349 | n->last_use_time = squid_curtime; | |
674ac814 | 350 | } |
62e76326 | 351 | |
28c60158 | 352 | xfree(hostname); |
67508012 | 353 | } |
354 | ||
b7ac5457 AJ |
355 | Ip::Address |
356 | networkFromInaddr(const Ip::Address &in) | |
67508012 | 357 | { |
b7ac5457 | 358 | Ip::Address out; |
cc192b50 | 359 | |
360 | out = in; | |
cc192b50 | 361 | |
362 | /* in IPv6 the 'network' should be the routing section. */ | |
4dd643d5 AJ |
363 | if ( in.isIPv6() ) { |
364 | out.applyMask(64, AF_INET6); | |
cc192b50 | 365 | debugs(14, 5, "networkFromInaddr : Masked IPv6 Address to " << in << "/64 routing part."); |
366 | return out; | |
367 | } | |
62e76326 | 368 | |
429fdbec | 369 | #if USE_CLASSFUL |
cc192b50 | 370 | struct in_addr b; |
371 | ||
4dd643d5 | 372 | in.getInAddr(b); |
62e76326 | 373 | |
67508012 | 374 | if (IN_CLASSC(b.s_addr)) |
62e76326 | 375 | b.s_addr &= IN_CLASSC_NET; |
67508012 | 376 | else if (IN_CLASSB(b.s_addr)) |
62e76326 | 377 | b.s_addr &= IN_CLASSB_NET; |
67508012 | 378 | else if (IN_CLASSA(b.s_addr)) |
62e76326 | 379 | b.s_addr &= IN_CLASSA_NET; |
380 | ||
cc192b50 | 381 | out = b; |
62e76326 | 382 | |
429fdbec | 383 | #endif |
62e76326 | 384 | |
cc192b50 | 385 | debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << out << "/24."); |
62e76326 | 386 | |
cc192b50 | 387 | /* use /24 for everything under IPv4 */ |
4dd643d5 | 388 | out.applyMask(24, AF_INET); |
cc192b50 | 389 | debugs(14, 5, "networkFromInaddr : Masked IPv4 Address to " << in << "/24."); |
390 | ||
391 | return out; | |
67508012 | 392 | } |
393 | ||
394 | static int | |
79d39a72 | 395 | sortByRtt(const void *A, const void *B) |
67508012 | 396 | { |
e6ccf245 | 397 | const netdbEntry *const *n1 = (const netdbEntry *const *)A; |
398 | const netdbEntry *const *n2 = (const netdbEntry *const *)B; | |
62e76326 | 399 | |
429fdbec | 400 | if ((*n1)->rtt > (*n2)->rtt) |
62e76326 | 401 | return 1; |
429fdbec | 402 | else if ((*n1)->rtt < (*n2)->rtt) |
62e76326 | 403 | return -1; |
67508012 | 404 | else |
62e76326 | 405 | return 0; |
67508012 | 406 | } |
407 | ||
429fdbec | 408 | static net_db_peer * |
409 | netdbPeerByName(const netdbEntry * n, const char *peername) | |
410 | { | |
411 | int i; | |
412 | net_db_peer *p = n->peers; | |
62e76326 | 413 | |
7c64cac7 | 414 | for (i = 0; i < n->n_peers; ++i, ++p) { |
62e76326 | 415 | if (!strcmp(p->peername, peername)) |
416 | return p; | |
429fdbec | 417 | } |
62e76326 | 418 | |
429fdbec | 419 | return NULL; |
420 | } | |
421 | ||
422 | static net_db_peer * | |
a3c6762c | 423 | netdbPeerAdd(netdbEntry * n, CachePeer * e) |
429fdbec | 424 | { |
425 | net_db_peer *p; | |
426 | net_db_peer *o; | |
427 | int osize; | |
428 | int i; | |
62e76326 | 429 | |
429fdbec | 430 | if (n->n_peers == n->n_peers_alloc) { |
62e76326 | 431 | o = n->peers; |
432 | osize = n->n_peers_alloc; | |
433 | ||
434 | if (n->n_peers_alloc == 0) | |
435 | n->n_peers_alloc = 2; | |
436 | else | |
437 | n->n_peers_alloc <<= 1; | |
438 | ||
bf8fe701 | 439 | debugs(38, 3, "netdbPeerAdd: Growing peer list for '" << n->network << "' to " << n->n_peers_alloc); |
62e76326 | 440 | |
441 | n->peers = (net_db_peer *)xcalloc(n->n_peers_alloc, sizeof(net_db_peer)); | |
442 | ||
7c64cac7 | 443 | for (i = 0; i < osize; ++i) |
62e76326 | 444 | *(n->peers + i) = *(o + i); |
445 | ||
446 | if (osize) { | |
447 | safe_free(o); | |
448 | } | |
429fdbec | 449 | } |
62e76326 | 450 | |
429fdbec | 451 | p = n->peers + n->n_peers; |
452 | p->peername = netdbPeerName(e->host); | |
7c64cac7 | 453 | ++ n->n_peers; |
429fdbec | 454 | return p; |
455 | } | |
456 | ||
457 | static int | |
79d39a72 | 458 | sortPeerByRtt(const void *A, const void *B) |
429fdbec | 459 | { |
e6ccf245 | 460 | const net_db_peer *p1 = (net_db_peer *)A; |
461 | const net_db_peer *p2 = (net_db_peer *)B; | |
62e76326 | 462 | |
429fdbec | 463 | if (p1->rtt > p2->rtt) |
62e76326 | 464 | return 1; |
429fdbec | 465 | else if (p1->rtt < p2->rtt) |
62e76326 | 466 | return -1; |
429fdbec | 467 | else |
62e76326 | 468 | return 0; |
429fdbec | 469 | } |
470 | ||
471 | static void | |
472 | netdbSaveState(void *foo) | |
473 | { | |
b7ed6dbb | 474 | if (strcmp(Config.netdbFilename, "none") == 0) |
26ac0430 | 475 | return; |
2b753521 | 476 | |
08e8e020 | 477 | Logfile *lf; |
429fdbec | 478 | netdbEntry *n; |
429fdbec | 479 | net_db_name *x; |
62e76326 | 480 | |
429fdbec | 481 | struct timeval start = current_time; |
482 | int count = 0; | |
111b16cf | 483 | /* |
484 | * This was nicer when we were using stdio, but thanks to | |
485 | * Solaris bugs, its a bad idea. fopen can fail if more than | |
486 | * 256 FDs are open. | |
487 | */ | |
e3732f4f | 488 | /* |
489 | * unlink() is here because there is currently no way to make | |
490 | * logfileOpen() use O_TRUNC. | |
491 | */ | |
2b753521 | 492 | unlink(Config.netdbFilename); |
493 | lf = logfileOpen(Config.netdbFilename, 4096, 0); | |
62e76326 | 494 | |
16cd62c7 | 495 | if (!lf) { |
b69e9ffa AJ |
496 | int xerrno = errno; |
497 | debugs(50, DBG_IMPORTANT, MYNAME << Config.netdbFilename << ": " << xstrerr(xerrno)); | |
62e76326 | 498 | return; |
429fdbec | 499 | } |
62e76326 | 500 | |
0f6bebac | 501 | hash_first(addr_table); |
62e76326 | 502 | |
0f6bebac | 503 | while ((n = (netdbEntry *) hash_next(addr_table))) { |
62e76326 | 504 | if (n->pings_recv == 0) |
505 | continue; | |
506 | ||
507 | logfilePrintf(lf, "%s %d %d %10.5f %10.5f %d %d", | |
508 | n->network, | |
509 | n->pings_sent, | |
510 | n->pings_recv, | |
511 | n->hops, | |
512 | n->rtt, | |
513 | (int) n->next_ping_time, | |
514 | (int) n->last_use_time); | |
515 | ||
516 | for (x = n->hosts; x; x = x->next) | |
4ec218d5 | 517 | logfilePrintf(lf, " %s", hashKeyStr(x)); |
62e76326 | 518 | |
519 | logfilePrintf(lf, "\n"); | |
520 | ||
7c64cac7 | 521 | ++count; |
62e76326 | 522 | |
111b16cf | 523 | #undef RBUF_SZ |
62e76326 | 524 | |
429fdbec | 525 | } |
62e76326 | 526 | |
08e8e020 | 527 | logfileClose(lf); |
587d8445 | 528 | getCurrentTime(); |
e0236918 | 529 | debugs(38, DBG_IMPORTANT, "NETDB state saved; " << |
26ac0430 AJ |
530 | count << " entries, " << |
531 | tvSubMsec(start, current_time) << " msec" ); | |
52040193 | 532 | eventAddIsh("netdbSaveState", netdbSaveState, NULL, 3600.0, 1); |
429fdbec | 533 | } |
534 | ||
535 | static void | |
536 | netdbReloadState(void) | |
537 | { | |
b7ed6dbb | 538 | if (strcmp(Config.netdbFilename, "none") == 0) |
26ac0430 | 539 | return; |
2b753521 | 540 | |
111b16cf | 541 | char *s; |
542 | int fd; | |
543 | int l; | |
62e76326 | 544 | |
111b16cf | 545 | struct stat sb; |
429fdbec | 546 | netdbEntry *n; |
547 | netdbEntry N; | |
62e76326 | 548 | |
b7ac5457 | 549 | Ip::Address addr; |
429fdbec | 550 | int count = 0; |
62e76326 | 551 | |
429fdbec | 552 | struct timeval start = current_time; |
111b16cf | 553 | /* |
554 | * This was nicer when we were using stdio, but thanks to | |
555 | * Solaris bugs, its a bad idea. fopen can fail if more than | |
556 | * 256 FDs are open. | |
557 | */ | |
2b753521 | 558 | fd = file_open(Config.netdbFilename, O_RDONLY | O_BINARY); |
62e76326 | 559 | |
111b16cf | 560 | if (fd < 0) |
62e76326 | 561 | return; |
562 | ||
053ea9f4 | 563 | if (fstat(fd, &sb) < 0) { |
62e76326 | 564 | file_close(fd); |
565 | return; | |
053ea9f4 | 566 | } |
62e76326 | 567 | |
e0bbe966 | 568 | char *t; |
569 | char *buf = (char *)xcalloc(1, sb.st_size + 1); | |
570 | t = buf; | |
d0ef8ea8 | 571 | l = FD_READ_METHOD(fd, buf, sb.st_size); |
111b16cf | 572 | file_close(fd); |
62e76326 | 573 | |
e0bbe966 | 574 | if (l <= 0) { |
62e76326 | 575 | safe_free (buf); |
576 | return; | |
e0bbe966 | 577 | }; |
62e76326 | 578 | |
111b16cf | 579 | while ((s = strchr(t, '\n'))) { |
62e76326 | 580 | char *q; |
581 | assert(s - buf < l); | |
582 | *s = '\0'; | |
b56b37cf | 583 | N = netdbEntry(); |
62e76326 | 584 | q = strtok(t, w_space); |
585 | t = s + 1; | |
586 | ||
587 | if (NULL == q) | |
588 | continue; | |
589 | ||
cc192b50 | 590 | if (! (addr = q) ) |
62e76326 | 591 | continue; |
592 | ||
f53969cc | 593 | if (netdbLookupAddr(addr) != NULL) /* no dups! */ |
62e76326 | 594 | continue; |
595 | ||
596 | if ((q = strtok(NULL, w_space)) == NULL) | |
597 | continue; | |
598 | ||
599 | N.pings_sent = atoi(q); | |
600 | ||
601 | if ((q = strtok(NULL, w_space)) == NULL) | |
602 | continue; | |
603 | ||
604 | N.pings_recv = atoi(q); | |
605 | ||
606 | if (N.pings_recv == 0) | |
607 | continue; | |
608 | ||
609 | /* give this measurement low weight */ | |
610 | N.pings_sent = 1; | |
611 | ||
612 | N.pings_recv = 1; | |
613 | ||
614 | if ((q = strtok(NULL, w_space)) == NULL) | |
615 | continue; | |
616 | ||
617 | N.hops = atof(q); | |
618 | ||
619 | if ((q = strtok(NULL, w_space)) == NULL) | |
620 | continue; | |
621 | ||
622 | N.rtt = atof(q); | |
623 | ||
624 | if ((q = strtok(NULL, w_space)) == NULL) | |
625 | continue; | |
626 | ||
627 | N.next_ping_time = (time_t) atoi(q); | |
628 | ||
629 | if ((q = strtok(NULL, w_space)) == NULL) | |
630 | continue; | |
631 | ||
632 | N.last_use_time = (time_t) atoi(q); | |
633 | ||
1a7cfe02 | 634 | n = new netdbEntry; |
62e76326 | 635 | |
41d00cd3 | 636 | memcpy(n, &N, sizeof(netdbEntry)); |
62e76326 | 637 | |
638 | netdbHashInsert(n, addr); | |
639 | ||
640 | while ((q = strtok(NULL, w_space)) != NULL) { | |
f53969cc | 641 | if (netdbLookupHost(q) != NULL) /* no dups! */ |
62e76326 | 642 | continue; |
643 | ||
644 | netdbHostInsert(n, q); | |
645 | } | |
646 | ||
7c64cac7 | 647 | ++count; |
429fdbec | 648 | } |
62e76326 | 649 | |
111b16cf | 650 | xfree(buf); |
587d8445 | 651 | getCurrentTime(); |
e0236918 | 652 | debugs(38, DBG_IMPORTANT, "NETDB state reloaded; " << |
26ac0430 AJ |
653 | count << " entries, " << |
654 | tvSubMsec(start, current_time) << " msec" ); | |
429fdbec | 655 | } |
656 | ||
858783c9 | 657 | static const char * |
429fdbec | 658 | netdbPeerName(const char *name) |
659 | { | |
858783c9 | 660 | const wordlist *w; |
62e76326 | 661 | |
429fdbec | 662 | for (w = peer_names; w; w = w->next) { |
62e76326 | 663 | if (!strcmp(w->key, name)) |
664 | return w->key; | |
429fdbec | 665 | } |
62e76326 | 666 | |
858783c9 | 667 | return wordlistAdd(&peer_names, name); |
429fdbec | 668 | } |
669 | ||
45c54bdc | 670 | static void |
671 | netdbFreeNetdbEntry(void *data) | |
672 | { | |
e6ccf245 | 673 | netdbEntry *n = (netdbEntry *)data; |
45c54bdc | 674 | safe_free(n->peers); |
1a7cfe02 | 675 | delete n; |
45c54bdc | 676 | } |
429fdbec | 677 | |
45c54bdc | 678 | static void |
679 | netdbFreeNameEntry(void *data) | |
680 | { | |
e6ccf245 | 681 | net_db_name *x = (net_db_name *)data; |
3c670b50 | 682 | delete x; |
45c54bdc | 683 | } |
684 | ||
685 | static void | |
2324cda2 | 686 | netdbExchangeHandleReply(void *data, StoreIOBuffer receivedData) |
45c54bdc | 687 | { |
b7ac5457 | 688 | Ip::Address addr; |
cc192b50 | 689 | |
e6ccf245 | 690 | netdbExchangeState *ex = (netdbExchangeState *)data; |
45c54bdc | 691 | int rec_sz = 0; |
57d55dfa | 692 | int o; |
62e76326 | 693 | |
cc192b50 | 694 | struct in_addr line_addr; |
45c54bdc | 695 | double rtt; |
696 | double hops; | |
697 | char *p; | |
698 | int j; | |
45c54bdc | 699 | size_t hdr_sz; |
70305391 | 700 | int nused = 0; |
add2192d | 701 | int size; |
702 | int oldbufofs = ex->buf_ofs; | |
703 | ||
45c54bdc | 704 | rec_sz = 0; |
cc192b50 | 705 | rec_sz += 1 + sizeof(struct in_addr); |
45c54bdc | 706 | rec_sz += 1 + sizeof(int); |
707 | rec_sz += 1 + sizeof(int); | |
2324cda2 | 708 | debugs(38, 3, "netdbExchangeHandleReply: " << receivedData.length << " read bytes"); |
62e76326 | 709 | |
891a70f1 | 710 | if (!ex->p.valid()) { |
bf8fe701 | 711 | debugs(38, 3, "netdbExchangeHandleReply: Peer became invalid"); |
fa91d030 | 712 | delete ex; |
62e76326 | 713 | return; |
45c54bdc | 714 | } |
62e76326 | 715 | |
bf8fe701 | 716 | debugs(38, 3, "netdbExchangeHandleReply: for '" << ex->p->host << ":" << ex->p->http_port << "'"); |
528b2c61 | 717 | |
fa91d030 | 718 | if (receivedData.length == 0 && !receivedData.flags.error) { |
bf8fe701 | 719 | debugs(38, 3, "netdbExchangeHandleReply: Done"); |
fa91d030 | 720 | delete ex; |
62e76326 | 721 | return; |
528b2c61 | 722 | } |
62e76326 | 723 | |
add2192d | 724 | p = ex->buf; |
725 | ||
726 | /* Get the size of the buffer now */ | |
2324cda2 | 727 | size = ex->buf_ofs + receivedData.length; |
137a13ea | 728 | debugs(38, 3, "netdbExchangeHandleReply: " << size << " bytes buf"); |
add2192d | 729 | |
730 | /* Check if we're still doing headers */ | |
62e76326 | 731 | |
add2192d | 732 | if (ex->connstate == STATE_HEADER) { |
733 | ||
2324cda2 | 734 | ex->buf_ofs += receivedData.length; |
62e76326 | 735 | |
736 | /* skip reply headers */ | |
737 | ||
738 | if ((hdr_sz = headersEnd(p, ex->buf_ofs))) { | |
137a13ea | 739 | debugs(38, 5, "netdbExchangeHandleReply: hdr_sz = " << hdr_sz); |
66d51f4f AR |
740 | const auto scode = ex->e->mem().baseReply().sline.status(); |
741 | assert(scode != Http::scNone); | |
742 | debugs(38, 3, "netdbExchangeHandleReply: reply status " << scode); | |
62e76326 | 743 | |
66d51f4f | 744 | if (scode != Http::scOkay) { |
fa91d030 | 745 | delete ex; |
62e76326 | 746 | return; |
747 | } | |
748 | ||
749 | assert((size_t)ex->buf_ofs >= hdr_sz); | |
750 | ||
751 | /* | |
752 | * Now, point p to the part of the buffer where the data | |
753 | * starts, and update the size accordingly | |
754 | */ | |
755 | assert(ex->used == 0); | |
756 | ex->used = hdr_sz; | |
757 | size = ex->buf_ofs - hdr_sz; | |
758 | p += hdr_sz; | |
759 | ||
760 | /* Finally, set the conn state mode to STATE_BODY */ | |
761 | ex->connstate = STATE_BODY; | |
762 | } else { | |
763 | StoreIOBuffer tempBuffer; | |
764 | tempBuffer.offset = ex->buf_ofs; | |
765 | tempBuffer.length = ex->buf_sz - ex->buf_ofs; | |
766 | tempBuffer.data = ex->buf + ex->buf_ofs; | |
767 | /* Have more headers .. */ | |
768 | storeClientCopy(ex->sc, ex->e, tempBuffer, | |
769 | netdbExchangeHandleReply, ex); | |
770 | return; | |
771 | } | |
45c54bdc | 772 | } |
62e76326 | 773 | |
add2192d | 774 | assert(ex->connstate == STATE_BODY); |
775 | ||
776 | /* If we get here, we have some body to parse .. */ | |
bf8fe701 | 777 | debugs(38, 5, "netdbExchangeHandleReply: start parsing loop, size = " << size); |
62e76326 | 778 | |
45c54bdc | 779 | while (size >= rec_sz) { |
bf8fe701 | 780 | debugs(38, 5, "netdbExchangeHandleReply: in parsing loop, size = " << size); |
4dd643d5 | 781 | addr.setAnyAddr(); |
62e76326 | 782 | hops = rtt = 0.0; |
783 | ||
784 | for (o = 0; o < rec_sz;) { | |
785 | switch ((int) *(p + o)) { | |
786 | ||
787 | case NETDB_EX_NETWORK: | |
7c64cac7 | 788 | ++o; |
2f8abb64 | 789 | /* FIXME INET6 : NetDB can still only send IPv4 */ |
41d00cd3 | 790 | memcpy(&line_addr, p + o, sizeof(struct in_addr)); |
cc192b50 | 791 | addr = line_addr; |
792 | o += sizeof(struct in_addr); | |
62e76326 | 793 | break; |
794 | ||
795 | case NETDB_EX_RTT: | |
7c64cac7 | 796 | ++o; |
41d00cd3 | 797 | memcpy(&j, p + o, sizeof(int)); |
62e76326 | 798 | o += sizeof(int); |
799 | rtt = (double) ntohl(j) / 1000.0; | |
800 | break; | |
801 | ||
802 | case NETDB_EX_HOPS: | |
7c64cac7 | 803 | ++o; |
41d00cd3 | 804 | memcpy(&j, p + o, sizeof(int)); |
62e76326 | 805 | o += sizeof(int); |
806 | hops = (double) ntohl(j) / 1000.0; | |
807 | break; | |
808 | ||
809 | default: | |
e0236918 | 810 | debugs(38, DBG_IMPORTANT, "netdbExchangeHandleReply: corrupt data, aborting"); |
fa91d030 | 811 | delete ex; |
62e76326 | 812 | return; |
813 | } | |
814 | } | |
815 | ||
4dd643d5 | 816 | if (!addr.isAnyAddr() && rtt > 0) |
891a70f1 | 817 | netdbExchangeUpdatePeer(addr, ex->p.get(), rtt, hops); |
62e76326 | 818 | |
819 | assert(o == rec_sz); | |
820 | ||
821 | ex->used += rec_sz; | |
822 | ||
823 | size -= rec_sz; | |
824 | ||
825 | p += rec_sz; | |
826 | ||
7c64cac7 | 827 | ++nused; |
45c54bdc | 828 | } |
add2192d | 829 | |
830 | /* | |
831 | * Copy anything that is left over to the beginning of the buffer, | |
832 | * and adjust buf_ofs accordingly | |
833 | */ | |
834 | ||
835 | /* | |
836 | * Evilly, size refers to the buf size left now, | |
837 | * ex->buf_ofs is the original buffer size, so just copy that | |
838 | * much data over | |
839 | */ | |
840 | memmove(ex->buf, ex->buf + (ex->buf_ofs - size), size); | |
62e76326 | 841 | |
add2192d | 842 | ex->buf_ofs = size; |
843 | ||
844 | /* | |
845 | * And don't re-copy the remaining data .. | |
846 | */ | |
847 | ex->used += size; | |
848 | ||
849 | /* | |
850 | * Now the tricky bit - size _included_ the leftover bit from the _last_ | |
851 | * storeClientCopy. We don't want to include that, or our offset will be wrong. | |
852 | * So, don't count the size of the leftover buffer we began with. | |
853 | * This can _disappear_ when we're not tracking offsets .. | |
854 | */ | |
855 | ex->used -= oldbufofs; | |
856 | ||
bf8fe701 | 857 | debugs(38, 3, "netdbExchangeHandleReply: size left over in this buffer: " << size << " bytes"); |
add2192d | 858 | |
bf8fe701 | 859 | debugs(38, 3, "netdbExchangeHandleReply: used " << nused << |
860 | " entries, (x " << rec_sz << " bytes) == " << nused * rec_sz << | |
861 | " bytes total"); | |
62e76326 | 862 | |
4a7a3d56 | 863 | debugs(38, 3, "netdbExchangeHandleReply: used " << ex->used); |
62e76326 | 864 | |
b7fe0ab0 | 865 | if (EBIT_TEST(ex->e->flags, ENTRY_ABORTED)) { |
bf8fe701 | 866 | debugs(38, 3, "netdbExchangeHandleReply: ENTRY_ABORTED"); |
fa91d030 | 867 | delete ex; |
ce9b54e8 | 868 | } else if (ex->e->store_status == STORE_PENDING) { |
62e76326 | 869 | StoreIOBuffer tempBuffer; |
870 | tempBuffer.offset = ex->used; | |
871 | tempBuffer.length = ex->buf_sz - ex->buf_ofs; | |
872 | tempBuffer.data = ex->buf + ex->buf_ofs; | |
2324cda2 | 873 | debugs(38, 3, "netdbExchangeHandleReply: EOF not received"); |
62e76326 | 874 | storeClientCopy(ex->sc, ex->e, tempBuffer, |
875 | netdbExchangeHandleReply, ex); | |
45c54bdc | 876 | } |
877 | } | |
878 | ||
f69d9265 AJ |
879 | #endif /* USE_ICMP */ |
880 | ||
e97f40f4 | 881 | /* PUBLIC FUNCTIONS */ |
882 | ||
67508012 | 883 | void |
e97f40f4 | 884 | netdbInit(void) |
885 | { | |
886 | #if USE_ICMP | |
1a7cfe02 | 887 | Mgr::RegisterAction("netdb", "Network Measurement Database", netdbDump, 0, 1); |
d120ed12 | 888 | |
19054954 | 889 | if (addr_table) |
62e76326 | 890 | return; |
891 | ||
1a7cfe02 | 892 | int n = hashPrime(Config.Netdb.high / 4); |
62e76326 | 893 | |
30abd221 | 894 | addr_table = hash_create((HASHCMP *) strcmp, n, hash_string); |
62e76326 | 895 | |
aa9e2cab | 896 | n = hashPrime(3 * Config.Netdb.high / 4); |
62e76326 | 897 | |
30abd221 | 898 | host_table = hash_create((HASHCMP *) strcmp, n, hash_string); |
62e76326 | 899 | |
52040193 | 900 | eventAddIsh("netdbSaveState", netdbSaveState, NULL, 3600.0, 1); |
62e76326 | 901 | |
429fdbec | 902 | netdbReloadState(); |
62e76326 | 903 | |
62ee09ca | 904 | #endif |
905 | } | |
906 | ||
e97f40f4 | 907 | void |
0ee4272b | 908 | netdbPingSite(const char *hostname) |
67508012 | 909 | { |
e97f40f4 | 910 | #if USE_ICMP |
67508012 | 911 | netdbEntry *n; |
62e76326 | 912 | |
e97f40f4 | 913 | if ((n = netdbLookupHost(hostname)) != NULL) |
62e76326 | 914 | if (n->next_ping_time > squid_curtime) |
915 | return; | |
916 | ||
aa839030 | 917 | ipcache_nbgethostbyname(hostname, netdbSendPing, |
26ac0430 | 918 | new generic_cbdata(xstrdup(hostname))); |
62e76326 | 919 | |
e97f40f4 | 920 | #endif |
67508012 | 921 | } |
922 | ||
e97f40f4 | 923 | void |
b7ac5457 | 924 | netdbHandlePingReply(const Ip::Address &from, int hops, int rtt) |
4d311579 | 925 | { |
e97f40f4 | 926 | #if USE_ICMP |
927 | netdbEntry *n; | |
928 | int N; | |
cc192b50 | 929 | debugs(38, 3, "netdbHandlePingReply: from " << from); |
62e76326 | 930 | |
cc192b50 | 931 | if ((n = netdbLookupAddr(from)) == NULL) |
62e76326 | 932 | return; |
933 | ||
429fdbec | 934 | N = ++n->pings_recv; |
62e76326 | 935 | |
429fdbec | 936 | if (N > 5) |
62e76326 | 937 | N = 5; |
938 | ||
e6ccf245 | 939 | if (rtt < 1) |
62e76326 | 940 | rtt = 1; |
941 | ||
e97f40f4 | 942 | n->hops = ((n->hops * (N - 1)) + hops) / N; |
62e76326 | 943 | |
e97f40f4 | 944 | n->rtt = ((n->rtt * (N - 1)) + rtt) / N; |
62e76326 | 945 | |
bf8fe701 | 946 | debugs(38, 3, "netdbHandlePingReply: " << n->network << "; rtt="<< |
947 | std::setw(5)<< std::setprecision(2) << n->rtt << " hops="<< | |
948 | std::setw(4) << n->hops); | |
62e76326 | 949 | |
e97f40f4 | 950 | #endif |
4d311579 | 951 | } |
e5f6c5c2 | 952 | |
953 | void | |
954 | netdbFreeMemory(void) | |
955 | { | |
e97f40f4 | 956 | #if USE_ICMP |
ec878047 | 957 | hashFreeItems(addr_table, netdbFreeNetdbEntry); |
e5f6c5c2 | 958 | hashFreeMemory(addr_table); |
afe95a7e | 959 | addr_table = NULL; |
ec878047 | 960 | hashFreeItems(host_table, netdbFreeNameEntry); |
961 | hashFreeMemory(host_table); | |
afe95a7e | 962 | host_table = NULL; |
429fdbec | 963 | wordlistDestroy(&peer_names); |
964 | peer_names = NULL; | |
e97f40f4 | 965 | #endif |
e5f6c5c2 | 966 | } |
967 | ||
e97f40f4 | 968 | void |
969 | netdbDump(StoreEntry * sentry) | |
970 | { | |
971 | #if USE_ICMP | |
972 | netdbEntry *n; | |
973 | netdbEntry **list; | |
429fdbec | 974 | net_db_name *x; |
e97f40f4 | 975 | int k; |
976 | int i; | |
429fdbec | 977 | int j; |
978 | net_db_peer *p; | |
15576b6a | 979 | storeAppendPrintf(sentry, "Network DB Statistics:\n"); |
cc192b50 | 980 | storeAppendPrintf(sentry, "%-46.46s %9s %7s %5s %s\n", /* Max between 16 (IPv4) or 46 (IPv6) */ |
62e76326 | 981 | "Network", |
982 | "recv/sent", | |
983 | "RTT", | |
984 | "Hops", | |
985 | "Hostnames"); | |
1a7cfe02 | 986 | list = (netdbEntry **)xcalloc(netdbEntry::UseCount(), sizeof(netdbEntry *)); |
0ee4272b | 987 | i = 0; |
0f6bebac | 988 | hash_first(addr_table); |
62e76326 | 989 | |
a38ec4b1 FC |
990 | while ((n = (netdbEntry *) hash_next(addr_table))) { |
991 | *(list + i) = n; | |
992 | ++i; | |
993 | } | |
62e76326 | 994 | |
1a7cfe02 | 995 | if (i != netdbEntry::UseCount()) |
fa84c01d | 996 | debugs(38, DBG_CRITICAL, "WARNING: netdb_addrs count off, found " << i << |
1a7cfe02 | 997 | ", expected " << netdbEntry::UseCount()); |
62e76326 | 998 | |
0ee4272b | 999 | qsort((char *) list, |
62e76326 | 1000 | i, |
1001 | sizeof(netdbEntry *), | |
1002 | sortByRtt); | |
1003 | ||
7c64cac7 | 1004 | for (k = 0; k < i; ++k) { |
62e76326 | 1005 | n = *(list + k); |
cc192b50 | 1006 | storeAppendPrintf(sentry, "%-46.46s %4d/%4d %7.1f %5.1f", /* Max between 16 (IPv4) or 46 (IPv6) */ |
62e76326 | 1007 | n->network, |
1008 | n->pings_recv, | |
1009 | n->pings_sent, | |
1010 | n->rtt, | |
1011 | n->hops); | |
1012 | ||
1013 | for (x = n->hosts; x; x = x->next) | |
4ec218d5 | 1014 | storeAppendPrintf(sentry, " %s", hashKeyStr(x)); |
62e76326 | 1015 | |
1016 | storeAppendPrintf(sentry, "\n"); | |
1017 | ||
1018 | p = n->peers; | |
1019 | ||
7c64cac7 | 1020 | for (j = 0; j < n->n_peers; ++j, ++p) { |
62e76326 | 1021 | storeAppendPrintf(sentry, " %-22.22s %7.1f %5.1f\n", |
1022 | p->peername, | |
1023 | p->rtt, | |
1024 | p->hops); | |
1025 | } | |
0ee4272b | 1026 | } |
62e76326 | 1027 | |
0ee4272b | 1028 | xfree(list); |
c8391077 | 1029 | #else |
62e76326 | 1030 | |
9b5c4a9a | 1031 | storeAppendPrintf(sentry,"NETDB support not compiled into this Squid cache.\n"); |
e97f40f4 | 1032 | #endif |
1033 | } | |
48f44632 | 1034 | |
1035 | int | |
1036 | netdbHostHops(const char *host) | |
1037 | { | |
1038 | #if USE_ICMP | |
1039 | netdbEntry *n = netdbLookupHost(host); | |
62e76326 | 1040 | |
429fdbec | 1041 | if (n) { |
62e76326 | 1042 | n->last_use_time = squid_curtime; |
1043 | return (int) (n->hops + 0.5); | |
429fdbec | 1044 | } |
62e76326 | 1045 | |
48f44632 | 1046 | #endif |
429fdbec | 1047 | return 0; |
48f44632 | 1048 | } |
1049 | ||
1050 | int | |
1051 | netdbHostRtt(const char *host) | |
1052 | { | |
1053 | #if USE_ICMP | |
1054 | netdbEntry *n = netdbLookupHost(host); | |
62e76326 | 1055 | |
429fdbec | 1056 | if (n) { |
62e76326 | 1057 | n->last_use_time = squid_curtime; |
1058 | return (int) (n->rtt + 0.5); | |
429fdbec | 1059 | } |
62e76326 | 1060 | |
429fdbec | 1061 | #endif |
1062 | return 0; | |
1063 | } | |
1064 | ||
1d6ae62d | 1065 | void |
1066 | netdbHostData(const char *host, int *samp, int *rtt, int *hops) | |
1067 | { | |
1068 | #if USE_ICMP | |
5942e8d4 | 1069 | netdbEntry *n = netdbLookupHost(host); |
62e76326 | 1070 | |
5942e8d4 | 1071 | if (n == NULL) |
62e76326 | 1072 | return; |
1073 | ||
5942e8d4 | 1074 | *samp = n->pings_recv; |
62e76326 | 1075 | |
5942e8d4 | 1076 | *rtt = (int) (n->rtt + 0.5); |
62e76326 | 1077 | |
5942e8d4 | 1078 | *hops = (int) (n->hops + 0.5); |
62e76326 | 1079 | |
4ed6ef62 | 1080 | n->last_use_time = squid_curtime; |
62e76326 | 1081 | |
1d6ae62d | 1082 | #endif |
1083 | } | |
1084 | ||
429fdbec | 1085 | void |
c8ab5ec6 | 1086 | netdbUpdatePeer(const AnyP::Uri &url, CachePeer *e, int irtt, int ihops) |
429fdbec | 1087 | { |
1088 | #if USE_ICMP | |
1089 | netdbEntry *n; | |
1090 | double rtt = (double) irtt; | |
1091 | double hops = (double) ihops; | |
1092 | net_db_peer *p; | |
5c51bffb AJ |
1093 | debugs(38, 3, url.host() << ", " << ihops << " hops, " << irtt << " rtt"); |
1094 | n = netdbLookupHost(url.host()); | |
62e76326 | 1095 | |
429fdbec | 1096 | if (n == NULL) { |
5c51bffb | 1097 | debugs(38, 3, "host " << url.host() << " not found"); |
62e76326 | 1098 | return; |
429fdbec | 1099 | } |
62e76326 | 1100 | |
429fdbec | 1101 | if ((p = netdbPeerByName(n, e->host)) == NULL) |
62e76326 | 1102 | p = netdbPeerAdd(n, e); |
1103 | ||
429fdbec | 1104 | p->rtt = rtt; |
62e76326 | 1105 | |
429fdbec | 1106 | p->hops = hops; |
62e76326 | 1107 | |
429fdbec | 1108 | p->expires = squid_curtime + 3600; |
62e76326 | 1109 | |
429fdbec | 1110 | if (n->n_peers < 2) |
62e76326 | 1111 | return; |
1112 | ||
429fdbec | 1113 | qsort((char *) n->peers, |
62e76326 | 1114 | n->n_peers, |
1115 | sizeof(net_db_peer), | |
1116 | sortPeerByRtt); | |
1117 | ||
48f44632 | 1118 | #endif |
48f44632 | 1119 | } |
ce75f381 | 1120 | |
27efd484 | 1121 | void |
a3c6762c | 1122 | netdbExchangeUpdatePeer(Ip::Address &addr, CachePeer * e, double rtt, double hops) |
27efd484 | 1123 | { |
1124 | #if USE_ICMP | |
1125 | netdbEntry *n; | |
1126 | net_db_peer *p; | |
cc192b50 | 1127 | debugs(38, 5, "netdbExchangeUpdatePeer: '" << addr << "', "<< |
bf8fe701 | 1128 | std::setfill('0')<< std::setprecision(2) << hops << " hops, " << |
1129 | rtt << " rtt"); | |
1130 | ||
4dd643d5 | 1131 | if ( !addr.isIPv4() ) { |
cc192b50 | 1132 | debugs(38, 5, "netdbExchangeUpdatePeer: Aborting peer update for '" << addr << "', NetDB cannot handle IPv6."); |
1133 | return; | |
1134 | } | |
1135 | ||
27efd484 | 1136 | n = netdbLookupAddr(addr); |
62e76326 | 1137 | |
27efd484 | 1138 | if (n == NULL) |
62e76326 | 1139 | n = netdbAdd(addr); |
1140 | ||
27efd484 | 1141 | assert(NULL != n); |
62e76326 | 1142 | |
27efd484 | 1143 | if ((p = netdbPeerByName(n, e->host)) == NULL) |
62e76326 | 1144 | p = netdbPeerAdd(n, e); |
1145 | ||
27efd484 | 1146 | p->rtt = rtt; |
62e76326 | 1147 | |
27efd484 | 1148 | p->hops = hops; |
62e76326 | 1149 | |
f53969cc | 1150 | p->expires = squid_curtime + 3600; /* XXX ? */ |
62e76326 | 1151 | |
27efd484 | 1152 | if (n->n_peers < 2) |
62e76326 | 1153 | return; |
1154 | ||
27efd484 | 1155 | qsort((char *) n->peers, |
62e76326 | 1156 | n->n_peers, |
1157 | sizeof(net_db_peer), | |
1158 | sortPeerByRtt); | |
1159 | ||
27efd484 | 1160 | #endif |
1161 | } | |
1162 | ||
587d8445 | 1163 | void |
b7ac5457 | 1164 | netdbDeleteAddrNetwork(Ip::Address &addr) |
587d8445 | 1165 | { |
1166 | #if USE_ICMP | |
1167 | netdbEntry *n = netdbLookupAddr(addr); | |
62e76326 | 1168 | |
587d8445 | 1169 | if (n == NULL) |
62e76326 | 1170 | return; |
1171 | ||
bf8fe701 | 1172 | debugs(38, 3, "netdbDeleteAddrNetwork: " << n->network); |
62e76326 | 1173 | |
587d8445 | 1174 | netdbRelease(n); |
1175 | #endif | |
1176 | } | |
de2a0782 | 1177 | |
1178 | void | |
1179 | netdbBinaryExchange(StoreEntry * s) | |
1180 | { | |
06a5ae20 | 1181 | HttpReply *reply = new HttpReply; |
9ad1cbca | 1182 | #if USE_ICMP |
62e76326 | 1183 | |
b7ac5457 | 1184 | Ip::Address addr; |
cc192b50 | 1185 | |
de2a0782 | 1186 | netdbEntry *n; |
de2a0782 | 1187 | int i; |
1188 | int j; | |
1189 | int rec_sz; | |
1190 | char *buf; | |
62e76326 | 1191 | |
cc192b50 | 1192 | struct in_addr line_addr; |
3900307b | 1193 | s->buffer(); |
955394ce | 1194 | reply->setHeaders(Http::scOkay, "OK", NULL, -1, squid_curtime, -2); |
db237875 | 1195 | s->replaceHttpReply(reply); |
de2a0782 | 1196 | rec_sz = 0; |
cc192b50 | 1197 | rec_sz += 1 + sizeof(struct in_addr); |
de2a0782 | 1198 | rec_sz += 1 + sizeof(int); |
1199 | rec_sz += 1 + sizeof(int); | |
e6ccf245 | 1200 | buf = (char *)memAllocate(MEM_4K_BUF); |
de2a0782 | 1201 | i = 0; |
0f6bebac | 1202 | hash_first(addr_table); |
62e76326 | 1203 | |
0f6bebac | 1204 | while ((n = (netdbEntry *) hash_next(addr_table))) { |
62e76326 | 1205 | if (0.0 == n->rtt) |
1206 | continue; | |
1207 | ||
f53969cc | 1208 | if (n->rtt > 60000) /* RTT > 1 MIN probably bogus */ |
62e76326 | 1209 | continue; |
1210 | ||
cc192b50 | 1211 | if (! (addr = n->network) ) |
1212 | continue; | |
1213 | ||
1214 | /* FIXME INET6 : NetDB cannot yet handle IPv6 addresses. Ensure only IPv4 get sent. */ | |
4dd643d5 | 1215 | if ( !addr.isIPv4() ) |
62e76326 | 1216 | continue; |
1217 | ||
a38ec4b1 FC |
1218 | buf[i] = (char) NETDB_EX_NETWORK; |
1219 | ++i; | |
62e76326 | 1220 | |
4dd643d5 | 1221 | addr.getInAddr(line_addr); |
41d00cd3 | 1222 | memcpy(&buf[i], &line_addr, sizeof(struct in_addr)); |
62e76326 | 1223 | |
cc192b50 | 1224 | i += sizeof(struct in_addr); |
62e76326 | 1225 | |
a38ec4b1 FC |
1226 | buf[i] = (char) NETDB_EX_RTT; |
1227 | ++i; | |
62e76326 | 1228 | |
1229 | j = htonl((int) (n->rtt * 1000)); | |
1230 | ||
41d00cd3 | 1231 | memcpy(&buf[i], &j, sizeof(int)); |
62e76326 | 1232 | |
1233 | i += sizeof(int); | |
1234 | ||
a38ec4b1 FC |
1235 | buf[i] = (char) NETDB_EX_HOPS; |
1236 | ++i; | |
62e76326 | 1237 | |
1238 | j = htonl((int) (n->hops * 1000)); | |
1239 | ||
41d00cd3 | 1240 | memcpy(&buf[i], &j, sizeof(int)); |
62e76326 | 1241 | |
1242 | i += sizeof(int); | |
1243 | ||
1244 | if (i + rec_sz > 4096) { | |
3900307b | 1245 | s->append(buf, i); |
62e76326 | 1246 | i = 0; |
1247 | } | |
de2a0782 | 1248 | } |
62e76326 | 1249 | |
84075e8f | 1250 | if (i > 0) { |
3900307b | 1251 | s->append(buf, i); |
62e76326 | 1252 | i = 0; |
84075e8f | 1253 | } |
62e76326 | 1254 | |
de2a0782 | 1255 | assert(0 == i); |
3900307b | 1256 | s->flush(); |
db1cd23c | 1257 | memFree(buf, MEM_4K_BUF); |
de2a0782 | 1258 | #else |
62e76326 | 1259 | |
955394ce | 1260 | reply->setHeaders(Http::scBadRequest, "Bad Request", NULL, -1, squid_curtime, -2); |
db237875 | 1261 | s->replaceHttpReply(reply); |
de2a0782 | 1262 | storeAppendPrintf(s, "NETDB support not compiled into this Squid cache.\n"); |
1263 | #endif | |
62e76326 | 1264 | |
528b2c61 | 1265 | s->complete(); |
9ad1cbca | 1266 | } |
1267 | ||
1268 | void | |
1269 | netdbExchangeStart(void *data) | |
1270 | { | |
45c54bdc | 1271 | #if USE_ICMP |
a3c6762c | 1272 | CachePeer *p = (CachePeer *)data; |
51b5dcf5 | 1273 | static const SBuf netDB("netdb"); |
c002f0e7 | 1274 | char *uri = internalRemoteUri(p->secure.encryptTransport, p->host, p->http_port, "/squid-internal-dynamic/", netDB); |
ad049d4d | 1275 | debugs(38, 3, "Requesting '" << uri << "'"); |
5ceaee75 | 1276 | const MasterXaction::Pointer mx = new MasterXaction(XactionInitiator::initIcmp); |
6c880a16 | 1277 | HttpRequestPointer req(HttpRequest::FromUrlXXX(uri, mx)); |
62e76326 | 1278 | |
891a70f1 | 1279 | if (!req) { |
ad049d4d | 1280 | debugs(38, DBG_IMPORTANT, MYNAME << ": Bad URI " << uri); |
62e76326 | 1281 | return; |
a74c5601 | 1282 | } |
62e76326 | 1283 | |
fa91d030 | 1284 | netdbExchangeState *ex = new netdbExchangeState(p, req); |
c2a7cefd | 1285 | ex->e = storeCreateEntry(uri, uri, RequestFlags(), Http::METHOD_GET); |
a74c5601 | 1286 | assert(NULL != ex->e); |
fa91d030 AJ |
1287 | |
1288 | StoreIOBuffer tempBuffer; | |
4b725156 | 1289 | tempBuffer.length = ex->buf_sz; |
1290 | tempBuffer.data = ex->buf; | |
fa91d030 AJ |
1291 | |
1292 | ex->sc = storeClientListAdd(ex->e, ex); | |
1293 | ||
4b725156 | 1294 | storeClientCopy(ex->sc, ex->e, tempBuffer, |
62e76326 | 1295 | netdbExchangeHandleReply, ex); |
f53969cc | 1296 | ex->r->flags.loopDetected = true; /* cheat! -- force direct */ |
62e76326 | 1297 | |
92d6986d | 1298 | // XXX: send as Proxy-Authenticate instead |
9bc73deb | 1299 | if (p->login) |
92d6986d | 1300 | ex->r->url.userInfo(SBuf(p->login)); |
62e76326 | 1301 | |
891a70f1 | 1302 | FwdState::fwdStart(Comm::ConnectionPointer(), ex->e, ex->r.getRaw()); |
45c54bdc | 1303 | #endif |
de2a0782 | 1304 | } |
69c95dd3 | 1305 | |
a3c6762c | 1306 | CachePeer * |
cb365059 | 1307 | netdbClosestParent(PeerSelector *ps) |
69c95dd3 | 1308 | { |
69c95dd3 | 1309 | #if USE_ICMP |
cb365059 EB |
1310 | assert(ps); |
1311 | HttpRequest *request = ps->request; | |
1312 | ||
a3c6762c | 1313 | CachePeer *p = NULL; |
69c95dd3 | 1314 | netdbEntry *n; |
1315 | const ipcache_addrs *ia; | |
1316 | net_db_peer *h; | |
1317 | int i; | |
5c51bffb | 1318 | n = netdbLookupHost(request->url.host()); |
62e76326 | 1319 | |
69c95dd3 | 1320 | if (NULL == n) { |
62e76326 | 1321 | /* try IP addr */ |
5c51bffb | 1322 | ia = ipcache_gethostbyname(request->url.host(), 0); |
62e76326 | 1323 | |
1324 | if (NULL != ia) | |
fd9c47d1 | 1325 | n = netdbLookupAddr(ia->current()); |
69c95dd3 | 1326 | } |
62e76326 | 1327 | |
69c95dd3 | 1328 | if (NULL == n) |
62e76326 | 1329 | return NULL; |
1330 | ||
69c95dd3 | 1331 | if (0 == n->n_peers) |
62e76326 | 1332 | return NULL; |
1333 | ||
4ed6ef62 | 1334 | n->last_use_time = squid_curtime; |
62e76326 | 1335 | |
1336 | /* | |
69c95dd3 | 1337 | * Find the parent with the least RTT to the origin server. |
1338 | * Make sure we don't return a parent who is farther away than | |
1339 | * we are. Note, the n->peers list is pre-sorted by RTT. | |
1340 | */ | |
7c64cac7 | 1341 | for (i = 0; i < n->n_peers; ++i) { |
62e76326 | 1342 | h = &n->peers[i]; |
1343 | ||
1344 | if (n->rtt > 0) | |
1345 | if (n->rtt < h->rtt) | |
1346 | break; | |
1347 | ||
1348 | p = peerFindByName(h->peername); | |
1349 | ||
f53969cc | 1350 | if (NULL == p) /* not found */ |
62e76326 | 1351 | continue; |
1352 | ||
5c51bffb | 1353 | if (neighborType(p, request->url) != PEER_PARENT) |
62e76326 | 1354 | continue; |
1355 | ||
cb365059 | 1356 | if (!peerHTTPOkay(p, ps)) /* not allowed */ |
62e76326 | 1357 | continue; |
1358 | ||
1359 | return p; | |
69c95dd3 | 1360 | } |
62e76326 | 1361 | |
69c95dd3 | 1362 | #endif |
1363 | return NULL; | |
1364 | } | |
f53969cc | 1365 |