|
48 | 48 | except ImportError:
|
49 | 49 | has_winreg = False
|
50 | 50 |
|
| 51 | +if sys.version_info[0] < 3: |
| 52 | + is_bytes = lambda obj: issubclass(obj.__class__, str) |
| 53 | + bytes = lambda *args: str(*args[:1]) |
| 54 | + NULL_BYTE = '\x00' |
| 55 | +else: |
| 56 | + is_bytes = lambda obj: issubclass(obj.__class__, bytes) |
| 57 | + str = lambda x: __builtins__['str'](x, 'UTF-8') |
| 58 | + NULL_BYTE = bytes('\x00', 'UTF-8') |
| 59 | + long = int |
| 60 | + |
51 | 61 | if has_ctypes:
|
52 | 62 | #
|
53 | 63 | # Windows Structures
|
@@ -503,6 +513,40 @@ def get_stat_buffer(path):
|
503 | 513 | return st_buf
|
504 | 514 |
|
505 | 515 | def netlink_request(req_type):
|
| 516 | + import select |
| 517 | + # See RFC 3549 |
| 518 | + NLM_F_REQUEST = 0x0001 |
| 519 | + NLM_F_ROOT = 0x0100 |
| 520 | + NLMSG_ERROR = 0x0002 |
| 521 | + NLMSG_DONE = 0x0003 |
| 522 | + |
| 523 | + sock = socket.socket(socket.AF_NETLINK, socket.SOCK_RAW, socket.NETLINK_ROUTE) |
| 524 | + sock.bind((os.getpid(), 0)) |
| 525 | + seq = int(time.time()) |
| 526 | + nlmsg = struct.pack('IHHIIB15x', 32, req_type, (NLM_F_REQUEST | NLM_F_ROOT), seq, 0, socket.AF_UNSPEC) |
| 527 | + sock.send(nlmsg) |
| 528 | + responses = [] |
| 529 | + if not len(select.select([sock.fileno()], [], [], 0.5)[0]): |
| 530 | + return responses |
| 531 | + raw_response_data = sock.recv(0xfffff) |
| 532 | + response = cstruct_unpack(NLMSGHDR, raw_response_data[:ctypes.sizeof(NLMSGHDR)]) |
| 533 | + raw_response_data = raw_response_data[ctypes.sizeof(NLMSGHDR):] |
| 534 | + while response.type != NLMSG_DONE: |
| 535 | + if response.type == NLMSG_ERROR: |
| 536 | + break |
| 537 | + response_data = raw_response_data[:(response.len - 16)] |
| 538 | + responses.append(response_data) |
| 539 | + raw_response_data = raw_response_data[len(response_data):] |
| 540 | + if not len(raw_response_data): |
| 541 | + if not len(select.select([sock.fileno()], [], [], 0.5)[0]): |
| 542 | + break |
| 543 | + raw_response_data = sock.recv(0xfffff) |
| 544 | + response = cstruct_unpack(NLMSGHDR, raw_response_data[:ctypes.sizeof(NLMSGHDR)]) |
| 545 | + raw_response_data = raw_response_data[ctypes.sizeof(NLMSGHDR):] |
| 546 | + sock.close() |
| 547 | + return responses |
| 548 | + |
| 549 | +def _netlink_request(req_type): |
506 | 550 | # See RFC 3549
|
507 | 551 | NLM_F_REQUEST = 0x0001
|
508 | 552 | NLM_F_ROOT = 0x0100
|
@@ -699,9 +743,8 @@ def stdapi_sys_process_get_processes_via_proc(request, response):
|
699 | 743 | cmd = open(os.path.join('/proc', pid, 'cmdline'), 'rb').read(512).replace('\x00', ' ')
|
700 | 744 | status_data = open(os.path.join('/proc', pid, 'status'), 'rb').read()
|
701 | 745 | status_data = map(lambda x: x.split('\t',1), status_data.split('\n'))
|
702 |
| - status_data = filter(lambda x: len(x) == 2, status_data) |
703 | 746 | status = {}
|
704 |
| - for k, v in status_data: |
| 747 | + for k, v in filter(lambda x: len(x) == 2, status_data): |
705 | 748 | status[k[:-1]] = v.strip()
|
706 | 749 | ppid = status.get('PPid')
|
707 | 750 | uid = status.get('Uid').split('\t', 1)[0]
|
@@ -974,7 +1017,7 @@ def stdapi_net_config_get_interfaces(request, response):
|
974 | 1017 | else:
|
975 | 1018 | return ERROR_FAILURE, response
|
976 | 1019 | for iface_info in interfaces:
|
977 |
| - iface_tlv = '' |
| 1020 | + iface_tlv = bytes() |
978 | 1021 | iface_tlv += tlv_pack(TLV_TYPE_MAC_NAME, iface_info.get('name', 'Unknown'))
|
979 | 1022 | iface_tlv += tlv_pack(TLV_TYPE_MAC_ADDRESS, iface_info.get('hw_addr', '\x00\x00\x00\x00\x00\x00'))
|
980 | 1023 | if 'mtu' in iface_info:
|
@@ -1002,7 +1045,7 @@ def stdapi_net_config_get_interfaces_via_netlink():
|
1002 | 1045 | 0x0100: 'PROMISC',
|
1003 | 1046 | 0x1000: 'MULTICAST'
|
1004 | 1047 | }
|
1005 |
| - iface_flags_sorted = iface_flags.keys() |
| 1048 | + iface_flags_sorted = list(iface_flags.keys()) |
1006 | 1049 | # Dictionaries don't maintain order
|
1007 | 1050 | iface_flags_sorted.sort()
|
1008 | 1051 | interfaces = {}
|
@@ -1106,7 +1149,7 @@ def stdapi_net_config_get_interfaces_via_osxsc():
|
1106 | 1149 | hw_addr = hw_addr.replace(':', '')
|
1107 | 1150 | hw_addr = hw_addr.decode('hex')
|
1108 | 1151 | iface_info['hw_addr'] = hw_addr
|
1109 |
| - ifnames = interfaces.keys() |
| 1152 | + ifnames = list(interfaces.keys()) |
1110 | 1153 | ifnames.sort()
|
1111 | 1154 | for iface_name, iface_info in interfaces.items():
|
1112 | 1155 | iface_info['index'] = ifnames.index(iface_name)
|
|
0 commit comments