]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/acl/Asn.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / acl / Asn.cc
index f7d3ca447b1bf5a0ef829bb3d005c6300a07c497..1adb07f360fadc6c9b2a6a672696391df1f69cd6 100644 (file)
@@ -1,36 +1,13 @@
-
 /*
- * DEBUG: section 53    AS Number handling
- * AUTHOR: Duane Wessels, Kostas Anagnostakis
- *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
+/* DEBUG: section 53    AS Number handling */
+
 #include "squid.h"
 #include "acl/Acl.h"
 #include "acl/Asn.h"
@@ -39,7 +16,7 @@
 #include "acl/DestinationIp.h"
 #include "acl/SourceAsn.h"
 #include "cache_cf.h"
-#include "src/forward.h"
+#include "FwdState.h"
 #include "HttpReply.h"
 #include "HttpRequest.h"
 #include "ipcache.h"
 #include "Store.h"
 #include "StoreClient.h"
 #include "StoreClient.h"
-#include "wordlist.h"
 
 #define WHOIS_PORT 43
-#define        AS_REQBUF_SZ    4096
+#define AS_REQBUF_SZ    4096
 
 /* BEGIN of definitions for radix tree entries */
 
@@ -75,7 +51,7 @@ struct squid_radix_node_head *AS_tree_head;
 
 /* explicit instantiation required for some systems */
 
-/// \cond AUTODOCS-IGNORE
+/// \cond AUTODOCS_IGNORE
 template cbdata_type CbDataList<int>::CBDATA_CbDataList;
 /// \endcond
 
@@ -86,13 +62,20 @@ template cbdata_type CbDataList<int>::CBDATA_CbDataList;
  */
 struct as_info {
     CbDataList<int> *as_number;
-    time_t expires;            /* NOTUSED */
+    time_t expires;     /* NOTUSED */
 };
 
