Exemple #1
0
    def from_bytes(cls, packet):
        """Create Message from bytes.

        Args:
            packet (bytes): byte representation of the message.
        """
        header, offset = Header.from_bytes(packet), 12

        questions = []
        for _ in range(header.qd_count):
            question, offset = Question.from_bytes(packet, offset)
            questions.append(question)

        answers = []
        for _ in range(header.an_count):
            answer, offset = ResourceRecord.from_bytes(packet, offset)
            answers.append(answer)

        authorities = []
        for _ in range(header.ns_count):
            authority, offset = ResourceRecord.from_bytes(packet, offset)
            authorities.append(authority)

        additionals = []
        for _ in range(header.ar_count):
            additional, offset = ResourceRecord.from_bytes(packet, offset)
            additionals.append(additional)

        return cls(header, questions, answers, authorities, additionals)
Exemple #2
0
 def test_resource_from_bytes(self, MockName, MockRData):
     MockName.from_bytes.return_value = (Name("example.com"), 13)
     MockRData.create_from_bytes.return_value = ARecordData("1.1.1.1")
     packet = (b"\x07example\x03com\x00\x00\x01\x00\x02\x00\x00\x00\x03\x00"
               b"\x04\x01\x01\x01\x01")
     record1, offset = ResourceRecord.from_bytes(packet, 0)
     record2 = ResourceRecord(Name("example.com"), Type.A, Class.CS, 3,
                              ARecordData("1.1.1.1"))
     self.assertEqual(record1, record2)
     MockName.from_bytes.assert_called_with(packet, 0)
     MockRData.create_from_bytes.assert_called_with(Type.A, packet, 23, 4)
    def from_bytes(cls, packet):
        """ Create Message from bytes

        Args:
            packet (bytes): byte representation of the message
        """
        parser = Parser()

        # Parse header
        header, offset = Header.from_bytes(packet), 12

        # Parse questions
        questions = []
        for _ in range(header.qd_count):
            question, offset = Question.from_bytes(packet, offset, parser)
            questions.append(question)

        # Parse answers
        answers = []
        for _ in range(header.an_count):
            answer, offset = ResourceRecord.from_bytes(packet, offset, parser)
            answers.append(answer)

        # Parse authorities
        authorities = []
        for _ in range(header.ns_count):
            authority, offset = ResourceRecord.from_bytes(packet, offset, parser)
            authorities.append(authority)

        # Parse additionals
        additionals = []
        for _ in range(header.ar_count):
            additional, offset = ResourceRecord.from_bytes(packet, offset, parser)
            additionals.append(additional)

        return cls(header, questions, answers, authorities, additionals)