2013-01-15 21:26:44 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// This service reads a file of rules describing TLD-like domain names. For a
|
|
|
|
// complete description of the expected file format and parsing rules, see
|
|
|
|
// http://wiki.mozilla.org/Gecko:Effective_TLD_Service
|
|
|
|
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2011-10-10 22:50:08 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsEffectiveTLDService.h"
|
|
|
|
#include "nsIIDNService.h"
|
|
|
|
#include "nsNetUtil.h"
|
2007-10-25 01:14:26 -07:00
|
|
|
#include "prnetdb.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsEffectiveTLDService, nsIEffectiveTLDService,
|
|
|
|
nsIMemoryReporter)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
2012-09-11 04:42:23 -07:00
|
|
|
#define ETLD_STR_NUM_1(line) str##line
|
|
|
|
#define ETLD_STR_NUM(line) ETLD_STR_NUM_1(line)
|
|
|
|
#define ETLD_ENTRY_OFFSET(name) offsetof(struct etld_string_list, ETLD_STR_NUM(__LINE__))
|
|
|
|
|
|
|
|
const ETLDEntry nsDomainEntry::entries[] = {
|
|
|
|
#define ETLD_ENTRY(name, ex, wild) { ETLD_ENTRY_OFFSET(name), ex, wild },
|
|
|
|
#include "etld_data.inc"
|
|
|
|
#undef ETLD_ENTRY
|
|
|
|
};
|
|
|
|
|
|
|
|
const union nsDomainEntry::etld_strings nsDomainEntry::strings = {
|
|
|
|
{
|
|
|
|
#define ETLD_ENTRY(name, ex, wild) name,
|
|
|
|
#include "etld_data.inc"
|
|
|
|
#undef ETLD_ENTRY
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Dummy function to statically ensure that our indices don't overflow
|
|
|
|
// the storage provided for them.
|
|
|
|
void
|
|
|
|
nsDomainEntry::FuncForStaticAsserts(void)
|
|
|
|
{
|
|
|
|
#define ETLD_ENTRY(name, ex, wild) \
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(ETLD_ENTRY_OFFSET(name) < (1 << ETLD_ENTRY_N_INDEX_BITS), \
|
|
|
|
"invalid strtab index");
|
2008-02-14 14:57:20 -08:00
|
|
|
#include "etld_data.inc"
|
2012-09-11 04:42:23 -07:00
|
|
|
#undef ETLD_ENTRY
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ETLD_ENTRY_OFFSET
|
|
|
|
#undef ETLD_STR_NUM
|
|
|
|
#undef ETLD_STR_NUM1
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
2012-10-19 05:07:13 -07:00
|
|
|
static nsEffectiveTLDService *gService = nullptr;
|
|
|
|
|
2013-09-02 01:41:57 -07:00
|
|
|
nsEffectiveTLDService::nsEffectiveTLDService()
|
2013-12-07 22:09:10 -08:00
|
|
|
: mHash(ArrayLength(nsDomainEntry::entries))
|
2013-09-02 01:41:57 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEffectiveTLDService::Init()
|
|
|
|
{
|
|
|
|
const ETLDEntry *entries = nsDomainEntry::entries;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-10-25 01:14:26 -07:00
|
|
|
nsresult rv;
|
|
|
|
mIDNService = do_GetService(NS_IDNSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2008-02-14 14:57:20 -08:00
|
|
|
// Initialize eTLD hash from static array
|
2012-09-11 04:42:23 -07:00
|
|
|
for (uint32_t i = 0; i < ArrayLength(nsDomainEntry::entries); i++) {
|
|
|
|
const char *domain = nsDomainEntry::GetEffectiveTLDName(entries[i].strtab_index);
|
2008-02-14 14:57:20 -08:00
|
|
|
#ifdef DEBUG
|
2012-09-11 04:42:23 -07:00
|
|
|
nsDependentCString name(domain);
|
|
|
|
nsAutoCString normalizedName(domain);
|
2008-02-14 14:57:20 -08:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(NormalizeHostname(normalizedName)),
|
|
|
|
"normalization failure!");
|
|
|
|
NS_ASSERTION(name.Equals(normalizedName), "domain not normalized!");
|
|
|
|
#endif
|
2012-09-11 04:42:23 -07:00
|
|
|
nsDomainEntry *entry = mHash.PutEntry(domain);
|
2008-02-14 14:57:20 -08:00
|
|
|
NS_ENSURE_TRUE(entry, NS_ERROR_OUT_OF_MEMORY);
|
2012-09-11 04:42:23 -07:00
|
|
|
entry->SetData(&entries[i]);
|
2007-07-25 23:31:49 -07:00
|
|
|
}
|
2012-10-19 05:07:13 -07:00
|
|
|
|
|
|
|
MOZ_ASSERT(!gService);
|
|
|
|
gService = this;
|
2013-11-06 21:35:30 -08:00
|
|
|
RegisterWeakMemoryReporter(this);
|
2012-10-19 05:07:13 -07:00
|
|
|
|
2008-02-14 14:57:20 -08:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-10-19 05:07:13 -07:00
|
|
|
nsEffectiveTLDService::~nsEffectiveTLDService()
|
|
|
|
{
|
2013-11-06 21:35:30 -08:00
|
|
|
UnregisterWeakMemoryReporter(this);
|
2012-10-19 05:07:13 -07:00
|
|
|
gService = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-12-07 22:09:10 -08:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(EffectiveTLDServiceMallocSizeOf)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEffectiveTLDService::CollectReports(nsIHandleReportCallback* aHandleReport,
|
2014-05-20 23:06:54 -07:00
|
|
|
nsISupports* aData, bool aAnonymize)
|
2013-11-06 21:35:30 -08:00
|
|
|
{
|
2013-12-07 22:09:10 -08:00
|
|
|
return MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/xpcom/effective-TLD-service", KIND_HEAP, UNITS_BYTES,
|
|
|
|
SizeOfIncludingThis(EffectiveTLDServiceMallocSizeOf),
|
|
|
|
"Memory used by the effective TLD service.");
|
2013-11-06 21:35:30 -08:00
|
|
|
}
|
|
|
|
|
2012-10-19 05:07:13 -07:00
|
|
|
size_t
|
2013-11-06 21:35:30 -08:00
|
|
|
nsEffectiveTLDService::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
|
2012-10-19 05:07:13 -07:00
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += mHash.SizeOfExcludingThis(nullptr, aMallocSizeOf);
|
|
|
|
|
|
|
|
// Measurement of the following members may be added later if DMD finds it is
|
|
|
|
// worthwhile:
|
|
|
|
// - mIDNService
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-10-25 01:14:26 -07:00
|
|
|
// External function for dealing with URI's correctly.
|
|
|
|
// Pulls out the host portion from an nsIURI, and calls through to
|
|
|
|
// GetPublicSuffixFromHost().
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEffectiveTLDService::GetPublicSuffix(nsIURI *aURI,
|
|
|
|
nsACString &aPublicSuffix)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(innerURI);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host;
|
2007-12-04 13:57:31 -08:00
|
|
|
nsresult rv = innerURI->GetAsciiHost(host);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-10-25 01:14:26 -07:00
|
|
|
|
|
|
|
return GetBaseDomainInternal(host, 0, aPublicSuffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
// External function for dealing with URI's correctly.
|
|
|
|
// Pulls out the host portion from an nsIURI, and calls through to
|
|
|
|
// GetBaseDomainFromHost().
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEffectiveTLDService::GetBaseDomain(nsIURI *aURI,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAdditionalParts,
|
2007-10-25 01:14:26 -07:00
|
|
|
nsACString &aBaseDomain)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2013-11-15 18:46:39 -08:00
|
|
|
NS_ENSURE_TRUE( ((int32_t)aAdditionalParts) >= 0, NS_ERROR_INVALID_ARG);
|
2007-10-25 01:14:26 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(innerURI);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host;
|
2007-12-04 13:57:31 -08:00
|
|
|
nsresult rv = innerURI->GetAsciiHost(host);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-10-25 01:14:26 -07:00
|
|
|
|
|
|
|
return GetBaseDomainInternal(host, aAdditionalParts + 1, aBaseDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
// External function for dealing with a host string directly: finds the public
|
|
|
|
// suffix (e.g. co.uk) for the given hostname. See GetBaseDomainInternal().
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEffectiveTLDService::GetPublicSuffixFromHost(const nsACString &aHostname,
|
|
|
|
nsACString &aPublicSuffix)
|
|
|
|
{
|
2007-12-04 13:57:31 -08:00
|
|
|
// Create a mutable copy of the hostname and normalize it to ACE.
|
|
|
|
// This will fail if the hostname includes invalid characters.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString normHostname(aHostname);
|
2007-12-04 13:57:31 -08:00
|
|
|
nsresult rv = NormalizeHostname(normHostname);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return GetBaseDomainInternal(normHostname, 0, aPublicSuffix);
|
2007-10-25 01:14:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// External function for dealing with a host string directly: finds the base
|
|
|
|
// domain (e.g. www.co.uk) for the given hostname and number of subdomain parts
|
|
|
|
// requested. See GetBaseDomainInternal().
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2007-10-25 01:14:26 -07:00
|
|
|
nsEffectiveTLDService::GetBaseDomainFromHost(const nsACString &aHostname,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAdditionalParts,
|
2007-10-25 01:14:26 -07:00
|
|
|
nsACString &aBaseDomain)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-15 18:46:39 -08:00
|
|
|
NS_ENSURE_TRUE( ((int32_t)aAdditionalParts) >= 0, NS_ERROR_INVALID_ARG);
|
|
|
|
|
2007-12-04 13:57:31 -08:00
|
|
|
// Create a mutable copy of the hostname and normalize it to ACE.
|
|
|
|
// This will fail if the hostname includes invalid characters.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString normHostname(aHostname);
|
2007-12-04 13:57:31 -08:00
|
|
|
nsresult rv = NormalizeHostname(normHostname);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return GetBaseDomainInternal(normHostname, aAdditionalParts + 1, aBaseDomain);
|
2007-10-25 01:14:26 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 09:59:49 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEffectiveTLDService::GetNextSubDomain(const nsACString& aHostname,
|
|
|
|
nsACString& aBaseDomain)
|
|
|
|
{
|
|
|
|
// Create a mutable copy of the hostname and normalize it to ACE.
|
|
|
|
// This will fail if the hostname includes invalid characters.
|
|
|
|
nsAutoCString normHostname(aHostname);
|
|
|
|
nsresult rv = NormalizeHostname(normHostname);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return GetBaseDomainInternal(normHostname, -1, aBaseDomain);
|
|
|
|
}
|
|
|
|
|
2007-10-25 01:14:26 -07:00
|
|
|
// Finds the base domain for a host, with requested number of additional parts.
|
|
|
|
// This will fail, generating an error, if the host is an IPv4/IPv6 address,
|
|
|
|
// if more subdomain parts are requested than are available, or if the hostname
|
|
|
|
// includes characters that are not valid in a URL. Normalization is performed
|
|
|
|
// on the host string and the result will be in UTF8.
|
|
|
|
nsresult
|
2007-12-04 13:57:31 -08:00
|
|
|
nsEffectiveTLDService::GetBaseDomainInternal(nsCString &aHostname,
|
2013-02-04 09:59:49 -08:00
|
|
|
int32_t aAdditionalParts,
|
2007-12-04 13:57:31 -08:00
|
|
|
nsACString &aBaseDomain)
|
2007-10-25 01:14:26 -07:00
|
|
|
{
|
|
|
|
if (aHostname.IsEmpty())
|
2010-01-12 10:29:20 -08:00
|
|
|
return NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS;
|
2007-10-25 01:14:26 -07:00
|
|
|
|
|
|
|
// chomp any trailing dot, and keep track of it for later
|
2011-09-28 23:19:26 -07:00
|
|
|
bool trailingDot = aHostname.Last() == '.';
|
2007-07-25 23:31:49 -07:00
|
|
|
if (trailingDot)
|
2007-12-04 13:57:31 -08:00
|
|
|
aHostname.Truncate(aHostname.Length() - 1);
|
2007-07-25 23:31:49 -07:00
|
|
|
|
2010-01-12 10:29:20 -08:00
|
|
|
// check the edge cases of the host being '.' or having a second trailing '.',
|
|
|
|
// since subsequent checks won't catch it.
|
|
|
|
if (aHostname.IsEmpty() || aHostname.Last() == '.')
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2007-10-25 01:14:26 -07:00
|
|
|
// Check if we're dealing with an IPv4/IPv6 hostname, and return
|
|
|
|
PRNetAddr addr;
|
2007-12-04 13:57:31 -08:00
|
|
|
PRStatus result = PR_StringToNetAddr(aHostname.get(), &addr);
|
2007-10-25 01:14:26 -07:00
|
|
|
if (result == PR_SUCCESS)
|
|
|
|
return NS_ERROR_HOST_IS_IP_ADDRESS;
|
|
|
|
|
2008-02-14 14:57:20 -08:00
|
|
|
// Walk up the domain tree, most specific to least specific,
|
|
|
|
// looking for matches at each level. Note that a given level may
|
2007-07-25 23:31:49 -07:00
|
|
|
// have multiple attributes (e.g. IsWild() and IsNormal()).
|
2012-07-30 07:20:58 -07:00
|
|
|
const char *prevDomain = nullptr;
|
2007-12-04 13:57:31 -08:00
|
|
|
const char *currDomain = aHostname.get();
|
2007-07-25 23:31:49 -07:00
|
|
|
const char *nextDot = strchr(currDomain, '.');
|
2007-12-04 13:57:31 -08:00
|
|
|
const char *end = currDomain + aHostname.Length();
|
2007-10-25 01:14:26 -07:00
|
|
|
const char *eTLD = currDomain;
|
2007-07-25 23:31:49 -07:00
|
|
|
while (1) {
|
2010-01-12 10:29:20 -08:00
|
|
|
// sanity check the string we're about to look up: it should not begin with
|
|
|
|
// a '.'; this would mean the hostname began with a '.' or had an
|
|
|
|
// embedded '..' sequence.
|
|
|
|
if (*currDomain == '.')
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// perform the hash lookup.
|
2007-07-25 23:31:49 -07:00
|
|
|
nsDomainEntry *entry = mHash.GetEntry(currDomain);
|
|
|
|
if (entry) {
|
|
|
|
if (entry->IsWild() && prevDomain) {
|
|
|
|
// wildcard rules imply an eTLD one level inferior to the match.
|
2007-10-25 01:14:26 -07:00
|
|
|
eTLD = prevDomain;
|
2007-07-25 23:31:49 -07:00
|
|
|
break;
|
|
|
|
|
2007-09-02 20:03:41 -07:00
|
|
|
} else if (entry->IsNormal() || !nextDot) {
|
|
|
|
// specific match, or we've hit the top domain level
|
2007-10-25 01:14:26 -07:00
|
|
|
eTLD = currDomain;
|
2007-07-25 23:31:49 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
} else if (entry->IsException()) {
|
|
|
|
// exception rules imply an eTLD one level superior to the match.
|
2007-10-25 01:14:26 -07:00
|
|
|
eTLD = nextDot + 1;
|
2007-07-25 23:31:49 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-26 01:55:53 -07:00
|
|
|
if (!nextDot) {
|
2007-10-25 01:14:26 -07:00
|
|
|
// we've hit the top domain level; use it by default.
|
|
|
|
eTLD = currDomain;
|
2007-07-26 01:55:53 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-25 23:31:49 -07:00
|
|
|
prevDomain = currDomain;
|
|
|
|
currDomain = nextDot + 1;
|
|
|
|
nextDot = strchr(currDomain, '.');
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 09:59:49 -08:00
|
|
|
const char *begin, *iter;
|
|
|
|
if (aAdditionalParts < 0) {
|
|
|
|
NS_ASSERTION(aAdditionalParts == -1,
|
2013-11-15 18:46:39 -08:00
|
|
|
"aAdditionalParts can't be negative and different from -1");
|
2007-10-25 01:14:26 -07:00
|
|
|
|
2013-02-04 09:59:49 -08:00
|
|
|
for (iter = aHostname.get(); iter != eTLD && *iter != '.'; iter++);
|
|
|
|
|
|
|
|
if (iter != eTLD) {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
if (iter != eTLD) {
|
|
|
|
aAdditionalParts = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// count off the number of requested domains.
|
|
|
|
begin = aHostname.get();
|
|
|
|
iter = eTLD;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (iter == begin)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (*(--iter) == '.' && aAdditionalParts-- == 0) {
|
|
|
|
++iter;
|
|
|
|
++aAdditionalParts;
|
|
|
|
break;
|
|
|
|
}
|
2007-10-25 01:14:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aAdditionalParts != 0)
|
|
|
|
return NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS;
|
|
|
|
|
|
|
|
aBaseDomain = Substring(iter, end);
|
2007-07-25 23:31:49 -07:00
|
|
|
// add on the trailing dot, if applicable
|
2007-10-25 01:14:26 -07:00
|
|
|
if (trailingDot)
|
|
|
|
aBaseDomain.Append('.');
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-25 23:31:49 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-14 14:57:20 -08:00
|
|
|
// Normalizes the given hostname, component by component. ASCII/ACE
|
|
|
|
// components are lower-cased, and UTF-8 components are normalized per
|
|
|
|
// RFC 3454 and converted to ACE.
|
2007-07-25 23:31:49 -07:00
|
|
|
nsresult
|
|
|
|
nsEffectiveTLDService::NormalizeHostname(nsCString &aHostname)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-14 14:57:20 -08:00
|
|
|
if (!IsASCII(aHostname)) {
|
|
|
|
nsresult rv = mIDNService->ConvertUTF8toACE(aHostname, aHostname);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-02-14 14:57:20 -08:00
|
|
|
ToLowerCase(aHostname);
|
2007-07-25 23:31:49 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|