Skip to content

Commit e57adb5

Browse files
author
Razvan Becheriu
committed
[#2844] pretty print options with binary data whenever possible
1 parent 2e68060 commit e57adb5

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/lib/dhcp/option.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ std::string Option::toText(int indent) const {
236236
output << setfill('0') << setw(2) << hex
237237
<< static_cast<unsigned short>(data_[i]);
238238
}
239+
if (!data_.size()) {
240+
output << "(no data)";
241+
} else if (str::isPrintable(data_)) {
242+
std::string printable(data_.cbegin(), data_.cend());
243+
output << " (" << printable << ")";
244+
}
239245

240246
// Append suboptions.
241247
output << suboptionsToText(indent + 2);

src/lib/dhcp/option_custom.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#include <dhcp/option_data_types.h>
1010
#include <dhcp/option_custom.h>
1111
#include <exceptions/isc_assert.h>
12+
#include <util/str.h>
1213
#include <util/encode/encode.h>
1314

1415
using namespace isc::asiolink;
16+
using namespace isc::util;
1517

1618
namespace isc {
1719
namespace dhcp {
@@ -398,8 +400,19 @@ OptionCustom::dataFieldToText(const OptionDataType data_type,
398400
// Get the value of the data field.
399401
switch (data_type) {
400402
case OPT_BINARY_TYPE:
401-
text << util::encode::encodeHex(readBinary(index));
403+
{
404+
auto data = readBinary(index);
405+
if (data.empty()) {
406+
text << "(no data)";
407+
} else {
408+
text << util::encode::encodeHex(data);
409+
if (str::isPrintable(data)) {
410+
std::string printable(data.cbegin(), data.cend());
411+
text << " (" << printable << ") ";
412+
}
413+
}
402414
break;
415+
}
403416
case OPT_BOOLEAN_TYPE:
404417
text << (readBoolean(index) ? "true" : "false");
405418
break;
@@ -438,6 +451,7 @@ OptionCustom::dataFieldToText(const OptionDataType data_type,
438451
{
439452
PSIDTuple t = readPsid(index);
440453
text << "len=" << t.first.asUnsigned() << ",psid=" << t.second.asUint16();
454+
break;
441455
}
442456
default:
443457
;

src/lib/dhcp/tests/pkt4_unittest.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,14 +1284,27 @@ TEST_F(Pkt4Test, toText) {
12841284
pkt.addOption(OptionPtr(new Option4AddrLst(123, IOAddress("192.0.2.3"))));
12851285
pkt.addOption(OptionPtr(new OptionUint32(Option::V4, 156, 123456)));
12861286
pkt.addOption(OptionPtr(new OptionString(Option::V4, 87, "lorem ipsum")));
1287+
OptionBuffer data = { 'a', 'b', 'c', 'd', 'e', 'f' };
1288+
OptionPtr opt(new Option(Option::V4, 231, data));
1289+
pkt.addOption(opt);
1290+
OptionBuffer data_sub = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
1291+
OptionPtr sub_opt(new Option(Option::V4, 1, data_sub));
1292+
opt->addOption(sub_opt);
1293+
data_sub.clear();
1294+
sub_opt.reset(new Option(Option::V4, 2, data_sub));
1295+
opt->addOption(sub_opt);
12871296

12881297
EXPECT_EQ("local_address=192.0.2.34:67, remote_address=192.10.33.4:68,\n"
12891298
"msg_type=DHCPDISCOVER (1), trans_id=0x9ef,\n"
12901299
"options:\n"
12911300
" type=053, len=001: 1 (uint8)\n"
12921301
" type=087, len=011: \"lorem ipsum\" (string)\n"
12931302
" type=123, len=004: 192.0.2.3\n"
1294-
" type=156, len=004: 123456 (uint32)",
1303+
" type=156, len=004: 123456 (uint32)\n"
1304+
" type=231, len=021: 61:62:63:64:65:66 (abcdef),\n"
1305+
"options:\n"
1306+
" type=001, len=011: 30:31:32:33:34:35:36:37:38:39:30 (01234567890)\n"
1307+
" type=002, len=000: (no data)",
12951308
pkt.toText());
12961309

12971310
// Now remove all options, including Message Type and check if the
@@ -1300,6 +1313,7 @@ TEST_F(Pkt4Test, toText) {
13001313
pkt.delOption(156);
13011314
pkt.delOption(87);
13021315
pkt.delOption(53);
1316+
pkt.delOption(231);
13031317

13041318
EXPECT_EQ("local_address=192.0.2.34:67, remote_address=192.10.33.4:68,\n"
13051319
"msg_type=(missing), trans_id=0x9ef,\n"

src/lib/dhcp/tests/pkt6_unittest.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,12 +1297,12 @@ TEST_F(Pkt6Test, toText) {
12971297
"2 relay(s):\n"
12981298
"relay[0]: msg-type=12(RELAY_FORWARD), hop-count=1,\n"
12991299
"link-address=2001:888:db8:1::, peer-address=fe80::200:21ff:fe5c:18a9, 2 option(s)\n"
1300-
"type=00018, len=00028: 49:53:41:4d:31:34:34:7c:32:39:39:7c:69:70:76:36:7c:6e:74:3a:76:70:3a:31:3a:31:31:30\n"
1300+
"type=00018, len=00028: 49:53:41:4d:31:34:34:7c:32:39:39:7c:69:70:76:36:7c:6e:74:3a:76:70:3a:31:3a:31:31:30 (ISAM144|299|ipv6|nt:vp:1:110)\n"
13011301
"type=00037, len=00018: 6527 (uint32) 0001000118B033410000215C18A9 (binary)\n"
13021302
"relay[1]: msg-type=12(RELAY_FORWARD), hop-count=0,\n"
13031303
"link-address=::, peer-address=fe80::200:21ff:fe5c:18a9, 2 option(s)\n"
1304-
"type=00018, len=00021: 49:53:41:4d:31:34:34:20:65:74:68:20:31:2f:31:2f:30:35:2f:30:31\n"
1305-
"type=00037, len=00004: 3561 (uint32) (binary)\n";
1304+
"type=00018, len=00021: 49:53:41:4d:31:34:34:20:65:74:68:20:31:2f:31:2f:30:35:2f:30:31 (ISAM144 eth 1/1/05/01)\n"
1305+
"type=00037, len=00004: 3561 (uint32) (no data) (binary)\n";
13061306

13071307
EXPECT_EQ(expected, msg->toText());
13081308
}

0 commit comments

Comments
 (0)