1 : //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Url Classifier code
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Dave Camp <dcamp@mozilla.com>
24 : * Gian-Carlo Pascutto <gpascutto@mozilla.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef LookupCache_h__
41 : #define LookupCache_h__
42 :
43 : #include "Entries.h"
44 : #include "nsString.h"
45 : #include "nsTArray.h"
46 : #include "nsAutoPtr.h"
47 : #include "nsCOMPtr.h"
48 : #include "nsIFile.h"
49 : #include "nsUrlClassifierPrefixSet.h"
50 : #include "prlog.h"
51 :
52 : namespace mozilla {
53 : namespace safebrowsing {
54 :
55 : #define MAX_HOST_COMPONENTS 5
56 : #define MAX_PATH_COMPONENTS 4
57 :
58 346 : class LookupResult {
59 : public:
60 116 : LookupResult() : mComplete(false), mNoise(false), mFresh(false), mProtocolConfirmed(false) {}
61 :
62 : // The fragment that matched in the LookupCache
63 : union {
64 : Prefix prefix;
65 : Completion complete;
66 : } hash;
67 :
68 13 : const Prefix &PrefixHash() { return hash.prefix; }
69 194 : const Completion &CompleteHash() { return hash.complete; }
70 :
71 444 : bool Confirmed() const { return (mComplete && mFresh) || mProtocolConfirmed; }
72 0 : bool Complete() const { return mComplete; }
73 :
74 : // True if we have a complete match for this hash in the table.
75 : bool mComplete;
76 :
77 : // True if this is a noise entry, i.e. an extra entry
78 : // that is inserted to mask the true URL we are requesting
79 : bool mNoise;
80 :
81 : // Value of actual key looked up in the prefixset (coded with client key)
82 : Prefix mCodedPrefix;
83 :
84 : // True if we've updated this table recently-enough.
85 : bool mFresh;
86 :
87 : bool mProtocolConfirmed;
88 :
89 : nsCString mTableName;
90 : };
91 :
92 : typedef nsTArray<LookupResult> LookupResultArray;
93 :
94 188 : struct CacheResult {
95 : AddComplete entry;
96 : nsCString table;
97 : };
98 : typedef nsTArray<CacheResult> CacheResultArray;
99 :
100 : class LookupCache {
101 : public:
102 : // Check for a canonicalized IP address.
103 : static bool IsCanonicalizedIP(const nsACString& aHost);
104 :
105 : // take a lookup string (www.hostname.com/path/to/resource.html) and
106 : // expand it into the set of fragments that should be searched for in an
107 : // entry
108 : static nsresult GetLookupFragments(const nsACString& aSpec,
109 : nsTArray<nsCString>* aFragments);
110 : // Similar to GetKey(), but if the domain contains three or more components,
111 : // two keys will be returned:
112 : // hostname.com/foo/bar -> [hostname.com]
113 : // mail.hostname.com/foo/bar -> [hostname.com, mail.hostname.com]
114 : // www.mail.hostname.com/foo/bar -> [hostname.com, mail.hostname.com]
115 : static nsresult GetHostKeys(const nsACString& aSpec,
116 : nsTArray<nsCString>* aHostKeys);
117 : // Get the database key for a given URI. This is the top three
118 : // domain components if they exist, otherwise the top two.
119 : // hostname.com/foo/bar -> hostname.com
120 : // mail.hostname.com/foo/bar -> mail.hostname.com
121 : // www.mail.hostname.com/foo/bar -> mail.hostname.com
122 : static nsresult GetKey(const nsACString& aSpec, Completion* aHash,
123 : nsCOMPtr<nsICryptoHash>& aCryptoHash);
124 :
125 : /* We have both a prefix and a domain. Drop the domain, but
126 : hash the domain, the prefix and a random value together,
127 : ensuring any collisions happens at a different points for
128 : different users.
129 : */
130 : static nsresult KeyedHash(PRUint32 aPref, PRUint32 aDomain,
131 : PRUint32 aKey, PRUint32* aOut);
132 :
133 : LookupCache(const nsACString& aTableName, nsIFile* aStoreFile);
134 : ~LookupCache();
135 :
136 570 : const nsCString &TableName() const { return mTableName; }
137 :
138 : nsresult Init();
139 : nsresult Open();
140 : // This will Clear() the passed arrays when done.
141 : nsresult Build(AddPrefixArray& aAddPrefixes,
142 : AddCompleteArray& aAddCompletes);
143 : nsresult GetPrefixes(nsTArray<PRUint32>* aAddPrefixes);
144 :
145 : #if DEBUG && defined(PR_LOGGING)
146 : void Dump();
147 : #endif
148 : nsresult WriteFile();
149 : nsresult Has(const Completion& aCompletion,
150 : const Completion& aHostkey,
151 : PRUint32 aHashKey,
152 : bool* aHas, bool* aComplete,
153 : Prefix* aOrigPrefix);
154 : bool IsPrimed();
155 :
156 : private:
157 :
158 : void Clear();
159 : nsresult Reset();
160 : void UpdateHeader();
161 : nsresult ReadHeader();
162 : nsresult EnsureSizeConsistent();
163 : nsresult ReadCompletions();
164 : nsresult LoadPrefixSet();
165 : // Construct a Prefix Set with known prefixes.
166 : // This will Clear() aAddPrefixes when done.
167 : nsresult ConstructPrefixSet(AddPrefixArray& aAddPrefixes);
168 :
169 : struct Header {
170 : uint32 magic;
171 : uint32 version;
172 : uint32 numCompletions;
173 : };
174 : Header mHeader;
175 :
176 : bool mPrimed;
177 : nsCString mTableName;
178 : nsCOMPtr<nsIFile> mStoreDirectory;
179 : nsCOMPtr<nsIInputStream> mInputStream;
180 : CompletionArray mCompletions;
181 : // Set of prefixes known to be in the database
182 : nsRefPtr<nsUrlClassifierPrefixSet> mPrefixSet;
183 : };
184 :
185 : }
186 : }
187 :
188 : #endif
|