Clean up some of the pack interface, especially attributes. Remove OrderedDict.
This commit is contained in:
45
nl80211.py
45
nl80211.py
@@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/python2.7
|
#!/usr/bin/python2.7
|
||||||
|
|
||||||
import collections
|
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import socket
|
import socket
|
||||||
@@ -67,7 +66,7 @@ class StructParser(struct.Struct):
|
|||||||
assert self.size == targetlen, 'Actual bytes: %d, expected bytes: %d' % (targetlen, self.size)
|
assert self.size == targetlen, 'Actual bytes: %d, expected bytes: %d' % (targetlen, self.size)
|
||||||
values = self.unpack_from(iterator.data, iterator.offset)
|
values = self.unpack_from(iterator.data, iterator.offset)
|
||||||
iterator.Advance(self.size)
|
iterator.Advance(self.size)
|
||||||
return collections.OrderedDict(zip(self._fields, values))
|
return dict(zip(self._fields, values))
|
||||||
|
|
||||||
def Pack(self, accumulator, **values):
|
def Pack(self, accumulator, **values):
|
||||||
ordered_values = []
|
ordered_values = []
|
||||||
@@ -120,10 +119,10 @@ class Attribute(object):
|
|||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def Pack(self, accumulator, attrtype, **attrargs):
|
def Pack(self, accumulator, attrtype, value):
|
||||||
sub_parser = self._attributes[attrtype][1]
|
sub_parser = self._attributes[attrtype][1]
|
||||||
sub_accumulator = Accumulator()
|
sub_accumulator = Accumulator()
|
||||||
sub_parser.Pack(sub_accumulator, **attrargs)
|
sub_parser.Pack(sub_accumulator, value)
|
||||||
self._nlattr.Pack(accumulator, len=self._nlattr.size + len(sub_accumulator), type=attrtype)
|
self._nlattr.Pack(accumulator, len=self._nlattr.size + len(sub_accumulator), type=attrtype)
|
||||||
accumulator.Append(str(sub_accumulator))
|
accumulator.Append(str(sub_accumulator))
|
||||||
|
|
||||||
@@ -131,18 +130,21 @@ class Attribute(object):
|
|||||||
class Attributes(object):
|
class Attributes(object):
|
||||||
def __init__(self, attributes):
|
def __init__(self, attributes):
|
||||||
super(Attributes, self).__init__()
|
super(Attributes, self).__init__()
|
||||||
|
self._attribute_idx = dict((v[0], k) for k, v in attributes.iteritems())
|
||||||
self._attribute = Attribute(attributes)
|
self._attribute = Attribute(attributes)
|
||||||
|
|
||||||
def Unpack(self, iterator, targetlen=None):
|
def Unpack(self, iterator, targetlen=None):
|
||||||
if targetlen is not None:
|
if targetlen is not None:
|
||||||
iterator = Iterator(iterator.Extract(targetlen))
|
iterator = Iterator(iterator.Extract(targetlen))
|
||||||
ret = collections.OrderedDict()
|
ret = {}
|
||||||
while not iterator.AtEnd():
|
while not iterator.AtEnd():
|
||||||
ret.update(self._attribute.Unpack(iterator))
|
ret.update(self._attribute.Unpack(iterator))
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def Pack(self, accumulator, attrtype, **attrargs):
|
def Pack(self, accumulator, **attrs):
|
||||||
return self._attribute.Pack(accumulator, attrtype, **attrargs)
|
for name, value in attrs.iteritems():
|
||||||
|
print 'name %s, value %s' % (name, value)
|
||||||
|
self._attribute.Pack(accumulator, self._attribute_idx[name], value)
|
||||||
|
|
||||||
|
|
||||||
flag = EmptyParser()
|
flag = EmptyParser()
|
||||||
@@ -217,29 +219,28 @@ STA_FLAG_AUTHENTICATED = 1 << 4
|
|||||||
STA_FLAG_TDLS_PEER = 1 << 5
|
STA_FLAG_TDLS_PEER = 1 << 5
|
||||||
STA_FLAG_ASSOCIATED = 1 << 6
|
STA_FLAG_ASSOCIATED = 1 << 6
|
||||||
|
|
||||||
ATTR_IFINDEX = 3
|
|
||||||
|
|
||||||
|
int_genquery = Accumulator()
|
||||||
genquery = Accumulator()
|
|
||||||
nlmsghdr.Pack(
|
|
||||||
genquery,
|
|
||||||
length=28, # XXX
|
|
||||||
type=20, # XXX
|
|
||||||
flags=F_REQUEST | F_ACK | F_DUMP,
|
|
||||||
seq=random.randint(0, 2 ** 32 - 1),
|
|
||||||
pid=os.getpid())
|
|
||||||
genlmsghdr.Pack(
|
genlmsghdr.Pack(
|
||||||
genquery,
|
int_genquery,
|
||||||
cmd=CMD_GET_STATION,
|
cmd=CMD_GET_STATION,
|
||||||
version=0,
|
version=0,
|
||||||
reserved=0)
|
reserved=0)
|
||||||
nl80211_attr.Pack(
|
nl80211_attr.Pack(
|
||||||
genquery,
|
int_genquery,
|
||||||
attrtype=ATTR_IFINDEX,
|
ifindex=6)
|
||||||
value=6)
|
genquery = Accumulator()
|
||||||
|
nlmsghdr.Pack(
|
||||||
|
genquery,
|
||||||
|
length=nlmsghdr.size + len(int_genquery),
|
||||||
|
type=20, # XXX
|
||||||
|
flags=F_REQUEST | F_ACK | F_DUMP,
|
||||||
|
seq=random.randint(0, 2 ** 32 - 1),
|
||||||
|
pid=os.getpid())
|
||||||
|
genquery.Append(str(int_genquery))
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_NETLINK, socket.SOCK_DGRAM, 16)
|
sock = socket.socket(socket.AF_NETLINK, socket.SOCK_DGRAM, 16)
|
||||||
sock.bind((0,0))
|
sock.bind((0, 0))
|
||||||
sock.send(str(genquery))
|
sock.send(str(genquery))
|
||||||
data = sock.recv(4096)
|
data = sock.recv(4096)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user