Exemple #1
0
    def test_eq(self):
        a = BGPMessage("abc")
        b = BGPMessage("abc")
        self.assertEqual(a, b)

        c = BGPMessage("def")
        self.assertNotEqual(a, c)

        self.assertEqual(a == list, False)
Exemple #2
0
    def __init__(self, payload, length, pcap_information):
        BGPMessage.__init__(self, payload, length, pcap_information)
        self.type = BGPStatics.MESSAGE_TYPE_UPDATE
        self.subtype = BGPStatics.UPDATE_TYPE_NONE

        # Message specific variables
        self.path_attributes = []
        self.path_attributes_length = None

        self.withdrawn_routes = []
        self.withdrawn_routes_length = None

        self.nlri = []

        self.__parse()
Exemple #3
0
    def __parse(self):
        logger = logging.getLogger("pbgpp.BGPPacket.__parse")

        # Split the byte string by BGP's magic marker and filter the empty matches from result list
        messages = re.split(b'(?:\xff){16}', self.payload)
        messages = list(filter(None,
                               messages))  # Fastest solution for filtering

        # Check for empty list (in this case we have a malformed/non-BGP packet)
        if len(messages) == 0:
            raise BGPPacketHasNoMessagesError(
                "parsed packet didn't contain any BGP messages")
        elif self.payload[
                0:
                16] != b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff':  # len(messages) >= 1
            # Check if the payload starts with the magic marker. If not we have to trash that bytes as it's
            # a result of a missing TCP stream reassembly
            messages.pop(0)
        else:
            pass  # contains only valid messages

        # Now iterate through the found messages ...
        for m in messages:
            try:
                # ... and add them to the message list of packet object using a message factory pattern
                self.add_message(BGPMessage.factory(m, self.pcap_information))
            except BGPMessageFactoryError as f:
                # This exception can be raised when no valid message type could be found
                # It's a common exception when there is a malformed packet - therefore: log it as INFO
                logger.info(
                    "BGPMessageFactoryError was raised due to unknown message type during initial packet parsing."
                )
            except BGPPacketError as p:
                # This exception can be raised when system tries to add non-BGPMessage object to message list
                # Uncommon to happen - Better raise an error
                logger.error(
                    "Tried to add a non-BGPMessage object to message list.")
            except BGPError as e:
                # A lot of other things could go wrong - fall back to a warning message
                logger.warning(
                    "Unspecified BGPError raised during initial packet parsing."
                )

        self.__parsed = True
Exemple #4
0
 def __init__(self, payload, length, pcap_information):
     BGPMessage.__init__(self, payload, length, pcap_information)
     self.type = BGPStatics.MESSAGE_TYPE_NOTIFICATION
     self.__parse()
Exemple #5
0
    def test_get_type(self):
        a = BGPMessage("abc")
        a.type = BGPStatics.MESSAGE_TYPE_OPEN

        self.assertEqual(BGPStatics.MESSAGE_TYPE_OPEN, a.get_type())
Exemple #6
0
    def test_get_length(self):
        a = BGPMessage("abc")
        a.length = 10

        self.assertEqual(10, a.get_length())
Exemple #7
0
    def test_len(self):
        a = BGPMessage("abc")
        a.length = 10

        self.assertEqual(10, len(a))
Exemple #8
0
 def test_init(self):
     a = BGPMessage("abc")
     self.assertIsInstance(a, BGPMessage)
Exemple #9
0
 def __init__(self, payload, length, pcap_information):
     BGPMessage.__init__(self, payload, length, pcap_information)
     self.type = BGPStatics.MESSAGE_TYPE_OPEN
     self.optional_parameter = []
     self.__parse()
Exemple #10
0
 def __init__(self, payload, length, pcap_information):
     BGPMessage.__init__(self, payload, length, pcap_information)
     self.type = BGPStatics.MESSAGE_TYPE_KEEPALIVE
     self.__parse()
Exemple #11
0
 def __init__(self, payload, length, pcap_information):
     BGPMessage.__init__(self, payload, length, pcap_information)
     self.type = BGPStatics.MESSAGE_TYPE_ROUTE_REFRESH
     self.__parse()