1# RADIUS DAS extensions to pyrad
2# Copyright (c) 2014, Jouni Malinen <j@w1.fi>
3#
4# This software may be distributed under the terms of the BSD license.
5# See README for more details.
6
7import hashlib
8import random
9import struct
10import pyrad.packet
11
12class DisconnectPacket(pyrad.packet.Packet):
13    def __init__(self, code=pyrad.packet.DisconnectRequest, id=None,
14                 secret=None, authenticator=None, **attributes):
15        pyrad.packet.Packet.__init__(self, code, id, secret, authenticator,
16                                     **attributes)
17
18    def RequestPacket(self):
19        attr = b''
20        for code, datalst in sorted(self.items()):
21            for data in datalst:
22                attr += self._PktEncodeAttribute(code, data)
23
24        if self.id is None:
25            self.id = random.randrange(0, 256)
26
27        header = struct.pack('!BBH', self.code, self.id, (20 + len(attr)))
28        self.authenticator = hashlib.md5(header[0:4] + 16 * b'\x00' + attr
29            + self.secret).digest()
30        return header + self.authenticator + attr
31
32class CoAPacket(pyrad.packet.Packet):
33    def __init__(self, code=pyrad.packet.CoARequest, id=None,
34                 secret=None, authenticator=None, **attributes):
35        pyrad.packet.Packet.__init__(self, code, id, secret, authenticator,
36                                     **attributes)
37
38    def RequestPacket(self):
39        attr = self._PktEncodeAttributes()
40
41        if self.id is None:
42            self.id = random.randrange(0, 256)
43
44        header = struct.pack('!BBH', self.code, self.id, (20 + len(attr)))
45        self.authenticator = hashlib.md5(header[0:4] + 16 * b'\x00' + attr
46            + self.secret).digest()
47        return header + self.authenticator + attr
48