Skip to content

Commit

Permalink
Move padding constants to their own namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
miodvallat committed Jan 6, 2025
1 parent a6b8e45 commit 2751ed0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
5 changes: 2 additions & 3 deletions pdns/dnspacket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,12 @@ void DNSPacket::wrapup(bool throwsOnTruncation)

if (d_ednspadding) {
size_t remaining = d_tcp ? 65535 : getMaxReplyLen();
const size_t blockSize = 468; // RFC8467 4.1
// Note that optsize already contains the size of the EDNS0 padding
// option header.
size_t modulo = (pw.size() + optsize) % blockSize;
size_t modulo = (pw.size() + optsize) % rfc8467::serverPaddingBlockSize;
size_t padSize = 0;
if (modulo > 0) {
padSize = std::min(blockSize - modulo, remaining);
padSize = std::min(rfc8467::serverPaddingBlockSize - modulo, remaining);
}
opts.emplace_back(EDNSOptionCode::PADDING, makeEDNSPaddingOptString(padSize));
}
Expand Down
6 changes: 6 additions & 0 deletions pdns/ednspadding.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@
#include <string>

std::string makeEDNSPaddingOptString(size_t bytes);

namespace rfc8467 {
// Constants from RFC8467 4.1 "Recommended Strategy: Block-Length Padding"
const size_t clientPaddingBlockSize = 128;

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable clientPaddingBlockSize is never read.
const size_t serverPaddingBlockSize = 468;
}
10 changes: 3 additions & 7 deletions pdns/recursordist/lwres.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,11 @@ static void addPadding(const DNSPacketWriter& pw, size_t bufsize, DNSPacketWrite
const size_t currentSize = pw.getSizeWithOpts(opts);
if (currentSize < (bufsize - 4)) {
const size_t remaining = bufsize - (currentSize + 4);
/* from rfc8467, "4.1. Recommended Strategy: Block-Length Padding":
Clients SHOULD pad queries to the closest multiple of 128 octets.
Note we are in the client role here.
*/
const size_t blockSize = 128;
const size_t modulo = (currentSize + 4) % blockSize;
// Note we are in the client role here.
const size_t modulo = (currentSize + 4) % rfc8467::clientPaddingBlockSize;
size_t padSize = 0;
if (modulo > 0) {
padSize = std::min(blockSize - modulo, remaining);
padSize = std::min(rfc8467::clientPaddingBlockSize - modulo, remaining);
}
opts.emplace_back(EDNSOptionCode::PADDING, makeEDNSPaddingOptString(padSize));
}
Expand Down
11 changes: 2 additions & 9 deletions pdns/recursordist/pdns_recursor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1623,17 +1623,10 @@ void startDoResolve(void* arg) // NOLINT(readability-function-cognitive-complexi

if (currentSize < (maxSize - 4)) {
size_t remaining = maxSize - (currentSize + 4);
/* from rfc8467, "4.1. Recommended Strategy: Block-Length Padding":
If a server receives a query that includes the EDNS(0) "Padding"
option, it MUST pad the corresponding response (see Section 4 of
RFC 7830) and SHOULD pad the corresponding response to a
multiple of 468 octets (see below).
*/
const size_t blockSize = 468;
size_t modulo = (currentSize + 4) % blockSize;
size_t modulo = (currentSize + 4) % rfc8467::serverPaddingBlockSize;
size_t padSize = 0;
if (modulo > 0) {
padSize = std::min(blockSize - modulo, remaining);
padSize = std::min(rfc8467::serverPaddingBlockSize - modulo, remaining);
}
returnedEdnsOptions.emplace_back(EDNSOptionCode::PADDING, makeEDNSPaddingOptString(padSize));
}
Expand Down

0 comments on commit 2751ed0

Please sign in to comment.