-struct ASState {
+class ASState
+{
+    CBDATA_CLASS(ASState);
+
+public:
+    ASState();
+    ~ASState();
+
     StoreEntry *entry;
     store_client *sc;
-    HttpRequest *request;
+    HttpRequest::Pointer request;
     int as_number;
     int64_t offset;
     int reqofs;
@@ -100,6 +83,27 @@ struct ASState {
     bool dataRead;
 };
 
+CBDATA_CLASS_INIT(ASState);
+
+ASState::ASState() :
+    entry(NULL),
+    sc(NULL),
+    request(NULL),
+    as_number(0),
+    offset(0),
+    reqofs(0),
+    dataRead(false)
+{
+    memset(reqbuf, 0, AS_REQBUF_SZ);
+}
+
+ASState::~ASState()
+{
+    debugs(53, 3, entry->url());
+    storeUnregister(sc, entry, this);
+    entry->unlock("~ASState");
+}
+
 /** entry into the radix tree */
 struct rtentry_t {
     struct squid_radix_node e_nodes[2];
@@ -118,8 +122,8 @@ static STCB asHandleReply;
 extern "C" {
 #endif
 
-    static int destroyRadixNode(struct squid_radix_node *rn, void *w);
-    static int printRadixNode(struct squid_radix_node *rn, void *sentry);
+static int destroyRadixNode(struct squid_radix_node *rn, void *w);
+static int printRadixNode(struct squid_radix_node *rn, void *sentry);
 
 #if defined(__cplusplus)
 }
@@ -127,8 +131,6 @@ extern "C" {
 
 void asnAclInitialize(ACL * acls);
 
-static void asStateFree(void *data);
-
 static void destroyRadixNodeInfo(as_info *);
 
 static OBJH asnStats;
@@ -149,10 +151,10 @@ asnMatchIp(CbDataList<int> *data, Ip::Address &addr)
     if (AS_tree_head == NULL)
         return 0;
 
-    if (addr.IsNoAddr())
+    if (addr.isNoAddr())
         return 0;
 
-    if (addr.IsAnyAddr())
+    if (addr.isAnyAddr())
         return 0;
 
     m_addr.addr = addr;
@@ -195,15 +197,13 @@ asnRegisterWithCacheManager(void)
 
 /* initialize the radix tree structure */
 
-SQUIDCEXTERN int squid_max_keylen;     /* yuck.. this is in lib/radix.c */
+SQUIDCEXTERN int squid_max_keylen;  /* yuck.. this is in lib/radix.c */
 
-CBDATA_TYPE(ASState);
 void
 asnInit(void)
 {
     static bool inited = false;
     squid_max_keylen = 40;
-    CBDATA_INIT_TYPE(ASState);
 
     if (!inited) {
         inited = true;
@@ -237,35 +237,25 @@ asnCacheStart(int as)
 {
     LOCAL_ARRAY(char, asres, 4096);
     StoreEntry *e;
-    ASState *asState;
-    asState = cbdataAlloc(ASState);
-    asState->dataRead = 0;
-    debugs(53, 3, "asnCacheStart: AS " << as);
+    ASState *asState = new ASState;
+    debugs(53, 3, "AS " << as);
     snprintf(asres, 4096, "whois://%s/!gAS%d", Config.as_whois_server, as);
     asState->as_number = as;
     asState->request = HttpRequest::CreateFromUrl(asres);
-    assert(NULL != asState->request);
-    HTTPMSGLOCK(asState->request);
+    assert(asState->request != NULL);
 
     if ((e = storeGetPublic(asres, Http::METHOD_GET)) == NULL) {
         e = storeCreateEntry(asres, asres, RequestFlags(), Http::METHOD_GET);
         asState->sc = storeClientListAdd(e, asState);
-        FwdState::fwdStart(Comm::ConnectionPointer(), e, asState->request);
+        FwdState::fwdStart(Comm::ConnectionPointer(), e, asState->request.getRaw());
     } else {
-
-        e->lock();
+        e->lock("Asn");
         asState->sc = storeClientListAdd(e, asState);
     }
 
     asState->entry = e;
-    asState->offset = 0;
-    asState->reqofs = 0;
     StoreIOBuffer readBuffer (AS_REQBUF_SZ, asState->offset, asState->reqbuf);
-    storeClientCopy(asState->sc,
-                    e,
-                    readBuffer,
-                    asHandleReply,
-                    asState);
+    storeClientCopy(asState->sc, e, readBuffer, asHandleReply, asState);
 }
 
 static void
@@ -284,21 +274,21 @@ asHandleReply(void *data, StoreIOBuffer result)
     /* First figure out whether we should abort the request */
 
     if (EBIT_TEST(e->flags, ENTRY_ABORTED)) {
-        asStateFree(asState);
+        delete asState;
         return;
     }
 
     if (result.length == 0 && asState->dataRead) {
-        debugs(53, 3, "asHandleReply: Done: " << e->url()  );
-        asStateFree(asState);
+        debugs(53, 3, "asHandleReply: Done: " << e->url());
+        delete asState;
         return;
     } else if (result.flags.error) {
         debugs(53, DBG_IMPORTANT, "asHandleReply: Called with Error set and size=" << (unsigned int) result.length);
-        asStateFree(asState);
+        delete asState;
         return;
     } else if (e->getReply()->sline.status() != Http::scOkay) {
         debugs(53, DBG_IMPORTANT, "WARNING: AS " << asState->as_number << " whois request failed");
-        asStateFree(asState);
+        delete asState;
         return;
     }
 
@@ -326,7 +316,7 @@ asHandleReply(void *data, StoreIOBuffer result)
         debugs(53, 3, "asHandleReply: AS# " << s << " (" << asState->as_number << ")");
         asnAddNet(s, asState->as_number);
         s = t + 1;
-        asState->dataRead = 1;
+        asState->dataRead = true;
     }
 
     /*
@@ -377,17 +367,6 @@ asHandleReply(void *data, StoreIOBuffer result)
     }
 }
 
-static void
-asStateFree(void *data)
-{
-    ASState *asState = (ASState *)data;
-    debugs(53, 3, "asnStateFree: " << asState->entry->url()  );
-    storeUnregister(asState->sc, asState->entry, asState);
-    asState->entry->unlock();
-    HTTPMSGUNLOCK(asState->request);
-    cbdataFree(asState);
-}
-
 /**
  * add a network (addr, mask) to the radix tree, with matching AS number
  */
@@ -422,8 +401,8 @@ asnAddNet(char *as_string, int as_number)
     t = strchr(as_string, '.');
 
     // generate Netbits Format Mask
-    mask.SetNoAddr();
-    mask.ApplyMask(bitl, (t!=NULL?AF_INET:AF_INET6) );
+    mask.setNoAddr();
+    mask.applyMask(bitl, (t!=NULL?AF_INET:AF_INET6) );
 
     debugs(53, 3, "asnAddNet: called for " << addr << "/" << mask );
 
@@ -460,7 +439,7 @@ asnAddNet(char *as_string, int as_number)
         e->e_info = asinfo;
     }
 
-    if (rn == 0) {             /* assert might expand to nothing */
+    if (rn == 0) {      /* assert might expand to nothing */
         xfree(asinfo);
         delete q;
         xfree(e);
@@ -522,8 +501,8 @@ printRadixNode(struct squid_radix_node *rn, void *_sentry)
     addr = e->e_addr.addr;
     mask = e->e_mask.addr;
     storeAppendPrintf(sentry, "%s/%d\t",
-                      addr.NtoA(buf, MAX_IPSTRLEN),
-                      mask.GetCIDR() );
+                      addr.toStr(buf, MAX_IPSTRLEN),
+                      mask.cidr() );
     asinfo = e->e_info;
     assert(asinfo->as_number);
 
@@ -548,20 +527,21 @@ ACLASN::match(Ip::Address toMatch)
     return asnMatchIp(data, toMatch);
 }
 
-wordlist *
-ACLASN::dump()
+SBufList
+ACLASN::dump() const
 {
-    wordlist *W = NULL;
-    char buf[32];
+    SBufList sl;
+
     CbDataList<int> *ldata = data;
 
     while (ldata != NULL) {
-        snprintf(buf, sizeof(buf), "%d", ldata->element);
-        wordlistAdd(&W, buf);
+        SBuf s;
+        s.Printf("%d", ldata->element);
+        sl.push_back(s);
         ldata = ldata->next;
     }
 
-    return W;
+    return sl;
 }
 
 bool
@@ -636,15 +616,14 @@ ACLDestinationASNStrategy::match (ACLData<MatchType> * &data, ACLFilledChecklist
 
     } else if (!checklist->request->flags.destinationIpLookedUp) {
         /* No entry in cache, lookup not attempted */
-        /* XXX FIXME: allow accessing the acl name here */
-        debugs(28, 3, "asnMatchAcl: Can't yet compare '" << "unknown" /*name*/ << "' ACL for '" << checklist->request->GetHost() << "'");
+        debugs(28, 3, "asnMatchAcl: Can't yet compare '" << AclMatchedName << "' ACL for '" << checklist->request->GetHost() << "'");
         if (checklist->goAsync(DestinationIPLookup::Instance()))
             return -1;
         // else fall through to noaddr match, hiding the lookup failure (XXX)
     }
-        Ip::Address noaddr;
-        noaddr.SetNoAddr();
-        return data->match(noaddr);
+    Ip::Address noaddr;
+    noaddr.setNoAddr();
+    return data->match(noaddr);
 }
 
 ACLDestinationASNStrategy *
@@ -654,3 +633,4 @@ ACLDestinationASNStrategy::Instance()
 }
 
 ACLDestinationASNStrategy ACLDestinationASNStrategy::Instance_;
+