dump: code style corrections

refs #3683

Change-Id: I694e04f418fd084f4f1b59693a336dea1b205ad3
diff --git a/tools/dump/ndndump.cpp b/tools/dump/ndndump.cpp
index e964c58..95875a9 100644
--- a/tools/dump/ndndump.cpp
+++ b/tools/dump/ndndump.cpp
@@ -64,6 +64,17 @@
 
 const size_t MAX_SNAPLEN = 65535;
 
+Ndndump::Ndndump()
+  : isVerbose(false)
+  , pcapProgram("(ether proto 0x8624) || (tcp port 6363) || (udp port 6363)")
+  // , isSuccinct(false)
+  // , isMatchInverted(false)
+  // , shouldPrintStructure(false)
+  // , isTcpOnly(false)
+  // , isUdpOnly(false)
+{
+}
+
 void
 Ndndump::run()
 {
@@ -71,7 +82,7 @@
     char errbuf[PCAP_ERRBUF_SIZE];
     const char* pcapDevice = pcap_lookupdev(errbuf);
 
-    if (pcapDevice == 0) {
+    if (pcapDevice == nullptr) {
       throw Error(errbuf);
     }
 
@@ -94,14 +105,14 @@
   if (!interface.empty()) {
     char errbuf[PCAP_ERRBUF_SIZE];
     m_pcap = pcap_open_live(interface.c_str(), MAX_SNAPLEN, 0, 1000, errbuf);
-    if (m_pcap == 0) {
+    if (m_pcap == nullptr) {
       throw Error("Cannot open interface " + interface + "(" + errbuf + ")");
     }
   }
   else {
     char errbuf[PCAP_ERRBUF_SIZE];
     m_pcap = pcap_open_offline(inputFile.c_str(), errbuf);
-    if (m_pcap == 0) {
+    if (m_pcap == nullptr) {
       throw Error("Cannot file " + inputFile + " for reading (" + errbuf + ")");
     }
   }
@@ -112,34 +123,32 @@
     }
 
     bpf_program program;
-    int returnValue = pcap_compile(m_pcap, &program, pcapProgram.c_str(), 0, PCAP_NETMASK_UNKNOWN);
+    int res = pcap_compile(m_pcap, &program, pcapProgram.c_str(), 0, PCAP_NETMASK_UNKNOWN);
 
-    if (returnValue < 0) {
+    if (res < 0) {
       throw Error("Cannot parse tcpdump expression '" + pcapProgram +
                   "' (" + pcap_geterr(m_pcap) + ")");
     }
 
-    returnValue = pcap_setfilter(m_pcap, &program);
+    res = pcap_setfilter(m_pcap, &program);
     pcap_freecode(&program);
 
-    if (returnValue < 0) {
+    if (res < 0) {
       throw Error(std::string("pcap_setfilter failed (") + pcap_geterr(m_pcap) + ")");
     }
   }
 
   m_dataLinkType = pcap_datalink(m_pcap);
-  if (m_dataLinkType != DLT_EN10MB &&
-      m_dataLinkType != DLT_PPP)
-    {
-      throw Error("Unsupported pcap format (" + boost::lexical_cast<std::string>(m_dataLinkType));
-    }
+  if (m_dataLinkType != DLT_EN10MB && m_dataLinkType != DLT_PPP) {
+    throw Error("Unsupported pcap format (" + to_string(m_dataLinkType));
+  }
 
   pcap_loop(m_pcap, -1, &Ndndump::onCapturedPacket, reinterpret_cast<uint8_t*>(this));
 }
 
 
 void
-Ndndump::onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet)
+Ndndump::onCapturedPacket(const pcap_pkthdr* header, const uint8_t* packet) const
 {
   std::ostringstream os;
   printInterceptTime(os, header);
@@ -153,8 +162,8 @@
     return;
   }
 
-  int returnValue = skipAndProcessFrameHeader(frameType, payload, payloadSize, os);
-  if (returnValue < 0) {
+  int res = skipAndProcessFrameHeader(frameType, payload, payloadSize, os);
+  if (res < 0) {
     return;
   }
 
@@ -223,13 +232,13 @@
       std::cout << os.str() << ", " << "UNKNOWN-NETWORK-PACKET" << std::endl;
     }
   }
-  catch (tlv::Error& e) {
+  catch (const tlv::Error& e) {
     std::cerr << e.what() << std::endl;
   }
 }
 
 void
-Ndndump::printInterceptTime(std::ostream& os, const pcap_pkthdr* header)
+Ndndump::printInterceptTime(std::ostream& os, const pcap_pkthdr* header) const
 {
   os << header->ts.tv_sec
      << "."
@@ -251,13 +260,12 @@
 }
 
 int
-Ndndump::skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize)
+Ndndump::skipDataLinkHeaderAndGetFrameType(const uint8_t*& payload, ssize_t& payloadSize) const
 {
   int frameType = 0;
 
   switch (m_dataLinkType) {
-  case DLT_EN10MB: // Ethernet frames can have Ethernet or 802.3 encapsulation
-    {
+    case DLT_EN10MB: { // Ethernet frames can have Ethernet or 802.3 encapsulation
       const ether_header* etherHeader = reinterpret_cast<const ether_header*>(payload);
 
       if (payloadSize < 0) {
@@ -271,16 +279,15 @@
 
       break;
     }
-  case DLT_PPP:
-    {
+    case DLT_PPP: {
       frameType = *payload;
-      payloadSize--;
-      payload++;
+      --payloadSize;
+      ++payload;
 
       if (!(frameType & 1)) {
         frameType = (frameType << 8) | *payload;
-        payloadSize--;
-        payload++;
+        --payloadSize;
+        ++payload;
       }
 
       if (payloadSize < 0) {
@@ -298,78 +305,74 @@
 int
 Ndndump::skipAndProcessFrameHeader(int frameType,
                                    const uint8_t*& payload, ssize_t& payloadSize,
-                                   std::ostream& os)
+                                   std::ostream& os) const
 {
-  switch (frameType)
-    {
-    case /*ETHERTYPE_IP*/0x0800:
-    case DLT_EN10MB: // pcap encapsulation
-      {
-        const ip* ipHeader = reinterpret_cast<const ip*>(payload);
-        size_t ipHeaderSize = IP_HL(ipHeader) * 4;
-        if (ipHeaderSize < 20) {
-          std::cerr << "invalid IP header len " << ipHeaderSize << " bytes" << std::endl;
-          return -1;
-        }
+  switch (frameType) {
+    case 0x0800: // ETHERTYPE_IP
+    case DLT_EN10MB: { // pcap encapsulation
+      const ip* ipHeader = reinterpret_cast<const ip*>(payload);
+      size_t ipHeaderSize = IP_HL(ipHeader) * 4;
+      if (ipHeaderSize < 20) {
+        std::cerr << "invalid IP header len " << ipHeaderSize << " bytes" << std::endl;
+        return -1;
+      }
 
-        os << "From: " << inet_ntoa(ipHeader->ip_src) << ", ";
-        os << "To: "   << inet_ntoa(ipHeader->ip_dst);
+      os << "From: " << inet_ntoa(ipHeader->ip_src) << ", ";
+      os << "To: "   << inet_ntoa(ipHeader->ip_dst);
 
-        payloadSize -= ipHeaderSize;
-        payload += ipHeaderSize;
+      payloadSize -= ipHeaderSize;
+      payload += ipHeaderSize;
 
-        if (payloadSize < 0) {
-          std::cerr << "Invalid pcap IP packet" << std::endl;
-          return -1;
-        }
+      if (payloadSize < 0) {
+        std::cerr << "Invalid pcap IP packet" << std::endl;
+        return -1;
+      }
 
-        switch (ipHeader->ip_p) {
-        case IPPROTO_UDP:
-          {
-            // if (!flags.udp)
-            //   return -1;
+      switch (ipHeader->ip_p) {
+        case IPPROTO_UDP: {
+          // if (!flags.udp)
+          //   return -1;
 
-            payloadSize -= sizeof(udphdr);
-            payload += sizeof(udphdr);
+          payloadSize -= sizeof(udphdr);
+          payload += sizeof(udphdr);
 
-            if (payloadSize < 0) {
-                std::cerr << "Invalid pcap UDP/IP packet" << std::endl;
-                return -1;
-              }
-
-            os << ", Tunnel Type: UDP";
-            break;
+          if (payloadSize < 0) {
+            std::cerr << "Invalid pcap UDP/IP packet" << std::endl;
+            return -1;
           }
-        case IPPROTO_TCP:
-          {
-            // if (!flags.tcp)
-            //   return -1;
 
-            const tcphdr* tcpHeader = reinterpret_cast<const tcphdr*>(payload);
-            size_t tcpHeaderSize = TH_OFF(tcpHeader) * 4;
+          os << ", Tunnel Type: UDP";
+          break;
+        }
+        case IPPROTO_TCP: {
+          // if (!flags.tcp)
+          //   return -1;
 
-            if (tcpHeaderSize < 20) {
-              std::cerr << "Invalid TCP Header len: "<< tcpHeaderSize <<" bytes" << std::endl;
-              return -1;
-            }
+          const tcphdr* tcpHeader = reinterpret_cast<const tcphdr*>(payload);
+          size_t tcpHeaderSize = TH_OFF(tcpHeader) * 4;
 
-            payloadSize -= tcpHeaderSize;
-            payload += tcpHeaderSize;
-
-            if (payloadSize < 0) {
-              std::cerr << "Invalid pcap TCP/IP packet" << std::endl;
-              return -1;
-            }
-
-            os << ", Tunnel Type: TCP";
-            break;
+          if (tcpHeaderSize < 20) {
+            std::cerr << "Invalid TCP Header len: " << tcpHeaderSize <<" bytes" << std::endl;
+            return -1;
           }
+
+          payloadSize -= tcpHeaderSize;
+          payload += tcpHeaderSize;
+
+          if (payloadSize < 0) {
+            std::cerr << "Invalid pcap TCP/IP packet" << std::endl;
+            return -1;
+          }
+
+          os << ", Tunnel Type: TCP";
+          break;
+        }
         default:
           return -1;
-        }
-
-        break;
       }
+
+      break;
+    }
     case /*ETHERTYPE_NDN*/0x7777:
       os << "Tunnel Type: EthernetFrame";
       break;
@@ -381,13 +384,23 @@
       payloadSize -= 2;
       payload += 2;
       break;
-    default:
+    default: // do nothing if it is not a recognized type of a packet
       return -1;
-      break; // do nothing if it is not a recognized type of a packet
-    }
+  }
 
   return 0;
 }
 
+bool
+Ndndump::matchesFilter(const Name& name) const
+{
+  if (nameFilter.empty())
+    return true;
+
+  /// \todo Switch to NDN regular expressions
+
+  return boost::regex_match(name.toUri(), nameFilter);
+}
+
 } // namespace dump
 } // namespace ndn