-
Notifications
You must be signed in to change notification settings - Fork 39.1k
test: support get_bind_addrs and feature_bind_extra on macOS & BSD
#34256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| # Copyright (c) 2014-present The Bitcoin Core developers | ||
| # Distributed under the MIT software license, see the accompanying | ||
| # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
| """Linux network utilities. | ||
| """Linux, macOS, and BSD network utilities. | ||
|
|
||
| Roughly based on https://web.archive.org/web/20190424172231/http://voorloopnul.com/blog/a-python-netstat-in-less-than-100-lines-of-code/ by Ricardo Pascal | ||
| """ | ||
|
|
@@ -88,40 +88,70 @@ def get_bind_addrs(pid): | |
| ''' | ||
| Get bind addresses as (host,port) tuples for process pid. | ||
| ''' | ||
| inodes = get_socket_inodes(pid) | ||
| bind_addrs = [] | ||
| for conn in netstat('tcp') + netstat('tcp6'): | ||
| if conn[3] == STATE_LISTEN and conn[4] in inodes: | ||
| bind_addrs.append(conn[1]) | ||
| return bind_addrs | ||
|
|
||
| # from: https://code.activestate.com/recipes/439093/ | ||
| if sys.platform == 'linux': | ||
| inodes = get_socket_inodes(pid) | ||
| bind_addrs = [] | ||
| for conn in netstat('tcp') + netstat('tcp6'): | ||
| if conn[3] == STATE_LISTEN and conn[4] in inodes: | ||
| bind_addrs.append(conn[1]) | ||
| return bind_addrs | ||
| elif sys.platform.startswith(("darwin", "freebsd", "netbsd", "openbsd")): | ||
| import re | ||
| import subprocess | ||
| output = subprocess.check_output(["lsof", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OpenBSD does not provide the |
||
| *(["-Di"] if sys.platform.startswith("freebsd") else []), # Ignore device cache to avoid stderr warnings. | ||
| "-nP", # Keep hosts and ports numeric. | ||
| "-a", # Require all filters to match. | ||
| "-p", str(pid), # Limit results to the target pid. | ||
| "-iTCP", # Only inspect TCP sockets. | ||
| "-sTCP:LISTEN", # Only keep listening sockets. | ||
| "-Ftn", # Emit machine-readable type and name fields. | ||
| ], text=True) | ||
| return [ | ||
| (addr_to_hex(("::" if sock_type == "IPv6" else "0.0.0.0") if host == "*" else host.strip("[]")), int(port)) | ||
| for sock_type, host, port in re.findall(r"t(IPv[46])\nn(\*|\[.+?]|[^:]+):(\d+)", output) | ||
|
l0rinc marked this conversation as resolved.
l0rinc marked this conversation as resolved.
|
||
| ] | ||
| else: | ||
| raise NotImplementedError(f"get_bind_addrs is not supported on {sys.platform}") | ||
|
|
||
| def all_interfaces(): | ||
| ''' | ||
| Return all interfaces that are up | ||
| Return all IPv4 interfaces that are up. | ||
| ''' | ||
| import fcntl # Linux only, so only import when required | ||
|
|
||
| is_64bits = sys.maxsize > 2**32 | ||
| struct_size = 40 if is_64bits else 32 | ||
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
| max_possible = 8 # initial value | ||
| while True: | ||
| bytes = max_possible * struct_size | ||
| names = array.array('B', b'\0' * bytes) | ||
| outbytes = struct.unpack('iL', fcntl.ioctl( | ||
| s.fileno(), | ||
| 0x8912, # SIOCGIFCONF | ||
| struct.pack('iL', bytes, names.buffer_info()[0]) | ||
| ))[0] | ||
| if outbytes == bytes: | ||
| max_possible *= 2 | ||
| else: | ||
| break | ||
| namestr = names.tobytes() | ||
| return [(namestr[i:i+16].split(b'\0', 1)[0], | ||
| socket.inet_ntoa(namestr[i+20:i+24])) | ||
| for i in range(0, outbytes, struct_size)] | ||
| if sys.platform == 'linux': | ||
| import fcntl # Linux only, so only import when required | ||
|
|
||
| is_64bits = sys.maxsize > 2**32 | ||
| struct_size = 40 if is_64bits else 32 | ||
| s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
| max_possible = 8 # initial value | ||
| while True: | ||
| bytes = max_possible * struct_size | ||
| names = array.array('B', b'\0' * bytes) | ||
| outbytes = struct.unpack('iL', fcntl.ioctl( | ||
| s.fileno(), | ||
| 0x8912, # SIOCGIFCONF | ||
| struct.pack('iL', bytes, names.buffer_info()[0]) | ||
| ))[0] | ||
| if outbytes == bytes: | ||
| max_possible *= 2 | ||
| else: | ||
| break | ||
| namestr = names.tobytes() | ||
| return [(namestr[i:i+16].split(b'\0', 1)[0], | ||
| socket.inet_ntoa(namestr[i+20:i+24])) | ||
| for i in range(0, outbytes, struct_size)] | ||
| elif sys.platform.startswith(("darwin", "freebsd", "netbsd", "openbsd")): | ||
| import re | ||
| import subprocess | ||
| output = subprocess.check_output(["ifconfig", "-au"], text=True) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OpenBSD's |
||
| return [ | ||
| (m["iface"].encode(), ip) | ||
| for m in re.finditer(r"(?m)^(?P<iface>\S+):(?P<block>[^\n]*(?:\n[ \t]+[^\n]*)*)", output) | ||
| for ip in re.findall(r"inet (\S+)", m["block"]) | ||
|
l0rinc marked this conversation as resolved.
Comment on lines
+150
to
+151
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree it's a (slight) shame about all the regex needed, but don't know a better way to do it either without a large rewrite to |
||
| ] | ||
| else: | ||
| raise NotImplementedError(f"all_interfaces is not supported on {sys.platform}") | ||
|
l0rinc marked this conversation as resolved.
|
||
|
|
||
| def addr_to_hex(addr): | ||
| ''' | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.