Kea 2.7.6
tsigrecord.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
2//
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/.
6
7#include <config.h>
8
9#include <dns/exceptions.h>
10#include <dns/messagerenderer.h>
11#include <dns/rrclass.h>
12#include <dns/rrttl.h>
13#include <dns/tsigrecord.h>
14#include <util/buffer.h>
15
16#include <ostream>
17#include <string>
18
19using namespace isc::util;
20using namespace isc::dns::rdata;
21
22namespace {
23// Internally used constants:
24
25// Size in octets for the RR type, class TTL, RDLEN fields.
26const size_t RR_COMMON_LEN = 10;
27
28// Size in octets for the fixed part of TSIG RDATAs.
29// - Time Signed (6)
30// - Fudge (2)
31// - MAC Size (2)
32// - Original ID (2)
33// - Error (2)
34// - Other Len (2)
35const size_t RDATA_COMMON_LEN = 16;
36}
37
38namespace isc {
39namespace dns {
41 const rdata::any::TSIG& tsig_rdata) :
42 key_name_(key_name), rdata_(tsig_rdata),
43 length_(RR_COMMON_LEN + RDATA_COMMON_LEN + key_name_.getLength() +
44 rdata_.getAlgorithm().getLength() +
45 rdata_.getMACSize() + rdata_.getOtherLen()) {
46}
47
48namespace {
49// This is a straightforward wrapper of dynamic_cast<const any::TSIG&>.
50// We use this so that we can throw the DNSMessageFORMERR exception when
51// unexpected type of RDATA is detected in the member initialization list
52// of the constructor below.
53const any::TSIG&
54castToTSIGRdata(const rdata::Rdata& rdata) {
55 const any::TSIG* tsig_rdata =
56 dynamic_cast<const any::TSIG*>(&rdata);
57 if (!tsig_rdata) {
59 "TSIG record is being constructed from "
60 "incompatible RDATA: " << rdata.toText());
61 }
62 return (*tsig_rdata);
63}
64}
65
66TSIGRecord::TSIGRecord(const Name& name, const RRClass& rrclass,
67 const RRTTL& ttl, const rdata::Rdata& rdata,
68 size_t length) :
69 key_name_(name), rdata_(castToTSIGRdata(rdata)), length_(length) {
70 if (rrclass != getClass()) {
71 isc_throw(DNSMessageFORMERR, "Unexpected TSIG RR class: " << rrclass);
72 }
73 if (ttl != RRTTL(TSIG_TTL)) {
74 isc_throw(DNSMessageFORMERR, "Unexpected TSIG TTL: " << ttl);
75 }
76}
77
78const RRClass&
80 return (RRClass::ANY());
81}
82
83const RRTTL&
85 static RRTTL ttl(TSIG_TTL);
86 return (ttl);
87}
88
89namespace {
90template <typename OUTPUT>
91void
92toWireCommon(OUTPUT& output, const rdata::any::TSIG& rdata) {
93 // RR type, class, TTL are fixed constants.
94 RRType::TSIG().toWire(output);
96 output.writeUint32(TSIGRecord::TSIG_TTL);
97
98 // RDLEN
99 output.writeUint16(RDATA_COMMON_LEN + rdata.getAlgorithm().getLength() +
100 rdata.getMACSize() + rdata.getOtherLen());
101
102 // TSIG RDATA
103 rdata.toWire(output);
104}
105}
106
107uint32_t
109 // If adding the TSIG would exceed the size limit, don't do it.
110 if (renderer.getLength() + length_ > renderer.getLengthLimit()) {
111 renderer.setTruncated();
112 return (0);
113 }
114
115 // key name = owner. note that we disable compression.
116 renderer.writeName(key_name_, false);
117 toWireCommon(renderer, rdata_);
118 return (1);
119}
120
121uint32_t
123 key_name_.toWire(buffer);
124 toWireCommon(buffer, rdata_);
125 return (1);
126}
127
128std::string
130 return (key_name_.toText() + " " + RRTTL(TSIG_TTL).toText() + " " +
131 getClass().toText() + " " + RRType::TSIG().toText() + " " +
132 rdata_.toText() + "\n");
133}
134
135std::ostream&
136operator<<(std::ostream& os, const TSIGRecord& record) {
137 return (os << record.toText());
138}
139} // namespace dns
140} // namespace isc
The AbstractMessageRenderer class is an abstract base class that provides common interfaces for rende...
virtual void setTruncated()=0
Mark the renderer to indicate truncation has occurred while rendering.
size_t getLength() const
Return the length of data written in the internal buffer.
virtual size_t getLengthLimit() const =0
Return the maximum length of rendered data that can fit in the corresponding DNS message without trun...
virtual void writeName(const Name &name, bool compress=true)=0
Write a Name object into the internal buffer in wire format, with or without name compression.
The Name class encapsulates DNS names.
Definition name.h:219
std::string toText(bool omit_final_dot=false) const
Convert the Name to a string.
Definition name.cc:503
void toWire(AbstractMessageRenderer &renderer) const
Render the Name in the wire format with compression.
Definition name.cc:498
size_t getLength() const
Gets the length of the Name in its wire format.
Definition name.h:356
The RRClass class encapsulates DNS resource record classes.
Definition rrclass.h:89
static const RRClass & ANY()
Definition rrclass.h:298
void toWire(AbstractMessageRenderer &renderer) const
Render the RRClass in the wire format.
Definition rrclass.cc:53
The RRTTL class encapsulates TTLs used in DNS resource records.
Definition rrttl.h:51
static const RRType & TSIG()
Definition rrtype.h:339
void toWire(AbstractMessageRenderer &renderer) const
Render the RRType in the wire format.
Definition rrtype.cc:54
TSIG resource record.
Definition tsigrecord.h:51
static const RRClass & getClass()
Return the RR class of TSIG.
Definition tsigrecord.cc:79
static const uint32_t TSIG_TTL
The TTL value to be used in TSIG RRs.
Definition tsigrecord.h:270
TSIGRecord(const Name &key_name, const rdata::any::TSIG &tsig_rdata)
Constructor from TSIG key name and RDATA.
Definition tsigrecord.cc:40
static const RRTTL & getTTL()
Return the TTL value of TSIG.
Definition tsigrecord.cc:84
std::string toText() const
Convert the TSIG record to a string.
uint32_t toWire(AbstractMessageRenderer &renderer) const
Render the TSIG RR in the wire format.
The Rdata class is an abstract base class that provides a set of common interfaces to manipulate conc...
Definition rdata.h:120
virtual std::string toText() const =0
Convert an Rdata to a string.
rdata::TSIG class represents the TSIG RDATA as defined in RFC2845.
Definition rdataclass.h:44
virtual std::string toText() const
Convert an Rdata to a string.
const Name & getAlgorithm() const
Return the algorithm name.
uint16_t getOtherLen() const
Return the value of the Other Len field.
virtual void toWire(isc::util::OutputBuffer &buffer) const
Render the Rdata in the wire format into a buffer.
uint16_t getMACSize() const
Return the value of the MAC Size field.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
Definition buffer.h:343
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
ostream & operator<<(std::ostream &os, const EDNS &edns)
Insert the EDNS as a string into stream.
Definition edns.cc:163
Defines the logger used by the top-level component of kea-lfc.