From 02d5a5ef6bc4ab37aa903df4c21f6f1ca641837e Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Mon, 4 May 2015 12:31:32 -0700 Subject: [PATCH] Adds a function to convert TCP flags into a string --- src/internet/model/tcp-header.cc | 61 ++++++++++++++------------------ src/internet/model/tcp-header.h | 12 ++++++- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/internet/model/tcp-header.cc b/src/internet/model/tcp-header.cc index 7a770eb31..8bb703132 100644 --- a/src/internet/model/tcp-header.cc +++ b/src/internet/model/tcp-header.cc @@ -51,6 +51,31 @@ TcpHeader::~TcpHeader () { } +std::string +TcpHeader::FlagsToString(const uint8_t& flags, const std::string& delimiter) +{ + static const char* flagNames[8] = { + "FIN", + "SYN", + "RST", + "PSH", + "ACK", + "URG", + "ECE", + "CWR" + }; + std::string flagsDescription = ""; + for(int i = 0; i < 8; ++i) + { + if( flags & (1 << i) ) + { + if(flagsDescription.length() > 0) flagsDescription += delimiter; + flagsDescription.append(flagNames[i]); + } + } + return flagsDescription; +} + void TcpHeader::EnableChecksums (void) { @@ -250,41 +275,7 @@ TcpHeader::Print (std::ostream &os) const if (m_flags != 0) { - os<<" ["; - if ((m_flags & FIN) != 0) - { - os<<" FIN "; - } - if ((m_flags & SYN) != 0) - { - os<<" SYN "; - } - if ((m_flags & RST) != 0) - { - os<<" RST "; - } - if ((m_flags & PSH) != 0) - { - os<<" PSH "; - } - if ((m_flags & ACK) != 0) - { - os<<" ACK "; - } - if ((m_flags & URG) != 0) - { - os<<" URG "; - } - if ((m_flags & ECE) != 0) - { - os<<" ECE "; - } - if ((m_flags & CWR) != 0) - { - os<<" CWR "; - } - - os<<"]"; + os<<" [" << FlagsToString(m_flags) <<"]"; } os<<" Seq="<