/*
- * Copyright (C) 2004-2015 Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (C) 2004-2016 Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1995-2003 by Internet Software Consortium
*
* This Source Code Form is subject to the terms of the Mozilla Public
/* DHCP message types. */
enum DHCPMessageType {
+ DHCP_NOTYPE = 0, ///< Message Type option missing
DHCPDISCOVER = 1,
DHCPOFFER = 2,
DHCPREQUEST = 3,
-// Copyright (C) 2011-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
//
// 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
uint8_t Pkt4::getType() const {
OptionPtr generic = getOption(DHO_DHCP_MESSAGE_TYPE);
if (!generic) {
- isc_throw(Unexpected, "Missing DHCP Message Type option");
+ return (DHCP_NOTYPE);
}
// Check if Message Type is specified as OptionInt<uint8_t>
const char*
Pkt4::getName() const {
- return (Pkt4::getName(getType()));
+
+ uint8_t msg_type = 0;
+ try {
+ msg_type = getType();
+ } catch (...) {
+ // Message Type option is missing.
+ }
+
+ return (Pkt4::getName(msg_type));
}
std::string
<< ", remote_adress=" << remote_addr_
<< ":" << remote_port_ << ", msg_type=";
- // Try to obtain message type. This may throw if the Message Type option is
- // not present. Therefore we guard it with try-catch, because we don't want
- // toText method to throw.
- try {
- uint8_t msg_type = getType();
+ // Try to obtain message type.
+ uint8_t msg_type = getType();
+ if (msg_type != DHCP_NOTYPE) {
output << getName(msg_type) << " (" << static_cast<int>(msg_type) << ")";
-
- } catch (...) {
+ } else {
// Message Type option is missing.
output << "(missing)";
}
/// @brief Returns DHCP message type (e.g. 1 = DHCPDISCOVER).
///
+ /// This method is exception safe. For packets without DHCP Message Type
+ /// option, it returns DHCP_NOTYPE (0).
+ ///
/// @return message type
uint8_t getType() const;
/// @brief Returns name of the DHCP message for a given type number.
///
+ /// This method is exception safe. For messages without DHCP Message Type
+ /// options, it returns UNKNOWN.
+ ///
/// @param type DHCPv4 message type which name should be returned.
///
/// @return Pointer to the "const" string containing DHCP message name.
}
+// Sanity check. Verifies that the getName() and getType()
+// don't throw.
+TEST_F(Pkt4Test, getType) {
+
+ Pkt4 pkt(DHCPDISCOVER, 2543);
+ pkt.delOption(DHO_DHCP_MESSAGE_TYPE);
+
+ ASSERT_NO_THROW(pkt.getType());
+ ASSERT_NO_THROW(pkt.getName());
+
+ // The method has to return something that is not NULL,
+ // even if the packet doesn't have Message Type option.
+ EXPECT_TRUE(pkt.getName());
+}
+
} // end of anonymous namespace