#include <statistics/counter.h>
+#include <boost/optional.hpp>
+
using namespace isc::dns;
using namespace isc::auth;
using namespace isc::statistics;
}
// OPCODE
- try {
- server_msg_counter_.inc(
- opcode_to_msgcounter[msgattrs.getRequestOpCode().getCode()]);
- } catch (const isc::InvalidOperation& ex) {
- // The exception will be thrown if OpCode is not set
- // (e.g. message is short).
- // Increment MSG_OPCODE_OTHER.
- server_msg_counter_.inc(MSG_OPCODE_OTHER);
+ const boost::optional<const isc::dns::Opcode&> opcode =
+ msgattrs.getRequestOpCode();
+ // Increment opcode counter only if the opcode exists.
+ if (opcode) {
+ server_msg_counter_.inc(opcode_to_msgcounter[opcode.get().getCode()]);
}
}
server_msg_counter_.inc(MSG_REQUEST_BADEDNSVER);
}
- try {
- if (msgattrs.getRequestOpCode() == Opcode::QUERY()) {
- // compound attributes
- const unsigned int answer_rrs =
- response.getRRCount(Message::SECTION_ANSWER);
- const bool is_aa_set =
- response.getHeaderFlag(Message::HEADERFLAG_AA);
-
- if (is_aa_set) {
- // QryAuthAns
- server_msg_counter_.inc(MSG_QRYAUTHANS);
- } else {
- // QryNoAuthAns
- server_msg_counter_.inc(MSG_QRYNOAUTHANS);
- }
+ const boost::optional<const isc::dns::Opcode&> opcode =
+ msgattrs.getRequestOpCode();
+ if (opcode && opcode.get() == Opcode::QUERY()) {
+ // compound attributes
+ const unsigned int answer_rrs =
+ response.getRRCount(Message::SECTION_ANSWER);
+ const bool is_aa_set =
+ response.getHeaderFlag(Message::HEADERFLAG_AA);
+
+ if (is_aa_set) {
+ // QryAuthAns
+ server_msg_counter_.inc(MSG_QRYAUTHANS);
+ } else {
+ // QryNoAuthAns
+ server_msg_counter_.inc(MSG_QRYNOAUTHANS);
+ }
- if (rcode == Rcode::NOERROR_CODE) {
- if (answer_rrs > 0) {
- // QrySuccess
- server_msg_counter_.inc(MSG_QRYSUCCESS);
+ if (rcode == Rcode::NOERROR_CODE) {
+ if (answer_rrs > 0) {
+ // QrySuccess
+ server_msg_counter_.inc(MSG_QRYSUCCESS);
+ } else {
+ if (is_aa_set) {
+ // QryNxrrset
+ server_msg_counter_.inc(MSG_QRYNXRRSET);
} else {
- if (is_aa_set) {
- // QryNxrrset
- server_msg_counter_.inc(MSG_QRYNXRRSET);
- } else {
- // QryReferral
- server_msg_counter_.inc(MSG_QRYREFERRAL);
- }
- }
- } else if (rcode == Rcode::REFUSED_CODE) {
- if (!response.getHeaderFlag(Message::HEADERFLAG_RD)) {
- // AuthRej
- server_msg_counter_.inc(MSG_QRYREJECT);
+ // QryReferral
+ server_msg_counter_.inc(MSG_QRYREFERRAL);
}
}
+ } else if (rcode == Rcode::REFUSED_CODE) {
+ if (!response.getHeaderFlag(Message::HEADERFLAG_RD)) {
+ // AuthRej
+ server_msg_counter_.inc(MSG_QRYREJECT);
+ }
}
- } catch (const isc::InvalidOperation& ex) {
- // The exception will be thrown if OpCode is not set
- // (e.g. message is short).
- // Do not increment the counters above since they are incremented
- // only if Opcode is Query.
}
}
std::map<std::string, int> expect;
expect["request.v4"] = 1;
expect["request.udp"] = 1;
- expect["opcode.other"] = 1;
checkStatisticsCounters(stats_after, expect);
}
std::map<std::string, int> expect;
expect["request.v4"] = 3;
expect["request.udp"] = 3;
- expect["opcode.other"] = 3;
checkStatisticsCounters(stats_after, expect);
}
expect["request.v4"] = 1;
expect["request.badednsver"] = 1;
expect["request.udp"] = 1;
- expect["opcode.other"] = 1;
expect["responses"] = 1;
expect["rcode.badvers"] = 1;
checkStatisticsCounters(stats_after, expect);
TEST_F(CountersTest, invalidOperationForGetRequestOpCode) {
MessageAttributes msgattrs;
- // getRequestOpCode() will throw isc::InvalidOperation when called before
+ // getRequestOpCode() should return boost::none when called before
// opcode is set with setRequestOpCode().
- EXPECT_THROW(msgattrs.getRequestOpCode(), isc::InvalidOperation);
+ EXPECT_FALSE(msgattrs.getRequestOpCode());
msgattrs.setRequestOpCode(Opcode::QUERY());
- // getRequestOpCode() will not throw at this point.
- EXPECT_NO_THROW(msgattrs.getRequestOpCode());
+ // getRequestOpCode() should be Opcode::QUERY.
+ EXPECT_EQ(Opcode::QUERY(), msgattrs.getRequestOpCode().get());
}
TEST_F(CountersTest, incrementResponse) {