]> git.ipfire.org Git - thirdparty/pdns.git/blob - regression-tests.recursor-dnssec/cookiesoption.py
Make sure we can install unsigned packages.
[thirdparty/pdns.git] / regression-tests.recursor-dnssec / cookiesoption.py
1 #!/usr/bin/env python2
2
3 import dns
4 import dns.edns
5 import dns.flags
6 import dns.message
7 import dns.query
8
9 class CookiesOption(dns.edns.Option):
10 """Implementation of draft-ietf-dnsop-cookies-09.
11 """
12
13 def __init__(self, client, server):
14 super(CookiesOption, self).__init__(10)
15
16 if len(client) != 8:
17 raise Exception('invalid client cookie length')
18
19 if server is not None and len(server) != 0 and (len(server) < 8 or len(server) > 32):
20 raise Exception('invalid server cookie length')
21
22 self.client = client
23 self.server = server
24
25 def to_wire(self, file):
26 """Create EDNS packet as defined in draft-ietf-dnsop-cookies-09."""
27
28 file.write(self.client)
29 if self.server and len(self.server) > 0:
30 file.write(self.server)
31
32 def from_wire(cls, otype, wire, current, olen):
33 """Read EDNS packet as defined in draft-ietf-dnsop-cookies-09.
34
35 Returns:
36 An instance of CookiesOption based on the EDNS packet
37 """
38
39 data = wire[current:current + olen]
40 if len(data) != 8 and (len(data) < 16 or len(data) > 40):
41 raise Exception('Invalid EDNS Cookies option')
42
43 client = data[:8]
44 if len(data) > 8:
45 server = data[8:]
46 else:
47 server = None
48
49 return cls(client, server)
50
51 from_wire = classmethod(from_wire)
52
53 def __repr__(self):
54 return '%s(%s, %s)' % (
55 self.__class__.__name__,
56 self.client,
57 self.server
58 )
59
60 def __eq__(self, other):
61 if not isinstance(other, CookiesOption):
62 return False
63 if self.client != other.client:
64 return False
65 if self.server != other.server:
66 return False
67 return True
68
69 def __ne__(self, other):
70 return not self.__eq__(other)
71
72
73 dns.edns._type_to_class[0x000A] = CookiesOption
74