added license file (license is FreeBSD license)
[gs105e.git] / gs105e.py
1 #!/usr/bin/python
2 import socket 
3 import time
4 import binascii
5 import pprint
6 import struct
7 import random
8
9 class gs105e:
10         def __init__(self,host,port):
11                 self.myhost = host
12                 self.myport = port
13                 self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
14                 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
15                 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
16                 self.socket.bind((host, port))
17                 self.seq = random.randint(100,2000)
18                 self.srcmac = 0x00218698da1e
19
20         def recv(self,recvfunc,maxlen=8192,timeout=0.005):
21                 self.socket.settimeout(timeout)
22                 try:
23                         message, address = self.socket.recvfrom(maxlen)
24                 except socket.timeout:
25                         return None
26                 self.recvfunc(message,address)
27                 self.recv(recvfunc,maxlen,timeout)
28
29         def parse_packet(self,p,hex=0):
30                 data = {}
31                 data["seq"]= p[22:24]
32                 data["cmd"]= p[32:34]
33                 data["mymac"]= p[8:14]
34                 data["theirmac"]= p[14:20]
35                 if hex == 1:
36                         for a in data: 
37                                 data[a]=binascii.hexlify(data[a])
38                 return data
39
40         def recvfunc(self,m,a):
41                 hexdata = self.parse_packet(m,1)
42                 data = self.parse_packet(m)
43
44                 if data["seq"] == "\x00\x01":
45                         pass
46                 print hexdata["theirmac"]
47
48         def send(self,host,port,data):
49                 self.socket.sendto(data,(host,port))
50                 self.seq+=1
51
52
53         def baseudp(self,cmd,ctype=0x0101,destmac=0):
54                 reserved = "\x00"
55                 data = struct.pack(">h",ctype) + 6* reserved + struct.pack('>LHLH', self.srcmac >> 16, self.srcmac & 0xffff, destmac >> 16, destmac & 0xffff) + 2*reserved  
56                 data += struct.pack(">h",self.seq)
57                 data +=  "NSDP" + 4 * reserved + cmd 
58                 return data
59
60         def discover(self):
61                 reserved = "\x00"
62                 cmd = "\x00\x01"
63
64                 data = self.baseudp(cmd)
65                 data += 3 * "\x00" + "\x02" 
66                 data += 3 * "\x00" + "\x03" +  3 * "\x00" + "\x04" + 3* "\x00" + "\x05"
67                 data += 3 * "\x00" + "\x06" +  3 * "\x00" + "\x07" + 3* "\x00" + "\x08"
68                 data += 3 * "\x00" + "\x0b" +  3 * "\x00" + "\x0c" + 3* "\x00" + "\x0d"
69                 data += 3 * "\x00" + "\x0e" +  3 * "\x00" + "\x0f" + 2 * "\x00"
70                 data +=  "\x74" + 3 * "\x00" 
71                 data += 2* "\xff" + 2* "\x00"
72 #               self.send("255.255.255.255",63322, data)
73 #               time.sleep(1)
74                 self.send("255.255.255.255",63322, data)
75                 time.sleep(0.7)
76
77                 data = self.baseudp(cmd)
78                 data += 3 * "\x00" + "\x02" 
79                 data += 3 * "\x00" + "\x03" +  3 * "\x00" + "\x04" + 3* "\x00" + "\x05"
80                 data += 3 * "\x00" + "\x06" +  3 * "\x00" + "\x07" + 3* "\x00" + "\x08"
81                 data += 3 * "\x00" + "\x0b" +  3 * "\x00" + "\x0c" + 3* "\x00" + "\x0d"
82                 data += 3 * "\x00" + "\x0e" +  3 * "\x00" + "\x0f" + 2 * "\x00"
83                 data += 2* "\xff" + 2* "\x00"
84         #       self.send("255.255.255.255",63322, data)
85                 self.recv(None)
86
87         def passwd(self, dest, oldpass, newpass):
88                 reserved = "\x00"
89                 cmd = "\x00\x0a"
90
91                 data = self.baseudp(cmd, destmac=dest, ctype=0x0103)
92                 data += struct.pack(">h", len(oldpass))
93                 data += oldpass
94                 data += struct.pack(">h", 9)
95                 data += struct.pack(">h", len(newpass))
96                 data += newpass
97                 data += 2* "\xff" + 2* "\x00"
98                 self.send("255.255.255.255",63322, data)
99                 time.sleep(0.2)
100                 self.recv(None)
101
102 g = gs105e('',63321)
103 #g.discover()
104 g.passwd(0xe091f5936b94, 'kat', 'hemmeligt')