2013-12-11 23:03:51 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
|
|
|
/* 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/. */
|
|
|
|
|
2017-05-26 04:08:00 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2014-08-26 13:09:00 +04:00
|
|
|
#include "mozilla/net/ChildDNSService.h"
|
2013-12-11 23:03:51 +04:00
|
|
|
#include "mozilla/net/DNSRequestChild.h"
|
|
|
|
#include "mozilla/net/NeckoChild.h"
|
2019-08-05 23:54:01 +03:00
|
|
|
#include "mozilla/net/SocketProcessChild.h"
|
2017-03-24 01:33:00 +03:00
|
|
|
#include "mozilla/SystemGroup.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2013-12-11 23:03:51 +04:00
|
|
|
#include "nsIDNSRecord.h"
|
2018-09-22 23:54:11 +03:00
|
|
|
#include "nsIDNSByTypeRecord.h"
|
2013-12-11 23:03:51 +04:00
|
|
|
#include "nsHostResolver.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsNetAddr.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// ChildDNSRecord:
|
|
|
|
// A simple class to provide nsIDNSRecord on the child
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class ChildDNSRecord : public nsIDNSRecord {
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIDNSRECORD
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord(const DNSRecord& reply, uint16_t flags);
|
2013-12-11 23:03:51 +04:00
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~ChildDNSRecord() = default;
|
2014-06-24 20:36:44 +04:00
|
|
|
|
2013-12-11 23:03:51 +04:00
|
|
|
nsCString mCanonicalName;
|
|
|
|
nsTArray<NetAddr> mAddresses;
|
|
|
|
uint32_t mCurrent; // addr iterator
|
|
|
|
uint32_t mLength; // number of addrs
|
|
|
|
uint16_t mFlags;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ChildDNSRecord, nsIDNSRecord)
|
2013-12-11 23:03:51 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord::ChildDNSRecord(const DNSRecord& reply, uint16_t flags)
|
2013-12-11 23:03:51 +04:00
|
|
|
: mCurrent(0), mFlags(flags) {
|
|
|
|
mCanonicalName = reply.canonicalName();
|
|
|
|
|
|
|
|
// A shame IPDL gives us no way to grab ownership of array: so copy it.
|
2019-05-01 11:47:10 +03:00
|
|
|
const nsTArray<NetAddr>& addrs = reply.addrs();
|
2013-12-11 23:03:51 +04:00
|
|
|
uint32_t i = 0;
|
|
|
|
mLength = addrs.Length();
|
|
|
|
for (; i < mLength; i++) {
|
|
|
|
mAddresses.AppendElement(addrs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// ChildDNSRecord::nsIDNSRecord
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord::GetCanonicalName(nsACString& result) {
|
2013-12-11 23:03:51 +04:00
|
|
|
if (!(mFlags & nsHostResolver::RES_CANON_NAME)) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = mCanonicalName;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord::IsTRR(bool* retval) {
|
2018-02-01 12:20:49 +03:00
|
|
|
*retval = false;
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2013-12-11 23:03:51 +04:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord::GetNextAddr(uint16_t port, NetAddr* addr) {
|
2013-12-11 23:03:51 +04:00
|
|
|
if (mCurrent >= mLength) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(addr, &mAddresses[mCurrent++], sizeof(NetAddr));
|
|
|
|
|
|
|
|
// both Ipv4/6 use same bits for port, so safe to just use ipv4's field
|
2014-07-23 22:48:05 +04:00
|
|
|
addr->inet.port = htons(port);
|
2013-12-11 23:03:51 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:55:07 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord::GetAddresses(nsTArray<NetAddr>& aAddressArray) {
|
2015-01-30 18:55:07 +03:00
|
|
|
aAddressArray = mAddresses;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-11 23:03:51 +04:00
|
|
|
// shamelessly copied from nsDNSRecord
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord::GetScriptableNextAddr(uint16_t port, nsINetAddr** result) {
|
2013-12-11 23:03:51 +04:00
|
|
|
NetAddr addr;
|
|
|
|
nsresult rv = GetNextAddr(port, &addr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*result = new nsNetAddr(&addr));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// also copied from nsDNSRecord
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord::GetNextAddrAsString(nsACString& result) {
|
2013-12-11 23:03:51 +04:00
|
|
|
NetAddr addr;
|
|
|
|
nsresult rv = GetNextAddr(0, &addr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[kIPv6CStrBufSize];
|
|
|
|
if (NetAddrToString(&addr, buf, sizeof(buf))) {
|
|
|
|
result.Assign(buf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_ERROR("NetAddrToString failed unexpectedly");
|
|
|
|
return NS_ERROR_FAILURE; // conversion failed for some reason
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSRecord::HasMore(bool* result) {
|
2013-12-11 23:03:51 +04:00
|
|
|
*result = mCurrent < mLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ChildDNSRecord::Rewind() {
|
|
|
|
mCurrent = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ChildDNSRecord::ReportUnusable(uint16_t aPort) {
|
|
|
|
// "We thank you for your feedback" == >/dev/null
|
|
|
|
// TODO: we could send info back to parent.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
class ChildDNSByTypeRecord : public nsIDNSByTypeRecord {
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIDNSBYTYPERECORD
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
explicit ChildDNSByTypeRecord(const nsTArray<nsCString>& reply);
|
2018-09-22 23:54:11 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~ChildDNSByTypeRecord() = default;
|
|
|
|
|
|
|
|
nsTArray<nsCString> mRecords;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(ChildDNSByTypeRecord, nsIDNSByTypeRecord)
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSByTypeRecord::ChildDNSByTypeRecord(const nsTArray<nsCString>& reply) {
|
2018-09-22 23:54:11 +03:00
|
|
|
mRecords = reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSByTypeRecord::GetRecords(nsTArray<nsCString>& aRecords) {
|
2018-09-22 23:54:11 +03:00
|
|
|
aRecords = mRecords;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ChildDNSByTypeRecord::GetRecordsAsOneString(nsACString& aRecords) {
|
2018-09-22 23:54:11 +03:00
|
|
|
// deep copy
|
|
|
|
for (uint32_t i = 0; i < mRecords.Length(); i++) {
|
|
|
|
aRecords.Append(mRecords[i]);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-26 13:09:00 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// CancelDNSRequestEvent
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class CancelDNSRequestEvent : public Runnable {
|
2014-08-26 13:09:00 +04:00
|
|
|
public:
|
2019-05-01 11:47:10 +03:00
|
|
|
CancelDNSRequestEvent(DNSRequestChild* aDnsReq, nsresult aReason)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("net::CancelDNSRequestEvent"),
|
|
|
|
mDnsRequest(aDnsReq),
|
2014-08-26 13:09:00 +04:00
|
|
|
mReasonForCancel(aReason) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-08-26 13:09:00 +04:00
|
|
|
if (mDnsRequest->mIPCOpen) {
|
|
|
|
// Send request to Parent process.
|
2017-02-14 07:25:35 +03:00
|
|
|
mDnsRequest->SendCancelDNSRequest(mDnsRequest->mHost, mDnsRequest->mType,
|
|
|
|
mDnsRequest->mOriginAttributes,
|
|
|
|
mDnsRequest->mFlags, mReasonForCancel);
|
2014-08-26 13:09:00 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-08-26 13:09:00 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DNSRequestChild> mDnsRequest;
|
2014-08-26 13:09:00 +04:00
|
|
|
nsresult mReasonForCancel;
|
|
|
|
};
|
|
|
|
|
2013-12-11 23:03:51 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// DNSRequestChild
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
DNSRequestChild::DNSRequestChild(const nsACString& aHost, const uint16_t& aType,
|
|
|
|
const OriginAttributes& aOriginAttributes,
|
|
|
|
const uint32_t& aFlags,
|
|
|
|
nsIDNSListener* aListener,
|
|
|
|
nsIEventTarget* target)
|
2013-12-11 23:03:51 +04:00
|
|
|
: mListener(aListener),
|
|
|
|
mTarget(target),
|
|
|
|
mResultStatus(NS_OK),
|
|
|
|
mHost(aHost),
|
2018-09-22 23:54:11 +03:00
|
|
|
mType(aType),
|
2017-02-14 07:25:35 +03:00
|
|
|
mOriginAttributes(aOriginAttributes),
|
2013-12-11 23:03:51 +04:00
|
|
|
mFlags(aFlags),
|
2014-08-26 13:09:00 +04:00
|
|
|
mIPCOpen(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-12-11 23:03:51 +04:00
|
|
|
void DNSRequestChild::StartRequest() {
|
|
|
|
// we can only do IPDL on the main thread
|
|
|
|
if (!NS_IsMainThread()) {
|
2017-03-24 01:33:00 +03:00
|
|
|
SystemGroup::Dispatch(
|
|
|
|
TaskCategory::Other,
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("net::DNSRequestChild::StartRequest", this,
|
|
|
|
&DNSRequestChild::StartRequest));
|
2013-12-11 23:03:51 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-05 23:54:01 +03:00
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
nsCOMPtr<nsIEventTarget> systemGroupEventTarget =
|
|
|
|
SystemGroup::EventTargetFor(TaskCategory::Other);
|
|
|
|
gNeckoChild->SetEventTargetForActor(this, systemGroupEventTarget);
|
2017-03-24 01:33:00 +03:00
|
|
|
|
2019-08-05 23:54:01 +03:00
|
|
|
mozilla::dom::ContentChild* cc =
|
|
|
|
static_cast<mozilla::dom::ContentChild*>(gNeckoChild->Manager());
|
|
|
|
if (cc->IsShuttingDown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send request to Parent process.
|
|
|
|
gNeckoChild->SendPDNSRequestConstructor(this, mHost, mOriginAttributes,
|
|
|
|
mFlags);
|
|
|
|
} else if (XRE_IsSocketProcess()) {
|
|
|
|
SocketProcessChild* child = SocketProcessChild::GetSingleton();
|
|
|
|
if (!child->CanSend()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-24 01:33:00 +03:00
|
|
|
|
2019-08-05 23:54:01 +03:00
|
|
|
child->SendPDNSRequestConstructor(this, mHost, mOriginAttributes, mFlags);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "Wrong process");
|
2017-05-26 04:08:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-26 13:09:00 +04:00
|
|
|
mIPCOpen = true;
|
2013-12-11 23:03:51 +04:00
|
|
|
|
|
|
|
// IPDL holds a reference until IPDL channel gets destroyed
|
|
|
|
AddIPDLReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DNSRequestChild::CallOnLookupComplete() {
|
|
|
|
MOZ_ASSERT(mListener);
|
|
|
|
mListener->OnLookupComplete(this, mResultRecord, mResultStatus);
|
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
void DNSRequestChild::CallOnLookupByTypeComplete() {
|
|
|
|
MOZ_ASSERT(mListener);
|
|
|
|
MOZ_ASSERT(mType != nsIDNSService::RESOLVE_TYPE_DEFAULT);
|
|
|
|
mListener->OnLookupByTypeComplete(this, mResultByTypeRecords, mResultStatus);
|
|
|
|
}
|
|
|
|
|
2014-08-26 13:09:00 +04:00
|
|
|
mozilla::ipc::IPCResult DNSRequestChild::RecvLookupCompleted(
|
2019-05-01 11:47:10 +03:00
|
|
|
const DNSRequestResponse& reply) {
|
2014-08-26 13:09:00 +04:00
|
|
|
mIPCOpen = false;
|
2013-12-11 23:03:51 +04:00
|
|
|
MOZ_ASSERT(mListener);
|
|
|
|
|
|
|
|
switch (reply.type()) {
|
|
|
|
case DNSRequestResponse::TDNSRecord: {
|
|
|
|
mResultRecord = new ChildDNSRecord(reply.get_DNSRecord(), mFlags);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DNSRequestResponse::Tnsresult: {
|
|
|
|
mResultStatus = reply.get_nsresult();
|
|
|
|
break;
|
|
|
|
}
|
2018-09-22 23:54:11 +03:00
|
|
|
case DNSRequestResponse::TArrayOfnsCString: {
|
|
|
|
MOZ_ASSERT(mType != nsIDNSService::RESOLVE_TYPE_DEFAULT);
|
|
|
|
mResultByTypeRecords =
|
|
|
|
new ChildDNSByTypeRecord(reply.get_ArrayOfnsCString());
|
|
|
|
break;
|
|
|
|
}
|
2013-12-11 23:03:51 +04:00
|
|
|
default:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unknown type");
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_FAIL_NO_REASON(this);
|
2013-12-11 23:03:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
bool targetIsMain = false;
|
|
|
|
if (!mTarget) {
|
|
|
|
targetIsMain = true;
|
|
|
|
} else {
|
|
|
|
mTarget->IsOnCurrentThread(&targetIsMain);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetIsMain) {
|
2018-09-22 23:54:11 +03:00
|
|
|
if (mType == nsIDNSService::RESOLVE_TYPE_DEFAULT) {
|
|
|
|
CallOnLookupComplete();
|
|
|
|
} else {
|
|
|
|
CallOnLookupByTypeComplete();
|
|
|
|
}
|
2013-12-11 23:03:51 +04:00
|
|
|
} else {
|
2018-09-22 23:54:11 +03:00
|
|
|
if (mType == nsIDNSService::RESOLVE_TYPE_DEFAULT) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NewRunnableMethod("net::DNSRequestChild::CallOnLookupComplete", this,
|
|
|
|
&DNSRequestChild::CallOnLookupComplete);
|
|
|
|
mTarget->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NewRunnableMethod("net::DNSRequestChild::CallOnLookupByTypeComplete",
|
|
|
|
this, &DNSRequestChild::CallOnLookupByTypeComplete);
|
|
|
|
mTarget->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2013-12-11 23:03:51 +04:00
|
|
|
}
|
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this);
|
2014-08-26 13:09:00 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2013-12-11 23:03:51 +04:00
|
|
|
}
|
|
|
|
|
2014-08-26 13:09:00 +04:00
|
|
|
void DNSRequestChild::ReleaseIPDLReference() {
|
|
|
|
// Request is done or destroyed. Remove it from the hash table.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ChildDNSService> dnsServiceChild =
|
2014-08-26 13:09:00 +04:00
|
|
|
dont_AddRef(ChildDNSService::GetSingleton());
|
|
|
|
dnsServiceChild->NotifyRequestDone(this);
|
|
|
|
|
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DNSRequestChild::ActorDestroy(ActorDestroyReason why) { mIPCOpen = false; }
|
|
|
|
|
2013-12-11 23:03:51 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// DNSRequestChild::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(DNSRequestChild, nsICancelable)
|
2013-12-11 23:03:51 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// DNSRequestChild::nsICancelable
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DNSRequestChild::Cancel(nsresult reason) {
|
2014-08-26 13:09:00 +04:00
|
|
|
if (mIPCOpen) {
|
|
|
|
// We can only do IPDL on the main thread
|
2017-03-24 01:33:00 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new CancelDNSRequestEvent(this, reason);
|
2017-07-26 11:13:35 +03:00
|
|
|
SystemGroup::Dispatch(TaskCategory::Other, runnable.forget());
|
2014-08-26 13:09:00 +04:00
|
|
|
}
|
2013-12-11 23:03:51 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|