def get_sock_diag(hdr, payload_parser): con = netlink.new_sock_diag() con.send(hdr) msgs = [] goout = False while True: d = con.recv(65533) b = cStringIO.StringIO(d) while True: if b.tell() >= len(d): break msg = netlink.parse_nlmsg(b) if msg["type"] == netlink.DONE: goout = True break elif msg["type"] == netlink.ERROR: raise ValueError(msg) mlen = b.tell() - 16 + msg["len"] payload = payload_parser(b) attrs = netlink.parse_attrs(b, mlen) msgs.append({"msg": msg, "payload": payload, "attrs": attrs}) if goout: break b.close() return msgs
def getaddr_inet(): con = netlink.new_route() payload = netlink.route_getaddr(netlink.AF_INET, 0x12345) con.send(payload) msgs = [] goout = False while True: d = con.recv(65533) b = cStringIO.StringIO(d) while True: if b.tell() >= len(d): break msg = netlink.parse_nlmsg(b) if msg["type"] == netlink.DONE: goout = True break elif msg["type"] == netlink.ERROR: raise ValueError(msg) mlen = b.tell() - 16 + msg["len"] payload = netlink.parse_ifaddr(b) attrs = netlink.parse_attrs(b, mlen) msgs.append({ "msg": msg, "payload": payload, "attrs": attrs }) if goout: break b.close() return msgs
def getaddr_inet(): con = netlink.new_route() payload = netlink.route_getaddr(netlink.AF_INET, 0x12345) con.send(payload) msgs = [] goout = False while True: d = con.recv(65533) b = cStringIO.StringIO(d) while True: if b.tell() >= len(d): break msg = netlink.parse_nlmsg(b) if msg["type"] == netlink.DONE: goout = True break elif msg["type"] == netlink.ERROR: raise ValueError(msg) mlen = b.tell() - 16 + msg["len"] payload = netlink.parse_ifaddr(b) attrs = netlink.parse_attrs(b, mlen) msgs.append({"msg": msg, "payload": payload, "attrs": attrs}) if goout: break b.close() return msgs
def get_ifindex(index): con = netlink.new_route() con.send(netlink.route_getlink(index, 0x20141015)) d = con.recv(4096) b = cStringIO.StringIO(d) msg = netlink.parse_nlmsg(b) payload = netlink.parse_ifinfo(b) attrs = netlink.parse_attrs(b, len(d)) con.close() b.close() return {"msg": msg, "payload": payload, "attrs": attrs}
def get_ifindex(index): con = netlink.new_route() con.send(netlink.route_getlink(index, 0x20141015)) d = con.recv(4096) b = cStringIO.StringIO(d) msg = netlink.parse_nlmsg(b) payload = netlink.parse_ifinfo(b) attrs = netlink.parse_attrs(b, len(d)) con.close() b.close() return { "msg": msg, "payload": payload, "attrs": attrs }
def get_iface(): con = netlink.new_generic() hdr = netlink.new_genlmsg( { "cmd": netlink.NL80211_CMD_GET_SCAN, "version": 0, "reserved": 0 } ) attr = netlink.newnlattr(netlink.NL80211_ATTR_IFINDEX, netlink.new_policy_u32(3)) payload = netlink.generic_wireless(hdr+attr, 0x12345) con.send(payload) d = con.recv(4096) b = cStringIO.StringIO(d) msg = netlink.parse_nlmsg(b) mlen = b.tell() - 16 + msg["len"] payload = netlink.parse_genlmsg(b) attrs = netlink.parse_attrs(b, mlen) return { "msg": msg, "payload": payload, "attrs": attrs }
def get_family(): con = netlink.new_generic() hdr = netlink.new_genlmsg( { "cmd": netlink.CTRL_CMD_GETFAMILY, "version": 0, "reserved": 0 } ) payload = netlink.generic_id_ctrl(hdr,0x12345) con.send(payload) msgs = [] goout = False while True: d = con.recv(65533) b = cStringIO.StringIO(d) while True: if b.tell() >= len(d): break msg = netlink.parse_nlmsg(b) if msg["type"] == netlink.DONE: goout = True break elif msg["type"] == netlink.ERROR: raise ValueError(msg) mlen = b.tell() - 16 + msg["len"] payload = netlink.parse_genlmsg(b) attrs = netlink.parse_attrs(b, mlen) msgs.append({ "msg": msg, "payload": payload, "attrs": attrs }) if goout: break b.close() return msgs