Message.py 1.77 KB
"""
 @author Georg Hopp

"""
import struct
import random

from ..Message import Message as BaseMessage

class Message(BaseMessage):
    TYPE_A     = 1
    TYPE_NS    = 2
    TYPE_CNAME = 5
    TYPE_SOA   = 6
    TYPE_WKS   = 11
    TYPE_PTR   = 12
    TYPE_HINFO = 13
    TYPE_MINFO = 14
    TYPE_MX    = 15
    TYPE_TXT   = 16

    CLASS_IN = 1
    CLASS_CH = 3
    CLASS_HS = 4

    OP_QUERY = 1

    FLAG_QR = int('1000000000000000', 2)

    def __init__(self, remote, msg=None):
        super(Message, self).__init__(remote)
        """
        if we want to create a response we initialize the message with the request.
        """
        if msg:
            if not msg.isRequest():
                raise Exception('initialize with non request')

            self._msg_id  = msg._msg_id
            self._flags   = msg._flags | Message.FLAG_QR
            self._queries = list(msg._queries)
        else:
            random.seed
            self._msg_id  = random.randint(0, 0xffff)
            self._flags   = 0
            self._queries = []

        self._answers     = []
        self._authoritys  = []
        self._additionals = []

    def isRequest(self):
        return 0 == self._flags & Message.FLAG_QR

    def isResponse(self):
        return not self.isRequest()

    def isCloseMessage(self):
        return False

    def isUpgradeMessage(self):
        return False

    def setRepsonse(self):
        self._flags |= Message.FLAG_QR

    def addQuery(self, name, typ=TYPE_A, cls=CLASS_IN):
        self._queries.append((name, typ, cls))

    def addAnswer(self, name, typ, cls, ttl, data):
        self._answers.append((name, typ, cls, ttl, data))

    def getResponseCode(self):
        return 0

    def getResponseMessage(self):
        return None

# vim: set ft=python et ts=4 sw=4 sts=4: