Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / netwerk / dns / ODoH.cpp
blobdd752f74e2904ba76549cb5776d27284b42ae31a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ODoH.h"
9 #include "mozilla/Base64.h"
10 #include "nsIURIMutator.h"
11 #include "ODoHService.h"
12 #include "TRRService.h"
13 // Put DNSLogging.h at the end to avoid LOG being overwritten by other headers.
14 #include "DNSLogging.h"
15 #include "nsNetUtil.h"
17 namespace mozilla {
18 namespace net {
20 NS_IMETHODIMP
21 ODoH::Run() {
22 if (!gODoHService) {
23 RecordReason(TRRSkippedReason::TRR_SEND_FAILED);
24 FailData(NS_ERROR_FAILURE);
25 return NS_OK;
28 if (!gODoHService->ODoHConfigs()) {
29 LOG((
30 "ODoH::Run ODoHConfigs is not available mTriedDownloadODoHConfigs=%d\n",
31 mTriedDownloadODoHConfigs));
32 // Make this lookup fail if we don't have a valid ODoHConfig and we already
33 // tried before.
34 if (NS_SUCCEEDED(gODoHService->UpdateODoHConfig()) &&
35 !mTriedDownloadODoHConfigs) {
36 gODoHService->AppendPendingODoHRequest(this);
37 mTriedDownloadODoHConfigs = true;
38 } else {
39 RecordReason(TRRSkippedReason::ODOH_UPDATE_KEY_FAILED);
40 FailData(NS_ERROR_FAILURE);
41 return NS_OK;
43 return NS_OK;
46 return TRR::Run();
49 DNSPacket* ODoH::GetOrCreateDNSPacket() {
50 if (!mPacket) {
51 mPacket = MakeUnique<ODoHDNSPacket>();
54 return mPacket.get();
57 nsresult ODoH::CreateQueryURI(nsIURI** aOutURI) {
58 nsAutoCString uri;
59 nsCOMPtr<nsIURI> dnsURI;
60 gODoHService->GetRequestURI(uri);
62 nsresult rv = NS_NewURI(getter_AddRefs(dnsURI), uri);
63 if (NS_FAILED(rv)) {
64 return rv;
67 dnsURI.forget(aOutURI);
68 return NS_OK;
71 void ODoH::HandleTimeout() {
72 // If this request is still in the pending queue, it means we can't get the
73 // ODoHConfigs within the timeout.
74 if (gODoHService->RemovePendingODoHRequest(this)) {
75 RecordReason(TRRSkippedReason::ODOH_KEY_NOT_AVAILABLE);
78 TRR::HandleTimeout();
81 void ODoH::HandleEncodeError(nsresult aStatusCode) {
82 MOZ_ASSERT(NS_FAILED(aStatusCode));
84 DNSPacketStatus status = mPacket->PacketStatus();
85 MOZ_ASSERT(status != DNSPacketStatus::Success);
87 if (status == DNSPacketStatus::KeyNotAvailable) {
88 RecordReason(TRRSkippedReason::ODOH_KEY_NOT_AVAILABLE);
89 } else if (status == DNSPacketStatus::KeyNotUsable) {
90 RecordReason(TRRSkippedReason::ODOH_KEY_NOT_USABLE);
91 } else if (status == DNSPacketStatus::EncryptError) {
92 RecordReason(TRRSkippedReason::ODOH_ENCRYPTION_FAILED);
93 } else {
94 MOZ_ASSERT_UNREACHABLE("Unexpected status code.");
98 void ODoH::HandleDecodeError(nsresult aStatusCode) {
99 MOZ_ASSERT(NS_FAILED(aStatusCode));
101 DNSPacketStatus status = mPacket->PacketStatus();
102 MOZ_ASSERT(status != DNSPacketStatus::Success);
104 if (status == DNSPacketStatus::DecryptError) {
105 RecordReason(TRRSkippedReason::ODOH_DECRYPTION_FAILED);
108 TRR::HandleDecodeError(aStatusCode);
111 } // namespace net
112 } // namespace mozilla