Message.py
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
@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: