diff --git a/Packet++/header/BgpLayer.h b/Packet++/header/BgpLayer.h index 675aaa9707..05d032addf 100644 --- a/Packet++/header/BgpLayer.h +++ b/Packet++/header/BgpLayer.h @@ -93,6 +93,28 @@ namespace pcpp return OsiModelApplicationLayer; } + /// @struct SerializedFields + /// Fields written by BgpLayer's serializeLayer(), in addition to + /// Layer::SerializedFields. + struct SerializedFields : Layer::SerializedFields + { + /// @return All field descriptors for BgpLayer + static std::vector all() + { + auto result = Layer::SerializedFields::all(); + std::initializer_list extra{ MessageType }; + std::copy(extra.begin(), extra.end(), std::back_inserter(result)); + return result; + } + + /// @brief BGP message type, one of: "OPEN", "UPDATE", "NOTIFICATION", "KEEPALIVE", "ROUTE-REFRESH" + static const FieldDescriptor MessageType; + + // cppcheck-suppress duplInheritedMember + /// Maximum field ID used by the layer. + static constexpr uint16_t MaxID = Layer::SerializedFields::MaxID + 1; + }; + /// Calculates the basic BGP fields: /// - Set marker to all ones /// - Set message type value @@ -100,6 +122,8 @@ namespace pcpp void computeCalculateFields() override; protected: + void serializeLayer(ObjectScope& serializer) const override; + // protected c'tors, this class cannot be instantiated by users BgpLayer() {} @@ -209,7 +233,7 @@ namespace pcpp /// Get a vector of the optional parameters in the message /// @param[out] optionalParameters The vector where the optional parameters will be written to. This method /// doesn't remove any existing data on this vector before pushing data to it - void getOptionalParameters(std::vector& optionalParameters); + void getOptionalParameters(std::vector& optionalParameters) const; /// @return The length in [bytes] of the optional parameters data in the message size_t getOptionalParametersLength(); @@ -236,6 +260,66 @@ namespace pcpp return BgpLayer::Open; } + /// @struct SerializedFields + /// Fields written by BgpOpenMessageLayer's serializeLayer(), in addition to + /// BgpLayer::SerializedFields. + struct SerializedFields : BgpLayer::SerializedFields + { + /// @return All field descriptors for BgpOpenMessageLayer + static std::vector all() + { + auto result = BgpLayer::SerializedFields::all(); + std::initializer_list extra{ Version, MyAutonomousSystem, HoldTime, BgpId, + OptionalParameters }; + std::copy(extra.begin(), extra.end(), std::back_inserter(result)); + return result; + } + + /// @brief BGP version number + static const FieldDescriptor Version; + + /// @brief Autonomous System number of the sender, in host byte order + static const FieldDescriptor MyAutonomousSystem; + + /// @brief Hold Timer value proposed by the sender, in host byte order + static const FieldDescriptor HoldTime; + + /// @brief BGP identifier of the sender, as an IPv4 address string + static const FieldDescriptor BgpId; + + /// @brief Optional parameters as an array of {type, length} objects + static const FieldDescriptor OptionalParameters; + + // cppcheck-suppress duplInheritedMember + /// Maximum field ID used by the layer. + static constexpr uint16_t MaxID = BgpLayer::SerializedFields::MaxID + 5; + + /// @struct OptionalParameterObject + /// Fields of a single BGP OPEN optional parameter. + struct OptionalParameterObject : ObjectFieldDescriptor + { + using ObjectFieldDescriptor::ObjectFieldDescriptor; + + /// @return All field descriptors for an optional parameter + static std::vector all() + { + return { Type, Length }; + } + + /// @brief Parameter type + static const FieldDescriptor Type; + + /// @brief Parameter length, in bytes + static const FieldDescriptor Length; + }; + + /// @brief Single optional parameter element (used as the array item type) + static const OptionalParameterObject OptionalParameter; + }; + + protected: + void serializeLayer(ObjectScope& serializer) const override; + private: /// Serializes the optional parameters to a byte array /// @throws std::invalid_argument if an optional parameter is invalid or the serialized data doesn't fit in @@ -337,7 +421,7 @@ namespace pcpp /// Get a vector of the Withdrawn Routes currently in the message /// @param[out] withdrawnRoutes A reference to a vector the Withdrawn Routes data will be written to - void getWithdrawnRoutes(std::vector& withdrawnRoutes); + void getWithdrawnRoutes(std::vector& withdrawnRoutes) const; /// Set Withdrawn Routes in this message. This method will override any existing Withdrawn Routes in the /// message. If the input is an empty vector all Withdrawn Routes will be removed. This method automatically @@ -358,7 +442,7 @@ namespace pcpp /// Get a vector of the Path Attributes currently in the message /// @param[out] pathAttributes A reference to a vector the Path Attributes data will be written to - void getPathAttributes(std::vector& pathAttributes); + void getPathAttributes(std::vector& pathAttributes) const; /// Set Path Attributes in this message. This method will override any existing Path Attributes in the message. /// If the input is an empty vector all Path Attributes will be removed. This method automatically sets the @@ -379,7 +463,7 @@ namespace pcpp /// Get a vector of the Network Layer Reachability Info currently in the message /// @param[out] nlri A reference to a vector the NLRI data will be written to - void getNetworkLayerReachabilityInfo(std::vector& nlri); + void getNetworkLayerReachabilityInfo(std::vector& nlri) const; /// Set NLRI data in this message. This method will override any existing NLRI data in the message. /// If the input is an empty vector all NLRI data will be removed. This method automatically sets the @@ -402,8 +486,90 @@ namespace pcpp return BgpLayer::Update; } + /// @struct SerializedFields + /// Fields written by BgpUpdateMessageLayer's serializeLayer(), in addition to + /// BgpLayer::SerializedFields. + struct SerializedFields : BgpLayer::SerializedFields + { + /// @return All field descriptors for BgpUpdateMessageLayer + static std::vector all() + { + auto result = BgpLayer::SerializedFields::all(); + std::initializer_list extra{ WithdrawnRoutes, PathAttributes, + NetworkLayerReachabilityInfo }; + std::copy(extra.begin(), extra.end(), std::back_inserter(result)); + return result; + } + + /// @brief Withdrawn routes as an array of {prefix, ipAddr} objects + static const FieldDescriptor WithdrawnRoutes; + + /// @brief Path attributes as an array of {flags, type, length} objects + static const FieldDescriptor PathAttributes; + + /// @brief Network Layer Reachability Information as an array of {prefix, ipAddr} objects + static const FieldDescriptor NetworkLayerReachabilityInfo; + + // cppcheck-suppress duplInheritedMember + /// Maximum field ID used by the layer. + static constexpr uint16_t MaxID = BgpLayer::SerializedFields::MaxID + 3; + + /// @struct PrefixAndIpObject + /// Fields of a single IPv4 prefix + address pair. + struct PrefixAndIpObject : ObjectFieldDescriptor + { + using ObjectFieldDescriptor::ObjectFieldDescriptor; + + /// @return All field descriptors for a prefix + IP pair + static std::vector all() + { + return { Prefix, IpAddr }; + } + + /// @brief IPv4 address mask (8, 16, 24 or 32) + static const FieldDescriptor Prefix; + + /// @brief IPv4 address as a string + static const FieldDescriptor IpAddr; + }; + + /// @brief Single withdrawn route element (used as the array item type) + static const PrefixAndIpObject WithdrawnRoute; + + /// @struct PathAttributeObject + /// Fields of a single BGP path attribute. + struct PathAttributeObject : ObjectFieldDescriptor + { + using ObjectFieldDescriptor::ObjectFieldDescriptor; + + /// @return All field descriptors for a path attribute + static std::vector all() + { + return { Flags, Type, Length }; + } + + /// @brief Path attribute flags + static const FieldDescriptor Flags; + + /// @brief Path attribute type + static const FieldDescriptor Type; + + /// @brief Path attribute length, in bytes + static const FieldDescriptor Length; + }; + + /// @brief Single path attribute element (used as the array item type) + static const PathAttributeObject PathAttribute; + + /// @brief Single NLRI element (used as the array item type) + static const PrefixAndIpObject Nlri; + }; + + protected: + void serializeLayer(ObjectScope& serializer) const override; + private: - void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector& result); + void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector& result) const; size_t prefixAndIPDataToByteArray(const std::vector& prefixAndIpData, uint8_t* resultByteArr, size_t maxByteArrSize); @@ -504,6 +670,34 @@ namespace pcpp return BgpLayer::Notification; } + /// @struct SerializedFields + /// Fields written by BgpNotificationMessageLayer's serializeLayer(), in addition to + /// BgpLayer::SerializedFields. + struct SerializedFields : BgpLayer::SerializedFields + { + /// @return All field descriptors for BgpNotificationMessageLayer + static std::vector all() + { + auto result = BgpLayer::SerializedFields::all(); + std::initializer_list extra{ ErrorCode, ErrorSubCode }; + std::copy(extra.begin(), extra.end(), std::back_inserter(result)); + return result; + } + + /// @brief BGP notification error code + static const FieldDescriptor ErrorCode; + + /// @brief BGP notification error sub-code + static const FieldDescriptor ErrorSubCode; + + // cppcheck-suppress duplInheritedMember + /// Maximum field ID used by the layer. + static constexpr uint16_t MaxID = BgpLayer::SerializedFields::MaxID + 2; + }; + + protected: + void serializeLayer(ObjectScope& serializer) const override; + private: void initMessageData(uint8_t errorCode, uint8_t errorSubCode, const uint8_t* notificationData, size_t notificationDataLen); @@ -544,6 +738,23 @@ namespace pcpp { return BgpLayer::Keepalive; } + + /// @struct SerializedFields + /// Fields written by BgpKeepaliveMessageLayer's serializeLayer(), in addition to + /// BgpLayer::SerializedFields. A KEEPALIVE message carries only the common BGP + /// header, so no message-specific fields are added here. + struct SerializedFields : BgpLayer::SerializedFields + { + /// @return All field descriptors for BgpKeepaliveMessageLayer + static std::vector all() + { + return BgpLayer::SerializedFields::all(); + } + + // cppcheck-suppress duplInheritedMember + /// Maximum field ID used by the layer. + static constexpr uint16_t MaxID = BgpLayer::SerializedFields::MaxID; + }; }; /// @class BgpRouteRefreshMessageLayer @@ -593,6 +804,34 @@ namespace pcpp { return BgpLayer::RouteRefresh; } + + /// @struct SerializedFields + /// Fields written by BgpRouteRefreshMessageLayer's serializeLayer(), in addition to + /// BgpLayer::SerializedFields. + struct SerializedFields : BgpLayer::SerializedFields + { + /// @return All field descriptors for BgpRouteRefreshMessageLayer + static std::vector all() + { + auto result = BgpLayer::SerializedFields::all(); + std::initializer_list extra{ Afi, Safi }; + std::copy(extra.begin(), extra.end(), std::back_inserter(result)); + return result; + } + + /// @brief Address Family Identifier, in host byte order + static const FieldDescriptor Afi; + + /// @brief Subsequent Address Family Identifier, in host byte order + static const FieldDescriptor Safi; + + // cppcheck-suppress duplInheritedMember + /// Maximum field ID used by the layer. + static constexpr uint16_t MaxID = BgpLayer::SerializedFields::MaxID + 2; + }; + + protected: + void serializeLayer(ObjectScope& serializer) const override; }; } // namespace pcpp diff --git a/Packet++/src/BgpLayer.cpp b/Packet++/src/BgpLayer.cpp index 7ae90d8e2a..c9edc3b227 100644 --- a/Packet++/src/BgpLayer.cpp +++ b/Packet++/src/BgpLayer.cpp @@ -181,6 +181,13 @@ namespace pcpp return Layer::shortenLayer(offsetInLayer, numOfBytesToShorten); } + const FieldDescriptor BgpLayer::SerializedFields::MessageType{ Layer::SerializedFields::MaxID + 1, "messageType" }; + + void BgpLayer::serializeLayer(ObjectScope& serializer) const + { + serializer.writeField(SerializedFields::MessageType, getMessageTypeAsString()); + } + // ~~~~~~~~~~~~~~~~~~~~ // BgpOpenMessageLayer // ~~~~~~~~~~~~~~~~~~~~ @@ -280,7 +287,7 @@ namespace pcpp msgHdr->bgpId = newBgpId.toInt(); } - void BgpOpenMessageLayer::getOptionalParameters(std::vector& optionalParameters) + void BgpOpenMessageLayer::getOptionalParameters(std::vector& optionalParameters) const { size_t headerLen = getHeaderLen(); bgp_open_message* msgHdr = getOpenMsgHeader(); @@ -397,6 +404,47 @@ namespace pcpp return setOptionalParameters(std::vector()); } + const FieldDescriptor BgpOpenMessageLayer::SerializedFields::Version{ BgpLayer::SerializedFields::MaxID + 1, + "version" }; + const FieldDescriptor BgpOpenMessageLayer::SerializedFields::MyAutonomousSystem{ + BgpLayer::SerializedFields::MaxID + 2, "myAutonomousSystem" + }; + const FieldDescriptor BgpOpenMessageLayer::SerializedFields::HoldTime{ BgpLayer::SerializedFields::MaxID + 3, + "holdTime" }; + const FieldDescriptor BgpOpenMessageLayer::SerializedFields::BgpId{ BgpLayer::SerializedFields::MaxID + 4, + "bgpId" }; + const FieldDescriptor BgpOpenMessageLayer::SerializedFields::OptionalParameters{ + BgpLayer::SerializedFields::MaxID + 5, "optionalParameters" + }; + const BgpOpenMessageLayer::SerializedFields::OptionalParameterObject + BgpOpenMessageLayer::SerializedFields::OptionalParameter{ 0, "optionalParameter" }; + + const FieldDescriptor BgpOpenMessageLayer::SerializedFields::OptionalParameterObject::Type{ 0, "type" }; + const FieldDescriptor BgpOpenMessageLayer::SerializedFields::OptionalParameterObject::Length{ 1, "length" }; + + void BgpOpenMessageLayer::serializeLayer(ObjectScope& serializer) const + { + BgpLayer::serializeLayer(serializer); + + auto* header = getOpenMsgHeader(); + serializer.writeField(SerializedFields::Version, header->version); + serializer.writeField(SerializedFields::MyAutonomousSystem, be16toh(header->myAutonomousSystem)); + serializer.writeField(SerializedFields::HoldTime, be16toh(header->holdTime)); + serializer.writeField(SerializedFields::BgpId, getBgpId().toString()); + + std::vector optionalParameters; + getOptionalParameters(optionalParameters); + auto optionalParametersArray = serializer.writeArray(SerializedFields::OptionalParameters); + for (const auto& optionalParameter : optionalParameters) + { + auto optionalParameterObject = optionalParametersArray.writeObject(SerializedFields::OptionalParameter); + optionalParameterObject.writeField(BgpOpenMessageLayer::SerializedFields::OptionalParameterObject::Type, + optionalParameter.type); + optionalParameterObject.writeField(BgpOpenMessageLayer::SerializedFields::OptionalParameterObject::Length, + optionalParameter.length); + } + } + // ~~~~~~~~~~~~~~~~~~~~~ // BgpUpdateMessageLayer // ~~~~~~~~~~~~~~~~~~~~~ @@ -457,7 +505,7 @@ namespace pcpp } void BgpUpdateMessageLayer::parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, - std::vector& result) + std::vector& result) const { size_t byteCount = 0; while (byteCount < dataLen) @@ -621,7 +669,7 @@ namespace pcpp return 0; } - void BgpUpdateMessageLayer::getWithdrawnRoutes(std::vector& withdrawnRoutes) + void BgpUpdateMessageLayer::getWithdrawnRoutes(std::vector& withdrawnRoutes) const { size_t withdrawnRouteLen = getWithdrawnRoutesLength(); if (withdrawnRouteLen == 0) @@ -705,7 +753,7 @@ namespace pcpp return setWithdrawnRoutes(std::vector()); } - void BgpUpdateMessageLayer::getPathAttributes(std::vector& pathAttributes) + void BgpUpdateMessageLayer::getPathAttributes(std::vector& pathAttributes) const { size_t pathAttrLen = getPathAttributesLength(); if (pathAttrLen == 0) @@ -803,7 +851,7 @@ namespace pcpp return 0; } - void BgpUpdateMessageLayer::getNetworkLayerReachabilityInfo(std::vector& nlri) + void BgpUpdateMessageLayer::getNetworkLayerReachabilityInfo(std::vector& nlri) const { size_t nlriSize = getNetworkLayerReachabilityInfoLength(); if (nlriSize == 0) @@ -878,6 +926,80 @@ namespace pcpp return setNetworkLayerReachabilityInfo(std::vector()); } + const FieldDescriptor BgpUpdateMessageLayer::SerializedFields::WithdrawnRoutes{ + BgpLayer::SerializedFields::MaxID + 1, "withdrawnRoutes" + }; + const FieldDescriptor BgpUpdateMessageLayer::SerializedFields::PathAttributes{ + BgpLayer::SerializedFields::MaxID + 2, "pathAttributes" + }; + const FieldDescriptor BgpUpdateMessageLayer::SerializedFields::NetworkLayerReachabilityInfo{ + BgpLayer::SerializedFields::MaxID + 3, "networkLayerReachabilityInfo" + }; + + const BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject + BgpUpdateMessageLayer::SerializedFields::WithdrawnRoute{ 0, "withdrawnRoute" }; + const BgpUpdateMessageLayer::SerializedFields::PathAttributeObject + BgpUpdateMessageLayer::SerializedFields::PathAttribute{ 0, "pathAttribute" }; + const BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject BgpUpdateMessageLayer::SerializedFields::Nlri{ + 0, "nlri" + }; + + const FieldDescriptor BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject::Prefix{ 0, "prefix" }; + const FieldDescriptor BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject::IpAddr{ 1, "ipAddr" }; + + const FieldDescriptor BgpUpdateMessageLayer::SerializedFields::PathAttributeObject::Flags{ 0, "flags" }; + const FieldDescriptor BgpUpdateMessageLayer::SerializedFields::PathAttributeObject::Type{ 1, "type" }; + const FieldDescriptor BgpUpdateMessageLayer::SerializedFields::PathAttributeObject::Length{ 2, "length" }; + + void BgpUpdateMessageLayer::serializeLayer(ObjectScope& serializer) const + { + BgpLayer::serializeLayer(serializer); + + { + std::vector withdrawnRoutes; + getWithdrawnRoutes(withdrawnRoutes); + auto withdrawnRoutesArray = serializer.writeArray(SerializedFields::WithdrawnRoutes); + for (const auto& withdrawnRoute : withdrawnRoutes) + { + auto withdrawnRouteObject = withdrawnRoutesArray.writeObject(SerializedFields::WithdrawnRoute); + withdrawnRouteObject.writeField(BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject::Prefix, + withdrawnRoute.prefix); + withdrawnRouteObject.writeField(BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject::IpAddr, + withdrawnRoute.ipAddr.toString()); + } + } + + { + std::vector pathAttributes; + getPathAttributes(pathAttributes); + auto pathAttributesArray = serializer.writeArray(SerializedFields::PathAttributes); + for (const auto& pathAttribute : pathAttributes) + { + auto pathAttributeObject = pathAttributesArray.writeObject(SerializedFields::PathAttribute); + pathAttributeObject.writeField(BgpUpdateMessageLayer::SerializedFields::PathAttributeObject::Flags, + pathAttribute.flags); + pathAttributeObject.writeField(BgpUpdateMessageLayer::SerializedFields::PathAttributeObject::Type, + pathAttribute.type); + pathAttributeObject.writeField(BgpUpdateMessageLayer::SerializedFields::PathAttributeObject::Length, + pathAttribute.length); + } + } + + { + std::vector nlri; + getNetworkLayerReachabilityInfo(nlri); + auto nlriArray = serializer.writeArray(SerializedFields::NetworkLayerReachabilityInfo); + for (const auto& nlriItem : nlri) + { + auto nlriObject = nlriArray.writeObject(SerializedFields::Nlri); + nlriObject.writeField(BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject::Prefix, + nlriItem.prefix); + nlriObject.writeField(BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject::IpAddr, + nlriItem.ipAddr.toString()); + } + } + } + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // BgpNotificationMessageLayer // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -946,7 +1068,7 @@ namespace pcpp std::string BgpNotificationMessageLayer::getNotificationDataAsHexString() const { - uint8_t* notificationData = getNotificationData(); + const uint8_t* notificationData = getNotificationData(); if (notificationData == nullptr) { return ""; @@ -1017,6 +1139,22 @@ namespace pcpp return setNotificationData(newNotificationData, newNotificationDataLen); } + const FieldDescriptor BgpNotificationMessageLayer::SerializedFields::ErrorCode{ + BgpLayer::SerializedFields::MaxID + 1, "errorCode" + }; + const FieldDescriptor BgpNotificationMessageLayer::SerializedFields::ErrorSubCode{ + BgpLayer::SerializedFields::MaxID + 2, "errorSubCode" + }; + + void BgpNotificationMessageLayer::serializeLayer(ObjectScope& serializer) const + { + BgpLayer::serializeLayer(serializer); + + auto* header = getNotificationMsgHeader(); + serializer.writeField(SerializedFields::ErrorCode, header->errorCode); + serializer.writeField(SerializedFields::ErrorSubCode, header->errorSubCode); + } + // ~~~~~~~~~~~~~~~~~~~~~~~~ // BgpKeepaliveMessageLayer // ~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1044,4 +1182,18 @@ namespace pcpp m_Protocol = BGP; } + const FieldDescriptor BgpRouteRefreshMessageLayer::SerializedFields::Afi{ BgpLayer::SerializedFields::MaxID + 1, + "afi" }; + const FieldDescriptor BgpRouteRefreshMessageLayer::SerializedFields::Safi{ BgpLayer::SerializedFields::MaxID + 2, + "safi" }; + + void BgpRouteRefreshMessageLayer::serializeLayer(ObjectScope& serializer) const + { + BgpLayer::serializeLayer(serializer); + + auto* header = getRouteRefreshHeader(); + serializer.writeField(SerializedFields::Afi, be16toh(header->afi)); + serializer.writeField(SerializedFields::Safi, header->safi); + } + } // namespace pcpp diff --git a/Tests/Packet++Test/Tests/BgpTests.cpp b/Tests/Packet++Test/Tests/BgpTests.cpp index 6963986ad1..32aeff9ff7 100644 --- a/Tests/Packet++Test/Tests/BgpTests.cpp +++ b/Tests/Packet++Test/Tests/BgpTests.cpp @@ -7,6 +7,8 @@ #include "TcpLayer.h" #include "BgpLayer.h" #include "SystemUtils.h" +#include "Serializers.h" +#include using pcpp_tests::utils::createPacketAndBufferFromHexResource; using pcpp_tests::utils::createPacketFromHexResource; @@ -40,6 +42,13 @@ PTF_TEST_CASE(BgpLayerParsingTest) } PTF_ASSERT_EQUAL(be16toh(bgpKALayer->getKeepaliveHeader()->length), 19); + { + std::ostringstream oss; + pcpp::JsonSerializer serializer(oss); + bgpKALayer->serialize(serializer); + PTF_ASSERT_EQUAL(oss.str(), R"({"protocolName":"BGP","protocolId":34,"length":19,"messageType":"KEEPALIVE"})"); + } + // parse BGP OPEN message pcpp::Packet bgpOpenPacket(rawPacket2.get()); @@ -76,6 +85,16 @@ PTF_TEST_CASE(BgpLayerParsingTest) PTF_ASSERT_EQUAL(optParam.value[optParamsDataInedx[i]], optParamsData[i]); } + { + std::ostringstream oss; + pcpp::JsonSerializer serializer(oss); + bgpOpenLayer->serialize(serializer); + PTF_ASSERT_EQUAL( + oss.str(), + R"({"protocolName":"BGP","protocolId":34,"length":57,"messageType":"OPEN","version":4,"myAutonomousSystem":1,"holdTime":180,"bgpId":"1.1.1.1","optionalParameters":[)" + R"({"type":2,"length":6},{"type":2,"length":2},{"type":2,"length":2},{"type":2,"length":2},{"type":2,"length":6}]})"); + } + // parse BGP NOTIFICATION message pcpp::Packet bgpNotificationPacket(rawPacket3.get()); @@ -100,6 +119,15 @@ PTF_TEST_CASE(BgpLayerParsingTest) "726520696e666f726d6174696f6e2e"; PTF_ASSERT_EQUAL(bgpNotificationLayer->getNotificationDataAsHexString(), notificationDataAsHexString); + { + std::ostringstream oss; + pcpp::JsonSerializer serializer(oss); + bgpNotificationLayer->serialize(serializer); + PTF_ASSERT_EQUAL( + oss.str(), + R"({"protocolName":"BGP","protocolId":34,"length":146,"messageType":"NOTIFICATION","errorCode":6,"errorSubCode":2})"); + } + pcpp::Packet bgpNotificationNoDataPacket(rawPacket4.get()); PTF_ASSERT_TRUE(bgpNotificationNoDataPacket.isPacketOfType(pcpp::BGP)); @@ -122,6 +150,15 @@ PTF_TEST_CASE(BgpLayerParsingTest) PTF_ASSERT_EQUAL(bgpRRLayer->getRouteRefreshHeader()->safi, 1); PTF_ASSERT_EQUAL(bgpRRLayer->getRouteRefreshHeader()->reserved, 1); + { + std::ostringstream oss; + pcpp::JsonSerializer serializer(oss); + bgpRRLayer->serialize(serializer); + PTF_ASSERT_EQUAL( + oss.str(), + R"({"protocolName":"BGP","protocolId":34,"length":23,"messageType":"ROUTE-REFRESH","afi":1,"safi":1})"); + } + // parse BGP UPDATE message with Withdrawn Routes pcpp::Packet bgpUpdatePacket1(rawPacket6.get()); @@ -154,6 +191,17 @@ PTF_TEST_CASE(BgpLayerParsingTest) PTF_ASSERT_NOT_NULL(bgpUpdateLayer->getNextLayer()); PTF_ASSERT_EQUAL(bgpUpdateLayer->getNextLayer()->getProtocol(), pcpp::BGP, enum); + { + std::ostringstream oss; + pcpp::JsonSerializer serializer(oss); + bgpUpdateLayer->serialize(serializer); + PTF_ASSERT_EQUAL( + oss.str(), R"({"protocolName":"BGP","protocolId":34,"length":38,"messageType":"UPDATE","withdrawnRoutes":[)" + R"({"prefix":24,"ipAddr":"40.1.1.0"},{"prefix":24,"ipAddr":"40.40.40.0"},)" + R"({"prefix":16,"ipAddr":"103.103.0.0"},{"prefix":24,"ipAddr":"103.103.40.0"}],)" + R"("pathAttributes":[],"networkLayerReachabilityInfo":[]})"); + } + // parse BGP UPDATE message with Path Attributes pcpp::Packet bgpUpdatePacket2(rawPacket7.get()); @@ -194,6 +242,16 @@ PTF_TEST_CASE(BgpLayerParsingTest) pcpp::BgpUpdateMessageLayer::prefix_and_ip nlri = nlriVec[0]; PTF_ASSERT_EQUAL(nlri.prefix, 24); PTF_ASSERT_EQUAL(nlri.ipAddr, pcpp::IPv4Address("104.104.40.0")); + + { + std::ostringstream oss; + pcpp::JsonSerializer serializer(oss); + bgpUpdateLayer->serialize(serializer); + PTF_ASSERT_EQUAL( + oss.str(), + R"json({"protocolName":"BGP","protocolId":34,"length":55,"messageType":"UPDATE","withdrawnRoutes":[],"pathAttributes":[{"flags":64,"type":1,"length":1},{"flags":64,"type":2,"length":14},{"flags":64,"type":3,"length":4}],"networkLayerReachabilityInfo":[{"prefix":24,"ipAddr":"104.104.40.0"}]})json"); + } + size_t pathAttrSize[3] = { 28, 24, 0 }; for (int i = 0; i < 3; i++) {