3 * DEBUG: section 53 AS Number handling
4 * AUTHOR: Duane Wessels, Kostas Anagnostakis
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 #include "acl/Checklist.h"
38 #include "acl/DestinationAsn.h"
39 #include "acl/DestinationIp.h"
40 #include "acl/SourceAsn.h"
43 #include "HttpReply.h"
44 #include "HttpRequest.h"
46 #include "mgr/Registration.h"
48 #include "RequestFlags.h"
49 #include "SquidConfig.h"
51 #include "StoreClient.h"
52 #include "StoreClient.h"
56 #define AS_REQBUF_SZ 4096
58 /* BEGIN of definitions for radix tree entries */
60 /* 32/128 bits address in memory with length */
67 m_ADDR() : len(sizeof(Ip::Address
)) {};
70 /* END of definitions for radix tree entries */
72 /* Head for ip to asn radix tree */
74 struct squid_radix_node_head
*AS_tree_head
;
76 /* explicit instantiation required for some systems */
78 /// \cond AUTODOCS-IGNORE
79 template cbdata_type CbDataList
<int>::CBDATA_CbDataList
;
83 * Structure for as number information. it could be simply
84 * a list but it's coded as a structure for future
85 * enhancements (e.g. expires)
88 CbDataList
<int> *as_number
;
89 time_t expires
; /* NOTUSED */
99 char reqbuf
[AS_REQBUF_SZ
];
103 /** entry into the radix tree */
105 struct squid_radix_node e_nodes
[2];
111 static int asnAddNet(char *, int);
113 static void asnCacheStart(int as
);
115 static STCB asHandleReply
;
117 #if defined(__cplusplus)
121 static int destroyRadixNode(struct squid_radix_node
*rn
, void *w
);
122 static int printRadixNode(struct squid_radix_node
*rn
, void *sentry
);
124 #if defined(__cplusplus)
128 void asnAclInitialize(ACL
* acls
);
130 static void asStateFree(void *data
);
132 static void destroyRadixNodeInfo(as_info
*);
134 static OBJH asnStats
;
139 asnMatchIp(CbDataList
<int> *data
, Ip::Address
&addr
)
141 struct squid_radix_node
*rn
;
144 CbDataList
<int> *a
= NULL
;
145 CbDataList
<int> *b
= NULL
;
147 debugs(53, 3, "asnMatchIp: Called for " << addr
);
149 if (AS_tree_head
== NULL
)
155 if (addr
.IsAnyAddr())
160 rn
= squid_rn_match(&m_addr
, AS_tree_head
);
163 debugs(53, 3, "asnMatchIp: Address not in as db.");
167 debugs(53, 3, "asnMatchIp: Found in db!");
168 e
= ((rtentry_t
*) rn
)->e_info
;
171 for (a
= data
; a
; a
= a
->next
)
172 for (b
= e
->as_number
; b
; b
= b
->next
)
173 if (a
->element
== b
->element
) {
174 debugs(53, 5, "asnMatchIp: Found a match!");
178 debugs(53, 5, "asnMatchIp: AS not in as db.");
183 ACLASN::prepareForUse()
185 for (CbDataList
<int> *i
= data
; i
; i
= i
->
187 asnCacheStart(i
->element
);
191 asnRegisterWithCacheManager(void)
193 Mgr::RegisterAction("asndb", "AS Number Database", asnStats
, 0, 1);
196 /* initialize the radix tree structure */
198 SQUIDCEXTERN
int squid_max_keylen
; /* yuck.. this is in lib/radix.c */
200 CBDATA_TYPE(ASState
);
204 static bool inited
= false;
205 squid_max_keylen
= 40;
206 CBDATA_INIT_TYPE(ASState
);
213 squid_rn_inithead(&AS_tree_head
, 8);
215 asnRegisterWithCacheManager();
221 squid_rn_walktree(AS_tree_head
, destroyRadixNode
, AS_tree_head
);
223 destroyRadixNode((struct squid_radix_node
*) 0, (void *) AS_tree_head
);
227 asnStats(StoreEntry
* sentry
)
229 storeAppendPrintf(sentry
, "Address \tAS Numbers\n");
230 squid_rn_walktree(AS_tree_head
, printRadixNode
, sentry
);
236 asnCacheStart(int as
)
238 LOCAL_ARRAY(char, asres
, 4096);
241 asState
= cbdataAlloc(ASState
);
242 asState
->dataRead
= 0;
243 debugs(53, 3, "asnCacheStart: AS " << as
);
244 snprintf(asres
, 4096, "whois://%s/!gAS%d", Config
.as_whois_server
, as
);
245 asState
->as_number
= as
;
246 asState
->request
= HttpRequest::CreateFromUrl(asres
);
247 assert(NULL
!= asState
->request
);
248 HTTPMSGLOCK(asState
->request
);
250 if ((e
= storeGetPublic(asres
, Http::METHOD_GET
)) == NULL
) {
251 e
= storeCreateEntry(asres
, asres
, RequestFlags(), Http::METHOD_GET
);
252 asState
->sc
= storeClientListAdd(e
, asState
);
253 FwdState::fwdStart(Comm::ConnectionPointer(), e
, asState
->request
);
257 asState
->sc
= storeClientListAdd(e
, asState
);
263 StoreIOBuffer
readBuffer (AS_REQBUF_SZ
, asState
->offset
, asState
->reqbuf
);
264 storeClientCopy(asState
->sc
,
272 asHandleReply(void *data
, StoreIOBuffer result
)
274 ASState
*asState
= (ASState
*)data
;
275 StoreEntry
*e
= asState
->entry
;
278 char *buf
= asState
->reqbuf
;
281 debugs(53, 3, "asHandleReply: Called with size=" << (unsigned int)result
.length
);
282 debugs(53, 3, "asHandleReply: buffer='" << buf
<< "'");
284 /* First figure out whether we should abort the request */
286 if (EBIT_TEST(e
->flags
, ENTRY_ABORTED
)) {
287 asStateFree(asState
);
291 if (result
.length
== 0 && asState
->dataRead
) {
292 debugs(53, 3, "asHandleReply: Done: " << e
->url() );
293 asStateFree(asState
);
295 } else if (result
.flags
.error
) {
296 debugs(53, DBG_IMPORTANT
, "asHandleReply: Called with Error set and size=" << (unsigned int) result
.length
);
297 asStateFree(asState
);
299 } else if (HTTP_OK
!= e
->getReply()->sline
.status
) {
300 debugs(53, DBG_IMPORTANT
, "WARNING: AS " << asState
->as_number
<< " whois request failed");
301 asStateFree(asState
);
306 * Next, attempt to parse our request
307 * Remembering that the actual buffer size is retsize + reqofs!
311 while ((size_t)(s
- buf
) < result
.length
+ asState
->reqofs
&& *s
!= '\0') {
312 while (*s
&& xisspace(*s
))
315 for (t
= s
; *t
; ++t
) {
321 /* oof, word should continue on next block */
326 debugs(53, 3, "asHandleReply: AS# " << s
<< " (" << asState
->as_number
<< ")");
327 asnAddNet(s
, asState
->as_number
);
329 asState
->dataRead
= 1;
333 * Next, grab the end of the 'valid data' in the buffer, and figure
334 * out how much data is left in our buffer, which we need to keep
335 * around for the next request
337 leftoversz
= (asState
->reqofs
+ result
.length
) - (s
- buf
);
339 assert(leftoversz
>= 0);
342 * Next, copy the left over data, from s to s + leftoversz to the
343 * beginning of the buffer
345 memmove(buf
, s
, leftoversz
);
348 * Next, update our offset and reqofs, and kick off a copy if required
350 asState
->offset
+= result
.length
;
352 asState
->reqofs
= leftoversz
;
354 debugs(53, 3, "asState->offset = " << asState
->offset
);
356 if (e
->store_status
== STORE_PENDING
) {
357 debugs(53, 3, "asHandleReply: store_status == STORE_PENDING: " << e
->url() );
358 StoreIOBuffer
tempBuffer (AS_REQBUF_SZ
- asState
->reqofs
,
360 asState
->reqbuf
+ asState
->reqofs
);
361 storeClientCopy(asState
->sc
,
367 StoreIOBuffer tempBuffer
;
368 debugs(53, 3, "asHandleReply: store complete, but data received " << e
->url() );
369 tempBuffer
.offset
= asState
->offset
;
370 tempBuffer
.length
= AS_REQBUF_SZ
- asState
->reqofs
;
371 tempBuffer
.data
= asState
->reqbuf
+ asState
->reqofs
;
372 storeClientCopy(asState
->sc
,
381 asStateFree(void *data
)
383 ASState
*asState
= (ASState
*)data
;
384 debugs(53, 3, "asnStateFree: " << asState
->entry
->url() );
385 storeUnregister(asState
->sc
, asState
->entry
, asState
);
386 asState
->entry
->unlock();
387 HTTPMSGUNLOCK(asState
->request
);
392 * add a network (addr, mask) to the radix tree, with matching AS number
395 asnAddNet(char *as_string
, int as_number
)
397 struct squid_radix_node
*rn
;
398 CbDataList
<int> **Tail
= NULL
;
399 CbDataList
<int> *q
= NULL
;
400 as_info
*asinfo
= NULL
;
407 t
= strchr(as_string
, '/');
410 debugs(53, 3, "asnAddNet: failed, invalid response from whois server.");
421 // INET6 TODO : find a better way of identifying the base IPA family for mask than this.
422 t
= strchr(as_string
, '.');
424 // generate Netbits Format Mask
426 mask
.ApplyMask(bitl
, (t
!=NULL
?AF_INET
:AF_INET6
) );
428 debugs(53, 3, "asnAddNet: called for " << addr
<< "/" << mask
);
430 rtentry_t
*e
= (rtentry_t
*)xcalloc(1, sizeof(rtentry_t
));
432 e
->e_addr
.addr
= addr
;
434 e
->e_mask
.addr
= mask
;
436 rn
= squid_rn_lookup(&e
->e_addr
, &e
->e_mask
, AS_tree_head
);
439 asinfo
= ((rtentry_t
*) rn
)->e_info
;
441 if (asinfo
->as_number
->find(as_number
)) {
442 debugs(53, 3, "asnAddNet: Ignoring repeated network '" << addr
<< "/" << bitl
<< "' for AS " << as_number
);
444 debugs(53, 3, "asnAddNet: Warning: Found a network with multiple AS numbers!");
446 for (Tail
= &asinfo
->as_number
; *Tail
; Tail
= &(*Tail
)->next
);
447 q
= new CbDataList
<int> (as_number
);
454 q
= new CbDataList
<int> (as_number
);
455 asinfo
= (as_info
*)xmalloc(sizeof(as_info
));
456 asinfo
->as_number
= q
;
457 squid_rn_addroute(&e
->e_addr
, &e
->e_mask
, AS_tree_head
, e
->e_nodes
);
458 rn
= squid_rn_match(&e
->e_addr
, AS_tree_head
);
463 if (rn
== 0) { /* assert might expand to nothing */
467 debugs(53, 3, "asnAddNet: Could not add entry.");
476 destroyRadixNode(struct squid_radix_node
*rn
, void *w
)
479 struct squid_radix_node_head
*rnh
= (struct squid_radix_node_head
*) w
;
481 if (rn
&& !(rn
->rn_flags
& RNF_ROOT
)) {
482 rtentry_t
*e
= (rtentry_t
*) rn
;
483 rn
= squid_rn_delete(rn
->rn_key
, rn
->rn_mask
, rnh
);
486 debugs(53, 3, "destroyRadixNode: internal screwup");
488 destroyRadixNodeInfo(e
->e_info
);
497 destroyRadixNodeInfo(as_info
* e_info
)
499 CbDataList
<int> *prev
= NULL
;
500 CbDataList
<int> *data
= e_info
->as_number
;
510 printRadixNode(struct squid_radix_node
*rn
, void *_sentry
)
512 StoreEntry
*sentry
= (StoreEntry
*)_sentry
;
513 rtentry_t
*e
= (rtentry_t
*) rn
;
516 char buf
[MAX_IPSTRLEN
];
522 addr
= e
->e_addr
.addr
;
523 mask
= e
->e_mask
.addr
;
524 storeAppendPrintf(sentry
, "%s/%d\t",
525 addr
.NtoA(buf
, MAX_IPSTRLEN
),
528 assert(asinfo
->as_number
);
530 for (q
= asinfo
->as_number
; q
; q
= q
->next
)
531 storeAppendPrintf(sentry
, " %d", q
->element
);
533 storeAppendPrintf(sentry
, "\n");
546 ACLASN::match(Ip::Address toMatch
)
548 return asnMatchIp(data
, toMatch
);
556 CbDataList
<int> *ldata
= data
;
558 while (ldata
!= NULL
) {
559 snprintf(buf
, sizeof(buf
), "%d", ldata
->element
);
560 wordlistAdd(&W
, buf
);
568 ACLASN::empty () const
576 CbDataList
<int> **curlist
= &data
;
577 CbDataList
<int> **Tail
;
578 CbDataList
<int> *q
= NULL
;
581 for (Tail
= curlist
; *Tail
; Tail
= &((*Tail
)->next
));
582 while ((t
= strtokFile())) {
583 q
= new CbDataList
<int> (atoi(t
));
589 ACLData
<Ip::Address
> *
590 ACLASN::clone() const
593 fatal ("cloning of ACLASN not implemented");
595 return new ACLASN(*this);
598 /* explicit template instantiation required for some systems */
600 template class ACLStrategised
<Ip::Address
>;
602 ACL::Prototype
ACLASN::SourceRegistryProtoype(&ACLASN::SourceRegistryEntry_
, "src_as");
604 ACLStrategised
<Ip::Address
> ACLASN::SourceRegistryEntry_(new ACLASN
, ACLSourceASNStrategy::Instance(), "src_as");
606 ACL::Prototype
ACLASN::DestinationRegistryProtoype(&ACLASN::DestinationRegistryEntry_
, "dst_as");
608 ACLStrategised
<Ip::Address
> ACLASN::DestinationRegistryEntry_(new ACLASN
, ACLDestinationASNStrategy::Instance(), "dst_as");
611 ACLSourceASNStrategy::match (ACLData
<Ip::Address
> * &data
, ACLFilledChecklist
*checklist
, ACLFlags
&)
613 return data
->match(checklist
->src_addr
);
616 ACLSourceASNStrategy
*
617 ACLSourceASNStrategy::Instance()
622 ACLSourceASNStrategy
ACLSourceASNStrategy::Instance_
;
625 ACLDestinationASNStrategy::match (ACLData
<MatchType
> * &data
, ACLFilledChecklist
*checklist
, ACLFlags
&)
627 const ipcache_addrs
*ia
= ipcache_gethostbyname(checklist
->request
->GetHost(), IP_LOOKUP_IF_MISS
);
630 for (int k
= 0; k
< (int) ia
->count
; ++k
) {
631 if (data
->match(ia
->in_addrs
[k
]))
637 } else if (!checklist
->request
->flags
.destinationIpLookedUp
) {
638 /* No entry in cache, lookup not attempted */
639 /* XXX FIXME: allow accessing the acl name here */
640 debugs(28, 3, "asnMatchAcl: Can't yet compare '" << "unknown" /*name*/ << "' ACL for '" << checklist
->request
->GetHost() << "'");
641 checklist
->changeState (DestinationIPLookup::Instance());
645 return data
->match(noaddr
);
651 ACLDestinationASNStrategy
*
652 ACLDestinationASNStrategy::Instance()
657 ACLDestinationASNStrategy
ACLDestinationASNStrategy::Instance_
;