Multi-message parsing in a generic netlink interface.

This commit is contained in:
Ian Gulliver
2015-01-09 06:21:00 +00:00
parent 9b6894b999
commit f4c6a7bff2

View File

@@ -241,16 +241,18 @@ ctrl_attr = Attributes({
7: ('mcast_groups', Array(mcast_grp_attr)), 7: ('mcast_groups', Array(mcast_grp_attr)),
}) })
F_REQUEST = 1 << 0 NLMSG_F_REQUEST = 1 << 0
F_MULTI = 1 << 1 NLMSG_F_MULTI = 1 << 1
F_ACK = 1 << 2 NLMSG_F_ACK = 1 << 2
F_ECHO = 1 << 3 NLMSG_F_ECHO = 1 << 3
F_DUMP_INTR = 1 << 4 NLMSG_F_DUMP_INTR = 1 << 4
F_ROOT = 1 << 8 NLMSG_F_ROOT = 1 << 8
F_MATCH = 1 << 9 NLMSG_F_MATCH = 1 << 9
F_ATOMIC = 1 << 10 NLMSG_F_ATOMIC = 1 << 10
F_DUMP = F_ROOT | F_MATCH NLMSG_F_DUMP = NLMSG_F_ROOT | NLMSG_F_MATCH
NLMSG_DONE = 3
CMD_GET_STATION = 17 CMD_GET_STATION = 17
@@ -272,13 +274,17 @@ class Connection(object):
self._sock.send(msg) self._sock.send(msg)
def RecvAndUnpack(self): def RecvAndUnpack(self):
data = self._sock.recv(4096) ret = []
iterator = Iterator(data) while True:
myhdr = nlmsghdr.Unpack(iterator) data = self._sock.recv(4096)
print 'nlmsghdr: %s' % myhdr iterator = Iterator(data)
int_iterator = iterator.ExtractIterator(myhdr['length'] - nlmsghdr.size) while not iterator.AtEnd():
print 'genlmsghdr: %s' % genlmsghdr.Unpack(int_iterator) myhdr = nlmsghdr.Unpack(iterator)
print 'ctrl_attr: %s' % ctrl_attr.Unpack(int_iterator) if myhdr['type'] == NLMSG_DONE:
return ret
ret.append(iterator.ExtractIterator(myhdr['length'] - nlmsghdr.size))
if not myhdr['flags'] & NLMSG_F_MULTI:
return ret
#int_genquery = Accumulator() #int_genquery = Accumulator()
@@ -311,4 +317,4 @@ class Connection(object):
query = '\24\0\0\0\20\0\5\3a\6\256T\v\17\0\0\3\1\0\0' query = '\24\0\0\0\20\0\5\3a\6\256T\v\17\0\0\3\1\0\0'
conn = Connection() conn = Connection()
conn.Send(query) conn.Send(query)
conn.RecvAndUnpack() print ['%s\n' % s for s in conn.RecvAndUnpack()]