forked from hipstercat/giantsd
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from .packet import Packet
|
|
import random
|
|
|
|
|
|
class EnumQuery:
|
|
LEAD = 0x00
|
|
COMMAND = 0x02
|
|
NO_APPLICATION_GUID = 0x02
|
|
HAS_APPLICATION_GUID = 0x01
|
|
|
|
def __init__(self, packet=None):
|
|
self.Lead = EnumQuery.LEAD
|
|
self.Command = EnumQuery.COMMAND
|
|
self.Payload = random.getrandbits(16)
|
|
self.Type = None
|
|
self.ApplicationGUID = None
|
|
self.ApplicationPayload = None
|
|
|
|
if packet:
|
|
self.parse(packet)
|
|
|
|
def parse(self, packet):
|
|
self.Lead = EnumQuery.LEAD
|
|
self.Command = EnumQuery.COMMAND
|
|
self.Payload = packet.getShort()
|
|
self.Type = packet.getByte()
|
|
if self.Type == EnumQuery.HAS_APPLICATION_GUID:
|
|
self.ApplicationGUID = packet.getBytes(16)
|
|
self.ApplicationPayload = packet.read()
|
|
elif self.Type == EnumQuery.NO_APPLICATION_GUID:
|
|
self.ApplicationPayload = packet.read()
|
|
else:
|
|
raise Exception("EnumQuery type was incorrect: "+self.Type)
|
|
|
|
def to_packet(self):
|
|
packet = Packet()
|
|
packet.putByte(EnumQuery.LEAD)
|
|
packet.putByte(EnumQuery.COMMAND)
|
|
packet.putShort(self.Payload)
|
|
if self.ApplicationGUID:
|
|
packet.putByte(EnumQuery.HAS_APPLICATION_GUID)
|
|
packet.putBytes(self.ApplicationGUID)
|
|
else:
|
|
packet.putByte(EnumQuery.NO_APPLICATION_GUID)
|
|
|
|
if self.ApplicationPayload:
|
|
packet.putBytes(self.ApplicationPayload)
|
|
|
|
return packet
|